output_device_mixer.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_OUTPUT_DEVICE_MIXER_H_
  5. #define SERVICES_AUDIO_OUTPUT_DEVICE_MIXER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "media/base/audio_parameters.h"
  11. #include "services/audio/reference_output.h"
  12. namespace media {
  13. class AudioOutputStream;
  14. }
  15. namespace audio {
  16. // Manages mixing and rendering of all audio outputs going to a specific output
  17. // device. As ReferenceOutput provides to Listeners an ability to listen to the
  18. // audio mix being rendered. If there is at least one listener connected, the
  19. // mixer is mixing all the audio output and rendering the mix as a single audio
  20. // output stream. Otherwise the streams are rendered independently.
  21. class OutputDeviceMixer : public ReferenceOutput {
  22. public:
  23. // Callback to be used by OutputDeviceMixer to create actual output streams
  24. // playing audio.
  25. using CreateStreamCallback =
  26. base::RepeatingCallback<media::AudioOutputStream*(
  27. const std::string& device_id,
  28. const media::AudioParameters& params)>;
  29. // A helper class for the clients to pass OutputDeviceMixer::Create around as
  30. // a callback.
  31. using CreateCallback =
  32. base::RepeatingCallback<std::unique_ptr<OutputDeviceMixer>(
  33. const std::string& device_id,
  34. const media::AudioParameters& output_params,
  35. CreateStreamCallback create_stream_callback,
  36. scoped_refptr<base::SingleThreadTaskRunner> task_runner)>;
  37. // Creates OutputDeviceMixer which manages playback to the device identified
  38. // by |device_id|. |output_params| - parameters for the audio mix playback;
  39. // |create_stream_callback| will be used by OutputDeviceMixer to create output
  40. // streams playing audio; |task_runner| is the main task runner of
  41. // OutputDeviceMixer.
  42. static std::unique_ptr<OutputDeviceMixer> Create(
  43. const std::string& device_id,
  44. const media::AudioParameters& output_params,
  45. CreateStreamCallback create_stream_callback,
  46. scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  47. // |device_id| is the id of the output device to manage the playback to.
  48. explicit OutputDeviceMixer(const std::string& device_id);
  49. OutputDeviceMixer(const OutputDeviceMixer&) = delete;
  50. OutputDeviceMixer& operator=(const OutputDeviceMixer&) = delete;
  51. // Id of the device audio output to which is managed by OutputDeviceMixer.
  52. const std::string& device_id() const { return device_id_; }
  53. // Creates an audio output stream managed by the given OutputDeviceMixer.
  54. // |params| - output stream parameters; |on_device_change_callback| - callback
  55. // to notify the AudioOutputStream client about device change events observed
  56. // by OutputDeviceMixer.
  57. virtual media::AudioOutputStream* MakeMixableStream(
  58. const media::AudioParameters& params,
  59. base::OnceCallback<void()> on_device_change_callback) = 0;
  60. // Notify OutputDeviceMixer about the device change event.
  61. virtual void ProcessDeviceChange() = 0;
  62. private:
  63. // Id of the device output to which is managed by the mixer.
  64. const std::string device_id_;
  65. };
  66. } // namespace audio
  67. #endif // SERVICES_AUDIO_OUTPUT_DEVICE_MIXER_H_