OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_factory_mac.h" | |
6 | |
7 #import "media/video/capture/mac/avfoundation_glue.h" | |
5 #include "media/video/capture/mac/video_capture_device_mac.h" | 8 #include "media/video/capture/mac/video_capture_device_mac.h" |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop/message_loop_proxy.h" | |
11 #include "base/time/time.h" | |
12 #import "media/video/capture/mac/avfoundation_glue.h" | |
13 #import "media/video/capture/mac/platform_video_capturing_mac.h" | |
14 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" | 9 #import "media/video/capture/mac/video_capture_device_avfoundation_mac.h" |
15 #import "media/video/capture/mac/video_capture_device_qtkit_mac.h" | 10 #import "media/video/capture/mac/video_capture_device_qtkit_mac.h" |
16 | 11 |
17 namespace media { | 12 namespace media { |
18 | 13 |
19 const int kMinFrameRate = 1; | |
20 const int kMaxFrameRate = 30; | |
21 | |
22 // In device identifiers, the USB VID and PID are stored in 4 bytes each. | |
23 const size_t kVidPidSize = 4; | |
24 | |
25 // Some devices are not correctly supported in AVFoundation, f.i. Blackmagic, | 14 // 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 | 15 // 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. | 16 // by a characteristic substring of the name, usually the vendor's name. |
28 const struct NameAndVid { | 17 const struct NameAndVid { |
29 const char* vid; | 18 const char* vid; |
30 const char* name; | 19 const char* name; |
31 } kBlacklistedCameras[] = { { "a82c", "Blackmagic" } }; | 20 } kBlacklistedCameras[] = { { "a82c", "Blackmagic" } }; |
32 | 21 |
33 const struct Resolution { | 22 // In device identifiers, the USB VID and PID are stored in 4 bytes each. |
34 const int width; | 23 const size_t kVidPidSize = 4; |
35 const int height; | |
36 } kQVGA = { 320, 240 }, | |
37 kVGA = { 640, 480 }, | |
38 kHD = { 1280, 720 }; | |
39 | 24 |
40 const struct Resolution* const kWellSupportedResolutions[] = { | 25 VideoCaptureDeviceFactoryMac::VideoCaptureDeviceFactoryMac() { |
41 &kQVGA, | 26 thread_checker_.DetachFromThread(); |
42 &kVGA, | |
43 &kHD, | |
44 }; | |
45 | |
46 // Rescaling the image to fix the pixel aspect ratio runs the risk of making | |
47 // the aspect ratio worse, if QTKit selects a new source mode with a different | |
48 // shape. This constant ensures that we don't take this risk if the current | |
49 // aspect ratio is tolerable. | |
50 const float kMaxPixelAspectRatio = 1.15; | |
51 | |
52 // TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability. | |
53 void GetBestMatchSupportedResolution(int* width, int* height) { | |
54 int min_diff = kint32max; | |
55 int matched_width = *width; | |
56 int matched_height = *height; | |
57 int desired_res_area = *width * *height; | |
58 for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) { | |
59 int area = kWellSupportedResolutions[i]->width * | |
60 kWellSupportedResolutions[i]->height; | |
61 int diff = std::abs(desired_res_area - area); | |
62 if (diff < min_diff) { | |
63 min_diff = diff; | |
64 matched_width = kWellSupportedResolutions[i]->width; | |
65 matched_height = kWellSupportedResolutions[i]->height; | |
66 } | |
67 } | |
68 *width = matched_width; | |
69 *height = matched_height; | |
70 } | 27 } |
71 | 28 |
72 //static | 29 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryMac::Create( |
73 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | 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; | |
perkj_chrome
2014/05/06 18:26:00
yes this check seems unneccessary. That must be ha
mcasas
2014/05/07 07:49:12
Done.
| |
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>(); | |
perkj_chrome
2014/05/06 18:26:00
This will leak the memory used by capture_device
mcasas
2014/05/07 07:49:12
Done.
| |
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); | |
perkj_chrome
2014/05/06 18:26:00
captur_device.pass
mcasas
2014/05/07 07:49:12
Done.
| |
55 } | |
56 | |
57 void VideoCaptureDeviceFactoryMac::GetDeviceNames( | |
58 VideoCaptureDevice::Names* const device_names) { | |
59 DCHECK(thread_checker_.CalledOnValidThread()); | |
74 // Loop through all available devices and add to |device_names|. | 60 // Loop through all available devices and add to |device_names|. |
75 NSDictionary* capture_devices; | 61 NSDictionary* capture_devices; |
76 if (AVFoundationGlue::IsAVFoundationSupported()) { | 62 if (AVFoundationGlue::IsAVFoundationSupported()) { |
77 bool is_any_device_blacklisted = false; | 63 bool is_any_device_blacklisted = false; |
78 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; | 64 DVLOG(1) << "Enumerating video capture devices using AVFoundation"; |
79 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; | 65 capture_devices = [VideoCaptureDeviceAVFoundation deviceNames]; |
80 std::string device_vid; | 66 std::string device_vid; |
81 // Enumerate all devices found by AVFoundation, translate the info for each | 67 // Enumerate all devices found by AVFoundation, translate the info for each |
82 // to class Name and add it to |device_names|. | 68 // to class Name and add it to |device_names|. |
83 for (NSString* key in capture_devices) { | 69 for (NSString* key in capture_devices) { |
84 Name name([[capture_devices valueForKey:key] UTF8String], | 70 VideoCaptureDevice::Name name( |
85 [key UTF8String], Name::AVFOUNDATION); | 71 [[capture_devices valueForKey:key] UTF8String], |
72 [key UTF8String], VideoCaptureDevice::Name::AVFOUNDATION); | |
86 device_names->push_back(name); | 73 device_names->push_back(name); |
87 // Extract the device's Vendor ID and compare to all blacklisted ones. | 74 // Extract the device's Vendor ID and compare to all blacklisted ones. |
88 device_vid = name.GetModel().substr(0, kVidPidSize); | 75 device_vid = name.GetModel().substr(0, kVidPidSize); |
89 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { | 76 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
90 is_any_device_blacklisted |= | 77 is_any_device_blacklisted |= |
91 !strcasecmp(device_vid.c_str(), kBlacklistedCameras[i].vid); | 78 !strcasecmp(device_vid.c_str(), kBlacklistedCameras[i].vid); |
92 if (is_any_device_blacklisted) | 79 if (is_any_device_blacklisted) |
93 break; | 80 break; |
94 } | 81 } |
95 } | 82 } |
96 // If there is any device blacklisted in the system, walk the QTKit device | 83 // 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|. | 84 // 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" | 85 // AVFoundation and QTKit device lists partially overlap, so add a "QTKit" |
99 // prefix to the latter ones to distinguish them from the AVFoundation ones. | 86 // prefix to the latter ones to distinguish them from the AVFoundation ones. |
100 if (is_any_device_blacklisted) { | 87 if (is_any_device_blacklisted) { |
101 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; | 88 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
102 for (NSString* key in capture_devices) { | 89 for (NSString* key in capture_devices) { |
103 NSString* device_name = [capture_devices valueForKey:key]; | 90 NSString* device_name = [capture_devices valueForKey:key]; |
104 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { | 91 for (size_t i = 0; i < arraysize(kBlacklistedCameras); ++i) { |
105 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name) | 92 if ([device_name rangeOfString:@(kBlacklistedCameras[i].name) |
106 options:NSCaseInsensitiveSearch].length != 0) { | 93 options:NSCaseInsensitiveSearch].length != 0) { |
107 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String]; | 94 DVLOG(1) << "Enumerated blacklisted " << [device_name UTF8String]; |
108 Name name("QTKit " + std::string([device_name UTF8String]), | 95 VideoCaptureDevice::Name name( |
109 [key UTF8String], Name::QTKIT); | 96 "QTKit " + std::string([device_name UTF8String]), |
97 [key UTF8String], VideoCaptureDevice::Name::QTKIT); | |
110 device_names->push_back(name); | 98 device_names->push_back(name); |
111 } | 99 } |
112 } | 100 } |
113 } | 101 } |
114 } | 102 } |
115 } else { | 103 } else { |
116 DVLOG(1) << "Enumerating video capture devices using QTKit"; | 104 DVLOG(1) << "Enumerating video capture devices using QTKit"; |
117 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; | 105 capture_devices = [VideoCaptureDeviceQTKit deviceNames]; |
118 for (NSString* key in capture_devices) { | 106 for (NSString* key in capture_devices) { |
119 Name name([[capture_devices valueForKey:key] UTF8String], | 107 VideoCaptureDevice::Name name( |
120 [key UTF8String], Name::QTKIT); | 108 [[capture_devices valueForKey:key] UTF8String], |
109 [key UTF8String], VideoCaptureDevice::Name::QTKIT); | |
121 device_names->push_back(name); | 110 device_names->push_back(name); |
122 } | 111 } |
123 } | 112 } |
124 } | 113 } |
125 | 114 |
126 // static | 115 void VideoCaptureDeviceFactoryMac::GetDeviceSupportedFormats( |
127 void VideoCaptureDevice::GetDeviceSupportedFormats(const Name& device, | 116 const VideoCaptureDevice::Name& device, |
128 VideoCaptureFormats* formats) { | 117 VideoCaptureFormats* supported_formats) { |
129 if (device.capture_api_type() == Name::AVFOUNDATION) { | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
119 if (device.capture_api_type() == VideoCaptureDevice::Name::AVFOUNDATION) { | |
130 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; | 120 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; |
131 [VideoCaptureDeviceAVFoundation getDevice:device | 121 [VideoCaptureDeviceAVFoundation getDevice:device |
132 supportedFormats:formats]; | 122 supportedFormats:supported_formats]; |
133 } else { | 123 } else { |
134 NOTIMPLEMENTED(); | 124 NOTIMPLEMENTED(); |
135 } | 125 } |
136 } | 126 } |
137 | 127 |
138 const std::string VideoCaptureDevice::Name::GetModel() const { | 128 } // namespace media |
139 // Both PID and VID are 4 characters. | |
140 if (unique_id_.size() < 2 * kVidPidSize) { | |
141 return ""; | |
142 } | |
143 | |
144 // The last characters of device id is a concatenation of VID and then PID. | |
145 const size_t vid_location = unique_id_.size() - 2 * kVidPidSize; | |
146 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); | |
147 const size_t pid_location = unique_id_.size() - kVidPidSize; | |
148 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); | |
149 | |
150 return id_vendor + ":" + id_product; | |
151 } | |
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 | |
164 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name) | |
165 : device_name_(device_name), | |
166 tried_to_square_pixels_(false), | |
167 task_runner_(base::MessageLoopProxy::current()), | |
168 state_(kNotInitialized), | |
169 capture_device_(nil), | |
170 weak_factory_(this) { | |
171 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); | |
172 } | |
173 | |
174 VideoCaptureDeviceMac::~VideoCaptureDeviceMac() { | |
175 DCHECK(task_runner_->BelongsToCurrentThread()); | |
176 [capture_device_ release]; | |
177 } | |
178 | |
179 void VideoCaptureDeviceMac::AllocateAndStart( | |
180 const VideoCaptureParams& params, | |
181 scoped_ptr<VideoCaptureDevice::Client> client) { | |
182 DCHECK(task_runner_->BelongsToCurrentThread()); | |
183 if (state_ != kIdle) { | |
184 return; | |
185 } | |
186 int width = params.requested_format.frame_size.width(); | |
187 int height = params.requested_format.frame_size.height(); | |
188 int frame_rate = params.requested_format.frame_rate; | |
189 | |
190 // QTKit API can scale captured frame to any size requested, which would lead | |
191 // to undesired aspect ratio changes. Try to open the camera with a known | |
192 // supported format and let the client crop/pad the captured frames. | |
193 if (!AVFoundationGlue::IsAVFoundationSupported()) | |
194 GetBestMatchSupportedResolution(&width, &height); | |
195 | |
196 client_ = client.Pass(); | |
197 NSString* deviceId = | |
198 [NSString stringWithUTF8String:device_name_.id().c_str()]; | |
199 | |
200 [capture_device_ setFrameReceiver:this]; | |
201 | |
202 if (![capture_device_ setCaptureDevice:deviceId]) { | |
203 SetErrorState("Could not open capture device."); | |
204 return; | |
205 } | |
206 if (frame_rate < kMinFrameRate) | |
207 frame_rate = kMinFrameRate; | |
208 else if (frame_rate > kMaxFrameRate) | |
209 frame_rate = kMaxFrameRate; | |
210 | |
211 capture_format_.frame_size.SetSize(width, height); | |
212 capture_format_.frame_rate = frame_rate; | |
213 capture_format_.pixel_format = PIXEL_FORMAT_UYVY; | |
214 | |
215 // QTKit: Set the capture resolution only if this is VGA or smaller, otherwise | |
216 // leave it unconfigured and start capturing: QTKit will produce frames at the | |
217 // native resolution, allowing us to identify cameras whose native resolution | |
218 // is too low for HD. This additional information comes at a cost in startup | |
219 // latency, because the webcam will need to be reopened if its default | |
220 // resolution is not HD or VGA. | |
221 // AVfoundation is configured for all resolutions. | |
222 if (AVFoundationGlue::IsAVFoundationSupported() || width <= kVGA.width || | |
223 height <= kVGA.height) { | |
224 if (!UpdateCaptureResolution()) | |
225 return; | |
226 } | |
227 if (![capture_device_ startCapture]) { | |
228 SetErrorState("Could not start capture device."); | |
229 return; | |
230 } | |
231 | |
232 state_ = kCapturing; | |
233 } | |
234 | |
235 void VideoCaptureDeviceMac::StopAndDeAllocate() { | |
236 DCHECK(task_runner_->BelongsToCurrentThread()); | |
237 DCHECK(state_ == kCapturing || state_ == kError) << state_; | |
238 [capture_device_ stopCapture]; | |
239 | |
240 [capture_device_ setCaptureDevice:nil]; | |
241 [capture_device_ setFrameReceiver:nil]; | |
242 client_.reset(); | |
243 state_ = kIdle; | |
244 tried_to_square_pixels_ = false; | |
245 } | |
246 | |
247 bool VideoCaptureDeviceMac::Init() { | |
248 DCHECK(task_runner_->BelongsToCurrentThread()); | |
249 DCHECK_EQ(state_, kNotInitialized); | |
250 | |
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) { | |
266 capture_device_ = | |
267 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; | |
268 } else { | |
269 capture_device_ = | |
270 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; | |
271 } | |
272 | |
273 if (!capture_device_) | |
274 return false; | |
275 | |
276 state_ = kIdle; | |
277 return true; | |
278 } | |
279 | |
280 void VideoCaptureDeviceMac::ReceiveFrame( | |
281 const uint8* video_frame, | |
282 int video_frame_length, | |
283 const VideoCaptureFormat& frame_format, | |
284 int aspect_numerator, | |
285 int aspect_denominator) { | |
286 // This method is safe to call from a device capture thread, i.e. any thread | |
287 // controlled by QTKit/AVFoundation. | |
288 if (!final_resolution_selected_) { | |
289 DCHECK(!AVFoundationGlue::IsAVFoundationSupported()); | |
290 if (capture_format_.frame_size.width() > kVGA.width || | |
291 capture_format_.frame_size.height() > kVGA.height) { | |
292 // We are requesting HD. Make sure that the picture is good, otherwise | |
293 // drop down to VGA. | |
294 bool change_to_vga = false; | |
295 if (frame_format.frame_size.width() < | |
296 capture_format_.frame_size.width() || | |
297 frame_format.frame_size.height() < | |
298 capture_format_.frame_size.height()) { | |
299 // These are the default capture settings, not yet configured to match | |
300 // |capture_format_|. | |
301 DCHECK(frame_format.frame_rate == 0); | |
302 DVLOG(1) << "Switching to VGA because the default resolution is " << | |
303 frame_format.frame_size.ToString(); | |
304 change_to_vga = true; | |
305 } | |
306 | |
307 if (capture_format_.frame_size == frame_format.frame_size && | |
308 aspect_numerator != aspect_denominator) { | |
309 DVLOG(1) << "Switching to VGA because HD has nonsquare pixel " << | |
310 "aspect ratio " << aspect_numerator << ":" << aspect_denominator; | |
311 change_to_vga = true; | |
312 } | |
313 | |
314 if (change_to_vga) | |
315 capture_format_.frame_size.SetSize(kVGA.width, kVGA.height); | |
316 } | |
317 | |
318 if (capture_format_.frame_size == frame_format.frame_size && | |
319 !tried_to_square_pixels_ && | |
320 (aspect_numerator > kMaxPixelAspectRatio * aspect_denominator || | |
321 aspect_denominator > kMaxPixelAspectRatio * aspect_numerator)) { | |
322 // The requested size results in non-square PAR. Shrink the frame to 1:1 | |
323 // PAR (assuming QTKit selects the same input mode, which is not | |
324 // guaranteed). | |
325 int new_width = capture_format_.frame_size.width(); | |
326 int new_height = capture_format_.frame_size.height(); | |
327 if (aspect_numerator < aspect_denominator) | |
328 new_width = (new_width * aspect_numerator) / aspect_denominator; | |
329 else | |
330 new_height = (new_height * aspect_denominator) / aspect_numerator; | |
331 capture_format_.frame_size.SetSize(new_width, new_height); | |
332 tried_to_square_pixels_ = true; | |
333 } | |
334 | |
335 if (capture_format_.frame_size == frame_format.frame_size) { | |
336 final_resolution_selected_ = true; | |
337 } else { | |
338 UpdateCaptureResolution(); | |
339 // Let the resolution update sink through QTKit and wait for next frame. | |
340 return; | |
341 } | |
342 } | |
343 | |
344 // QTKit capture source can change resolution if someone else reconfigures the | |
345 // camera, and that is fine: http://crbug.com/353620. In AVFoundation, this | |
346 // should not happen, it should resize internally. | |
347 if (!AVFoundationGlue::IsAVFoundationSupported()) { | |
348 capture_format_.frame_size = frame_format.frame_size; | |
349 } else if (capture_format_.frame_size != frame_format.frame_size) { | |
350 ReceiveError("Captured resolution " + frame_format.frame_size.ToString() + | |
351 ", and expected " + capture_format_.frame_size.ToString()); | |
352 return; | |
353 } | |
354 | |
355 client_->OnIncomingCapturedData(video_frame, | |
356 video_frame_length, | |
357 capture_format_, | |
358 0, | |
359 base::TimeTicks::Now()); | |
360 } | |
361 | |
362 void VideoCaptureDeviceMac::ReceiveError(const std::string& reason) { | |
363 task_runner_->PostTask(FROM_HERE, | |
364 base::Bind(&VideoCaptureDeviceMac::SetErrorState, | |
365 weak_factory_.GetWeakPtr(), | |
366 reason)); | |
367 } | |
368 | |
369 void VideoCaptureDeviceMac::SetErrorState(const std::string& reason) { | |
370 DCHECK(task_runner_->BelongsToCurrentThread()); | |
371 DLOG(ERROR) << reason; | |
372 state_ = kError; | |
373 client_->OnError(reason); | |
374 } | |
375 | |
376 bool VideoCaptureDeviceMac::UpdateCaptureResolution() { | |
377 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() | |
378 width:capture_format_.frame_size.width() | |
379 frameRate:capture_format_.frame_rate]) { | |
380 ReceiveError("Could not configure capture device."); | |
381 return false; | |
382 } | |
383 return true; | |
384 } | |
385 | |
386 } // namespace media | |
OLD | NEW |