blob: 2e78e716a83f77aec4bc5bbd16446df58e5ffa47 [file] [log] [blame]
Olga Sharonova09ceed22020-09-30 16:27:391/*
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 Chapovalov3f7566a2024-01-19 12:12:5017#include "api/task_queue/task_queue_base.h"
Olga Sharonova09ceed22020-09-30 16:27:3918#include "rtc_base/ref_count.h"
Olga Sharonova09ceed22020-09-30 16:27:3919
20namespace webrtc {
21
22class AudioFrame;
23class 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.
29class 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 Hanspersa9bba042023-05-30 11:43:4141 Factory(std::unique_ptr<AudioFrameProcessor> frame_processor,
42 TaskQueueFactory& task_queue_factory);
Olga Sharonova09ceed22020-09-30 16:27:3943
44 std::unique_ptr<AsyncAudioProcessing> CreateAsyncAudioProcessing(
45 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback);
46
47 private:
Peter Hanspersa9bba042023-05-30 11:43:4148 // 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 Sharonova09ceed22020-09-30 16:27:3956 AudioFrameProcessor& frame_processor_;
Peter Hanspersa9bba042023-05-30 11:43:4157 std::unique_ptr<AudioFrameProcessor> owned_frame_processor_;
Olga Sharonova09ceed22020-09-30 16:27:3958 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 Titovbc88b542021-07-28 18:23:4067 // `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 Hanspersa9bba042023-05-30 11:43:4171 // 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 Sharonova09ceed22020-09-30 16:27:3974 AsyncAudioProcessing(
75 AudioFrameProcessor& frame_processor,
76 TaskQueueFactory& task_queue_factory,
77 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback);
78
Peter Hanspersa9bba042023-05-30 11:43:4179 // 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 Titovbc88b542021-07-28 18:23:4089 // Accepts `frame` for asynchronous processing. Thread-safe.
Olga Sharonova09ceed22020-09-30 16:27:3990 void Process(std::unique_ptr<AudioFrame> frame);
91
92 private:
93 AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback_;
Peter Hanspersa9bba042023-05-30 11:43:4194 // 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 Sharonova09ceed22020-09-30 16:27:39102 AudioFrameProcessor& frame_processor_;
Peter Hanspersa9bba042023-05-30 11:43:41103 std::unique_ptr<AudioFrameProcessor> owned_frame_processor_;
Danil Chapovalov3f7566a2024-01-19 12:12:50104 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue_;
Olga Sharonova09ceed22020-09-30 16:27:39105};
106
107} // namespace webrtc
108
109#endif // MODULES_ASYNC_AUDIO_PROCESSING_ASYNC_AUDIO_PROCESSING_H_