device_monitor_udev.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2013 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. // libudev is used for monitoring device changes.
  5. #include "media/device_monitors/device_monitor_udev.h"
  6. #include <string>
  7. #include <vector>
  8. #include "base/bind.h"
  9. #include "base/sequence_checker.h"
  10. #include "base/system/system_monitor.h"
  11. #include "base/task/thread_pool.h"
  12. #include "device/udev_linux/udev.h"
  13. #include "device/udev_linux/udev_watcher.h"
  14. namespace {
  15. struct SubsystemMap {
  16. base::SystemMonitor::DeviceType device_type;
  17. const char* subsystem;
  18. const char* devtype;
  19. };
  20. const char kAudioSubsystem[] = "sound";
  21. const char kVideoSubsystem[] = "video4linux";
  22. // Add more subsystems here for monitoring.
  23. const SubsystemMap kSubsystemMap[] = {
  24. {base::SystemMonitor::DEVTYPE_AUDIO, kAudioSubsystem, ""},
  25. {base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE, kVideoSubsystem, ""},
  26. };
  27. } // namespace
  28. namespace media {
  29. // Wraps a device::UdevWatcher with an API that makes it easier to use from
  30. // DeviceMonitorLinux. Since it is essentially a wrapper around blocking udev
  31. // calls, Initialize() must be called from a task runner that can block.
  32. class DeviceMonitorLinux::BlockingTaskRunnerHelper
  33. : public device::UdevWatcher::Observer {
  34. public:
  35. BlockingTaskRunnerHelper();
  36. BlockingTaskRunnerHelper(const BlockingTaskRunnerHelper&) = delete;
  37. BlockingTaskRunnerHelper& operator=(const BlockingTaskRunnerHelper&) = delete;
  38. ~BlockingTaskRunnerHelper() override = default;
  39. void Initialize();
  40. private:
  41. void OnDevicesChanged(device::ScopedUdevDevicePtr device);
  42. // device::UdevWatcher::Observer overrides
  43. void OnDeviceAdded(device::ScopedUdevDevicePtr device) override;
  44. void OnDeviceRemoved(device::ScopedUdevDevicePtr device) override;
  45. void OnDeviceChanged(device::ScopedUdevDevicePtr device) override;
  46. std::unique_ptr<device::UdevWatcher> udev_watcher_;
  47. SEQUENCE_CHECKER(sequence_checker_);
  48. };
  49. DeviceMonitorLinux::BlockingTaskRunnerHelper::BlockingTaskRunnerHelper() {
  50. // Detaches from the sequence on which this object was created. It will be
  51. // bound to its owning sequence when Initialize() is called.
  52. DETACH_FROM_SEQUENCE(sequence_checker_);
  53. }
  54. void DeviceMonitorLinux::BlockingTaskRunnerHelper::Initialize() {
  55. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  56. std::vector<device::UdevWatcher::Filter> filters;
  57. for (const auto& [device_type, subsys, devtype] : kSubsystemMap) {
  58. filters.emplace_back(subsys, devtype);
  59. }
  60. udev_watcher_ = device::UdevWatcher::StartWatching(this, filters);
  61. }
  62. void DeviceMonitorLinux::BlockingTaskRunnerHelper::OnDeviceAdded(
  63. device::ScopedUdevDevicePtr device) {
  64. OnDevicesChanged(std::move(device));
  65. }
  66. void DeviceMonitorLinux::BlockingTaskRunnerHelper::OnDeviceRemoved(
  67. device::ScopedUdevDevicePtr device) {
  68. OnDevicesChanged(std::move(device));
  69. }
  70. void DeviceMonitorLinux::BlockingTaskRunnerHelper::OnDeviceChanged(
  71. device::ScopedUdevDevicePtr device) {
  72. OnDevicesChanged(std::move(device));
  73. }
  74. void DeviceMonitorLinux::BlockingTaskRunnerHelper::OnDevicesChanged(
  75. device::ScopedUdevDevicePtr device) {
  76. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  77. base::SystemMonitor::DeviceType type = base::SystemMonitor::DEVTYPE_UNKNOWN;
  78. const std::string subsystem(device::udev_device_get_subsystem(device.get()));
  79. for (const auto& [device_type, subsys, devtype] : kSubsystemMap) {
  80. if (subsystem == subsys) {
  81. type = device_type;
  82. break;
  83. }
  84. }
  85. DCHECK_NE(type, base::SystemMonitor::DEVTYPE_UNKNOWN);
  86. // base::SystemMonitor takes care of notifying each observer in their own task
  87. // runner via base::ObserverListThreadSafe.
  88. base::SystemMonitor::Get()->ProcessDevicesChanged(type);
  89. }
  90. DeviceMonitorLinux::DeviceMonitorLinux()
  91. : blocking_task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
  92. {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
  93. base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})),
  94. blocking_task_helper_(new BlockingTaskRunnerHelper,
  95. base::OnTaskRunnerDeleter(blocking_task_runner_)) {
  96. // Unretained() is safe because the deletion of |blocking_task_helper_|
  97. // is scheduled on |blocking_task_runner_| when DeviceMonitorLinux is
  98. // deleted.
  99. blocking_task_runner_->PostTask(
  100. FROM_HERE,
  101. base::BindOnce(&DeviceMonitorLinux::BlockingTaskRunnerHelper::Initialize,
  102. base::Unretained(blocking_task_helper_.get())));
  103. }
  104. DeviceMonitorLinux::~DeviceMonitorLinux() = default;
  105. } // namespace media