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 |
219 // Walk the capture devices. No need to check for device presence again since | 233 // Walk the capture devices. No need to check for device presence again since |
220 // that is anyway needed in GetDeviceFilter(). "google camera adapter" and old | 234 // that is anyway needed in GetDeviceFilter(). "google camera adapter" and old |
221 // VFW devices are already skipped previously in GetDeviceNames() enumeration. | 235 // VFW devices are already skipped previously in GetDeviceNames() enumeration. |
222 base::win::ScopedComPtr<IBaseFilter> capture_filter; | 236 base::win::ScopedComPtr<IBaseFilter> capture_filter; |
223 HRESULT hr = | 237 hr = VideoCaptureDeviceWin::GetDeviceFilter(device.capabilities_id(), |
224 VideoCaptureDeviceWin::GetDeviceFilter(device.capabilities_id(), | 238 CLSID_VideoInputDeviceCategory, |
225 CLSID_VideoInputDeviceCategory, | 239 capture_filter.Receive()); |
226 capture_filter.Receive()); | |
227 if (!capture_filter) { | 240 if (!capture_filter) { |
228 DLOG(ERROR) << "Failed to create capture filter: " | 241 DLOG(ERROR) << "Failed to create capture filter: " |
229 << logging::SystemErrorCodeToString(hr); | 242 << logging::SystemErrorCodeToString(hr); |
230 return; | 243 return; |
231 } | 244 } |
232 | 245 |
233 base::win::ScopedComPtr<IPin> output_capture_pin( | 246 base::win::ScopedComPtr<IPin> output_capture_pin( |
234 VideoCaptureDeviceWin::GetPin(capture_filter, | 247 VideoCaptureDeviceWin::GetPin(capture_filter, |
235 PINDIR_OUTPUT, | 248 PINDIR_OUTPUT, |
236 PIN_CATEGORY_CAPTURE, | 249 PIN_CATEGORY_CAPTURE, |
237 GUID_NULL)); | 250 GUID_NULL)); |
238 if (!output_capture_pin) { | 251 if (!output_capture_pin) { |
239 DLOG(ERROR) << "Failed to get capture output pin"; | 252 DLOG(ERROR) << "Failed to get capture output pin"; |
240 return; | 253 return; |
241 } | 254 } |
242 | 255 |
243 CapabilityList capabilities; | 256 ScopedComPtr<IAMStreamConfig> stream_config; |
244 if (!VideoCaptureDeviceWin::CreateCapabilityMap( | 257 hr = output_capture_pin.QueryInterface(stream_config.Receive()); |
245 output_capture_pin, capture_filter, &capabilities)) { | 258 if (FAILED(hr)) { |
246 DLOG(ERROR) << "CreateCapabilityMap failed"; | 259 DLOG(ERROR) << "Failed to get IAMStreamConfig interface from " |
| 260 "capture device: " << logging::SystemErrorCodeToString(hr); |
247 return; | 261 return; |
248 } | 262 } |
249 | 263 |
250 for (const CapabilityWin& capability : capabilities) | 264 int count = 0, size = 0; |
251 formats->push_back(capability.supported_format); | 265 hr = stream_config->GetNumberOfCapabilities(&count, &size); |
| 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 } |
252 } | 304 } |
253 | 305 |
254 static void GetDeviceSupportedFormatsMediaFoundation( | 306 static void GetDeviceSupportedFormatsMediaFoundation( |
255 const Name& device, | 307 const Name& device, |
256 VideoCaptureFormats* formats) { | 308 VideoCaptureFormats* formats) { |
257 DVLOG(1) << "GetDeviceSupportedFormatsMediaFoundation for " << device.name(); | 309 DVLOG(1) << "GetDeviceSupportedFormatsMediaFoundation for " << device.name(); |
258 ScopedComPtr<IMFMediaSource> source; | 310 ScopedComPtr<IMFMediaSource> source; |
259 if (!CreateVideoCaptureDeviceMediaFoundation(device.id().c_str(), | 311 if (!CreateVideoCaptureDeviceMediaFoundation(device.id().c_str(), |
260 source.Receive())) { | 312 source.Receive())) { |
261 return; | 313 return; |
262 } | 314 } |
263 | 315 |
264 base::win::ScopedComPtr<IMFSourceReader> reader; | 316 base::win::ScopedComPtr<IMFSourceReader> reader; |
265 HRESULT hr = | 317 HRESULT hr = |
266 MFCreateSourceReaderFromMediaSource(source, NULL, reader.Receive()); | 318 MFCreateSourceReaderFromMediaSource(source, NULL, reader.Receive()); |
267 if (FAILED(hr)) { | 319 if (FAILED(hr)) { |
268 DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource failed: " | 320 DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource failed: " |
269 << logging::SystemErrorCodeToString(hr); | 321 << logging::SystemErrorCodeToString(hr); |
270 return; | 322 return; |
271 } | 323 } |
272 | 324 |
273 CapabilityList capabilities; | 325 DWORD stream_index = 0; |
274 VideoCaptureDeviceMFWin::FillCapabilities(reader, &capabilities); | 326 ScopedComPtr<IMFMediaType> type; |
| 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); |
275 | 338 |
276 for (const CapabilityWin& capability : capabilities) | 339 UINT32 numerator, denominator; |
277 formats->push_back(capability.supported_format); | 340 hr = MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator); |
| 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 } |
278 } | 364 } |
279 | 365 |
280 // Returns true iff the current platform supports the Media Foundation API | 366 // Returns true iff the current platform supports the Media Foundation API |
281 // and that the DLLs are available. On Vista this API is an optional download | 367 // and that the DLLs are available. On Vista this API is an optional download |
282 // but the API is advertised as a part of Windows 7 and onwards. However, | 368 // but the API is advertised as a part of Windows 7 and onwards. However, |
283 // we've seen that the required DLLs are not available in some Win7 | 369 // we've seen that the required DLLs are not available in some Win7 |
284 // distributions such as Windows 7 N and Windows 7 KN. | 370 // distributions such as Windows 7 N and Windows 7 KN. |
285 // static | 371 // static |
286 bool VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() { | 372 bool VideoCaptureDeviceFactoryWin::PlatformSupportsMediaFoundation() { |
287 // Even though the DLLs might be available on Vista, we get crashes | 373 // 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... |
360 } | 446 } |
361 } | 447 } |
362 } | 448 } |
363 } | 449 } |
364 } | 450 } |
365 | 451 |
366 void VideoCaptureDeviceFactoryWin::GetDeviceSupportedFormats( | 452 void VideoCaptureDeviceFactoryWin::GetDeviceSupportedFormats( |
367 const Name& device, | 453 const Name& device, |
368 VideoCaptureFormats* formats) { | 454 VideoCaptureFormats* formats) { |
369 DCHECK(thread_checker_.CalledOnValidThread()); | 455 DCHECK(thread_checker_.CalledOnValidThread()); |
370 DCHECK(formats); | |
371 if (use_media_foundation_) | 456 if (use_media_foundation_) |
372 GetDeviceSupportedFormatsMediaFoundation(device, formats); | 457 GetDeviceSupportedFormatsMediaFoundation(device, formats); |
373 else | 458 else |
374 GetDeviceSupportedFormatsDirectShow(device, formats); | 459 GetDeviceSupportedFormatsDirectShow(device, formats); |
375 for (const VideoCaptureFormat& format : *formats) | |
376 DVLOG(1) << device.name() << " " << format.ToString(); | |
377 } | 460 } |
378 | 461 |
379 } // namespace media | 462 } // namespace media |
OLD | NEW |