Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 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" |
Danil Chapovalov | 3f7566a | 2024-01-19 12:12:50 | [diff] [blame] | 17 | #include "api/task_queue/task_queue_base.h" |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 18 | #include "rtc_base/ref_count.h" |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 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); |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 41 | Factory(std::unique_ptr<AudioFrameProcessor> frame_processor, |
| 42 | TaskQueueFactory& task_queue_factory); |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 43 | |
| 44 | std::unique_ptr<AsyncAudioProcessing> CreateAsyncAudioProcessing( |
| 45 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback); |
| 46 | |
| 47 | private: |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 48 | // TODO(bugs.webrtc.org/15111): |
| 49 | // Remove 'AudioFrameProcessor& frame_processor_' in favour of |
| 50 | // std::unique_ptr in the follow-up. |
| 51 | // While transitioning this API from using AudioFrameProcessor& to using |
| 52 | // std::unique_ptr<AudioFrameProcessor>, we have two member variable both |
| 53 | // referencing the same object. Throughout the lifetime of the Factory |
| 54 | // only one of the variables is used, depending on which constructor was |
| 55 | // called. |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 56 | AudioFrameProcessor& frame_processor_; |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 57 | std::unique_ptr<AudioFrameProcessor> owned_frame_processor_; |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 58 | TaskQueueFactory& task_queue_factory_; |
| 59 | }; |
| 60 | |
| 61 | AsyncAudioProcessing(const AsyncAudioProcessing&) = delete; |
| 62 | AsyncAudioProcessing& operator=(const AsyncAudioProcessing&) = delete; |
| 63 | |
| 64 | ~AsyncAudioProcessing(); |
| 65 | |
| 66 | // Creates AsyncAudioProcessing which will pass audio frames to |
Artem Titov | bc88b54 | 2021-07-28 18:23:40 | [diff] [blame] | 67 | // `frame_processor` on `task_queue_` and reply with processed frames passed |
| 68 | // into `on_frame_processed_callback`, which is posted back onto |
| 69 | // `task_queue_`. `task_queue_` is created using the provided |
| 70 | // `task_queue_factory`. |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 71 | // TODO(bugs.webrtc.org/15111): |
| 72 | // Remove this method in favour of the method taking the |
| 73 | // unique_ptr<AudioFrameProcessor> in the follow-up. |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 74 | AsyncAudioProcessing( |
| 75 | AudioFrameProcessor& frame_processor, |
| 76 | TaskQueueFactory& task_queue_factory, |
| 77 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback); |
| 78 | |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 79 | // Creates AsyncAudioProcessing which will pass audio frames to |
| 80 | // `frame_processor` on `task_queue_` and reply with processed frames passed |
| 81 | // into `on_frame_processed_callback`, which is posted back onto |
| 82 | // `task_queue_`. `task_queue_` is created using the provided |
| 83 | // `task_queue_factory`. |
| 84 | AsyncAudioProcessing( |
| 85 | std::unique_ptr<AudioFrameProcessor> frame_processor, |
| 86 | TaskQueueFactory& task_queue_factory, |
| 87 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback); |
| 88 | |
Artem Titov | bc88b54 | 2021-07-28 18:23:40 | [diff] [blame] | 89 | // Accepts `frame` for asynchronous processing. Thread-safe. |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 90 | void Process(std::unique_ptr<AudioFrame> frame); |
| 91 | |
| 92 | private: |
| 93 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback_; |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 94 | // TODO(bugs.webrtc.org/15111): |
| 95 | // Remove 'AudioFrameProcessor& frame_processor_' in favour of |
| 96 | // std::unique_ptr in the follow-up. |
| 97 | // While transitioning this API from using AudioFrameProcessor& to using |
| 98 | // std::unique_ptr<AudioFrameProcessor>, we have two member variable both |
| 99 | // referencing the same object. Throughout the lifetime of the Factory |
| 100 | // only one of the variables is used, depending on which constructor was |
| 101 | // called. |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 102 | AudioFrameProcessor& frame_processor_; |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 103 | std::unique_ptr<AudioFrameProcessor> owned_frame_processor_; |
Danil Chapovalov | 3f7566a | 2024-01-19 12:12:50 | [diff] [blame] | 104 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue_; |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace webrtc |
| 108 | |
| 109 | #endif // MODULES_ASYNC_AUDIO_PROCESSING_ASYNC_AUDIO_PROCESSING_H_ |