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, float* frame_rate) {
38 UINT32 numerator, denominator;
39 if (FAILED(MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator,
44 *frame_rate = static_cast<float>(numerator) / denominator;
48 static bool FillFormat(IMFMediaType* type, VideoCaptureFormat* format) {
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)) {
61 HRESULT FillCapabilities(IMFSourceReader* source,
62 CapabilityList* capabilities) {
63 DWORD stream_index = 0;
64 ScopedComPtr<IMFMediaType> type;
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);
75 if (capabilities->empty() && (SUCCEEDED(hr) || hr == MF_E_NO_MORE_TYPES))
76 hr = HRESULT_FROM_WIN32(ERROR_EMPTY);
78 return (hr == MF_E_NO_MORE_TYPES) ? S_OK : hr;
82 class MFReaderCallback final
83 : public base::RefCountedThreadSafe<MFReaderCallback>,
84 public IMFSourceReaderCallback {
86 MFReaderCallback(VideoCaptureDeviceMFWin* observer)
87 : observer_(observer), wait_event_(NULL) {
90 void SetSignalOnFlush(base::WaitableEvent* event) {
94 STDMETHOD(QueryInterface)(REFIID riid, void** object) {
95 if (riid != IID_IUnknown && riid != IID_IMFSourceReaderCallback)
97 *object = static_cast<IMFSourceReaderCallback*>(this);
102 STDMETHOD_(ULONG, AddRef)() {
103 base::RefCountedThreadSafe<MFReaderCallback>::AddRef();
107 STDMETHOD_(ULONG, Release)() {
108 base::RefCountedThreadSafe<MFReaderCallback>::Release();
112 STDMETHOD(OnReadSample)(HRESULT status, DWORD stream_index,
113 DWORD stream_flags, LONGLONG time_stamp, IMFSample* sample) {
114 base::TimeTicks stamp(base::TimeTicks::Now());
116 observer_->OnIncomingCapturedData(NULL, 0, 0, stamp);
121 sample->GetBufferCount(&count);
123 for (DWORD i = 0; i < count; ++i) {
124 ScopedComPtr<IMFMediaBuffer> buffer;
125 sample->GetBufferByIndex(i, buffer.Receive());
127 DWORD length = 0, max_length = 0;
129 buffer->Lock(&data, &max_length, &length);
130 observer_->OnIncomingCapturedData(data, length, 0, stamp);
137 STDMETHOD(OnFlush)(DWORD stream_index) {
139 wait_event_->Signal();
145 STDMETHOD(OnEvent)(DWORD stream_index, IMFMediaEvent* event) {
151 friend class base::RefCountedThreadSafe<MFReaderCallback>;
152 ~MFReaderCallback() {}
154 VideoCaptureDeviceMFWin* observer_;
155 base::WaitableEvent* wait_event_;
159 bool VideoCaptureDeviceMFWin::FormatFromGuid(const GUID& guid,
160 VideoPixelFormat* format) {
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 },
174 for (int i = 0; i < arraysize(kFormatMap); ++i) {
175 if (kFormatMap[i].guid == guid) {
176 *format = kFormatMap[i].format;
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();
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();
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;
204 VideoCaptureDeviceMFWin::VideoCaptureDeviceMFWin(const Name& device_name)
205 : name_(device_name), capture_(0) {
209 VideoCaptureDeviceMFWin::~VideoCaptureDeviceMFWin() {
210 DCHECK(CalledOnValidThread());
213 bool VideoCaptureDeviceMFWin::Init(
214 const base::win::ScopedComPtr<IMFMediaSource>& source) {
215 DCHECK(CalledOnValidThread());
218 ScopedComPtr<IMFAttributes> attributes;
219 MFCreateAttributes(attributes.Receive(), 1);
222 callback_ = new MFReaderCallback(this);
223 attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, callback_.get());
225 return SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attributes,
229 void VideoCaptureDeviceMFWin::AllocateAndStart(
230 const VideoCaptureParams& params,
231 scoped_ptr<VideoCaptureDevice::Client> client) {
232 DCHECK(CalledOnValidThread());
234 base::AutoLock lock(lock_);
236 client_ = client.Pass();
237 DCHECK_EQ(capture_, false);
239 CapabilityList capabilities;
242 hr = FillCapabilities(reader_, &capabilities);
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());
250 hr = reader_->SetCurrentMediaType(kFirstVideoStream, NULL, type);
252 hr = reader_->ReadSample(kFirstVideoStream, 0, NULL, NULL, NULL,
255 capture_format_ = found_capability.supported_format;
267 void VideoCaptureDeviceMFWin::StopAndDeAllocate() {
268 DCHECK(CalledOnValidThread());
269 base::WaitableEvent flushed(false, false);
270 const int kFlushTimeOutInMs = 1000;
273 base::AutoLock lock(lock_);
276 callback_->SetSignalOnFlush(&flushed);
277 wait = SUCCEEDED(reader_->Flush(
278 static_cast<DWORD>(MF_SOURCE_READER_ALL_STREAMS)));
280 callback_->SetSignalOnFlush(NULL);
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.
292 flushed.TimedWait(base::TimeDelta::FromMilliseconds(kFlushTimeOutInMs));
295 void VideoCaptureDeviceMFWin::OnIncomingCapturedData(
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);
308 reader_->ReadSample(kFirstVideoStream, 0, NULL, NULL, NULL, NULL);
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
320 void VideoCaptureDeviceMFWin::OnError(HRESULT hr) {
323 base::StringPrintf("VideoCaptureDeviceMFWin: %s",
324 logging::SystemErrorCodeToString(hr).c_str()));