blob: c27d02df6432a82361982773b251f9c1695aebf2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
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
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#ifndef WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_
21#define WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/basictypes.h"
24#include "webrtc/base/criticalsection.h"
25#include "webrtc/base/messagehandler.h"
deadbeefee8c6d32015-08-13 14:27:18 -070026#include "webrtc/base/scoped_ptr.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000027#include "webrtc/base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028#include "webrtc/common_types.h"
29#include "webrtc/modules/audio_device/include/audio_device.h"
30
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033} // namespace rtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35class FakeAudioCaptureModule
36 : public webrtc::AudioDeviceModule,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +020039 typedef uint16_t Sample;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41 // The value for the following constants have been derived by running VoE
42 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 static const size_t kNumberSamples = 440;
44 static const size_t kNumberBytesPerSample = sizeof(Sample);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46 // Creates a FakeAudioCaptureModule or returns NULL on failure.
deadbeefee8c6d32015-08-13 14:27:18 -070047 static rtc::scoped_refptr<FakeAudioCaptureModule> Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49 // Returns the number of frames that have been successfully pulled by the
50 // instance. Note that correctly detecting success can only be done if the
51 // pulled frame was generated/pushed from a FakeAudioCaptureModule.
52 int frames_received() const;
53
54 // Following functions are inherited from webrtc::AudioDeviceModule.
55 // Only functions called by PeerConnection are implemented, the rest do
56 // nothing and return success. If a function is not expected to be called by
57 // PeerConnection an assertion is triggered if it is in fact called.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 int64_t TimeUntilNextProcess() override;
torbjorngda33a8a2016-02-25 04:34:03 -080059 int32_t Process() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 ErrorCode LastError() const override;
64 int32_t RegisterEventObserver(
65 webrtc::AudioDeviceObserver* event_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
wu@webrtc.org8804a292013-10-22 23:09:20 +000067 // Note: Calling this method from a callback may result in deadlock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 int32_t RegisterAudioCallback(
69 webrtc::AudioTransport* audio_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000071 int32_t Init() override;
72 int32_t Terminate() override;
73 bool Initialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000075 int16_t PlayoutDevices() override;
76 int16_t RecordingDevices() override;
77 int32_t PlayoutDeviceName(uint16_t index,
78 char name[webrtc::kAdmMaxDeviceNameSize],
79 char guid[webrtc::kAdmMaxGuidSize]) override;
80 int32_t RecordingDeviceName(uint16_t index,
81 char name[webrtc::kAdmMaxDeviceNameSize],
82 char guid[webrtc::kAdmMaxGuidSize]) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000084 int32_t SetPlayoutDevice(uint16_t index) override;
85 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
86 int32_t SetRecordingDevice(uint16_t index) override;
87 int32_t SetRecordingDevice(WindowsDeviceType device) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000089 int32_t PlayoutIsAvailable(bool* available) override;
90 int32_t InitPlayout() override;
91 bool PlayoutIsInitialized() const override;
92 int32_t RecordingIsAvailable(bool* available) override;
93 int32_t InitRecording() override;
94 bool RecordingIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int32_t StartPlayout() override;
97 int32_t StopPlayout() override;
98 bool Playing() const override;
99 int32_t StartRecording() override;
100 int32_t StopRecording() override;
101 bool Recording() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000103 int32_t SetAGC(bool enable) override;
104 bool AGC() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 int32_t SetWaveOutVolume(uint16_t volume_left,
107 uint16_t volume_right) override;
108 int32_t WaveOutVolume(uint16_t* volume_left,
109 uint16_t* volume_right) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000111 int32_t InitSpeaker() override;
112 bool SpeakerIsInitialized() const override;
113 int32_t InitMicrophone() override;
114 bool MicrophoneIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000116 int32_t SpeakerVolumeIsAvailable(bool* available) override;
117 int32_t SetSpeakerVolume(uint32_t volume) override;
118 int32_t SpeakerVolume(uint32_t* volume) const override;
119 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
120 int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
121 int32_t SpeakerVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000123 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
124 int32_t SetMicrophoneVolume(uint32_t volume) override;
125 int32_t MicrophoneVolume(uint32_t* volume) const override;
126 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
129 int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 int32_t SpeakerMuteIsAvailable(bool* available) override;
132 int32_t SetSpeakerMute(bool enable) override;
133 int32_t SpeakerMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000135 int32_t MicrophoneMuteIsAvailable(bool* available) override;
136 int32_t SetMicrophoneMute(bool enable) override;
137 int32_t MicrophoneMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000139 int32_t MicrophoneBoostIsAvailable(bool* available) override;
140 int32_t SetMicrophoneBoost(bool enable) override;
141 int32_t MicrophoneBoost(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000143 int32_t StereoPlayoutIsAvailable(bool* available) const override;
144 int32_t SetStereoPlayout(bool enable) override;
145 int32_t StereoPlayout(bool* enabled) const override;
146 int32_t StereoRecordingIsAvailable(bool* available) const override;
147 int32_t SetStereoRecording(bool enable) override;
148 int32_t StereoRecording(bool* enabled) const override;
149 int32_t SetRecordingChannel(const ChannelType channel) override;
150 int32_t RecordingChannel(ChannelType* channel) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000152 int32_t SetPlayoutBuffer(const BufferType type,
153 uint16_t size_ms = 0) override;
154 int32_t PlayoutBuffer(BufferType* type, uint16_t* size_ms) const override;
155 int32_t PlayoutDelay(uint16_t* delay_ms) const override;
156 int32_t RecordingDelay(uint16_t* delay_ms) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000158 int32_t CPULoad(uint16_t* load) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000160 int32_t StartRawOutputFileRecording(
161 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
162 int32_t StopRawOutputFileRecording() override;
163 int32_t StartRawInputFileRecording(
164 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
165 int32_t StopRawInputFileRecording() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000167 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override;
168 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override;
169 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override;
170 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000172 int32_t ResetAudioDevice() override;
173 int32_t SetLoudspeakerStatus(bool enable) override;
174 int32_t GetLoudspeakerStatus(bool* enabled) const override;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000175 virtual bool BuiltInAECIsAvailable() const { return false; }
176 virtual int32_t EnableBuiltInAEC(bool enable) { return -1; }
henrikac14f5ff2015-09-23 14:08:33 +0200177 virtual bool BuiltInAGCIsAvailable() const { return false; }
178 virtual int32_t EnableBuiltInAGC(bool enable) { return -1; }
179 virtual bool BuiltInNSIsAvailable() const { return false; }
180 virtual int32_t EnableBuiltInNS(bool enable) { return -1; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 // End of functions inherited from webrtc::AudioDeviceModule.
182
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 // The following function is inherited from rtc::MessageHandler.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000184 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185
186 protected:
187 // The constructor is protected because the class needs to be created as a
188 // reference counted object (for memory managment reasons). It could be
189 // exposed in which case the burden of proper instantiation would be put on
190 // the creator of a FakeAudioCaptureModule instance. To create an instance of
191 // this class use the Create(..) API.
deadbeefee8c6d32015-08-13 14:27:18 -0700192 explicit FakeAudioCaptureModule();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 // The destructor is protected because it is reference counted and should not
194 // be deleted directly.
195 virtual ~FakeAudioCaptureModule();
196
197 private:
198 // Initializes the state of the FakeAudioCaptureModule. This API is called on
199 // creation by the Create() API.
200 bool Initialize();
201 // SetBuffer() sets all samples in send_buffer_ to |value|.
202 void SetSendBuffer(int value);
203 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
204 void ResetRecBuffer();
205 // Returns true if rec_buffer_ contains one or more sample greater than or
206 // equal to |value|.
207 bool CheckRecBuffer(int value);
208
wu@webrtc.org8804a292013-10-22 23:09:20 +0000209 // Returns true/false depending on if recording or playback has been
210 // enabled/started.
211 bool ShouldStartProcessing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212
wu@webrtc.org8804a292013-10-22 23:09:20 +0000213 // Starts or stops the pushing and pulling of audio frames.
214 void UpdateProcessing(bool start);
215
216 // Starts the periodic calling of ProcessFrame() in a thread safe way.
217 void StartProcessP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 // Periodcally called function that ensures that frames are pulled and pushed
219 // periodically if enabled/started.
220 void ProcessFrameP();
221 // Pulls frames from the registered webrtc::AudioTransport.
222 void ReceiveFrameP();
223 // Pushes frames to the registered webrtc::AudioTransport.
224 void SendFrameP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225
226 // The time in milliseconds when Process() was last called or 0 if no call
227 // has been made.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200228 uint32_t last_process_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
230 // Callback for playout and recording.
231 webrtc::AudioTransport* audio_callback_;
232
233 bool recording_; // True when audio is being pushed from the instance.
234 bool playing_; // True when audio is being pulled by the instance.
235
236 bool play_is_initialized_; // True when the instance is ready to pull audio.
237 bool rec_is_initialized_; // True when the instance is ready to push audio.
238
239 // Input to and output from RecordedDataIsAvailable(..) makes it possible to
240 // modify the current mic level. The implementation does not care about the
241 // mic level so it just feeds back what it receives.
242 uint32_t current_mic_level_;
243
244 // next_frame_time_ is updated in a non-drifting manner to indicate the next
245 // wall clock time the next frame should be generated and received. started_
246 // ensures that next_frame_time_ can be initialized properly on first call.
247 bool started_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200248 uint32_t next_frame_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
deadbeefee8c6d32015-08-13 14:27:18 -0700250 rtc::scoped_ptr<rtc::Thread> process_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
252 // Buffer for storing samples received from the webrtc::AudioTransport.
253 char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
254 // Buffer for samples to send to the webrtc::AudioTransport.
255 char send_buffer_[kNumberSamples * kNumberBytesPerSample];
256
257 // Counter of frames received that have samples of high enough amplitude to
258 // indicate that the frames are not faked somewhere in the audio pipeline
259 // (e.g. by a jitter buffer).
260 int frames_received_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000261
262 // Protects variables that are accessed from process_thread_ and
263 // the main thread.
pbos5ad935c2016-01-25 03:52:44 -0800264 rtc::CriticalSection crit_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000265 // Protects |audio_callback_| that is accessed from process_thread_ and
266 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000267 rtc::CriticalSection crit_callback_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268};
269
Henrik Kjellander15583c12016-02-10 10:53:12 +0100270#endif // WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_