henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | #include "talk/app/webrtc/test/fakeaudiocapturemodule.h" |
| 29 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 30 | #include "webrtc/base/common.h" |
| 31 | #include "webrtc/base/refcount.h" |
| 32 | #include "webrtc/base/thread.h" |
| 33 | #include "webrtc/base/timeutils.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
| 35 | // Audio sample value that is high enough that it doesn't occur naturally when |
| 36 | // frames are being faked. E.g. NetEq will not generate this large sample value |
| 37 | // unless it has received an audio frame containing a sample of this value. |
| 38 | // Even simpler buffers would likely just contain audio sample values of 0. |
| 39 | static const int kHighSampleValue = 10000; |
| 40 | |
| 41 | // Same value as src/modules/audio_device/main/source/audio_device_config.h in |
| 42 | // https://code.google.com/p/webrtc/ |
| 43 | static const uint32 kAdmMaxIdleTimeProcess = 1000; |
| 44 | |
| 45 | // Constants here are derived by running VoE using a real ADM. |
| 46 | // The constants correspond to 10ms of mono audio at 44kHz. |
| 47 | static const int kTimePerFrameMs = 10; |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 48 | static const uint8_t kNumberOfChannels = 1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | static const int kSamplesPerSecond = 44000; |
| 50 | static const int kTotalDelayMs = 0; |
| 51 | static const int kClockDriftMs = 0; |
| 52 | static const uint32_t kMaxVolume = 14392; |
| 53 | |
| 54 | enum { |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 55 | MSG_START_PROCESS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | MSG_RUN_PROCESS, |
| 57 | MSG_STOP_PROCESS, |
| 58 | }; |
| 59 | |
| 60 | FakeAudioCaptureModule::FakeAudioCaptureModule( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 61 | rtc::Thread* process_thread) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | : last_process_time_ms_(0), |
| 63 | audio_callback_(NULL), |
| 64 | recording_(false), |
| 65 | playing_(false), |
| 66 | play_is_initialized_(false), |
| 67 | rec_is_initialized_(false), |
| 68 | current_mic_level_(kMaxVolume), |
| 69 | started_(false), |
| 70 | next_frame_time_(0), |
| 71 | process_thread_(process_thread), |
| 72 | frames_received_(0) { |
| 73 | } |
| 74 | |
| 75 | FakeAudioCaptureModule::~FakeAudioCaptureModule() { |
| 76 | // Ensure that thread stops calling ProcessFrame(). |
| 77 | process_thread_->Send(this, MSG_STOP_PROCESS); |
| 78 | } |
| 79 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 80 | rtc::scoped_refptr<FakeAudioCaptureModule> FakeAudioCaptureModule::Create( |
| 81 | rtc::Thread* process_thread) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | if (process_thread == NULL) return NULL; |
| 83 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 84 | rtc::scoped_refptr<FakeAudioCaptureModule> capture_module( |
| 85 | new rtc::RefCountedObject<FakeAudioCaptureModule>(process_thread)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 86 | if (!capture_module->Initialize()) { |
| 87 | return NULL; |
| 88 | } |
| 89 | return capture_module; |
| 90 | } |
| 91 | |
| 92 | int FakeAudioCaptureModule::frames_received() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 93 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | return frames_received_; |
| 95 | } |
| 96 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 97 | int64_t FakeAudioCaptureModule::TimeUntilNextProcess() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 98 | const uint32 current_time = rtc::Time(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | if (current_time < last_process_time_ms_) { |
| 100 | // TODO: wraparound could be handled more gracefully. |
| 101 | return 0; |
| 102 | } |
| 103 | const uint32 elapsed_time = current_time - last_process_time_ms_; |
| 104 | if (kAdmMaxIdleTimeProcess < elapsed_time) { |
| 105 | return 0; |
| 106 | } |
| 107 | return kAdmMaxIdleTimeProcess - elapsed_time; |
| 108 | } |
| 109 | |
| 110 | int32_t FakeAudioCaptureModule::Process() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | last_process_time_ms_ = rtc::Time(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | int32_t FakeAudioCaptureModule::ActiveAudioLayer( |
| 116 | AudioLayer* /*audio_layer*/) const { |
| 117 | ASSERT(false); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | webrtc::AudioDeviceModule::ErrorCode FakeAudioCaptureModule::LastError() const { |
| 122 | ASSERT(false); |
| 123 | return webrtc::AudioDeviceModule::kAdmErrNone; |
| 124 | } |
| 125 | |
| 126 | int32_t FakeAudioCaptureModule::RegisterEventObserver( |
| 127 | webrtc::AudioDeviceObserver* /*event_callback*/) { |
| 128 | // Only used to report warnings and errors. This fake implementation won't |
| 129 | // generate any so discard this callback. |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int32_t FakeAudioCaptureModule::RegisterAudioCallback( |
| 134 | webrtc::AudioTransport* audio_callback) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 135 | rtc::CritScope cs(&crit_callback_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | audio_callback_ = audio_callback; |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int32_t FakeAudioCaptureModule::Init() { |
| 141 | // Initialize is called by the factory method. Safe to ignore this Init call. |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int32_t FakeAudioCaptureModule::Terminate() { |
| 146 | // Clean up in the destructor. No action here, just success. |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | bool FakeAudioCaptureModule::Initialized() const { |
| 151 | ASSERT(false); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int16_t FakeAudioCaptureModule::PlayoutDevices() { |
| 156 | ASSERT(false); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int16_t FakeAudioCaptureModule::RecordingDevices() { |
| 161 | ASSERT(false); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int32_t FakeAudioCaptureModule::PlayoutDeviceName( |
| 166 | uint16_t /*index*/, |
| 167 | char /*name*/[webrtc::kAdmMaxDeviceNameSize], |
| 168 | char /*guid*/[webrtc::kAdmMaxGuidSize]) { |
| 169 | ASSERT(false); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int32_t FakeAudioCaptureModule::RecordingDeviceName( |
| 174 | uint16_t /*index*/, |
| 175 | char /*name*/[webrtc::kAdmMaxDeviceNameSize], |
| 176 | char /*guid*/[webrtc::kAdmMaxGuidSize]) { |
| 177 | ASSERT(false); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int32_t FakeAudioCaptureModule::SetPlayoutDevice(uint16_t /*index*/) { |
| 182 | // No playout device, just playing from file. Return success. |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int32_t FakeAudioCaptureModule::SetPlayoutDevice(WindowsDeviceType /*device*/) { |
| 187 | if (play_is_initialized_) { |
| 188 | return -1; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int32_t FakeAudioCaptureModule::SetRecordingDevice(uint16_t /*index*/) { |
| 194 | // No recording device, just dropping audio. Return success. |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int32_t FakeAudioCaptureModule::SetRecordingDevice( |
| 199 | WindowsDeviceType /*device*/) { |
| 200 | if (rec_is_initialized_) { |
| 201 | return -1; |
| 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int32_t FakeAudioCaptureModule::PlayoutIsAvailable(bool* /*available*/) { |
| 207 | ASSERT(false); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int32_t FakeAudioCaptureModule::InitPlayout() { |
| 212 | play_is_initialized_ = true; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | bool FakeAudioCaptureModule::PlayoutIsInitialized() const { |
| 217 | return play_is_initialized_; |
| 218 | } |
| 219 | |
| 220 | int32_t FakeAudioCaptureModule::RecordingIsAvailable(bool* /*available*/) { |
| 221 | ASSERT(false); |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | int32_t FakeAudioCaptureModule::InitRecording() { |
| 226 | rec_is_initialized_ = true; |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | bool FakeAudioCaptureModule::RecordingIsInitialized() const { |
| 231 | ASSERT(false); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | int32_t FakeAudioCaptureModule::StartPlayout() { |
| 236 | if (!play_is_initialized_) { |
| 237 | return -1; |
| 238 | } |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 239 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 240 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 241 | playing_ = true; |
| 242 | } |
| 243 | bool start = true; |
| 244 | UpdateProcessing(start); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int32_t FakeAudioCaptureModule::StopPlayout() { |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 249 | bool start = false; |
| 250 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 251 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 252 | playing_ = false; |
| 253 | start = ShouldStartProcessing(); |
| 254 | } |
| 255 | UpdateProcessing(start); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | bool FakeAudioCaptureModule::Playing() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 260 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 261 | return playing_; |
| 262 | } |
| 263 | |
| 264 | int32_t FakeAudioCaptureModule::StartRecording() { |
| 265 | if (!rec_is_initialized_) { |
| 266 | return -1; |
| 267 | } |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 268 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 269 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 270 | recording_ = true; |
| 271 | } |
| 272 | bool start = true; |
| 273 | UpdateProcessing(start); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int32_t FakeAudioCaptureModule::StopRecording() { |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 278 | bool start = false; |
| 279 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 280 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 281 | recording_ = false; |
| 282 | start = ShouldStartProcessing(); |
| 283 | } |
| 284 | UpdateProcessing(start); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | bool FakeAudioCaptureModule::Recording() const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 289 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | return recording_; |
| 291 | } |
| 292 | |
| 293 | int32_t FakeAudioCaptureModule::SetAGC(bool /*enable*/) { |
| 294 | // No AGC but not needed since audio is pregenerated. Return success. |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | bool FakeAudioCaptureModule::AGC() const { |
| 299 | ASSERT(false); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | int32_t FakeAudioCaptureModule::SetWaveOutVolume(uint16_t /*volume_left*/, |
| 304 | uint16_t /*volume_right*/) { |
| 305 | ASSERT(false); |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | int32_t FakeAudioCaptureModule::WaveOutVolume( |
| 310 | uint16_t* /*volume_left*/, |
| 311 | uint16_t* /*volume_right*/) const { |
| 312 | ASSERT(false); |
| 313 | return 0; |
| 314 | } |
| 315 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 316 | int32_t FakeAudioCaptureModule::InitSpeaker() { |
| 317 | // No speaker, just playing from file. Return success. |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | bool FakeAudioCaptureModule::SpeakerIsInitialized() const { |
| 322 | ASSERT(false); |
| 323 | return 0; |
| 324 | } |
| 325 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 326 | int32_t FakeAudioCaptureModule::InitMicrophone() { |
| 327 | // No microphone, just playing from file. Return success. |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | bool FakeAudioCaptureModule::MicrophoneIsInitialized() const { |
| 332 | ASSERT(false); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int32_t FakeAudioCaptureModule::SpeakerVolumeIsAvailable(bool* /*available*/) { |
| 337 | ASSERT(false); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | int32_t FakeAudioCaptureModule::SetSpeakerVolume(uint32_t /*volume*/) { |
| 342 | ASSERT(false); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | int32_t FakeAudioCaptureModule::SpeakerVolume(uint32_t* /*volume*/) const { |
| 347 | ASSERT(false); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | int32_t FakeAudioCaptureModule::MaxSpeakerVolume( |
| 352 | uint32_t* /*max_volume*/) const { |
| 353 | ASSERT(false); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | int32_t FakeAudioCaptureModule::MinSpeakerVolume( |
| 358 | uint32_t* /*min_volume*/) const { |
| 359 | ASSERT(false); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | int32_t FakeAudioCaptureModule::SpeakerVolumeStepSize( |
| 364 | uint16_t* /*step_size*/) const { |
| 365 | ASSERT(false); |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | int32_t FakeAudioCaptureModule::MicrophoneVolumeIsAvailable( |
| 370 | bool* /*available*/) { |
| 371 | ASSERT(false); |
| 372 | return 0; |
| 373 | } |
| 374 | |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 375 | int32_t FakeAudioCaptureModule::SetMicrophoneVolume(uint32_t volume) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 376 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 377 | current_mic_level_ = volume; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int32_t FakeAudioCaptureModule::MicrophoneVolume(uint32_t* volume) const { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 382 | rtc::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 383 | *volume = current_mic_level_; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | int32_t FakeAudioCaptureModule::MaxMicrophoneVolume( |
| 388 | uint32_t* max_volume) const { |
| 389 | *max_volume = kMaxVolume; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | int32_t FakeAudioCaptureModule::MinMicrophoneVolume( |
| 394 | uint32_t* /*min_volume*/) const { |
| 395 | ASSERT(false); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | int32_t FakeAudioCaptureModule::MicrophoneVolumeStepSize( |
| 400 | uint16_t* /*step_size*/) const { |
| 401 | ASSERT(false); |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | int32_t FakeAudioCaptureModule::SpeakerMuteIsAvailable(bool* /*available*/) { |
| 406 | ASSERT(false); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int32_t FakeAudioCaptureModule::SetSpeakerMute(bool /*enable*/) { |
| 411 | ASSERT(false); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int32_t FakeAudioCaptureModule::SpeakerMute(bool* /*enabled*/) const { |
| 416 | ASSERT(false); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | int32_t FakeAudioCaptureModule::MicrophoneMuteIsAvailable(bool* /*available*/) { |
| 421 | ASSERT(false); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int32_t FakeAudioCaptureModule::SetMicrophoneMute(bool /*enable*/) { |
| 426 | ASSERT(false); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | int32_t FakeAudioCaptureModule::MicrophoneMute(bool* /*enabled*/) const { |
| 431 | ASSERT(false); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | int32_t FakeAudioCaptureModule::MicrophoneBoostIsAvailable( |
| 436 | bool* /*available*/) { |
| 437 | ASSERT(false); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | int32_t FakeAudioCaptureModule::SetMicrophoneBoost(bool /*enable*/) { |
| 442 | ASSERT(false); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | int32_t FakeAudioCaptureModule::MicrophoneBoost(bool* /*enabled*/) const { |
| 447 | ASSERT(false); |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | int32_t FakeAudioCaptureModule::StereoPlayoutIsAvailable( |
| 452 | bool* available) const { |
| 453 | // No recording device, just dropping audio. Stereo can be dropped just |
| 454 | // as easily as mono. |
| 455 | *available = true; |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | int32_t FakeAudioCaptureModule::SetStereoPlayout(bool /*enable*/) { |
| 460 | // No recording device, just dropping audio. Stereo can be dropped just |
| 461 | // as easily as mono. |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | int32_t FakeAudioCaptureModule::StereoPlayout(bool* /*enabled*/) const { |
| 466 | ASSERT(false); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | int32_t FakeAudioCaptureModule::StereoRecordingIsAvailable( |
| 471 | bool* available) const { |
| 472 | // Keep thing simple. No stereo recording. |
| 473 | *available = false; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | int32_t FakeAudioCaptureModule::SetStereoRecording(bool enable) { |
| 478 | if (!enable) { |
| 479 | return 0; |
| 480 | } |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | int32_t FakeAudioCaptureModule::StereoRecording(bool* /*enabled*/) const { |
| 485 | ASSERT(false); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | int32_t FakeAudioCaptureModule::SetRecordingChannel( |
| 490 | const ChannelType channel) { |
| 491 | if (channel != AudioDeviceModule::kChannelBoth) { |
| 492 | // There is no right or left in mono. I.e. kChannelBoth should be used for |
| 493 | // mono. |
| 494 | ASSERT(false); |
| 495 | return -1; |
| 496 | } |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | int32_t FakeAudioCaptureModule::RecordingChannel(ChannelType* channel) const { |
| 501 | // Stereo recording not supported. However, WebRTC ADM returns kChannelBoth |
| 502 | // in that case. Do the same here. |
| 503 | *channel = AudioDeviceModule::kChannelBoth; |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | int32_t FakeAudioCaptureModule::SetPlayoutBuffer(const BufferType /*type*/, |
| 508 | uint16_t /*size_ms*/) { |
| 509 | ASSERT(false); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | int32_t FakeAudioCaptureModule::PlayoutBuffer(BufferType* /*type*/, |
| 514 | uint16_t* /*size_ms*/) const { |
| 515 | ASSERT(false); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | int32_t FakeAudioCaptureModule::PlayoutDelay(uint16_t* delay_ms) const { |
| 520 | // No delay since audio frames are dropped. |
| 521 | *delay_ms = 0; |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | int32_t FakeAudioCaptureModule::RecordingDelay(uint16_t* /*delay_ms*/) const { |
| 526 | ASSERT(false); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | int32_t FakeAudioCaptureModule::CPULoad(uint16_t* /*load*/) const { |
| 531 | ASSERT(false); |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | int32_t FakeAudioCaptureModule::StartRawOutputFileRecording( |
| 536 | const char /*pcm_file_name_utf8*/[webrtc::kAdmMaxFileNameSize]) { |
| 537 | ASSERT(false); |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | int32_t FakeAudioCaptureModule::StopRawOutputFileRecording() { |
| 542 | ASSERT(false); |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | int32_t FakeAudioCaptureModule::StartRawInputFileRecording( |
| 547 | const char /*pcm_file_name_utf8*/[webrtc::kAdmMaxFileNameSize]) { |
| 548 | ASSERT(false); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | int32_t FakeAudioCaptureModule::StopRawInputFileRecording() { |
| 553 | ASSERT(false); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | int32_t FakeAudioCaptureModule::SetRecordingSampleRate( |
| 558 | const uint32_t /*samples_per_sec*/) { |
| 559 | ASSERT(false); |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | int32_t FakeAudioCaptureModule::RecordingSampleRate( |
| 564 | uint32_t* /*samples_per_sec*/) const { |
| 565 | ASSERT(false); |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | int32_t FakeAudioCaptureModule::SetPlayoutSampleRate( |
| 570 | const uint32_t /*samples_per_sec*/) { |
| 571 | ASSERT(false); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | int32_t FakeAudioCaptureModule::PlayoutSampleRate( |
| 576 | uint32_t* /*samples_per_sec*/) const { |
| 577 | ASSERT(false); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | int32_t FakeAudioCaptureModule::ResetAudioDevice() { |
| 582 | ASSERT(false); |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | int32_t FakeAudioCaptureModule::SetLoudspeakerStatus(bool /*enable*/) { |
| 587 | ASSERT(false); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | int32_t FakeAudioCaptureModule::GetLoudspeakerStatus(bool* /*enabled*/) const { |
| 592 | ASSERT(false); |
| 593 | return 0; |
| 594 | } |
| 595 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 596 | void FakeAudioCaptureModule::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 597 | switch (msg->message_id) { |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 598 | case MSG_START_PROCESS: |
| 599 | StartProcessP(); |
| 600 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 601 | case MSG_RUN_PROCESS: |
| 602 | ProcessFrameP(); |
| 603 | break; |
| 604 | case MSG_STOP_PROCESS: |
| 605 | StopProcessP(); |
| 606 | break; |
| 607 | default: |
| 608 | // All existing messages should be caught. Getting here should never |
| 609 | // happen. |
| 610 | ASSERT(false); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | bool FakeAudioCaptureModule::Initialize() { |
| 615 | // Set the send buffer samples high enough that it would not occur on the |
| 616 | // remote side unless a packet containing a sample of that magnitude has been |
| 617 | // sent to it. Note that the audio processing pipeline will likely distort the |
| 618 | // original signal. |
| 619 | SetSendBuffer(kHighSampleValue); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 620 | last_process_time_ms_ = rtc::Time(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
| 624 | void FakeAudioCaptureModule::SetSendBuffer(int value) { |
| 625 | Sample* buffer_ptr = reinterpret_cast<Sample*>(send_buffer_); |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame^] | 626 | const int buffer_size_in_samples = |
| 627 | sizeof(send_buffer_) / kNumberBytesPerSample; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 628 | for (int i = 0; i < buffer_size_in_samples; ++i) { |
| 629 | buffer_ptr[i] = value; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void FakeAudioCaptureModule::ResetRecBuffer() { |
| 634 | memset(rec_buffer_, 0, sizeof(rec_buffer_)); |
| 635 | } |
| 636 | |
| 637 | bool FakeAudioCaptureModule::CheckRecBuffer(int value) { |
| 638 | const Sample* buffer_ptr = reinterpret_cast<const Sample*>(rec_buffer_); |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame^] | 639 | const int buffer_size_in_samples = |
| 640 | sizeof(rec_buffer_) / kNumberBytesPerSample; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 641 | for (int i = 0; i < buffer_size_in_samples; ++i) { |
| 642 | if (buffer_ptr[i] >= value) return true; |
| 643 | } |
| 644 | return false; |
| 645 | } |
| 646 | |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 647 | bool FakeAudioCaptureModule::ShouldStartProcessing() { |
| 648 | return recording_ || playing_; |
| 649 | } |
| 650 | |
| 651 | void FakeAudioCaptureModule::UpdateProcessing(bool start) { |
| 652 | if (start) { |
| 653 | process_thread_->Post(this, MSG_START_PROCESS); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 654 | } else { |
| 655 | process_thread_->Send(this, MSG_STOP_PROCESS); |
| 656 | } |
| 657 | } |
| 658 | |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 659 | void FakeAudioCaptureModule::StartProcessP() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 660 | ASSERT(rtc::Thread::Current() == process_thread_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 661 | if (started_) { |
| 662 | // Already started. |
| 663 | return; |
| 664 | } |
| 665 | ProcessFrameP(); |
| 666 | } |
| 667 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 668 | void FakeAudioCaptureModule::ProcessFrameP() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 669 | ASSERT(rtc::Thread::Current() == process_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 670 | if (!started_) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 671 | next_frame_time_ = rtc::Time(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 672 | started_ = true; |
| 673 | } |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 674 | |
| 675 | bool playing; |
| 676 | bool recording; |
| 677 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 678 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 679 | playing = playing_; |
| 680 | recording = recording_; |
| 681 | } |
| 682 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 683 | // Receive and send frames every kTimePerFrameMs. |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 684 | if (playing) { |
| 685 | ReceiveFrameP(); |
| 686 | } |
| 687 | if (recording) { |
| 688 | SendFrameP(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | next_frame_time_ += kTimePerFrameMs; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 692 | const uint32 current_time = rtc::Time(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 693 | const uint32 wait_time = (next_frame_time_ > current_time) ? |
| 694 | next_frame_time_ - current_time : 0; |
| 695 | process_thread_->PostDelayed(wait_time, this, MSG_RUN_PROCESS); |
| 696 | } |
| 697 | |
| 698 | void FakeAudioCaptureModule::ReceiveFrameP() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 699 | ASSERT(rtc::Thread::Current() == process_thread_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 700 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 701 | rtc::CritScope cs(&crit_callback_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 702 | if (!audio_callback_) { |
| 703 | return; |
| 704 | } |
| 705 | ResetRecBuffer(); |
| 706 | uint32_t nSamplesOut = 0; |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 707 | int64_t elapsed_time_ms = 0; |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 708 | int64_t ntp_time_ms = 0; |
| 709 | if (audio_callback_->NeedMorePlayData(kNumberSamples, kNumberBytesPerSample, |
| 710 | kNumberOfChannels, kSamplesPerSecond, |
| 711 | rec_buffer_, nSamplesOut, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 712 | &elapsed_time_ms, &ntp_time_ms) != 0) { |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 713 | ASSERT(false); |
| 714 | } |
| 715 | ASSERT(nSamplesOut == kNumberSamples); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 716 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 717 | // The SetBuffer() function ensures that after decoding, the audio buffer |
| 718 | // should contain samples of similar magnitude (there is likely to be some |
| 719 | // distortion due to the audio pipeline). If one sample is detected to |
| 720 | // have the same or greater magnitude somewhere in the frame, an actual frame |
| 721 | // has been received from the remote side (i.e. faked frames are not being |
| 722 | // pulled). |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 723 | if (CheckRecBuffer(kHighSampleValue)) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 724 | rtc::CritScope cs(&crit_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 725 | ++frames_received_; |
| 726 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void FakeAudioCaptureModule::SendFrameP() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 730 | ASSERT(rtc::Thread::Current() == process_thread_); |
| 731 | rtc::CritScope cs(&crit_callback_); |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 732 | if (!audio_callback_) { |
| 733 | return; |
| 734 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 735 | bool key_pressed = false; |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 736 | uint32_t current_mic_level = 0; |
| 737 | MicrophoneVolume(¤t_mic_level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 738 | if (audio_callback_->RecordedDataIsAvailable(send_buffer_, kNumberSamples, |
| 739 | kNumberBytesPerSample, |
| 740 | kNumberOfChannels, |
| 741 | kSamplesPerSecond, kTotalDelayMs, |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 742 | kClockDriftMs, current_mic_level, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 743 | key_pressed, |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 744 | current_mic_level) != 0) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 745 | ASSERT(false); |
| 746 | } |
wu@webrtc.org | 8804a29 | 2013-10-22 23:09:20 +0000 | [diff] [blame] | 747 | SetMicrophoneVolume(current_mic_level); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void FakeAudioCaptureModule::StopProcessP() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 751 | ASSERT(rtc::Thread::Current() == process_thread_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 752 | started_ = false; |
| 753 | process_thread_->Clear(this); |
| 754 | } |