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