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.

173 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012 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/deviceinfo.h"
  28. #include "talk/base/common.h" // for ASSERT
  29. #include "talk/media/devices/libudevsymboltable.h"
  30. namespace cricket {
  31. class ScopedLibUdev {
  32. public:
  33. static ScopedLibUdev* Create() {
  34. ScopedLibUdev* ret_val = new ScopedLibUdev();
  35. if (!ret_val->Init()) {
  36. delete ret_val;
  37. return NULL;
  38. }
  39. return ret_val;
  40. }
  41. ~ScopedLibUdev() {
  42. libudev_.Unload();
  43. }
  44. LibUDevSymbolTable* instance() { return &libudev_; }
  45. private:
  46. ScopedLibUdev() {}
  47. bool Init() {
  48. return libudev_.Load();
  49. }
  50. LibUDevSymbolTable libudev_;
  51. };
  52. class ScopedUdev {
  53. public:
  54. explicit ScopedUdev(LibUDevSymbolTable* libudev) : libudev_(libudev) {
  55. udev_ = libudev_->udev_new()();
  56. }
  57. ~ScopedUdev() {
  58. if (udev_) libudev_->udev_unref()(udev_);
  59. }
  60. udev* instance() { return udev_; }
  61. private:
  62. LibUDevSymbolTable* libudev_;
  63. udev* udev_;
  64. };
  65. class ScopedUdevEnumerate {
  66. public:
  67. ScopedUdevEnumerate(LibUDevSymbolTable* libudev, udev* udev)
  68. : libudev_(libudev) {
  69. enumerate_ = libudev_->udev_enumerate_new()(udev);
  70. }
  71. ~ScopedUdevEnumerate() {
  72. if (enumerate_) libudev_->udev_enumerate_unref()(enumerate_);
  73. }
  74. udev_enumerate* instance() { return enumerate_; }
  75. private:
  76. LibUDevSymbolTable* libudev_;
  77. udev_enumerate* enumerate_;
  78. };
  79. bool GetUsbProperty(const Device& device, const char* property_name,
  80. std::string* property) {
  81. talk_base::scoped_ptr<ScopedLibUdev> libudev_context(ScopedLibUdev::Create());
  82. if (!libudev_context) {
  83. return false;
  84. }
  85. ScopedUdev udev_context(libudev_context->instance());
  86. if (!udev_context.instance()) {
  87. return false;
  88. }
  89. ScopedUdevEnumerate enumerate_context(libudev_context->instance(),
  90. udev_context.instance());
  91. if (!enumerate_context.instance()) {
  92. return false;
  93. }
  94. libudev_context->instance()->udev_enumerate_add_match_subsystem()(
  95. enumerate_context.instance(), "video4linux");
  96. libudev_context->instance()->udev_enumerate_scan_devices()(
  97. enumerate_context.instance());
  98. udev_list_entry* devices =
  99. libudev_context->instance()->udev_enumerate_get_list_entry()(
  100. enumerate_context.instance());
  101. if (!devices) {
  102. return false;
  103. }
  104. udev_list_entry* dev_list_entry = NULL;
  105. const char* property_value = NULL;
  106. // Macro that expands to a for-loop over the devices.
  107. for (dev_list_entry = devices; dev_list_entry != NULL;
  108. dev_list_entry = libudev_context->instance()->
  109. udev_list_entry_get_next()(dev_list_entry)) {
  110. const char* path = libudev_context->instance()->udev_list_entry_get_name()(
  111. dev_list_entry);
  112. if (!path) continue;
  113. udev_device* dev =
  114. libudev_context->instance()->udev_device_new_from_syspath()(
  115. udev_context.instance(), path);
  116. if (!dev) continue;
  117. const char* device_node =
  118. libudev_context->instance()->udev_device_get_devnode()(dev);
  119. if (!device_node || device.id.compare(device_node) != 0) {
  120. continue;
  121. }
  122. dev = libudev_context->instance()->
  123. udev_device_get_parent_with_subsystem_devtype()(
  124. dev, "usb", "usb_device");
  125. if (!dev) continue;
  126. property_value = libudev_context->instance()->
  127. udev_device_get_sysattr_value()(
  128. dev, property_name);
  129. break;
  130. }
  131. if (!property_value) {
  132. return false;
  133. }
  134. property->assign(property_value);
  135. return true;
  136. }
  137. bool GetUsbUvcId(const Device& device, std::string* uvc_id) {
  138. std::string id_vendor;
  139. std::string id_product;
  140. if (!GetUsbProperty(device, "idVendor", &id_vendor)) {
  141. return false;
  142. }
  143. if (!GetUsbProperty(device, "idProduct", &id_product)) {
  144. return false;
  145. }
  146. uvc_id->clear();
  147. uvc_id->append(id_vendor);
  148. uvc_id->append(":");
  149. uvc_id->append(id_product);
  150. return true;
  151. }
  152. bool GetUsbVersion(const Device& device, std::string* usb_version) {
  153. return GetUsbProperty(device, "version", usb_version);
  154. }
  155. } // namespace cricket