~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/app/webrtc/test/fakeaudiocapturemodule.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
44
#ifdef WEBRTC_RELATIVE_PATH
 
45
#include "common_types.h"
 
46
#include "modules/audio_device/main/interface/audio_device.h"
 
47
#else
 
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
 
51
 
 
52
namespace talk_base {
 
53
 
 
54
class Thread;
 
55
 
 
56
}  // namespace talk_base
 
57
 
 
58
class FakeAudioCaptureModule
 
59
    : public webrtc::AudioDeviceModule,
 
60
      public talk_base::MessageHandler {
 
61
 public:
 
62
  typedef uint16 Sample;
 
63
 
 
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)};
 
68
 
 
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);
 
74
 
 
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;
 
79
 
 
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);
 
90
 
 
91
  virtual int32_t ActiveAudioLayer(AudioLayer* audio_layer) const;
 
92
 
 
93
  virtual ErrorCode LastError() const;
 
94
  virtual int32_t RegisterEventObserver(
 
95
      webrtc::AudioDeviceObserver* event_callback);
 
96
 
 
97
  virtual int32_t RegisterAudioCallback(webrtc::AudioTransport* audio_callback);
 
98
 
 
99
  virtual int32_t Init();
 
100
  virtual int32_t Terminate();
 
101
  virtual bool Initialized() const;
 
102
 
 
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]);
 
111
 
 
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);
 
116
 
 
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;
 
123
 
 
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;
 
130
 
 
131
  virtual int32_t SetAGC(bool enable);
 
132
  virtual bool AGC() const;
 
133
 
 
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;
 
138
 
 
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;
 
145
 
 
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;
 
152
 
 
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;
 
157
 
 
158
  virtual int32_t MinMicrophoneVolume(uint32_t* min_volume) const;
 
159
  virtual int32_t MicrophoneVolumeStepSize(uint16_t* step_size) const;
 
160
 
 
161
  virtual int32_t SpeakerMuteIsAvailable(bool* available);
 
162
  virtual int32_t SetSpeakerMute(bool enable);
 
163
  virtual int32_t SpeakerMute(bool* enabled) const;
 
164
 
 
165
  virtual int32_t MicrophoneMuteIsAvailable(bool* available);
 
166
  virtual int32_t SetMicrophoneMute(bool enable);
 
167
  virtual int32_t MicrophoneMute(bool* enabled) const;
 
168
 
 
169
  virtual int32_t MicrophoneBoostIsAvailable(bool* available);
 
170
  virtual int32_t SetMicrophoneBoost(bool enable);
 
171
  virtual int32_t MicrophoneBoost(bool* enabled) const;
 
172
 
 
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;
 
181
 
 
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;
 
188
 
 
189
  virtual int32_t CPULoad(uint16_t* load) const;
 
190
 
 
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();
 
197
 
 
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;
 
202
 
 
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.
 
207
 
 
208
  // The following function is inherited from talk_base::MessageHandler.
 
209
  virtual void OnMessage(talk_base::Message* msg);
 
210
 
 
211
 protected:
 
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();
 
221
 
 
222
 private:
 
223
  // Initializes the state of the FakeAudioCaptureModule. This API is called on
 
224
  // creation by the Create() API.
 
225
  bool Initialize();
 
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
 
231
  // equal to |value|.
 
232
  bool CheckRecBuffer(int value);
 
233
 
 
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();
 
237
 
 
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
  talk_base::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
 
 
286
#endif  // TALK_APP_WEBRTC_TEST_FAKEAUDIOCAPTUREMODULE_H_