3
* Copyright 2012, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
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.
34
// Note P postfix of a function indicates that it should only be called by the
37
#ifndef TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
38
#define TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
40
#include "talk/base/basictypes.h"
41
#include "talk/base/messagehandler.h"
42
#include "talk/base/scoped_ref_ptr.h"
44
#ifdef WEBRTC_RELATIVE_PATH
45
#include "common_types.h"
46
#include "modules/audio_device/main/interface/audio_device.h"
48
#include "third_party/webrtc/files/include/audio_device.h"
49
#include "third_party/webrtc/files/include/common_types.h"
50
#endif // WEBRTC_RELATIVE_PATH
56
} // namespace talk_base
58
class FakeAudioCaptureModule
59
: public webrtc::AudioDeviceModule,
60
public talk_base::MessageHandler {
62
typedef uint16 Sample;
64
// The value for the following constants have been derived by running VoE
65
// using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
66
enum{kNumberSamples = 440};
67
enum{kNumberBytesPerSample = sizeof(Sample)};
69
// Creates a FakeAudioCaptureModule or returns NULL on failure.
70
// |process_thread| is used to push and pull audio frames to and from the
71
// returned instance. Note: ownership of |process_thread| is not handed over.
72
static talk_base::scoped_refptr<FakeAudioCaptureModule> Create(
73
talk_base::Thread* process_thread);
75
// Returns the number of frames that have been successfully pulled by the
76
// instance. Note that correctly detecting success can only be done if the
77
// pulled frame was generated/pushed from a FakeAudioCaptureModule.
78
int frames_received() const;
80
// Following functions are inherited from webrtc::AudioDeviceModule.
81
// Only functions called by PeerConnection are implemented, the rest do
82
// nothing and return success. If a function is not expected to be called by
83
// PeerConnection an assertion is triggered if it is in fact called.
84
virtual int32_t Version(char* version,
85
uint32_t& remaining_buffer_in_bytes,
86
uint32_t& position) const;
87
virtual int32_t TimeUntilNextProcess();
88
virtual int32_t Process();
89
virtual WebRtc_Word32 ChangeUniqueId(const WebRtc_Word32 id);
91
virtual int32_t ActiveAudioLayer(AudioLayer* audio_layer) const;
93
virtual ErrorCode LastError() const;
94
virtual int32_t RegisterEventObserver(
95
webrtc::AudioDeviceObserver* event_callback);
97
virtual int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback);
99
virtual int32_t Init();
100
virtual int32_t Terminate();
101
virtual bool Initialized() const;
103
virtual int16_t PlayoutDevices();
104
virtual int16_t RecordingDevices();
105
virtual int32_t PlayoutDeviceName(uint16_t index,
106
char name[webrtc::kAdmMaxDeviceNameSize],
107
char guid[webrtc::kAdmMaxGuidSize]);
108
virtual int32_t RecordingDeviceName(uint16_t index,
109
char name[webrtc::kAdmMaxDeviceNameSize],
110
char guid[webrtc::kAdmMaxGuidSize]);
112
virtual int32_t SetPlayoutDevice(uint16_t index);
113
virtual int32_t SetPlayoutDevice(WindowsDeviceType device);
114
virtual int32_t SetRecordingDevice(uint16_t index);
115
virtual int32_t SetRecordingDevice(WindowsDeviceType device);
117
virtual int32_t PlayoutIsAvailable(bool* available);
118
virtual int32_t InitPlayout();
119
virtual bool PlayoutIsInitialized() const;
120
virtual int32_t RecordingIsAvailable(bool* available);
121
virtual int32_t InitRecording();
122
virtual bool RecordingIsInitialized() const;
124
virtual int32_t StartPlayout();
125
virtual int32_t StopPlayout();
126
virtual bool Playing() const;
127
virtual int32_t StartRecording();
128
virtual int32_t StopRecording();
129
virtual bool Recording() const;
131
virtual int32_t SetAGC(bool enable);
132
virtual bool AGC() const;
134
virtual int32_t SetWaveOutVolume(uint16_t volume_left,
135
uint16_t volume_right);
136
virtual int32_t WaveOutVolume(uint16_t* volume_left,
137
uint16_t* volume_right) const;
139
virtual int32_t SpeakerIsAvailable(bool* available);
140
virtual int32_t InitSpeaker();
141
virtual bool SpeakerIsInitialized() const;
142
virtual int32_t MicrophoneIsAvailable(bool* available);
143
virtual int32_t InitMicrophone();
144
virtual bool MicrophoneIsInitialized() const;
146
virtual int32_t SpeakerVolumeIsAvailable(bool* available);
147
virtual int32_t SetSpeakerVolume(uint32_t volume);
148
virtual int32_t SpeakerVolume(uint32_t* volume) const;
149
virtual int32_t MaxSpeakerVolume(uint32_t* max_volume) const;
150
virtual int32_t MinSpeakerVolume(uint32_t* min_volume) const;
151
virtual int32_t SpeakerVolumeStepSize(uint16_t* step_size) const;
153
virtual int32_t MicrophoneVolumeIsAvailable(bool* available);
154
virtual int32_t SetMicrophoneVolume(uint32_t volume);
155
virtual int32_t MicrophoneVolume(uint32_t* volume) const;
156
virtual int32_t MaxMicrophoneVolume(uint32_t* max_volume) const;
158
virtual int32_t MinMicrophoneVolume(uint32_t* min_volume) const;
159
virtual int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const;
161
virtual int32_t SpeakerMuteIsAvailable(bool* available);
162
virtual int32_t SetSpeakerMute(bool enable);
163
virtual int32_t SpeakerMute(bool* enabled) const;
165
virtual int32_t MicrophoneMuteIsAvailable(bool* available);
166
virtual int32_t SetMicrophoneMute(bool enable);
167
virtual int32_t MicrophoneMute(bool* enabled) const;
169
virtual int32_t MicrophoneBoostIsAvailable(bool* available);
170
virtual int32_t SetMicrophoneBoost(bool enable);
171
virtual int32_t MicrophoneBoost(bool* enabled) const;
173
virtual int32_t StereoPlayoutIsAvailable(bool* available) const;
174
virtual int32_t SetStereoPlayout(bool enable);
175
virtual int32_t StereoPlayout(bool* enabled) const;
176
virtual int32_t StereoRecordingIsAvailable(bool* available) const;
177
virtual int32_t SetStereoRecording(bool enable);
178
virtual int32_t StereoRecording(bool* enabled) const;
179
virtual int32_t SetRecordingChannel(const ChannelType channel);
180
virtual int32_t RecordingChannel(ChannelType* channel) const;
182
virtual int32_t SetPlayoutBuffer(const BufferType type,
183
uint16_t size_ms = 0);
184
virtual int32_t PlayoutBuffer(BufferType* type,
185
uint16_t* size_ms) const;
186
virtual int32_t PlayoutDelay(uint16_t* delay_ms) const;
187
virtual int32_t RecordingDelay(uint16_t* delay_ms) const;
189
virtual int32_t CPULoad(uint16_t* load) const;
191
virtual int32_t StartRawOutputFileRecording(
192
const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]);
193
virtual int32_t StopRawOutputFileRecording();
194
virtual int32_t StartRawInputFileRecording(
195
const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]);
196
virtual int32_t StopRawInputFileRecording();
198
virtual int32_t SetRecordingSampleRate(const uint32_t samples_per_sec);
199
virtual int32_t RecordingSampleRate(uint32_t* samples_per_sec) const;
200
virtual int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec);
201
virtual int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const;
203
virtual int32_t ResetAudioDevice();
204
virtual int32_t SetLoudspeakerStatus(bool enable);
205
virtual int32_t GetLoudspeakerStatus(bool* enabled) const;
206
// End of functions inherited from webrtc::AudioDeviceModule.
208
// The following function is inherited from talk_base::MessageHandler.
209
virtual void OnMessage(talk_base::Message* msg);
212
// The constructor is protected because the class needs to be created as a
213
// reference counted object (for memory managment reasons). It could be
214
// exposed in which case the burden of proper instantiation would be put on
215
// the creator of a FakeAudioCaptureModule instance. To create an instance of
216
// this class use the Create(..) API.
217
explicit FakeAudioCaptureModule(talk_base::Thread* process_thread);
218
// The destructor is protected because it is reference counted and should not
219
// be deleted directly.
220
virtual ~FakeAudioCaptureModule();
223
// Initializes the state of the FakeAudioCaptureModule. This API is called on
224
// creation by the Create() API.
226
// SetBuffer() sets all samples in send_buffer_ to |value|.
227
void SetSendBuffer(int value);
228
// Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
229
void ResetRecBuffer();
230
// Returns true if rec_buffer_ contains one or more sample greater than or
232
bool CheckRecBuffer(int value);
234
// Starts or stops the pushing and pulling of audio frames depending on if
235
// recording or playback has been enabled/started.
236
void UpdateProcessing();
238
// Periodcally called function that ensures that frames are pulled and pushed
239
// periodically if enabled/started.
240
void ProcessFrameP();
241
// Pulls frames from the registered webrtc::AudioTransport.
242
void ReceiveFrameP();
243
// Pushes frames to the registered webrtc::AudioTransport.
245
// Stops the periodic calling of ProcessFrame() in a thread safe way.
248
// The time in milliseconds when Process() was last called or 0 if no call
250
uint32 last_process_time_ms_;
252
// Callback for playout and recording.
253
webrtc::AudioTransport* audio_callback_;
255
bool recording_; // True when audio is being pushed from the instance.
256
bool playing_; // True when audio is being pulled by the instance.
258
bool play_is_initialized_; // True when the instance is ready to pull audio.
259
bool rec_is_initialized_; // True when the instance is ready to push audio.
261
// Input to and output from RecordedDataIsAvailable(..) makes it possible to
262
// modify the current mic level. The implementation does not care about the
263
// mic level so it just feeds back what it receives.
264
uint32_t current_mic_level_;
266
// next_frame_time_ is updated in a non-drifting manner to indicate the next
267
// wall clock time the next frame should be generated and received. started_
268
// ensures that next_frame_time_ can be initialized properly on first call.
270
uint32 next_frame_time_;
272
// User provided thread context.
273
talk_base::Thread* process_thread_;
275
// Buffer for storing samples received from the webrtc::AudioTransport.
276
char rec_buffer_[kNumberSamples * kNumberBytesPerSample];
277
// Buffer for samples to send to the webrtc::AudioTransport.
278
char send_buffer_[kNumberSamples * kNumberBytesPerSample];
280
// Counter of frames received that have samples of high enough amplitude to
281
// indicate that the frames are not faked somewhere in the audio pipeline
282
// (e.g. by a jitter buffer).
283
int frames_received_;
286
#endif // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_