xref: /aosp_15_r20/external/webrtc/pc/test/fake_audio_capture_module.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
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 
20 #ifndef PC_TEST_FAKE_AUDIO_CAPTURE_MODULE_H_
21 #define PC_TEST_FAKE_AUDIO_CAPTURE_MODULE_H_
22 
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 #include <memory>
27 
28 #include "api/scoped_refptr.h"
29 #include "api/sequence_checker.h"
30 #include "modules/audio_device/include/audio_device.h"
31 #include "modules/audio_device/include/audio_device_defines.h"
32 #include "rtc_base/synchronization/mutex.h"
33 #include "rtc_base/thread_annotations.h"
34 
35 namespace rtc {
36 class Thread;
37 }  // namespace rtc
38 
39 class FakeAudioCaptureModule : public webrtc::AudioDeviceModule {
40  public:
41   typedef uint16_t Sample;
42 
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.
45   static const size_t kNumberSamples = 440;
46   static const size_t kNumberBytesPerSample = sizeof(Sample);
47 
48   // Creates a FakeAudioCaptureModule or returns NULL on failure.
49   static rtc::scoped_refptr<FakeAudioCaptureModule> Create();
50 
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 RTC_LOCKS_EXCLUDED(mutex_);
55 
56   int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override;
57 
58   // Note: Calling this method from a callback may result in deadlock.
59   int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback) override
60       RTC_LOCKS_EXCLUDED(mutex_);
61 
62   int32_t Init() override;
63   int32_t Terminate() override;
64   bool Initialized() const override;
65 
66   int16_t PlayoutDevices() override;
67   int16_t RecordingDevices() override;
68   int32_t PlayoutDeviceName(uint16_t index,
69                             char name[webrtc::kAdmMaxDeviceNameSize],
70                             char guid[webrtc::kAdmMaxGuidSize]) override;
71   int32_t RecordingDeviceName(uint16_t index,
72                               char name[webrtc::kAdmMaxDeviceNameSize],
73                               char guid[webrtc::kAdmMaxGuidSize]) override;
74 
75   int32_t SetPlayoutDevice(uint16_t index) override;
76   int32_t SetPlayoutDevice(WindowsDeviceType device) override;
77   int32_t SetRecordingDevice(uint16_t index) override;
78   int32_t SetRecordingDevice(WindowsDeviceType device) override;
79 
80   int32_t PlayoutIsAvailable(bool* available) override;
81   int32_t InitPlayout() override;
82   bool PlayoutIsInitialized() const override;
83   int32_t RecordingIsAvailable(bool* available) override;
84   int32_t InitRecording() override;
85   bool RecordingIsInitialized() const override;
86 
87   int32_t StartPlayout() RTC_LOCKS_EXCLUDED(mutex_) override;
88   int32_t StopPlayout() RTC_LOCKS_EXCLUDED(mutex_) override;
89   bool Playing() const RTC_LOCKS_EXCLUDED(mutex_) override;
90   int32_t StartRecording() RTC_LOCKS_EXCLUDED(mutex_) override;
91   int32_t StopRecording() RTC_LOCKS_EXCLUDED(mutex_) override;
92   bool Recording() const RTC_LOCKS_EXCLUDED(mutex_) override;
93 
94   int32_t InitSpeaker() override;
95   bool SpeakerIsInitialized() const override;
96   int32_t InitMicrophone() override;
97   bool MicrophoneIsInitialized() const override;
98 
99   int32_t SpeakerVolumeIsAvailable(bool* available) override;
100   int32_t SetSpeakerVolume(uint32_t volume) override;
101   int32_t SpeakerVolume(uint32_t* volume) const override;
102   int32_t MaxSpeakerVolume(uint32_t* max_volume) const override;
103   int32_t MinSpeakerVolume(uint32_t* min_volume) const override;
104 
105   int32_t MicrophoneVolumeIsAvailable(bool* available) override;
106   int32_t SetMicrophoneVolume(uint32_t volume)
107       RTC_LOCKS_EXCLUDED(mutex_) override;
108   int32_t MicrophoneVolume(uint32_t* volume) const
109       RTC_LOCKS_EXCLUDED(mutex_) override;
110   int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override;
111 
112   int32_t MinMicrophoneVolume(uint32_t* min_volume) const override;
113 
114   int32_t SpeakerMuteIsAvailable(bool* available) override;
115   int32_t SetSpeakerMute(bool enable) override;
116   int32_t SpeakerMute(bool* enabled) const override;
117 
118   int32_t MicrophoneMuteIsAvailable(bool* available) override;
119   int32_t SetMicrophoneMute(bool enable) override;
120   int32_t MicrophoneMute(bool* enabled) const override;
121 
122   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 
129   int32_t PlayoutDelay(uint16_t* delay_ms) const override;
130 
BuiltInAECIsAvailable()131   bool BuiltInAECIsAvailable() const override { return false; }
EnableBuiltInAEC(bool enable)132   int32_t EnableBuiltInAEC(bool enable) override { return -1; }
BuiltInAGCIsAvailable()133   bool BuiltInAGCIsAvailable() const override { return false; }
EnableBuiltInAGC(bool enable)134   int32_t EnableBuiltInAGC(bool enable) override { return -1; }
BuiltInNSIsAvailable()135   bool BuiltInNSIsAvailable() const override { return false; }
EnableBuiltInNS(bool enable)136   int32_t EnableBuiltInNS(bool enable) override { return -1; }
137 
GetPlayoutUnderrunCount()138   int32_t GetPlayoutUnderrunCount() const override { return -1; }
139 #if defined(WEBRTC_IOS)
GetPlayoutAudioParameters(webrtc::AudioParameters * params)140   int GetPlayoutAudioParameters(
141       webrtc::AudioParameters* params) const override {
142     return -1;
143   }
GetRecordAudioParameters(webrtc::AudioParameters * params)144   int GetRecordAudioParameters(webrtc::AudioParameters* params) const override {
145     return -1;
146   }
147 #endif  // WEBRTC_IOS
148 
149   // End of functions inherited from webrtc::AudioDeviceModule.
150 
151  protected:
152   // The constructor is protected because the class needs to be created as a
153   // reference counted object (for memory managment reasons). It could be
154   // exposed in which case the burden of proper instantiation would be put on
155   // the creator of a FakeAudioCaptureModule instance. To create an instance of
156   // this class use the Create(..) API.
157   FakeAudioCaptureModule();
158   // The destructor is protected because it is reference counted and should not
159   // be deleted directly.
160   virtual ~FakeAudioCaptureModule();
161 
162  private:
163   // Initializes the state of the FakeAudioCaptureModule. This API is called on
164   // creation by the Create() API.
165   bool Initialize();
166   // SetBuffer() sets all samples in send_buffer_ to `value`.
167   void SetSendBuffer(int value);
168   // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
169   void ResetRecBuffer();
170   // Returns true if rec_buffer_ contains one or more sample greater than or
171   // equal to `value`.
172   bool CheckRecBuffer(int value);
173 
174   // Returns true/false depending on if recording or playback has been
175   // enabled/started.
176   bool ShouldStartProcessing() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
177 
178   // Starts or stops the pushing and pulling of audio frames.
179   void UpdateProcessing(bool start) RTC_LOCKS_EXCLUDED(mutex_);
180 
181   // Starts the periodic calling of ProcessFrame() in a thread safe way.
182   void StartProcessP();
183   // Periodcally called function that ensures that frames are pulled and pushed
184   // periodically if enabled/started.
185   void ProcessFrameP() RTC_LOCKS_EXCLUDED(mutex_);
186   // Pulls frames from the registered webrtc::AudioTransport.
187   void ReceiveFrameP() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
188   // Pushes frames to the registered webrtc::AudioTransport.
189   void SendFrameP() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
190 
191   // Callback for playout and recording.
192   webrtc::AudioTransport* audio_callback_ RTC_GUARDED_BY(mutex_);
193 
194   bool recording_ RTC_GUARDED_BY(
195       mutex_);  // True when audio is being pushed from the instance.
196   bool playing_ RTC_GUARDED_BY(
197       mutex_);  // True when audio is being pulled by the instance.
198 
199   bool play_is_initialized_;  // True when the instance is ready to pull audio.
200   bool rec_is_initialized_;   // True when the instance is ready to push audio.
201 
202   // Input to and output from RecordedDataIsAvailable(..) makes it possible to
203   // modify the current mic level. The implementation does not care about the
204   // mic level so it just feeds back what it receives.
205   uint32_t current_mic_level_ RTC_GUARDED_BY(mutex_);
206 
207   // next_frame_time_ is updated in a non-drifting manner to indicate the next
208   // wall clock time the next frame should be generated and received. started_
209   // ensures that next_frame_time_ can be initialized properly on first call.
210   bool started_ RTC_GUARDED_BY(mutex_);
211   int64_t next_frame_time_ RTC_GUARDED_BY(process_thread_checker_);
212 
213   std::unique_ptr<rtc::Thread> process_thread_;
214 
215   // Buffer for storing samples received from the webrtc::AudioTransport.
216   char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
217   // Buffer for samples to send to the webrtc::AudioTransport.
218   char send_buffer_[kNumberSamples * kNumberBytesPerSample];
219 
220   // Counter of frames received that have samples of high enough amplitude to
221   // indicate that the frames are not faked somewhere in the audio pipeline
222   // (e.g. by a jitter buffer).
223   int frames_received_;
224 
225   // Protects variables that are accessed from process_thread_ and
226   // the main thread.
227   mutable webrtc::Mutex mutex_;
228   webrtc::SequenceChecker process_thread_checker_;
229 };
230 
231 #endif  // PC_TEST_FAKE_AUDIO_CAPTURE_MODULE_H_
232