| 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 | |
| 33 const struct Resolution { | 25 const struct Resolution { |
| 34 const int width; | 26 const int width; |
| 35 const int height; | 27 const int height; |
| 36 } kQVGA = { 320, 240 }, | 28 } kQVGA = { 320, 240 }, |
| 37 kVGA = { 640, 480 }, | 29 kVGA = { 640, 480 }, |
| 38 kHD = { 1280, 720 }; | 30 kHD = { 1280, 720 }; |
| 39 | 31 |
| 40 const struct Resolution* const kWellSupportedResolutions[] = { | 32 const struct Resolution* const kWellSupportedResolutions[] = { |
| 41 &kQVGA, | 33 &kQVGA, |
| 42 &kVGA, | 34 &kVGA, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 if (diff < min_diff) { | 54 if (diff < min_diff) { |
| 63 min_diff = diff; | 55 min_diff = diff; |
| 64 matched_width = kWellSupportedResolutions[i]->width; | 56 matched_width = kWellSupportedResolutions[i]->width; |
| 65 matched_height = kWellSupportedResolutions[i]->height; | 57 matched_height = kWellSupportedResolutions[i]->height; |
| 66 } | 58 } |
| 67 } | 59 } |
| 68 *width = matched_width; | 60 *width = matched_width; |
| 69 *height = matched_height; | 61 *height = matched_height; |
| 70 } | 62 } |
| 71 | 63 |
| 72 //static | |
| 73 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | |
| 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 } | |
| 124 } | |
| 125 | |
| 126 // static | |
| 127 void VideoCaptureDevice::GetDeviceSupportedFormats(const Name& device, | |
| 128 VideoCaptureFormats* formats) { | |
| 129 if (device.capture_api_type() == Name::AVFOUNDATION) { | |
| 130 DVLOG(1) << "Enumerating video capture capabilities, AVFoundation"; | |
| 131 [VideoCaptureDeviceAVFoundation getDevice:device | |
| 132 supportedFormats:formats]; | |
| 133 } else { | |
| 134 NOTIMPLEMENTED(); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 const std::string VideoCaptureDevice::Name::GetModel() const { | 64 const std::string VideoCaptureDevice::Name::GetModel() const { |
| 139 // Both PID and VID are 4 characters. | 65 // Both PID and VID are 4 characters. |
| 140 if (unique_id_.size() < 2 * kVidPidSize) { | 66 if (unique_id_.size() < 2 * kVidPidSize) { |
| 141 return ""; | 67 return ""; |
| 142 } | 68 } |
| 143 | 69 |
| 144 // The last characters of device id is a concatenation of VID and then PID. | 70 // 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; | 71 const size_t vid_location = unique_id_.size() - 2 * kVidPidSize; |
| 146 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); | 72 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); |
| 147 const size_t pid_location = unique_id_.size() - kVidPidSize; | 73 const size_t pid_location = unique_id_.size() - kVidPidSize; |
| 148 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); | 74 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); |
| 149 | 75 |
| 150 return id_vendor + ":" + id_product; | 76 return id_vendor + ":" + id_product; |
| 151 } | 77 } |
| 152 | 78 |
| 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) | 79 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name) |
| 165 : device_name_(device_name), | 80 : device_name_(device_name), |
| 166 tried_to_square_pixels_(false), | 81 tried_to_square_pixels_(false), |
| 167 task_runner_(base::MessageLoopProxy::current()), | 82 task_runner_(base::MessageLoopProxy::current()), |
| 168 state_(kNotInitialized), | 83 state_(kNotInitialized), |
| 169 capture_device_(nil), | 84 capture_device_(nil), |
| 170 weak_factory_(this) { | 85 weak_factory_(this) { |
| 171 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); | 86 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); |
| 172 } | 87 } |
| 173 | 88 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 DCHECK(state_ == kCapturing || state_ == kError) << state_; | 152 DCHECK(state_ == kCapturing || state_ == kError) << state_; |
| 238 [capture_device_ stopCapture]; | 153 [capture_device_ stopCapture]; |
| 239 | 154 |
| 240 [capture_device_ setCaptureDevice:nil]; | 155 [capture_device_ setCaptureDevice:nil]; |
| 241 [capture_device_ setFrameReceiver:nil]; | 156 [capture_device_ setFrameReceiver:nil]; |
| 242 client_.reset(); | 157 client_.reset(); |
| 243 state_ = kIdle; | 158 state_ = kIdle; |
| 244 tried_to_square_pixels_ = false; | 159 tried_to_square_pixels_ = false; |
| 245 } | 160 } |
| 246 | 161 |
| 247 bool VideoCaptureDeviceMac::Init() { | 162 bool VideoCaptureDeviceMac::Init( |
| 163 VideoCaptureDevice::Name::CaptureApiType capture_api_type) { |
| 248 DCHECK(task_runner_->BelongsToCurrentThread()); | 164 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 249 DCHECK_EQ(state_, kNotInitialized); | 165 DCHECK_EQ(state_, kNotInitialized); |
| 250 | 166 |
| 251 // TODO(mcasas): The following check might not be necessary; if the device has | 167 if (capture_api_type == Name::AVFOUNDATION) { |
| 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_ = | 168 capture_device_ = |
| 267 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; | 169 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; |
| 268 } else { | 170 } else { |
| 269 capture_device_ = | 171 capture_device_ = |
| 270 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; | 172 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; |
| 271 } | 173 } |
| 272 | 174 |
| 273 if (!capture_device_) | 175 if (!capture_device_) |
| 274 return false; | 176 return false; |
| 275 | 177 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() | 279 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() |
| 378 width:capture_format_.frame_size.width() | 280 width:capture_format_.frame_size.width() |
| 379 frameRate:capture_format_.frame_rate]) { | 281 frameRate:capture_format_.frame_rate]) { |
| 380 ReceiveError("Could not configure capture device."); | 282 ReceiveError("Could not configure capture device."); |
| 381 return false; | 283 return false; |
| 382 } | 284 } |
| 383 return true; | 285 return true; |
| 384 } | 286 } |
| 385 | 287 |
| 386 } // namespace media | 288 } // namespace media |
| OLD | NEW |