Chromium Code Reviews| 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, |
| 43 &kHD, | 35 &kHD, |
| 44 }; | 36 }; |
| 45 | 37 |
| 46 // Rescaling the image to fix the pixel aspect ratio runs the risk of making | 38 // 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 | 39 // 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 | 40 // shape. This constant ensures that we don't take this risk if the current |
| 49 // aspect ratio is tolerable. | 41 // aspect ratio is tolerable. |
| 50 const float kMaxPixelAspectRatio = 1.15; | 42 const float kMaxPixelAspectRatio = 1.15; |
| 51 | 43 |
| 44 // TODO(mcasas): Remove the following static methods when they are no longer | |
|
perkj_chrome
2014/05/07 10:46:49
nit: move to after GetBestMatchSupportedResolution
mcasas
2014/05/07 12:18:23
Done.
| |
| 45 // referenced from VideoCaptureDeviceFactory, i.e. when all OS platforms have | |
| 46 // splitted the VideoCaptureDevice into VideoCaptureDevice and | |
| 47 // VideoCaptureDeviceFactory. | |
| 48 | |
| 49 // static | |
| 50 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) { | |
| 51 NOTREACHED(); | |
| 52 return NULL; | |
| 53 } | |
| 54 // static | |
| 55 void VideoCaptureDevice::GetDeviceNames(Names* device_names) { | |
| 56 NOTREACHED(); | |
| 57 } | |
| 58 | |
| 59 // static | |
| 60 void VideoCaptureDevice::GetDeviceSupportedFormats( | |
| 61 const Name& device, | |
| 62 VideoCaptureFormats* supported_formats) { | |
| 63 NOTREACHED(); | |
| 64 } | |
| 65 | |
| 52 // TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability. | 66 // TODO(ronghuawu): Replace this with CapabilityList::GetBestMatchedCapability. |
| 53 void GetBestMatchSupportedResolution(int* width, int* height) { | 67 void GetBestMatchSupportedResolution(int* width, int* height) { |
| 54 int min_diff = kint32max; | 68 int min_diff = kint32max; |
| 55 int matched_width = *width; | 69 int matched_width = *width; |
| 56 int matched_height = *height; | 70 int matched_height = *height; |
| 57 int desired_res_area = *width * *height; | 71 int desired_res_area = *width * *height; |
| 58 for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) { | 72 for (size_t i = 0; i < arraysize(kWellSupportedResolutions); ++i) { |
| 59 int area = kWellSupportedResolutions[i]->width * | 73 int area = kWellSupportedResolutions[i]->width * |
| 60 kWellSupportedResolutions[i]->height; | 74 kWellSupportedResolutions[i]->height; |
| 61 int diff = std::abs(desired_res_area - area); | 75 int diff = std::abs(desired_res_area - area); |
| 62 if (diff < min_diff) { | 76 if (diff < min_diff) { |
| 63 min_diff = diff; | 77 min_diff = diff; |
| 64 matched_width = kWellSupportedResolutions[i]->width; | 78 matched_width = kWellSupportedResolutions[i]->width; |
| 65 matched_height = kWellSupportedResolutions[i]->height; | 79 matched_height = kWellSupportedResolutions[i]->height; |
| 66 } | 80 } |
| 67 } | 81 } |
| 68 *width = matched_width; | 82 *width = matched_width; |
| 69 *height = matched_height; | 83 *height = matched_height; |
| 70 } | 84 } |
| 71 | 85 |
| 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 { | 86 const std::string VideoCaptureDevice::Name::GetModel() const { |
| 139 // Both PID and VID are 4 characters. | 87 // Both PID and VID are 4 characters. |
| 140 if (unique_id_.size() < 2 * kVidPidSize) { | 88 if (unique_id_.size() < 2 * kVidPidSize) { |
| 141 return ""; | 89 return ""; |
| 142 } | 90 } |
| 143 | 91 |
| 144 // The last characters of device id is a concatenation of VID and then PID. | 92 // 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; | 93 const size_t vid_location = unique_id_.size() - 2 * kVidPidSize; |
| 146 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); | 94 std::string id_vendor = unique_id_.substr(vid_location, kVidPidSize); |
| 147 const size_t pid_location = unique_id_.size() - kVidPidSize; | 95 const size_t pid_location = unique_id_.size() - kVidPidSize; |
| 148 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); | 96 std::string id_product = unique_id_.substr(pid_location, kVidPidSize); |
| 149 | 97 |
| 150 return id_vendor + ":" + id_product; | 98 return id_vendor + ":" + id_product; |
| 151 } | 99 } |
| 152 | 100 |
| 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) | 101 VideoCaptureDeviceMac::VideoCaptureDeviceMac(const Name& device_name) |
| 165 : device_name_(device_name), | 102 : device_name_(device_name), |
| 166 tried_to_square_pixels_(false), | 103 tried_to_square_pixels_(false), |
| 167 task_runner_(base::MessageLoopProxy::current()), | 104 task_runner_(base::MessageLoopProxy::current()), |
| 168 state_(kNotInitialized), | 105 state_(kNotInitialized), |
| 169 capture_device_(nil), | 106 capture_device_(nil), |
| 170 weak_factory_(this) { | 107 weak_factory_(this) { |
| 171 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); | 108 final_resolution_selected_ = AVFoundationGlue::IsAVFoundationSupported(); |
| 172 } | 109 } |
| 173 | 110 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 DCHECK(state_ == kCapturing || state_ == kError) << state_; | 174 DCHECK(state_ == kCapturing || state_ == kError) << state_; |
| 238 [capture_device_ stopCapture]; | 175 [capture_device_ stopCapture]; |
| 239 | 176 |
| 240 [capture_device_ setCaptureDevice:nil]; | 177 [capture_device_ setCaptureDevice:nil]; |
| 241 [capture_device_ setFrameReceiver:nil]; | 178 [capture_device_ setFrameReceiver:nil]; |
| 242 client_.reset(); | 179 client_.reset(); |
| 243 state_ = kIdle; | 180 state_ = kIdle; |
| 244 tried_to_square_pixels_ = false; | 181 tried_to_square_pixels_ = false; |
| 245 } | 182 } |
| 246 | 183 |
| 247 bool VideoCaptureDeviceMac::Init() { | 184 bool VideoCaptureDeviceMac::Init( |
| 185 VideoCaptureDevice::Name::CaptureApiType capture_api_type) { | |
| 248 DCHECK(task_runner_->BelongsToCurrentThread()); | 186 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 249 DCHECK_EQ(state_, kNotInitialized); | 187 DCHECK_EQ(state_, kNotInitialized); |
| 250 | 188 |
| 251 // TODO(mcasas): The following check might not be necessary; if the device has | 189 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_ = | 190 capture_device_ = |
| 267 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; | 191 [[VideoCaptureDeviceAVFoundation alloc] initWithFrameReceiver:this]; |
| 268 } else { | 192 } else { |
| 269 capture_device_ = | 193 capture_device_ = |
| 270 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; | 194 [[VideoCaptureDeviceQTKit alloc] initWithFrameReceiver:this]; |
| 271 } | 195 } |
| 272 | 196 |
| 273 if (!capture_device_) | 197 if (!capture_device_) |
| 274 return false; | 198 return false; |
| 275 | 199 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 377 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() | 301 if (![capture_device_ setCaptureHeight:capture_format_.frame_size.height() |
| 378 width:capture_format_.frame_size.width() | 302 width:capture_format_.frame_size.width() |
| 379 frameRate:capture_format_.frame_rate]) { | 303 frameRate:capture_format_.frame_rate]) { |
| 380 ReceiveError("Could not configure capture device."); | 304 ReceiveError("Could not configure capture device."); |
| 381 return false; | 305 return false; |
| 382 } | 306 } |
| 383 return true; | 307 return true; |
| 384 } | 308 } |
| 385 | 309 |
| 386 } // namespace media | 310 } // namespace media |
| OLD | NEW |