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/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"
18 using base::win::ScopedCoMem;
19 using base::win::ScopedComPtr;
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;
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)))
33 frame_size->SetSize(width32, height32);
37 static 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,
46 *frame_rate_numerator = numerator;
47 *frame_rate_denominator = denominator;
51 static bool FillCapabilitiesFromType(IMFMediaType* type,
52 VideoCaptureCapabilityWin* capability) {
54 if (FAILED(type->GetGUID(MF_MT_SUBTYPE, &type_guid)) ||
55 !GetFrameSize(type, &capability->supported_format.frame_size) ||
57 &capability->frame_rate_numerator,
58 &capability->frame_rate_denominator) ||
59 !VideoCaptureDeviceMFWin::FormatFromGuid(type_guid,
60 &capability->supported_format.pixel_format)) {
63 capability->supported_format.frame_rate =
64 capability->frame_rate_numerator / capability->frame_rate_denominator;
69 HRESULT FillCapabilities(IMFSourceReader* source,
70 CapabilityList* capabilities) {
71 DWORD stream_index = 0;
72 ScopedComPtr<IMFMediaType> type;
74 for (hr = source->GetNativeMediaType(kFirstVideoStream, stream_index,
77 hr = source->GetNativeMediaType(kFirstVideoStream, stream_index,
79 VideoCaptureCapabilityWin capability(stream_index++);
80 if (FillCapabilitiesFromType(type, &capability))
81 capabilities->Add(capability);
85 if (capabilities->empty() && (SUCCEEDED(hr) || hr == MF_E_NO_MORE_TYPES))
86 hr = HRESULT_FROM_WIN32(ERROR_EMPTY);
88 return (hr == MF_E_NO_MORE_TYPES) ? S_OK : hr;
92 class MFReaderCallback FINAL
93 : public base::RefCountedThreadSafe<MFReaderCallback>,
94 public IMFSourceReaderCallback {
96 MFReaderCallback(VideoCaptureDeviceMFWin* observer)
97 : observer_(observer), wait_event_(NULL) {
100 void SetSignalOnFlush(base::WaitableEvent* event) {
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);
112 STDMETHOD_(ULONG, AddRef)() {
113 base::RefCountedThreadSafe<MFReaderCallback>::AddRef();
117 STDMETHOD_(ULONG, Release)() {
118 base::RefCountedThreadSafe<MFReaderCallback>::Release();
122 STDMETHOD(OnReadSample)(HRESULT status, DWORD stream_index,
123 DWORD stream_flags, LONGLONG time_stamp, IMFSample* sample) {
124 base::TimeTicks stamp(base::TimeTicks::Now());
126 observer_->OnIncomingCapturedData(NULL, 0, 0, stamp);
131 sample->GetBufferCount(&count);
133 for (DWORD i = 0; i < count; ++i) {
134 ScopedComPtr<IMFMediaBuffer> buffer;
135 sample->GetBufferByIndex(i, buffer.Receive());
137 DWORD length = 0, max_length = 0;
139 buffer->Lock(&data, &max_length, &length);
140 observer_->OnIncomingCapturedData(data, length, 0, stamp);
147 STDMETHOD(OnFlush)(DWORD stream_index) {
149 wait_event_->Signal();
155 STDMETHOD(OnEvent)(DWORD stream_index, IMFMediaEvent* event) {
161 friend class base::RefCountedThreadSafe<MFReaderCallback>;
162 ~MFReaderCallback() {}
164 VideoCaptureDeviceMFWin* observer_;
165 base::WaitableEvent* wait_event_;
169 bool VideoCaptureDeviceMFWin::FormatFromGuid(const GUID& guid,
170 VideoPixelFormat* format) {
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 },
184 for (int i = 0; i < arraysize(kFormatMap); ++i) {
185 if (kFormatMap[i].guid == guid) {
186 *format = kFormatMap[i].format;
194 const 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();
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();
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;
214 VideoCaptureDeviceMFWin::VideoCaptureDeviceMFWin(const Name& device_name)
215 : name_(device_name), capture_(0) {
219 VideoCaptureDeviceMFWin::~VideoCaptureDeviceMFWin() {
220 DCHECK(CalledOnValidThread());
223 bool VideoCaptureDeviceMFWin::Init(
224 const base::win::ScopedComPtr<IMFMediaSource>& source) {
225 DCHECK(CalledOnValidThread());
228 ScopedComPtr<IMFAttributes> attributes;
229 MFCreateAttributes(attributes.Receive(), 1);
232 callback_ = new MFReaderCallback(this);
233 attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, callback_.get());
235 return SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attributes,
239 void VideoCaptureDeviceMFWin::AllocateAndStart(
240 const VideoCaptureParams& params,
241 scoped_ptr<VideoCaptureDevice::Client> client) {
242 DCHECK(CalledOnValidThread());
244 base::AutoLock lock(lock_);
246 client_ = client.Pass();
247 DCHECK_EQ(capture_, false);
249 CapabilityList capabilities;
252 hr = FillCapabilities(reader_, &capabilities);
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);
260 ScopedComPtr<IMFMediaType> type;
261 hr = reader_->GetNativeMediaType(
262 kFirstVideoStream, found_capability.stream_index, type.Receive());
264 hr = reader_->SetCurrentMediaType(kFirstVideoStream, NULL, type);
266 hr = reader_->ReadSample(kFirstVideoStream, 0, NULL, NULL, NULL,
269 capture_format_ = found_capability.supported_format;
281 void VideoCaptureDeviceMFWin::StopAndDeAllocate() {
282 DCHECK(CalledOnValidThread());
283 base::WaitableEvent flushed(false, false);
284 const int kFlushTimeOutInMs = 1000;
287 base::AutoLock lock(lock_);
290 callback_->SetSignalOnFlush(&flushed);
291 wait = SUCCEEDED(reader_->Flush(
292 static_cast<DWORD>(MF_SOURCE_READER_ALL_STREAMS)));
294 callback_->SetSignalOnFlush(NULL);
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.
306 flushed.TimedWait(base::TimeDelta::FromMilliseconds(kFlushTimeOutInMs));
309 void VideoCaptureDeviceMFWin::OnIncomingCapturedData(
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);
322 reader_->ReadSample(kFirstVideoStream, 0, NULL, NULL, NULL, NULL);
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
334 void VideoCaptureDeviceMFWin::OnError(HRESULT hr) {
335 std::string log_msg = base::StringPrintf("VideoCaptureDeviceMFWin: %x", hr);
336 DLOG(ERROR) << log_msg;
338 client_->OnError(log_msg);