1 /* 2 * libjingle 3 * Copyright 2012, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 // This class implements an AudioCaptureModule that can be used to detect if 29 // audio is being received properly if it is fed by another AudioCaptureModule 30 // in some arbitrary audio pipeline where they are connected. It does not play 31 // out or record any audio so it does not need access to any hardware and can 32 // therefore be used in the gtest testing framework. 33 34 // Note P postfix of a function indicates that it should only be called by the 35 // processing thread. 36 37 #ifndef TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_ 38 #define TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_ 39 40 #include "talk/base/basictypes.h" 41 #include "talk/base/messagehandler.h" 42 #include "talk/base/scoped_ref_ptr.h" 43 #include "webrtc/common_types.h" 44 #include "webrtc/modules/audio_device/include/audio_device.h" 45 46 namespace talk_base { 47 48 class Thread; 49 50 } // namespace talk_base 51 52 class FakeAudioCaptureModule 53 : public webrtc::AudioDeviceModule, 54 public talk_base::MessageHandler { 55 public: 56 typedef uint16 Sample; 57 58 // The value for the following constants have been derived by running VoE 59 // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz. 60 enum{kNumberSamples = 440}; 61 enum{kNumberBytesPerSample = sizeof(Sample)}; 62 63 // Creates a FakeAudioCaptureModule or returns NULL on failure. 64 // |process_thread| is used to push and pull audio frames to and from the 65 // returned instance. Note: ownership of |process_thread| is not handed over. 66 static talk_base::scoped_refptr<FakeAudioCaptureModule> Create( 67 talk_base::Thread* process_thread); 68 69 // Returns the number of frames that have been successfully pulled by the 70 // instance. Note that correctly detecting success can only be done if the 71 // pulled frame was generated/pushed from a FakeAudioCaptureModule. 72 int frames_received() const; 73 74 // Following functions are inherited from webrtc::AudioDeviceModule. 75 // Only functions called by PeerConnection are implemented, the rest do 76 // nothing and return success. If a function is not expected to be called by 77 // PeerConnection an assertion is triggered if it is in fact called. 78 virtual int32_t Version(char* version, 79 uint32_t& remaining_buffer_in_bytes, 80 uint32_t& position) const; 81 virtual int32_t TimeUntilNextProcess(); 82 virtual int32_t Process(); 83 virtual int32_t ChangeUniqueId(const int32_t id); 84 85 virtual int32_t ActiveAudioLayer(AudioLayer* audio_layer) const; 86 87 virtual ErrorCode LastError() const; 88 virtual int32_t RegisterEventObserver( 89 webrtc::AudioDeviceObserver* event_callback); 90 91 virtual int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback); 92 93 virtual int32_t Init(); 94 virtual int32_t Terminate(); 95 virtual bool Initialized() const; 96 97 virtual int16_t PlayoutDevices(); 98 virtual int16_t RecordingDevices(); 99 virtual int32_t PlayoutDeviceName(uint16_t index, 100 char name[webrtc::kAdmMaxDeviceNameSize], 101 char guid[webrtc::kAdmMaxGuidSize]); 102 virtual int32_t RecordingDeviceName(uint16_t index, 103 char name[webrtc::kAdmMaxDeviceNameSize], 104 char guid[webrtc::kAdmMaxGuidSize]); 105 106 virtual int32_t SetPlayoutDevice(uint16_t index); 107 virtual int32_t SetPlayoutDevice(WindowsDeviceType device); 108 virtual int32_t SetRecordingDevice(uint16_t index); 109 virtual int32_t SetRecordingDevice(WindowsDeviceType device); 110 111 virtual int32_t PlayoutIsAvailable(bool* available); 112 virtual int32_t InitPlayout(); 113 virtual bool PlayoutIsInitialized() const; 114 virtual int32_t RecordingIsAvailable(bool* available); 115 virtual int32_t InitRecording(); 116 virtual bool RecordingIsInitialized() const; 117 118 virtual int32_t StartPlayout(); 119 virtual int32_t StopPlayout(); 120 virtual bool Playing() const; 121 virtual int32_t StartRecording(); 122 virtual int32_t StopRecording(); 123 virtual bool Recording() const; 124 125 virtual int32_t SetAGC(bool enable); 126 virtual bool AGC() const; 127 128 virtual int32_t SetWaveOutVolume(uint16_t volume_left, 129 uint16_t volume_right); 130 virtual int32_t WaveOutVolume(uint16_t* volume_left, 131 uint16_t* volume_right) const; 132 133 virtual int32_t SpeakerIsAvailable(bool* available); 134 virtual int32_t InitSpeaker(); 135 virtual bool SpeakerIsInitialized() const; 136 virtual int32_t MicrophoneIsAvailable(bool* available); 137 virtual int32_t InitMicrophone(); 138 virtual bool MicrophoneIsInitialized() const; 139 140 virtual int32_t SpeakerVolumeIsAvailable(bool* available); 141 virtual int32_t SetSpeakerVolume(uint32_t volume); 142 virtual int32_t SpeakerVolume(uint32_t* volume) const; 143 virtual int32_t MaxSpeakerVolume(uint32_t* max_volume) const; 144 virtual int32_t MinSpeakerVolume(uint32_t* min_volume) const; 145 virtual int32_t SpeakerVolumeStepSize(uint16_t* step_size) const; 146 147 virtual int32_t MicrophoneVolumeIsAvailable(bool* available); 148 virtual int32_t SetMicrophoneVolume(uint32_t volume); 149 virtual int32_t MicrophoneVolume(uint32_t* volume) const; 150 virtual int32_t MaxMicrophoneVolume(uint32_t* max_volume) const; 151 152 virtual int32_t MinMicrophoneVolume(uint32_t* min_volume) const; 153 virtual int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const; 154 155 virtual int32_t SpeakerMuteIsAvailable(bool* available); 156 virtual int32_t SetSpeakerMute(bool enable); 157 virtual int32_t SpeakerMute(bool* enabled) const; 158 159 virtual int32_t MicrophoneMuteIsAvailable(bool* available); 160 virtual int32_t SetMicrophoneMute(bool enable); 161 virtual int32_t MicrophoneMute(bool* enabled) const; 162 163 virtual int32_t MicrophoneBoostIsAvailable(bool* available); 164 virtual int32_t SetMicrophoneBoost(bool enable); 165 virtual int32_t MicrophoneBoost(bool* enabled) const; 166 167 virtual int32_t StereoPlayoutIsAvailable(bool* available) const; 168 virtual int32_t SetStereoPlayout(bool enable); 169 virtual int32_t StereoPlayout(bool* enabled) const; 170 virtual int32_t StereoRecordingIsAvailable(bool* available) const; 171 virtual int32_t SetStereoRecording(bool enable); 172 virtual int32_t StereoRecording(bool* enabled) const; 173 virtual int32_t SetRecordingChannel(const ChannelType channel); 174 virtual int32_t RecordingChannel(ChannelType* channel) const; 175 176 virtual int32_t SetPlayoutBuffer(const BufferType type, 177 uint16_t size_ms = 0); 178 virtual int32_t PlayoutBuffer(BufferType* type, 179 uint16_t* size_ms) const; 180 virtual int32_t PlayoutDelay(uint16_t* delay_ms) const; 181 virtual int32_t RecordingDelay(uint16_t* delay_ms) const; 182 183 virtual int32_t CPULoad(uint16_t* load) const; 184 185 virtual int32_t StartRawOutputFileRecording( 186 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]); 187 virtual int32_t StopRawOutputFileRecording(); 188 virtual int32_t StartRawInputFileRecording( 189 const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]); 190 virtual int32_t StopRawInputFileRecording(); 191 192 virtual int32_t SetRecordingSampleRate(const uint32_t samples_per_sec); 193 virtual int32_t RecordingSampleRate(uint32_t* samples_per_sec) const; 194 virtual int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec); 195 virtual int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const; 196 197 virtual int32_t ResetAudioDevice(); 198 virtual int32_t SetLoudspeakerStatus(bool enable); 199 virtual int32_t GetLoudspeakerStatus(bool* enabled) const; 200 // End of functions inherited from webrtc::AudioDeviceModule. 201 202 // The following function is inherited from talk_base::MessageHandler. 203 virtual void OnMessage(talk_base::Message* msg); 204 205 protected: 206 // The constructor is protected because the class needs to be created as a 207 // reference counted object (for memory managment reasons). It could be 208 // exposed in which case the burden of proper instantiation would be put on 209 // the creator of a FakeAudioCaptureModule instance. To create an instance of 210 // this class use the Create(..) API. 211 explicit FakeAudioCaptureModule(talk_base::Thread* process_thread); 212 // The destructor is protected because it is reference counted and should not 213 // be deleted directly. 214 virtual ~FakeAudioCaptureModule(); 215 216 private: 217 // Initializes the state of the FakeAudioCaptureModule. This API is called on 218 // creation by the Create() API. 219 bool Initialize(); 220 // SetBuffer() sets all samples in send_buffer_ to |value|. 221 void SetSendBuffer(int value); 222 // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0. 223 void ResetRecBuffer(); 224 // Returns true if rec_buffer_ contains one or more sample greater than or 225 // equal to |value|. 226 bool CheckRecBuffer(int value); 227 228 // Starts or stops the pushing and pulling of audio frames depending on if 229 // recording or playback has been enabled/started. 230 void UpdateProcessing(); 231 232 // Periodcally called function that ensures that frames are pulled and pushed 233 // periodically if enabled/started. 234 void ProcessFrameP(); 235 // Pulls frames from the registered webrtc::AudioTransport. 236 void ReceiveFrameP(); 237 // Pushes frames to the registered webrtc::AudioTransport. 238 void SendFrameP(); 239 // Stops the periodic calling of ProcessFrame() in a thread safe way. 240 void StopProcessP(); 241 242 // The time in milliseconds when Process() was last called or 0 if no call 243 // has been made. 244 uint32 last_process_time_ms_; 245 246 // Callback for playout and recording. 247 webrtc::AudioTransport* audio_callback_; 248 249 bool recording_; // True when audio is being pushed from the instance. 250 bool playing_; // True when audio is being pulled by the instance. 251 252 bool play_is_initialized_; // True when the instance is ready to pull audio. 253 bool rec_is_initialized_; // True when the instance is ready to push audio. 254 255 // Input to and output from RecordedDataIsAvailable(..) makes it possible to 256 // modify the current mic level. The implementation does not care about the 257 // mic level so it just feeds back what it receives. 258 uint32_t current_mic_level_; 259 260 // next_frame_time_ is updated in a non-drifting manner to indicate the next 261 // wall clock time the next frame should be generated and received. started_ 262 // ensures that next_frame_time_ can be initialized properly on first call. 263 bool started_; 264 uint32 next_frame_time_; 265 266 // User provided thread context. 267 talk_base::Thread* process_thread_; 268 269 // Buffer for storing samples received from the webrtc::AudioTransport. 270 char rec_buffer_[kNumberSamples * kNumberBytesPerSample]; 271 // Buffer for samples to send to the webrtc::AudioTransport. 272 char send_buffer_[kNumberSamples * kNumberBytesPerSample]; 273 274 // Counter of frames received that have samples of high enough amplitude to 275 // indicate that the frames are not faked somewhere in the audio pipeline 276 // (e.g. by a jitter buffer). 277 int frames_received_; 278 }; 279 280 #endif // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_ 281