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 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 // Some devices are not correctly supported in AVFoundation, f.i. Blackmagic, |
| 26 // see http://crbug.com/347371. The devices are identified by USB Vendor ID and |
| 27 // by a characteristic substring of the name, usually the vendor's name. |
| 28 const struct NameAndVid { |
| 29 const char* vid; |
| 30 const char* name; |
| 31 } kBlacklistedCameras[] = { { "a82c", "Blackmagic" } }; |
| 32 |
25 const struct Resolution { | 33 const struct Resolution { |
26 const int width; | 34 const int width; |
27 const int height; | 35 const int height; |
28 } kQVGA = { 320, 240 }, | 36 } kQVGA = { 320, 240 }, |
29 kVGA = { 640, 480 }, | 37 kVGA = { 640, 480 }, |
30 kHD = { 1280, 720 }; | 38 kHD = { 1280, 720 }; |
31 | 39 |
32 const struct Resolution* const kWellSupportedResolutions[] = { | 40 const struct Resolution* const kWellSupportedResolutions[] = { |
33 &kQVGA, | 41 &kQVGA, |
34 &kVGA, | 42 &kVGA, |
(...skipping 19 matching lines...) Expand all Loading... |
54 if (diff < min_diff) { | 62 if (diff < min_diff) { |
55 min_diff = diff; | 63 min_diff = diff; |
56 matched_width = kWellSupportedResolutions[i]->width; | 64 matched_width = kWellSupportedResolutions[i]->width; |
57 matched_height = kWellSupportedResolutions[i]->height; | 65 matched_height = kWellSupportedResolutions[i]->height; |
58 } | 66 } |
59 } | 67 } |
60 *width = matched_width; | 68 *width = matched_width; |
61 *height = matched_height; | 69 *height = matched_height; |
62 } | 70 } |
63 | 71 |
64 // TODO(mcasas): Remove the following static methods when they are no longer | 72 //static |
65 // referenced from VideoCaptureDeviceFactory, i.e. when all OS platforms have | |
66 // splitted the VideoCaptureDevice into VideoCaptureDevice and | |
67 // VideoCaptureDeviceFactory. | |
68 | |
69 // static | |
70 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { | |
71 NOTREACHED(); | |
72 return NULL; | |
73 } | |
74 // static | |
75 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | 73 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { |
76 NOTREACHED(); | 74 // Loop through all available devices and add to |device_names|. |
| 75 NSDictionary* capture_devices; |
| 76 if (AVFoundationGlue::IsAVFoundationSupported()) { |
| 77 bool is_any_device_blacklisted = false; |
| 78 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; |
| 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 |= |
| 91 !strcasecmp(device_vid.c_str(), kBlacklistedCameras[i].vid); |
| 92 if (is_any_device_blacklisted) |
| 93 break; |
| 94 } |
| 95 } |
| 96 // If there is any device blacklisted in the system, walk the QTKit device |
| 97 // list and add those devices with a blacklisted name to the |device_names|. |
| 98 // AVFoundation and QTKit device lists partially overlap, so add a "QTKit" |
| 99 // prefix to the latter ones to distinguish them from the AVFoundation ones. |
| 100 if (is_any_device_blacklisted) { |
| 101 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
| 102 for (NSString* key in capture_devices) { |
| 103 NSString* device_name = [capture_devices valueForKey:key]; |
| 104 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
| 105 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name) |
| 106 options:NSCaseInsensitiveSearch].length != 0) { |
| 107 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String]; |
| 108 Name name("QTKit " + std::string([device_name UTF8String]), |
| 109 [key UTF8String], Name::QTKIT); |
| 110 device_names->push_back(name); |
| 111 } |
| 112 } |
| 113 } |
| 114 } |
| 115 } else { |
| 116 DVLOG(1) << "Enumerating video capture devices using QTKit"; |
| 117 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
| 118 for (NSString* key in capture_devices) { |
| 119 Name name([[capture_devices valueForKey:key] UTF8String], |
| 120 [key UTF8String], Name::QTKIT); |
| 121 device_names->push_back(name); |
| 122 } |
| 123 } |
77 } | 124 } |
78 | 125 |
79 // static | 126 // static |
80 void VideoCaptureDevice::GetDeviceSupportedFormats( | 127 void VideoCaptureDevice::GetDeviceSupportedFormats(const Name& device, |
81 const Name& device, | 128 VideoCaptureFormats* formats) { |
82 VideoCaptureFormats* supported_formats) { | 129 if (device.capture_api_type() == Name::AVFOUNDATION) { |
83 NOTREACHED(); | 130 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; |
| 131 [VideoCaptureDeviceAVFoundation getDevice:device |
| 132 supportedFormats:formats]; |
| 133 } else { |
| 134 NOTIMPLEMENTED(); |
| 135 } |
84 } | 136 } |
85 | 137 |
86 const std::string VideoCaptureDevice::Name::GetModel() const { | 138 const std::string VideoCaptureDevice::Name::GetModel() const { |
87 // Both PID and VID are 4 characters. | 139 // Both PID and VID are 4 characters. |
88 if (unique_id_.size() < 2 * kVidPidSize) { | 140 if (unique_id_.size() < 2 * kVidPidSize) { |
89 return ""; | 141 return ""; |
90 } | 142 } |
91 | 143 |
92 // The last characters of device id is a concatenation of VID and then PID. | 144 // The last characters of device id is a concatenation of VID and then PID. |
93 const size_t vid_location = unique_id_.size() - 2 * kVidPidSize; | 145 const size_t vid_location = unique_id_.size() - 2 * kVidPidSize; |
94 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); | 146 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); |
95 const size_t pid_location = unique_id_.size() - kVidPidSize; | 147 const size_t pid_location = unique_id_.size() - kVidPidSize; |
96 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); | 148 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); |
97 | 149 |
98 return id_vendor + ":" + id_product; | 150 return id_vendor + ":" + id_product; |
99 } | 151 } |
100 | 152 |
| 153 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { |
| 154 VideoCaptureDeviceMac* capture_device = |
| 155 new VideoCaptureDeviceMac(device_name); |
| 156 if (!capture_device->Init()) { |
| 157 LOG(ERROR) << "Could not initialize VideoCaptureDevice."; |
| 158 delete capture_device; |
| 159 capture_device = NULL; |
| 160 } |
| 161 return capture_device; |
| 162 } |
| 163 |
101 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name) | 164 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name) |
102 : device_name_(device_name), | 165 : device_name_(device_name), |
103 tried_to_square_pixels_(false), | 166 tried_to_square_pixels_(false), |
104 task_runner_(base::MessageLoopProxy::current()), | 167 task_runner_(base::MessageLoopProxy::current()), |
105 state_(kNotInitialized), | 168 state_(kNotInitialized), |
106 capture_device_(nil), | 169 capture_device_(nil), |
107 weak_factory_(this) { | 170 weak_factory_(this) { |
108 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); | 171 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); |
109 } | 172 } |
110 | 173 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 DCHECK(state_ == kCapturing || state_ == kError) << state_; | 237 DCHECK(state_ == kCapturing || state_ == kError) << state_; |
175 [capture_device_ stopCapture]; | 238 [capture_device_ stopCapture]; |
176 | 239 |
177 [capture_device_ setCaptureDevice:nil]; | 240 [capture_device_ setCaptureDevice:nil]; |
178 [capture_device_ setFrameReceiver:nil]; | 241 [capture_device_ setFrameReceiver:nil]; |
179 client_.reset(); | 242 client_.reset(); |
180 state_ = kIdle; | 243 state_ = kIdle; |
181 tried_to_square_pixels_ = false; | 244 tried_to_square_pixels_ = false; |
182 } | 245 } |
183 | 246 |
184 bool VideoCaptureDeviceMac::Init( | 247 bool VideoCaptureDeviceMac::Init() { |
185 VideoCaptureDevice::Name::CaptureApiType capture_api_type) { | |
186 DCHECK(task_runner_->BelongsToCurrentThread()); | 248 DCHECK(task_runner_->BelongsToCurrentThread()); |
187 DCHECK_EQ(state_, kNotInitialized); | 249 DCHECK_EQ(state_, kNotInitialized); |
188 | 250 |
189 if (capture_api_type == Name::AVFOUNDATION) { | 251 // TODO(mcasas): The following check might not be necessary; if the device has |
| 252 // disappeared after enumeration and before coming here, opening would just |
| 253 // fail but not necessarily produce a crash. |
| 254 Names device_names; |
| 255 GetDeviceNames(&device_names); |
| 256 Names::iterator it = device_names.begin(); |
| 257 for (; it != device_names.end(); ++it) { |
| 258 if (it->id() == device_name_.id()) |
| 259 break; |
| 260 } |
| 261 if (it == device_names.end()) |
| 262 return false; |
| 263 |
| 264 DCHECK_NE(it->capture_api_type(), Name::API_TYPE_UNKNOWN); |
| 265 if (it->capture_api_type() == Name::AVFOUNDATION) { |
190 capture_device_ = | 266 capture_device_ = |
191 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; | 267 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; |
192 } else { | 268 } else { |
193 capture_device_ = | 269 capture_device_ = |
194 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; | 270 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; |
195 } | 271 } |
196 | 272 |
197 if (!capture_device_) | 273 if (!capture_device_) |
198 return false; | 274 return false; |
199 | 275 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() | 377 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() |
302 width:capture_format_.frame_size.width() | 378 width:capture_format_.frame_size.width() |
303 frameRate:capture_format_.frame_rate]) { | 379 frameRate:capture_format_.frame_rate]) { |
304 ReceiveError("Could not configure capture device."); | 380 ReceiveError("Could not configure capture device."); |
305 return false; | 381 return false; |
306 } | 382 } |
307 return true; | 383 return true; |
308 } | 384 } |
309 | 385 |
310 } // namespace media | 386 } // namespace media |
OLD | NEW |