| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/capture/video/mac/video_capture_device_factory_mac.h" | |
| 6 | |
| 7 #import <IOKit/audio/IOAudioTypes.h> | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/profiler/scoped_tracker.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "base/task_runner_util.h" | |
| 18 #import "media/base/mac/avfoundation_glue.h" | |
| 19 #import "media/capture/video/mac/video_capture_device_avfoundation_mac.h" | |
| 20 #import "media/capture/video/mac/video_capture_device_decklink_mac.h" | |
| 21 #include "media/capture/video/mac/video_capture_device_mac.h" | |
| 22 | |
| 23 namespace media { | |
| 24 | |
| 25 // Blacklisted devices are identified by a characteristic trailing substring of | |
| 26 // uniqueId. At the moment these are just Blackmagic devices. | |
| 27 const struct NameAndVid { | |
| 28 const char* unique_id_signature; | |
| 29 const int capture_width; | |
| 30 const int capture_height; | |
| 31 const float capture_frame_rate; | |
| 32 } kBlacklistedCameras[] = {{"-01FDA82C8A9C", 1280, 720, 60.0f}}; | |
| 33 | |
| 34 static bool IsDeviceBlacklisted(const VideoCaptureDevice::Name& name) { | |
| 35 bool is_device_blacklisted = false; | |
| 36 for(size_t i = 0; | |
| 37 !is_device_blacklisted && i < arraysize(kBlacklistedCameras); ++i) { | |
| 38 is_device_blacklisted = | |
| 39 base::EndsWith(name.id(), | |
| 40 kBlacklistedCameras[i].unique_id_signature, | |
| 41 base::CompareCase::INSENSITIVE_ASCII); | |
| 42 } | |
| 43 DVLOG_IF(2, is_device_blacklisted) << "Blacklisted camera: " << name.name() | |
| 44 << ", id: " << name.id(); | |
| 45 return is_device_blacklisted; | |
| 46 } | |
| 47 | |
| 48 VideoCaptureDeviceFactoryMac::VideoCaptureDeviceFactoryMac() { | |
| 49 thread_checker_.DetachFromThread(); | |
| 50 } | |
| 51 | |
| 52 VideoCaptureDeviceFactoryMac::~VideoCaptureDeviceFactoryMac() { | |
| 53 } | |
| 54 | |
| 55 std::unique_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryMac::Create( | |
| 56 const VideoCaptureDevice::Name& device_name) { | |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 58 DCHECK_NE(device_name.capture_api_type(), | |
| 59 VideoCaptureDevice::Name::API_TYPE_UNKNOWN); | |
| 60 | |
| 61 std::unique_ptr<VideoCaptureDevice> capture_device; | |
| 62 if (device_name.capture_api_type() == VideoCaptureDevice::Name::DECKLINK) { | |
| 63 capture_device.reset(new VideoCaptureDeviceDeckLinkMac(device_name)); | |
| 64 } else { | |
| 65 VideoCaptureDeviceMac* device = new VideoCaptureDeviceMac(device_name); | |
| 66 capture_device.reset(device); | |
| 67 if (!device->Init(device_name.capture_api_type())) { | |
| 68 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; | |
| 69 capture_device.reset(); | |
| 70 } | |
| 71 } | |
| 72 return std::unique_ptr<VideoCaptureDevice>(std::move(capture_device)); | |
| 73 } | |
| 74 | |
| 75 void VideoCaptureDeviceFactoryMac::GetDeviceNames( | |
| 76 VideoCaptureDevice::Names* device_names) { | |
| 77 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/458397 is | |
| 78 // fixed. | |
| 79 tracked_objects::ScopedTracker tracking_profile( | |
| 80 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 81 "458397 VideoCaptureDeviceFactoryMac::GetDeviceNames")); | |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 83 // Loop through all available devices and add to |device_names|. | |
| 84 NSDictionary* capture_devices; | |
| 85 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; | |
| 86 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; | |
| 87 // Enumerate all devices found by AVFoundation, translate the info for each | |
| 88 // to class Name and add it to |device_names|. | |
| 89 for (NSString* key in capture_devices) { | |
| 90 int transport_type = [[capture_devices valueForKey:key] transportType]; | |
| 91 // Transport types are defined for Audio devices and reused for video. | |
| 92 VideoCaptureDevice::Name::TransportType device_transport_type = | |
| 93 (transport_type == kIOAudioDeviceTransportTypeBuiltIn || | |
| 94 transport_type == kIOAudioDeviceTransportTypeUSB) | |
| 95 ? VideoCaptureDevice::Name::USB_OR_BUILT_IN | |
| 96 : VideoCaptureDevice::Name::OTHER_TRANSPORT; | |
| 97 VideoCaptureDevice::Name name( | |
| 98 [[[capture_devices valueForKey:key] deviceName] UTF8String], | |
| 99 [key UTF8String], VideoCaptureDevice::Name::AVFOUNDATION, | |
| 100 device_transport_type); | |
| 101 if (IsDeviceBlacklisted(name)) | |
| 102 continue; | |
| 103 device_names->push_back(name); | |
| 104 } | |
| 105 // Also retrieve Blackmagic devices, if present, via DeckLink SDK API. | |
| 106 VideoCaptureDeviceDeckLinkMac::EnumerateDevices(device_names); | |
| 107 } | |
| 108 | |
| 109 void VideoCaptureDeviceFactoryMac::EnumerateDeviceNames( | |
| 110 const base::Callback< | |
| 111 void(std::unique_ptr<media::VideoCaptureDevice::Names>)>& callback) { | |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 113 std::unique_ptr<VideoCaptureDevice::Names> device_names( | |
| 114 new VideoCaptureDevice::Names()); | |
| 115 GetDeviceNames(device_names.get()); | |
| 116 callback.Run(std::move(device_names)); | |
| 117 } | |
| 118 | |
| 119 void VideoCaptureDeviceFactoryMac::GetDeviceSupportedFormats( | |
| 120 const VideoCaptureDevice::Name& device, | |
| 121 VideoCaptureFormats* supported_formats) { | |
| 122 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 123 switch (device.capture_api_type()) { | |
| 124 case VideoCaptureDevice::Name::AVFOUNDATION: | |
| 125 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; | |
| 126 [VideoCaptureDeviceAVFoundation getDevice:device | |
| 127 supportedFormats:supported_formats]; | |
| 128 break; | |
| 129 case VideoCaptureDevice::Name::DECKLINK: | |
| 130 DVLOG(1) << "Enumerating video capture capabilities " << device.name(); | |
| 131 VideoCaptureDeviceDeckLinkMac::EnumerateDeviceCapabilities( | |
| 132 device, supported_formats); | |
| 133 break; | |
| 134 default: | |
| 135 NOTREACHED(); | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 // static | |
| 140 VideoCaptureDeviceFactory* | |
| 141 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory( | |
| 142 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { | |
| 143 return new VideoCaptureDeviceFactoryMac(); | |
| 144 } | |
| 145 | |
| 146 } // namespace media | |
| OLD | NEW |