Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: webrtc/modules/audio_device/audio_device_buffer.cc

Issue 2482053003: AudioDeviceBuffer now uses 16-bit buffers (Closed)
Patch Set: Final fix for Android Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/audio_device/audio_device_buffer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <algorithm> 11 #include <algorithm>
12 12
13 #include "webrtc/modules/audio_device/audio_device_buffer.h" 13 #include "webrtc/modules/audio_device/audio_device_buffer.h"
14 14
15 #include "webrtc/base/arraysize.h" 15 #include "webrtc/base/arraysize.h"
16 #include "webrtc/base/bind.h" 16 #include "webrtc/base/bind.h"
17 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
18 #include "webrtc/base/logging.h" 18 #include "webrtc/base/logging.h"
19 #include "webrtc/base/format_macros.h" 19 #include "webrtc/base/format_macros.h"
20 #include "webrtc/base/timeutils.h" 20 #include "webrtc/base/timeutils.h"
21 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h" 21 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h"
22 #include "webrtc/modules/audio_device/audio_device_config.h" 22 #include "webrtc/modules/audio_device/audio_device_config.h"
23 #include "webrtc/system_wrappers/include/metrics.h" 23 #include "webrtc/system_wrappers/include/metrics.h"
24 24
25 #include "webrtc/base/platform_thread.h"
26
27 namespace webrtc { 25 namespace webrtc {
28 26
29 static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; 27 static const char kTimerQueueName[] = "AudioDeviceBufferTimer";
30 28
31 // Time between two sucessive calls to LogStats(). 29 // Time between two sucessive calls to LogStats().
32 static const size_t kTimerIntervalInSeconds = 10; 30 static const size_t kTimerIntervalInSeconds = 10;
33 static const size_t kTimerIntervalInMilliseconds = 31 static const size_t kTimerIntervalInMilliseconds =
34 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; 32 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec;
35 // Min time required to qualify an audio session as a "call". If playout or 33 // Min time required to qualify an audio session as a "call". If playout or
36 // recording has been active for less than this time we will not store any 34 // recording has been active for less than this time we will not store any
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 LOG(LS_WARNING) << "Not implemented"; 292 LOG(LS_WARNING) << "Not implemented";
295 return 0; 293 return 0;
296 } 294 }
297 295
298 int32_t AudioDeviceBuffer::StopOutputFileRecording() { 296 int32_t AudioDeviceBuffer::StopOutputFileRecording() {
299 LOG(LS_WARNING) << "Not implemented"; 297 LOG(LS_WARNING) << "Not implemented";
300 return 0; 298 return 0;
301 } 299 }
302 300
303 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, 301 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
304 size_t num_samples) { 302 size_t samples_per_channel) {
305 RTC_DCHECK_RUN_ON(&recording_thread_checker_); 303 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
306 // Copy the complete input buffer to the local buffer. 304 // Copy the complete input buffer to the local buffer.
307 const size_t size_in_bytes = num_samples * rec_channels_ * sizeof(int16_t);
308 const size_t old_size = rec_buffer_.size(); 305 const size_t old_size = rec_buffer_.size();
309 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); 306 rec_buffer_.SetData(static_cast<const int16_t*>(audio_buffer),
307 rec_channels_ * samples_per_channel);
310 // Keep track of the size of the recording buffer. Only updated when the 308 // Keep track of the size of the recording buffer. Only updated when the
311 // size changes, which is a rare event. 309 // size changes, which is a rare event.
312 if (old_size != rec_buffer_.size()) { 310 if (old_size != rec_buffer_.size()) {
313 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size(); 311 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
314 } 312 }
313
315 // Derive a new level value twice per second and check if it is non-zero. 314 // Derive a new level value twice per second and check if it is non-zero.
316 int16_t max_abs = 0; 315 int16_t max_abs = 0;
317 RTC_DCHECK_LT(rec_stat_count_, 50); 316 RTC_DCHECK_LT(rec_stat_count_, 50);
318 if (++rec_stat_count_ >= 50) { 317 if (++rec_stat_count_ >= 50) {
319 const size_t size = num_samples * rec_channels_;
320 // Returns the largest absolute value in a signed 16-bit vector. 318 // Returns the largest absolute value in a signed 16-bit vector.
321 max_abs = WebRtcSpl_MaxAbsValueW16( 319 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size());
322 reinterpret_cast<const int16_t*>(rec_buffer_.data()), size);
323 rec_stat_count_ = 0; 320 rec_stat_count_ = 0;
324 // Set |only_silence_recorded_| to false as soon as at least one detection 321 // Set |only_silence_recorded_| to false as soon as at least one detection
325 // of a non-zero audio packet is found. It can only be restored to true 322 // of a non-zero audio packet is found. It can only be restored to true
326 // again by restarting the call. 323 // again by restarting the call.
327 if (max_abs > 0) { 324 if (max_abs > 0) {
328 only_silence_recorded_ = false; 325 only_silence_recorded_ = false;
329 } 326 }
330 } 327 }
331 // Update some stats but do it on the task queue to ensure that the members 328 // Update some stats but do it on the task queue to ensure that the members
332 // are modified and read on the same thread. Note that |max_abs| will be 329 // are modified and read on the same thread. Note that |max_abs| will be
333 // zero in most calls and then have no effect of the stats. It is only updated 330 // zero in most calls and then have no effect of the stats. It is only updated
334 // approximately two times per second and can then change the stats. 331 // approximately two times per second and can then change the stats.
335 task_queue_.PostTask( 332 task_queue_.PostTask([this, max_abs, samples_per_channel] {
336 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); 333 UpdateRecStats(max_abs, samples_per_channel);
334 });
337 return 0; 335 return 0;
338 } 336 }
339 337
340 int32_t AudioDeviceBuffer::DeliverRecordedData() { 338 int32_t AudioDeviceBuffer::DeliverRecordedData() {
341 RTC_DCHECK_RUN_ON(&recording_thread_checker_); 339 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
342 if (!audio_transport_cb_) { 340 if (!audio_transport_cb_) {
343 LOG(LS_WARNING) << "Invalid audio transport"; 341 LOG(LS_WARNING) << "Invalid audio transport";
344 return 0; 342 return 0;
345 } 343 }
346 const size_t rec_bytes_per_sample = rec_channels_ * sizeof(int16_t); 344 const size_t frames = rec_buffer_.size() / rec_channels_;
345 const size_t bytes_per_frame = rec_channels_ * sizeof(int16_t);
347 uint32_t new_mic_level(0); 346 uint32_t new_mic_level(0);
348 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; 347 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
349 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample;
350 int32_t res = audio_transport_cb_->RecordedDataIsAvailable( 348 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
351 rec_buffer_.data(), num_samples, rec_bytes_per_sample, rec_channels_, 349 rec_buffer_.data(), frames, bytes_per_frame, rec_channels_,
352 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_, 350 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_,
353 typing_status_, new_mic_level); 351 typing_status_, new_mic_level);
354 if (res != -1) { 352 if (res != -1) {
355 new_mic_level_ = new_mic_level; 353 new_mic_level_ = new_mic_level;
356 } else { 354 } else {
357 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; 355 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
358 } 356 }
359 return 0; 357 return 0;
360 } 358 }
361 359
362 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { 360 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
363 RTC_DCHECK_RUN_ON(&playout_thread_checker_); 361 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
364 // The consumer can change the request size on the fly and we therefore 362 // The consumer can change the requested size on the fly and we therefore
365 // resize the buffer accordingly. Also takes place at the first call to this 363 // resize the buffer accordingly. Also takes place at the first call to this
366 // method. 364 // method.
367 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t); 365 const size_t total_samples = play_channels_ * samples_per_channel;
368 const size_t size_in_bytes = num_samples * play_bytes_per_sample; 366 if (play_buffer_.size() != total_samples) {
369 if (play_buffer_.size() != size_in_bytes) { 367 play_buffer_.SetSize(total_samples);
370 play_buffer_.SetSize(size_in_bytes);
371 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); 368 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
372 } 369 }
373 370
374 size_t num_samples_out(0); 371 size_t num_samples_out(0);
375 // It is currently supported to start playout without a valid audio 372 // It is currently supported to start playout without a valid audio
376 // transport object. Leads to warning and silence. 373 // transport object. Leads to warning and silence.
377 if (!audio_transport_cb_) { 374 if (!audio_transport_cb_) {
378 LOG(LS_WARNING) << "Invalid audio transport"; 375 LOG(LS_WARNING) << "Invalid audio transport";
379 return 0; 376 return 0;
380 } 377 }
381 378
382 // Retrieve new 16-bit PCM audio data using the audio transport instance. 379 // Retrieve new 16-bit PCM audio data using the audio transport instance.
383 int64_t elapsed_time_ms = -1; 380 int64_t elapsed_time_ms = -1;
384 int64_t ntp_time_ms = -1; 381 int64_t ntp_time_ms = -1;
382 const size_t bytes_per_frame = play_channels_ * sizeof(int16_t);
385 uint32_t res = audio_transport_cb_->NeedMorePlayData( 383 uint32_t res = audio_transport_cb_->NeedMorePlayData(
386 num_samples, play_bytes_per_sample, play_channels_, play_sample_rate_, 384 samples_per_channel, bytes_per_frame, play_channels_, play_sample_rate_,
387 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); 385 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
388 if (res != 0) { 386 if (res != 0) {
389 LOG(LS_ERROR) << "NeedMorePlayData() failed"; 387 LOG(LS_ERROR) << "NeedMorePlayData() failed";
390 } 388 }
391 389
392 // Derive a new level value twice per second. 390 // Derive a new level value twice per second.
393 int16_t max_abs = 0; 391 int16_t max_abs = 0;
394 RTC_DCHECK_LT(play_stat_count_, 50); 392 RTC_DCHECK_LT(play_stat_count_, 50);
395 if (++play_stat_count_ >= 50) { 393 if (++play_stat_count_ >= 50) {
396 const size_t size = num_samples * play_channels_;
397 // Returns the largest absolute value in a signed 16-bit vector. 394 // Returns the largest absolute value in a signed 16-bit vector.
398 max_abs = WebRtcSpl_MaxAbsValueW16( 395 max_abs =
399 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); 396 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
400 play_stat_count_ = 0; 397 play_stat_count_ = 0;
401 } 398 }
402 // Update some stats but do it on the task queue to ensure that the members 399 // Update some stats but do it on the task queue to ensure that the members
403 // are modified and read on the same thread. Note that |max_abs| will be 400 // are modified and read on the same thread. Note that |max_abs| will be
404 // zero in most calls and then have no effect of the stats. It is only updated 401 // zero in most calls and then have no effect of the stats. It is only updated
405 // approximately two times per second and can then change the stats. 402 // approximately two times per second and can then change the stats.
406 task_queue_.PostTask([this, max_abs, num_samples_out] { 403 task_queue_.PostTask([this, max_abs, num_samples_out] {
407 UpdatePlayStats(max_abs, num_samples_out); 404 UpdatePlayStats(max_abs, num_samples_out);
408 }); 405 });
409 return static_cast<int32_t>(num_samples_out); 406 return static_cast<int32_t>(num_samples_out);
410 } 407 }
411 408
412 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { 409 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
413 RTC_DCHECK_RUN_ON(&playout_thread_checker_); 410 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
414 RTC_DCHECK_GT(play_buffer_.size(), 0u); 411 RTC_DCHECK_GT(play_buffer_.size(), 0u);
415 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t); 412 const size_t bytes_per_sample = sizeof(int16_t);
416 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); 413 memcpy(audio_buffer, play_buffer_.data(),
417 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); 414 play_buffer_.size() * bytes_per_sample);
415 // Return samples per channel or number of frames.
416 return static_cast<int32_t>(play_buffer_.size() / play_channels_);
418 } 417 }
419 418
420 void AudioDeviceBuffer::StartPeriodicLogging() { 419 void AudioDeviceBuffer::StartPeriodicLogging() {
421 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 420 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
422 AudioDeviceBuffer::LOG_START)); 421 AudioDeviceBuffer::LOG_START));
423 } 422 }
424 423
425 void AudioDeviceBuffer::StopPeriodicLogging() { 424 void AudioDeviceBuffer::StopPeriodicLogging() {
426 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 425 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
427 AudioDeviceBuffer::LOG_STOP)); 426 AudioDeviceBuffer::LOG_STOP));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 496
498 void AudioDeviceBuffer::ResetPlayStats() { 497 void AudioDeviceBuffer::ResetPlayStats() {
499 RTC_DCHECK_RUN_ON(&task_queue_); 498 RTC_DCHECK_RUN_ON(&task_queue_);
500 play_callbacks_ = 0; 499 play_callbacks_ = 0;
501 last_play_callbacks_ = 0; 500 last_play_callbacks_ = 0;
502 play_samples_ = 0; 501 play_samples_ = 0;
503 last_play_samples_ = 0; 502 last_play_samples_ = 0;
504 max_play_level_ = 0; 503 max_play_level_ = 0;
505 } 504 }
506 505
507 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { 506 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
507 size_t samples_per_channel) {
508 RTC_DCHECK_RUN_ON(&task_queue_); 508 RTC_DCHECK_RUN_ON(&task_queue_);
509 ++rec_callbacks_; 509 ++rec_callbacks_;
510 rec_samples_ += num_samples; 510 rec_samples_ += samples_per_channel;
511 if (max_abs > max_rec_level_) { 511 if (max_abs > max_rec_level_) {
512 max_rec_level_ = max_abs; 512 max_rec_level_ = max_abs;
513 } 513 }
514 } 514 }
515 515
516 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { 516 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
517 size_t samples_per_channel) {
517 RTC_DCHECK_RUN_ON(&task_queue_); 518 RTC_DCHECK_RUN_ON(&task_queue_);
518 ++play_callbacks_; 519 ++play_callbacks_;
519 play_samples_ += num_samples; 520 play_samples_ += samples_per_channel;
520 if (max_abs > max_play_level_) { 521 if (max_abs > max_play_level_) {
521 max_play_level_ = max_abs; 522 max_play_level_ = max_abs;
522 } 523 }
523 } 524 }
524 525
525 } // namespace webrtc 526 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_device/audio_device_buffer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698