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