service.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 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. #ifndef SERVICES_AUDIO_SERVICE_H_
  5. #define SERVICES_AUDIO_SERVICE_H_
  6. #include <memory>
  7. #include "base/threading/thread_checker.h"
  8. #include "build/build_config.h"
  9. #include "media/mojo/mojom/audio_stream_factory.mojom.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "services/audio/public/mojom/audio_service.mojom.h"
  12. #include "services/audio/public/mojom/debug_recording.mojom.h"
  13. #include "services/audio/public/mojom/device_notifications.mojom.h"
  14. #include "services/audio/public/mojom/log_factory_manager.mojom.h"
  15. #include "services/audio/public/mojom/system_info.mojom.h"
  16. #include "services/audio/public/mojom/testing_api.mojom.h"
  17. #include "services/audio/stream_factory.h"
  18. #include "services/audio/testing_api_binder.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace base {
  21. class DeferredSequencedTaskRunner;
  22. class SystemMonitor;
  23. } // namespace base
  24. namespace media {
  25. class AudioDeviceListenerMac;
  26. class AudioManager;
  27. class AudioLogFactory;
  28. } // namespace media
  29. namespace audio {
  30. class AecdumpRecordingManager;
  31. class DebugRecording;
  32. class DeviceNotifier;
  33. class LogFactoryManager;
  34. class ServiceMetrics;
  35. class SystemInfo;
  36. class Service final : public mojom::AudioService {
  37. public:
  38. // Abstracts AudioManager ownership. Lives and must be accessed on a thread
  39. // its created on, and that thread must be AudioManager main thread.
  40. class AudioManagerAccessor {
  41. public:
  42. virtual ~AudioManagerAccessor() {}
  43. // Must be called before destruction to cleanly shut down AudioManager.
  44. // Service must ensure AudioManager is not called after that.
  45. virtual void Shutdown() = 0;
  46. // Returns a pointer to AudioManager.
  47. virtual media::AudioManager* GetAudioManager() = 0;
  48. // Attempts to associate |factory| with the audio manager.
  49. // |factory| must outlive the audio manager.
  50. // It only makes sense to call this method before GetAudioManager().
  51. virtual void SetAudioLogFactory(media::AudioLogFactory* factory) = 0;
  52. };
  53. // If |enable_remote_client_support| is true, the service will make available
  54. // a DeviceNotifier object that allows clients to/ subscribe to notifications
  55. // about device changes and a LogFactoryManager object that allows clients to
  56. // set a factory for audio logs.
  57. Service(std::unique_ptr<AudioManagerAccessor> audio_manager_accessor,
  58. bool enable_remote_client_support,
  59. mojo::PendingReceiver<mojom::AudioService> receiver);
  60. Service(const Service&) = delete;
  61. Service& operator=(const Service&) = delete;
  62. ~Service() final;
  63. // Returns a DeferredSequencedTaskRunner to be used to run the audio service
  64. // when launched in the browser process.
  65. static base::DeferredSequencedTaskRunner* GetInProcessTaskRunner();
  66. // Allows tests to override how SystemInfo interface receivers are bound.
  67. // Used by FakeSystemInfo.
  68. static void SetSystemInfoBinderForTesting(SystemInfoBinder binder);
  69. // Allows tests to inject support for TestingApi binding, which is normally
  70. // unsupported by the service.
  71. static void SetTestingApiBinderForTesting(TestingApiBinder binder);
  72. private:
  73. // mojom::AudioService implementation:
  74. void BindSystemInfo(
  75. mojo::PendingReceiver<mojom::SystemInfo> receiver) override;
  76. void BindDebugRecording(
  77. mojo::PendingReceiver<mojom::DebugRecording> receiver) override;
  78. void BindStreamFactory(mojo::PendingReceiver<media::mojom::AudioStreamFactory>
  79. receiver) override;
  80. void BindDeviceNotifier(
  81. mojo::PendingReceiver<mojom::DeviceNotifier> receiver) override;
  82. void BindLogFactoryManager(
  83. mojo::PendingReceiver<mojom::LogFactoryManager> receiver) override;
  84. void BindTestingApi(
  85. mojo::PendingReceiver<mojom::TestingApi> receiver) override;
  86. // Initializes a platform-specific device monitor for device-change
  87. // notifications. If the client uses the DeviceNotifier interface to get
  88. // notifications this function should be called before the DeviceMonitor is
  89. // created. If the client uses base::SystemMonitor to get notifications,
  90. // this function should be called on service startup.
  91. void InitializeDeviceMonitor();
  92. // The thread Service runs on should be the same as the main thread of
  93. // AudioManager provided by AudioManagerAccessor.
  94. THREAD_CHECKER(thread_checker_);
  95. base::RepeatingClosure quit_closure_;
  96. mojo::Receiver<mojom::AudioService> receiver_;
  97. std::unique_ptr<AudioManagerAccessor> audio_manager_accessor_;
  98. const bool enable_remote_client_support_;
  99. std::unique_ptr<base::SystemMonitor> system_monitor_;
  100. #if BUILDFLAG(IS_MAC)
  101. std::unique_ptr<media::AudioDeviceListenerMac> audio_device_listener_mac_;
  102. #endif
  103. std::unique_ptr<SystemInfo> system_info_;
  104. // Manages starting / stopping of diagnostic audio processing recordings. Must
  105. // outlive |debug_recording_| and |stream_factory_|, if instantiated.
  106. std::unique_ptr<AecdumpRecordingManager> aecdump_recording_manager_;
  107. std::unique_ptr<DebugRecording> debug_recording_;
  108. absl::optional<StreamFactory> stream_factory_;
  109. std::unique_ptr<DeviceNotifier> device_notifier_;
  110. std::unique_ptr<LogFactoryManager> log_factory_manager_;
  111. std::unique_ptr<ServiceMetrics> metrics_;
  112. };
  113. } // namespace audio
  114. #endif // SERVICES_AUDIO_SERVICE_H_