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

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

Issue 2482053003: AudioDeviceBuffer now uses 16-bit buffers (Closed)
Patch Set: 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 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 num_samples) {
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); 305 const size_t sample_frame_size = rec_channels_;
306 const size_t packet_size_in_frames = sample_frame_size * num_samples;
kwiberg-webrtc 2016/11/08 12:19:58 No... Since a frame consists of 1 or more samples
henrika_webrtc 2016/11/08 12:33:59 I am using a standard notation from how audio samp
kwiberg-webrtc 2016/11/08 13:01:55 OK.
308 const size_t old_size = rec_buffer_.size(); 307 const size_t old_size = rec_buffer_.size();
309 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); 308 rec_buffer_.SetData(static_cast<const uint16_t*>(audio_buffer),
309 packet_size_in_frames);
310 // Keep track of the size of the recording buffer. Only updated when the 310 // Keep track of the size of the recording buffer. Only updated when the
311 // size changes, which is a rare event. 311 // size changes, which is a rare event.
312 if (old_size != rec_buffer_.size()) { 312 if (old_size != rec_buffer_.size()) {
313 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size(); 313 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size();
314 } 314 }
315 // Derive a new level value twice per second and check if it is non-zero. 315 // Derive a new level value twice per second and check if it is non-zero.
316 int16_t max_abs = 0; 316 int16_t max_abs = 0;
317 RTC_DCHECK_LT(rec_stat_count_, 50); 317 RTC_DCHECK_LT(rec_stat_count_, 50);
318 if (++rec_stat_count_ >= 50) { 318 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. 319 // Returns the largest absolute value in a signed 16-bit vector.
321 max_abs = WebRtcSpl_MaxAbsValueW16( 320 max_abs = WebRtcSpl_MaxAbsValueW16(
322 reinterpret_cast<const int16_t*>(rec_buffer_.data()), size); 321 reinterpret_cast<const int16_t*>(rec_buffer_.data()),
kwiberg-webrtc 2016/11/08 12:19:58 You shouldn't need this cast anymore, since rec_bu
henrika_webrtc 2016/11/08 12:33:59 Acknowledged.
322 rec_buffer_.size());
323 rec_stat_count_ = 0; 323 rec_stat_count_ = 0;
324 // Set |only_silence_recorded_| to false as soon as at least one detection 324 // 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 325 // of a non-zero audio packet is found. It can only be restored to true
326 // again by restarting the call. 326 // again by restarting the call.
327 if (max_abs > 0) { 327 if (max_abs > 0) {
328 only_silence_recorded_ = false; 328 only_silence_recorded_ = false;
329 } 329 }
330 } 330 }
331 // Update some stats but do it on the task queue to ensure that the members 331 // 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 332 // 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 333 // 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. 334 // approximately two times per second and can then change the stats.
335 task_queue_.PostTask( 335 task_queue_.PostTask(
336 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); 336 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); });
337 return 0; 337 return 0;
338 } 338 }
339 339
340 int32_t AudioDeviceBuffer::DeliverRecordedData() { 340 int32_t AudioDeviceBuffer::DeliverRecordedData() {
341 RTC_DCHECK_RUN_ON(&recording_thread_checker_); 341 RTC_DCHECK_RUN_ON(&recording_thread_checker_);
342 if (!audio_transport_cb_) { 342 if (!audio_transport_cb_) {
343 LOG(LS_WARNING) << "Invalid audio transport"; 343 LOG(LS_WARNING) << "Invalid audio transport";
344 return 0; 344 return 0;
345 } 345 }
346 const size_t rec_bytes_per_sample = rec_channels_ * sizeof(int16_t); 346 const size_t sample_frame_size = rec_channels_;
347 const size_t sample_frame_size_in_bytes = sample_frame_size * sizeof(int16_t);
347 uint32_t new_mic_level(0); 348 uint32_t new_mic_level(0);
348 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; 349 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( 350 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
351 rec_buffer_.data(), num_samples, rec_bytes_per_sample, rec_channels_, 351 rec_buffer_.data(), rec_buffer_.size(), sample_frame_size_in_bytes,
352 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_, 352 rec_channels_, rec_sample_rate_, total_delay_ms, clock_drift_,
353 typing_status_, new_mic_level); 353 current_mic_level_, typing_status_, new_mic_level);
354 if (res != -1) { 354 if (res != -1) {
355 new_mic_level_ = new_mic_level; 355 new_mic_level_ = new_mic_level;
356 } else { 356 } else {
357 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; 357 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
358 } 358 }
359 return 0; 359 return 0;
360 } 360 }
361 361
362 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { 362 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) {
363 RTC_DCHECK_RUN_ON(&playout_thread_checker_); 363 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
364 // The consumer can change the request size on the fly and we therefore 364 // 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 365 // resize the buffer accordingly. Also takes place at the first call to this
366 // method. 366 // method. Each sample frame contains |sample_frame_size| * sizeof(int16_t)
367 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t); 367 // bytes.
368 const size_t size_in_bytes = num_samples * play_bytes_per_sample; 368 const size_t sample_frame_size = play_channels_;
369 if (play_buffer_.size() != size_in_bytes) { 369 const size_t packet_size_in_frames = sample_frame_size * num_samples;
kwiberg-webrtc 2016/11/08 12:19:58 Same comment as above about the variable names.
henrika_webrtc 2016/11/08 12:33:59 See above
370 play_buffer_.SetSize(size_in_bytes); 370 if (play_buffer_.size() != packet_size_in_frames) {
371 play_buffer_.SetSize(packet_size_in_frames);
371 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); 372 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
372 } 373 }
373 374
374 size_t num_samples_out(0); 375 size_t num_samples_out(0);
375 // It is currently supported to start playout without a valid audio 376 // It is currently supported to start playout without a valid audio
376 // transport object. Leads to warning and silence. 377 // transport object. Leads to warning and silence.
377 if (!audio_transport_cb_) { 378 if (!audio_transport_cb_) {
378 LOG(LS_WARNING) << "Invalid audio transport"; 379 LOG(LS_WARNING) << "Invalid audio transport";
379 return 0; 380 return 0;
380 } 381 }
381 382
382 // Retrieve new 16-bit PCM audio data using the audio transport instance. 383 // Retrieve new 16-bit PCM audio data using the audio transport instance.
383 int64_t elapsed_time_ms = -1; 384 int64_t elapsed_time_ms = -1;
384 int64_t ntp_time_ms = -1; 385 int64_t ntp_time_ms = -1;
386 const size_t sample_frame_size_in_bytes = sample_frame_size * sizeof(int16_t);
385 uint32_t res = audio_transport_cb_->NeedMorePlayData( 387 uint32_t res = audio_transport_cb_->NeedMorePlayData(
386 num_samples, play_bytes_per_sample, play_channels_, play_sample_rate_, 388 num_samples, sample_frame_size_in_bytes, play_channels_,
387 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); 389 play_sample_rate_, play_buffer_.data(), num_samples_out, &elapsed_time_ms,
390 &ntp_time_ms);
388 if (res != 0) { 391 if (res != 0) {
389 LOG(LS_ERROR) << "NeedMorePlayData() failed"; 392 LOG(LS_ERROR) << "NeedMorePlayData() failed";
390 } 393 }
391 394
392 // Derive a new level value twice per second. 395 // Derive a new level value twice per second.
393 int16_t max_abs = 0; 396 int16_t max_abs = 0;
394 RTC_DCHECK_LT(play_stat_count_, 50); 397 RTC_DCHECK_LT(play_stat_count_, 50);
395 if (++play_stat_count_ >= 50) { 398 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. 399 // Returns the largest absolute value in a signed 16-bit vector.
398 max_abs = WebRtcSpl_MaxAbsValueW16( 400 max_abs = WebRtcSpl_MaxAbsValueW16(
399 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); 401 reinterpret_cast<const int16_t*>(play_buffer_.data()),
kwiberg-webrtc 2016/11/08 12:19:58 Remove cast?
henrika_webrtc 2016/11/08 12:33:59 Done.
402 play_buffer_.size());
400 play_stat_count_ = 0; 403 play_stat_count_ = 0;
401 } 404 }
402 // Update some stats but do it on the task queue to ensure that the members 405 // 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 406 // 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 407 // 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. 408 // approximately two times per second and can then change the stats.
406 task_queue_.PostTask([this, max_abs, num_samples_out] { 409 task_queue_.PostTask([this, max_abs, num_samples_out] {
407 UpdatePlayStats(max_abs, num_samples_out); 410 UpdatePlayStats(max_abs, num_samples_out);
408 }); 411 });
409 return static_cast<int32_t>(num_samples_out); 412 return static_cast<int32_t>(num_samples_out);
410 } 413 }
411 414
412 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { 415 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
413 RTC_DCHECK_RUN_ON(&playout_thread_checker_); 416 RTC_DCHECK_RUN_ON(&playout_thread_checker_);
414 RTC_DCHECK_GT(play_buffer_.size(), 0u); 417 RTC_DCHECK_GT(play_buffer_.size(), 0u);
415 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t); 418 const size_t play_bytes_per_sample = play_channels_ * sizeof(int16_t);
416 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); 419 memcpy(audio_buffer, play_buffer_.data(),
417 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); 420 play_buffer_.size() * play_bytes_per_sample);
421 return static_cast<int32_t>(play_buffer_.size());
418 } 422 }
419 423
420 void AudioDeviceBuffer::StartPeriodicLogging() { 424 void AudioDeviceBuffer::StartPeriodicLogging() {
421 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 425 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
422 AudioDeviceBuffer::LOG_START)); 426 AudioDeviceBuffer::LOG_START));
423 } 427 }
424 428
425 void AudioDeviceBuffer::StopPeriodicLogging() { 429 void AudioDeviceBuffer::StopPeriodicLogging() {
426 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 430 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
427 AudioDeviceBuffer::LOG_STOP)); 431 AudioDeviceBuffer::LOG_STOP));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { 520 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) {
517 RTC_DCHECK_RUN_ON(&task_queue_); 521 RTC_DCHECK_RUN_ON(&task_queue_);
518 ++play_callbacks_; 522 ++play_callbacks_;
519 play_samples_ += num_samples; 523 play_samples_ += num_samples;
520 if (max_abs > max_play_level_) { 524 if (max_abs > max_play_level_) {
521 max_play_level_ = max_abs; 525 max_play_level_ = max_abs;
522 } 526 }
523 } 527 }
524 528
525 } // namespace webrtc 529 } // 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