blob: aa10efc0e1587ebad85547734b8db68bc1b1eed6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#ifndef PC_TEST_FAKEAUDIOCAPTUREMODULE_H_
21#define PC_TEST_FAKEAUDIOCAPTUREMODULE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
kwibergd1fe2812016-04-27 06:47:29 -070023#include <memory>
24
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_device/include/audio_device.h"
27#include "rtc_base/basictypes.h"
28#include "rtc_base/criticalsection.h"
29#include "rtc_base/messagehandler.h"
30#include "rtc_base/scoped_ref_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033class Thread;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034} // namespace rtc
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36class FakeAudioCaptureModule
37 : public webrtc::AudioDeviceModule,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000038 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039 public:
Peter Boström0c4e06b2015-10-07 12:23:21 +020040 typedef uint16_t Sample;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42 // The value for the following constants have been derived by running VoE
43 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
Peter Kastingdce40cf2015-08-24 14:52:23 -070044 static const size_t kNumberSamples = 440;
45 static const size_t kNumberBytesPerSample = sizeof(Sample);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47 // Creates a FakeAudioCaptureModule or returns NULL on failure.
deadbeefee8c6d32015-08-13 14:27:18 -070048 static rtc::scoped_refptr<FakeAudioCaptureModule> Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50 // Returns the number of frames that have been successfully pulled by the
51 // instance. Note that correctly detecting success can only be done if the
52 // pulled frame was generated/pushed from a FakeAudioCaptureModule.
53 int frames_received() const;
54
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000055 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
wu@webrtc.org8804a292013-10-22 23:09:20 +000057 // Note: Calling this method from a callback may result in deadlock.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 int32_t RegisterAudioCallback(
59 webrtc::AudioTransport* audio_callback) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 int32_t Init() override;
62 int32_t Terminate() override;
63 bool Initialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 int16_t PlayoutDevices() override;
66 int16_t RecordingDevices() override;
67 int32_t PlayoutDeviceName(uint16_t index,
68 char name[webrtc::kAdmMaxDeviceNameSize],
69 char guid[webrtc::kAdmMaxGuidSize]) override;
70 int32_t RecordingDeviceName(uint16_t index,
71 char name[webrtc::kAdmMaxDeviceNameSize],
72 char guid[webrtc::kAdmMaxGuidSize]) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 int32_t SetPlayoutDevice(uint16_t index) override;
75 int32_t SetPlayoutDevice(WindowsDeviceType device) override;
76 int32_t SetRecordingDevice(uint16_t index) override;
77 int32_t SetRecordingDevice(WindowsDeviceType device) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000079 int32_t PlayoutIsAvailable(bool* available) override;
80 int32_t InitPlayout() override;
81 bool PlayoutIsInitialized() const override;
82 int32_t RecordingIsAvailable(bool* available) override;
83 int32_t InitRecording() override;
84 bool RecordingIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 int32_t StartPlayout() override;
87 int32_t StopPlayout() override;
88 bool Playing() const override;
89 int32_t StartRecording() override;
90 int32_t StopRecording() override;
91 bool Recording() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000093 int32_t SetAGC(bool enable) override;
94 bool AGC() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int32_t InitSpeaker() override;
97 bool SpeakerIsInitialized() const override;
98 int32_t InitMicrophone() override;
99 bool MicrophoneIsInitialized() const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 int32_t SpeakerVolumeIsAvailable(bool* available) override;
102 int32_t SetSpeakerVolume(uint32_t volume) override;
103 int32_t SpeakerVolume(uint32_t* volume) const override;
104 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
105 int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000107 int32_t MicrophoneVolumeIsAvailable(bool* available) override;
108 int32_t SetMicrophoneVolume(uint32_t volume) override;
109 int32_t MicrophoneVolume(uint32_t* volume) const override;
110 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000112 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000114 int32_t SpeakerMuteIsAvailable(bool* available) override;
115 int32_t SetSpeakerMute(bool enable) override;
116 int32_t SpeakerMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 int32_t MicrophoneMuteIsAvailable(bool* available) override;
119 int32_t SetMicrophoneMute(bool enable) override;
120 int32_t MicrophoneMute(bool* enabled) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000122 int32_t StereoPlayoutIsAvailable(bool* available) const override;
123 int32_t SetStereoPlayout(bool enable) override;
124 int32_t StereoPlayout(bool* enabled) const override;
125 int32_t StereoRecordingIsAvailable(bool* available) const override;
126 int32_t SetStereoRecording(bool enable) override;
127 int32_t StereoRecording(bool* enabled) const override;
128 int32_t SetRecordingChannel(const ChannelType channel) override;
129 int32_t RecordingChannel(ChannelType* channel) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 int32_t PlayoutDelay(uint16_t* delay_ms) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000133 int32_t SetRecordingSampleRate(const uint32_t samples_per_sec) override;
134 int32_t RecordingSampleRate(uint32_t* samples_per_sec) const override;
135 int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) override;
136 int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000138 int32_t SetLoudspeakerStatus(bool enable) override;
139 int32_t GetLoudspeakerStatus(bool* enabled) const override;
nisseef8b61e2016-04-29 06:09:15 -0700140 bool BuiltInAECIsAvailable() const override { return false; }
141 int32_t EnableBuiltInAEC(bool enable) override { return -1; }
142 bool BuiltInAGCIsAvailable() const override { return false; }
143 int32_t EnableBuiltInAGC(bool enable) override { return -1; }
144 bool BuiltInNSIsAvailable() const override { return false; }
145 int32_t EnableBuiltInNS(bool enable) override { return -1; }
maxmorin88e31a32016-08-16 00:56:09 -0700146#if defined(WEBRTC_IOS)
147 int GetPlayoutAudioParameters(
148 webrtc::AudioParameters* params) const override {
149 return -1;
150 }
151 int GetRecordAudioParameters(webrtc::AudioParameters* params) const override {
152 return -1;
153 }
154#endif // WEBRTC_IOS
155
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 // End of functions inherited from webrtc::AudioDeviceModule.
157
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000158 // The following function is inherited from rtc::MessageHandler.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000159 void OnMessage(rtc::Message* msg) override;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
161 protected:
162 // The constructor is protected because the class needs to be created as a
163 // reference counted object (for memory managment reasons). It could be
164 // exposed in which case the burden of proper instantiation would be put on
165 // the creator of a FakeAudioCaptureModule instance. To create an instance of
166 // this class use the Create(..) API.
Steve Anton36b29d12017-10-30 09:57:42 -0700167 FakeAudioCaptureModule();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 // The destructor is protected because it is reference counted and should not
169 // be deleted directly.
170 virtual ~FakeAudioCaptureModule();
171
172 private:
173 // Initializes the state of the FakeAudioCaptureModule. This API is called on
174 // creation by the Create() API.
175 bool Initialize();
176 // SetBuffer() sets all samples in send_buffer_ to |value|.
177 void SetSendBuffer(int value);
178 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
179 void ResetRecBuffer();
180 // Returns true if rec_buffer_ contains one or more sample greater than or
181 // equal to |value|.
182 bool CheckRecBuffer(int value);
183
wu@webrtc.org8804a292013-10-22 23:09:20 +0000184 // Returns true/false depending on if recording or playback has been
185 // enabled/started.
186 bool ShouldStartProcessing();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
wu@webrtc.org8804a292013-10-22 23:09:20 +0000188 // Starts or stops the pushing and pulling of audio frames.
189 void UpdateProcessing(bool start);
190
191 // Starts the periodic calling of ProcessFrame() in a thread safe way.
192 void StartProcessP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 // Periodcally called function that ensures that frames are pulled and pushed
194 // periodically if enabled/started.
195 void ProcessFrameP();
196 // Pulls frames from the registered webrtc::AudioTransport.
197 void ReceiveFrameP();
198 // Pushes frames to the registered webrtc::AudioTransport.
199 void SendFrameP();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 // Callback for playout and recording.
202 webrtc::AudioTransport* audio_callback_;
203
Steve Anton36b29d12017-10-30 09:57:42 -0700204 bool recording_; // True when audio is being pushed from the instance.
205 bool playing_; // True when audio is being pulled by the instance.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
Steve Anton36b29d12017-10-30 09:57:42 -0700207 bool play_is_initialized_; // True when the instance is ready to pull audio.
208 bool rec_is_initialized_; // True when the instance is ready to push audio.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209
210 // Input to and output from RecordedDataIsAvailable(..) makes it possible to
211 // modify the current mic level. The implementation does not care about the
212 // mic level so it just feeds back what it receives.
213 uint32_t current_mic_level_;
214
215 // next_frame_time_ is updated in a non-drifting manner to indicate the next
216 // wall clock time the next frame should be generated and received. started_
217 // ensures that next_frame_time_ can be initialized properly on first call.
218 bool started_;
Honghai Zhang82d78622016-05-06 11:29:15 -0700219 int64_t next_frame_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
kwibergd1fe2812016-04-27 06:47:29 -0700221 std::unique_ptr<rtc::Thread> process_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222
223 // Buffer for storing samples received from the webrtc::AudioTransport.
224 char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
225 // Buffer for samples to send to the webrtc::AudioTransport.
226 char send_buffer_[kNumberSamples * kNumberBytesPerSample];
227
228 // Counter of frames received that have samples of high enough amplitude to
229 // indicate that the frames are not faked somewhere in the audio pipeline
230 // (e.g. by a jitter buffer).
231 int frames_received_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000232
233 // Protects variables that are accessed from process_thread_ and
234 // the main thread.
pbos5ad935c2016-01-25 03:52:44 -0800235 rtc::CriticalSection crit_;
wu@webrtc.org8804a292013-10-22 23:09:20 +0000236 // Protects |audio_callback_| that is accessed from process_thread_ and
237 // the main thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000238 rtc::CriticalSection crit_callback_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239};
240
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200241#endif // PC_TEST_FAKEAUDIOCAPTUREMODULE_H_