xref: /aosp_15_r20/external/webrtc/modules/async_audio_processing/async_audio_processing.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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;
Factory(AudioFrameProcessor & frame_processor,TaskQueueFactory & task_queue_factory)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 
27 std::unique_ptr<AsyncAudioProcessing>
CreateAsyncAudioProcessing(AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback)28 AsyncAudioProcessing::Factory::CreateAsyncAudioProcessing(
29     AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback) {
30   return std::make_unique<AsyncAudioProcessing>(
31       frame_processor_, task_queue_factory_,
32       std::move(on_frame_processed_callback));
33 }
34 
~AsyncAudioProcessing()35 AsyncAudioProcessing::~AsyncAudioProcessing() {
36   frame_processor_.SetSink(nullptr);
37 }
38 
AsyncAudioProcessing(AudioFrameProcessor & frame_processor,TaskQueueFactory & task_queue_factory,AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback)39 AsyncAudioProcessing::AsyncAudioProcessing(
40     AudioFrameProcessor& frame_processor,
41     TaskQueueFactory& task_queue_factory,
42     AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback)
43     : on_frame_processed_callback_(std::move(on_frame_processed_callback)),
44       frame_processor_(frame_processor),
45       task_queue_(task_queue_factory.CreateTaskQueue(
46           "AsyncAudioProcessing",
47           TaskQueueFactory::Priority::NORMAL)) {
48   frame_processor_.SetSink([this](std::unique_ptr<AudioFrame> frame) {
49     task_queue_.PostTask([this, frame = std::move(frame)]() mutable {
50       on_frame_processed_callback_(std::move(frame));
51     });
52   });
53 }
54 
Process(std::unique_ptr<AudioFrame> frame)55 void AsyncAudioProcessing::Process(std::unique_ptr<AudioFrame> frame) {
56   task_queue_.PostTask([this, frame = std::move(frame)]() mutable {
57     frame_processor_.Process(std::move(frame));
58   });
59 }
60 
61 }  // namespace webrtc
62