Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / video / capture / win / video_capture_device_mf_win.cc
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.
4
5 #include "media/video/capture/win/video_capture_device_mf_win.h"
6
7 #include <mfapi.h>
8 #include <mferror.h>
9
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"
18
19 using base::win::ScopedCoMem;
20 using base::win::ScopedComPtr;
21
22 namespace media {
23 namespace {
24
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;
30
31 class MFInitializerSingleton {
32  public:
33   MFInitializerSingleton() { MFStartup(MF_VERSION, MFSTARTUP_LITE); }
34   ~MFInitializerSingleton() { MFShutdown(); }
35 };
36
37 static base::LazyInstance<MFInitializerSingleton> g_mf_initialize =
38     LAZY_INSTANCE_INITIALIZER;
39
40 void EnsureMFInit() {
41   g_mf_initialize.Get();
42 }
43
44 bool PrepareVideoCaptureAttributes(IMFAttributes** attributes, int count) {
45   EnsureMFInit();
46
47   if (FAILED(MFCreateAttributes(attributes, count)))
48     return false;
49
50   return SUCCEEDED((*attributes)->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
51       MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID));
52 }
53
54 bool EnumerateVideoDevices(IMFActivate*** devices,
55                            UINT32* count) {
56   ScopedComPtr<IMFAttributes> attributes;
57   if (!PrepareVideoCaptureAttributes(attributes.Receive(), 1))
58     return false;
59
60   return SUCCEEDED(MFEnumDeviceSources(attributes, devices, count));
61 }
62
63 bool CreateVideoCaptureDevice(const char* sym_link, IMFMediaSource** source) {
64   ScopedComPtr<IMFAttributes> attributes;
65   if (!PrepareVideoCaptureAttributes(attributes.Receive(), 2))
66     return false;
67
68   attributes->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
69                         base::SysUTF8ToWide(sym_link).c_str());
70
71   return SUCCEEDED(MFCreateDeviceSource(attributes, source));
72 }
73
74 bool FormatFromGuid(const GUID& guid, VideoPixelFormat* format) {
75   struct {
76     const GUID& guid;
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 },
86   };
87
88   for (int i = 0; i < arraysize(kFormatMap); ++i) {
89     if (kFormatMap[i].guid == guid) {
90       *format = kFormatMap[i].format;
91       return true;
92     }
93   }
94
95   return false;
96 }
97
98 bool GetFrameSize(IMFMediaType* type, gfx::Size* frame_size) {
99   UINT32 width32, height32;
100   if (FAILED(MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width32, &height32)))
101     return false;
102   frame_size->SetSize(width32, height32);
103   return true;
104 }
105
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,
111                                  &denominator))||
112       !denominator) {
113     return false;
114   }
115   *frame_rate_numerator = numerator;
116   *frame_rate_denominator = denominator;
117   return true;
118 }
119
120 bool FillCapabilitiesFromType(IMFMediaType* type,
121                               VideoCaptureCapabilityWin* capability) {
122   GUID type_guid;
123   if (FAILED(type->GetGUID(MF_MT_SUBTYPE, &type_guid)) ||
124       !GetFrameSize(type, &capability->supported_format.frame_size) ||
125       !GetFrameRate(type,
126                     &capability->frame_rate_numerator,
127                     &capability->frame_rate_denominator) ||
128       !FormatFromGuid(type_guid, &capability->supported_format.pixel_format)) {
129     return false;
130   }
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;
134
135   return true;
136 }
137
138 HRESULT FillCapabilities(IMFSourceReader* source,
139                          CapabilityList* capabilities) {
140   DWORD stream_index = 0;
141   ScopedComPtr<IMFMediaType> type;
142   HRESULT hr;
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);
148     type.Release();
149   }
150
151   if (capabilities->empty() && (SUCCEEDED(hr) || hr == MF_E_NO_MORE_TYPES))
152     hr = HRESULT_FROM_WIN32(ERROR_EMPTY);
153
154   return (hr == MF_E_NO_MORE_TYPES) ? S_OK : hr;
155 }
156
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",
162   };
163
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))
168       return false;
169   }
170
171   return true;
172 }
173
174 }  // namespace
175
176 class MFReaderCallback
177     : public base::RefCountedThreadSafe<MFReaderCallback>,
178       public IMFSourceReaderCallback {
179  public:
180   MFReaderCallback(VideoCaptureDeviceMFWin* observer)
181       : observer_(observer), wait_event_(NULL) {
182   }
183
184   void SetSignalOnFlush(base::WaitableEvent* event) {
185     wait_event_ = event;
186   }
187
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);
192     AddRef();
193     return S_OK;
194   }
195
196   STDMETHOD_(ULONG, AddRef)() {
197     base::RefCountedThreadSafe<MFReaderCallback>::AddRef();
198     return 1U;
199   }
200
201   STDMETHOD_(ULONG, Release)() {
202     base::RefCountedThreadSafe<MFReaderCallback>::Release();
203     return 1U;
204   }
205
206   STDMETHOD(OnReadSample)(HRESULT status, DWORD stream_index,
207       DWORD stream_flags, LONGLONG time_stamp, IMFSample* sample) {
208     base::TimeTicks stamp(base::TimeTicks::Now());
209     if (!sample) {
210       observer_->OnIncomingCapturedFrame(NULL, 0, stamp, 0);
211       return S_OK;
212     }
213
214     DWORD count = 0;
215     sample->GetBufferCount(&count);
216
217     for (DWORD i = 0; i < count; ++i) {
218       ScopedComPtr<IMFMediaBuffer> buffer;
219       sample->GetBufferByIndex(i, buffer.Receive());
220       if (buffer) {
221         DWORD length = 0, max_length = 0;
222         BYTE* data = NULL;
223         buffer->Lock(&data, &max_length, &length);
224         observer_->OnIncomingCapturedFrame(data, length, stamp, 0);
225         buffer->Unlock();
226       }
227     }
228     return S_OK;
229   }
230
231   STDMETHOD(OnFlush)(DWORD stream_index) {
232     if (wait_event_) {
233       wait_event_->Signal();
234       wait_event_ = NULL;
235     }
236     return S_OK;
237   }
238
239   STDMETHOD(OnEvent)(DWORD stream_index, IMFMediaEvent* event) {
240     NOTIMPLEMENTED();
241     return S_OK;
242   }
243
244  private:
245   friend class base::RefCountedThreadSafe<MFReaderCallback>;
246   ~MFReaderCallback() {}
247
248   VideoCaptureDeviceMFWin* observer_;
249   base::WaitableEvent* wait_event_;
250 };
251
252 // static
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)
257     return false;
258
259   static bool g_dlls_available = LoadMediaFoundationDlls();
260   return g_dlls_available;
261 }
262
263 // static
264 void VideoCaptureDeviceMFWin::GetDeviceNames(Names* device_names) {
265   ScopedCoMem<IMFActivate*> devices;
266   UINT32 count;
267   if (!EnumerateVideoDevices(&devices, &count))
268     return;
269
270   HRESULT hr;
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,
278             &id_size))) {
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);
283     } else {
284       DLOG(WARNING) << "GetAllocatedString failed: " << std::hex << hr;
285     }
286     devices[i]->Release();
287   }
288 }
289
290 // static
291 void VideoCaptureDeviceMFWin::GetDeviceSupportedFormats(const Name& device,
292     VideoCaptureFormats* formats) {
293   ScopedComPtr<IMFMediaSource> source;
294   if (!CreateVideoCaptureDevice(device.id().c_str(), source.Receive()))
295     return;
296
297   HRESULT hr;
298   base::win::ScopedComPtr<IMFSourceReader> reader;
299   if (FAILED(hr = MFCreateSourceReaderFromMediaSource(source, NULL,
300                                                       reader.Receive()))) {
301     DLOG(ERROR) << "MFCreateSourceReaderFromMediaSource: " << std::hex << hr;
302     return;
303   }
304
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);
311     if (FAILED(hr)) {
312       DLOG(ERROR) << "MFGetAttributeSize: " << std::hex << hr;
313       return;
314     }
315     VideoCaptureFormat capture_format;
316     capture_format.frame_size.SetSize(width, height);
317
318     UINT32 numerator, denominator;
319     hr = MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, &denominator);
320     if (FAILED(hr)) {
321       DLOG(ERROR) << "MFGetAttributeSize: " << std::hex << hr;
322       return;
323     }
324     capture_format.frame_rate = denominator ? numerator / denominator : 0;
325
326     GUID type_guid;
327     hr = type->GetGUID(MF_MT_SUBTYPE, &type_guid);
328     if (FAILED(hr)) {
329       DLOG(ERROR) << "GetGUID: " << std::hex << hr;
330       return;
331     }
332     FormatFromGuid(type_guid, &capture_format.pixel_format);
333     type.Release();
334     formats->push_back(capture_format);
335     ++stream_index;
336
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;
341   }
342 }
343
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()) {
350     return "";
351   }
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()) {
355     return "";
356   }
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;
362 }
363
364 VideoCaptureDeviceMFWin::VideoCaptureDeviceMFWin(const Name& device_name)
365     : name_(device_name), capture_(0) {
366   DetachFromThread();
367 }
368
369 VideoCaptureDeviceMFWin::~VideoCaptureDeviceMFWin() {
370   DCHECK(CalledOnValidThread());
371 }
372
373 bool VideoCaptureDeviceMFWin::Init() {
374   DCHECK(CalledOnValidThread());
375   DCHECK(!reader_);
376
377   ScopedComPtr<IMFMediaSource> source;
378   if (!CreateVideoCaptureDevice(name_.id().c_str(), source.Receive()))
379     return false;
380
381   ScopedComPtr<IMFAttributes> attributes;
382   MFCreateAttributes(attributes.Receive(), 1);
383   DCHECK(attributes);
384
385   callback_ = new MFReaderCallback(this);
386   attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, callback_.get());
387
388   return SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attributes,
389                                                        reader_.Receive()));
390 }
391
392 void VideoCaptureDeviceMFWin::AllocateAndStart(
393     const VideoCaptureParams& params,
394     scoped_ptr<VideoCaptureDevice::Client> client) {
395   DCHECK(CalledOnValidThread());
396
397   base::AutoLock lock(lock_);
398
399   client_ = client.Pass();
400   DCHECK_EQ(capture_, false);
401
402   CapabilityList capabilities;
403   HRESULT hr = S_OK;
404   if (!reader_ || FAILED(hr = FillCapabilities(reader_, &capabilities))) {
405     OnError(hr);
406     return;
407   }
408
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);
414
415   ScopedComPtr<IMFMediaType> type;
416   if (FAILED(hr = reader_->GetNativeMediaType(
417           MF_SOURCE_READER_FIRST_VIDEO_STREAM, found_capability.stream_index,
418           type.Receive())) ||
419       FAILED(hr = reader_->SetCurrentMediaType(
420           MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, type))) {
421     OnError(hr);
422     return;
423   }
424
425   if (FAILED(hr = reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0,
426                                       NULL, NULL, NULL, NULL))) {
427     OnError(hr);
428     return;
429   }
430   capture_format_ = found_capability.supported_format;
431   capture_ = true;
432 }
433
434 void VideoCaptureDeviceMFWin::StopAndDeAllocate() {
435   DCHECK(CalledOnValidThread());
436   base::WaitableEvent flushed(false, false);
437   const int kFlushTimeOutInMs = 1000;
438   bool wait = false;
439   {
440     base::AutoLock lock(lock_);
441     if (capture_) {
442       capture_ = false;
443       callback_->SetSignalOnFlush(&flushed);
444       HRESULT hr = reader_->Flush(MF_SOURCE_READER_ALL_STREAMS);
445       wait = SUCCEEDED(hr);
446       if (!wait) {
447         callback_->SetSignalOnFlush(NULL);
448       }
449     }
450     client_.reset();
451   }
452
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.
457   // See issue/226396.
458   if (wait)
459     flushed.TimedWait(base::TimeDelta::FromMilliseconds(kFlushTimeOutInMs));
460 }
461
462 void VideoCaptureDeviceMFWin::OnIncomingCapturedFrame(
463     const uint8* data,
464     int length,
465     const base::TimeTicks& time_stamp,
466     int rotation) {
467   base::AutoLock lock(lock_);
468   if (data && client_.get())
469     client_->OnIncomingCapturedFrame(data,
470                                      length,
471                                      time_stamp,
472                                      rotation,
473                                      capture_format_);
474
475   if (capture_) {
476     HRESULT hr = reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0,
477                                      NULL, NULL, NULL, NULL);
478     if (FAILED(hr)) {
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
484       OnError(hr);
485     }
486   }
487 }
488
489 void VideoCaptureDeviceMFWin::OnError(HRESULT hr) {
490   std::string log_msg = base::StringPrintf("VideoCaptureDeviceMFWin: %x", hr);
491   DLOG(ERROR) << log_msg;
492   if (client_.get())
493     client_->OnError(log_msg);
494 }
495
496 }  // namespace media