async_audio_processing.h (4598B)
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 "api/ref_count.h" 18 #include "api/task_queue/task_queue_base.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 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 Factory(std::unique_ptr<AudioFrameProcessor> frame_processor, 42 TaskQueueFactory& task_queue_factory); 43 44 std::unique_ptr<AsyncAudioProcessing> CreateAsyncAudioProcessing( 45 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback); 46 47 private: 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. 56 AudioFrameProcessor& frame_processor_; 57 std::unique_ptr<AudioFrameProcessor> owned_frame_processor_; 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 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`. 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. 74 AsyncAudioProcessing( 75 AudioFrameProcessor& frame_processor, 76 TaskQueueFactory& task_queue_factory, 77 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback); 78 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 89 // Accepts `frame` for asynchronous processing. Thread-safe. 90 void Process(std::unique_ptr<AudioFrame> frame); 91 92 private: 93 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback_; 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. 102 AudioFrameProcessor& frame_processor_; 103 std::unique_ptr<AudioFrameProcessor> owned_frame_processor_; 104 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue_; 105 }; 106 107 } // namespace webrtc 108 109 #endif // MODULES_ASYNC_AUDIO_PROCESSING_ASYNC_AUDIO_PROCESSING_H_