OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2012 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 // This class implements an AudioCaptureModule that can be used to detect if | |
12 // audio is being received properly if it is fed by another AudioCaptureModule | |
13 // in some arbitrary audio pipeline where they are connected. It does not play | |
14 // out or record any audio so it does not need access to any hardware and can | |
15 // therefore be used in the gtest testing framework. | |
16 | |
17 // Note P postfix of a function indicates that it should only be called by the | |
18 // processing thread. | |
19 | |
20 #ifndef WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_ | |
21 #define WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_ | |
22 | |
23 #include <memory> | |
24 | |
25 #include "webrtc/base/basictypes.h" | |
26 #include "webrtc/base/criticalsection.h" | |
27 #include "webrtc/base/messagehandler.h" | |
28 #include "webrtc/base/scoped_ref_ptr.h" | |
29 #include "webrtc/common_types.h" | |
30 #include "webrtc/modules/audio_device/include/audio_device.h" | |
31 | |
32 namespace rtc { | |
33 class Thread; | |
34 } // namespace rtc | |
35 | |
36 class FakeAudioCaptureModule | |
37 : public webrtc::AudioDeviceModule, | |
38 public rtc::MessageHandler { | |
39 public: | |
40 typedef uint16_t Sample; | |
41 | |
42 // The value for the following constants have been derived by running VoE | |
43 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz. | |
44 static const size_t kNumberSamples = 440; | |
45 static const size_t kNumberBytesPerSample = sizeof(Sample); | |
46 | |
47 // Creates a FakeAudioCaptureModule or returns NULL on failure. | |
48 static rtc::scoped_refptr<FakeAudioCaptureModule> Create(); | |
49 | |
50 // Returns the number of frames that have been successfully pulled by the | |
51 // instance. Note that correctly detecting success can only be done if the | |
52 // pulled frame was generated/pushed from a FakeAudioCaptureModule. | |
53 int frames_received() const; | |
54 | |
55 // Following functions are inherited from webrtc::AudioDeviceModule. | |
56 // Only functions called by PeerConnection are implemented, the rest do | |
57 // nothing and return success. If a function is not expected to be called by | |
58 // PeerConnection an assertion is triggered if it is in fact called. | |
59 int64_t TimeUntilNextProcess() override; | |
60 void Process() override; | |
61 | |
62 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override; | |
63 | |
64 ErrorCode LastError() const override; | |
65 int32_t RegisterEventObserver( | |
66 webrtc::AudioDeviceObserver* event_callback) override; | |
67 | |
68 // Note: Calling this method from a callback may result in deadlock. | |
69 int32_t RegisterAudioCallback( | |
70 webrtc::AudioTransport* audio_callback) override; | |
71 | |
72 int32_t Init() override; | |
73 int32_t Terminate() override; | |
74 bool Initialized() const override; | |
75 | |
76 int16_t PlayoutDevices() override; | |
77 int16_t RecordingDevices() override; | |
78 int32_t PlayoutDeviceName(uint16_t index, | |
79 char name[webrtc::kAdmMaxDeviceNameSize], | |
80 char guid[webrtc::kAdmMaxGuidSize]) override; | |
81 int32_t RecordingDeviceName(uint16_t index, | |
82 char name[webrtc::kAdmMaxDeviceNameSize], | |
83 char guid[webrtc::kAdmMaxGuidSize]) override; | |
84 | |
85 int32_t SetPlayoutDevice(uint16_t index) override; | |
86 int32_t SetPlayoutDevice(WindowsDeviceType device) override; | |
87 int32_t SetRecordingDevice(uint16_t index) override; | |
88 int32_t SetRecordingDevice(WindowsDeviceType device) override; | |
89 | |
90 int32_t PlayoutIsAvailable(bool* available) override; | |
91 int32_t InitPlayout() override; | |
92 bool PlayoutIsInitialized() const override; | |
93 int32_t RecordingIsAvailable(bool* available) override; | |
94 int32_t InitRecording() override; | |
95 bool RecordingIsInitialized() const override; | |
96 | |
97 int32_t StartPlayout() override; | |
98 int32_t StopPlayout() override; | |
99 bool Playing() const override; | |
100 int32_t StartRecording() override; | |
101 int32_t StopRecording() override; | |
102 bool Recording() const override; | |
103 | |
104 int32_t SetAGC(bool enable) override; | |
105 bool AGC() const override; | |
106 | |
107 int32_t SetWaveOutVolume(uint16_t volume_left, | |
108 uint16_t volume_right) override; | |
109 int32_t WaveOutVolume(uint16_t* volume_left, | |
110 uint16_t* volume_right) const override; | |
111 | |
112 int32_t InitSpeaker() override; | |
113 bool SpeakerIsInitialized() const override; | |
114 int32_t InitMicrophone() override; | |
115 bool MicrophoneIsInitialized() const override; | |
116 | |
117 int32_t SpeakerVolumeIsAvailable(bool* available) override; | |
118 int32_t SetSpeakerVolume(uint32_t volume) override; | |
119 int32_t SpeakerVolume(uint32_t* volume) const override; | |
120 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override; | |
121 int32_t MinSpeakerVolume(uint32_t* min_volume) const override; | |
122 int32_t SpeakerVolumeStepSize(uint16_t* step_size) const override; | |
123 | |
124 int32_t MicrophoneVolumeIsAvailable(bool* available) override; | |
125 int32_t SetMicrophoneVolume(uint32_t volume) override; | |
126 int32_t MicrophoneVolume(uint32_t* volume) const override; | |
127 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override; | |
128 | |
129 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override; | |
130 int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const override; | |
131 | |
132 int32_t SpeakerMuteIsAvailable(bool* available) override; | |
133 int32_t SetSpeakerMute(bool enable) override; | |
134 int32_t SpeakerMute(bool* enabled) const override; | |
135 | |
136 int32_t MicrophoneMuteIsAvailable(bool* available) override; | |
137 int32_t SetMicrophoneMute(bool enable) override; | |
138 int32_t MicrophoneMute(bool* enabled) const override; | |
139 | |
140 int32_t MicrophoneBoostIsAvailable(bool* available) override; | |
141 int32_t SetMicrophoneBoost(bool enable) override; | |
142 int32_t MicrophoneBoost(bool* enabled) const override; | |
143 | |
144 int32_t StereoPlayoutIsAvailable(bool* available) const override; | |
145 int32_t SetStereoPlayout(bool enable) override; | |
146 int32_t StereoPlayout(bool* enabled) const override; | |
147 int32_t StereoRecordingIsAvailable(bool* available) const override; | |
148 int32_t SetStereoRecording(bool enable) override; | |
149 int32_t StereoRecording(bool* enabled) const override; | |
150 int32_t SetRecordingChannel(const ChannelType channel) override; | |
151 int32_t RecordingChannel(ChannelType* channel) const override; | |
152 | |
153 int32_t SetPlayoutBuffer(const BufferType type, | |
154 uint16_t size_ms = 0) override; | |
155 int32_t PlayoutBuffer(BufferType* type, uint16_t* size_ms) const override; | |
156 int32_t PlayoutDelay(uint16_t* delay_ms) const override; | |
157 int32_t RecordingDelay(uint16_t* delay_ms) const override; | |
158 | |
159 int32_t CPULoad(uint16_t* load) const override; | |
160 | |
161 int32_t StartRawOutputFileRecording( | |
162 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override; | |
163 int32_t StopRawOutputFileRecording() override; | |
164 int32_t StartRawInputFileRecording( | |
165 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override; | |
166 int32_t StopRawInputFileRecording() override; | |
167 | |
168 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override; | |
169 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override; | |
170 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override; | |
171 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override; | |
172 | |
173 int32_t ResetAudioDevice() override; | |
174 int32_t SetLoudspeakerStatus(bool enable) override; | |
175 int32_t GetLoudspeakerStatus(bool* enabled) const override; | |
176 bool BuiltInAECIsAvailable() const override { return false; } | |
177 int32_t EnableBuiltInAEC(bool enable) override { return -1; } | |
178 bool BuiltInAGCIsAvailable() const override { return false; } | |
179 int32_t EnableBuiltInAGC(bool enable) override { return -1; } | |
180 bool BuiltInNSIsAvailable() const override { return false; } | |
181 int32_t EnableBuiltInNS(bool enable) override { return -1; } | |
182 #if defined(WEBRTC_IOS) | |
183 int GetPlayoutAudioParameters( | |
184 webrtc::AudioParameters* params) const override { | |
185 return -1; | |
186 } | |
187 int GetRecordAudioParameters(webrtc::AudioParameters* params) const override { | |
188 return -1; | |
189 } | |
190 #endif // WEBRTC_IOS | |
191 | |
192 // End of functions inherited from webrtc::AudioDeviceModule. | |
193 | |
194 // The following function is inherited from rtc::MessageHandler. | |
195 void OnMessage(rtc::Message* msg) override; | |
196 | |
197 protected: | |
198 // The constructor is protected because the class needs to be created as a | |
199 // reference counted object (for memory managment reasons). It could be | |
200 // exposed in which case the burden of proper instantiation would be put on | |
201 // the creator of a FakeAudioCaptureModule instance. To create an instance of | |
202 // this class use the Create(..) API. | |
203 explicit FakeAudioCaptureModule(); | |
204 // The destructor is protected because it is reference counted and should not | |
205 // be deleted directly. | |
206 virtual ~FakeAudioCaptureModule(); | |
207 | |
208 private: | |
209 // Initializes the state of the FakeAudioCaptureModule. This API is called on | |
210 // creation by the Create() API. | |
211 bool Initialize(); | |
212 // SetBuffer() sets all samples in send_buffer_ to |value|. | |
213 void SetSendBuffer(int value); | |
214 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0. | |
215 void ResetRecBuffer(); | |
216 // Returns true if rec_buffer_ contains one or more sample greater than or | |
217 // equal to |value|. | |
218 bool CheckRecBuffer(int value); | |
219 | |
220 // Returns true/false depending on if recording or playback has been | |
221 // enabled/started. | |
222 bool ShouldStartProcessing(); | |
223 | |
224 // Starts or stops the pushing and pulling of audio frames. | |
225 void UpdateProcessing(bool start); | |
226 | |
227 // Starts the periodic calling of ProcessFrame() in a thread safe way. | |
228 void StartProcessP(); | |
229 // Periodcally called function that ensures that frames are pulled and pushed | |
230 // periodically if enabled/started. | |
231 void ProcessFrameP(); | |
232 // Pulls frames from the registered webrtc::AudioTransport. | |
233 void ReceiveFrameP(); | |
234 // Pushes frames to the registered webrtc::AudioTransport. | |
235 void SendFrameP(); | |
236 | |
237 // The time in milliseconds when Process() was last called or 0 if no call | |
238 // has been made. | |
239 int64_t last_process_time_ms_; | |
240 | |
241 // Callback for playout and recording. | |
242 webrtc::AudioTransport* audio_callback_; | |
243 | |
244 bool recording_; // True when audio is being pushed from the instance. | |
245 bool playing_; // True when audio is being pulled by the instance. | |
246 | |
247 bool play_is_initialized_; // True when the instance is ready to pull audio. | |
248 bool rec_is_initialized_; // True when the instance is ready to push audio. | |
249 | |
250 // Input to and output from RecordedDataIsAvailable(..) makes it possible to | |
251 // modify the current mic level. The implementation does not care about the | |
252 // mic level so it just feeds back what it receives. | |
253 uint32_t current_mic_level_; | |
254 | |
255 // next_frame_time_ is updated in a non-drifting manner to indicate the next | |
256 // wall clock time the next frame should be generated and received. started_ | |
257 // ensures that next_frame_time_ can be initialized properly on first call. | |
258 bool started_; | |
259 int64_t next_frame_time_; | |
260 | |
261 std::unique_ptr<rtc::Thread> process_thread_; | |
262 | |
263 // Buffer for storing samples received from the webrtc::AudioTransport. | |
264 char rec_buffer_[kNumberSamples * kNumberBytesPerSample]; | |
265 // Buffer for samples to send to the webrtc::AudioTransport. | |
266 char send_buffer_[kNumberSamples * kNumberBytesPerSample]; | |
267 | |
268 // Counter of frames received that have samples of high enough amplitude to | |
269 // indicate that the frames are not faked somewhere in the audio pipeline | |
270 // (e.g. by a jitter buffer). | |
271 int frames_received_; | |
272 | |
273 // Protects variables that are accessed from process_thread_ and | |
274 // the main thread. | |
275 rtc::CriticalSection crit_; | |
276 // Protects |audio_callback_| that is accessed from process_thread_ and | |
277 // the main thread. | |
278 rtc::CriticalSection crit_callback_; | |
279 }; | |
280 | |
281 #endif // WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_ | |
OLD | NEW |