blob: 93f0b13601fb790f42548b541f3adbd403eaba4b [file] [log] [blame]
Artem Titov0f039732018-03-07 12:20:51 +01001/*
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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
14#include <stdint.h>
Artem Titov0f039732018-03-07 12:20:51 +010015#include <memory>
16#include <string>
Artem Titov0f039732018-03-07 12:20:51 +010017
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "api/array_view.h"
Artem Titov0f039732018-03-07 12:20:51 +010019#include "modules/audio_device/include/audio_device.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "modules/audio_device/include/audio_device_defines.h"
Artem Titov0f039732018-03-07 12:20:51 +010021#include "rtc_base/buffer.h"
22#include "rtc_base/event.h"
Artem Titov8458cff2018-03-22 16:50:06 +010023#include "rtc_base/platform_file.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "rtc_base/scoped_ref_ptr.h"
Artem Titov0f039732018-03-07 12:20:51 +010025
26namespace webrtc {
27
28// TestAudioDeviceModule implements an AudioDevice module that can act both as a
29// capturer and a renderer. It will use 10ms audio frames.
30class TestAudioDeviceModule : public AudioDeviceModule {
31 public:
32 // Returns the number of samples that Capturers and Renderers with this
33 // sampling frequency will work with every time Capture or Render is called.
34 static size_t SamplesPerFrame(int sampling_frequency_in_hz);
35
36 class Capturer {
37 public:
38 virtual ~Capturer() {}
39 // Returns the sampling frequency in Hz of the audio data that this
40 // capturer produces.
41 virtual int SamplingFrequency() const = 0;
Pengyu Liaoba907f02018-03-09 09:25:46 -080042 // Returns the number of channels of captured audio data.
43 virtual int NumChannels() const = 0;
Artem Titov0f039732018-03-07 12:20:51 +010044 // Replaces the contents of |buffer| with 10ms of captured audio data
45 // (see TestAudioDeviceModule::SamplesPerFrame). Returns true if the
46 // capturer can keep producing data, or false when the capture finishes.
47 virtual bool Capture(rtc::BufferT<int16_t>* buffer) = 0;
48 };
49
50 class Renderer {
51 public:
52 virtual ~Renderer() {}
53 // Returns the sampling frequency in Hz of the audio data that this
54 // renderer receives.
55 virtual int SamplingFrequency() const = 0;
Pengyu Liaoba907f02018-03-09 09:25:46 -080056 // Returns the number of channels of audio data to be required.
57 virtual int NumChannels() const = 0;
Artem Titov0f039732018-03-07 12:20:51 +010058 // Renders the passed audio data and returns true if the renderer wants
59 // to keep receiving data, or false otherwise.
60 virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
61 };
62
63 // A fake capturer that generates pulses with random samples between
64 // -max_amplitude and +max_amplitude.
65 class PulsedNoiseCapturer : public Capturer {
66 public:
67 virtual ~PulsedNoiseCapturer() {}
68
69 virtual void SetMaxAmplitude(int16_t amplitude) = 0;
70 };
71
72 virtual ~TestAudioDeviceModule() {}
73
74 // Creates a new TestAudioDeviceModule. When capturing or playing, 10 ms audio
75 // frames will be processed every 10ms / |speed|.
76 // |capturer| is an object that produces audio data. Can be nullptr if this
77 // device is never used for recording.
78 // |renderer| is an object that receives audio data that would have been
79 // played out. Can be nullptr if this device is never used for playing.
80 // Use one of the Create... functions to get these instances.
81 static rtc::scoped_refptr<TestAudioDeviceModule> CreateTestAudioDeviceModule(
82 std::unique_ptr<Capturer> capturer,
83 std::unique_ptr<Renderer> renderer,
84 float speed = 1);
85
Pengyu Liaoba907f02018-03-09 09:25:46 -080086 // Returns a Capturer instance that generates a signal of |num_channels|
87 // channels where every second frame is zero and every second frame is evenly
88 // distributed random noise with max amplitude |max_amplitude|.
89 static std::unique_ptr<PulsedNoiseCapturer> CreatePulsedNoiseCapturer(
90 int16_t max_amplitude,
91 int sampling_frequency_in_hz,
Artem Titov8458cff2018-03-22 16:50:06 +010092 int num_channels = 1);
Pengyu Liaoba907f02018-03-09 09:25:46 -080093
Artem Titov8458cff2018-03-22 16:50:06 +010094 // Returns a Renderer instance that does nothing with the audio data.
95 static std::unique_ptr<Renderer> CreateDiscardRenderer(
96 int sampling_frequency_in_hz,
97 int num_channels = 1);
98
99 // WavReader and WavWriter creation based on file name.
Artem Titov0f039732018-03-07 12:20:51 +0100100
Pengyu Liaoba907f02018-03-09 09:25:46 -0800101 // Returns a Capturer instance that gets its data from a file. The sample rate
Artem Titov8458cff2018-03-22 16:50:06 +0100102 // and channels will be checked against the Wav file.
Pengyu Liaoba907f02018-03-09 09:25:46 -0800103 static std::unique_ptr<Capturer> CreateWavFileReader(
104 std::string filename,
105 int sampling_frequency_in_hz,
Artem Titov8458cff2018-03-22 16:50:06 +0100106 int num_channels = 1);
Artem Titov0f039732018-03-07 12:20:51 +0100107
108 // Returns a Capturer instance that gets its data from a file.
Artem Titov8458cff2018-03-22 16:50:06 +0100109 // Automatically detects sample rate and num of channels.
Artem Titov0f039732018-03-07 12:20:51 +0100110 static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
111
112 // Returns a Renderer instance that writes its data to a file.
113 static std::unique_ptr<Renderer> CreateWavFileWriter(
114 std::string filename,
Pengyu Liaoba907f02018-03-09 09:25:46 -0800115 int sampling_frequency_in_hz,
Artem Titov8458cff2018-03-22 16:50:06 +0100116 int num_channels = 1);
Artem Titov0f039732018-03-07 12:20:51 +0100117
118 // Returns a Renderer instance that writes its data to a WAV file, cutting
119 // off silence at the beginning (not necessarily perfect silence, see
120 // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
121 static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
122 std::string filename,
Pengyu Liaoba907f02018-03-09 09:25:46 -0800123 int sampling_frequency_in_hz,
Artem Titov8458cff2018-03-22 16:50:06 +0100124 int num_channels = 1);
Pengyu Liaoba907f02018-03-09 09:25:46 -0800125
Artem Titov8458cff2018-03-22 16:50:06 +0100126 // WavReader and WavWriter creation based on rtc::PlatformFile.
Artem Titov0f039732018-03-07 12:20:51 +0100127
Artem Titov8458cff2018-03-22 16:50:06 +0100128 // Returns a Capturer instance that gets its data from a file. The sample rate
129 // and channels will be checked against the Wav file.
130 static std::unique_ptr<Capturer> CreateWavFileReader(
131 rtc::PlatformFile file,
Pengyu Liaoba907f02018-03-09 09:25:46 -0800132 int sampling_frequency_in_hz,
Artem Titov8458cff2018-03-22 16:50:06 +0100133 int num_channels = 1);
Pengyu Liaoba907f02018-03-09 09:25:46 -0800134
Artem Titov8458cff2018-03-22 16:50:06 +0100135 // Returns a Capturer instance that gets its data from a file.
136 // Automatically detects sample rate and num of channels.
137 static std::unique_ptr<Capturer> CreateWavFileReader(rtc::PlatformFile file);
138
139 // Returns a Renderer instance that writes its data to a file.
140 static std::unique_ptr<Renderer> CreateWavFileWriter(
141 rtc::PlatformFile file,
142 int sampling_frequency_in_hz,
143 int num_channels = 1);
144
145 // Returns a Renderer instance that writes its data to a WAV file, cutting
146 // off silence at the beginning (not necessarily perfect silence, see
147 // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
148 static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
149 rtc::PlatformFile file,
150 int sampling_frequency_in_hz,
151 int num_channels = 1);
Artem Titov0f039732018-03-07 12:20:51 +0100152
153 virtual int32_t Init() = 0;
154 virtual int32_t RegisterAudioCallback(AudioTransport* callback) = 0;
155
156 virtual int32_t StartPlayout() = 0;
157 virtual int32_t StopPlayout() = 0;
158 virtual int32_t StartRecording() = 0;
159 virtual int32_t StopRecording() = 0;
160
161 virtual bool Playing() const = 0;
162 virtual bool Recording() const = 0;
163
164 // Blocks until the Renderer refuses to receive data.
165 // Returns false if |timeout_ms| passes before that happens.
166 virtual bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever) = 0;
167 // Blocks until the Recorder stops producing data.
168 // Returns false if |timeout_ms| passes before that happens.
169 virtual bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever) = 0;
170};
171
172} // namespace webrtc
173
174#endif // MODULES_AUDIO_DEVICE_INCLUDE_TEST_AUDIO_DEVICE_H_