OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/video/capture/mac/video_capture_device_mac.h" | 5 #include "media/video/capture/mac/video_capture_device_mac.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #import "media/video/capture/mac/avfoundation_glue.h" | 12 #import "media/video/capture/mac/avfoundation_glue.h" |
13 #import "media/video/capture/mac/platform_video_capturing_mac.h" | 13 #import "media/video/capture/mac/platform_video_capturing_mac.h" |
14 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" | 14 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" |
15 #import "media/video/capture/mac/video_capture_device_qtkit_mac.h" | 15 #import "media/video/capture/mac/video_capture_device_qtkit_mac.h" |
16 | 16 |
17 namespace media { | 17 namespace media { |
18 | 18 |
19 const int kMinFrameRate = 1; | 19 const int kMinFrameRate = 1; |
20 const int kMaxFrameRate = 30; | 20 const int kMaxFrameRate = 30; |
21 | 21 |
22 // In QT device identifiers, the USB VID and PID are stored in 4 bytes each. | 22 // In device identifiers, the USB VID and PID are stored in 4 bytes each. |
23 const size_t kVidPidSize = 4; | 23 const size_t kVidPidSize = 4; |
24 | 24 |
25 struct Resolution { | 25 // Some devices are not correctly supported in AVFoundation, f.i. Blackmagic, |
26 int width; | 26 // see http://crbug.com/347371. The devices are identified by USB Vendor ID and |
27 int height; | 27 // by a characteristic substring of the name, usually the vendor's name. |
28 }; | 28 const struct NameAndVid { |
| 29 const char* vid; |
| 30 const char* name; |
| 31 } kBlacklistedCameras[] = { { "a82c", "Blackmagic" } }; |
29 | 32 |
30 const Resolution kQVGA = { 320, 240 }, | 33 const struct Resolution { |
31 kVGA = { 640, 480 }, | 34 const int width; |
32 kHD = { 1280, 720 }; | 35 const int height; |
| 36 } kQVGA = { 320, 240 }, |
| 37 kVGA = { 640, 480 }, |
| 38 kHD = { 1280, 720 }; |
33 | 39 |
34 const Resolution* const kWellSupportedResolutions[] = { | 40 const struct Resolution* const kWellSupportedResolutions[] = { |
35 &kQVGA, | 41 &kQVGA, |
36 &kVGA, | 42 &kVGA, |
37 &kHD, | 43 &kHD, |
38 }; | 44 }; |
39 | 45 |
40 // Rescaling the image to fix the pixel aspect ratio runs the risk of making | 46 // Rescaling the image to fix the pixel aspect ratio runs the risk of making |
41 // the aspect ratio worse, if QTKit selects a new source mode with a different | 47 // the aspect ratio worse, if QTKit selects a new source mode with a different |
42 // shape. This constant ensures that we don't take this risk if the current | 48 // shape. This constant ensures that we don't take this risk if the current |
43 // aspect ratio is tolerable. | 49 // aspect ratio is tolerable. |
44 const float kMaxPixelAspectRatio = 1.15; | 50 const float kMaxPixelAspectRatio = 1.15; |
45 | 51 |
46 // TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability. | 52 // TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability. |
47 void GetBestMatchSupportedResolution(int* width, int* height) { | 53 void GetBestMatchSupportedResolution(int* width, int* height) { |
48 int min_diff = kint32max; | 54 int min_diff = kint32max; |
49 int matched_width = *width; | 55 int matched_width = *width; |
50 int matched_height = *height; | 56 int matched_height = *height; |
51 int desired_res_area = *width * *height; | 57 int desired_res_area = *width * *height; |
52 for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) { | 58 for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) { |
53 int area = kWellSupportedResolutions[i]->width * | 59 int area = kWellSupportedResolutions[i]->width * |
54 kWellSupportedResolutions[i]->height; | 60 kWellSupportedResolutions[i]->height; |
55 int diff = std::abs(desired_res_area - area); | 61 int diff = std::abs(desired_res_area - area); |
56 if (diff < min_diff) { | 62 if (diff < min_diff) { |
57 min_diff = diff; | 63 min_diff = diff; |
58 matched_width = kWellSupportedResolutions[i]->width; | 64 matched_width = kWellSupportedResolutions[i]->width; |
59 matched_height = kWellSupportedResolutions[i]->height; | 65 matched_height = kWellSupportedResolutions[i]->height; |
60 } | 66 } |
61 } | 67 } |
62 *width = matched_width; | 68 *width = matched_width; |
63 *height = matched_height; | 69 *height = matched_height; |
64 } | 70 } |
65 | 71 |
| 72 //static |
66 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | 73 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { |
67 // Loop through all available devices and add to |device_names|. | 74 // Loop through all available devices and add to |device_names|. |
68 device_names->clear(); | |
69 | |
70 NSDictionary* capture_devices; | 75 NSDictionary* capture_devices; |
71 if (AVFoundationGlue::IsAVFoundationSupported()) { | 76 if (AVFoundationGlue::IsAVFoundationSupported()) { |
| 77 bool is_any_device_blacklisted = false; |
72 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; | 78 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; |
73 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; | 79 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; |
| 80 std::string device_vid; |
| 81 // Enumerate all devices found by AVFoundation, translate the info for each |
| 82 // to class Name and add it to |device_names|. |
| 83 for (NSString* key in capture_devices) { |
| 84 Name name([[capture_devices valueForKey:key] UTF8String], |
| 85 [key UTF8String], Name::AVFOUNDATION); |
| 86 device_names->push_back(name); |
| 87 // Extract the device's Vendor ID and compare to all blacklisted ones. |
| 88 device_vid = name.GetModel().substr(0, kVidPidSize); |
| 89 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
| 90 is_any_device_blacklisted |= (device_vid == kBlacklistedCameras[i].vid); |
| 91 if (is_any_device_blacklisted) |
| 92 break; |
| 93 } |
| 94 } |
| 95 // If there is any device blacklisted in the system, walk the QTKit device |
| 96 // list and add those devices with a blacklisted name to the |device_names|. |
| 97 // AVFoundation and QTKit device lists partially overlap, so add a "QTKit" |
| 98 // prefix to the latter ones to distinguish them from the AVFoundation ones. |
| 99 if (is_any_device_blacklisted) { |
| 100 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
| 101 for (NSString* key in capture_devices) { |
| 102 NSString* device_name = [capture_devices valueForKey:key]; |
| 103 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
| 104 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name) |
| 105 options:NSCaseInsensitiveSearch].length != 0) { |
| 106 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String]; |
| 107 Name name("QTKit " + std::string([device_name UTF8String]), |
| 108 [key UTF8String], Name::QTKIT); |
| 109 device_names->push_back(name); |
| 110 } |
| 111 } |
| 112 } |
| 113 } |
74 } else { | 114 } else { |
75 DVLOG(1) << "Enumerating video capture devices using QTKit"; | 115 DVLOG(1) << "Enumerating video capture devices using QTKit"; |
76 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; | 116 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
77 } | 117 for (NSString* key in capture_devices) { |
78 for (NSString* key in capture_devices) { | 118 Name name([[capture_devices valueForKey:key] UTF8String], |
79 Name name([[capture_devices valueForKey:key] UTF8String], | 119 [key UTF8String], Name::QTKIT); |
80 [key UTF8String]); | 120 device_names->push_back(name); |
81 device_names->push_back(name); | 121 } |
82 } | 122 } |
83 } | 123 } |
84 | 124 |
85 // static | 125 // static |
86 void VideoCaptureDevice::GetDeviceSupportedFormats(const Name& device, | 126 void VideoCaptureDevice::GetDeviceSupportedFormats(const Name& device, |
87 VideoCaptureFormats* formats) { | 127 VideoCaptureFormats* formats) { |
88 if (AVFoundationGlue::IsAVFoundationSupported()) { | 128 if (device.capture_api_type() == Name::AVFOUNDATION) { |
89 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; | 129 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; |
90 [VideoCaptureDeviceAVFoundation getDevice:device | 130 [VideoCaptureDeviceAVFoundation getDevice:device |
91 supportedFormats:formats]; | 131 supportedFormats:formats]; |
92 } else { | 132 } else { |
93 NOTIMPLEMENTED(); | 133 NOTIMPLEMENTED(); |
94 } | 134 } |
95 } | 135 } |
96 | 136 |
97 const std::string VideoCaptureDevice::Name::GetModel() const { | 137 const std::string VideoCaptureDevice::Name::GetModel() const { |
98 // Both PID and VID are 4 characters. | 138 // Both PID and VID are 4 characters. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 Names device_names; | 252 Names device_names; |
213 GetDeviceNames(&device_names); | 253 GetDeviceNames(&device_names); |
214 Names::iterator it = device_names.begin(); | 254 Names::iterator it = device_names.begin(); |
215 for (; it != device_names.end(); ++it) { | 255 for (; it != device_names.end(); ++it) { |
216 if (it->id() == device_name_.id()) | 256 if (it->id() == device_name_.id()) |
217 break; | 257 break; |
218 } | 258 } |
219 if (it == device_names.end()) | 259 if (it == device_names.end()) |
220 return false; | 260 return false; |
221 | 261 |
222 if (AVFoundationGlue::IsAVFoundationSupported()) { | 262 DCHECK_NE(it->capture_api_type(), Name::API_TYPE_UNKNOWN); |
| 263 if (it->capture_api_type() == Name::AVFOUNDATION) { |
223 capture_device_ = | 264 capture_device_ = |
224 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; | 265 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; |
225 } else { | 266 } else { |
226 capture_device_ = | 267 capture_device_ = |
227 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; | 268 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; |
228 } | 269 } |
229 | 270 |
230 if (!capture_device_) | 271 if (!capture_device_) |
231 return false; | 272 return false; |
232 | 273 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() | 375 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() |
335 width:capture_format_.frame_size.width() | 376 width:capture_format_.frame_size.width() |
336 frameRate:capture_format_.frame_rate]) { | 377 frameRate:capture_format_.frame_rate]) { |
337 ReceiveError("Could not configure capture device."); | 378 ReceiveError("Could not configure capture device."); |
338 return false; | 379 return false; |
339 } | 380 } |
340 return true; | 381 return true; |
341 } | 382 } |
342 | 383 |
343 } // namespace media | 384 } // namespace media |
OLD | NEW |