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

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

Issue 2482053003: AudioDeviceBuffer now uses 16-bit buffers (Closed)
Patch Set: nit 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 }
315 // Derive a new level value twice per second and check if it is non-zero. 313 // Derive a new level value twice per second and check if it is non-zero.
316 int16_t max_abs = 0; 314 int16_t max_abs = 0;
317 RTC_DCHECK_LT(rec_stat_count_, 50); 315 RTC_DCHECK_LT(rec_stat_count_, 50);
318 if (++rec_stat_count_ >= 50) { 316 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. 317 // Returns the largest absolute value in a signed 16-bit vector.
321 max_abs = WebRtcSpl_MaxAbsValueW16( 318 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; 319 rec_stat_count_ = 0;
324 // Set |only_silence_recorded_| to false as soon as at least one detection 320 // 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 321 // of a non-zero audio packet is found. It can only be restored to true
326 // again by restarting the call. 322 // again by restarting the call.
327 if (max_abs > 0) { 323 if (max_abs > 0) {
328 only_silence_recorded_ = false; 324 only_silence_recorded_ = false;
329 } 325 }
330 } 326 }
331 // Update some stats but do it on the task queue to ensure that the members 327 // 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 328 // 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 329 // 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. 330 // approximately two times per second and can then change the stats.
335 task_queue_.PostTask( 331 task_queue_.PostTask([this, max_abs, samples_per_channel] {
336 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); 332 UpdateRecStats(max_abs, samples_per_channel);
333 });
337 return 0; 334 return 0;
338 } 335 }
339 336
340 int32_t AudioDeviceBuffer::DeliverRecordedData() { 337 int32_t AudioDeviceBuffer::DeliverRecordedData() {
341 RTC_DCHECK_RUN_ON(&recording_thread_checker_); 338 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
342 if (!audio_transport_cb_) { 339 if (!audio_transport_cb_) {
343 LOG(LS_WARNING) << "Invalid audio transport"; 340 LOG(LS_WARNING) << "Invalid audio transport";
344 return 0; 341 return 0;
345 } 342 }
346 const size_t rec_bytes_per_sample = rec_channels_ * sizeof(int16_t); 343 const size_t frame_size_in_bytes = rec_channels_ * sizeof(int16_t);
kwiberg-webrtc 2016/11/08 14:54:40 Good. Or "bytes_per_frame", which means the same t
henrika_webrtc 2016/11/08 15:31:02 Done.
347 uint32_t new_mic_level(0); 344 uint32_t new_mic_level(0);
348 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; 345 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( 346 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
351 rec_buffer_.data(), num_samples, rec_bytes_per_sample, rec_channels_, 347 rec_buffer_.data(), rec_buffer_.size(), frame_size_in_bytes,
352 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_, 348 rec_channels_, rec_sample_rate_, total_delay_ms, clock_drift_,
353 typing_status_, new_mic_level); 349 current_mic_level_, typing_status_, new_mic_level);
354 if (res != -1) { 350 if (res != -1) {
355 new_mic_level_ = new_mic_level; 351 new_mic_level_ = new_mic_level;
356 } else { 352 } else {
357 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; 353 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
358 } 354 }
359 return 0; 355 return 0;
360 } 356 }
361 357
362 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { 358 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t samples_per_channel) {
363 RTC_DCHECK_RUN_ON(&playout_thread_checker_); 359 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
364 // The consumer can change the request size on the fly and we therefore 360 // 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 361 // resize the buffer accordingly. Also takes place at the first call to this
366 // method. 362 // method.
367 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t); 363 const size_t total_samples = play_channels_ * samples_per_channel;
368 const size_t size_in_bytes = num_samples * play_bytes_per_sample; 364 if (play_buffer_.size() != total_samples) {
369 if (play_buffer_.size() != size_in_bytes) { 365 play_buffer_.SetSize(total_samples);
370 play_buffer_.SetSize(size_in_bytes);
371 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); 366 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
372 } 367 }
373 368
374 size_t num_samples_out(0); 369 size_t num_samples_out(0);
375 // It is currently supported to start playout without a valid audio 370 // It is currently supported to start playout without a valid audio
376 // transport object. Leads to warning and silence. 371 // transport object. Leads to warning and silence.
377 if (!audio_transport_cb_) { 372 if (!audio_transport_cb_) {
378 LOG(LS_WARNING) << "Invalid audio transport"; 373 LOG(LS_WARNING) << "Invalid audio transport";
379 return 0; 374 return 0;
380 } 375 }
381 376
382 // Retrieve new 16-bit PCM audio data using the audio transport instance. 377 // Retrieve new 16-bit PCM audio data using the audio transport instance.
383 int64_t elapsed_time_ms = -1; 378 int64_t elapsed_time_ms = -1;
384 int64_t ntp_time_ms = -1; 379 int64_t ntp_time_ms = -1;
380 const size_t frame_size_in_bytes = play_channels_ * sizeof(int16_t);
385 uint32_t res = audio_transport_cb_->NeedMorePlayData( 381 uint32_t res = audio_transport_cb_->NeedMorePlayData(
386 num_samples, play_bytes_per_sample, play_channels_, play_sample_rate_, 382 samples_per_channel, frame_size_in_bytes, play_channels_,
387 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); 383 play_sample_rate_, play_buffer_.data(), num_samples_out, &elapsed_time_ms,
384 &ntp_time_ms);
388 if (res != 0) { 385 if (res != 0) {
389 LOG(LS_ERROR) << "NeedMorePlayData() failed"; 386 LOG(LS_ERROR) << "NeedMorePlayData() failed";
390 } 387 }
391 388
392 // Derive a new level value twice per second. 389 // Derive a new level value twice per second.
393 int16_t max_abs = 0; 390 int16_t max_abs = 0;
394 RTC_DCHECK_LT(play_stat_count_, 50); 391 RTC_DCHECK_LT(play_stat_count_, 50);
395 if (++play_stat_count_ >= 50) { 392 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. 393 // Returns the largest absolute value in a signed 16-bit vector.
398 max_abs = WebRtcSpl_MaxAbsValueW16( 394 max_abs =
399 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); 395 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size());
400 play_stat_count_ = 0; 396 play_stat_count_ = 0;
401 } 397 }
402 // Update some stats but do it on the task queue to ensure that the members 398 // 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 399 // 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 400 // 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. 401 // approximately two times per second and can then change the stats.
406 task_queue_.PostTask([this, max_abs, num_samples_out] { 402 task_queue_.PostTask([this, max_abs, num_samples_out] {
407 UpdatePlayStats(max_abs, num_samples_out); 403 UpdatePlayStats(max_abs, num_samples_out);
408 }); 404 });
409 return static_cast<int32_t>(num_samples_out); 405 return static_cast<int32_t>(num_samples_out);
410 } 406 }
411 407
412 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { 408 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
413 RTC_DCHECK_RUN_ON(&playout_thread_checker_); 409 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
414 RTC_DCHECK_GT(play_buffer_.size(), 0u); 410 RTC_DCHECK_GT(play_buffer_.size(), 0u);
415 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t); 411 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t);
416 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); 412 memcpy(audio_buffer, play_buffer_.data(),
417 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); 413 play_buffer_.size() * play_bytes_per_sample);
414 return static_cast<int32_t>(play_buffer_.size());
418 } 415 }
419 416
420 void AudioDeviceBuffer::StartPeriodicLogging() { 417 void AudioDeviceBuffer::StartPeriodicLogging() {
421 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 418 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
422 AudioDeviceBuffer::LOG_START)); 419 AudioDeviceBuffer::LOG_START));
423 } 420 }
424 421
425 void AudioDeviceBuffer::StopPeriodicLogging() { 422 void AudioDeviceBuffer::StopPeriodicLogging() {
426 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 423 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
427 AudioDeviceBuffer::LOG_STOP)); 424 AudioDeviceBuffer::LOG_STOP));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 494
498 void AudioDeviceBuffer::ResetPlayStats() { 495 void AudioDeviceBuffer::ResetPlayStats() {
499 RTC_DCHECK_RUN_ON(&task_queue_); 496 RTC_DCHECK_RUN_ON(&task_queue_);
500 play_callbacks_ = 0; 497 play_callbacks_ = 0;
501 last_play_callbacks_ = 0; 498 last_play_callbacks_ = 0;
502 play_samples_ = 0; 499 play_samples_ = 0;
503 last_play_samples_ = 0; 500 last_play_samples_ = 0;
504 max_play_level_ = 0; 501 max_play_level_ = 0;
505 } 502 }
506 503
507 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { 504 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs,
505 size_t samples_per_channel) {
508 RTC_DCHECK_RUN_ON(&task_queue_); 506 RTC_DCHECK_RUN_ON(&task_queue_);
509 ++rec_callbacks_; 507 ++rec_callbacks_;
510 rec_samples_ += num_samples; 508 rec_samples_ += samples_per_channel;
511 if (max_abs > max_rec_level_) { 509 if (max_abs > max_rec_level_) {
512 max_rec_level_ = max_abs; 510 max_rec_level_ = max_abs;
513 } 511 }
514 } 512 }
515 513
516 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { 514 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs,
515 size_t samples_per_channel) {
517 RTC_DCHECK_RUN_ON(&task_queue_); 516 RTC_DCHECK_RUN_ON(&task_queue_);
518 ++play_callbacks_; 517 ++play_callbacks_;
519 play_samples_ += num_samples; 518 play_samples_ += samples_per_channel;
520 if (max_abs > max_play_level_) { 519 if (max_abs > max_play_level_) {
521 max_play_level_ = max_abs; 520 max_play_level_ = max_abs;
522 } 521 }
523 } 522 }
524 523
525 } // namespace webrtc 524 } // 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