OLD | NEW |
1 // Copyright 2014 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/win/video_capture_device_factory_win.h" | 5 #include "media/video/capture/win/video_capture_device_factory_win.h" |
6 | 6 |
7 #include <mfapi.h> | 7 #include <mfapi.h> |
8 #include <mferror.h> | 8 #include <mferror.h> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 209 } |
210 DLOG_IF(ERROR, FAILED(hr)) << "GetAllocatedString failed: " | 210 DLOG_IF(ERROR, FAILED(hr)) << "GetAllocatedString failed: " |
211 << logging::SystemErrorCodeToString(hr); | 211 << logging::SystemErrorCodeToString(hr); |
212 devices[i]->Release(); | 212 devices[i]->Release(); |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 static void GetDeviceSupportedFormatsDirectShow(const Name& device, | 216 static void GetDeviceSupportedFormatsDirectShow(const Name& device, |
217 VideoCaptureFormats* formats) { | 217 VideoCaptureFormats* formats) { |
218 DVLOG(1) << "GetDeviceSupportedFormatsDirectShow for " << device.name(); | 218 DVLOG(1) << "GetDeviceSupportedFormatsDirectShow for " << device.name(); |
219 ScopedComPtr<ICreateDevEnum> dev_enum; | |
220 HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL, | |
221 CLSCTX_INPROC); | |
222 if (FAILED(hr)) | |
223 return; | |
224 | |
225 ScopedComPtr<IEnumMoniker> enum_moniker; | |
226 hr = dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, | |
227 enum_moniker.Receive(), 0); | |
228 // CreateClassEnumerator returns S_FALSE on some Windows OS when no camera | |
229 // exists. Therefore the FAILED macro can't be used. | |
230 if (hr != S_OK) | |
231 return; | |
232 | |
233 // Walk the capture devices. No need to check for device presence again since | 219 // Walk the capture devices. No need to check for device presence again since |
234 // that is anyway needed in GetDeviceFilter(). "google camera adapter" and old | 220 // that is anyway needed in GetDeviceFilter(). "google camera adapter" and old |
235 // VFW devices are already skipped previously in GetDeviceNames() enumeration. | 221 // VFW devices are already skipped previously in GetDeviceNames() enumeration. |
236 base::win::ScopedComPtr<IBaseFilter> capture_filter; | 222 base::win::ScopedComPtr<IBaseFilter> capture_filter; |
237 hr = VideoCaptureDeviceWin::GetDeviceFilter(device.capabilities_id(), | 223 HRESULT hr = |
238 CLSID_VideoInputDeviceCategory, | 224 VideoCaptureDeviceWin::GetDeviceFilter(device.capabilities_id(), |
239 capture_filter.Receive()); | 225 CLSID_VideoInputDeviceCategory, |
| 226 capture_filter.Receive()); |
240 if (!capture_filter) { | 227 if (!capture_filter) { |
241 DLOG(ERROR) << "Failed to create capture filter: " | 228 DLOG(ERROR) << "Failed to create capture filter: " |
242 << logging::SystemErrorCodeToString(hr); | 229 << logging::SystemErrorCodeToString(hr); |
243 return; | 230 return; |
244 } | 231 } |
245 | 232 |
246 base::win::ScopedComPtr<IPin> output_capture_pin( | 233 base::win::ScopedComPtr<IPin> output_capture_pin( |
247 VideoCaptureDeviceWin::GetPin(capture_filter, | 234 VideoCaptureDeviceWin::GetPin(capture_filter, |
248 PINDIR_OUTPUT, | 235 PINDIR_OUTPUT, |
249 PIN_CATEGORY_CAPTURE, | 236 PIN_CATEGORY_CAPTURE, |
250 GUID_NULL)); | 237 GUID_NULL)); |
251 if (!output_capture_pin) { | 238 if (!output_capture_pin) { |
252 DLOG(ERROR) << "Failed to get capture output pin"; | 239 DLOG(ERROR) << "Failed to get capture output pin"; |
253 return; | 240 return; |
254 } | 241 } |
255 | 242 |
256 ScopedComPtr<IAMStreamConfig> stream_config; | 243 CapabilityList capabilities; |
257 hr = output_capture_pin.QueryInterface(stream_config.Receive()); | 244 if (!VideoCaptureDeviceWin::CreateCapabilityMap( |
258 if (FAILED(hr)) { | 245 output_capture_pin, capture_filter, &capabilities)) { |
259 DLOG(ERROR) << "Failed to get IAMStreamConfig interface from " | 246 DLOG(ERROR) << "CreateCapabilityMap failed"; |
260 "capture device: " << logging::SystemErrorCodeToString(hr); | |
261 return; | 247 return; |
262 } | 248 } |
263 | 249 |
264 int count = 0, size = 0; | 250 for (const CapabilityWin& capability : capabilities) |
265 hr = stream_config->GetNumberOfCapabilities(&count, &size); | 251 formats->push_back(capability.supported_format); |
266 if (FAILED(hr)) { | |
267 DLOG(ERROR) << "GetNumberOfCapabilities failed: " | |
268 << logging::SystemErrorCodeToString(hr); | |
269 return; | |
270 } | |
271 | |
272 scoped_ptr<BYTE[]> caps(new BYTE[size]); | |
273 for (int i = 0; i < count; ++i) { | |
274 VideoCaptureDeviceWin::ScopedMediaType media_type; | |
275 hr = stream_config->GetStreamCaps(i, media_type.Receive(), caps.get()); | |
276 // GetStreamCaps() may return S_FALSE, so don't use FAILED() or SUCCEED() | |
277 // macros here since they'll trigger incorrectly. | |
278 if (hr != S_OK || !media_type.get()) { | |
279 DLOG(ERROR) << "GetStreamCaps failed: " | |
280 << logging::SystemErrorCodeToString(hr); | |
281 return; | |
282 } | |
283 | |
284 if (media_type->majortype == MEDIATYPE_Video && | |
285 media_type->formattype == FORMAT_VideoInfo) { | |
286 VideoCaptureFormat format; | |
287 format.pixel_format = | |
288 VideoCaptureDeviceWin::TranslateMediaSubtypeToPixelFormat( | |
289 media_type->subtype); | |
290 if (format.pixel_format == PIXEL_FORMAT_UNKNOWN) | |
291 continue; | |
292 VIDEOINFOHEADER* h = | |
293 reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat); | |
294 format.frame_size.SetSize(h->bmiHeader.biWidth, | |
295 h->bmiHeader.biHeight); | |
296 // Trust the frame rate from the VIDEOINFOHEADER. | |
297 format.frame_rate = (h->AvgTimePerFrame > 0) ? | |
298 kSecondsToReferenceTime / static_cast<float>(h->AvgTimePerFrame) : | |
299 0.0f; | |
300 formats->push_back(format); | |
301 DVLOG(1) << device.name() << " " << format.ToString(); | |
302 } | |
303 } | |
304 } | 252 } |
305 | 253 |
306 static void GetDeviceSupportedFormatsMediaFoundation( | 254 static void GetDeviceSupportedFormatsMediaFoundation( |
307 const Name& device, | 255 const Name& device, |
308 VideoCaptureFormats* formats) { | 256 VideoCaptureFormats* formats) { |
309 DVLOG(1) << "GetDeviceSupportedFormatsMediaFoundation for " << device.name(); | 257 DVLOG(1) << "GetDeviceSupportedFormatsMediaFoundation for " << device.name(); |
310 ScopedComPtr<IMFMediaSource> source; | 258 ScopedComPtr<IMFMediaSource> source; |
311 if (!CreateVideoCaptureDeviceMediaFoundation(device.id().c_str(), | 259 if (!CreateVideoCaptureDeviceMediaFoundation(device.id().c_str(), |
312 source.Receive())) { | 260 source.Receive())) { |
313 return; | 261 return; |
314 } | 262 } |
315 | 263 |
316 base::win::ScopedComPtr<IMFSourceReader> reader; | 264 base::win::ScopedComPtr<IMFSourceReader> reader; |
317 HRESULT hr = | 265 HRESULT hr = |
318 MFCreateSourceReaderFromMediaSource(source, NULL, reader.Receive()); | 266 MFCreateSourceReaderFromMediaSource(source, NULL, reader.Receive()); |
319 if (FAILED(hr)) { | 267 if (FAILED(hr)) { |
320 DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource failed: " | 268 DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource failed: " |
321 << logging::SystemErrorCodeToString(hr); | 269 << logging::SystemErrorCodeToString(hr); |
322 return; | 270 return; |
323 } | 271 } |
324 | 272 |
325 DWORD stream_index = 0; | 273 CapabilityList capabilities; |
326 ScopedComPtr<IMFMediaType> type; | 274 VideoCaptureDeviceMFWin::FillCapabilities(reader, &capabilities); |
327 while (SUCCEEDED(reader->GetNativeMediaType( | |
328 kFirstVideoStream, stream_index, type.Receive()))) { | |
329 UINT32 width, height; | |
330 hr = MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width, &height); | |
331 if (FAILED(hr)) { | |
332 DLOG(ERROR) << "MFGetAttributeSize failed: " | |
333 << logging::SystemErrorCodeToString(hr); | |
334 return; | |
335 } | |
336 VideoCaptureFormat capture_format; | |
337 capture_format.frame_size.SetSize(width, height); | |
338 | 275 |
339 UINT32 numerator, denominator; | 276 for (const CapabilityWin& capability : capabilities) |
340 hr = MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator); | 277 formats->push_back(capability.supported_format); |
341 if (FAILED(hr)) { | |
342 DLOG(ERROR) << "MFGetAttributeSize failed: " | |
343 << logging::SystemErrorCodeToString(hr); | |
344 return; | |
345 } | |
346 capture_format.frame_rate = denominator | |
347 ? static_cast<float>(numerator) / denominator : 0.0f; | |
348 | |
349 GUID type_guid; | |
350 hr = type->GetGUID(MF_MT_SUBTYPE, &type_guid); | |
351 if (FAILED(hr)) { | |
352 DLOG(ERROR) << "GetGUID failed: " | |
353 << logging::SystemErrorCodeToString(hr); | |
354 return; | |
355 } | |
356 VideoCaptureDeviceMFWin::FormatFromGuid(type_guid, | |
357 &capture_format.pixel_format); | |
358 type.Release(); | |
359 formats->push_back(capture_format); | |
360 ++stream_index; | |
361 | |
362 DVLOG(1) << device.name() << " " << capture_format.ToString(); | |
363 } | |
364 } | 278 } |
365 | 279 |
366 // Returns true iff the current platform supports the Media Foundation API | 280 // Returns true iff the current platform supports the Media Foundation API |
367 // and that the DLLs are available. On Vista this API is an optional download | 281 // and that the DLLs are available. On Vista this API is an optional download |
368 // but the API is advertised as a part of Windows 7 and onwards. However, | 282 // but the API is advertised as a part of Windows 7 and onwards. However, |
369 // we've seen that the required DLLs are not available in some Win7 | 283 // we've seen that the required DLLs are not available in some Win7 |
370 // distributions such as Windows 7 N and Windows 7 KN. | 284 // distributions such as Windows 7 N and Windows 7 KN. |
371 // static | 285 // static |
372 bool VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() { | 286 bool VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() { |
373 // Even though the DLLs might be available on Vista, we get crashes | 287 // Even though the DLLs might be available on Vista, we get crashes |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 } | 360 } |
447 } | 361 } |
448 } | 362 } |
449 } | 363 } |
450 } | 364 } |
451 | 365 |
452 void VideoCaptureDeviceFactoryWin::GetDeviceSupportedFormats( | 366 void VideoCaptureDeviceFactoryWin::GetDeviceSupportedFormats( |
453 const Name& device, | 367 const Name& device, |
454 VideoCaptureFormats* formats) { | 368 VideoCaptureFormats* formats) { |
455 DCHECK(thread_checker_.CalledOnValidThread()); | 369 DCHECK(thread_checker_.CalledOnValidThread()); |
| 370 DCHECK(formats); |
456 if (use_media_foundation_) | 371 if (use_media_foundation_) |
457 GetDeviceSupportedFormatsMediaFoundation(device, formats); | 372 GetDeviceSupportedFormatsMediaFoundation(device, formats); |
458 else | 373 else |
459 GetDeviceSupportedFormatsDirectShow(device, formats); | 374 GetDeviceSupportedFormatsDirectShow(device, formats); |
| 375 for (const VideoCaptureFormat& format : *formats) |
| 376 DVLOG(1) << device.name() << " " << format.ToString(); |
460 } | 377 } |
461 | 378 |
462 } // namespace media | 379 } // namespace media |
OLD | NEW |