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.
5 #include "media/video/capture/mac/video_capture_device_factory_mac.h"
7 #import "media/video/capture/mac/avfoundation_glue.h"
8 #include "media/video/capture/mac/video_capture_device_mac.h"
9 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h"
10 #import "media/video/capture/mac/video_capture_device_qtkit_mac.h"
14 // Some devices are not correctly supported in AVFoundation, f.i. Blackmagic,
15 // see http://crbug.com/347371. The devices are identified by USB Vendor ID and
16 // by a characteristic substring of the name, usually the vendor's name.
17 const struct NameAndVid {
20 } kBlacklistedCameras[] = { { "a82c", "Blackmagic" } };
22 // In device identifiers, the USB VID and PID are stored in 4 bytes each.
23 const size_t kVidPidSize = 4;
25 VideoCaptureDeviceFactoryMac::VideoCaptureDeviceFactoryMac() {
26 thread_checker_.DetachFromThread();
29 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryMac::Create(
30 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
31 const VideoCaptureDevice::Name& device_name) {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 DCHECK_NE(device_name.capture_api_type(),
34 VideoCaptureDevice::Name::API_TYPE_UNKNOWN);
36 VideoCaptureDevice::Names device_names;
37 GetDeviceNames(&device_names);
38 VideoCaptureDevice::Names::iterator it = device_names.begin();
39 for (; it != device_names.end(); ++it) {
40 if (it->id() == device_name.id())
43 if (it == device_names.end())
44 return scoped_ptr<VideoCaptureDevice>();
46 scoped_ptr<VideoCaptureDeviceMac> capture_device(
47 new VideoCaptureDeviceMac(device_name));
48 if (!capture_device->Init(device_name.capture_api_type())) {
49 LOG(ERROR) << "Could not initialize VideoCaptureDevice.";
50 capture_device.reset();
52 return scoped_ptr<VideoCaptureDevice>(capture_device.Pass());
55 void VideoCaptureDeviceFactoryMac::GetDeviceNames(
56 VideoCaptureDevice::Names* const device_names) {
57 DCHECK(thread_checker_.CalledOnValidThread());
58 // Loop through all available devices and add to |device_names|.
59 NSDictionary* capture_devices;
60 if (AVFoundationGlue::IsAVFoundationSupported()) {
61 bool is_any_device_blacklisted = false;
62 DVLOG(1) << "Enumerating video capture devices using AVFoundation";
63 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames];
64 std::string device_vid;
65 // Enumerate all devices found by AVFoundation, translate the info for each
66 // to class Name and add it to |device_names|.
67 for (NSString* key in capture_devices) {
68 VideoCaptureDevice::Name name(
69 [[capture_devices valueForKey:key] UTF8String],
70 [key UTF8String], VideoCaptureDevice::Name::AVFOUNDATION);
71 device_names->push_back(name);
72 // Extract the device's Vendor ID and compare to all blacklisted ones.
73 device_vid = name.GetModel().substr(0, kVidPidSize);
74 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) {
75 is_any_device_blacklisted |=
76 !strcasecmp(device_vid.c_str(), kBlacklistedCameras[i].vid);
77 if (is_any_device_blacklisted)
81 // If there is any device blacklisted in the system, walk the QTKit device
82 // list and add those devices with a blacklisted name to the |device_names|.
83 // AVFoundation and QTKit device lists partially overlap, so add a "QTKit"
84 // prefix to the latter ones to distinguish them from the AVFoundation ones.
85 if (is_any_device_blacklisted) {
86 capture_devices = [VideoCaptureDeviceQTKit deviceNames];
87 for (NSString* key in capture_devices) {
88 NSString* device_name = [capture_devices valueForKey:key];
89 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) {
90 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name)
91 options:NSCaseInsensitiveSearch].length != 0) {
92 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String];
93 VideoCaptureDevice::Name name(
94 "QTKit " + std::string([device_name UTF8String]),
95 [key UTF8String], VideoCaptureDevice::Name::QTKIT);
96 device_names->push_back(name);
102 DVLOG(1) << "Enumerating video capture devices using QTKit";
103 capture_devices = [VideoCaptureDeviceQTKit deviceNames];
104 for (NSString* key in capture_devices) {
105 VideoCaptureDevice::Name name(
106 [[capture_devices valueForKey:key] UTF8String],
107 [key UTF8String], VideoCaptureDevice::Name::QTKIT);
108 device_names->push_back(name);
113 void VideoCaptureDeviceFactoryMac::GetDeviceSupportedFormats(
114 const VideoCaptureDevice::Name& device,
115 VideoCaptureFormats* supported_formats) {
116 DCHECK(thread_checker_.CalledOnValidThread());
117 if (device.capture_api_type() == VideoCaptureDevice::Name::AVFOUNDATION) {
118 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation";
119 [VideoCaptureDeviceAVFoundation getDevice:device
120 supportedFormats:supported_formats];