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