xref: /aosp_15_r20/external/webrtc/modules/audio_device/include/test_audio_device.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2018 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 #ifndef MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_
11 #define MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #include <memory>
17 #include <string>
18 
19 #include "absl/strings/string_view.h"
20 #include "api/array_view.h"
21 #include "api/scoped_refptr.h"
22 #include "api/task_queue/task_queue_factory.h"
23 #include "modules/audio_device/include/audio_device.h"
24 #include "modules/audio_device/include/audio_device_defines.h"
25 #include "rtc_base/buffer.h"
26 
27 namespace webrtc {
28 
29 // TestAudioDeviceModule implements an AudioDevice module that can act both as a
30 // capturer and a renderer. It will use 10ms audio frames.
31 class TestAudioDeviceModule : public AudioDeviceModule {
32  public:
33   // Returns the number of samples that Capturers and Renderers with this
34   // sampling frequency will work with every time Capture or Render is called.
35   static size_t SamplesPerFrame(int sampling_frequency_in_hz);
36 
37   class Capturer {
38    public:
~Capturer()39     virtual ~Capturer() {}
40     // Returns the sampling frequency in Hz of the audio data that this
41     // capturer produces.
42     virtual int SamplingFrequency() const = 0;
43     // Returns the number of channels of captured audio data.
44     virtual int NumChannels() const = 0;
45     // Replaces the contents of `buffer` with 10ms of captured audio data
46     // (see TestAudioDeviceModule::SamplesPerFrame). Returns true if the
47     // capturer can keep producing data, or false when the capture finishes.
48     virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0;
49   };
50 
51   class Renderer {
52    public:
~Renderer()53     virtual ~Renderer() {}
54     // Returns the sampling frequency in Hz of the audio data that this
55     // renderer receives.
56     virtual int SamplingFrequency() const = 0;
57     // Returns the number of channels of audio data to be required.
58     virtual int NumChannels() const = 0;
59     // Renders the passed audio data and returns true if the renderer wants
60     // to keep receiving data, or false otherwise.
61     virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
62   };
63 
64   // A fake capturer that generates pulses with random samples between
65   // -max_amplitude and +max_amplitude.
66   class PulsedNoiseCapturer : public Capturer {
67    public:
~PulsedNoiseCapturer()68     ~PulsedNoiseCapturer() override {}
69 
70     virtual void SetMaxAmplitude(int16_t amplitude) = 0;
71   };
72 
~TestAudioDeviceModule()73   ~TestAudioDeviceModule() override {}
74 
75   // Creates a new TestAudioDeviceModule. When capturing or playing, 10 ms audio
76   // frames will be processed every 10ms / `speed`.
77   // `capturer` is an object that produces audio data. Can be nullptr if this
78   // device is never used for recording.
79   // `renderer` is an object that receives audio data that would have been
80   // played out. Can be nullptr if this device is never used for playing.
81   // Use one of the Create... functions to get these instances.
82   static rtc::scoped_refptr<TestAudioDeviceModule> Create(
83       TaskQueueFactory* task_queue_factory,
84       std::unique_ptr<Capturer> capturer,
85       std::unique_ptr<Renderer> renderer,
86       float speed = 1);
87 
88   // Returns a Capturer instance that generates a signal of `num_channels`
89   // channels where every second frame is zero and every second frame is evenly
90   // distributed random noise with max amplitude `max_amplitude`.
91   static std::unique_ptr<PulsedNoiseCapturer> CreatePulsedNoiseCapturer(
92       int16_t max_amplitude,
93       int sampling_frequency_in_hz,
94       int num_channels = 1);
95 
96   // Returns a Renderer instance that does nothing with the audio data.
97   static std::unique_ptr<Renderer> CreateDiscardRenderer(
98       int sampling_frequency_in_hz,
99       int num_channels = 1);
100 
101   // WavReader and WavWriter creation based on file name.
102 
103   // Returns a Capturer instance that gets its data from a file. The sample rate
104   // and channels will be checked against the Wav file.
105   static std::unique_ptr<Capturer> CreateWavFileReader(
106       absl::string_view filename,
107       int sampling_frequency_in_hz,
108       int num_channels = 1);
109 
110   // Returns a Capturer instance that gets its data from a file.
111   // Automatically detects sample rate and num of channels.
112   // `repeat` - if true, the file will be replayed from the start when we reach
113   // the end of file.
114   static std::unique_ptr<Capturer> CreateWavFileReader(
115       absl::string_view filename,
116       bool repeat = false);
117 
118   // Returns a Renderer instance that writes its data to a file.
119   static std::unique_ptr<Renderer> CreateWavFileWriter(
120       absl::string_view filename,
121       int sampling_frequency_in_hz,
122       int num_channels = 1);
123 
124   // Returns a Renderer instance that writes its data to a WAV file, cutting
125   // off silence at the beginning (not necessarily perfect silence, see
126   // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
127   static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
128       absl::string_view filename,
129       int sampling_frequency_in_hz,
130       int num_channels = 1);
131 
132   int32_t Init() override = 0;
133   int32_t RegisterAudioCallback(AudioTransport* callback) override = 0;
134 
135   int32_t StartPlayout() override = 0;
136   int32_t StopPlayout() override = 0;
137   int32_t StartRecording() override = 0;
138   int32_t StopRecording() override = 0;
139 
140   bool Playing() const override = 0;
141   bool Recording() const override = 0;
142 
143   // Blocks forever until the Recorder stops producing data.
144   virtual void WaitForRecordingEnd() = 0;
145 };
146 
147 }  // namespace webrtc
148 
149 #endif  // MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_
150