Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 play_channels_(0), | 46 play_channels_(0), |
| 47 playing_(false), | 47 playing_(false), |
| 48 recording_(false), | 48 recording_(false), |
| 49 current_mic_level_(0), | 49 current_mic_level_(0), |
| 50 new_mic_level_(0), | 50 new_mic_level_(0), |
| 51 typing_status_(false), | 51 typing_status_(false), |
| 52 play_delay_ms_(0), | 52 play_delay_ms_(0), |
| 53 rec_delay_ms_(0), | 53 rec_delay_ms_(0), |
| 54 clock_drift_(0), | 54 clock_drift_(0), |
| 55 num_stat_reports_(0), | 55 num_stat_reports_(0), |
| 56 rec_callbacks_(0), | |
| 57 last_rec_callbacks_(0), | |
| 58 play_callbacks_(0), | |
| 59 last_play_callbacks_(0), | |
| 60 rec_samples_(0), | |
| 61 last_rec_samples_(0), | |
| 62 play_samples_(0), | |
| 63 last_play_samples_(0), | |
| 64 max_rec_level_(0), | |
| 65 max_play_level_(0), | |
| 66 last_timer_task_time_(0), | 56 last_timer_task_time_(0), |
| 67 rec_stat_count_(0), | 57 rec_stat_count_(0), |
| 68 play_stat_count_(0), | 58 play_stat_count_(0), |
| 69 play_start_time_(0), | 59 play_start_time_(0), |
| 70 rec_start_time_(0), | |
| 71 only_silence_recorded_(true), | 60 only_silence_recorded_(true), |
| 72 log_stats_(false) { | 61 log_stats_(false) { |
| 73 LOG(INFO) << "AudioDeviceBuffer::ctor"; | 62 LOG(INFO) << "AudioDeviceBuffer::ctor"; |
| 74 playout_thread_checker_.DetachFromThread(); | 63 playout_thread_checker_.DetachFromThread(); |
| 75 recording_thread_checker_.DetachFromThread(); | 64 recording_thread_checker_.DetachFromThread(); |
| 76 } | 65 } |
| 77 | 66 |
| 78 AudioDeviceBuffer::~AudioDeviceBuffer() { | 67 AudioDeviceBuffer::~AudioDeviceBuffer() { |
| 79 RTC_DCHECK_RUN_ON(&main_thread_checker_); | 68 RTC_DCHECK_RUN_ON(&main_thread_checker_); |
| 80 RTC_DCHECK(!playing_); | 69 RTC_DCHECK(!playing_); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 // Returns the largest absolute value in a signed 16-bit vector. | 308 // Returns the largest absolute value in a signed 16-bit vector. |
| 320 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size()); | 309 max_abs = WebRtcSpl_MaxAbsValueW16(rec_buffer_.data(), rec_buffer_.size()); |
| 321 rec_stat_count_ = 0; | 310 rec_stat_count_ = 0; |
| 322 // Set |only_silence_recorded_| to false as soon as at least one detection | 311 // Set |only_silence_recorded_| to false as soon as at least one detection |
| 323 // of a non-zero audio packet is found. It can only be restored to true | 312 // of a non-zero audio packet is found. It can only be restored to true |
| 324 // again by restarting the call. | 313 // again by restarting the call. |
| 325 if (max_abs > 0) { | 314 if (max_abs > 0) { |
| 326 only_silence_recorded_ = false; | 315 only_silence_recorded_ = false; |
| 327 } | 316 } |
| 328 } | 317 } |
| 329 // Update some stats but do it on the task queue to ensure that the members | 318 // Update recording stats which is used as base for periodic logging of the |
| 330 // are modified and read on the same thread. Note that |max_abs| will be | 319 // audio input state. |
| 331 // zero in most calls and then have no effect of the stats. It is only updated | 320 UpdateRecStats(max_abs, samples_per_channel); |
| 332 // approximately two times per second and can then change the stats. | |
| 333 task_queue_.PostTask([this, max_abs, samples_per_channel] { | |
| 334 UpdateRecStats(max_abs, samples_per_channel); | |
| 335 }); | |
| 336 return 0; | 321 return 0; |
| 337 } | 322 } |
| 338 | 323 |
| 339 int32_t AudioDeviceBuffer::DeliverRecordedData() { | 324 int32_t AudioDeviceBuffer::DeliverRecordedData() { |
| 340 RTC_DCHECK_RUN_ON(&recording_thread_checker_); | 325 RTC_DCHECK_RUN_ON(&recording_thread_checker_); |
| 341 if (!audio_transport_cb_) { | 326 if (!audio_transport_cb_) { |
| 342 LOG(LS_WARNING) << "Invalid audio transport"; | 327 LOG(LS_WARNING) << "Invalid audio transport"; |
| 343 return 0; | 328 return 0; |
| 344 } | 329 } |
| 345 const size_t frames = rec_buffer_.size() / rec_channels_; | 330 const size_t frames = rec_buffer_.size() / rec_channels_; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 | 375 |
| 391 // Derive a new level value twice per second. | 376 // Derive a new level value twice per second. |
| 392 int16_t max_abs = 0; | 377 int16_t max_abs = 0; |
| 393 RTC_DCHECK_LT(play_stat_count_, 50); | 378 RTC_DCHECK_LT(play_stat_count_, 50); |
| 394 if (++play_stat_count_ >= 50) { | 379 if (++play_stat_count_ >= 50) { |
| 395 // Returns the largest absolute value in a signed 16-bit vector. | 380 // Returns the largest absolute value in a signed 16-bit vector. |
| 396 max_abs = | 381 max_abs = |
| 397 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size()); | 382 WebRtcSpl_MaxAbsValueW16(play_buffer_.data(), play_buffer_.size()); |
| 398 play_stat_count_ = 0; | 383 play_stat_count_ = 0; |
| 399 } | 384 } |
| 400 // Update some stats but do it on the task queue to ensure that the members | 385 // Update playout stats which is used as base for periodic logging of the |
| 401 // are modified and read on the same thread. Note that |max_abs| will be | 386 // audio output state. |
| 402 // zero in most calls and then have no effect of the stats. It is only updated | 387 UpdatePlayStats(max_abs, num_samples_out); |
| 403 // approximately two times per second and can then change the stats. | |
| 404 task_queue_.PostTask([this, max_abs, num_samples_out] { | |
| 405 UpdatePlayStats(max_abs, num_samples_out); | |
| 406 }); | |
| 407 return static_cast<int32_t>(num_samples_out); | 388 return static_cast<int32_t>(num_samples_out); |
| 408 } | 389 } |
| 409 | 390 |
| 410 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { | 391 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
| 411 RTC_DCHECK_RUN_ON(&playout_thread_checker_); | 392 RTC_DCHECK_RUN_ON(&playout_thread_checker_); |
| 412 RTC_DCHECK_GT(play_buffer_.size(), 0); | 393 RTC_DCHECK_GT(play_buffer_.size(), 0); |
| 413 const size_t bytes_per_sample = sizeof(int16_t); | 394 const size_t bytes_per_sample = sizeof(int16_t); |
| 414 memcpy(audio_buffer, play_buffer_.data(), | 395 memcpy(audio_buffer, play_buffer_.data(), |
| 415 play_buffer_.size() * bytes_per_sample); | 396 play_buffer_.size() * bytes_per_sample); |
| 416 // Return samples per channel or number of frames. | 397 // Return samples per channel or number of frames. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 446 | 427 |
| 447 // Avoid adding more logs since we are in STOP mode. | 428 // Avoid adding more logs since we are in STOP mode. |
| 448 if (!log_stats_) { | 429 if (!log_stats_) { |
| 449 return; | 430 return; |
| 450 } | 431 } |
| 451 | 432 |
| 452 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; | 433 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
| 453 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); | 434 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); |
| 454 last_timer_task_time_ = now_time; | 435 last_timer_task_time_ = now_time; |
| 455 | 436 |
| 437 const Stats stats = [&] { | |
| 438 rtc::CritScope cs(&lock_); | |
| 439 return stats_; | |
| 440 }(); | |
| 441 | |
| 456 // Log the latest statistics but skip the first round just after state was | 442 // Log the latest statistics but skip the first round just after state was |
| 457 // set to LOG_START. Hence, first printed log will be after ~10 seconds. | 443 // set to LOG_START. Hence, first printed log will be after ~10 seconds. |
| 458 if (++num_stat_reports_ > 1 && time_since_last > 0) { | 444 if (++num_stat_reports_ > 1 && time_since_last > 0) { |
| 459 uint32_t diff_samples = rec_samples_ - last_rec_samples_; | 445 uint32_t diff_samples = stats.rec_samples - last_stats_.rec_samples; |
| 460 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); | 446 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
| 461 LOG(INFO) << "[REC : " << time_since_last << "msec, " | 447 LOG(INFO) << "[REC : " << time_since_last << "msec, " |
| 462 << rec_sample_rate_ / 1000 | 448 << rec_sample_rate_ / 1000 << "kHz] callbacks: " |
| 463 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ | 449 << stats.rec_callbacks - last_stats_.rec_callbacks << ", " |
| 464 << ", " | |
| 465 << "samples: " << diff_samples << ", " | 450 << "samples: " << diff_samples << ", " |
| 466 << "rate: " << static_cast<int>(rate + 0.5) << ", " | 451 << "rate: " << static_cast<int>(rate + 0.5) << ", " |
| 467 << "level: " << max_rec_level_; | 452 << "level: " << stats.max_rec_level; |
| 468 | 453 |
| 469 diff_samples = play_samples_ - last_play_samples_; | 454 diff_samples = stats.play_samples - last_stats_.play_samples; |
| 470 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); | 455 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
| 471 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " | 456 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
| 472 << play_sample_rate_ / 1000 | 457 << play_sample_rate_ / 1000 << "kHz] callbacks: " |
| 473 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ | 458 << stats.play_callbacks - last_stats_.play_callbacks << ", " |
| 474 << ", " | |
| 475 << "samples: " << diff_samples << ", " | 459 << "samples: " << diff_samples << ", " |
| 476 << "rate: " << static_cast<int>(rate + 0.5) << ", " | 460 << "rate: " << static_cast<int>(rate + 0.5) << ", " |
| 477 << "level: " << max_play_level_; | 461 << "level: " << stats.max_play_level; |
| 462 | |
| 463 last_stats_ = stats; | |
| 464 { | |
| 465 rtc::CritScope cs(&lock_); | |
|
the sun
2017/02/02 19:38:38
No need to take the lock twice - just remove this,
henrika_webrtc
2017/02/03 09:00:54
Better than mine. Thanks!
| |
| 466 stats_.max_rec_level = 0; | |
| 467 stats_.max_play_level = 0; | |
| 468 } | |
| 478 } | 469 } |
| 479 | 470 |
| 480 last_rec_callbacks_ = rec_callbacks_; | |
| 481 last_play_callbacks_ = play_callbacks_; | |
| 482 last_rec_samples_ = rec_samples_; | |
| 483 last_play_samples_ = play_samples_; | |
| 484 max_rec_level_ = 0; | |
| 485 max_play_level_ = 0; | |
| 486 | |
| 487 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); | 471 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
| 488 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; | 472 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
| 489 | 473 |
| 490 // Keep posting new (delayed) tasks until state is changed to kLogStop. | 474 // Keep posting new (delayed) tasks until state is changed to kLogStop. |
| 491 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, | 475 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
| 492 AudioDeviceBuffer::LOG_ACTIVE), | 476 AudioDeviceBuffer::LOG_ACTIVE), |
| 493 time_to_wait_ms); | 477 time_to_wait_ms); |
| 494 } | 478 } |
| 495 | 479 |
| 496 void AudioDeviceBuffer::ResetRecStats() { | 480 void AudioDeviceBuffer::ResetRecStats() { |
| 497 RTC_DCHECK_RUN_ON(&task_queue_); | 481 RTC_DCHECK_RUN_ON(&task_queue_); |
| 498 rec_callbacks_ = 0; | 482 last_stats_.ResetRecStats(); |
| 499 last_rec_callbacks_ = 0; | 483 rtc::CritScope cs(&lock_); |
| 500 rec_samples_ = 0; | 484 stats_.ResetRecStats(); |
| 501 last_rec_samples_ = 0; | |
| 502 max_rec_level_ = 0; | |
| 503 } | 485 } |
| 504 | 486 |
| 505 void AudioDeviceBuffer::ResetPlayStats() { | 487 void AudioDeviceBuffer::ResetPlayStats() { |
| 506 RTC_DCHECK_RUN_ON(&task_queue_); | 488 RTC_DCHECK_RUN_ON(&task_queue_); |
| 507 play_callbacks_ = 0; | 489 last_stats_.ResetPlayStats(); |
| 508 last_play_callbacks_ = 0; | 490 rtc::CritScope cs(&lock_); |
| 509 play_samples_ = 0; | 491 stats_.ResetPlayStats(); |
| 510 last_play_samples_ = 0; | |
| 511 max_play_level_ = 0; | |
| 512 } | 492 } |
| 513 | 493 |
| 514 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, | 494 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, |
| 515 size_t samples_per_channel) { | 495 size_t samples_per_channel) { |
| 516 RTC_DCHECK_RUN_ON(&task_queue_); | 496 RTC_DCHECK_RUN_ON(&recording_thread_checker_); |
| 517 ++rec_callbacks_; | 497 rtc::CritScope cs(&lock_); |
| 518 rec_samples_ += samples_per_channel; | 498 ++stats_.rec_callbacks; |
| 519 if (max_abs > max_rec_level_) { | 499 stats_.rec_samples += samples_per_channel; |
| 520 max_rec_level_ = max_abs; | 500 if (max_abs > stats_.max_rec_level) { |
| 501 stats_.max_rec_level = max_abs; | |
| 521 } | 502 } |
| 522 } | 503 } |
| 523 | 504 |
| 524 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, | 505 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, |
| 525 size_t samples_per_channel) { | 506 size_t samples_per_channel) { |
| 526 RTC_DCHECK_RUN_ON(&task_queue_); | 507 RTC_DCHECK_RUN_ON(&playout_thread_checker_); |
| 527 ++play_callbacks_; | 508 rtc::CritScope cs(&lock_); |
| 528 play_samples_ += samples_per_channel; | 509 ++stats_.play_callbacks; |
| 529 if (max_abs > max_play_level_) { | 510 stats_.play_samples += samples_per_channel; |
| 530 max_play_level_ = max_abs; | 511 if (max_abs > stats_.max_play_level) { |
| 512 stats_.max_play_level = max_abs; | |
| 531 } | 513 } |
| 532 } | 514 } |
| 533 | 515 |
| 534 } // namespace webrtc | 516 } // namespace webrtc |
| OLD | NEW |