processing_audio_fifo.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 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_PROCESSING_AUDIO_FIFO_H_
  5. #define SERVICES_AUDIO_PROCESSING_AUDIO_FIFO_H_
  6. #include "base/callback_forward.h"
  7. #include "base/sequence_checker.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/synchronization/atomic_flag.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/synchronization/waitable_event.h"
  12. #include "base/thread_annotations.h"
  13. #include "media/base/audio_parameters.h"
  14. #include "services/audio/realtime_audio_thread.h"
  15. namespace media {
  16. class AudioBus;
  17. }
  18. namespace audio {
  19. // ProcessingAudioFifo is a ring buffer, which offloads audio processing to its
  20. // own dedicated real-time thread. It interacts with 3 sequences:
  21. // - An owning sequence, on which it is constructed, started, stopped and
  22. // destroyed.
  23. // - A capture sequence which calls PushData().
  24. // - A processing thread, which ProcessingAudioFifo owns, on which a
  25. // processing callback is called.
  26. class ProcessingAudioFifo {
  27. public:
  28. using ProcessAudioCallback = base::RepeatingCallback<
  29. void(const media::AudioBus&, base::TimeTicks, double, bool)>;
  30. using LogCallback = base::RepeatingCallback<void(base::StringPiece)>;
  31. // |processing_callback| will only be called back on the processing thread.
  32. ProcessingAudioFifo(const media::AudioParameters& input_params,
  33. int fifo_size,
  34. ProcessAudioCallback processing_callback,
  35. LogCallback log_callback);
  36. // Note: This synchronously waits for |audio_processing_thread_.Stop()|.
  37. ~ProcessingAudioFifo();
  38. // Disallow copy and assign.
  39. ProcessingAudioFifo(const ProcessingAudioFifo&) = delete;
  40. ProcessingAudioFifo& operator=(const ProcessingAudioFifo&) = delete;
  41. // Adds data to the FIFO, waking up the processing thread in the process.
  42. // If the FIFO is full, new data will be dropped.
  43. // Called on the capture thread.
  44. void PushData(const media::AudioBus* audio_bus,
  45. base::TimeTicks capture_time,
  46. double volume,
  47. bool key_pressed);
  48. // Starts the processing thread. Cannot be called more than once.
  49. void Start();
  50. // |fake_new_data_captured| will replace |new_data_captured_| in the
  51. // ProcessAudioLoop().
  52. void StartForTesting(base::WaitableEvent* fake_new_data_captured);
  53. // Adds a callback that will be run immediately after |processing_callback_|,
  54. // on the same sequences as |processing_callback_|.
  55. void AttachOnProcessedCallbackForTesting(
  56. base::RepeatingClosure on_processed_callback);
  57. private:
  58. friend class ProcessingAudioFifoTest;
  59. class StatsReporter;
  60. struct CaptureData;
  61. void StartInternal(base::WaitableEvent* new_data_captured);
  62. void StopProcessingLoop();
  63. void ProcessAudioLoop(base::WaitableEvent* new_data_captured);
  64. CaptureData* GetDataAtIndex(int idx);
  65. base::Lock fifo_index_lock_;
  66. // Total number of segments written to |fifo_|.
  67. // Updated on capture thread. Read on capture and processing thread.
  68. int write_count_ GUARDED_BY(fifo_index_lock_) = 0;
  69. // Total number of segments read from the |fifo_|, and processed by
  70. // |processing_callback_|.
  71. // Updated on processing thread. Read on capture and processing thread.
  72. int read_count_ GUARDED_BY(fifo_index_lock_) = 0;
  73. // Pre-allocated circular buffer of captured audio data, used to handoff data
  74. // from the capture thread to the processing thread.
  75. const int fifo_size_;
  76. std::vector<CaptureData> fifo_;
  77. // Expected format of captured audio data.
  78. const media::AudioParameters input_params_;
  79. // Real-time audio processing thread, on which |processing_callback_| is
  80. // called.
  81. RealtimeAudioThread audio_processing_thread_;
  82. // Processes captured audio data. Only run on |audio_processing_thread_|.
  83. ProcessAudioCallback processing_callback_;
  84. base::AtomicFlag fifo_stopping_;
  85. // Event signaling that there is new audio data to process. Called from the
  86. // capture thread, and waited for on the processing thread.
  87. base::WaitableEvent new_data_captured_;
  88. std::unique_ptr<StatsReporter> stats_reporter_;
  89. SEQUENCE_CHECKER(owning_sequence_checker_);
  90. };
  91. } // namespace audio
  92. #endif // SERVICES_AUDIO_PROCESSING_AUDIO_FIFO_H_