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.

406 lines
13 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/linuxdevicemanager.h"
  28. #include <unistd.h>
  29. #include "talk/base/fileutils.h"
  30. #include "talk/base/linux.h"
  31. #include "talk/base/logging.h"
  32. #include "talk/base/pathutils.h"
  33. #include "talk/base/physicalsocketserver.h"
  34. #include "talk/base/stream.h"
  35. #include "talk/base/stringutils.h"
  36. #include "talk/base/thread.h"
  37. #include "talk/media/base/mediacommon.h"
  38. #include "talk/media/devices/libudevsymboltable.h"
  39. #include "talk/media/devices/v4llookup.h"
  40. #include "talk/sound/platformsoundsystem.h"
  41. #include "talk/sound/platformsoundsystemfactory.h"
  42. #include "talk/sound/sounddevicelocator.h"
  43. #include "talk/sound/soundsysteminterface.h"
  44. namespace cricket {
  45. DeviceManagerInterface* DeviceManagerFactory::Create() {
  46. return new LinuxDeviceManager();
  47. }
  48. class LinuxDeviceWatcher
  49. : public DeviceWatcher,
  50. private talk_base::Dispatcher {
  51. public:
  52. explicit LinuxDeviceWatcher(DeviceManagerInterface* dm);
  53. virtual ~LinuxDeviceWatcher();
  54. virtual bool Start();
  55. virtual void Stop();
  56. private:
  57. virtual uint32 GetRequestedEvents();
  58. virtual void OnPreEvent(uint32 ff);
  59. virtual void OnEvent(uint32 ff, int err);
  60. virtual int GetDescriptor();
  61. virtual bool IsDescriptorClosed();
  62. DeviceManagerInterface* manager_;
  63. LibUDevSymbolTable libudev_;
  64. struct udev* udev_;
  65. struct udev_monitor* udev_monitor_;
  66. bool registered_;
  67. };
  68. static const char* const kFilteredAudioDevicesName[] = {
  69. #if defined(CHROMEOS)
  70. "surround40:",
  71. "surround41:",
  72. "surround50:",
  73. "surround51:",
  74. "surround71:",
  75. "iec958:", // S/PDIF
  76. #endif
  77. NULL,
  78. };
  79. static const char* kFilteredVideoDevicesName[] = {
  80. NULL,
  81. };
  82. LinuxDeviceManager::LinuxDeviceManager()
  83. : sound_system_(new PlatformSoundSystemFactory()) {
  84. set_watcher(new LinuxDeviceWatcher(this));
  85. }
  86. LinuxDeviceManager::~LinuxDeviceManager() {
  87. }
  88. bool LinuxDeviceManager::GetAudioDevices(bool input,
  89. std::vector<Device>* devs) {
  90. devs->clear();
  91. if (!sound_system_.get()) {
  92. return false;
  93. }
  94. SoundSystemInterface::SoundDeviceLocatorList list;
  95. bool success;
  96. if (input) {
  97. success = sound_system_->EnumerateCaptureDevices(&list);
  98. } else {
  99. success = sound_system_->EnumeratePlaybackDevices(&list);
  100. }
  101. if (!success) {
  102. LOG(LS_ERROR) << "Can't enumerate devices";
  103. sound_system_.release();
  104. return false;
  105. }
  106. // We have to start the index at 1 because webrtc VoiceEngine puts the default
  107. // device at index 0, but Enumerate(Capture|Playback)Devices does not include
  108. // a locator for the default device.
  109. int index = 1;
  110. for (SoundSystemInterface::SoundDeviceLocatorList::iterator i = list.begin();
  111. i != list.end();
  112. ++i, ++index) {
  113. devs->push_back(Device((*i)->name(), index));
  114. }
  115. SoundSystemInterface::ClearSoundDeviceLocatorList(&list);
  116. sound_system_.release();
  117. return FilterDevices(devs, kFilteredAudioDevicesName);
  118. }
  119. static const std::string kVideoMetaPathK2_4("/proc/video/dev/");
  120. static const std::string kVideoMetaPathK2_6("/sys/class/video4linux/");
  121. enum MetaType { M2_4, M2_6, NONE };
  122. static void ScanDeviceDirectory(const std::string& devdir,
  123. std::vector<Device>* devices) {
  124. talk_base::scoped_ptr<talk_base::DirectoryIterator> directoryIterator(
  125. talk_base::Filesystem::IterateDirectory());
  126. if (directoryIterator->Iterate(talk_base::Pathname(devdir))) {
  127. do {
  128. std::string filename = directoryIterator->Name();
  129. std::string device_name = devdir + filename;
  130. if (!directoryIterator->IsDots()) {
  131. if (filename.find("video") == 0 &&
  132. V4LLookup::IsV4L2Device(device_name)) {
  133. devices->push_back(Device(device_name, device_name));
  134. }
  135. }
  136. } while (directoryIterator->Next());
  137. }
  138. }
  139. static std::string GetVideoDeviceNameK2_6(const std::string& device_meta_path) {
  140. std::string device_name;
  141. talk_base::scoped_ptr<talk_base::FileStream> device_meta_stream(
  142. talk_base::Filesystem::OpenFile(device_meta_path, "r"));
  143. if (device_meta_stream) {
  144. if (device_meta_stream->ReadLine(&device_name) != talk_base::SR_SUCCESS) {
  145. LOG(LS_ERROR) << "Failed to read V4L2 device meta " << device_meta_path;
  146. }
  147. device_meta_stream->Close();
  148. }
  149. return device_name;
  150. }
  151. static std::string Trim(const std::string& s, const std::string& drop = " \t") {
  152. std::string::size_type first = s.find_first_not_of(drop);
  153. std::string::size_type last = s.find_last_not_of(drop);
  154. if (first == std::string::npos || last == std::string::npos)
  155. return std::string("");
  156. return s.substr(first, last - first + 1);
  157. }
  158. static std::string GetVideoDeviceNameK2_4(const std::string& device_meta_path) {
  159. talk_base::ConfigParser::MapVector all_values;
  160. talk_base::ConfigParser config_parser;
  161. talk_base::FileStream* file_stream =
  162. talk_base::Filesystem::OpenFile(device_meta_path, "r");
  163. if (file_stream == NULL) return "";
  164. config_parser.Attach(file_stream);
  165. config_parser.Parse(&all_values);
  166. for (talk_base::ConfigParser::MapVector::iterator i = all_values.begin();
  167. i != all_values.end(); ++i) {
  168. talk_base::ConfigParser::SimpleMap::iterator device_name_i =
  169. i->find("name");
  170. if (device_name_i != i->end()) {
  171. return device_name_i->second;
  172. }
  173. }
  174. return "";
  175. }
  176. static std::string GetVideoDeviceName(MetaType meta,
  177. const std::string& device_file_name) {
  178. std::string device_meta_path;
  179. std::string device_name;
  180. std::string meta_file_path;
  181. if (meta == M2_6) {
  182. meta_file_path = kVideoMetaPathK2_6 + device_file_name + "/name";
  183. LOG(LS_INFO) << "Trying " + meta_file_path;
  184. device_name = GetVideoDeviceNameK2_6(meta_file_path);
  185. if (device_name.empty()) {
  186. meta_file_path = kVideoMetaPathK2_6 + device_file_name + "/model";
  187. LOG(LS_INFO) << "Trying " << meta_file_path;
  188. device_name = GetVideoDeviceNameK2_6(meta_file_path);
  189. }
  190. } else {
  191. meta_file_path = kVideoMetaPathK2_4 + device_file_name;
  192. LOG(LS_INFO) << "Trying " << meta_file_path;
  193. device_name = GetVideoDeviceNameK2_4(meta_file_path);
  194. }
  195. if (device_name.empty()) {
  196. device_name = "/dev/" + device_file_name;
  197. LOG(LS_ERROR)
  198. << "Device name not found, defaulting to device path " << device_name;
  199. }
  200. LOG(LS_INFO) << "Name for " << device_file_name << " is " << device_name;
  201. return Trim(device_name);
  202. }
  203. static void ScanV4L2Devices(std::vector<Device>* devices) {
  204. LOG(LS_INFO) << ("Enumerating V4L2 devices");
  205. MetaType meta;
  206. std::string metadata_dir;
  207. talk_base::scoped_ptr<talk_base::DirectoryIterator> directoryIterator(
  208. talk_base::Filesystem::IterateDirectory());
  209. // Try and guess kernel version
  210. if (directoryIterator->Iterate(kVideoMetaPathK2_6)) {
  211. meta = M2_6;
  212. metadata_dir = kVideoMetaPathK2_6;
  213. } else if (directoryIterator->Iterate(kVideoMetaPathK2_4)) {
  214. meta = M2_4;
  215. metadata_dir = kVideoMetaPathK2_4;
  216. } else {
  217. meta = NONE;
  218. }
  219. if (meta != NONE) {
  220. LOG(LS_INFO) << "V4L2 device metadata found at " << metadata_dir;
  221. do {
  222. std::string filename = directoryIterator->Name();
  223. if (filename.find("video") == 0) {
  224. std::string device_path = "/dev/" + filename;
  225. if (V4LLookup::IsV4L2Device(device_path)) {
  226. devices->push_back(
  227. Device(GetVideoDeviceName(meta, filename), device_path));
  228. }
  229. }
  230. } while (directoryIterator->Next());
  231. } else {
  232. LOG(LS_ERROR) << "Unable to detect v4l2 metadata directory";
  233. }
  234. if (devices->size() == 0) {
  235. LOG(LS_INFO) << "Plan B. Scanning all video devices in /dev directory";
  236. ScanDeviceDirectory("/dev/", devices);
  237. }
  238. LOG(LS_INFO) << "Total V4L2 devices found : " << devices->size();
  239. }
  240. bool LinuxDeviceManager::GetVideoCaptureDevices(std::vector<Device>* devices) {
  241. devices->clear();
  242. ScanV4L2Devices(devices);
  243. return FilterDevices(devices, kFilteredVideoDevicesName);
  244. }
  245. LinuxDeviceWatcher::LinuxDeviceWatcher(DeviceManagerInterface* dm)
  246. : DeviceWatcher(dm),
  247. manager_(dm),
  248. udev_(NULL),
  249. udev_monitor_(NULL),
  250. registered_(false) {
  251. }
  252. LinuxDeviceWatcher::~LinuxDeviceWatcher() {
  253. }
  254. bool LinuxDeviceWatcher::Start() {
  255. // We deliberately return true in the failure paths here because libudev is
  256. // not a critical component of a Linux system so it may not be present/usable,
  257. // and we don't want to halt LinuxDeviceManager initialization in such a case.
  258. if (!libudev_.Load()) {
  259. LOG(LS_WARNING) << "libudev not present/usable; LinuxDeviceWatcher disabled";
  260. return true;
  261. }
  262. udev_ = libudev_.udev_new()();
  263. if (!udev_) {
  264. LOG_ERR(LS_ERROR) << "udev_new()";
  265. return true;
  266. }
  267. // The second argument here is the event source. It can be either "kernel" or
  268. // "udev", but "udev" is the only correct choice. Apps listen on udev and the
  269. // udev daemon in turn listens on the kernel.
  270. udev_monitor_ = libudev_.udev_monitor_new_from_netlink()(udev_, "udev");
  271. if (!udev_monitor_) {
  272. LOG_ERR(LS_ERROR) << "udev_monitor_new_from_netlink()";
  273. return true;
  274. }
  275. // We only listen for changes in the video devices. Audio devices are more or
  276. // less unimportant because receiving device change notifications really only
  277. // matters for broadcasting updated send/recv capabilities based on whether
  278. // there is at least one device available, and almost all computers have at
  279. // least one audio device. Also, PulseAudio device notifications don't come
  280. // from the udev daemon, they come from the PulseAudio daemon, so we'd only
  281. // want to listen for audio device changes from udev if using ALSA. For
  282. // simplicity, we don't bother with any audio stuff at all.
  283. if (libudev_.udev_monitor_filter_add_match_subsystem_devtype()(
  284. udev_monitor_, "video4linux", NULL) < 0) {
  285. LOG_ERR(LS_ERROR) << "udev_monitor_filter_add_match_subsystem_devtype()";
  286. return true;
  287. }
  288. if (libudev_.udev_monitor_enable_receiving()(udev_monitor_) < 0) {
  289. LOG_ERR(LS_ERROR) << "udev_monitor_enable_receiving()";
  290. return true;
  291. }
  292. static_cast<talk_base::PhysicalSocketServer*>(
  293. talk_base::Thread::Current()->socketserver())->Add(this);
  294. registered_ = true;
  295. return true;
  296. }
  297. void LinuxDeviceWatcher::Stop() {
  298. if (registered_) {
  299. static_cast<talk_base::PhysicalSocketServer*>(
  300. talk_base::Thread::Current()->socketserver())->Remove(this);
  301. registered_ = false;
  302. }
  303. if (udev_monitor_) {
  304. libudev_.udev_monitor_unref()(udev_monitor_);
  305. udev_monitor_ = NULL;
  306. }
  307. if (udev_) {
  308. libudev_.udev_unref()(udev_);
  309. udev_ = NULL;
  310. }
  311. libudev_.Unload();
  312. }
  313. uint32 LinuxDeviceWatcher::GetRequestedEvents() {
  314. return talk_base::DE_READ;
  315. }
  316. void LinuxDeviceWatcher::OnPreEvent(uint32 ff) {
  317. // Nothing to do.
  318. }
  319. void LinuxDeviceWatcher::OnEvent(uint32 ff, int err) {
  320. udev_device* device = libudev_.udev_monitor_receive_device()(udev_monitor_);
  321. if (!device) {
  322. // Probably the socket connection to the udev daemon was terminated (perhaps
  323. // the daemon crashed or is being restarted?).
  324. LOG_ERR(LS_WARNING) << "udev_monitor_receive_device()";
  325. // Stop listening to avoid potential livelock (an fd with EOF in it is
  326. // always considered readable).
  327. static_cast<talk_base::PhysicalSocketServer*>(
  328. talk_base::Thread::Current()->socketserver())->Remove(this);
  329. registered_ = false;
  330. return;
  331. }
  332. // Else we read the device successfully.
  333. // Since we already have our own filesystem-based device enumeration code, we
  334. // simply re-enumerate rather than inspecting the device event.
  335. libudev_.udev_device_unref()(device);
  336. manager_->SignalDevicesChange();
  337. }
  338. int LinuxDeviceWatcher::GetDescriptor() {
  339. return libudev_.udev_monitor_get_fd()(udev_monitor_);
  340. }
  341. bool LinuxDeviceWatcher::IsDescriptorClosed() {
  342. // If it is closed then we will just get an error in
  343. // udev_monitor_receive_device and unregister, so we don't need to check for
  344. // it separately.
  345. return false;
  346. }
  347. }; // namespace cricket