audio_processor_handler.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2022 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_AUDIO_PROCESSOR_HANDLER_H_
  5. #define SERVICES_AUDIO_AUDIO_PROCESSOR_HANDLER_H_
  6. #include <atomic>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/sequence_checker.h"
  9. #include "media/base/audio_processing.h"
  10. #include "media/mojo/mojom/audio_processing.mojom.h"
  11. #include "media/webrtc/audio_processor.h"
  12. #include "mojo/public/cpp/bindings/receiver.h"
  13. #include "services/audio/aecdump_recording_manager.h"
  14. #include "services/audio/reference_output.h"
  15. namespace media {
  16. class AudioBus;
  17. class AudioParameters;
  18. } // namespace media
  19. namespace audio {
  20. // Encapsulates audio processing effects in the audio process, using a
  21. // media::AudioProcessor. Forwards capture audio, playout audio, and
  22. // control calls to the processor.
  23. //
  24. // The class can be operated on by three different sequences:
  25. // - An owning sequence, which performs construction, destruction, getting
  26. // stats, and similar control flow.
  27. // - A capture thread, which calls ProcessCapturedAudio().
  28. // - A playout thread, which calls OnPlayoutData().
  29. //
  30. // All functions should be called on the owning thread, unless otherwise
  31. // specified. It is the responsibility of the owner to ensure that the playout
  32. // thread and capture thread stop calling into the AudioProcessorHandler before
  33. // destruction.
  34. class AudioProcessorHandler final : public ReferenceOutput::Listener,
  35. public media::mojom::AudioProcessorControls,
  36. public AecdumpRecordingSource {
  37. public:
  38. using DeliverProcessedAudioCallback =
  39. media::AudioProcessor::DeliverProcessedAudioCallback;
  40. using LogCallback = base::RepeatingCallback<void(base::StringPiece)>;
  41. // |settings| specifies which audio processing effects to apply. Some effect
  42. // must be required, i.e. the AudioProcessorHandler may only be created if
  43. // |settings.NeedAudioModification()| is true.
  44. // |input_format| and |output_format| specify formats before and after
  45. // processing, where |*_format|.frames_per_buffer() must be 10 ms if
  46. // |settings|.NeedWebrtcAudioProcessing().
  47. // |log_callback| is used for logging messages on the owning sequence.
  48. // |deliver_processed_audio_callback| is used to deliver processed audio
  49. // provided to ProcessCapturedAudio().
  50. // |controls_receiver| calls are received by the AudioProcessorHandler.
  51. // |aecdump_recording_manager| is used to register and deregister an aecdump
  52. // recording source, and must outlive the AudioProcessorHandler if not null.
  53. AudioProcessorHandler(
  54. const media::AudioProcessingSettings& settings,
  55. const media::AudioParameters& input_format,
  56. const media::AudioParameters& output_format,
  57. LogCallback log_callback,
  58. DeliverProcessedAudioCallback deliver_processed_audio_callback,
  59. mojo::PendingReceiver<media::mojom::AudioProcessorControls>
  60. controls_receiver,
  61. AecdumpRecordingManager* aecdump_recording_manager);
  62. AudioProcessorHandler(const AudioProcessorHandler&) = delete;
  63. AudioProcessorHandler& operator=(const AudioProcessorHandler&) = delete;
  64. ~AudioProcessorHandler() final;
  65. // Processes and delivers capture audio.
  66. // See media::AudioProcessor::ProcessCapturedAudio for API details.
  67. // Called on the capture thread.
  68. void ProcessCapturedAudio(const media::AudioBus& audio_source,
  69. base::TimeTicks audio_capture_time,
  70. double volume,
  71. bool key_pressed);
  72. // The format of audio input to the processor; constant throughout its
  73. // lifetime.
  74. const media::AudioParameters& input_format() const {
  75. return audio_processor_->input_format();
  76. }
  77. private:
  78. // Used in the mojom::AudioProcessorControls implementation.
  79. using GetStatsCallback =
  80. base::OnceCallback<void(const media::AudioProcessingStats& stats)>;
  81. // ReferenceOutput::Listener implementation.
  82. // Called on the playout thread.
  83. void OnPlayoutData(const media::AudioBus& audio_bus,
  84. int sample_rate,
  85. base::TimeDelta delay) final;
  86. // mojom::AudioProcessorControls implementation.
  87. void GetStats(GetStatsCallback callback) final;
  88. void SetPreferredNumCaptureChannels(int32_t num_preferred_channels) final;
  89. // AecdumpRecordingSource implementation.
  90. void StartAecdump(base::File aecdump_file) final;
  91. void StopAecdump() final;
  92. SEQUENCE_CHECKER(owning_sequence_);
  93. // The audio processor is accessed on all threads (OS capture thread, OS
  94. // playout thread, owning sequence) and created / destroyed on the owning
  95. // sequence.
  96. const std::unique_ptr<media::AudioProcessor> audio_processor_;
  97. mojo::Receiver<media::mojom::AudioProcessorControls> receiver_
  98. GUARDED_BY_CONTEXT(owning_sequence_);
  99. // Used to deregister as an aecdump recording source upon destruction.
  100. const raw_ptr<AecdumpRecordingManager> aecdump_recording_manager_
  101. GUARDED_BY_CONTEXT(owning_sequence_);
  102. // The number of channels preferred by consumers of the captured audio.
  103. // Initially, consumers are assumed to use mono audio in order to 1) avoid
  104. // unnecessary computational load and 2) preserve the historical default.
  105. // Written from the owning thread in SetPreferredNumCaptureChannels and read
  106. // from the real-time capture thread in ProcessCapturedAudio.
  107. // We use an atomic instead of a lock in order to avoid blocking on the
  108. // real-time thread.
  109. std::atomic<int32_t> num_preferred_channels_ = 1;
  110. };
  111. } // namespace audio
  112. #endif // SERVICES_AUDIO_AUDIO_PROCESSOR_HANDLER_H_