blob: 8ff4aa19e7552eea7a0520bf6f4907ce865a081c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// This class implements an AudioCaptureModule that can be used to detect if
29// audio is being received properly if it is fed by another AudioCaptureModule
30// in some arbitrary audio pipeline where they are connected. It does not play
31// out or record any audio so it does not need access to any hardware and can
32// therefore be used in the gtest testing framework.
33
34// Note P postfix of a function indicates that it should only be called by the
35// processing thread.
36
37#ifndef TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
38#define TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
39
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000040#include "webrtc/base/basictypes.h"
41#include "webrtc/base/criticalsection.h"
42#include "webrtc/base/messagehandler.h"
43#include "webrtc/base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044#include "webrtc/common_types.h"
45#include "webrtc/modules/audio_device/include/audio_device.h"
46
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49class Thread;
50
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051} // namespace rtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53class FakeAudioCaptureModule
54 : public webrtc::AudioDeviceModule,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000055 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 public:
57 typedef uint16 Sample;
58
59 // The value for the following constants have been derived by running VoE
60 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
Peter Kastingf045e4d2015-06-10 21:15:38 -070061 static const int kNumberSamples = 440;
62 static const int kNumberBytesPerSample = sizeof(Sample);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
64 // Creates a FakeAudioCaptureModule or returns NULL on failure.
65 // |process_thread| is used to push and pull audio frames to and from the
66 // returned instance. Note: ownership of |process_thread| is not handed over.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000067 static rtc::scoped_refptr<FakeAudioCaptureModule> Create(
68 rtc::Thread* process_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
70 // Returns the number of frames that have been successfully pulled by the
71 // instance. Note that correctly detecting success can only be done if the
72 // pulled frame was generated/pushed from a FakeAudioCaptureModule.
73 int frames_received() const;
74
75 // Following functions are inherited from webrtc::AudioDeviceModule.
76 // Only functions called by PeerConnection are implemented, the rest do
77 // nothing and return success. If a function is not expected to be called by
78 // PeerConnection an assertion is triggered if it is in fact called.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000079 int64_t TimeUntilNextProcess() override;
80 int32_t Process() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000082 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000084 ErrorCode LastError() const override;
85 int32_t RegisterEventObserver(
86 webrtc::AudioDeviceObserver* event_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
wu@webrtc.org8804a292013-10-22 23:09:20 +000088 // Note: Calling this method from a callback may result in deadlock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 int32_t RegisterAudioCallback(
90 webrtc::AudioTransport* audio_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 int32_t Init() override;
93 int32_t Terminate() override;
94 bool Initialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int16_t PlayoutDevices() override;
97 int16_t RecordingDevices() override;
98 int32_t PlayoutDeviceName(uint16_t index,
99 char name[webrtc::kAdmMaxDeviceNameSize],
100 char guid[webrtc::kAdmMaxGuidSize]) override;
101 int32_t RecordingDeviceName(uint16_t index,
102 char name[webrtc::kAdmMaxDeviceNameSize],
103 char guid[webrtc::kAdmMaxGuidSize]) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 int32_t SetPlayoutDevice(uint16_t index) override;
106 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
107 int32_t SetRecordingDevice(uint16_t index) override;
108 int32_t SetRecordingDevice(WindowsDeviceType device) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 int32_t PlayoutIsAvailable(bool* available) override;
111 int32_t InitPlayout() override;
112 bool PlayoutIsInitialized() const override;
113 int32_t RecordingIsAvailable(bool* available) override;
114 int32_t InitRecording() override;
115 bool RecordingIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000117 int32_t StartPlayout() override;
118 int32_t StopPlayout() override;
119 bool Playing() const override;
120 int32_t StartRecording() override;
121 int32_t StopRecording() override;
122 bool Recording() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 int32_t SetAGC(bool enable) override;
125 bool AGC() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000127 int32_t SetWaveOutVolume(uint16_t volume_left,
128 uint16_t volume_right) override;
129 int32_t WaveOutVolume(uint16_t* volume_left,
130 uint16_t* volume_right) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000132 int32_t InitSpeaker() override;
133 bool SpeakerIsInitialized() const override;
134 int32_t InitMicrophone() override;
135 bool MicrophoneIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 int32_t SpeakerVolumeIsAvailable(bool* available) override;
138 int32_t SetSpeakerVolume(uint32_t volume) override;
139 int32_t SpeakerVolume(uint32_t* volume) const override;
140 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
141 int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
142 int32_t SpeakerVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000144 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
145 int32_t SetMicrophoneVolume(uint32_t volume) override;
146 int32_t MicrophoneVolume(uint32_t* volume) const override;
147 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000149 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
150 int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000152 int32_t SpeakerMuteIsAvailable(bool* available) override;
153 int32_t SetSpeakerMute(bool enable) override;
154 int32_t SpeakerMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000156 int32_t MicrophoneMuteIsAvailable(bool* available) override;
157 int32_t SetMicrophoneMute(bool enable) override;
158 int32_t MicrophoneMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000160 int32_t MicrophoneBoostIsAvailable(bool* available) override;
161 int32_t SetMicrophoneBoost(bool enable) override;
162 int32_t MicrophoneBoost(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000164 int32_t StereoPlayoutIsAvailable(bool* available) const override;
165 int32_t SetStereoPlayout(bool enable) override;
166 int32_t StereoPlayout(bool* enabled) const override;
167 int32_t StereoRecordingIsAvailable(bool* available) const override;
168 int32_t SetStereoRecording(bool enable) override;
169 int32_t StereoRecording(bool* enabled) const override;
170 int32_t SetRecordingChannel(const ChannelType channel) override;
171 int32_t RecordingChannel(ChannelType* channel) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000173 int32_t SetPlayoutBuffer(const BufferType type,
174 uint16_t size_ms = 0) override;
175 int32_t PlayoutBuffer(BufferType* type, uint16_t* size_ms) const override;
176 int32_t PlayoutDelay(uint16_t* delay_ms) const override;
177 int32_t RecordingDelay(uint16_t* delay_ms) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000179 int32_t CPULoad(uint16_t* load) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000181 int32_t StartRawOutputFileRecording(
182 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
183 int32_t StopRawOutputFileRecording() override;
184 int32_t StartRawInputFileRecording(
185 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
186 int32_t StopRawInputFileRecording() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000188 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override;
189 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override;
190 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override;
191 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000193 int32_t ResetAudioDevice() override;
194 int32_t SetLoudspeakerStatus(bool enable) override;
195 int32_t GetLoudspeakerStatus(bool* enabled) const override;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000196 virtual bool BuiltInAECIsAvailable() const { return false; }
197 virtual int32_t EnableBuiltInAEC(bool enable) { return -1; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 // End of functions inherited from webrtc::AudioDeviceModule.
199
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 // The following function is inherited from rtc::MessageHandler.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000201 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202
203 protected:
204 // The constructor is protected because the class needs to be created as a
205 // reference counted object (for memory managment reasons). It could be
206 // exposed in which case the burden of proper instantiation would be put on
207 // the creator of a FakeAudioCaptureModule instance. To create an instance of
208 // this class use the Create(..) API.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000209 explicit FakeAudioCaptureModule(rtc::Thread* process_thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 // The destructor is protected because it is reference counted and should not
211 // be deleted directly.
212 virtual ~FakeAudioCaptureModule();
213
214 private:
215 // Initializes the state of the FakeAudioCaptureModule. This API is called on
216 // creation by the Create() API.
217 bool Initialize();
218 // SetBuffer() sets all samples in send_buffer_ to |value|.
219 void SetSendBuffer(int value);
220 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
221 void ResetRecBuffer();
222 // Returns true if rec_buffer_ contains one or more sample greater than or
223 // equal to |value|.
224 bool CheckRecBuffer(int value);
225
wu@webrtc.org8804a292013-10-22 23:09:20 +0000226 // Returns true/false depending on if recording or playback has been
227 // enabled/started.
228 bool ShouldStartProcessing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
wu@webrtc.org8804a292013-10-22 23:09:20 +0000230 // Starts or stops the pushing and pulling of audio frames.
231 void UpdateProcessing(bool start);
232
233 // Starts the periodic calling of ProcessFrame() in a thread safe way.
234 void StartProcessP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 // Periodcally called function that ensures that frames are pulled and pushed
236 // periodically if enabled/started.
237 void ProcessFrameP();
238 // Pulls frames from the registered webrtc::AudioTransport.
239 void ReceiveFrameP();
240 // Pushes frames to the registered webrtc::AudioTransport.
241 void SendFrameP();
242 // Stops the periodic calling of ProcessFrame() in a thread safe way.
243 void StopProcessP();
244
245 // The time in milliseconds when Process() was last called or 0 if no call
246 // has been made.
247 uint32 last_process_time_ms_;
248
249 // Callback for playout and recording.
250 webrtc::AudioTransport* audio_callback_;
251
252 bool recording_; // True when audio is being pushed from the instance.
253 bool playing_; // True when audio is being pulled by the instance.
254
255 bool play_is_initialized_; // True when the instance is ready to pull audio.
256 bool rec_is_initialized_; // True when the instance is ready to push audio.
257
258 // Input to and output from RecordedDataIsAvailable(..) makes it possible to
259 // modify the current mic level. The implementation does not care about the
260 // mic level so it just feeds back what it receives.
261 uint32_t current_mic_level_;
262
263 // next_frame_time_ is updated in a non-drifting manner to indicate the next
264 // wall clock time the next frame should be generated and received. started_
265 // ensures that next_frame_time_ can be initialized properly on first call.
266 bool started_;
267 uint32 next_frame_time_;
268
269 // User provided thread context.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000270 rtc::Thread* process_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271
272 // Buffer for storing samples received from the webrtc::AudioTransport.
273 char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
274 // Buffer for samples to send to the webrtc::AudioTransport.
275 char send_buffer_[kNumberSamples * kNumberBytesPerSample];
276
277 // Counter of frames received that have samples of high enough amplitude to
278 // indicate that the frames are not faked somewhere in the audio pipeline
279 // (e.g. by a jitter buffer).
280 int frames_received_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000281
282 // Protects variables that are accessed from process_thread_ and
283 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 mutable rtc::CriticalSection crit_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000285 // Protects |audio_callback_| that is accessed from process_thread_ and
286 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287 rtc::CriticalSection crit_callback_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288};
289
290#endif // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_