- add sources.
[platform/framework/web/crosswalk.git] / src / media / video / capture / win / video_capture_device_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_win.h"
6
7 #include <algorithm>
8 #include <list>
9
10 #include "base/command_line.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/win/metro.h"
14 #include "base/win/scoped_co_mem.h"
15 #include "base/win/scoped_variant.h"
16 #include "media/base/media_switches.h"
17 #include "media/video/capture/win/video_capture_device_mf_win.h"
18
19 using base::win::ScopedCoMem;
20 using base::win::ScopedComPtr;
21 using base::win::ScopedVariant;
22
23 namespace media {
24 namespace {
25
26 // Finds and creates a DirectShow Video Capture filter matching the device_name.
27 HRESULT GetDeviceFilter(const VideoCaptureDevice::Name& device_name,
28                         IBaseFilter** filter) {
29   DCHECK(filter);
30
31   ScopedComPtr<ICreateDevEnum> dev_enum;
32   HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL,
33                                        CLSCTX_INPROC);
34   if (FAILED(hr))
35     return hr;
36
37   ScopedComPtr<IEnumMoniker> enum_moniker;
38   hr = dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
39                                        enum_moniker.Receive(), 0);
40   // CreateClassEnumerator returns S_FALSE on some Windows OS
41   // when no camera exist. Therefore the FAILED macro can't be used.
42   if (hr != S_OK)
43     return NULL;
44
45   ScopedComPtr<IMoniker> moniker;
46   ScopedComPtr<IBaseFilter> capture_filter;
47   DWORD fetched = 0;
48   while (enum_moniker->Next(1, moniker.Receive(), &fetched) == S_OK) {
49     ScopedComPtr<IPropertyBag> prop_bag;
50     hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, prop_bag.ReceiveVoid());
51     if (FAILED(hr)) {
52       moniker.Release();
53       continue;
54     }
55
56     // Find the description or friendly name.
57     static const wchar_t* kPropertyNames[] = {
58       L"DevicePath", L"Description", L"FriendlyName"
59     };
60     ScopedVariant name;
61     for (size_t i = 0;
62          i < arraysize(kPropertyNames) && name.type() != VT_BSTR; ++i) {
63       prop_bag->Read(kPropertyNames[i], name.Receive(), 0);
64     }
65     if (name.type() == VT_BSTR) {
66       std::string device_path(base::SysWideToUTF8(V_BSTR(&name)));
67       if (device_path.compare(device_name.id()) == 0) {
68         // We have found the requested device
69         hr = moniker->BindToObject(0, 0, IID_IBaseFilter,
70                                    capture_filter.ReceiveVoid());
71         DVPLOG_IF(2, FAILED(hr)) << "Failed to bind camera filter.";
72         break;
73       }
74     }
75     moniker.Release();
76   }
77
78   *filter = capture_filter.Detach();
79   if (!*filter && SUCCEEDED(hr))
80     hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
81
82   return hr;
83 }
84
85 // Check if a Pin matches a category.
86 bool PinMatchesCategory(IPin* pin, REFGUID category) {
87   DCHECK(pin);
88   bool found = false;
89   ScopedComPtr<IKsPropertySet> ks_property;
90   HRESULT hr = ks_property.QueryFrom(pin);
91   if (SUCCEEDED(hr)) {
92     GUID pin_category;
93     DWORD return_value;
94     hr = ks_property->Get(AMPROPSETID_Pin, AMPROPERTY_PIN_CATEGORY, NULL, 0,
95                           &pin_category, sizeof(pin_category), &return_value);
96     if (SUCCEEDED(hr) && (return_value == sizeof(pin_category))) {
97       found = (pin_category == category);
98     }
99   }
100   return found;
101 }
102
103 // Finds a IPin on a IBaseFilter given the direction an category.
104 HRESULT GetPin(IBaseFilter* filter, PIN_DIRECTION pin_dir, REFGUID category,
105                IPin** pin) {
106   DCHECK(pin);
107   ScopedComPtr<IEnumPins> pin_emum;
108   HRESULT hr = filter->EnumPins(pin_emum.Receive());
109   if (pin_emum == NULL)
110     return hr;
111
112   // Get first unconnected pin.
113   hr = pin_emum->Reset();  // set to first pin
114   while ((hr = pin_emum->Next(1, pin, NULL)) == S_OK) {
115     PIN_DIRECTION this_pin_dir = static_cast<PIN_DIRECTION>(-1);
116     hr = (*pin)->QueryDirection(&this_pin_dir);
117     if (pin_dir == this_pin_dir) {
118       if (category == GUID_NULL || PinMatchesCategory(*pin, category))
119         return S_OK;
120     }
121     (*pin)->Release();
122   }
123
124   return E_FAIL;
125 }
126
127 // Release the format block for a media type.
128 // http://msdn.microsoft.com/en-us/library/dd375432(VS.85).aspx
129 void FreeMediaType(AM_MEDIA_TYPE* mt) {
130   if (mt->cbFormat != 0) {
131     CoTaskMemFree(mt->pbFormat);
132     mt->cbFormat = 0;
133     mt->pbFormat = NULL;
134   }
135   if (mt->pUnk != NULL) {
136     NOTREACHED();
137     // pUnk should not be used.
138     mt->pUnk->Release();
139     mt->pUnk = NULL;
140   }
141 }
142
143 // Delete a media type structure that was allocated on the heap.
144 // http://msdn.microsoft.com/en-us/library/dd375432(VS.85).aspx
145 void DeleteMediaType(AM_MEDIA_TYPE* mt) {
146   if (mt != NULL) {
147     FreeMediaType(mt);
148     CoTaskMemFree(mt);
149   }
150 }
151
152 }  // namespace
153
154 // static
155 void VideoCaptureDevice::GetDeviceNames(Names* device_names) {
156   const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
157   // Use Media Foundation for Metro processes (after and including Win8)
158   // and DirectShow for any other platforms.
159   if (base::win::IsMetroProcess() &&
160       !cmd_line->HasSwitch(switches::kForceDirectShowVideoCapture)) {
161     VideoCaptureDeviceMFWin::GetDeviceNames(device_names);
162   } else {
163     VideoCaptureDeviceWin::GetDeviceNames(device_names);
164   }
165 }
166
167 // static
168 void VideoCaptureDevice::GetDeviceSupportedFormats(const Name& device,
169     VideoCaptureCapabilities* formats) {
170   NOTIMPLEMENTED();
171 }
172
173 // static
174 VideoCaptureDevice* VideoCaptureDevice::Create(const Name& device_name) {
175   VideoCaptureDevice* ret = NULL;
176   if (device_name.capture_api_type() == Name::MEDIA_FOUNDATION) {
177     DCHECK(VideoCaptureDeviceMFWin::PlatformSupported());
178     scoped_ptr<VideoCaptureDeviceMFWin> device(
179         new VideoCaptureDeviceMFWin(device_name));
180     DVLOG(1) << " MediaFoundation Device: " << device_name.name();
181     if (device->Init())
182       ret = device.release();
183   } else if (device_name.capture_api_type() == Name::DIRECT_SHOW) {
184     scoped_ptr<VideoCaptureDeviceWin> device(
185         new VideoCaptureDeviceWin(device_name));
186     DVLOG(1) << " DirectShow Device: " << device_name.name();
187     if (device->Init())
188       ret = device.release();
189   } else{
190     NOTREACHED() << " Couldn't recognize VideoCaptureDevice type";
191   }
192
193   return ret;
194 }
195
196 // static
197 void VideoCaptureDeviceWin::GetDeviceNames(Names* device_names) {
198   DCHECK(device_names);
199
200   ScopedComPtr<ICreateDevEnum> dev_enum;
201   HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL,
202                                        CLSCTX_INPROC);
203   if (FAILED(hr))
204     return;
205
206   ScopedComPtr<IEnumMoniker> enum_moniker;
207   hr = dev_enum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
208                                        enum_moniker.Receive(), 0);
209   // CreateClassEnumerator returns S_FALSE on some Windows OS
210   // when no camera exist. Therefore the FAILED macro can't be used.
211   if (hr != S_OK)
212     return;
213
214   device_names->clear();
215
216   // Name of a fake DirectShow filter that exist on computers with
217   // GTalk installed.
218   static const char kGoogleCameraAdapter[] = "google camera adapter";
219
220   // Enumerate all video capture devices.
221   ScopedComPtr<IMoniker> moniker;
222   int index = 0;
223   while (enum_moniker->Next(1, moniker.Receive(), NULL) == S_OK) {
224     ScopedComPtr<IPropertyBag> prop_bag;
225     hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, prop_bag.ReceiveVoid());
226     if (FAILED(hr)) {
227       moniker.Release();
228       continue;
229     }
230
231     // Find the description or friendly name.
232     ScopedVariant name;
233     hr = prop_bag->Read(L"Description", name.Receive(), 0);
234     if (FAILED(hr))
235       hr = prop_bag->Read(L"FriendlyName", name.Receive(), 0);
236
237     if (SUCCEEDED(hr) && name.type() == VT_BSTR) {
238       // Ignore all VFW drivers and the special Google Camera Adapter.
239       // Google Camera Adapter is not a real DirectShow camera device.
240       // VFW is very old Video for Windows drivers that can not be used.
241       const wchar_t* str_ptr = V_BSTR(&name);
242       const int name_length = arraysize(kGoogleCameraAdapter) - 1;
243
244       if ((wcsstr(str_ptr, L"(VFW)") == NULL) &&
245           lstrlenW(str_ptr) < name_length ||
246           (!(LowerCaseEqualsASCII(str_ptr, str_ptr + name_length,
247                                   kGoogleCameraAdapter)))) {
248         std::string id;
249         std::string device_name(base::SysWideToUTF8(str_ptr));
250         name.Reset();
251         hr = prop_bag->Read(L"DevicePath", name.Receive(), 0);
252         if (FAILED(hr) || name.type() != VT_BSTR) {
253           id = device_name;
254         } else {
255           DCHECK_EQ(name.type(), VT_BSTR);
256           id = base::SysWideToUTF8(V_BSTR(&name));
257         }
258
259         device_names->push_back(Name(device_name, id, Name::DIRECT_SHOW));
260       }
261     }
262     moniker.Release();
263   }
264 }
265
266 VideoCaptureDeviceWin::VideoCaptureDeviceWin(const Name& device_name)
267     : device_name_(device_name),
268       state_(kIdle) {
269   DetachFromThread();
270 }
271
272 VideoCaptureDeviceWin::~VideoCaptureDeviceWin() {
273   DCHECK(CalledOnValidThread());
274   if (media_control_)
275     media_control_->Stop();
276
277   if (graph_builder_) {
278     if (sink_filter_) {
279       graph_builder_->RemoveFilter(sink_filter_);
280       sink_filter_ = NULL;
281     }
282
283     if (capture_filter_)
284       graph_builder_->RemoveFilter(capture_filter_);
285
286     if (mjpg_filter_)
287       graph_builder_->RemoveFilter(mjpg_filter_);
288   }
289 }
290
291 bool VideoCaptureDeviceWin::Init() {
292   DCHECK(CalledOnValidThread());
293   HRESULT hr = GetDeviceFilter(device_name_, capture_filter_.Receive());
294   if (!capture_filter_) {
295     DVLOG(2) << "Failed to create capture filter.";
296     return false;
297   }
298
299   hr = GetPin(capture_filter_, PINDIR_OUTPUT, PIN_CATEGORY_CAPTURE,
300               output_capture_pin_.Receive());
301   if (!output_capture_pin_) {
302     DVLOG(2) << "Failed to get capture output pin";
303     return false;
304   }
305
306   // Create the sink filter used for receiving Captured frames.
307   sink_filter_ = new SinkFilter(this);
308   if (sink_filter_ == NULL) {
309     DVLOG(2) << "Failed to create send filter";
310     return false;
311   }
312
313   input_sink_pin_ = sink_filter_->GetPin(0);
314
315   hr = graph_builder_.CreateInstance(CLSID_FilterGraph, NULL,
316                                      CLSCTX_INPROC_SERVER);
317   if (FAILED(hr)) {
318     DVLOG(2) << "Failed to create graph builder.";
319     return false;
320   }
321
322   hr = graph_builder_.QueryInterface(media_control_.Receive());
323   if (FAILED(hr)) {
324     DVLOG(2) << "Failed to create media control builder.";
325     return false;
326   }
327
328   hr = graph_builder_->AddFilter(capture_filter_, NULL);
329   if (FAILED(hr)) {
330     DVLOG(2) << "Failed to add the capture device to the graph.";
331     return false;
332   }
333
334   hr = graph_builder_->AddFilter(sink_filter_, NULL);
335   if (FAILED(hr)) {
336     DVLOG(2)<< "Failed to add the send filter to the graph.";
337     return false;
338   }
339
340   return CreateCapabilityMap();
341 }
342
343 void VideoCaptureDeviceWin::AllocateAndStart(
344     const VideoCaptureCapability& capture_format,
345     scoped_ptr<VideoCaptureDevice::Client> client) {
346   DCHECK(CalledOnValidThread());
347   if (state_ != kIdle)
348     return;
349
350   client_ = client.Pass();
351
352   // Get the camera capability that best match the requested resolution.
353   const VideoCaptureCapabilityWin& found_capability =
354       capabilities_.GetBestMatchedCapability(capture_format.width,
355                                              capture_format.height,
356                                              capture_format.frame_rate);
357   VideoCaptureCapability capability = found_capability;
358
359   // Reduce the frame rate if the requested frame rate is lower
360   // than the capability.
361   if (capability.frame_rate > capture_format.frame_rate)
362     capability.frame_rate = capture_format.frame_rate;
363
364   AM_MEDIA_TYPE* pmt = NULL;
365   VIDEO_STREAM_CONFIG_CAPS caps;
366
367   ScopedComPtr<IAMStreamConfig> stream_config;
368   HRESULT hr = output_capture_pin_.QueryInterface(stream_config.Receive());
369   if (FAILED(hr)) {
370     SetErrorState("Can't get the Capture format settings");
371     return;
372   }
373
374   // Get the windows capability from the capture device.
375   hr = stream_config->GetStreamCaps(found_capability.stream_index, &pmt,
376                                     reinterpret_cast<BYTE*>(&caps));
377   if (SUCCEEDED(hr)) {
378     if (pmt->formattype == FORMAT_VideoInfo) {
379       VIDEOINFOHEADER* h = reinterpret_cast<VIDEOINFOHEADER*>(pmt->pbFormat);
380       if (capability.frame_rate > 0)
381         h->AvgTimePerFrame = kSecondsToReferenceTime / capability.frame_rate;
382     }
383     // Set the sink filter to request this capability.
384     sink_filter_->SetRequestedMediaCapability(capability);
385     // Order the capture device to use this capability.
386     hr = stream_config->SetFormat(pmt);
387   }
388
389   if (FAILED(hr))
390     SetErrorState("Failed to set capture device output format");
391
392   if (capability.color == PIXEL_FORMAT_MJPEG &&
393       !mjpg_filter_.get()) {
394     // Create MJPG filter if we need it.
395     hr = mjpg_filter_.CreateInstance(CLSID_MjpegDec, NULL, CLSCTX_INPROC);
396
397     if (SUCCEEDED(hr)) {
398       GetPin(mjpg_filter_, PINDIR_INPUT, GUID_NULL, input_mjpg_pin_.Receive());
399       GetPin(mjpg_filter_, PINDIR_OUTPUT, GUID_NULL,
400              output_mjpg_pin_.Receive());
401       hr = graph_builder_->AddFilter(mjpg_filter_, NULL);
402     }
403
404     if (FAILED(hr)) {
405       mjpg_filter_.Release();
406       input_mjpg_pin_.Release();
407       output_mjpg_pin_.Release();
408     }
409   }
410
411   if (capability.color == PIXEL_FORMAT_MJPEG &&
412       mjpg_filter_.get()) {
413     // Connect the camera to the MJPEG decoder.
414     hr = graph_builder_->ConnectDirect(output_capture_pin_, input_mjpg_pin_,
415                                        NULL);
416     // Connect the MJPEG filter to the Capture filter.
417     hr += graph_builder_->ConnectDirect(output_mjpg_pin_, input_sink_pin_,
418                                         NULL);
419   } else {
420     hr = graph_builder_->ConnectDirect(output_capture_pin_, input_sink_pin_,
421                                        NULL);
422   }
423
424   if (FAILED(hr)) {
425     SetErrorState("Failed to connect the Capture graph.");
426     return;
427   }
428
429   hr = media_control_->Pause();
430   if (FAILED(hr)) {
431     SetErrorState("Failed to Pause the Capture device. "
432                   "Is it already occupied?");
433     return;
434   }
435
436   // Get the capability back from the sink filter after the filter have been
437   // connected.
438   const VideoCaptureCapability& used_capability
439       = sink_filter_->ResultingCapability();
440   client_->OnFrameInfo(used_capability);
441
442   // Start capturing.
443   hr = media_control_->Run();
444   if (FAILED(hr)) {
445     SetErrorState("Failed to start the Capture device.");
446     return;
447   }
448
449   state_ = kCapturing;
450 }
451
452 void VideoCaptureDeviceWin::StopAndDeAllocate() {
453   DCHECK(CalledOnValidThread());
454   if (state_ != kCapturing)
455     return;
456
457   HRESULT hr = media_control_->Stop();
458   if (FAILED(hr)) {
459     SetErrorState("Failed to stop the capture graph.");
460     return;
461   }
462
463   graph_builder_->Disconnect(output_capture_pin_);
464   graph_builder_->Disconnect(input_sink_pin_);
465
466   // If the _mjpg filter exist disconnect it even if it has not been used.
467   if (mjpg_filter_) {
468     graph_builder_->Disconnect(input_mjpg_pin_);
469     graph_builder_->Disconnect(output_mjpg_pin_);
470   }
471
472   if (FAILED(hr)) {
473     SetErrorState("Failed to Stop the Capture device");
474     return;
475   }
476   client_.reset();
477   state_ = kIdle;
478 }
479
480 // Implements SinkFilterObserver::SinkFilterObserver.
481 void VideoCaptureDeviceWin::FrameReceived(const uint8* buffer,
482                                           int length) {
483   client_->OnIncomingCapturedFrame(buffer, length, base::Time::Now(),
484                                    0, false, false);
485 }
486
487 bool VideoCaptureDeviceWin::CreateCapabilityMap() {
488   DCHECK(CalledOnValidThread());
489   ScopedComPtr<IAMStreamConfig> stream_config;
490   HRESULT hr = output_capture_pin_.QueryInterface(stream_config.Receive());
491   if (FAILED(hr)) {
492     DVLOG(2) << "Failed to get IAMStreamConfig interface from "
493                 "capture device";
494     return false;
495   }
496
497   // Get interface used for getting the frame rate.
498   ScopedComPtr<IAMVideoControl> video_control;
499   hr = capture_filter_.QueryInterface(video_control.Receive());
500   DVLOG_IF(2, FAILED(hr)) << "IAMVideoControl Interface NOT SUPPORTED";
501
502   AM_MEDIA_TYPE* media_type = NULL;
503   VIDEO_STREAM_CONFIG_CAPS caps;
504   int count, size;
505
506   hr = stream_config->GetNumberOfCapabilities(&count, &size);
507   if (FAILED(hr)) {
508     DVLOG(2) << "Failed to GetNumberOfCapabilities";
509     return false;
510   }
511
512   for (int i = 0; i < count; ++i) {
513     hr = stream_config->GetStreamCaps(i, &media_type,
514                                       reinterpret_cast<BYTE*>(&caps));
515     // GetStreamCaps() may return S_FALSE, so don't use FAILED() or SUCCEED()
516     // macros here since they'll trigger incorrectly.
517     if (hr != S_OK) {
518       DVLOG(2) << "Failed to GetStreamCaps";
519       return false;
520     }
521
522     if (media_type->majortype == MEDIATYPE_Video &&
523         media_type->formattype == FORMAT_VideoInfo) {
524       VideoCaptureCapabilityWin capability(i);
525       VIDEOINFOHEADER* h =
526           reinterpret_cast<VIDEOINFOHEADER*>(media_type->pbFormat);
527       capability.width = h->bmiHeader.biWidth;
528       capability.height = h->bmiHeader.biHeight;
529
530       // Try to get a better |time_per_frame| from IAMVideoControl.  If not, use
531       // the value from VIDEOINFOHEADER.
532       REFERENCE_TIME time_per_frame = h->AvgTimePerFrame;
533       if (video_control) {
534         ScopedCoMem<LONGLONG> max_fps;
535         LONG list_size = 0;
536         SIZE size = { capability.width, capability.height };
537
538         // GetFrameRateList doesn't return max frame rate always
539         // eg: Logitech Notebook. This may be due to a bug in that API
540         // because GetFrameRateList array is reversed in the above camera. So
541         // a util method written. Can't assume the first value will return
542         // the max fps.
543         hr = video_control->GetFrameRateList(output_capture_pin_, i, size,
544                                              &list_size, &max_fps);
545         // Sometimes |list_size| will be > 0, but max_fps will be NULL.  Some
546         // drivers may return an HRESULT of S_FALSE which SUCCEEDED() translates
547         // into success, so explicitly check S_OK.  See http://crbug.com/306237.
548         if (hr == S_OK && list_size > 0 && max_fps) {
549           time_per_frame = *std::min_element(max_fps.get(),
550                                              max_fps.get() + list_size);
551         }
552       }
553
554       capability.frame_rate = (time_per_frame > 0) ?
555           static_cast<int>(kSecondsToReferenceTime / time_per_frame) : 0;
556
557       // DirectShow works at the moment only on integer frame_rate but the
558       // best capability matching class works on rational frame rates.
559       capability.frame_rate_numerator = capability.frame_rate;
560       capability.frame_rate_denominator = 1;
561
562       // We can't switch MEDIATYPE :~(.
563       if (media_type->subtype == kMediaSubTypeI420) {
564         capability.color = PIXEL_FORMAT_I420;
565       } else if (media_type->subtype == MEDIASUBTYPE_IYUV) {
566         // This is identical to PIXEL_FORMAT_I420.
567         capability.color = PIXEL_FORMAT_I420;
568       } else if (media_type->subtype == MEDIASUBTYPE_RGB24) {
569         capability.color = PIXEL_FORMAT_RGB24;
570       } else if (media_type->subtype == MEDIASUBTYPE_YUY2) {
571         capability.color = PIXEL_FORMAT_YUY2;
572       } else if (media_type->subtype == MEDIASUBTYPE_MJPG) {
573         capability.color = PIXEL_FORMAT_MJPEG;
574       } else if (media_type->subtype == MEDIASUBTYPE_UYVY) {
575         capability.color = PIXEL_FORMAT_UYVY;
576       } else if (media_type->subtype == MEDIASUBTYPE_ARGB32) {
577         capability.color = PIXEL_FORMAT_ARGB;
578       } else {
579         WCHAR guid_str[128];
580         StringFromGUID2(media_type->subtype, guid_str, arraysize(guid_str));
581         DVLOG(2) << "Device supports (also) an unknown media type " << guid_str;
582         continue;
583       }
584       capabilities_.Add(capability);
585     }
586     DeleteMediaType(media_type);
587     media_type = NULL;
588   }
589
590   return !capabilities_.empty();
591 }
592
593 void VideoCaptureDeviceWin::SetErrorState(const char* reason) {
594   DCHECK(CalledOnValidThread());
595   DVLOG(1) << reason;
596   state_ = kError;
597   client_->OnError();
598 }
599 }  // namespace media