blob: be1df9a7bd7e48c249b8011256994d9757bb7cc7 [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:
56 typedef uint16 Sample;
57
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 Kastingf045e4d2015-06-10 21:15:38 -070060 static const int kNumberSamples = 440;
61 static const int 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; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 // End of functions inherited from webrtc::AudioDeviceModule.
195
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000196 // The following function is inherited from rtc::MessageHandler.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000197 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199 protected:
200 // The constructor is protected because the class needs to be created as a
201 // reference counted object (for memory managment reasons). It could be
202 // exposed in which case the burden of proper instantiation would be put on
203 // the creator of a FakeAudioCaptureModule instance. To create an instance of
204 // this class use the Create(..) API.
deadbeefee8c6d32015-08-13 14:27:18 -0700205 explicit FakeAudioCaptureModule();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 // The destructor is protected because it is reference counted and should not
207 // be deleted directly.
208 virtual ~FakeAudioCaptureModule();
209
210 private:
211 // Initializes the state of the FakeAudioCaptureModule. This API is called on
212 // creation by the Create() API.
213 bool Initialize();
214 // SetBuffer() sets all samples in send_buffer_ to |value|.
215 void SetSendBuffer(int value);
216 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
217 void ResetRecBuffer();
218 // Returns true if rec_buffer_ contains one or more sample greater than or
219 // equal to |value|.
220 bool CheckRecBuffer(int value);
221
wu@webrtc.org8804a292013-10-22 23:09:20 +0000222 // Returns true/false depending on if recording or playback has been
223 // enabled/started.
224 bool ShouldStartProcessing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
wu@webrtc.org8804a292013-10-22 23:09:20 +0000226 // Starts or stops the pushing and pulling of audio frames.
227 void UpdateProcessing(bool start);
228
229 // Starts the periodic calling of ProcessFrame() in a thread safe way.
230 void StartProcessP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 // Periodcally called function that ensures that frames are pulled and pushed
232 // periodically if enabled/started.
233 void ProcessFrameP();
234 // Pulls frames from the registered webrtc::AudioTransport.
235 void ReceiveFrameP();
236 // Pushes frames to the registered webrtc::AudioTransport.
237 void SendFrameP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238
239 // The time in milliseconds when Process() was last called or 0 if no call
240 // has been made.
241 uint32 last_process_time_ms_;
242
243 // Callback for playout and recording.
244 webrtc::AudioTransport* audio_callback_;
245
246 bool recording_; // True when audio is being pushed from the instance.
247 bool playing_; // True when audio is being pulled by the instance.
248
249 bool play_is_initialized_; // True when the instance is ready to pull audio.
250 bool rec_is_initialized_; // True when the instance is ready to push audio.
251
252 // Input to and output from RecordedDataIsAvailable(..) makes it possible to
253 // modify the current mic level. The implementation does not care about the
254 // mic level so it just feeds back what it receives.
255 uint32_t current_mic_level_;
256
257 // next_frame_time_ is updated in a non-drifting manner to indicate the next
258 // wall clock time the next frame should be generated and received. started_
259 // ensures that next_frame_time_ can be initialized properly on first call.
260 bool started_;
261 uint32 next_frame_time_;
262
deadbeefee8c6d32015-08-13 14:27:18 -0700263 rtc::scoped_ptr<rtc::Thread> process_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264
265 // Buffer for storing samples received from the webrtc::AudioTransport.
266 char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
267 // Buffer for samples to send to the webrtc::AudioTransport.
268 char send_buffer_[kNumberSamples * kNumberBytesPerSample];
269
270 // Counter of frames received that have samples of high enough amplitude to
271 // indicate that the frames are not faked somewhere in the audio pipeline
272 // (e.g. by a jitter buffer).
273 int frames_received_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000274
275 // Protects variables that are accessed from process_thread_ and
276 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000277 mutable rtc::CriticalSection crit_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000278 // Protects |audio_callback_| that is accessed from process_thread_ and
279 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280 rtc::CriticalSection crit_callback_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281};
282
283#endif // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_