blob: db56c745e04b4d4617af7ae594f32d12f27ef35a [file] [log] [blame]
stefan@webrtc.orge0284102013-11-18 11:45:111/*
2 * Copyright (c) 2013 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 WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
11#define WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_
12
kwibergb670f852016-05-01 21:53:4613#include <memory>
stefan@webrtc.orge0284102013-11-18 11:45:1114#include <string>
perkj4c8e9ef2017-01-31 21:32:4915#include <vector>
stefan@webrtc.orge0284102013-11-18 11:45:1116
kwiberg23aa43d2017-09-04 12:43:1717#include "webrtc/api/array_view.h"
stefan@webrtc.orge0284102013-11-18 11:45:1118#include "webrtc/modules/audio_device/include/fake_audio_device.h"
Edward Lemur76de83e2017-07-06 17:44:3419#include "webrtc/rtc_base/buffer.h"
20#include "webrtc/rtc_base/criticalsection.h"
21#include "webrtc/rtc_base/event.h"
22#include "webrtc/rtc_base/platform_thread.h"
stefan@webrtc.orge0284102013-11-18 11:45:1123#include "webrtc/typedefs.h"
24
25namespace webrtc {
26
Peter Boströma8c4f432015-04-08 09:24:1927class EventTimerWrapper;
stefan@webrtc.orge0284102013-11-18 11:45:1128
29namespace test {
30
perkj4c8e9ef2017-01-31 21:32:4931// FakeAudioDevice implements an AudioDevice module that can act both as a
32// capturer and a renderer. It will use 10ms audio frames.
stefan@webrtc.orge0284102013-11-18 11:45:1133class FakeAudioDevice : public FakeAudioDeviceModule {
34 public:
oprypin35a36cd2017-03-14 16:01:4735 // Returns the number of samples that Capturers and Renderers with this
36 // sampling frequency will work with every time Capture or Render is called.
37 static size_t SamplesPerFrame(int sampling_frequency_in_hz);
38
39 class Capturer {
40 public:
41 virtual ~Capturer() {}
42 // Returns the sampling frequency in Hz of the audio data that this
43 // capturer produces.
44 virtual int SamplingFrequency() const = 0;
45 // Replaces the contents of |buffer| with 10ms of captured audio data
46 // (see FakeAudioDevice::SamplesPerFrame). Returns true if the capturer can
47 // 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:
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 // Renders the passed audio data and returns true if the renderer wants
58 // to keep receiving data, or false otherwise.
59 virtual bool Render(rtc::ArrayView<const int16_t> data) = 0;
60 };
61
perkj4c8e9ef2017-01-31 21:32:4962 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio
oprypin35a36cd2017-03-14 16:01:4763 // frames will be processed every 10ms / |speed|.
64 // |capturer| is an object that produces audio data. Can be nullptr if this
65 // device is never used for recording.
66 // |renderer| is an object that receives audio data that would have been
67 // played out. Can be nullptr if this device is never used for playing.
68 // Use one of the Create... functions to get these instances.
69 FakeAudioDevice(std::unique_ptr<Capturer> capturer,
70 std::unique_ptr<Renderer> renderer,
71 float speed = 1);
perkj4c8e9ef2017-01-31 21:32:4972 ~FakeAudioDevice() override;
stefan@webrtc.orge0284102013-11-18 11:45:1173
oprypin35a36cd2017-03-14 16:01:4774 // Returns a Capturer instance that generates a signal where every second
75 // frame is zero and every second frame is evenly distributed random noise
76 // with max amplitude |max_amplitude|.
77 static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer(
78 int16_t max_amplitude, int sampling_frequency_in_hz);
79
80 // Returns a Capturer instance that gets its data from a file.
81 static std::unique_ptr<Capturer> CreateWavFileReader(
82 std::string filename, int sampling_frequency_in_hz);
83
84 // Returns a Capturer instance that gets its data from a file.
85 // Automatically detects sample rate.
86 static std::unique_ptr<Capturer> CreateWavFileReader(std::string filename);
87
88 // Returns a Renderer instance that writes its data to a file.
89 static std::unique_ptr<Renderer> CreateWavFileWriter(
90 std::string filename, int sampling_frequency_in_hz);
91
oprypin1b0415d2017-03-23 10:40:0392 // Returns a Renderer instance that writes its data to a WAV file, cutting
93 // off silence at the beginning (not necessarily perfect silence, see
94 // kAmplitudeThreshold) and at the end (only actual 0 samples in this case).
95 static std::unique_ptr<Renderer> CreateBoundedWavFileWriter(
96 std::string filename, int sampling_frequency_in_hz);
97
oprypin35a36cd2017-03-14 16:01:4798 // Returns a Renderer instance that does nothing with the audio data.
99 static std::unique_ptr<Renderer> CreateDiscardRenderer(
100 int sampling_frequency_in_hz);
101
kjellander@webrtc.org860ac532015-03-04 12:58:35102 int32_t Init() override;
103 int32_t RegisterAudioCallback(AudioTransport* callback) override;
stefan@webrtc.orge0284102013-11-18 11:45:11104
perkj4c8e9ef2017-01-31 21:32:49105 int32_t StartPlayout() override;
106 int32_t StopPlayout() override;
107 int32_t StartRecording() override;
108 int32_t StopRecording() override;
109
kjellander@webrtc.org860ac532015-03-04 12:58:35110 bool Playing() const override;
kjellander@webrtc.org860ac532015-03-04 12:58:35111 bool Recording() const override;
stefan@webrtc.orge0284102013-11-18 11:45:11112
oprypin35a36cd2017-03-14 16:01:47113 // Blocks until the Renderer refuses to receive data.
114 // Returns false if |timeout_ms| passes before that happens.
115 bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever);
116 // Blocks until the Recorder stops producing data.
117 // Returns false if |timeout_ms| passes before that happens.
118 bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever);
119
120 private:
stefan@webrtc.orge0284102013-11-18 11:45:11121 static bool Run(void* obj);
perkj4c8e9ef2017-01-31 21:32:49122 void ProcessAudio();
stefan@webrtc.orge0284102013-11-18 11:45:11123
oprypin35a36cd2017-03-14 16:01:47124 const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_);
125 const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_);
danilchapa4777ed2016-02-10 18:54:47126 const float speed_;
stefan@webrtc.orge0284102013-11-18 11:45:11127
pbos1e5b8052016-01-25 11:52:44128 rtc::CriticalSection lock_;
perkj4c8e9ef2017-01-31 21:32:49129 AudioTransport* audio_callback_ GUARDED_BY(lock_);
130 bool rendering_ GUARDED_BY(lock_);
131 bool capturing_ GUARDED_BY(lock_);
oprypin35a36cd2017-03-14 16:01:47132 rtc::Event done_rendering_;
133 rtc::Event done_capturing_;
perkj4c8e9ef2017-01-31 21:32:49134
135 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_);
oprypin35a36cd2017-03-14 16:01:47136 rtc::BufferT<int16_t> recording_buffer_ GUARDED_BY(lock_);
perkj4c8e9ef2017-01-31 21:32:49137
138 std::unique_ptr<EventTimerWrapper> tick_;
Peter Boströmdaf1aa42015-11-26 16:45:47139 rtc::PlatformThread thread_;
stefan@webrtc.orge0284102013-11-18 11:45:11140};
141} // namespace test
142} // namespace webrtc
143
144#endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_