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 "webrtc/base/basictypes.h"
41#include "webrtc/base/criticalsection.h"
42#include "webrtc/base/messagehandler.h"
43#include "webrtc/base/scoped_ref_ptr.h"
44#include "webrtc/common_types.h"
45#include "webrtc/modules/audio_device/include/audio_device.h"
46
47namespace rtc {
48
49class Thread;
50
51}  // namespace rtc
52
53class FakeAudioCaptureModule
54    : public webrtc::AudioDeviceModule,
55      public rtc::MessageHandler {
56 public:
57  typedef uint16 Sample;
58
59  // The value for the following constants have been derived by running VoE
60  // using a real ADM. The constants correspond to 10ms of mono audio at 44kHz.
61  enum{kNumberSamples = 440};
62  enum{kNumberBytesPerSample = sizeof(Sample)};
63
64  // Creates a FakeAudioCaptureModule or returns NULL on failure.
65  // |process_thread| is used to push and pull audio frames to and from the
66  // returned instance. Note: ownership of |process_thread| is not handed over.
67  static rtc::scoped_refptr<FakeAudioCaptureModule> Create(
68      rtc::Thread* process_thread);
69
70  // Returns the number of frames that have been successfully pulled by the
71  // instance. Note that correctly detecting success can only be done if the
72  // pulled frame was generated/pushed from a FakeAudioCaptureModule.
73  int frames_received() const;
74
75  // Following functions are inherited from webrtc::AudioDeviceModule.
76  // Only functions called by PeerConnection are implemented, the rest do
77  // nothing and return success. If a function is not expected to be called by
78  // PeerConnection an assertion is triggered if it is in fact called.
79  virtual int32_t TimeUntilNextProcess() OVERRIDE;
80  virtual int32_t Process() OVERRIDE;
81  virtual int32_t ChangeUniqueId(const int32_t id) OVERRIDE;
82
83  virtual int32_t ActiveAudioLayer(AudioLayer* audio_layer) const OVERRIDE;
84
85  virtual ErrorCode LastError() const OVERRIDE;
86  virtual int32_t RegisterEventObserver(
87      webrtc::AudioDeviceObserver* event_callback) OVERRIDE;
88
89  // Note: Calling this method from a callback may result in deadlock.
90  virtual int32_t RegisterAudioCallback(
91      webrtc::AudioTransport* audio_callback) OVERRIDE;
92
93  virtual int32_t Init() OVERRIDE;
94  virtual int32_t Terminate() OVERRIDE;
95  virtual bool Initialized() const OVERRIDE;
96
97  virtual int16_t PlayoutDevices() OVERRIDE;
98  virtual int16_t RecordingDevices() OVERRIDE;
99  virtual int32_t PlayoutDeviceName(
100      uint16_t index,
101      char name[webrtc::kAdmMaxDeviceNameSize],
102      char guid[webrtc::kAdmMaxGuidSize]) OVERRIDE;
103  virtual int32_t RecordingDeviceName(
104      uint16_t index,
105      char name[webrtc::kAdmMaxDeviceNameSize],
106      char guid[webrtc::kAdmMaxGuidSize]) OVERRIDE;
107
108  virtual int32_t SetPlayoutDevice(uint16_t index) OVERRIDE;
109  virtual int32_t SetPlayoutDevice(WindowsDeviceType device) OVERRIDE;
110  virtual int32_t SetRecordingDevice(uint16_t index) OVERRIDE;
111  virtual int32_t SetRecordingDevice(WindowsDeviceType device) OVERRIDE;
112
113  virtual int32_t PlayoutIsAvailable(bool* available) OVERRIDE;
114  virtual int32_t InitPlayout() OVERRIDE;
115  virtual bool PlayoutIsInitialized() const OVERRIDE;
116  virtual int32_t RecordingIsAvailable(bool* available) OVERRIDE;
117  virtual int32_t InitRecording() OVERRIDE;
118  virtual bool RecordingIsInitialized() const OVERRIDE;
119
120  virtual int32_t StartPlayout() OVERRIDE;
121  virtual int32_t StopPlayout() OVERRIDE;
122  virtual bool Playing() const OVERRIDE;
123  virtual int32_t StartRecording() OVERRIDE;
124  virtual int32_t StopRecording() OVERRIDE;
125  virtual bool Recording() const OVERRIDE;
126
127  virtual int32_t SetAGC(bool enable) OVERRIDE;
128  virtual bool AGC() const OVERRIDE;
129
130  virtual int32_t SetWaveOutVolume(uint16_t volume_left,
131                                   uint16_t volume_right) OVERRIDE;
132  virtual int32_t WaveOutVolume(uint16_t* volume_left,
133                                uint16_t* volume_right) const OVERRIDE;
134
135  virtual int32_t InitSpeaker() OVERRIDE;
136  virtual bool SpeakerIsInitialized() const OVERRIDE;
137  virtual int32_t InitMicrophone() OVERRIDE;
138  virtual bool MicrophoneIsInitialized() const OVERRIDE;
139
140  virtual int32_t SpeakerVolumeIsAvailable(bool* available) OVERRIDE;
141  virtual int32_t SetSpeakerVolume(uint32_t volume) OVERRIDE;
142  virtual int32_t SpeakerVolume(uint32_t* volume) const OVERRIDE;
143  virtual int32_t MaxSpeakerVolume(uint32_t* max_volume) const OVERRIDE;
144  virtual int32_t MinSpeakerVolume(uint32_t* min_volume) const OVERRIDE;
145  virtual int32_t SpeakerVolumeStepSize(uint16_t* step_size) const OVERRIDE;
146
147  virtual int32_t MicrophoneVolumeIsAvailable(bool* available) OVERRIDE;
148  virtual int32_t SetMicrophoneVolume(uint32_t volume) OVERRIDE;
149  virtual int32_t MicrophoneVolume(uint32_t* volume) const OVERRIDE;
150  virtual int32_t MaxMicrophoneVolume(uint32_t* max_volume) const OVERRIDE;
151
152  virtual int32_t MinMicrophoneVolume(uint32_t* min_volume) const OVERRIDE;
153  virtual int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const OVERRIDE;
154
155  virtual int32_t SpeakerMuteIsAvailable(bool* available) OVERRIDE;
156  virtual int32_t SetSpeakerMute(bool enable) OVERRIDE;
157  virtual int32_t SpeakerMute(bool* enabled) const OVERRIDE;
158
159  virtual int32_t MicrophoneMuteIsAvailable(bool* available) OVERRIDE;
160  virtual int32_t SetMicrophoneMute(bool enable) OVERRIDE;
161  virtual int32_t MicrophoneMute(bool* enabled) const OVERRIDE;
162
163  virtual int32_t MicrophoneBoostIsAvailable(bool* available) OVERRIDE;
164  virtual int32_t SetMicrophoneBoost(bool enable) OVERRIDE;
165  virtual int32_t MicrophoneBoost(bool* enabled) const OVERRIDE;
166
167  virtual int32_t StereoPlayoutIsAvailable(bool* available) const OVERRIDE;
168  virtual int32_t SetStereoPlayout(bool enable) OVERRIDE;
169  virtual int32_t StereoPlayout(bool* enabled) const OVERRIDE;
170  virtual int32_t StereoRecordingIsAvailable(bool* available) const OVERRIDE;
171  virtual int32_t SetStereoRecording(bool enable) OVERRIDE;
172  virtual int32_t StereoRecording(bool* enabled) const OVERRIDE;
173  virtual int32_t SetRecordingChannel(const ChannelType channel) OVERRIDE;
174  virtual int32_t RecordingChannel(ChannelType* channel) const OVERRIDE;
175
176  virtual int32_t SetPlayoutBuffer(const BufferType type,
177                                   uint16_t size_ms = 0) OVERRIDE;
178  virtual int32_t PlayoutBuffer(BufferType* type,
179                                uint16_t* size_ms) const OVERRIDE;
180  virtual int32_t PlayoutDelay(uint16_t* delay_ms) const OVERRIDE;
181  virtual int32_t RecordingDelay(uint16_t* delay_ms) const OVERRIDE;
182
183  virtual int32_t CPULoad(uint16_t* load) const OVERRIDE;
184
185  virtual int32_t StartRawOutputFileRecording(
186      const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) OVERRIDE;
187  virtual int32_t StopRawOutputFileRecording() OVERRIDE;
188  virtual int32_t StartRawInputFileRecording(
189      const char pcm_file_name_utf8[webrtc::kAdmMaxFileNameSize]) OVERRIDE;
190  virtual int32_t StopRawInputFileRecording() OVERRIDE;
191
192  virtual int32_t SetRecordingSampleRate(
193      const uint32_t samples_per_sec) OVERRIDE;
194  virtual int32_t RecordingSampleRate(uint32_t* samples_per_sec) const OVERRIDE;
195  virtual int32_t SetPlayoutSampleRate(const uint32_t samples_per_sec) OVERRIDE;
196  virtual int32_t PlayoutSampleRate(uint32_t* samples_per_sec) const OVERRIDE;
197
198  virtual int32_t ResetAudioDevice() OVERRIDE;
199  virtual int32_t SetLoudspeakerStatus(bool enable) OVERRIDE;
200  virtual int32_t GetLoudspeakerStatus(bool* enabled) const OVERRIDE;
201  // End of functions inherited from webrtc::AudioDeviceModule.
202
203  // The following function is inherited from rtc::MessageHandler.
204  virtual void OnMessage(rtc::Message* msg) OVERRIDE;
205
206 protected:
207  // The constructor is protected because the class needs to be created as a
208  // reference counted object (for memory managment reasons). It could be
209  // exposed in which case the burden of proper instantiation would be put on
210  // the creator of a FakeAudioCaptureModule instance. To create an instance of
211  // this class use the Create(..) API.
212  explicit FakeAudioCaptureModule(rtc::Thread* process_thread);
213  // The destructor is protected because it is reference counted and should not
214  // be deleted directly.
215  virtual ~FakeAudioCaptureModule();
216
217 private:
218  // Initializes the state of the FakeAudioCaptureModule. This API is called on
219  // creation by the Create() API.
220  bool Initialize();
221  // SetBuffer() sets all samples in send_buffer_ to |value|.
222  void SetSendBuffer(int value);
223  // Resets rec_buffer_. I.e., sets all rec_buffer_ samples to 0.
224  void ResetRecBuffer();
225  // Returns true if rec_buffer_ contains one or more sample greater than or
226  // equal to |value|.
227  bool CheckRecBuffer(int value);
228
229  // Returns true/false depending on if recording or playback has been
230  // enabled/started.
231  bool ShouldStartProcessing();
232
233  // Starts or stops the pushing and pulling of audio frames.
234  void UpdateProcessing(bool start);
235
236  // Starts the periodic calling of ProcessFrame() in a thread safe way.
237  void StartProcessP();
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.
244  void SendFrameP();
245  // Stops the periodic calling of ProcessFrame() in a thread safe way.
246  void StopProcessP();
247
248  // The time in milliseconds when Process() was last called or 0 if no call
249  // has been made.
250  uint32 last_process_time_ms_;
251
252  // Callback for playout and recording.
253  webrtc::AudioTransport* audio_callback_;
254
255  bool recording_; // True when audio is being pushed from the instance.
256  bool playing_; // True when audio is being pulled by the instance.
257
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.
260
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_;
265
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.
269  bool started_;
270  uint32 next_frame_time_;
271
272  // User provided thread context.
273  rtc::Thread* process_thread_;
274
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];
279
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_;
284
285  // Protects variables that are accessed from process_thread_ and
286  // the main thread.
287  mutable rtc::CriticalSection crit_;
288  // Protects |audio_callback_| that is accessed from process_thread_ and
289  // the main thread.
290  rtc::CriticalSection crit_callback_;
291};
292
293#endif  // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_
294