1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
/*
* Copyright 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/test/fake_audio_capture_module.h"
#include <string.h>
#include <cstdint>
#include "api/audio/audio_device_defines.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "api/sequence_checker.h"
#include "api/units/time_delta.h"
#include "rtc_base/checks.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/thread.h"
#include "rtc_base/time_utils.h"
using ::webrtc::TimeDelta;
// Audio sample value that is high enough that it doesn't occur naturally when
// frames are being faked. E.g. NetEq will not generate this large sample value
// unless it has received an audio frame containing a sample of this value.
// Even simpler buffers would likely just contain audio sample values of 0.
static const int kHighSampleValue = 10000;
// Constants here are derived by running VoE using a real ADM.
// The constants correspond to 10ms of mono audio at 44kHz.
static const int kTimePerFrameMs = 10;
static const uint8_t kNumberOfChannels = 1;
static const int kSamplesPerSecond = 44000;
static const int kTotalDelayMs = 0;
static const int kClockDriftMs = 0;
static const uint32_t kMaxVolume = 14392;
FakeAudioCaptureModule::FakeAudioCaptureModule()
: audio_callback_(nullptr),
recording_(false),
playing_(false),
play_is_initialized_(false),
rec_is_initialized_(false),
current_mic_level_(kMaxVolume),
started_(false),
next_frame_time_(0),
frames_received_(0) {}
FakeAudioCaptureModule::~FakeAudioCaptureModule() {
if (process_thread_) {
process_thread_->Stop();
}
}
webrtc::scoped_refptr<FakeAudioCaptureModule> FakeAudioCaptureModule::Create() {
auto capture_module = webrtc::make_ref_counted<FakeAudioCaptureModule>();
if (!capture_module->Initialize()) {
return nullptr;
}
return capture_module;
}
int FakeAudioCaptureModule::frames_received() const {
webrtc::MutexLock lock(&mutex_);
return frames_received_;
}
int32_t FakeAudioCaptureModule::ActiveAudioLayer(
AudioLayer* /*audio_layer*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::RegisterAudioCallback(
webrtc::AudioTransport* audio_callback) {
webrtc::MutexLock lock(&mutex_);
audio_callback_ = audio_callback;
return 0;
}
int32_t FakeAudioCaptureModule::Init() {
// Initialize is called by the factory method. Safe to ignore this Init call.
return 0;
}
int32_t FakeAudioCaptureModule::Terminate() {
// Clean up in the destructor. No action here, just success.
return 0;
}
bool FakeAudioCaptureModule::Initialized() const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int16_t FakeAudioCaptureModule::PlayoutDevices() {
RTC_DCHECK_NOTREACHED();
return 0;
}
int16_t FakeAudioCaptureModule::RecordingDevices() {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::PlayoutDeviceName(
uint16_t /*index*/,
char /*name*/[webrtc::kAdmMaxDeviceNameSize],
char /*guid*/[webrtc::kAdmMaxGuidSize]) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::RecordingDeviceName(
uint16_t /*index*/,
char /*name*/[webrtc::kAdmMaxDeviceNameSize],
char /*guid*/[webrtc::kAdmMaxGuidSize]) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SetPlayoutDevice(uint16_t /*index*/) {
// No playout device, just playing from file. Return success.
return 0;
}
int32_t FakeAudioCaptureModule::SetPlayoutDevice(WindowsDeviceType /*device*/) {
if (play_is_initialized_) {
return -1;
}
return 0;
}
int32_t FakeAudioCaptureModule::SetRecordingDevice(uint16_t /*index*/) {
// No recording device, just dropping audio. Return success.
return 0;
}
int32_t FakeAudioCaptureModule::SetRecordingDevice(
WindowsDeviceType /*device*/) {
if (rec_is_initialized_) {
return -1;
}
return 0;
}
int32_t FakeAudioCaptureModule::PlayoutIsAvailable(bool* /*available*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::InitPlayout() {
play_is_initialized_ = true;
return 0;
}
bool FakeAudioCaptureModule::PlayoutIsInitialized() const {
return play_is_initialized_;
}
int32_t FakeAudioCaptureModule::RecordingIsAvailable(bool* /*available*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::InitRecording() {
rec_is_initialized_ = true;
return 0;
}
bool FakeAudioCaptureModule::RecordingIsInitialized() const {
return rec_is_initialized_;
}
int32_t FakeAudioCaptureModule::StartPlayout() {
if (!play_is_initialized_) {
return -1;
}
{
webrtc::MutexLock lock(&mutex_);
playing_ = true;
}
bool start = true;
UpdateProcessing(start);
return 0;
}
int32_t FakeAudioCaptureModule::StopPlayout() {
bool start = false;
{
webrtc::MutexLock lock(&mutex_);
playing_ = false;
start = ShouldStartProcessing();
}
UpdateProcessing(start);
return 0;
}
bool FakeAudioCaptureModule::Playing() const {
webrtc::MutexLock lock(&mutex_);
return playing_;
}
int32_t FakeAudioCaptureModule::StartRecording() {
if (!rec_is_initialized_) {
return -1;
}
{
webrtc::MutexLock lock(&mutex_);
recording_ = true;
}
bool start = true;
UpdateProcessing(start);
return 0;
}
int32_t FakeAudioCaptureModule::StopRecording() {
bool start = false;
{
webrtc::MutexLock lock(&mutex_);
recording_ = false;
start = ShouldStartProcessing();
}
UpdateProcessing(start);
return 0;
}
bool FakeAudioCaptureModule::Recording() const {
webrtc::MutexLock lock(&mutex_);
return recording_;
}
int32_t FakeAudioCaptureModule::InitSpeaker() {
// No speaker, just playing from file. Return success.
return 0;
}
bool FakeAudioCaptureModule::SpeakerIsInitialized() const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::InitMicrophone() {
// No microphone, just playing from file. Return success.
return 0;
}
bool FakeAudioCaptureModule::MicrophoneIsInitialized() const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SpeakerVolumeIsAvailable(bool* /*available*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SetSpeakerVolume(uint32_t /*volume*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SpeakerVolume(uint32_t* /*volume*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::MaxSpeakerVolume(
uint32_t* /*max_volume*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::MinSpeakerVolume(
uint32_t* /*min_volume*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::MicrophoneVolumeIsAvailable(
bool* /*available*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SetMicrophoneVolume(uint32_t volume) {
webrtc::MutexLock lock(&mutex_);
current_mic_level_ = volume;
return 0;
}
int32_t FakeAudioCaptureModule::MicrophoneVolume(uint32_t* volume) const {
webrtc::MutexLock lock(&mutex_);
*volume = current_mic_level_;
return 0;
}
int32_t FakeAudioCaptureModule::MaxMicrophoneVolume(
uint32_t* max_volume) const {
*max_volume = kMaxVolume;
return 0;
}
int32_t FakeAudioCaptureModule::MinMicrophoneVolume(
uint32_t* /*min_volume*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SpeakerMuteIsAvailable(bool* /*available*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SetSpeakerMute(bool /*enable*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SpeakerMute(bool* /*enabled*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::MicrophoneMuteIsAvailable(bool* /*available*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::SetMicrophoneMute(bool /*enable*/) {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::MicrophoneMute(bool* /*enabled*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::StereoPlayoutIsAvailable(
bool* available) const {
// No recording device, just dropping audio. Stereo can be dropped just
// as easily as mono.
*available = true;
return 0;
}
int32_t FakeAudioCaptureModule::SetStereoPlayout(bool /*enable*/) {
// No recording device, just dropping audio. Stereo can be dropped just
// as easily as mono.
return 0;
}
int32_t FakeAudioCaptureModule::StereoPlayout(bool* /*enabled*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::StereoRecordingIsAvailable(
bool* available) const {
// Keep thing simple. No stereo recording.
*available = false;
return 0;
}
int32_t FakeAudioCaptureModule::SetStereoRecording(bool enable) {
if (!enable) {
return 0;
}
return -1;
}
int32_t FakeAudioCaptureModule::StereoRecording(bool* /*enabled*/) const {
RTC_DCHECK_NOTREACHED();
return 0;
}
int32_t FakeAudioCaptureModule::PlayoutDelay(uint16_t* delay_ms) const {
// No delay since audio frames are dropped.
*delay_ms = 0;
return 0;
}
bool FakeAudioCaptureModule::Initialize() {
// Set the send buffer samples high enough that it would not occur on the
// remote side unless a packet containing a sample of that magnitude has been
// sent to it. Note that the audio processing pipeline will likely distort the
// original signal.
SetSendBuffer(kHighSampleValue);
return true;
}
void FakeAudioCaptureModule::SetSendBuffer(int value) {
Sample* buffer_ptr = reinterpret_cast<Sample*>(send_buffer_);
const size_t buffer_size_in_samples =
sizeof(send_buffer_) / kNumberBytesPerSample;
for (size_t i = 0; i < buffer_size_in_samples; ++i) {
buffer_ptr[i] = value;
}
}
void FakeAudioCaptureModule::ResetRecBuffer() {
memset(rec_buffer_, 0, sizeof(rec_buffer_));
}
bool FakeAudioCaptureModule::CheckRecBuffer(int value) {
const Sample* buffer_ptr = reinterpret_cast<const Sample*>(rec_buffer_);
const size_t buffer_size_in_samples =
sizeof(rec_buffer_) / kNumberBytesPerSample;
for (size_t i = 0; i < buffer_size_in_samples; ++i) {
if (buffer_ptr[i] >= value)
return true;
}
return false;
}
bool FakeAudioCaptureModule::ShouldStartProcessing() {
return recording_ || playing_;
}
void FakeAudioCaptureModule::UpdateProcessing(bool start) {
if (start) {
if (!process_thread_) {
process_thread_ = webrtc::Thread::Create();
process_thread_->Start();
}
process_thread_->PostTask([this] { StartProcessP(); });
} else {
if (process_thread_) {
process_thread_->Stop();
process_thread_.reset(nullptr);
process_thread_checker_.Detach();
}
webrtc::MutexLock lock(&mutex_);
started_ = false;
}
}
void FakeAudioCaptureModule::StartProcessP() {
RTC_DCHECK_RUN_ON(&process_thread_checker_);
{
webrtc::MutexLock lock(&mutex_);
if (started_) {
// Already started.
return;
}
}
ProcessFrameP();
}
void FakeAudioCaptureModule::ProcessFrameP() {
RTC_DCHECK_RUN_ON(&process_thread_checker_);
{
webrtc::MutexLock lock(&mutex_);
if (!started_) {
next_frame_time_ = webrtc::TimeMillis();
started_ = true;
}
// Receive and send frames every kTimePerFrameMs.
if (playing_) {
ReceiveFrameP();
}
if (recording_) {
SendFrameP();
}
}
next_frame_time_ += kTimePerFrameMs;
const int64_t current_time = webrtc::TimeMillis();
const int64_t wait_time =
(next_frame_time_ > current_time) ? next_frame_time_ - current_time : 0;
process_thread_->PostDelayedTask([this] { ProcessFrameP(); },
TimeDelta::Millis(wait_time));
}
void FakeAudioCaptureModule::ReceiveFrameP() {
RTC_DCHECK_RUN_ON(&process_thread_checker_);
if (!audio_callback_) {
return;
}
ResetRecBuffer();
size_t nSamplesOut = 0;
int64_t elapsed_time_ms = 0;
int64_t ntp_time_ms = 0;
if (audio_callback_->NeedMorePlayData(kNumberSamples, kNumberBytesPerSample,
kNumberOfChannels, kSamplesPerSecond,
rec_buffer_, nSamplesOut,
&elapsed_time_ms, &ntp_time_ms) != 0) {
RTC_DCHECK_NOTREACHED();
}
RTC_CHECK(nSamplesOut == kNumberSamples);
// The SetBuffer() function ensures that after decoding, the audio buffer
// should contain samples of similar magnitude (there is likely to be some
// distortion due to the audio pipeline). If one sample is detected to
// have the same or greater magnitude somewhere in the frame, an actual frame
// has been received from the remote side (i.e. faked frames are not being
// pulled).
if (CheckRecBuffer(kHighSampleValue)) {
++frames_received_;
}
}
void FakeAudioCaptureModule::SendFrameP() {
RTC_DCHECK_RUN_ON(&process_thread_checker_);
if (!audio_callback_) {
return;
}
bool key_pressed = false;
uint32_t current_mic_level = current_mic_level_;
if (audio_callback_->RecordedDataIsAvailable(
send_buffer_, kNumberSamples, kNumberBytesPerSample,
kNumberOfChannels, kSamplesPerSecond, kTotalDelayMs, kClockDriftMs,
current_mic_level, key_pressed, current_mic_level) != 0) {
RTC_DCHECK_NOTREACHED();
}
current_mic_level_ = current_mic_level;
}
|