device_listener_output_stream.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2021 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_DEVICE_LISTENER_OUTPUT_STREAM_H_
  5. #define SERVICES_AUDIO_DEVICE_LISTENER_OUTPUT_STREAM_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "media/audio/audio_io.h"
  9. #include "media/audio/audio_manager.h"
  10. namespace audio {
  11. // Simple wrapper class, which forwards all AudioOutputStream calls to the
  12. // wrapped |stream_|. It also listens for device change events (via
  13. // the AudioDeviceListener interface, or via errors reporting device changes),
  14. // and forwards them via the |on_device_change_callback_|, on the owning
  15. // (AudioManager) thread. |on_device_change_callback_| must synchronously close
  16. // the DeviceListenerOutputStream.
  17. class DeviceListenerOutputStream final
  18. : public media::AudioOutputStream,
  19. public media::AudioOutputStream::AudioSourceCallback,
  20. public media::AudioManager::AudioDeviceListener {
  21. public:
  22. // Note: |on_device_change_callback| must synchronously call close(), which
  23. // will delete |this|.
  24. DeviceListenerOutputStream(media::AudioManager* audio_manager,
  25. media::AudioOutputStream* wrapped_stream,
  26. base::OnceClosure on_device_change_callback);
  27. DeviceListenerOutputStream(const DeviceListenerOutputStream&) = delete;
  28. DeviceListenerOutputStream& operator=(const DeviceListenerOutputStream&) =
  29. delete;
  30. // AudioOutputStream implementation
  31. bool Open() final;
  32. void Start(
  33. media::AudioOutputStream::AudioSourceCallback* source_callback) final;
  34. void Stop() final;
  35. void SetVolume(double volume) final;
  36. void GetVolume(double* volume) final;
  37. void Close() final;
  38. void Flush() final;
  39. private:
  40. ~DeviceListenerOutputStream() final;
  41. // AudioManager::AudioDeviceListener implementation.
  42. void OnDeviceChange() final;
  43. // AudioOutputStream::AudioSourceCallback implementation.
  44. int OnMoreData(base::TimeDelta delay,
  45. base::TimeTicks delay_timestamp,
  46. int prior_frames_skipped,
  47. media::AudioBus* dest) final;
  48. int OnMoreData(base::TimeDelta delay,
  49. base::TimeTicks delay_timestamp,
  50. int prior_frames_skipped,
  51. media::AudioBus* dest,
  52. bool is_mixing) final;
  53. void OnError(ErrorType type) final;
  54. void ReportError(ErrorType type);
  55. const raw_ptr<media::AudioManager> audio_manager_;
  56. raw_ptr<media::AudioOutputStream> stream_;
  57. // Callback to process the device change.
  58. base::OnceClosure on_device_change_callback_;
  59. // Actual producer of the audio.
  60. raw_ptr<media::AudioOutputStream::AudioSourceCallback> source_callback_ =
  61. nullptr;
  62. // The task runner for the audio manager. The main task runner for the object.
  63. const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  64. // For posting cancelable tasks.
  65. base::WeakPtrFactory<DeviceListenerOutputStream> weak_factory_{this};
  66. };
  67. } // namespace audio
  68. #endif // SERVICES_AUDIO_DEVICE_LISTENER_OUTPUT_STREAM_H_