sync_mixing_graph_input.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_SYNC_MIXING_GRAPH_INPUT_H_
  5. #define SERVICES_AUDIO_SYNC_MIXING_GRAPH_INPUT_H_
  6. #include <atomic>
  7. #include <memory>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "media/audio/audio_io.h"
  11. #include "media/base/audio_converter.h"
  12. #include "media/base/audio_parameters.h"
  13. #include "services/audio/mixing_graph.h"
  14. namespace media {
  15. class AudioPullFifo;
  16. }
  17. namespace audio {
  18. // Input to the mixing graph.
  19. class SyncMixingGraphInput final : public MixingGraph::Input {
  20. public:
  21. const double kDefaultVolume = 1.0;
  22. SyncMixingGraphInput(MixingGraph* graph,
  23. const media::AudioParameters& params);
  24. ~SyncMixingGraphInput() final;
  25. // media::AudioConverter::InputCallback.
  26. double ProvideInput(media::AudioBus* audio_bus,
  27. uint32_t frames_delayed) final;
  28. const media::AudioParameters& GetParams() const final;
  29. // Sets a new volume to be applied to the input during mixing.
  30. void SetVolume(double volume) final;
  31. // Adds the input to the mixing graph. The mixing graph will repeatedly pull
  32. // data from |callback|.
  33. void Start(media::AudioOutputStream::AudioSourceCallback* callback) final;
  34. // Removes the input from the mixing graph. The mixing graph will stop pulling
  35. // data from the input.
  36. void Stop() final;
  37. private:
  38. void Render(int fifo_frame_delay, media::AudioBus* audio_bus);
  39. // Pointer to the mixing graph to which the input belongs.
  40. const raw_ptr<MixingGraph> graph_;
  41. // Channel layout, sample rate and number of frames of the input.
  42. const media::AudioParameters params_;
  43. // Volume of the input.
  44. std::atomic<double> volume_{kDefaultVolume};
  45. // Callback providing audio to the mixing graph when requested.
  46. raw_ptr<media::AudioOutputStream::AudioSourceCallback> source_callback_ =
  47. nullptr;
  48. // Handles buffering when there is a mismatch in number of frames between the
  49. // input and the output of the mixing graph. Created on-demand.
  50. std::unique_ptr<media::AudioPullFifo> fifo_;
  51. // Used for calculating the playback delay.
  52. int converter_render_frame_delay_ = 0;
  53. SEQUENCE_CHECKER(owning_sequence_);
  54. };
  55. } // namespace audio
  56. #endif // SERVICES_AUDIO_SYNC_MIXING_GRAPH_INPUT_H_