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 <memory> 24 25 #include "api/scoped_refptr.h" 26 #include "modules/audio_device/include/audio_device.h" 27 #include "rtc_base/message_handler.h" 28 #include "rtc_base/synchronization/mutex.h" 29 #include "rtc_base/synchronization/sequence_checker.h" 30 31 namespace rtc { 32 class Thread; 33 } // namespace rtc 34 35 class FakeAudioCaptureModule : public webrtc::AudioDeviceModule, 36 public rtc::MessageHandler { 37 public: 38 typedef uint16_t Sample; 39 40 // The value for the following constants have been derived by running VoE 41 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz. 42 static const size_t kNumberSamples = 440; 43 static const size_t kNumberBytesPerSample = sizeof(Sample); 44 45 // Creates a FakeAudioCaptureModule or returns NULL on failure. 46 static rtc::scoped_refptr<FakeAudioCaptureModule> Create(); 47 48 // Returns the number of frames that have been successfully pulled by the 49 // instance. Note that correctly detecting success can only be done if the 50 // pulled frame was generated/pushed from a FakeAudioCaptureModule. 51 int frames_received() const RTC_LOCKS_EXCLUDED(mutex_); 52 53 int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override; 54 55 // Note: Calling this method from a callback may result in deadlock. 56 int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback) override 57 RTC_LOCKS_EXCLUDED(mutex_); 58 59 int32_t Init() override; 60 int32_t Terminate() override; 61 bool Initialized() const override; 62 63 int16_t PlayoutDevices() override; 64 int16_t RecordingDevices() override; 65 int32_t PlayoutDeviceName(uint16_t index, 66 char name[webrtc::kAdmMaxDeviceNameSize], 67 char guid[webrtc::kAdmMaxGuidSize]) override; 68 int32_t RecordingDeviceName(uint16_t index, 69 char name[webrtc::kAdmMaxDeviceNameSize], 70 char guid[webrtc::kAdmMaxGuidSize]) override; 71 72 int32_t SetPlayoutDevice(uint16_t index) override; 73 int32_t SetPlayoutDevice(WindowsDeviceType device) override; 74 int32_t SetRecordingDevice(uint16_t index) override; 75 int32_t SetRecordingDevice(WindowsDeviceType device) override; 76 77 int32_t PlayoutIsAvailable(bool* available) override; 78 int32_t InitPlayout() override; 79 bool PlayoutIsInitialized() const override; 80 int32_t RecordingIsAvailable(bool* available) override; 81 int32_t InitRecording() override; 82 bool RecordingIsInitialized() const override; 83 84 int32_t StartPlayout() RTC_LOCKS_EXCLUDED(mutex_) override; 85 int32_t StopPlayout() RTC_LOCKS_EXCLUDED(mutex_) override; 86 bool Playing() const RTC_LOCKS_EXCLUDED(mutex_) override; 87 int32_t StartRecording() RTC_LOCKS_EXCLUDED(mutex_) override; 88 int32_t StopRecording() RTC_LOCKS_EXCLUDED(mutex_) override; 89 bool Recording() const RTC_LOCKS_EXCLUDED(mutex_) override; 90 91 int32_t InitSpeaker() override; 92 bool SpeakerIsInitialized() const override; 93 int32_t InitMicrophone() override; 94 bool MicrophoneIsInitialized() const override; 95 96 int32_t SpeakerVolumeIsAvailable(bool* available) override; 97 int32_t SetSpeakerVolume(uint32_t volume) override; 98 int32_t SpeakerVolume(uint32_t* volume) const override; 99 int32_t MaxSpeakerVolume(uint32_t* max_volume) const override; 100 int32_t MinSpeakerVolume(uint32_t* min_volume) const override; 101 102 int32_t MicrophoneVolumeIsAvailable(bool* available) override; 103 int32_t SetMicrophoneVolume(uint32_t volume) 104 RTC_LOCKS_EXCLUDED(mutex_) override; 105 int32_t MicrophoneVolume(uint32_t* volume) const 106 RTC_LOCKS_EXCLUDED(mutex_) override; 107 int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override; 108 109 int32_t MinMicrophoneVolume(uint32_t* min_volume) const override; 110 111 int32_t SpeakerMuteIsAvailable(bool* available) override; 112 int32_t SetSpeakerMute(bool enable) override; 113 int32_t SpeakerMute(bool* enabled) const override; 114 115 int32_t MicrophoneMuteIsAvailable(bool* available) override; 116 int32_t SetMicrophoneMute(bool enable) override; 117 int32_t MicrophoneMute(bool* enabled) const override; 118 119 int32_t StereoPlayoutIsAvailable(bool* available) const override; 120 int32_t SetStereoPlayout(bool enable) override; 121 int32_t StereoPlayout(bool* enabled) const override; 122 int32_t StereoRecordingIsAvailable(bool* available) const override; 123 int32_t SetStereoRecording(bool enable) override; 124 int32_t StereoRecording(bool* enabled) const override; 125 126 int32_t PlayoutDelay(uint16_t* delay_ms) const override; 127 BuiltInAECIsAvailable()128 bool BuiltInAECIsAvailable() const override { return false; } EnableBuiltInAEC(bool enable)129 int32_t EnableBuiltInAEC(bool enable) override { return -1; } BuiltInAGCIsAvailable()130 bool BuiltInAGCIsAvailable() const override { return false; } EnableBuiltInAGC(bool enable)131 int32_t EnableBuiltInAGC(bool enable) override { return -1; } BuiltInNSIsAvailable()132 bool BuiltInNSIsAvailable() const override { return false; } EnableBuiltInNS(bool enable)133 int32_t EnableBuiltInNS(bool enable) override { return -1; } 134 GetPlayoutUnderrunCount()135 int32_t GetPlayoutUnderrunCount() const override { return -1; } 136 #if defined(WEBRTC_IOS) GetPlayoutAudioParameters(webrtc::AudioParameters * params)137 int GetPlayoutAudioParameters( 138 webrtc::AudioParameters* params) const override { 139 return -1; 140 } GetRecordAudioParameters(webrtc::AudioParameters * params)141 int GetRecordAudioParameters(webrtc::AudioParameters* params) const override { 142 return -1; 143 } 144 #endif // WEBRTC_IOS 145 146 // End of functions inherited from webrtc::AudioDeviceModule. 147 148 // The following function is inherited from rtc::MessageHandler. 149 void OnMessage(rtc::Message* msg) override; 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