blob: ca42c3b018df12b31b756723d1eb6e6946207988 [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
kwibergd1fe2812016-04-27 06:47:29 -070023#include <memory>
24
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000025#include "webrtc/base/basictypes.h"
26#include "webrtc/base/criticalsection.h"
27#include "webrtc/base/messagehandler.h"
deadbeefee8c6d32015-08-13 14:27:18 -070028#include "webrtc/base/scoped_ptr.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000029#include "webrtc/base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030#include "webrtc/common_types.h"
31#include "webrtc/modules/audio_device/include/audio_device.h"
32
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035} // namespace rtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37class FakeAudioCaptureModule
38 : public webrtc::AudioDeviceModule,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000039 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +020041 typedef uint16_t Sample;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43 // The value for the following constants have been derived by running VoE
44 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070045 static const size_t kNumberSamples = 440;
46 static const size_t kNumberBytesPerSample = sizeof(Sample);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48 // Creates a FakeAudioCaptureModule or returns NULL on failure.
deadbeefee8c6d32015-08-13 14:27:18 -070049 static rtc::scoped_refptr<FakeAudioCaptureModule> Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
51 // Returns the number of frames that have been successfully pulled by the
52 // instance. Note that correctly detecting success can only be done if the
53 // pulled frame was generated/pushed from a FakeAudioCaptureModule.
54 int frames_received() const;
55
56 // Following functions are inherited from webrtc::AudioDeviceModule.
57 // Only functions called by PeerConnection are implemented, the rest do
58 // nothing and return success. If a function is not expected to be called by
59 // PeerConnection an assertion is triggered if it is in fact called.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000060 int64_t TimeUntilNextProcess() override;
pbosa26ac922016-02-25 04:50:01 -080061 void Process() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000063 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 ErrorCode LastError() const override;
66 int32_t RegisterEventObserver(
67 webrtc::AudioDeviceObserver* event_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
wu@webrtc.org8804a292013-10-22 23:09:20 +000069 // Note: Calling this method from a callback may result in deadlock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000070 int32_t RegisterAudioCallback(
71 webrtc::AudioTransport* audio_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000073 int32_t Init() override;
74 int32_t Terminate() override;
75 bool Initialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000077 int16_t PlayoutDevices() override;
78 int16_t RecordingDevices() override;
79 int32_t PlayoutDeviceName(uint16_t index,
80 char name[webrtc::kAdmMaxDeviceNameSize],
81 char guid[webrtc::kAdmMaxGuidSize]) override;
82 int32_t RecordingDeviceName(uint16_t index,
83 char name[webrtc::kAdmMaxDeviceNameSize],
84 char guid[webrtc::kAdmMaxGuidSize]) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 int32_t SetPlayoutDevice(uint16_t index) override;
87 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
88 int32_t SetRecordingDevice(uint16_t index) override;
89 int32_t SetRecordingDevice(WindowsDeviceType device) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 int32_t PlayoutIsAvailable(bool* available) override;
92 int32_t InitPlayout() override;
93 bool PlayoutIsInitialized() const override;
94 int32_t RecordingIsAvailable(bool* available) override;
95 int32_t InitRecording() override;
96 bool RecordingIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000098 int32_t StartPlayout() override;
99 int32_t StopPlayout() override;
100 bool Playing() const override;
101 int32_t StartRecording() override;
102 int32_t StopRecording() override;
103 bool Recording() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000105 int32_t SetAGC(bool enable) override;
106 bool AGC() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000108 int32_t SetWaveOutVolume(uint16_t volume_left,
109 uint16_t volume_right) override;
110 int32_t WaveOutVolume(uint16_t* volume_left,
111 uint16_t* volume_right) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 int32_t InitSpeaker() override;
114 bool SpeakerIsInitialized() const override;
115 int32_t InitMicrophone() override;
116 bool MicrophoneIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 int32_t SpeakerVolumeIsAvailable(bool* available) override;
119 int32_t SetSpeakerVolume(uint32_t volume) override;
120 int32_t SpeakerVolume(uint32_t* volume) const override;
121 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
122 int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
123 int32_t SpeakerVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000125 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
126 int32_t SetMicrophoneVolume(uint32_t volume) override;
127 int32_t MicrophoneVolume(uint32_t* volume) const override;
128 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000130 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
131 int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000133 int32_t SpeakerMuteIsAvailable(bool* available) override;
134 int32_t SetSpeakerMute(bool enable) override;
135 int32_t SpeakerMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 int32_t MicrophoneMuteIsAvailable(bool* available) override;
138 int32_t SetMicrophoneMute(bool enable) override;
139 int32_t MicrophoneMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000141 int32_t MicrophoneBoostIsAvailable(bool* available) override;
142 int32_t SetMicrophoneBoost(bool enable) override;
143 int32_t MicrophoneBoost(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000145 int32_t StereoPlayoutIsAvailable(bool* available) const override;
146 int32_t SetStereoPlayout(bool enable) override;
147 int32_t StereoPlayout(bool* enabled) const override;
148 int32_t StereoRecordingIsAvailable(bool* available) const override;
149 int32_t SetStereoRecording(bool enable) override;
150 int32_t StereoRecording(bool* enabled) const override;
151 int32_t SetRecordingChannel(const ChannelType channel) override;
152 int32_t RecordingChannel(ChannelType* channel) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000154 int32_t SetPlayoutBuffer(const BufferType type,
155 uint16_t size_ms = 0) override;
156 int32_t PlayoutBuffer(BufferType* type, uint16_t* size_ms) const override;
157 int32_t PlayoutDelay(uint16_t* delay_ms) const override;
158 int32_t RecordingDelay(uint16_t* delay_ms) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000160 int32_t CPULoad(uint16_t* load) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000162 int32_t StartRawOutputFileRecording(
163 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
164 int32_t StopRawOutputFileRecording() override;
165 int32_t StartRawInputFileRecording(
166 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) override;
167 int32_t StopRawInputFileRecording() override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000169 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override;
170 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override;
171 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override;
172 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000174 int32_t ResetAudioDevice() override;
175 int32_t SetLoudspeakerStatus(bool enable) override;
176 int32_t GetLoudspeakerStatus(bool* enabled) const override;
nisseef8b61e2016-04-29 06:09:15 -0700177 bool BuiltInAECIsAvailable() const override { return false; }
178 int32_t EnableBuiltInAEC(bool enable) override { return -1; }
179 bool BuiltInAGCIsAvailable() const override { return false; }
180 int32_t EnableBuiltInAGC(bool enable) override { return -1; }
181 bool BuiltInNSIsAvailable() const override { return false; }
182 int32_t EnableBuiltInNS(bool enable) override { return -1; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 // End of functions inherited from webrtc::AudioDeviceModule.
184
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000185 // The following function is inherited from rtc::MessageHandler.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000186 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
188 protected:
189 // The constructor is protected because the class needs to be created as a
190 // reference counted object (for memory managment reasons). It could be
191 // exposed in which case the burden of proper instantiation would be put on
192 // the creator of a FakeAudioCaptureModule instance. To create an instance of
193 // this class use the Create(..) API.
deadbeefee8c6d32015-08-13 14:27:18 -0700194 explicit FakeAudioCaptureModule();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 // The destructor is protected because it is reference counted and should not
196 // be deleted directly.
197 virtual ~FakeAudioCaptureModule();
198
199 private:
200 // Initializes the state of the FakeAudioCaptureModule. This API is called on
201 // creation by the Create() API.
202 bool Initialize();
203 // SetBuffer() sets all samples in send_buffer_ to |value|.
204 void SetSendBuffer(int value);
205 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
206 void ResetRecBuffer();
207 // Returns true if rec_buffer_ contains one or more sample greater than or
208 // equal to |value|.
209 bool CheckRecBuffer(int value);
210
wu@webrtc.org8804a292013-10-22 23:09:20 +0000211 // Returns true/false depending on if recording or playback has been
212 // enabled/started.
213 bool ShouldStartProcessing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
wu@webrtc.org8804a292013-10-22 23:09:20 +0000215 // Starts or stops the pushing and pulling of audio frames.
216 void UpdateProcessing(bool start);
217
218 // Starts the periodic calling of ProcessFrame() in a thread safe way.
219 void StartProcessP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 // Periodcally called function that ensures that frames are pulled and pushed
221 // periodically if enabled/started.
222 void ProcessFrameP();
223 // Pulls frames from the registered webrtc::AudioTransport.
224 void ReceiveFrameP();
225 // Pushes frames to the registered webrtc::AudioTransport.
226 void SendFrameP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227
228 // The time in milliseconds when Process() was last called or 0 if no call
229 // has been made.
Honghai Zhang82d78622016-05-06 11:29:15 -0700230 int64_t last_process_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231
232 // Callback for playout and recording.
233 webrtc::AudioTransport* audio_callback_;
234
235 bool recording_; // True when audio is being pushed from the instance.
236 bool playing_; // True when audio is being pulled by the instance.
237
238 bool play_is_initialized_; // True when the instance is ready to pull audio.
239 bool rec_is_initialized_; // True when the instance is ready to push audio.
240
241 // Input to and output from RecordedDataIsAvailable(..) makes it possible to
242 // modify the current mic level. The implementation does not care about the
243 // mic level so it just feeds back what it receives.
244 uint32_t current_mic_level_;
245
246 // next_frame_time_ is updated in a non-drifting manner to indicate the next
247 // wall clock time the next frame should be generated and received. started_
248 // ensures that next_frame_time_ can be initialized properly on first call.
249 bool started_;
Honghai Zhang82d78622016-05-06 11:29:15 -0700250 int64_t next_frame_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
kwibergd1fe2812016-04-27 06:47:29 -0700252 std::unique_ptr<rtc::Thread> process_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
254 // Buffer for storing samples received from the webrtc::AudioTransport.
255 char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
256 // Buffer for samples to send to the webrtc::AudioTransport.
257 char send_buffer_[kNumberSamples * kNumberBytesPerSample];
258
259 // Counter of frames received that have samples of high enough amplitude to
260 // indicate that the frames are not faked somewhere in the audio pipeline
261 // (e.g. by a jitter buffer).
262 int frames_received_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000263
264 // Protects variables that are accessed from process_thread_ and
265 // the main thread.
pbos5ad935c2016-01-25 03:52:44 -0800266 rtc::CriticalSection crit_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000267 // Protects |audio_callback_| that is accessed from process_thread_ and
268 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000269 rtc::CriticalSection crit_callback_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270};
271
Henrik Kjellander15583c12016-02-10 10:53:12 +0100272#endif // WEBRTC_API_TEST_FAKEAUDIOCAPTUREMODULE_H_