mixing_graph.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_MIXING_GRAPH_H_
  5. #define SERVICES_AUDIO_MIXING_GRAPH_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/time/time.h"
  9. #include "media/audio/audio_io.h"
  10. #include "media/base/audio_bus.h"
  11. #include "media/base/audio_converter.h"
  12. #include "media/base/audio_parameters.h"
  13. namespace audio {
  14. // The graph is mixing audio provided by multiple audio sources
  15. // (AudioOutputStream::AudioSourceCallback instances) and represents the mix as
  16. // a single AudioOutputStream::AudioSourceCallback, which in turn serves as an
  17. // audio source providing audio buffers to AudioOutputStream for playback.
  18. class MixingGraph : public media::AudioOutputStream::AudioSourceCallback {
  19. public:
  20. using InputCallback = media::AudioConverter::InputCallback;
  21. using OnMoreDataCallback =
  22. base::RepeatingCallback<void(const media::AudioBus&, base::TimeDelta)>;
  23. using OnErrorCallback = base::RepeatingCallback<void(
  24. media::AudioOutputStream::AudioSourceCallback::ErrorType)>;
  25. // A helper class for the clients to pass MixingGraph::Create() around as a
  26. // callback.
  27. using CreateCallback = base::OnceCallback<std::unique_ptr<MixingGraph>(
  28. const media::AudioParameters& output_params,
  29. OnMoreDataCallback on_more_data_cb,
  30. OnErrorCallback on_error_cb)>;
  31. // Represents an audio source as an input to MixingGraph.
  32. // An adapter from AudioOutputStream::AudioSourceCallback to
  33. // AudioConverter::InputCallback.
  34. class Input : public InputCallback {
  35. public:
  36. Input() = default;
  37. ~Input() override = default;
  38. Input(const Input&) = delete;
  39. Input& operator=(const Input&) = delete;
  40. // Returns the audio source parameters.
  41. virtual const media::AudioParameters& GetParams() const = 0;
  42. // Sets the volume.
  43. virtual void SetVolume(double volume) = 0;
  44. // Starts providing audio from |source_callback| to the graph which created
  45. // the given Input.
  46. virtual void Start(
  47. media::AudioOutputStream::AudioSourceCallback* source_callback) = 0;
  48. // Stops providing audio to the graph which created the given Input.
  49. virtual void Stop() = 0;
  50. };
  51. // Creates a graph which will provide the audio mix formatted as
  52. // |output_params| each time its
  53. // AudioOutputStream::AudioSourceCallback::OnMoreData() method is called, and
  54. // will also provide the audio mix to |on_more_data_cb| callback.
  55. // |on_error_cb| will be used to notify the client about audio rendering
  56. // errors.
  57. static std::unique_ptr<MixingGraph> Create(
  58. const media::AudioParameters& output_params,
  59. OnMoreDataCallback on_more_data_cb,
  60. OnErrorCallback on_error_cb);
  61. MixingGraph() = default;
  62. MixingGraph(const MixingGraph&) = delete;
  63. MixingGraph& operator=(const MixingGraph&) = delete;
  64. // Creates a graph input with given audio source parameters.
  65. virtual std::unique_ptr<Input> CreateInput(
  66. const media::AudioParameters& params) = 0;
  67. protected:
  68. friend class SyncMixingGraphInput;
  69. // Adds an input to the graph. To be called by Input::Start().
  70. virtual void AddInput(Input* node) = 0;
  71. // Removes an input from the graph. To be called by Input::Stop().
  72. virtual void RemoveInput(Input* node) = 0;
  73. };
  74. } // namespace audio
  75. #endif // SERVICES_AUDIO_MIXING_GRAPH_H_