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