| 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &id, | 179 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &id, |
| 180 &id_size); | 180 &id_size); |
| 181 if (SUCCEEDED(hr)) { | 181 if (SUCCEEDED(hr)) { |
| 182 device_names->push_back(VideoCaptureDevice::Name( | 182 device_names->push_back(VideoCaptureDevice::Name( |
| 183 base::SysWideToUTF8(std::wstring(name, name_size)), | 183 base::SysWideToUTF8(std::wstring(name, name_size)), |
| 184 base::SysWideToUTF8(std::wstring(id, id_size)), | 184 base::SysWideToUTF8(std::wstring(id, id_size)), |
| 185 VideoCaptureDevice::Name::MEDIA_FOUNDATION)); | 185 VideoCaptureDevice::Name::MEDIA_FOUNDATION)); |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 if (FAILED(hr)) | 188 if (FAILED(hr)) |
| 189 DLOG(WARNING) << "GetAllocatedString failed: " << std::hex << hr; | 189 DPLOG(ERROR) << "Failed to GetAllocatedString"; |
| 190 |
| 190 devices[i]->Release(); | 191 devices[i]->Release(); |
| 191 } | 192 } |
| 192 } | 193 } |
| 193 | 194 |
| 194 static void GetDeviceSupportedFormatsDirectShow( | 195 static void GetDeviceSupportedFormatsDirectShow( |
| 195 const VideoCaptureDevice::Name& device, | 196 const VideoCaptureDevice::Name& device, |
| 196 VideoCaptureFormats* formats) { | 197 VideoCaptureFormats* formats) { |
| 197 DVLOG(1) << "GetDeviceSupportedFormatsDirectShow for " << device.name(); | 198 DVLOG(1) << "GetDeviceSupportedFormatsDirectShow for " << device.name(); |
| 198 ScopedComPtr<ICreateDevEnum> dev_enum; | 199 ScopedComPtr<ICreateDevEnum> dev_enum; |
| 199 HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL, | 200 HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL, |
| 200 CLSCTX_INPROC); | 201 CLSCTX_INPROC); |
| 201 if (FAILED(hr)) | 202 if (FAILED(hr)) |
| 202 return; | 203 return; |
| 203 | 204 |
| 204 ScopedComPtr<IEnumMoniker> enum_moniker; | 205 ScopedComPtr<IEnumMoniker> enum_moniker; |
| 205 hr = dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, | 206 hr = dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, |
| 206 enum_moniker.Receive(), 0); | 207 enum_moniker.Receive(), 0); |
| 207 // CreateClassEnumerator returns S_FALSE on some Windows OS when no camera | 208 // CreateClassEnumerator returns S_FALSE on some Windows OS when no camera |
| 208 // exists. Therefore the FAILED macro can't be used. | 209 // exists. Therefore the FAILED macro can't be used. |
| 209 if (hr != S_OK) | 210 if (hr != S_OK) |
| 210 return; | 211 return; |
| 211 | 212 |
| 212 // Walk the capture devices. No need to check for device presence again, that | 213 // Walk the capture devices. No need to check for device presence again, that |
| 213 // is caught in GetDeviceFilter(). "google camera adapter" and old VFW devices | 214 // is caught in GetDeviceFilter(). "google camera adapter" and old VFW devices |
| 214 // are already skipped in the previous GetDeviceNames() enumeration. | 215 // are already skipped in the previous GetDeviceNames() enumeration. |
| 215 base::win::ScopedComPtr<IBaseFilter> capture_filter; | 216 base::win::ScopedComPtr<IBaseFilter> capture_filter; |
| 216 hr = VideoCaptureDeviceWin::GetDeviceFilter(device, | 217 hr = VideoCaptureDeviceWin::GetDeviceFilter(device, |
| 217 capture_filter.Receive()); | 218 capture_filter.Receive()); |
| 218 if (!capture_filter) { | 219 if (!capture_filter) { |
| 219 DVLOG(2) << "Failed to create capture filter."; | 220 DPLOG(ERROR) << "Failed to create capture filter"; |
| 220 return; | 221 return; |
| 221 } | 222 } |
| 222 | 223 |
| 223 base::win::ScopedComPtr<IPin> output_capture_pin( | 224 base::win::ScopedComPtr<IPin> output_capture_pin( |
| 224 VideoCaptureDeviceWin::GetPin(capture_filter, | 225 VideoCaptureDeviceWin::GetPin(capture_filter, |
| 225 PINDIR_OUTPUT, | 226 PINDIR_OUTPUT, |
| 226 PIN_CATEGORY_CAPTURE)); | 227 PIN_CATEGORY_CAPTURE)); |
| 227 if (!output_capture_pin) { | 228 if (!output_capture_pin) { |
| 228 DVLOG(2) << "Failed to get capture output pin"; | 229 DPLOG(ERROR) << "Failed to get capture output pin"; |
| 229 return; | 230 return; |
| 230 } | 231 } |
| 231 | 232 |
| 232 ScopedComPtr<IAMStreamConfig> stream_config; | 233 ScopedComPtr<IAMStreamConfig> stream_config; |
| 233 hr = output_capture_pin.QueryInterface(stream_config.Receive()); | 234 hr = output_capture_pin.QueryInterface(stream_config.Receive()); |
| 234 if (FAILED(hr)) { | 235 if (FAILED(hr)) { |
| 235 DVLOG(2) << "Failed to get IAMStreamConfig interface from " | 236 DPLOG(ERROR) << "Failed to get IAMStreamConfig interface from " |
| 236 "capture device"; | 237 "capture device"; |
| 237 return; | 238 return; |
| 238 } | 239 } |
| 239 | 240 |
| 240 int count = 0, size = 0; | 241 int count = 0, size = 0; |
| 241 hr = stream_config->GetNumberOfCapabilities(&count, &size); | 242 hr = stream_config->GetNumberOfCapabilities(&count, &size); |
| 242 if (FAILED(hr)) { | 243 if (FAILED(hr)) { |
| 243 DVLOG(2) << "Failed to GetNumberOfCapabilities"; | 244 DPLOG(ERROR) << "Failed to GetNumberOfCapabilities"; |
| 244 return; | 245 return; |
| 245 } | 246 } |
| 246 | 247 |
| 247 scoped_ptr<BYTE[]> caps(new BYTE[size]); | 248 scoped_ptr<BYTE[]> caps(new BYTE[size]); |
| 248 for (int i = 0; i < count; ++i) { | 249 for (int i = 0; i < count; ++i) { |
| 249 VideoCaptureDeviceWin::ScopedMediaType media_type; | 250 VideoCaptureDeviceWin::ScopedMediaType media_type; |
| 250 hr = stream_config->GetStreamCaps(i, media_type.Receive(), caps.get()); | 251 hr = stream_config->GetStreamCaps(i, media_type.Receive(), caps.get()); |
| 251 // GetStreamCaps() may return S_FALSE, so don't use FAILED() or SUCCEED() | 252 // GetStreamCaps() may return S_FALSE, so don't use FAILED() or SUCCEED() |
| 252 // macros here since they'll trigger incorrectly. | 253 // macros here since they'll trigger incorrectly. |
| 253 if (hr != S_OK) { | 254 if (hr != S_OK) { |
| 254 DVLOG(2) << "Failed to GetStreamCaps"; | 255 DPLOG(ERROR) << "Failed to GetStreamCaps"; |
| 255 return; | 256 return; |
| 256 } | 257 } |
| 257 | 258 |
| 258 if (media_type->majortype == MEDIATYPE_Video && | 259 if (media_type->majortype == MEDIATYPE_Video && |
| 259 media_type->formattype == FORMAT_VideoInfo) { | 260 media_type->formattype == FORMAT_VideoInfo) { |
| 260 VideoCaptureFormat format; | 261 VideoCaptureFormat format; |
| 261 format.pixel_format = | 262 format.pixel_format = |
| 262 VideoCaptureDeviceWin::TranslateMediaSubtypeToPixelFormat( | 263 VideoCaptureDeviceWin::TranslateMediaSubtypeToPixelFormat( |
| 263 media_type->subtype); | 264 media_type->subtype); |
| 264 if (format.pixel_format == PIXEL_FORMAT_UNKNOWN) | 265 if (format.pixel_format == PIXEL_FORMAT_UNKNOWN) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 286 ScopedComPtr<IMFMediaSource> source; | 287 ScopedComPtr<IMFMediaSource> source; |
| 287 if (!CreateVideoCaptureDeviceMediaFoundation(device.id().c_str(), | 288 if (!CreateVideoCaptureDeviceMediaFoundation(device.id().c_str(), |
| 288 source.Receive())) { | 289 source.Receive())) { |
| 289 return; | 290 return; |
| 290 } | 291 } |
| 291 | 292 |
| 292 base::win::ScopedComPtr<IMFSourceReader> reader; | 293 base::win::ScopedComPtr<IMFSourceReader> reader; |
| 293 HRESULT hr = | 294 HRESULT hr = |
| 294 MFCreateSourceReaderFromMediaSource(source, NULL, reader.Receive()); | 295 MFCreateSourceReaderFromMediaSource(source, NULL, reader.Receive()); |
| 295 if (FAILED(hr)) { | 296 if (FAILED(hr)) { |
| 296 DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource: " << std::hex << hr; | 297 DPLOG(ERROR) << "Failed to MFCreateSourceReaderFromMediaSource"; |
| 297 return; | 298 return; |
| 298 } | 299 } |
| 299 | 300 |
| 300 DWORD stream_index = 0; | 301 DWORD stream_index = 0; |
| 301 ScopedComPtr<IMFMediaType> type; | 302 ScopedComPtr<IMFMediaType> type; |
| 302 for (hr = reader->GetNativeMediaType(kFirstVideoStream, stream_index, | 303 for (hr = reader->GetNativeMediaType(kFirstVideoStream, stream_index, |
| 303 type.Receive()); | 304 type.Receive()); |
| 304 SUCCEEDED(hr); | 305 SUCCEEDED(hr); |
| 305 hr = reader->GetNativeMediaType(kFirstVideoStream, stream_index, | 306 hr = reader->GetNativeMediaType(kFirstVideoStream, stream_index, |
| 306 type.Receive())) { | 307 type.Receive())) { |
| 307 UINT32 width, height; | 308 UINT32 width, height; |
| 308 hr = MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width, &height); | 309 hr = MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width, &height); |
| 309 if (FAILED(hr)) { | 310 if (FAILED(hr)) { |
| 310 DLOG(ERROR) << "MFGetAttributeSize: " << std::hex << hr; | 311 DPLOG(ERROR) << "Failed to MFGetAttributeSize"; |
| 311 return; | 312 return; |
| 312 } | 313 } |
| 313 VideoCaptureFormat capture_format; | 314 VideoCaptureFormat capture_format; |
| 314 capture_format.frame_size.SetSize(width, height); | 315 capture_format.frame_size.SetSize(width, height); |
| 315 | 316 |
| 316 UINT32 numerator, denominator; | 317 UINT32 numerator, denominator; |
| 317 hr = MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator); | 318 hr = MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator); |
| 318 if (FAILED(hr)) { | 319 if (FAILED(hr)) { |
| 319 DLOG(ERROR) << "MFGetAttributeSize: " << std::hex << hr; | 320 DPLOG(ERROR) << "Failed to MFGetAttributeSize"; |
| 320 return; | 321 return; |
| 321 } | 322 } |
| 322 capture_format.frame_rate = denominator | 323 capture_format.frame_rate = denominator |
| 323 ? static_cast<float>(numerator) / denominator : 0.0f; | 324 ? static_cast<float>(numerator) / denominator : 0.0f; |
| 324 | 325 |
| 325 GUID type_guid; | 326 GUID type_guid; |
| 326 hr = type->GetGUID(MF_MT_SUBTYPE, &type_guid); | 327 hr = type->GetGUID(MF_MT_SUBTYPE, &type_guid); |
| 327 if (FAILED(hr)) { | 328 if (FAILED(hr)) { |
| 328 DLOG(ERROR) << "GetGUID: " << std::hex << hr; | 329 DPLOG(ERROR) << "Failed ot GetGUID"; |
| 329 return; | 330 return; |
| 330 } | 331 } |
| 331 VideoCaptureDeviceMFWin::FormatFromGuid(type_guid, | 332 VideoCaptureDeviceMFWin::FormatFromGuid(type_guid, |
| 332 &capture_format.pixel_format); | 333 &capture_format.pixel_format); |
| 333 type.Release(); | 334 type.Release(); |
| 334 formats->push_back(capture_format); | 335 formats->push_back(capture_format); |
| 335 ++stream_index; | 336 ++stream_index; |
| 336 | 337 |
| 337 DVLOG(1) << device.name() << " resolution: " | 338 DVLOG(1) << device.name() << " resolution: " |
| 338 << capture_format.frame_size.ToString() << ", fps: " | 339 << capture_format.frame_size.ToString() << ", fps: " |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 const VideoCaptureDevice::Name& device, | 411 const VideoCaptureDevice::Name& device, |
| 411 VideoCaptureFormats* formats) { | 412 VideoCaptureFormats* formats) { |
| 412 DCHECK(thread_checker_.CalledOnValidThread()); | 413 DCHECK(thread_checker_.CalledOnValidThread()); |
| 413 if (use_media_foundation_) | 414 if (use_media_foundation_) |
| 414 GetDeviceSupportedFormatsMediaFoundation(device, formats); | 415 GetDeviceSupportedFormatsMediaFoundation(device, formats); |
| 415 else | 416 else |
| 416 GetDeviceSupportedFormatsDirectShow(device, formats); | 417 GetDeviceSupportedFormatsDirectShow(device, formats); |
| 417 } | 418 } |
| 418 | 419 |
| 419 } // namespace media | 420 } // namespace media |
| OLD | NEW |