| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/video/capture/win/video_capture_device_factory_win.h" | 5 #include "media/video/capture/win/video_capture_device_factory_win.h" |
| 6 | 6 |
| 7 #include <mfapi.h> | 7 #include <mfapi.h> |
| 8 #include <mferror.h> | 8 #include <mferror.h> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/win/metro.h" | 15 #include "base/win/metro.h" |
| 15 #include "base/win/scoped_co_mem.h" | 16 #include "base/win/scoped_co_mem.h" |
| 16 #include "base/win/scoped_variant.h" | 17 #include "base/win/scoped_variant.h" |
| 17 #include "base/win/windows_version.h" | 18 #include "base/win/windows_version.h" |
| 18 #include "media/base/media_switches.h" | 19 #include "media/base/media_switches.h" |
| 19 #include "media/video/capture/win/video_capture_device_mf_win.h" | 20 #include "media/video/capture/win/video_capture_device_mf_win.h" |
| 20 #include "media/video/capture/win/video_capture_device_win.h" | 21 #include "media/video/capture/win/video_capture_device_win.h" |
| 21 | 22 |
| 22 using base::win::ScopedCoMem; | 23 using base::win::ScopedCoMem; |
| 23 using base::win::ScopedComPtr; | 24 using base::win::ScopedComPtr; |
| 24 using base::win::ScopedVariant; | 25 using base::win::ScopedVariant; |
| 25 using Name = media::VideoCaptureDevice::Name; | 26 using Name = media::VideoCaptureDevice::Name; |
| 26 using Names = media::VideoCaptureDevice::Names; | 27 using Names = media::VideoCaptureDevice::Names; |
| 27 | 28 |
| 28 namespace media { | 29 namespace media { |
| 29 | 30 |
| 31 // We would like to avoid enumerating and/or using certain devices due to they |
| 32 // provoking crashes or any other reason. This enum is defined for the purposes |
| 33 // of UMA collection. Existing entries cannot be removed. |
| 34 enum BlacklistedCameraNames { |
| 35 BLACKLISTED_CAMERA_GOOGLE_CAMERA_ADAPTER, |
| 36 BLACKLISTED_CAMERA_IP_CAMERA, |
| 37 BLACKLISTED_CAMERA_CYBERLINK_WEBCAM_SPLITTER, |
| 38 // This one must be last, and equal to the previous enumerated value. |
| 39 BLACKLISTED_CAMERA_MAX = BLACKLISTED_CAMERA_CYBERLINK_WEBCAM_SPLITTER |
| 40 }; |
| 41 |
| 30 // Lazy Instance to initialize the MediaFoundation Library. | 42 // Lazy Instance to initialize the MediaFoundation Library. |
| 31 class MFInitializerSingleton { | 43 class MFInitializerSingleton { |
| 32 public: | 44 public: |
| 33 MFInitializerSingleton() { MFStartup(MF_VERSION, MFSTARTUP_LITE); } | 45 MFInitializerSingleton() { MFStartup(MF_VERSION, MFSTARTUP_LITE); } |
| 34 ~MFInitializerSingleton() { MFShutdown(); } | 46 ~MFInitializerSingleton() { MFShutdown(); } |
| 35 }; | 47 }; |
| 36 | 48 |
| 37 static base::LazyInstance<MFInitializerSingleton> g_mf_initialize = | 49 static base::LazyInstance<MFInitializerSingleton> g_mf_initialize = |
| 38 LAZY_INSTANCE_INITIALIZER; | 50 LAZY_INSTANCE_INITIALIZER; |
| 39 | 51 |
| 52 // Blacklisted devices are identified by a characteristic prefix of the name. |
| 53 // This prefix is used case-insensitively. This list must be kept in sync with |
| 54 // |BlacklistedCameraNames|. |
| 55 static const char* kBlacklistedCameraNames[] = { |
| 56 // Name of a fake DirectShow filter on computers with GTalk installed. |
| 57 {"Google Camera Adapter"}, |
| 58 // The following two software WebCams cause crashes. |
| 59 {"IP Camera [JPEG/MJPEG]"}, |
| 60 {"CyberLink Webcam Splitter"} |
| 61 }; |
| 62 |
| 40 static void EnsureMediaFoundationInit() { | 63 static void EnsureMediaFoundationInit() { |
| 41 g_mf_initialize.Get(); | 64 g_mf_initialize.Get(); |
| 42 } | 65 } |
| 43 | 66 |
| 44 static bool LoadMediaFoundationDlls() { | 67 static bool LoadMediaFoundationDlls() { |
| 45 static const wchar_t* const kMfDLLs[] = { | 68 static const wchar_t* const kMfDLLs[] = { |
| 46 L"%WINDIR%\\system32\\mf.dll", | 69 L"%WINDIR%\\system32\\mf.dll", |
| 47 L"%WINDIR%\\system32\\mfplat.dll", | 70 L"%WINDIR%\\system32\\mfplat.dll", |
| 48 L"%WINDIR%\\system32\\mfreadwrite.dll", | 71 L"%WINDIR%\\system32\\mfreadwrite.dll", |
| 49 }; | 72 }; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 106 |
| 84 static bool EnumerateVideoDevicesMediaFoundation(IMFActivate*** devices, | 107 static bool EnumerateVideoDevicesMediaFoundation(IMFActivate*** devices, |
| 85 UINT32* count) { | 108 UINT32* count) { |
| 86 ScopedComPtr<IMFAttributes> attributes; | 109 ScopedComPtr<IMFAttributes> attributes; |
| 87 if (!PrepareVideoCaptureAttributesMediaFoundation(attributes.Receive(), 1)) | 110 if (!PrepareVideoCaptureAttributesMediaFoundation(attributes.Receive(), 1)) |
| 88 return false; | 111 return false; |
| 89 | 112 |
| 90 return SUCCEEDED(MFEnumDeviceSources(attributes, devices, count)); | 113 return SUCCEEDED(MFEnumDeviceSources(attributes, devices, count)); |
| 91 } | 114 } |
| 92 | 115 |
| 116 static bool IsDeviceBlackListed(const std::string& name) { |
| 117 DCHECK_EQ(BLACKLISTED_CAMERA_MAX, |
| 118 static_cast<int>(arraysize(kBlacklistedCameraNames))); |
| 119 for (size_t i = 0; i < arraysize(kBlacklistedCameraNames); ++i) { |
| 120 if (StartsWithASCII(name, kBlacklistedCameraNames[i], false)) { |
| 121 DVLOG(1) << "Enumerated blacklisted device: " << name; |
| 122 UMA_HISTOGRAM_ENUMERATION("Media.VideoCapture.BlacklistedDevice", |
| 123 i, BLACKLISTED_CAMERA_MAX + 1); |
| 124 return true; |
| 125 } |
| 126 } |
| 127 return false; |
| 128 } |
| 129 |
| 93 static void GetDeviceNamesDirectShow( | 130 static void GetDeviceNamesDirectShow( |
| 94 const CLSID& class_id, | 131 const CLSID& class_id, |
| 95 const Name::CaptureApiType capture_api_type, | 132 const Name::CaptureApiType capture_api_type, |
| 96 Names* device_names) { | 133 Names* device_names) { |
| 97 DCHECK(device_names); | 134 DCHECK(device_names); |
| 98 DVLOG(1) << " GetDeviceNamesDirectShow"; | 135 DVLOG(1) << " GetDeviceNamesDirectShow"; |
| 99 | 136 |
| 100 ScopedComPtr<ICreateDevEnum> dev_enum; | 137 ScopedComPtr<ICreateDevEnum> dev_enum; |
| 101 HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL, | 138 HRESULT hr = dev_enum.CreateInstance(CLSID_SystemDeviceEnum, NULL, |
| 102 CLSCTX_INPROC); | 139 CLSCTX_INPROC); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 121 | 158 |
| 122 // Find the description or friendly name. | 159 // Find the description or friendly name. |
| 123 ScopedVariant name; | 160 ScopedVariant name; |
| 124 hr = prop_bag->Read(L"Description", name.Receive(), 0); | 161 hr = prop_bag->Read(L"Description", name.Receive(), 0); |
| 125 if (FAILED(hr)) | 162 if (FAILED(hr)) |
| 126 hr = prop_bag->Read(L"FriendlyName", name.Receive(), 0); | 163 hr = prop_bag->Read(L"FriendlyName", name.Receive(), 0); |
| 127 | 164 |
| 128 if (FAILED(hr) || name.type() != VT_BSTR) | 165 if (FAILED(hr) || name.type() != VT_BSTR) |
| 129 continue; | 166 continue; |
| 130 | 167 |
| 131 // Ignore all VFW drivers and the special Google Camera Adapter. | 168 const std::string device_name(base::SysWideToUTF8(V_BSTR(&name))); |
| 132 // Google Camera Adapter is not a real DirectShow camera device. | 169 if (IsDeviceBlackListed(device_name)) |
| 133 // VFW are very old Video for Windows drivers that can not be used. | |
| 134 const wchar_t* str_ptr = V_BSTR(&name); | |
| 135 // Name of a fake DirectShow filter that exist on computers with | |
| 136 // GTalk installed. | |
| 137 static const char kGoogleCameraAdapter[] = "google camera adapter"; | |
| 138 if (wcsstr(str_ptr, L"(VFW)") != NULL || | |
| 139 LowerCaseEqualsASCII(str_ptr, | |
| 140 str_ptr + arraysize(kGoogleCameraAdapter) - 1, | |
| 141 kGoogleCameraAdapter)) { | |
| 142 continue; | 170 continue; |
| 143 } | |
| 144 | 171 |
| 145 const std::string device_name(base::SysWideToUTF8(str_ptr)); | |
| 146 name.Reset(); | 172 name.Reset(); |
| 147 hr = prop_bag->Read(L"DevicePath", name.Receive(), 0); | 173 hr = prop_bag->Read(L"DevicePath", name.Receive(), 0); |
| 148 std::string id; | 174 std::string id; |
| 149 if (FAILED(hr) || name.type() != VT_BSTR) { | 175 if (FAILED(hr) || name.type() != VT_BSTR) { |
| 150 id = device_name; | 176 id = device_name; |
| 151 } else { | 177 } else { |
| 152 DCHECK_EQ(name.type(), VT_BSTR); | 178 DCHECK_EQ(name.type(), VT_BSTR); |
| 153 id = base::SysWideToUTF8(V_BSTR(&name)); | 179 id = base::SysWideToUTF8(V_BSTR(&name)); |
| 154 } | 180 } |
| 155 device_names->push_back(Name(device_name, id, capture_api_type)); | 181 device_names->push_back(Name(device_name, id, capture_api_type)); |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 const Name& device, | 453 const Name& device, |
| 428 VideoCaptureFormats* formats) { | 454 VideoCaptureFormats* formats) { |
| 429 DCHECK(thread_checker_.CalledOnValidThread()); | 455 DCHECK(thread_checker_.CalledOnValidThread()); |
| 430 if (use_media_foundation_) | 456 if (use_media_foundation_) |
| 431 GetDeviceSupportedFormatsMediaFoundation(device, formats); | 457 GetDeviceSupportedFormatsMediaFoundation(device, formats); |
| 432 else | 458 else |
| 433 GetDeviceSupportedFormatsDirectShow(device, formats); | 459 GetDeviceSupportedFormatsDirectShow(device, formats); |
| 434 } | 460 } |
| 435 | 461 |
| 436 } // namespace media | 462 } // namespace media |
| OLD | NEW |