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
3 // found in the LICENSE file.
5 #include "media/video/capture/win/video_capture_device_mf_win.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/win/scoped_co_mem.h"
16 #include "base/win/windows_version.h"
17 #include "media/video/capture/win/capability_list_win.h"
19 using base::win::ScopedCoMem;
20 using base::win::ScopedComPtr;
25 // In Windows device identifiers, the USB VID and PID are preceded by the string
26 // "vid_" or "pid_". The identifiers are each 4 bytes long.
27 const char kVidPrefix[] = "vid_"; // Also contains '\0'.
28 const char kPidPrefix[] = "pid_"; // Also contains '\0'.
29 const size_t kVidPidSize = 4;
31 class MFInitializerSingleton {
33 MFInitializerSingleton() { MFStartup(MF_VERSION, MFSTARTUP_LITE); }
34 ~MFInitializerSingleton() { MFShutdown(); }
37 static base::LazyInstance<MFInitializerSingleton> g_mf_initialize =
38 LAZY_INSTANCE_INITIALIZER;
41 g_mf_initialize.Get();
44 bool PrepareVideoCaptureAttributes(IMFAttributes** attributes, int count) {
47 if (FAILED(MFCreateAttributes(attributes, count)))
50 return SUCCEEDED((*attributes)->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
51 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID));
54 bool EnumerateVideoDevices(IMFActivate*** devices,
56 ScopedComPtr<IMFAttributes> attributes;
57 if (!PrepareVideoCaptureAttributes(attributes.Receive(), 1))
60 return SUCCEEDED(MFEnumDeviceSources(attributes, devices, count));
63 bool CreateVideoCaptureDevice(const char* sym_link, IMFMediaSource** source) {
64 ScopedComPtr<IMFAttributes> attributes;
65 if (!PrepareVideoCaptureAttributes(attributes.Receive(), 2))
68 attributes->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
69 base::SysUTF8ToWide(sym_link).c_str());
71 return SUCCEEDED(MFCreateDeviceSource(attributes, source));
74 bool FormatFromGuid(const GUID& guid, VideoPixelFormat* format) {
77 const VideoPixelFormat format;
78 } static const kFormatMap[] = {
79 { MFVideoFormat_I420, PIXEL_FORMAT_I420 },
80 { MFVideoFormat_YUY2, PIXEL_FORMAT_YUY2 },
81 { MFVideoFormat_UYVY, PIXEL_FORMAT_UYVY },
82 { MFVideoFormat_RGB24, PIXEL_FORMAT_RGB24 },
83 { MFVideoFormat_ARGB32, PIXEL_FORMAT_ARGB },
84 { MFVideoFormat_MJPG, PIXEL_FORMAT_MJPEG },
85 { MFVideoFormat_YV12, PIXEL_FORMAT_YV12 },
88 for (int i = 0; i < arraysize(kFormatMap); ++i) {
89 if (kFormatMap[i].guid == guid) {
90 *format = kFormatMap[i].format;
98 bool GetFrameSize(IMFMediaType* type, gfx::Size* frame_size) {
99 UINT32 width32, height32;
100 if (FAILED(MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width32, &height32)))
102 frame_size->SetSize(width32, height32);
106 bool GetFrameRate(IMFMediaType* type,
107 int* frame_rate_numerator,
108 int* frame_rate_denominator) {
109 UINT32 numerator, denominator;
110 if (FAILED(MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator,
115 *frame_rate_numerator = numerator;
116 *frame_rate_denominator = denominator;
120 bool FillCapabilitiesFromType(IMFMediaType* type,
121 VideoCaptureCapabilityWin* capability) {
123 if (FAILED(type->GetGUID(MF_MT_SUBTYPE, &type_guid)) ||
124 !GetFrameSize(type, &capability->supported_format.frame_size) ||
126 &capability->frame_rate_numerator,
127 &capability->frame_rate_denominator) ||
128 !FormatFromGuid(type_guid, &capability->supported_format.pixel_format)) {
131 // Keep the integer version of the frame_rate for (potential) returns.
132 capability->supported_format.frame_rate =
133 capability->frame_rate_numerator / capability->frame_rate_denominator;
138 HRESULT FillCapabilities(IMFSourceReader* source,
139 CapabilityList* capabilities) {
140 DWORD stream_index = 0;
141 ScopedComPtr<IMFMediaType> type;
143 while (SUCCEEDED(hr = source->GetNativeMediaType(
144 MF_SOURCE_READER_FIRST_VIDEO_STREAM, stream_index, type.Receive()))) {
145 VideoCaptureCapabilityWin capability(stream_index++);
146 if (FillCapabilitiesFromType(type, &capability))
147 capabilities->Add(capability);
151 if (capabilities->empty() && (SUCCEEDED(hr) || hr == MF_E_NO_MORE_TYPES))
152 hr = HRESULT_FROM_WIN32(ERROR_EMPTY);
154 return (hr == MF_E_NO_MORE_TYPES) ? S_OK : hr;
157 bool LoadMediaFoundationDlls() {
158 static const wchar_t* const kMfDLLs[] = {
159 L"%WINDIR%\\system32\\mf.dll",
160 L"%WINDIR%\\system32\\mfplat.dll",
161 L"%WINDIR%\\system32\\mfreadwrite.dll",
164 for (int i = 0; i < arraysize(kMfDLLs); ++i) {
165 wchar_t path[MAX_PATH] = {0};
166 ExpandEnvironmentStringsW(kMfDLLs[i], path, arraysize(path));
167 if (!LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH))
176 class MFReaderCallback
177 : public base::RefCountedThreadSafe<MFReaderCallback>,
178 public IMFSourceReaderCallback {
180 MFReaderCallback(VideoCaptureDeviceMFWin* observer)
181 : observer_(observer), wait_event_(NULL) {
184 void SetSignalOnFlush(base::WaitableEvent* event) {
188 STDMETHOD(QueryInterface)(REFIID riid, void** object) {
189 if (riid != IID_IUnknown && riid != IID_IMFSourceReaderCallback)
190 return E_NOINTERFACE;
191 *object = static_cast<IMFSourceReaderCallback*>(this);
196 STDMETHOD_(ULONG, AddRef)() {
197 base::RefCountedThreadSafe<MFReaderCallback>::AddRef();
201 STDMETHOD_(ULONG, Release)() {
202 base::RefCountedThreadSafe<MFReaderCallback>::Release();
206 STDMETHOD(OnReadSample)(HRESULT status, DWORD stream_index,
207 DWORD stream_flags, LONGLONG time_stamp, IMFSample* sample) {
208 base::TimeTicks stamp(base::TimeTicks::Now());
210 observer_->OnIncomingCapturedFrame(NULL, 0, stamp, 0);
215 sample->GetBufferCount(&count);
217 for (DWORD i = 0; i < count; ++i) {
218 ScopedComPtr<IMFMediaBuffer> buffer;
219 sample->GetBufferByIndex(i, buffer.Receive());
221 DWORD length = 0, max_length = 0;
223 buffer->Lock(&data, &max_length, &length);
224 observer_->OnIncomingCapturedFrame(data, length, stamp, 0);
231 STDMETHOD(OnFlush)(DWORD stream_index) {
233 wait_event_->Signal();
239 STDMETHOD(OnEvent)(DWORD stream_index, IMFMediaEvent* event) {
245 friend class base::RefCountedThreadSafe<MFReaderCallback>;
246 ~MFReaderCallback() {}
248 VideoCaptureDeviceMFWin* observer_;
249 base::WaitableEvent* wait_event_;
253 bool VideoCaptureDeviceMFWin::PlatformSupported() {
254 // Even though the DLLs might be available on Vista, we get crashes
255 // when running our tests on the build bots.
256 if (base::win::GetVersion() < base::win::VERSION_WIN7)
259 static bool g_dlls_available = LoadMediaFoundationDlls();
260 return g_dlls_available;
264 void VideoCaptureDeviceMFWin::GetDeviceNames(Names* device_names) {
265 ScopedCoMem<IMFActivate*> devices;
267 if (!EnumerateVideoDevices(&devices, &count))
271 for (UINT32 i = 0; i < count; ++i) {
272 UINT32 name_size, id_size;
273 ScopedCoMem<wchar_t> name, id;
274 if (SUCCEEDED(hr = devices[i]->GetAllocatedString(
275 MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &name, &name_size)) &&
276 SUCCEEDED(hr = devices[i]->GetAllocatedString(
277 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &id,
279 std::wstring name_w(name, name_size), id_w(id, id_size);
280 Name device(base::SysWideToUTF8(name_w), base::SysWideToUTF8(id_w),
281 Name::MEDIA_FOUNDATION);
282 device_names->push_back(device);
284 DLOG(WARNING) << "GetAllocatedString failed: " << std::hex << hr;
286 devices[i]->Release();
291 void VideoCaptureDeviceMFWin::GetDeviceSupportedFormats(const Name& device,
292 VideoCaptureFormats* formats) {
293 ScopedComPtr<IMFMediaSource> source;
294 if (!CreateVideoCaptureDevice(device.id().c_str(), source.Receive()))
298 base::win::ScopedComPtr<IMFSourceReader> reader;
299 if (FAILED(hr = MFCreateSourceReaderFromMediaSource(source, NULL,
300 reader.Receive()))) {
301 DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource: " << std::hex << hr;
305 DWORD stream_index = 0;
306 ScopedComPtr<IMFMediaType> type;
307 while (SUCCEEDED(hr = reader->GetNativeMediaType(
308 MF_SOURCE_READER_FIRST_VIDEO_STREAM, stream_index, type.Receive()))) {
309 UINT32 width, height;
310 hr = MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width, &height);
312 DLOG(ERROR) << "MFGetAttributeSize: " << std::hex << hr;
315 VideoCaptureFormat capture_format;
316 capture_format.frame_size.SetSize(width, height);
318 UINT32 numerator, denominator;
319 hr = MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator);
321 DLOG(ERROR) << "MFGetAttributeSize: " << std::hex << hr;
324 capture_format.frame_rate = denominator ? numerator / denominator : 0;
327 hr = type->GetGUID(MF_MT_SUBTYPE, &type_guid);
329 DLOG(ERROR) << "GetGUID: " << std::hex << hr;
332 FormatFromGuid(type_guid, &capture_format.pixel_format);
334 formats->push_back(capture_format);
337 DVLOG(1) << device.name() << " resolution: "
338 << capture_format.frame_size.ToString() << ", fps: "
339 << capture_format.frame_rate << ", pixel format: "
340 << capture_format.pixel_format;
344 const std::string VideoCaptureDevice::Name::GetModel() const {
345 const size_t vid_prefix_size = sizeof(kVidPrefix) - 1;
346 const size_t pid_prefix_size = sizeof(kPidPrefix) - 1;
347 const size_t vid_location = unique_id_.find(kVidPrefix);
348 if (vid_location == std::string::npos ||
349 vid_location + vid_prefix_size + kVidPidSize > unique_id_.size()) {
352 const size_t pid_location = unique_id_.find(kPidPrefix);
353 if (pid_location == std::string::npos ||
354 pid_location + pid_prefix_size + kVidPidSize > unique_id_.size()) {
357 std::string id_vendor =
358 unique_id_.substr(vid_location + vid_prefix_size, kVidPidSize);
359 std::string id_product =
360 unique_id_.substr(pid_location + pid_prefix_size, kVidPidSize);
361 return id_vendor + ":" + id_product;
364 VideoCaptureDeviceMFWin::VideoCaptureDeviceMFWin(const Name& device_name)
365 : name_(device_name), capture_(0) {
369 VideoCaptureDeviceMFWin::~VideoCaptureDeviceMFWin() {
370 DCHECK(CalledOnValidThread());
373 bool VideoCaptureDeviceMFWin::Init() {
374 DCHECK(CalledOnValidThread());
377 ScopedComPtr<IMFMediaSource> source;
378 if (!CreateVideoCaptureDevice(name_.id().c_str(), source.Receive()))
381 ScopedComPtr<IMFAttributes> attributes;
382 MFCreateAttributes(attributes.Receive(), 1);
385 callback_ = new MFReaderCallback(this);
386 attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, callback_.get());
388 return SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attributes,
392 void VideoCaptureDeviceMFWin::AllocateAndStart(
393 const VideoCaptureParams& params,
394 scoped_ptr<VideoCaptureDevice::Client> client) {
395 DCHECK(CalledOnValidThread());
397 base::AutoLock lock(lock_);
399 client_ = client.Pass();
400 DCHECK_EQ(capture_, false);
402 CapabilityList capabilities;
404 if (!reader_ || FAILED(hr = FillCapabilities(reader_, &capabilities))) {
409 VideoCaptureCapabilityWin found_capability =
410 capabilities.GetBestMatchedFormat(
411 params.requested_format.frame_size.width(),
412 params.requested_format.frame_size.height(),
413 params.requested_format.frame_rate);
415 ScopedComPtr<IMFMediaType> type;
416 if (FAILED(hr = reader_->GetNativeMediaType(
417 MF_SOURCE_READER_FIRST_VIDEO_STREAM, found_capability.stream_index,
419 FAILED(hr = reader_->SetCurrentMediaType(
420 MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, type))) {
425 if (FAILED(hr = reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0,
426 NULL, NULL, NULL, NULL))) {
430 capture_format_ = found_capability.supported_format;
434 void VideoCaptureDeviceMFWin::StopAndDeAllocate() {
435 DCHECK(CalledOnValidThread());
436 base::WaitableEvent flushed(false, false);
437 const int kFlushTimeOutInMs = 1000;
440 base::AutoLock lock(lock_);
443 callback_->SetSignalOnFlush(&flushed);
444 HRESULT hr = reader_->Flush(MF_SOURCE_READER_ALL_STREAMS);
445 wait = SUCCEEDED(hr);
447 callback_->SetSignalOnFlush(NULL);
453 // If the device has been unplugged, the Flush() won't trigger the event
454 // and a timeout will happen.
455 // TODO(tommi): Hook up the IMFMediaEventGenerator notifications API and
456 // do not wait at all after getting MEVideoCaptureDeviceRemoved event.
459 flushed.TimedWait(base::TimeDelta::FromMilliseconds(kFlushTimeOutInMs));
462 void VideoCaptureDeviceMFWin::OnIncomingCapturedFrame(
465 const base::TimeTicks& time_stamp,
467 base::AutoLock lock(lock_);
468 if (data && client_.get())
469 client_->OnIncomingCapturedFrame(data,
476 HRESULT hr = reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0,
477 NULL, NULL, NULL, NULL);
479 // If running the *VideoCap* unit tests on repeat, this can sometimes
480 // fail with HRESULT_FROM_WINHRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION).
481 // It's not clear to me why this is, but it is possible that it has
482 // something to do with this bug:
483 // http://support.microsoft.com/kb/979567
489 void VideoCaptureDeviceMFWin::OnError(HRESULT hr) {
490 std::string log_msg = base::StringPrintf("VideoCaptureDeviceMFWin: %x", hr);
491 DLOG(ERROR) << log_msg;
493 client_->OnError(log_msg);