Chromium Code Reviews| 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/video/capture/win/video_capture_device_mf_win.h" | |
| 6 | |
| 7 #include <mfapi.h> | |
| 8 #include <mferror.h> | |
| 9 | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/synchronization/waitable_event.h" | |
| 13 #include "base/sys_string_conversions.h" | |
| 14 #include "base/win/scoped_co_mem.h" | |
| 15 #include "media/video/capture/win/capability_list_win.h" | |
| 16 | |
| 17 using base::win::ScopedCoMem; | |
| 18 using base::win::ScopedComPtr; | |
| 19 | |
| 20 namespace media { | |
| 21 namespace { | |
| 22 | |
| 23 class MFInitializerSingleton { | |
| 24 public: | |
| 25 MFInitializerSingleton() { MFStartup(MF_VERSION, MFSTARTUP_LITE); } | |
| 26 ~MFInitializerSingleton() { MFShutdown(); } | |
| 27 }; | |
| 28 | |
| 29 static base::LazyInstance<MFInitializerSingleton> g_mf_initialize = | |
| 30 LAZY_INSTANCE_INITIALIZER; | |
| 31 | |
| 32 void EnsureMFInit() { | |
| 33 g_mf_initialize.Get(); | |
| 34 } | |
| 35 | |
| 36 bool PrepareVideoCaptureAttributes(IMFAttributes** attributes, int count) { | |
| 37 EnsureMFInit(); | |
| 38 | |
| 39 if (FAILED(MFCreateAttributes(attributes, count))) | |
| 40 return false; | |
| 41 | |
| 42 return SUCCEEDED((*attributes)->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, | |
| 43 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID)); | |
| 44 } | |
| 45 | |
| 46 bool EnumerateVideoDevices(IMFActivate*** devices, | |
| 47 UINT32* count) { | |
| 48 ScopedComPtr<IMFAttributes> attributes; | |
| 49 if (!PrepareVideoCaptureAttributes(attributes.Receive(), 1)) | |
| 50 return false; | |
| 51 | |
| 52 return SUCCEEDED(MFEnumDeviceSources(attributes, devices, count)); | |
| 53 } | |
| 54 | |
| 55 bool CreateVideoCaptureDevice(const char* sym_link, IMFMediaSource** source) { | |
| 56 ScopedComPtr<IMFAttributes> attributes; | |
| 57 if (!PrepareVideoCaptureAttributes(attributes.Receive(), 2)) | |
| 58 return false; | |
| 59 | |
| 60 attributes->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, | |
| 61 base::SysUTF8ToWide(sym_link).c_str()); | |
| 62 | |
| 63 return SUCCEEDED(MFCreateDeviceSource(attributes, source)); | |
| 64 } | |
| 65 | |
| 66 bool FormatFromGuid(const GUID& guid, VideoCaptureCapability::Format* format) { | |
| 67 struct { | |
| 68 const GUID& guid; | |
| 69 const VideoCaptureCapability::Format format; | |
| 70 } static const kFormatMap[] = { | |
| 71 { MFVideoFormat_I420, VideoCaptureCapability::kI420 }, | |
| 72 { MFVideoFormat_YUY2, VideoCaptureCapability::kYUY2 }, | |
| 73 { MFVideoFormat_UYVY, VideoCaptureCapability::kUYVY }, | |
| 74 { MFVideoFormat_RGB24, VideoCaptureCapability::kRGB24 }, | |
| 75 { MFVideoFormat_ARGB32, VideoCaptureCapability::kARGB }, | |
| 76 { MFVideoFormat_MJPG, VideoCaptureCapability::kMJPEG }, | |
| 77 { MFVideoFormat_YV12, VideoCaptureCapability::kYV12 }, | |
| 78 }; | |
| 79 | |
| 80 for (int i = 0; i < arraysize(kFormatMap); ++i) { | |
| 81 if (kFormatMap[i].guid == guid) { | |
| 82 *format = kFormatMap[i].format; | |
| 83 return true; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 bool GetFrameSize(IMFMediaType* type, int* width, int* height) { | |
| 91 UINT32 width32, height32; | |
| 92 if (FAILED(MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &width32, &height32))) | |
| 93 return false; | |
| 94 *width = width32; | |
| 95 *height = height32; | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 bool GetFrameRate(IMFMediaType* type, int* frame_rate) { | |
| 100 UINT32 numerator, denominator; | |
| 101 if (FAILED(MFGetAttributeRatio(type, MF_MT_FRAME_RATE, &numerator, | |
| 102 &denominator))) { | |
| 103 return false; | |
| 104 } | |
| 105 *frame_rate = numerator / denominator; | |
|
mflodman_chromium_OOO
2012/11/29 20:00:02
Protect agains denominator == 0? For DirectShow, t
tommi (sloooow) - chröme
2012/11/30 07:04:06
Gaa thanks!
| |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 bool FillCapabilitiesFromType(IMFMediaType* type, | |
| 110 VideoCaptureCapability* capability) { | |
| 111 GUID type_guid; | |
| 112 if (FAILED(type->GetGUID(MF_MT_SUBTYPE, &type_guid)) || | |
| 113 !FormatFromGuid(type_guid, &capability->color) || | |
| 114 !GetFrameSize(type, &capability->width, &capability->height) || | |
| 115 !GetFrameRate(type, &capability->frame_rate)) { | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 capability->expected_capture_delay = 0; // Currently not used. | |
| 120 capability->interlaced = false; // Currently not used. | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 HRESULT FillCapabilities(IMFSourceReader* source, | |
| 126 CapabilityList* capabilities) { | |
| 127 DWORD stream_index = 0; | |
| 128 ScopedComPtr<IMFMediaType> type; | |
| 129 HRESULT hr; | |
| 130 while (SUCCEEDED(hr = source->GetNativeMediaType( | |
| 131 MF_SOURCE_READER_FIRST_VIDEO_STREAM, stream_index, type.Receive()))) { | |
| 132 VideoCaptureCapabilityWin capability(stream_index++); | |
| 133 if (FillCapabilitiesFromType(type, &capability)) | |
| 134 capabilities->Add(capability); | |
| 135 type.Release(); | |
| 136 } | |
| 137 | |
| 138 if (SUCCEEDED(hr) && capabilities->empty()) | |
| 139 hr = HRESULT_FROM_WIN32(ERROR_EMPTY); | |
| 140 | |
| 141 return (hr == MF_E_NO_MORE_TYPES) ? S_OK : hr; | |
| 142 } | |
| 143 | |
| 144 } // namespace | |
| 145 | |
| 146 class MFReaderCallback | |
| 147 : public base::RefCountedThreadSafe<MFReaderCallback>, | |
| 148 public IMFSourceReaderCallback { | |
| 149 public: | |
| 150 MFReaderCallback(VideoCaptureDeviceMFWin* observer) | |
| 151 : observer_(observer), wait_event_(NULL) { | |
| 152 } | |
| 153 | |
| 154 void SetSignalOnFlush(base::WaitableEvent* event) { | |
| 155 wait_event_ = event; | |
| 156 } | |
| 157 | |
| 158 STDMETHOD(QueryInterface)(REFIID riid, void** object) { | |
| 159 if (riid != IID_IUnknown && riid != IID_IMFSourceReaderCallback) | |
| 160 return E_NOINTERFACE; | |
| 161 *object = static_cast<IMFSourceReaderCallback*>(this); | |
| 162 AddRef(); | |
| 163 return S_OK; | |
| 164 } | |
| 165 | |
| 166 STDMETHOD_(ULONG, AddRef)() { | |
| 167 base::RefCountedThreadSafe<MFReaderCallback>::AddRef(); | |
| 168 return 1U; | |
| 169 } | |
| 170 | |
| 171 STDMETHOD_(ULONG, Release)() { | |
| 172 base::RefCountedThreadSafe<MFReaderCallback>::Release(); | |
| 173 return 1U; | |
| 174 } | |
| 175 | |
| 176 STDMETHOD(OnReadSample)(HRESULT status, DWORD stream_index, | |
| 177 DWORD stream_flags, LONGLONG time_stamp, IMFSample* sample) { | |
| 178 base::Time stamp(base::Time::Now()); | |
| 179 if (!sample) { | |
| 180 observer_->OnIncomingCapturedFrame(NULL, 0, stamp); | |
| 181 return S_OK; | |
| 182 } | |
| 183 | |
| 184 DWORD count = 0; | |
| 185 sample->GetBufferCount(&count); | |
|
mflodman_chromium_OOO
2012/11/29 20:00:02
When testing, have you ever seen multiple buffers
tommi (sloooow) - chröme
2012/11/30 07:04:06
I've never seen that. From what I've read it's un
| |
| 186 | |
| 187 for (DWORD i = 0; i < count; ++i) { | |
| 188 ScopedComPtr<IMFMediaBuffer> buffer; | |
| 189 sample->GetBufferByIndex(i, buffer.Receive()); | |
| 190 if (buffer) { | |
| 191 DWORD length = 0, max_length = 0; | |
| 192 BYTE* data = NULL; | |
| 193 buffer->Lock(&data, &max_length, &length); | |
| 194 observer_->OnIncomingCapturedFrame(data, length, stamp); | |
| 195 buffer->Unlock(); | |
| 196 } | |
| 197 } | |
| 198 return S_OK; | |
| 199 } | |
| 200 | |
| 201 STDMETHOD(OnFlush)(DWORD stream_index) { | |
| 202 if (wait_event_) { | |
| 203 wait_event_->Signal(); | |
| 204 wait_event_ = NULL; | |
| 205 } | |
| 206 return S_OK; | |
| 207 } | |
| 208 | |
| 209 STDMETHOD(OnEvent)(DWORD stream_index, IMFMediaEvent* event) { | |
| 210 NOTIMPLEMENTED(); | |
| 211 return S_OK; | |
| 212 } | |
| 213 | |
| 214 private: | |
| 215 friend class base::RefCountedThreadSafe<MFReaderCallback>; | |
| 216 ~MFReaderCallback() {} | |
| 217 | |
| 218 VideoCaptureDeviceMFWin* observer_; | |
| 219 base::WaitableEvent* wait_event_; | |
| 220 }; | |
| 221 | |
| 222 // static | |
| 223 void VideoCaptureDeviceMFWin::GetDeviceNames(Names* device_names) { | |
| 224 ScopedCoMem<IMFActivate*> devices; | |
| 225 UINT32 count; | |
| 226 if (!EnumerateVideoDevices(&devices, &count)) | |
| 227 return; | |
| 228 | |
| 229 HRESULT hr; | |
| 230 for (UINT32 i = 0; i < count; ++i) { | |
| 231 UINT32 name_size, id_size; | |
| 232 ScopedCoMem<wchar_t> name, id; | |
| 233 if (SUCCEEDED(hr = devices[i]->GetAllocatedString( | |
| 234 MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &name, &name_size)) && | |
| 235 SUCCEEDED(hr = devices[i]->GetAllocatedString( | |
| 236 MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &id, | |
| 237 &id_size))) { | |
| 238 std::wstring name_w(name, name_size), id_w(id, id_size); | |
| 239 Name device; | |
| 240 device.device_name = base::SysWideToUTF8(name_w); | |
| 241 device.unique_id = base::SysWideToUTF8(id_w); | |
| 242 device_names->push_back(device); | |
| 243 } else { | |
| 244 DLOG(WARNING) << "GetAllocatedString failed: " << std::hex << hr; | |
| 245 } | |
| 246 devices[i]->Release(); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 VideoCaptureDeviceMFWin::VideoCaptureDeviceMFWin(const Name& device_name) | |
| 251 : name_(device_name), observer_(NULL), capture_(0) { | |
| 252 DetachFromThread(); | |
| 253 } | |
| 254 | |
| 255 VideoCaptureDeviceMFWin::~VideoCaptureDeviceMFWin() { | |
| 256 DCHECK(CalledOnValidThread()); | |
| 257 } | |
| 258 | |
| 259 bool VideoCaptureDeviceMFWin::Init() { | |
| 260 DCHECK(CalledOnValidThread()); | |
| 261 DCHECK(!reader_); | |
| 262 | |
| 263 ScopedComPtr<IMFMediaSource> source; | |
| 264 if (!CreateVideoCaptureDevice(name_.unique_id.c_str(), source.Receive())) | |
| 265 return false; | |
| 266 | |
| 267 ScopedComPtr<IMFAttributes> attributes; | |
| 268 MFCreateAttributes(attributes.Receive(), 1); | |
| 269 DCHECK(attributes); | |
| 270 | |
| 271 callback_ = new MFReaderCallback(this); | |
| 272 attributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, callback_.get()); | |
| 273 | |
| 274 return SUCCEEDED(MFCreateSourceReaderFromMediaSource(source, attributes, | |
| 275 reader_.Receive())); | |
| 276 } | |
| 277 | |
| 278 void VideoCaptureDeviceMFWin::Allocate( | |
| 279 int width, | |
| 280 int height, | |
| 281 int frame_rate, | |
| 282 VideoCaptureDevice::EventHandler* observer) { | |
| 283 DCHECK(CalledOnValidThread()); | |
| 284 | |
| 285 base::AutoLock lock(lock_); | |
| 286 | |
| 287 if (observer_) { | |
| 288 DCHECK_EQ(observer, observer_); | |
| 289 return; | |
| 290 } | |
| 291 | |
| 292 observer_ = observer; | |
| 293 DCHECK_EQ(capture_, false); | |
| 294 | |
| 295 CapabilityList capabilities; | |
| 296 HRESULT hr = S_OK; | |
| 297 if (!reader_ || FAILED(hr = FillCapabilities(reader_, &capabilities))) { | |
| 298 OnError(hr); | |
| 299 return; | |
| 300 } | |
| 301 | |
| 302 const VideoCaptureCapabilityWin& found_capability = | |
| 303 capabilities.GetBestMatchedCapability(width, height, frame_rate); | |
| 304 | |
| 305 ScopedComPtr<IMFMediaType> type; | |
| 306 if (FAILED(hr = reader_->GetNativeMediaType( | |
| 307 MF_SOURCE_READER_FIRST_VIDEO_STREAM, found_capability.stream_index, | |
| 308 type.Receive())) || | |
| 309 FAILED(hr = reader_->SetCurrentMediaType( | |
| 310 MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, type))) { | |
| 311 OnError(hr); | |
| 312 return; | |
| 313 } | |
| 314 | |
| 315 observer_->OnFrameInfo(found_capability); | |
| 316 } | |
| 317 | |
| 318 void VideoCaptureDeviceMFWin::Start() { | |
| 319 DCHECK(CalledOnValidThread()); | |
| 320 | |
| 321 base::AutoLock lock(lock_); | |
| 322 if (!capture_) { | |
| 323 capture_ = true; | |
| 324 HRESULT hr; | |
| 325 if (FAILED(hr = reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, | |
| 326 NULL, NULL, NULL, NULL))) { | |
| 327 OnError(hr); | |
| 328 capture_ = false; | |
| 329 } | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 void VideoCaptureDeviceMFWin::Stop() { | |
| 334 DCHECK(CalledOnValidThread()); | |
| 335 base::WaitableEvent flushed(false, false); | |
| 336 bool wait = false; | |
| 337 { | |
| 338 base::AutoLock lock(lock_); | |
| 339 if (capture_) { | |
| 340 capture_ = false; | |
| 341 callback_->SetSignalOnFlush(&flushed); | |
| 342 HRESULT hr = reader_->Flush(MF_SOURCE_READER_ALL_STREAMS); | |
| 343 wait = SUCCEEDED(hr); | |
| 344 if (!wait) { | |
| 345 callback_->SetSignalOnFlush(NULL); | |
| 346 OnError(hr); | |
| 347 } | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 if (wait) | |
| 352 flushed.Wait(); | |
| 353 } | |
| 354 | |
| 355 void VideoCaptureDeviceMFWin::DeAllocate() { | |
| 356 DCHECK(CalledOnValidThread()); | |
| 357 | |
| 358 Stop(); | |
| 359 | |
| 360 base::AutoLock lock(lock_); | |
| 361 observer_ = NULL; | |
| 362 } | |
| 363 | |
| 364 const VideoCaptureDevice::Name& VideoCaptureDeviceMFWin::device_name() { | |
| 365 DCHECK(CalledOnValidThread()); | |
| 366 return name_; | |
| 367 } | |
| 368 | |
| 369 void VideoCaptureDeviceMFWin::OnIncomingCapturedFrame( | |
| 370 const uint8* data, | |
| 371 int length, | |
| 372 const base::Time& time_stamp) { | |
| 373 base::AutoLock lock(lock_); | |
| 374 if (data && observer_) | |
| 375 observer_->OnIncomingCapturedFrame(data, length, time_stamp); | |
| 376 | |
| 377 if (capture_) { | |
| 378 HRESULT hr = reader_->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, | |
| 379 NULL, NULL, NULL, NULL); | |
| 380 if (FAILED(hr)) { | |
| 381 // If running the *VideoCap* unit tests on repeat, this can sometimes | |
| 382 // fail with HRESULT_FROM_WINHRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION). | |
| 383 // It's not clear to me why this is, but it is possible that it has | |
| 384 // something to do with this bug: | |
| 385 // http://support.microsoft.com/kb/979567 | |
| 386 OnError(hr); | |
| 387 } | |
| 388 } | |
| 389 } | |
| 390 | |
| 391 void VideoCaptureDeviceMFWin::OnError(HRESULT hr) { | |
| 392 DLOG(ERROR) << "VideoCaptureDeviceMFWin: " << std::hex << hr; | |
| 393 if (observer_) | |
| 394 observer_->OnError(); | |
| 395 } | |
| 396 | |
| 397 } // namespace media | |
| OLD | NEW |