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/video/capture/mac/video_capture_device_factory_mac.h" | |
6 | |
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" | |
11 | |
12 namespace media { | |
13 | |
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 { | |
18 const char* vid; | |
19 const char* name; | |
20 } kBlacklistedCameras[] = { { "a82c", "Blackmagic" } }; | |
21 | |
22 // In device identifiers, the USB VID and PID are stored in 4 bytes each. | |
23 const size_t kVidPidSize = 4; | |
24 | |
25 VideoCaptureDeviceFactoryMac::VideoCaptureDeviceFactoryMac() { | |
26 thread_checker_.DetachFromThread(); | |
27 } | |
28 | |
29 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryMac::Create( | |
30 const VideoCaptureDevice::Name& device_name) { | |
31 DCHECK(thread_checker_.CalledOnValidThread()); | |
32 DCHECK_NE(device_name.capture_api_type(), | |
33 VideoCaptureDevice::Name::API_TYPE_UNKNOWN); | |
34 | |
35 VideoCaptureDevice::Names device_names; | |
36 GetDeviceNames(&device_names); | |
37 VideoCaptureDevice::Names::iterator it = device_names.begin(); | |
38 for (; it != device_names.end(); ++it) { | |
39 if (it->id() == device_name.id()) | |
40 break; | |
41 } | |
42 if (it == device_names.end()) | |
43 return scoped_ptr<VideoCaptureDevice>(); | |
44 | |
45 scoped_ptr<VideoCaptureDeviceMac> capture_device( | |
46 new VideoCaptureDeviceMac(device_name)); | |
47 if (!capture_device->Init(device_name.capture_api_type())) { | |
48 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; | |
49 capture_device.reset(); | |
50 } | |
51 return scoped_ptr<VideoCaptureDevice>(capture_device.Pass()); | |
52 } | |
53 | |
54 void VideoCaptureDeviceFactoryMac::GetDeviceNames( | |
55 VideoCaptureDevice::Names* const device_names) { | |
56 DCHECK(thread_checker_.CalledOnValidThread()); | |
57 // Loop through all available devices and add to |device_names|. | |
58 NSDictionary* capture_devices; | |
59 if (AVFoundationGlue::IsAVFoundationSupported()) { | |
60 bool is_any_device_blacklisted = false; | |
61 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; | |
62 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; | |
63 std::string device_vid; | |
64 // Enumerate all devices found by AVFoundation, translate the info for each | |
65 // to class Name and add it to |device_names|. | |
66 for (NSString* key in capture_devices) { | |
67 VideoCaptureDevice::Name name( | |
68 [[capture_devices valueForKey:key] UTF8String], | |
69 [key UTF8String], VideoCaptureDevice::Name::AVFOUNDATION); | |
70 device_names->push_back(name); | |
71 // Extract the device's Vendor ID and compare to all blacklisted ones. | |
72 device_vid = name.GetModel().substr(0, kVidPidSize); | |
73 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { | |
74 is_any_device_blacklisted |= | |
75 !strcasecmp(device_vid.c_str(), kBlacklistedCameras[i].vid); | |
76 if (is_any_device_blacklisted) | |
77 break; | |
78 } | |
79 } | |
80 // If there is any device blacklisted in the system, walk the QTKit device | |
81 // list and add those devices with a blacklisted name to the |device_names|. | |
82 // AVFoundation and QTKit device lists partially overlap, so add a "QTKit" | |
83 // prefix to the latter ones to distinguish them from the AVFoundation ones. | |
84 if (is_any_device_blacklisted) { | |
85 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; | |
86 for (NSString* key in capture_devices) { | |
87 NSString* device_name = [capture_devices valueForKey:key]; | |
88 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { | |
89 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name) | |
90 options:NSCaseInsensitiveSearch].length != 0) { | |
91 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String]; | |
92 VideoCaptureDevice::Name name( | |
93 "QTKit " + std::string([device_name UTF8String]), | |
94 [key UTF8String], VideoCaptureDevice::Name::QTKIT); | |
95 device_names->push_back(name); | |
96 } | |
97 } | |
98 } | |
99 } | |
100 } else { | |
101 DVLOG(1) << "Enumerating video capture devices using QTKit"; | |
102 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; | |
103 for (NSString* key in capture_devices) { | |
104 VideoCaptureDevice::Name name( | |
105 [[capture_devices valueForKey:key] UTF8String], | |
106 [key UTF8String], VideoCaptureDevice::Name::QTKIT); | |
107 device_names->push_back(name); | |
108 } | |
109 } | |
110 } | |
111 | |
112 void VideoCaptureDeviceFactoryMac::GetDeviceSupportedFormats( | |
113 const VideoCaptureDevice::Name& device, | |
114 VideoCaptureFormats* supported_formats) { | |
115 DCHECK(thread_checker_.CalledOnValidThread()); | |
116 if (device.capture_api_type() == VideoCaptureDevice::Name::AVFOUNDATION) { | |
117 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; | |
118 [VideoCaptureDeviceAVFoundation getDevice:device | |
119 supportedFormats:supported_formats]; | |
120 } else { | |
121 NOTIMPLEMENTED(); | |
122 } | |
123 } | |
124 | |
125 } // namespace media | |
OLD | NEW |