blob: 4284b9ed51b89778e33aa0f2893be4a98fff9603 [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"
deadbeefee8c6d32015-08-13 14:27:18 -070043#include "webrtc/base/scoped_ptr.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044#include "webrtc/base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045#include "webrtc/common_types.h"
46#include "webrtc/modules/audio_device/include/audio_device.h"
47
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000050} // namespace rtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
52class FakeAudioCaptureModule
53 : public webrtc::AudioDeviceModule,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +020056 typedef uint16_t Sample;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58 // The value for the following constants have been derived by running VoE
59 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070060 static const size_t kNumberSamples = 440;
61 static const size_t kNumberBytesPerSample = sizeof(Sample);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
63 // Creates a FakeAudioCaptureModule or returns NULL on failure.
deadbeefee8c6d32015-08-13 14:27:18 -070064 static rtc::scoped_refptr<FakeAudioCaptureModule> Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
66 // Returns the number of frames that have been successfully pulled by the
67 // instance. Note that correctly detecting success can only be done if the
68 // pulled frame was generated/pushed from a FakeAudioCaptureModule.
69 int frames_received() const;
70
71 // Following functions are inherited from webrtc::AudioDeviceModule.
72 // Only functions called by PeerConnection are implemented, the rest do
73 // nothing and return success. If a function is not expected to be called by
74 // PeerConnection an assertion is triggered if it is in fact called.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 int64_t TimeUntilNextProcess() override;
76 int32_t Process() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000078 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000080 ErrorCode LastError() const override;
81 int32_t RegisterEventObserver(
82 webrtc::AudioDeviceObserver* event_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
wu@webrtc.org8804a292013-10-22 23:09:20 +000084 // Note: Calling this method from a callback may result in deadlock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000085 int32_t RegisterAudioCallback(
86 webrtc::AudioTransport* audio_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000088 int32_t Init() override;
89 int32_t Terminate() override;
90 bool Initialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000092 int16_t PlayoutDevices() override;
93 int16_t RecordingDevices() override;
94 int32_t PlayoutDeviceName(uint16_t index,
95 char name[webrtc::kAdmMaxDeviceNameSize],
96 char guid[webrtc::kAdmMaxGuidSize]) override;
97 int32_t RecordingDeviceName(uint16_t index,
98 char name[webrtc::kAdmMaxDeviceNameSize],
99 char guid[webrtc::kAdmMaxGuidSize]) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 int32_t SetPlayoutDevice(uint16_t index) override;
102 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
103 int32_t SetRecordingDevice(uint16_t index) override;
104 int32_t SetRecordingDevice(WindowsDeviceType device) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 int32_t PlayoutIsAvailable(bool* available) override;
107 int32_t InitPlayout() override;
108 bool PlayoutIsInitialized() const override;
109 int32_t RecordingIsAvailable(bool* available) override;
110 int32_t InitRecording() override;
111 bool RecordingIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 int32_t StartPlayout() override;
114 int32_t StopPlayout() override;
115 bool Playing() const override;
116 int32_t StartRecording() override;
117 int32_t StopRecording() override;
118 bool Recording() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000120 int32_t SetAGC(bool enable) override;
121 bool AGC() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 int32_t SetWaveOutVolume(uint16_t volume_left,
124 uint16_t volume_right) override;
125 int32_t WaveOutVolume(uint16_t* volume_left,
126 uint16_t* volume_right) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 int32_t InitSpeaker() override;
129 bool SpeakerIsInitialized() const override;
130 int32_t InitMicrophone() override;
131 bool MicrophoneIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000133 int32_t SpeakerVolumeIsAvailable(bool* available) override;
134 int32_t SetSpeakerVolume(uint32_t volume) override;
135 int32_t SpeakerVolume(uint32_t* volume) const override;
136 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
137 int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
138 int32_t SpeakerVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000140 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
141 int32_t SetMicrophoneVolume(uint32_t volume) override;
142 int32_t MicrophoneVolume(uint32_t* volume) const override;
143 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000145 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
146 int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000148 int32_t SpeakerMuteIsAvailable(bool* available) override;
149 int32_t SetSpeakerMute(bool enable) override;
150 int32_t SpeakerMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000152 int32_t MicrophoneMuteIsAvailable(bool* available) override;
153 int32_t SetMicrophoneMute(bool enable) override;
154 int32_t MicrophoneMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000156 int32_t MicrophoneBoostIsAvailable(bool* available) override;
157 int32_t SetMicrophoneBoost(bool enable) override;
158 int32_t MicrophoneBoost(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000160 int32_t StereoPlayoutIsAvailable(bool* available) const override;
161 int32_t SetStereoPlayout(bool enable) override;
162 int32_t StereoPlayout(bool* enabled) const override;
163 int32_t StereoRecordingIsAvailable(bool* available) const override;
164 int32_t SetStereoRecording(bool enable) override;
165 int32_t StereoRecording(bool* enabled) const override;
166 int32_t SetRecordingChannel(const ChannelType channel) override;
167 int32_t RecordingChannel(ChannelType* channel) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000169 int32_t SetPlayoutBuffer(const BufferType type,
170 uint16_t size_ms = 0) override;
171 int32_t PlayoutBuffer(BufferType* type, uint16_t* size_ms) const override;
172 int32_t PlayoutDelay(uint16_t* delay_ms) const override;
173 int32_t RecordingDelay(uint16_t* delay_ms) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000175 int32_t CPULoad(uint16_t* load) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000177 int32_t StartRawOutputFileRecording(
178 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
179 int32_t StopRawOutputFileRecording() override;
180 int32_t StartRawInputFileRecording(
181 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
182 int32_t StopRawInputFileRecording() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000184 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override;
185 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override;
186 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override;
187 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000189 int32_t ResetAudioDevice() override;
190 int32_t SetLoudspeakerStatus(bool enable) override;
191 int32_t GetLoudspeakerStatus(bool* enabled) const override;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000192 virtual bool BuiltInAECIsAvailable() const { return false; }
193 virtual int32_t EnableBuiltInAEC(bool enable) { return -1; }
henrikac14f5ff2015-09-23 14:08:33 +0200194 virtual bool BuiltInAGCIsAvailable() const { return false; }
195 virtual int32_t EnableBuiltInAGC(bool enable) { return -1; }
196 virtual bool BuiltInNSIsAvailable() const { return false; }
197 virtual int32_t EnableBuiltInNS(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.
deadbeefee8c6d32015-08-13 14:27:18 -0700209 explicit FakeAudioCaptureModule();
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();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
243 // The time in milliseconds when Process() was last called or 0 if no call
244 // has been made.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200245 uint32_t last_process_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246
247 // Callback for playout and recording.
248 webrtc::AudioTransport* audio_callback_;
249
250 bool recording_; // True when audio is being pushed from the instance.
251 bool playing_; // True when audio is being pulled by the instance.
252
253 bool play_is_initialized_; // True when the instance is ready to pull audio.
254 bool rec_is_initialized_; // True when the instance is ready to push audio.
255
256 // Input to and output from RecordedDataIsAvailable(..) makes it possible to
257 // modify the current mic level. The implementation does not care about the
258 // mic level so it just feeds back what it receives.
259 uint32_t current_mic_level_;
260
261 // next_frame_time_ is updated in a non-drifting manner to indicate the next
262 // wall clock time the next frame should be generated and received. started_
263 // ensures that next_frame_time_ can be initialized properly on first call.
264 bool started_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200265 uint32_t next_frame_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266
deadbeefee8c6d32015-08-13 14:27:18 -0700267 rtc::scoped_ptr<rtc::Thread> process_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268
269 // Buffer for storing samples received from the webrtc::AudioTransport.
270 char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
271 // Buffer for samples to send to the webrtc::AudioTransport.
272 char send_buffer_[kNumberSamples * kNumberBytesPerSample];
273
274 // Counter of frames received that have samples of high enough amplitude to
275 // indicate that the frames are not faked somewhere in the audio pipeline
276 // (e.g. by a jitter buffer).
277 int frames_received_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000278
279 // Protects variables that are accessed from process_thread_ and
280 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000281 mutable rtc::CriticalSection crit_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000282 // Protects |audio_callback_| that is accessed from process_thread_ and
283 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284 rtc::CriticalSection crit_callback_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285};
286
287#endif // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_