blob: d61b1264fc8fcf763730f047883849c8229d9bbb [file] [log] [blame]
Olga Sharonova09ceed22020-09-30 16:27:391
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
19namespace webrtc {
20
21AsyncAudioProcessing::Factory::~Factory() = default;
22AsyncAudioProcessing::Factory::Factory(AudioFrameProcessor& frame_processor,
23 TaskQueueFactory& task_queue_factory)
24 : frame_processor_(frame_processor),
25 task_queue_factory_(task_queue_factory) {}
26
Peter Hanspersa9bba042023-05-30 11:43:4127AsyncAudioProcessing::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 Sharonova09ceed22020-09-30 16:27:3934std::unique_ptr<AsyncAudioProcessing>
35AsyncAudioProcessing::Factory::CreateAsyncAudioProcessing(
36 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback) {
Peter Hanspersa9bba042023-05-30 11:43:4137 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 Sharonova09ceed22020-09-30 16:27:3946}
47
48AsyncAudioProcessing::~AsyncAudioProcessing() {
Peter Hanspersa9bba042023-05-30 11:43:4149 if (owned_frame_processor_) {
50 owned_frame_processor_->SetSink(nullptr);
51 } else {
52 frame_processor_.SetSink(nullptr);
53 }
Olga Sharonova09ceed22020-09-30 16:27:3954}
55
56AsyncAudioProcessing::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 Chapovalov3f7566a2024-01-19 12:12:5066 task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
Olga Sharonova09ceed22020-09-30 16:27:3967 on_frame_processed_callback_(std::move(frame));
68 });
69 });
70}
71
Peter Hanspersa9bba042023-05-30 11:43:4172AsyncAudioProcessing::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 Chapovalov3f7566a2024-01-19 12:12:5083 task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
Peter Hanspersa9bba042023-05-30 11:43:4184 on_frame_processed_callback_(std::move(frame));
85 });
Olga Sharonova09ceed22020-09-30 16:27:3986 });
87}
88
Peter Hanspersa9bba042023-05-30 11:43:4189void AsyncAudioProcessing::Process(std::unique_ptr<AudioFrame> frame) {
90 if (owned_frame_processor_) {
Danil Chapovalov3f7566a2024-01-19 12:12:5091 task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
Peter Hanspersa9bba042023-05-30 11:43:4192 owned_frame_processor_->Process(std::move(frame));
93 });
94 } else {
Danil Chapovalov3f7566a2024-01-19 12:12:5095 task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
Peter Hanspersa9bba042023-05-30 11:43:4196 frame_processor_.Process(std::move(frame));
97 });
98 }
99}
100
Olga Sharonova09ceed22020-09-30 16:27:39101} // namespace webrtc