Upstream version 11.40.277.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/memory/ref_counted.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/win/scoped_co_mem.h"
15 #include "base/win/windows_version.h"
16 #include "media/video/capture/win/capability_list_win.h"
17
18 using base::win::ScopedCoMem;
19 using base::win::ScopedComPtr;
20
21 namespace media {
22
23 // In Windows device identifiers, the USB VID and PID are preceded by the string
24 // "vid_" or "pid_".  The identifiers are each 4 bytes long.
25 const char kVidPrefix[] = "vid_";  // Also contains '\0'.
26 const char kPidPrefix[] = "pid_";  // Also contains '\0'.
27 const size_t kVidPidSize = 4;
28
29 static bool GetFrameSize(IMFMediaType* type, gfx::Size* frame_size) {
30   UINT32 width32, height32;
31   if (FAILED(MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width32, &height32)))
32     return false;
33   frame_size->SetSize(width32, height32);
34   return true;
35 }
36
37 static bool GetFrameRate(IMFMediaType* type, float* frame_rate) {
38   UINT32 numerator, denominator;
39   if (FAILED(MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator,
40                                  &denominator))||
41       !denominator) {
42     return false;
43   }
44   *frame_rate = static_cast<float>(numerator) / denominator;
45   return true;
46 }
47
48 static bool FillFormat(IMFMediaType* type, VideoCaptureFormat* format) {
49   GUID type_guid;
50   if (FAILED(type->GetGUID(MF_MT_SUBTYPE, &type_guid)) ||
51       !GetFrameSize(type, &format->frame_size) ||
52       !GetFrameRate(type, &format->frame_rate) ||
53       !VideoCaptureDeviceMFWin::FormatFromGuid(type_guid,
54                                                &format->pixel_format)) {
55     return false;
56   }
57
58   return true;
59 }
60
61 HRESULT FillCapabilities(IMFSourceReader* source,
62                          CapabilityList* capabilities) {
63   DWORD stream_index = 0;
64   ScopedComPtr<IMFMediaType> type;
65   HRESULT hr;
66   while (SUCCEEDED(hr = source->GetNativeMediaType(
67                        kFirstVideoStream, stream_index, type.Receive()))) {
68     VideoCaptureFormat format;
69     if (FillFormat(type, &format))
70       capabilities->emplace_back(stream_index, format);
71     type.Release();
72     ++stream_index;
73   }
74
75   if (capabilities->empty() && (SUCCEEDED(hr) || hr == MF_E_NO_MORE_TYPES))
76     hr = HRESULT_FROM_WIN32(ERROR_EMPTY);
77
78   return (hr == MF_E_NO_MORE_TYPES) ? S_OK : hr;
79 }
80
81
82 class MFReaderCallback final
83     : public base::RefCountedThreadSafe<MFReaderCallback>,
84       public IMFSourceReaderCallback {
85  public:
86   MFReaderCallback(VideoCaptureDeviceMFWin* observer)
87       : observer_(observer), wait_event_(NULL) {
88   }
89
90   void SetSignalOnFlush(base::WaitableEvent* event) {
91     wait_event_ = event;
92   }
93
94   STDMETHOD(QueryInterface)(REFIID riid, void** object) {
95     if (riid != IID_IUnknown && riid != IID_IMFSourceReaderCallback)
96       return E_NOINTERFACE;
97     *object = static_cast<IMFSourceReaderCallback*>(this);
98     AddRef();
99     return S_OK;
100   }
101
102   STDMETHOD_(ULONG, AddRef)() {
103     base::RefCountedThreadSafe<MFReaderCallback>::AddRef();
104     return 1U;
105   }
106
107   STDMETHOD_(ULONG, Release)() {
108     base::RefCountedThreadSafe<MFReaderCallback>::Release();
109     return 1U;
110   }
111
112   STDMETHOD(OnReadSample)(HRESULT status, DWORD stream_index,
113       DWORD stream_flags, LONGLONG time_stamp, IMFSample* sample) {
114     base::TimeTicks stamp(base::TimeTicks::Now());
115     if (!sample) {
116       observer_->OnIncomingCapturedData(NULL, 0, 0, stamp);
117       return S_OK;
118     }
119
120     DWORD count = 0;
121     sample->GetBufferCount(&count);
122
123     for (DWORD i = 0; i < count; ++i) {
124       ScopedComPtr<IMFMediaBuffer> buffer;
125       sample->GetBufferByIndex(i, buffer.Receive());
126       if (buffer) {
127         DWORD length = 0, max_length = 0;
128         BYTE* data = NULL;
129         buffer->Lock(&data, &max_length, &length);
130         observer_->OnIncomingCapturedData(data, length, 0, stamp);
131         buffer->Unlock();
132       }
133     }
134     return S_OK;
135   }
136
137   STDMETHOD(OnFlush)(DWORD stream_index) {
138     if (wait_event_) {
139       wait_event_->Signal();
140       wait_event_ = NULL;
141     }
142     return S_OK;
143   }
144
145   STDMETHOD(OnEvent)(DWORD stream_index, IMFMediaEvent* event) {
146     NOTIMPLEMENTED();
147     return S_OK;
148   }
149
150  private:
151   friend class base::RefCountedThreadSafe<MFReaderCallback>;
152   ~MFReaderCallback() {}
153
154   VideoCaptureDeviceMFWin* observer_;
155   base::WaitableEvent* wait_event_;
156 };
157
158 // static
159 bool VideoCaptureDeviceMFWin::FormatFromGuid(const GUID& guid,
160                                              VideoPixelFormat* format) {
161   struct {
162     const GUID& guid;
163     const VideoPixelFormat format;
164   } static const kFormatMap[] = {
165     { MFVideoFormat_I420, PIXEL_FORMAT_I420 },
166     { MFVideoFormat_YUY2, PIXEL_FORMAT_YUY2 },
167     { MFVideoFormat_UYVY, PIXEL_FORMAT_UYVY },
168     { MFVideoFormat_RGB24, PIXEL_FORMAT_RGB24 },
169     { MFVideoFormat_ARGB32, PIXEL_FORMAT_ARGB },
170     { MFVideoFormat_MJPG, PIXEL_FORMAT_MJPEG },
171     { MFVideoFormat_YV12, PIXEL_FORMAT_YV12 },
172   };
173
174   for (int i = 0; i < arraysize(kFormatMap); ++i) {
175     if (kFormatMap[i].guid == guid) {
176       *format = kFormatMap[i].format;
177       return true;
178     }
179   }
180
181   return false;
182 }
183
184 const std::string VideoCaptureDevice::Name::GetModel() const {
185   const size_t vid_prefix_size = sizeof(kVidPrefix) - 1;
186   const size_t pid_prefix_size = sizeof(kPidPrefix) - 1;
187   const size_t vid_location = unique_id_.find(kVidPrefix);
188   if (vid_location == std::string::npos ||
189       vid_location + vid_prefix_size + kVidPidSize > unique_id_.size()) {
190     return std::string();
191   }
192   const size_t pid_location = unique_id_.find(kPidPrefix);
193   if (pid_location == std::string::npos ||
194       pid_location + pid_prefix_size + kVidPidSize > unique_id_.size()) {
195     return std::string();
196   }
197   std::string id_vendor =
198       unique_id_.substr(vid_location + vid_prefix_size, kVidPidSize);
199   std::string id_product =
200       unique_id_.substr(pid_location + pid_prefix_size, kVidPidSize);
201   return id_vendor + ":" + id_product;
202 }
203
204 VideoCaptureDeviceMFWin::VideoCaptureDeviceMFWin(const Name& device_name)
205     : name_(device_name), capture_(0) {
206   DetachFromThread();
207 }
208
209 VideoCaptureDeviceMFWin::~VideoCaptureDeviceMFWin() {
210   DCHECK(CalledOnValidThread());
211 }
212
213 bool VideoCaptureDeviceMFWin::Init(
214     const base::win::ScopedComPtr<IMFMediaSource>& source) {
215   DCHECK(CalledOnValidThread());
216   DCHECK(!reader_);
217
218   ScopedComPtr<IMFAttributes> attributes;
219   MFCreateAttributes(attributes.Receive(), 1);
220   DCHECK(attributes);
221
222   callback_ = new MFReaderCallback(this);
223   attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, callback_.get());
224
225   return SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attributes,
226                                                        reader_.Receive()));
227 }
228
229 void VideoCaptureDeviceMFWin::AllocateAndStart(
230     const VideoCaptureParams& params,
231     scoped_ptr<VideoCaptureDevice::Client> client) {
232   DCHECK(CalledOnValidThread());
233
234   base::AutoLock lock(lock_);
235
236   client_ = client.Pass();
237   DCHECK_EQ(capture_, false);
238
239   CapabilityList capabilities;
240   HRESULT hr = S_OK;
241   if (reader_) {
242     hr = FillCapabilities(reader_, &capabilities);
243     if (SUCCEEDED(hr)) {
244       const CapabilityWin found_capability =
245           GetBestMatchedCapability(params.requested_format, capabilities);
246       ScopedComPtr<IMFMediaType> type;
247       hr = reader_->GetNativeMediaType(
248           kFirstVideoStream, found_capability.stream_index, type.Receive());
249       if (SUCCEEDED(hr)) {
250         hr = reader_->SetCurrentMediaType(kFirstVideoStream, NULL, type);
251         if (SUCCEEDED(hr)) {
252           hr = reader_->ReadSample(kFirstVideoStream, 0, NULL, NULL, NULL,
253                                    NULL);
254           if (SUCCEEDED(hr)) {
255             capture_format_ = found_capability.supported_format;
256             capture_ = true;
257             return;
258           }
259         }
260       }
261     }
262   }
263
264   OnError(hr);
265 }
266
267 void VideoCaptureDeviceMFWin::StopAndDeAllocate() {
268   DCHECK(CalledOnValidThread());
269   base::WaitableEvent flushed(false, false);
270   const int kFlushTimeOutInMs = 1000;
271   bool wait = false;
272   {
273     base::AutoLock lock(lock_);
274     if (capture_) {
275       capture_ = false;
276       callback_->SetSignalOnFlush(&flushed);
277       wait = SUCCEEDED(reader_->Flush(
278           static_cast<DWORD>(MF_SOURCE_READER_ALL_STREAMS)));
279       if (!wait) {
280         callback_->SetSignalOnFlush(NULL);
281       }
282     }
283     client_.reset();
284   }
285
286   // If the device has been unplugged, the Flush() won't trigger the event
287   // and a timeout will happen.
288   // TODO(tommi): Hook up the IMFMediaEventGenerator notifications API and
289   // do not wait at all after getting MEVideoCaptureDeviceRemoved event.
290   // See issue/226396.
291   if (wait)
292     flushed.TimedWait(base::TimeDelta::FromMilliseconds(kFlushTimeOutInMs));
293 }
294
295 void VideoCaptureDeviceMFWin::OnIncomingCapturedData(
296     const uint8* data,
297     int length,
298     int rotation,
299     const base::TimeTicks& time_stamp) {
300   base::AutoLock lock(lock_);
301   if (data && client_.get()) {
302     client_->OnIncomingCapturedData(
303         data, length, capture_format_, rotation, time_stamp);
304   }
305
306   if (capture_) {
307     HRESULT hr =
308         reader_->ReadSample(kFirstVideoStream, 0, NULL, NULL, NULL, NULL);
309     if (FAILED(hr)) {
310       // If running the *VideoCap* unit tests on repeat, this can sometimes
311       // fail with HRESULT_FROM_WINHRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION).
312       // It's not clear to me why this is, but it is possible that it has
313       // something to do with this bug:
314       // http://support.microsoft.com/kb/979567
315       OnError(hr);
316     }
317   }
318 }
319
320 void VideoCaptureDeviceMFWin::OnError(HRESULT hr) {
321   if (client_.get()) {
322     client_->OnError(
323         base::StringPrintf("VideoCaptureDeviceMFWin: %s",
324                            logging::SystemErrorCodeToString(hr).c_str()));
325   }
326 }
327
328 }  // namespace media