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 VideoCaptureDeviceMac* capture_device = |
| 33 new VideoCaptureDeviceMac(device_name); |
| 34 |
| 35 // TODO(mcasas): The following check might not be necessary; if the device has |
| 36 // disappeared after enumeration and before coming here, opening would just |
| 37 // fail but not necessarily produce a crash. |
| 38 VideoCaptureDevice::Names device_names; |
| 39 GetDeviceNames(&device_names); |
| 40 VideoCaptureDevice::Names::const_iterator it = device_names.begin(); |
| 41 for (; it != device_names.end(); ++it) { |
| 42 if (it->id() == device_name.id()) |
| 43 break; |
| 44 } |
| 45 if (it == device_names.end()) |
| 46 return scoped_ptr<VideoCaptureDevice>(); |
| 47 DCHECK_NE(it->capture_api_type(), VideoCaptureDevice::Name::API_TYPE_UNKNOWN); |
| 48 |
| 49 if (!capture_device->Init(it->capture_api_type())) { |
| 50 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; |
| 51 delete capture_device; |
| 52 capture_device = NULL; |
| 53 } |
| 54 return scoped_ptr<VideoCaptureDevice>(capture_device); |
| 55 } |
| 56 |
| 57 void VideoCaptureDeviceFactoryMac::GetDeviceNames( |
| 58 VideoCaptureDevice::Names* const device_names) { |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 // Loop through all available devices and add to |device_names|. |
| 61 NSDictionary* capture_devices; |
| 62 if (AVFoundationGlue::IsAVFoundationSupported()) { |
| 63 bool is_any_device_blacklisted = false; |
| 64 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; |
| 65 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; |
| 66 std::string device_vid; |
| 67 // Enumerate all devices found by AVFoundation, translate the info for each |
| 68 // to class Name and add it to |device_names|. |
| 69 for (NSString* key in capture_devices) { |
| 70 VideoCaptureDevice::Name name( |
| 71 [[capture_devices valueForKey:key] UTF8String], |
| 72 [key UTF8String], VideoCaptureDevice::Name::AVFOUNDATION); |
| 73 device_names->push_back(name); |
| 74 // Extract the device's Vendor ID and compare to all blacklisted ones. |
| 75 device_vid = name.GetModel().substr(0, kVidPidSize); |
| 76 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
| 77 is_any_device_blacklisted |= |
| 78 !strcasecmp(device_vid.c_str(), kBlacklistedCameras[i].vid); |
| 79 if (is_any_device_blacklisted) |
| 80 break; |
| 81 } |
| 82 } |
| 83 // If there is any device blacklisted in the system, walk the QTKit device |
| 84 // list and add those devices with a blacklisted name to the |device_names|. |
| 85 // AVFoundation and QTKit device lists partially overlap, so add a "QTKit" |
| 86 // prefix to the latter ones to distinguish them from the AVFoundation ones. |
| 87 if (is_any_device_blacklisted) { |
| 88 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
| 89 for (NSString* key in capture_devices) { |
| 90 NSString* device_name = [capture_devices valueForKey:key]; |
| 91 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
| 92 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name) |
| 93 options:NSCaseInsensitiveSearch].length != 0) { |
| 94 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String]; |
| 95 VideoCaptureDevice::Name name( |
| 96 "QTKit " + std::string([device_name UTF8String]), |
| 97 [key UTF8String], VideoCaptureDevice::Name::QTKIT); |
| 98 device_names->push_back(name); |
| 99 } |
| 100 } |
| 101 } |
| 102 } |
| 103 } else { |
| 104 DVLOG(1) << "Enumerating video capture devices using QTKit"; |
| 105 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
| 106 for (NSString* key in capture_devices) { |
| 107 VideoCaptureDevice::Name name( |
| 108 [[capture_devices valueForKey:key] UTF8String], |
| 109 [key UTF8String], VideoCaptureDevice::Name::QTKIT); |
| 110 device_names->push_back(name); |
| 111 } |
| 112 } |
| 113 } |
| 114 |
| 115 void VideoCaptureDeviceFactoryMac::GetDeviceSupportedFormats( |
| 116 const VideoCaptureDevice::Name& device, |
| 117 VideoCaptureFormats* supported_formats) { |
| 118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 119 if (device.capture_api_type() == VideoCaptureDevice::Name::AVFOUNDATION) { |
| 120 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; |
| 121 [VideoCaptureDeviceAVFoundation getDevice:device |
| 122 supportedFormats:supported_formats]; |
| 123 } else { |
| 124 NOTIMPLEMENTED(); |
| 125 } |
| 126 } |
| 127 |
| 128 } // namespace media |
OLD | NEW |