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