Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license |
| 6 | * that can be found in the LICENSE file in the root of the source |
| 7 | * tree. An additional intellectual property rights grant can be found |
| 8 | * in the file PATENTS. All contributing project authors may |
| 9 | * be found in the AUTHORS file in the root of the source tree. |
| 10 | */ |
| 11 | #include "modules/async_audio_processing/async_audio_processing.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
| 15 | #include "api/audio/audio_frame.h" |
| 16 | #include "api/task_queue/task_queue_factory.h" |
| 17 | #include "rtc_base/checks.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | AsyncAudioProcessing::Factory::~Factory() = default; |
| 22 | AsyncAudioProcessing::Factory::Factory(AudioFrameProcessor& frame_processor, |
| 23 | TaskQueueFactory& task_queue_factory) |
| 24 | : frame_processor_(frame_processor), |
| 25 | task_queue_factory_(task_queue_factory) {} |
| 26 | |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 27 | AsyncAudioProcessing::Factory::Factory( |
| 28 | std::unique_ptr<AudioFrameProcessor> frame_processor, |
| 29 | TaskQueueFactory& task_queue_factory) |
| 30 | : frame_processor_(*frame_processor), |
| 31 | owned_frame_processor_(std::move(frame_processor)), |
| 32 | task_queue_factory_(task_queue_factory) {} |
| 33 | |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 34 | std::unique_ptr<AsyncAudioProcessing> |
| 35 | AsyncAudioProcessing::Factory::CreateAsyncAudioProcessing( |
| 36 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback) { |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 37 | if (owned_frame_processor_) { |
| 38 | return std::make_unique<AsyncAudioProcessing>( |
| 39 | std::move(owned_frame_processor_), task_queue_factory_, |
| 40 | std::move(on_frame_processed_callback)); |
| 41 | } else { |
| 42 | return std::make_unique<AsyncAudioProcessing>( |
| 43 | frame_processor_, task_queue_factory_, |
| 44 | std::move(on_frame_processed_callback)); |
| 45 | } |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | AsyncAudioProcessing::~AsyncAudioProcessing() { |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 49 | if (owned_frame_processor_) { |
| 50 | owned_frame_processor_->SetSink(nullptr); |
| 51 | } else { |
| 52 | frame_processor_.SetSink(nullptr); |
| 53 | } |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | AsyncAudioProcessing::AsyncAudioProcessing( |
| 57 | AudioFrameProcessor& frame_processor, |
| 58 | TaskQueueFactory& task_queue_factory, |
| 59 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback) |
| 60 | : on_frame_processed_callback_(std::move(on_frame_processed_callback)), |
| 61 | frame_processor_(frame_processor), |
| 62 | task_queue_(task_queue_factory.CreateTaskQueue( |
| 63 | "AsyncAudioProcessing", |
| 64 | TaskQueueFactory::Priority::NORMAL)) { |
| 65 | frame_processor_.SetSink([this](std::unique_ptr<AudioFrame> frame) { |
Danil Chapovalov | 3f7566a | 2024-01-19 12:12:50 | [diff] [blame] | 66 | task_queue_->PostTask([this, frame = std::move(frame)]() mutable { |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 67 | on_frame_processed_callback_(std::move(frame)); |
| 68 | }); |
| 69 | }); |
| 70 | } |
| 71 | |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 72 | AsyncAudioProcessing::AsyncAudioProcessing( |
| 73 | std::unique_ptr<AudioFrameProcessor> frame_processor, |
| 74 | TaskQueueFactory& task_queue_factory, |
| 75 | AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback) |
| 76 | : on_frame_processed_callback_(std::move(on_frame_processed_callback)), |
| 77 | frame_processor_(*frame_processor), |
| 78 | owned_frame_processor_(std::move(frame_processor)), |
| 79 | task_queue_(task_queue_factory.CreateTaskQueue( |
| 80 | "AsyncAudioProcessing", |
| 81 | TaskQueueFactory::Priority::NORMAL)) { |
| 82 | owned_frame_processor_->SetSink([this](std::unique_ptr<AudioFrame> frame) { |
Danil Chapovalov | 3f7566a | 2024-01-19 12:12:50 | [diff] [blame] | 83 | task_queue_->PostTask([this, frame = std::move(frame)]() mutable { |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 84 | on_frame_processed_callback_(std::move(frame)); |
| 85 | }); |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 86 | }); |
| 87 | } |
| 88 | |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 89 | void AsyncAudioProcessing::Process(std::unique_ptr<AudioFrame> frame) { |
| 90 | if (owned_frame_processor_) { |
Danil Chapovalov | 3f7566a | 2024-01-19 12:12:50 | [diff] [blame] | 91 | task_queue_->PostTask([this, frame = std::move(frame)]() mutable { |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 92 | owned_frame_processor_->Process(std::move(frame)); |
| 93 | }); |
| 94 | } else { |
Danil Chapovalov | 3f7566a | 2024-01-19 12:12:50 | [diff] [blame] | 95 | task_queue_->PostTask([this, frame = std::move(frame)]() mutable { |
Peter Hanspers | a9bba04 | 2023-05-30 11:43:41 | [diff] [blame] | 96 | frame_processor_.Process(std::move(frame)); |
| 97 | }); |
| 98 | } |
| 99 | } |
| 100 | |
Olga Sharonova | 09ceed2 | 2020-09-30 16:27:39 | [diff] [blame] | 101 | } // namespace webrtc |