xref: /aosp_15_r20/external/webrtc/modules/async_audio_processing/async_audio_processing.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MODULES_ASYNC_AUDIO_PROCESSING_ASYNC_AUDIO_PROCESSING_H_
12 #define MODULES_ASYNC_AUDIO_PROCESSING_ASYNC_AUDIO_PROCESSING_H_
13 
14 #include <memory>
15 
16 #include "api/audio/audio_frame_processor.h"
17 #include "rtc_base/ref_count.h"
18 #include "rtc_base/task_queue.h"
19 
20 namespace webrtc {
21 
22 class AudioFrame;
23 class TaskQueueFactory;
24 
25 // Helper class taking care of interactions with AudioFrameProcessor
26 // in asynchronous manner. Offloads AudioFrameProcessor::Process calls
27 // to a dedicated task queue. Makes sure that it's always safe for
28 // AudioFrameProcessor to pass processed frames back to its sink.
29 class AsyncAudioProcessing final {
30  public:
31   // Helper class passing AudioFrameProcessor and TaskQueueFactory into
32   // AsyncAudioProcessing constructor.
33   class Factory : public rtc::RefCountInterface {
34    public:
35     Factory(const Factory&) = delete;
36     Factory& operator=(const Factory&) = delete;
37 
38     ~Factory();
39     Factory(AudioFrameProcessor& frame_processor,
40             TaskQueueFactory& task_queue_factory);
41 
42     std::unique_ptr<AsyncAudioProcessing> CreateAsyncAudioProcessing(
43         AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback);
44 
45    private:
46     AudioFrameProcessor& frame_processor_;
47     TaskQueueFactory& task_queue_factory_;
48   };
49 
50   AsyncAudioProcessing(const AsyncAudioProcessing&) = delete;
51   AsyncAudioProcessing& operator=(const AsyncAudioProcessing&) = delete;
52 
53   ~AsyncAudioProcessing();
54 
55   // Creates AsyncAudioProcessing which will pass audio frames to
56   // `frame_processor` on `task_queue_` and reply with processed frames passed
57   // into `on_frame_processed_callback`, which is posted back onto
58   // `task_queue_`. `task_queue_` is created using the provided
59   // `task_queue_factory`.
60   AsyncAudioProcessing(
61       AudioFrameProcessor& frame_processor,
62       TaskQueueFactory& task_queue_factory,
63       AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback);
64 
65   // Accepts `frame` for asynchronous processing. Thread-safe.
66   void Process(std::unique_ptr<AudioFrame> frame);
67 
68  private:
69   AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback_;
70   AudioFrameProcessor& frame_processor_;
71   rtc::TaskQueue task_queue_;
72 };
73 
74 }  // namespace webrtc
75 
76 #endif  // MODULES_ASYNC_AUDIO_PROCESSING_ASYNC_AUDIO_PROCESSING_H_
77