OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #ifndef WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ | 10 #ifndef WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ |
11 #define WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ | 11 #define WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ |
12 | 12 |
13 #include <memory> | 13 #include <memory> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
| 17 #include "webrtc/base/array_view.h" |
| 18 #include "webrtc/base/buffer.h" |
17 #include "webrtc/base/criticalsection.h" | 19 #include "webrtc/base/criticalsection.h" |
| 20 #include "webrtc/base/event.h" |
18 #include "webrtc/base/platform_thread.h" | 21 #include "webrtc/base/platform_thread.h" |
19 #include "webrtc/modules/audio_device/include/fake_audio_device.h" | 22 #include "webrtc/modules/audio_device/include/fake_audio_device.h" |
20 #include "webrtc/typedefs.h" | 23 #include "webrtc/typedefs.h" |
21 | 24 |
22 namespace webrtc { | 25 namespace webrtc { |
23 | 26 |
24 class EventTimerWrapper; | 27 class EventTimerWrapper; |
25 | 28 |
26 namespace test { | 29 namespace test { |
27 | 30 |
28 // FakeAudioDevice implements an AudioDevice module that can act both as a | 31 // FakeAudioDevice implements an AudioDevice module that can act both as a |
29 // capturer and a renderer. It will use 10ms audio frames. | 32 // capturer and a renderer. It will use 10ms audio frames. |
30 class FakeAudioDevice : public FakeAudioDeviceModule { | 33 class FakeAudioDevice : public FakeAudioDeviceModule { |
31 public: | 34 public: |
| 35 // 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 |
32 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio | 62 // Creates a new FakeAudioDevice. When capturing or playing, 10 ms audio |
33 // frames will be processed every 100ms / |speed|. | 63 // frames will be processed every 10ms / |speed|. |
34 // |sampling_frequency_in_hz| can be 8, 16, 32, 44.1 or 48kHz. | 64 // |capturer| is an object that produces audio data. Can be nullptr if this |
35 // When recording is started, it will generates a signal where every second | 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); |
| 72 ~FakeAudioDevice() override; |
| 73 |
| 74 // Returns a Capturer instance that generates a signal where every second |
36 // frame is zero and every second frame is evenly distributed random noise | 75 // frame is zero and every second frame is evenly distributed random noise |
37 // with max amplitude |max_amplitude|. | 76 // with max amplitude |max_amplitude|. |
38 FakeAudioDevice(float speed, | 77 static std::unique_ptr<Capturer> CreatePulsedNoiseCapturer( |
39 int sampling_frequency_in_hz, | 78 int16_t max_amplitude, int sampling_frequency_in_hz); |
40 int16_t max_amplitude); | |
41 ~FakeAudioDevice() override; | |
42 | 79 |
43 private: | 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 |
| 92 // Returns a Renderer instance that does nothing with the audio data. |
| 93 static std::unique_ptr<Renderer> CreateDiscardRenderer( |
| 94 int sampling_frequency_in_hz); |
| 95 |
44 int32_t Init() override; | 96 int32_t Init() override; |
45 int32_t RegisterAudioCallback(AudioTransport* callback) override; | 97 int32_t RegisterAudioCallback(AudioTransport* callback) override; |
46 | 98 |
47 int32_t StartPlayout() override; | 99 int32_t StartPlayout() override; |
48 int32_t StopPlayout() override; | 100 int32_t StopPlayout() override; |
49 int32_t StartRecording() override; | 101 int32_t StartRecording() override; |
50 int32_t StopRecording() override; | 102 int32_t StopRecording() override; |
51 | 103 |
52 bool Playing() const override; | 104 bool Playing() const override; |
53 bool Recording() const override; | 105 bool Recording() const override; |
54 | 106 |
| 107 // Blocks until the Renderer refuses to receive data. |
| 108 // Returns false if |timeout_ms| passes before that happens. |
| 109 bool WaitForPlayoutEnd(int timeout_ms = rtc::Event::kForever); |
| 110 // Blocks until the Recorder stops producing data. |
| 111 // Returns false if |timeout_ms| passes before that happens. |
| 112 bool WaitForRecordingEnd(int timeout_ms = rtc::Event::kForever); |
| 113 |
| 114 private: |
55 static bool Run(void* obj); | 115 static bool Run(void* obj); |
56 void ProcessAudio(); | 116 void ProcessAudio(); |
57 | 117 |
58 const int sampling_frequency_in_hz_; | 118 const std::unique_ptr<Capturer> capturer_ GUARDED_BY(lock_); |
59 const size_t num_samples_per_frame_; | 119 const std::unique_ptr<Renderer> renderer_ GUARDED_BY(lock_); |
60 const float speed_; | 120 const float speed_; |
61 | 121 |
62 rtc::CriticalSection lock_; | 122 rtc::CriticalSection lock_; |
63 AudioTransport* audio_callback_ GUARDED_BY(lock_); | 123 AudioTransport* audio_callback_ GUARDED_BY(lock_); |
64 bool rendering_ GUARDED_BY(lock_); | 124 bool rendering_ GUARDED_BY(lock_); |
65 bool capturing_ GUARDED_BY(lock_); | 125 bool capturing_ GUARDED_BY(lock_); |
66 | 126 rtc::Event done_rendering_; |
67 class PulsedNoiseCapturer; | 127 rtc::Event done_capturing_; |
68 const std::unique_ptr<PulsedNoiseCapturer> capturer_ GUARDED_BY(lock_); | |
69 | 128 |
70 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_); | 129 std::vector<int16_t> playout_buffer_ GUARDED_BY(lock_); |
| 130 rtc::BufferT<int16_t> recording_buffer_ GUARDED_BY(lock_); |
71 | 131 |
72 std::unique_ptr<EventTimerWrapper> tick_; | 132 std::unique_ptr<EventTimerWrapper> tick_; |
73 rtc::PlatformThread thread_; | 133 rtc::PlatformThread thread_; |
74 }; | 134 }; |
75 } // namespace test | 135 } // namespace test |
76 } // namespace webrtc | 136 } // namespace webrtc |
77 | 137 |
78 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ | 138 #endif // WEBRTC_TEST_FAKE_AUDIO_DEVICE_H_ |
OLD | NEW |