You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004 Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/media/devices/devicemanager.h"
  28. #include "talk/base/fileutils.h"
  29. #include "talk/base/logging.h"
  30. #include "talk/base/pathutils.h"
  31. #include "talk/base/stringutils.h"
  32. #include "talk/base/thread.h"
  33. #include "talk/base/windowpicker.h"
  34. #include "talk/base/windowpickerfactory.h"
  35. #include "talk/media/base/mediacommon.h"
  36. #include "talk/media/devices/deviceinfo.h"
  37. #include "talk/media/devices/filevideocapturer.h"
  38. #if !(defined(IOS) || defined(ANDROID))
  39. #if defined(HAVE_WEBRTC_VIDEO)
  40. #include "talk/media/webrtc/webrtcvideocapturer.h"
  41. #endif
  42. #if defined(HAVE_WEBRTC_VIDEO)
  43. #define VIDEO_CAPTURER_NAME WebRtcVideoCapturer
  44. #endif
  45. #endif
  46. namespace {
  47. bool StringMatchWithWildcard(
  48. const std::pair<const std::basic_string<char>, cricket::VideoFormat> key,
  49. const std::string& val) {
  50. return talk_base::string_match(val.c_str(), key.first.c_str());
  51. }
  52. } // namespace
  53. namespace cricket {
  54. // Initialize to empty string.
  55. const char DeviceManagerInterface::kDefaultDeviceName[] = "";
  56. class DefaultVideoCapturerFactory : public VideoCapturerFactory {
  57. public:
  58. DefaultVideoCapturerFactory() {}
  59. virtual ~DefaultVideoCapturerFactory() {}
  60. VideoCapturer* Create(const Device& device) {
  61. #if defined(VIDEO_CAPTURER_NAME)
  62. VIDEO_CAPTURER_NAME* return_value = new VIDEO_CAPTURER_NAME;
  63. if (!return_value->Init(device)) {
  64. delete return_value;
  65. return NULL;
  66. }
  67. return return_value;
  68. #else
  69. return NULL;
  70. #endif
  71. }
  72. };
  73. DeviceManager::DeviceManager()
  74. : initialized_(false),
  75. device_video_capturer_factory_(new DefaultVideoCapturerFactory),
  76. window_picker_(talk_base::WindowPickerFactory::CreateWindowPicker()) {
  77. }
  78. DeviceManager::~DeviceManager() {
  79. if (initialized()) {
  80. Terminate();
  81. }
  82. }
  83. bool DeviceManager::Init() {
  84. if (!initialized()) {
  85. if (!watcher()->Start()) {
  86. return false;
  87. }
  88. set_initialized(true);
  89. }
  90. return true;
  91. }
  92. void DeviceManager::Terminate() {
  93. if (initialized()) {
  94. watcher()->Stop();
  95. set_initialized(false);
  96. }
  97. }
  98. int DeviceManager::GetCapabilities() {
  99. std::vector<Device> devices;
  100. int caps = VIDEO_RECV;
  101. if (GetAudioInputDevices(&devices) && !devices.empty()) {
  102. caps |= AUDIO_SEND;
  103. }
  104. if (GetAudioOutputDevices(&devices) && !devices.empty()) {
  105. caps |= AUDIO_RECV;
  106. }
  107. if (GetVideoCaptureDevices(&devices) && !devices.empty()) {
  108. caps |= VIDEO_SEND;
  109. }
  110. return caps;
  111. }
  112. bool DeviceManager::GetAudioInputDevices(std::vector<Device>* devices) {
  113. return GetAudioDevices(true, devices);
  114. }
  115. bool DeviceManager::GetAudioOutputDevices(std::vector<Device>* devices) {
  116. return GetAudioDevices(false, devices);
  117. }
  118. bool DeviceManager::GetAudioInputDevice(const std::string& name, Device* out) {
  119. return GetAudioDevice(true, name, out);
  120. }
  121. bool DeviceManager::GetAudioOutputDevice(const std::string& name, Device* out) {
  122. return GetAudioDevice(false, name, out);
  123. }
  124. bool DeviceManager::GetVideoCaptureDevices(std::vector<Device>* devices) {
  125. devices->clear();
  126. #if defined(IOS) || defined(ANDROID)
  127. // On Android, we treat the camera(s) as a single device. Even if there are
  128. // multiple cameras, that's abstracted away at a higher level.
  129. Device dev("camera", "1"); // name and ID
  130. devices->push_back(dev);
  131. return true;
  132. #else
  133. return false;
  134. #endif
  135. }
  136. bool DeviceManager::GetVideoCaptureDevice(const std::string& name,
  137. Device* out) {
  138. // If the name is empty, return the default device.
  139. if (name.empty() || name == kDefaultDeviceName) {
  140. LOG(LS_INFO) << "Creating default VideoCapturer";
  141. return GetDefaultVideoCaptureDevice(out);
  142. }
  143. std::vector<Device> devices;
  144. if (!GetVideoCaptureDevices(&devices)) {
  145. return false;
  146. }
  147. for (std::vector<Device>::const_iterator it = devices.begin();
  148. it != devices.end(); ++it) {
  149. if (name == it->name) {
  150. LOG(LS_INFO) << "Creating VideoCapturer for " << name;
  151. *out = *it;
  152. return true;
  153. }
  154. }
  155. // If |name| is a valid name for a file, return a file video capturer device.
  156. if (talk_base::Filesystem::IsFile(name)) {
  157. LOG(LS_INFO) << "Creating FileVideoCapturer";
  158. *out = FileVideoCapturer::CreateFileVideoCapturerDevice(name);
  159. return true;
  160. }
  161. return false;
  162. }
  163. void DeviceManager::SetVideoCaptureDeviceMaxFormat(
  164. const std::string& uvc_id,
  165. const VideoFormat& max_format) {
  166. max_formats_[uvc_id] = max_format;
  167. }
  168. void DeviceManager::ClearVideoCaptureDeviceMaxFormat(
  169. const std::string& uvc_id) {
  170. max_formats_.erase(uvc_id);
  171. }
  172. VideoCapturer* DeviceManager::CreateVideoCapturer(const Device& device) const {
  173. #if defined(IOS) || defined(ANDROID)
  174. LOG_F(LS_ERROR) << " should never be called!";
  175. return NULL;
  176. #else
  177. // TODO(hellner): throw out the creation of a file video capturer once the
  178. // refactoring is completed.
  179. if (FileVideoCapturer::IsFileVideoCapturerDevice(device)) {
  180. FileVideoCapturer* capturer = new FileVideoCapturer;
  181. if (!capturer->Init(device)) {
  182. delete capturer;
  183. return NULL;
  184. }
  185. capturer->set_repeat(talk_base::kForever);
  186. return capturer;
  187. }
  188. VideoCapturer* capturer = device_video_capturer_factory_->Create(device);
  189. if (!capturer) {
  190. return NULL;
  191. }
  192. VideoFormat video_format;
  193. GetMaxFormat(device, &video_format);
  194. capturer->ConstrainSupportedFormats(video_format);
  195. return capturer;
  196. #endif
  197. }
  198. bool DeviceManager::GetWindows(
  199. std::vector<talk_base::WindowDescription>* descriptions) {
  200. if (!window_picker_) {
  201. return false;
  202. }
  203. return window_picker_->GetWindowList(descriptions);
  204. }
  205. VideoCapturer* DeviceManager::CreateWindowCapturer(talk_base::WindowId window) {
  206. #if defined(WINDOW_CAPTURER_NAME)
  207. WINDOW_CAPTURER_NAME* window_capturer = new WINDOW_CAPTURER_NAME();
  208. if (!window_capturer->Init(window)) {
  209. delete window_capturer;
  210. return NULL;
  211. }
  212. return window_capturer;
  213. #else
  214. return NULL;
  215. #endif
  216. }
  217. bool DeviceManager::GetDesktops(
  218. std::vector<talk_base::DesktopDescription>* descriptions) {
  219. if (!window_picker_) {
  220. return false;
  221. }
  222. return window_picker_->GetDesktopList(descriptions);
  223. }
  224. VideoCapturer* DeviceManager::CreateDesktopCapturer(
  225. talk_base::DesktopId desktop) {
  226. #if defined(DESKTOP_CAPTURER_NAME)
  227. DESKTOP_CAPTURER_NAME* desktop_capturer = new DESKTOP_CAPTURER_NAME();
  228. if (!desktop_capturer->Init(desktop.index())) {
  229. delete desktop_capturer;
  230. return NULL;
  231. }
  232. return desktop_capturer;
  233. #else
  234. return NULL;
  235. #endif
  236. }
  237. bool DeviceManager::GetAudioDevices(bool input,
  238. std::vector<Device>* devs) {
  239. devs->clear();
  240. #if defined(IOS) || defined(ANDROID)
  241. // Under Android, we don't access the device file directly.
  242. // Arbitrary use 0 for the mic and 1 for the output.
  243. // These ids are used in MediaEngine::SetSoundDevices(in, out);
  244. // The strings are for human consumption.
  245. if (input) {
  246. devs->push_back(Device("audiorecord", 0));
  247. } else {
  248. devs->push_back(Device("audiotrack", 1));
  249. }
  250. return true;
  251. #else
  252. return false;
  253. #endif
  254. }
  255. bool DeviceManager::GetAudioDevice(bool is_input, const std::string& name,
  256. Device* out) {
  257. // If the name is empty, return the default device id.
  258. if (name.empty() || name == kDefaultDeviceName) {
  259. *out = Device(name, -1);
  260. return true;
  261. }
  262. std::vector<Device> devices;
  263. bool ret = is_input ? GetAudioInputDevices(&devices) :
  264. GetAudioOutputDevices(&devices);
  265. if (ret) {
  266. ret = false;
  267. for (size_t i = 0; i < devices.size(); ++i) {
  268. if (devices[i].name == name) {
  269. *out = devices[i];
  270. ret = true;
  271. break;
  272. }
  273. }
  274. }
  275. return ret;
  276. }
  277. bool DeviceManager::GetDefaultVideoCaptureDevice(Device* device) {
  278. bool ret = false;
  279. // We just return the first device.
  280. std::vector<Device> devices;
  281. ret = (GetVideoCaptureDevices(&devices) && !devices.empty());
  282. if (ret) {
  283. *device = devices[0];
  284. }
  285. return ret;
  286. }
  287. bool DeviceManager::IsInWhitelist(const std::string& key,
  288. VideoFormat* video_format) const {
  289. std::map<std::string, VideoFormat>::const_iterator found =
  290. std::search_n(max_formats_.begin(), max_formats_.end(), 1, key,
  291. StringMatchWithWildcard);
  292. if (found == max_formats_.end()) {
  293. return false;
  294. }
  295. *video_format = found->second;
  296. return true;
  297. }
  298. bool DeviceManager::IsDeviceWhitelisted(const Device& device,
  299. VideoFormat* video_format) const {
  300. // Match UVC ID if available. Failing that, match device name.
  301. std::string uvc_id;
  302. if (GetUsbUvcId(device, &uvc_id) && IsInWhitelist(uvc_id, video_format)) {
  303. return true;
  304. }
  305. return IsInWhitelist(device.name, video_format);
  306. }
  307. void DeviceManager::GetMaxFormat(const Device& device,
  308. VideoFormat* video_format) const {
  309. if (!IsDeviceWhitelisted(device, video_format)) {
  310. // Default capabilities to VGA.
  311. *video_format = VideoFormat(640,
  312. 480,
  313. cricket::VideoFormat::FpsToInterval(30),
  314. FOURCC_I420);
  315. }
  316. }
  317. bool DeviceManager::ShouldDeviceBeIgnored(const std::string& device_name,
  318. const char* const exclusion_list[]) {
  319. // If exclusion_list is empty return directly.
  320. if (!exclusion_list)
  321. return false;
  322. int i = 0;
  323. while (exclusion_list[i]) {
  324. if (strnicmp(device_name.c_str(), exclusion_list[i],
  325. strlen(exclusion_list[i])) == 0) {
  326. LOG(LS_INFO) << "Ignoring device " << device_name;
  327. return true;
  328. }
  329. ++i;
  330. }
  331. return false;
  332. }
  333. bool DeviceManager::FilterDevices(std::vector<Device>* devices,
  334. const char* const exclusion_list[]) {
  335. if (!devices) {
  336. return false;
  337. }
  338. for (std::vector<Device>::iterator it = devices->begin();
  339. it != devices->end(); ) {
  340. if (ShouldDeviceBeIgnored(it->name, exclusion_list)) {
  341. it = devices->erase(it);
  342. } else {
  343. ++it;
  344. }
  345. }
  346. return true;
  347. }
  348. } // namespace cricket