sync_mixing_graph_input.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "services/audio/sync_mixing_graph_input.h"
  5. #include "base/trace_event/trace_event.h"
  6. #include "media/base/audio_pull_fifo.h"
  7. #include "media/base/audio_timestamp_helper.h"
  8. namespace audio {
  9. SyncMixingGraphInput::SyncMixingGraphInput(MixingGraph* graph,
  10. const media::AudioParameters& params)
  11. : graph_(graph), params_(params) {
  12. DCHECK(graph);
  13. CHECK(params_.IsValid());
  14. }
  15. SyncMixingGraphInput::~SyncMixingGraphInput() {
  16. DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  17. }
  18. // Either calls Render() directly to produce the requested input audio, or - in
  19. // the case of a frames per buffer mismatch - pulls audio from the |fifo_| which
  20. // in turn calls Render() as needed.
  21. double SyncMixingGraphInput::ProvideInput(media::AudioBus* audio_bus,
  22. uint32_t frames_delayed) {
  23. DCHECK_EQ(audio_bus->channels(), params_.channels());
  24. TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("audio"),
  25. "SyncMixingGraphInput::ProvideInput", "frames_delayed",
  26. frames_delayed, "bus frames", audio_bus->frames());
  27. if (!fifo_ && audio_bus->frames() != params_.frames_per_buffer()) {
  28. fifo_ = std::make_unique<media::AudioPullFifo>(
  29. params_.channels(), params_.frames_per_buffer(),
  30. base::BindRepeating(&SyncMixingGraphInput::Render,
  31. base::Unretained(this)));
  32. }
  33. // Used by Render() for delay calculation.
  34. converter_render_frame_delay_ = frames_delayed;
  35. if (fifo_)
  36. fifo_->Consume(audio_bus, audio_bus->frames());
  37. else
  38. Render(0, audio_bus);
  39. converter_render_frame_delay_ = 0;
  40. return volume_;
  41. }
  42. const media::AudioParameters& SyncMixingGraphInput::GetParams() const {
  43. return params_;
  44. }
  45. void SyncMixingGraphInput::SetVolume(double volume) {
  46. DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  47. volume_ = volume;
  48. }
  49. void SyncMixingGraphInput::Start(
  50. media::AudioOutputStream::AudioSourceCallback* source_callback) {
  51. DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  52. DCHECK(!fifo_);
  53. DCHECK(!source_callback_);
  54. source_callback_ = source_callback;
  55. graph_->AddInput(this);
  56. }
  57. void SyncMixingGraphInput::Stop() {
  58. DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
  59. graph_->RemoveInput(this);
  60. if (fifo_)
  61. fifo_.reset();
  62. source_callback_ = nullptr;
  63. }
  64. void SyncMixingGraphInput::Render(int fifo_frame_delay,
  65. media::AudioBus* audio_bus) {
  66. DCHECK(source_callback_);
  67. TRACE_EVENT_BEGIN1(TRACE_DISABLED_BY_DEFAULT("audio"),
  68. "SyncMixingGraphInput::Render", "this",
  69. static_cast<void*>(this));
  70. base::TimeDelta delay = media::AudioTimestampHelper::FramesToTime(
  71. converter_render_frame_delay_ + fifo_frame_delay, params_.sample_rate());
  72. source_callback_->OnMoreData(delay, base::TimeTicks::Now(), 0, audio_bus,
  73. /*is_mixing=*/true);
  74. TRACE_EVENT_END2(TRACE_DISABLED_BY_DEFAULT("audio"),
  75. "SyncMixingGraphInput::Render", "total frames delay",
  76. converter_render_frame_delay_ + fifo_frame_delay,
  77. "bus frames", audio_bus->frames());
  78. }
  79. } // namespace audio