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 |
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/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" | |
22 | 24 |
23 namespace webrtc { | 25 namespace webrtc { |
24 | 26 |
25 static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; | 27 static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; |
26 | 28 |
27 // Time between two sucessive calls to LogStats(). | 29 // Time between two sucessive calls to LogStats(). |
28 static const size_t kTimerIntervalInSeconds = 10; | 30 static const size_t kTimerIntervalInSeconds = 10; |
29 static const size_t kTimerIntervalInMilliseconds = | 31 static const size_t kTimerIntervalInMilliseconds = |
30 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; | 32 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; |
31 | 33 |
(...skipping 20 matching lines...) Expand all Loading... | |
52 clock_drift_(0), | 54 clock_drift_(0), |
53 num_stat_reports_(0), | 55 num_stat_reports_(0), |
54 rec_callbacks_(0), | 56 rec_callbacks_(0), |
55 last_rec_callbacks_(0), | 57 last_rec_callbacks_(0), |
56 play_callbacks_(0), | 58 play_callbacks_(0), |
57 last_play_callbacks_(0), | 59 last_play_callbacks_(0), |
58 rec_samples_(0), | 60 rec_samples_(0), |
59 last_rec_samples_(0), | 61 last_rec_samples_(0), |
60 play_samples_(0), | 62 play_samples_(0), |
61 last_play_samples_(0), | 63 last_play_samples_(0), |
62 last_log_stat_time_(0) { | 64 last_log_stat_time_(0), |
65 max_rec_level_(0), | |
66 max_play_level_(0), | |
67 num_rec_level_is_zero_(0) { | |
63 LOG(INFO) << "AudioDeviceBuffer::ctor"; | 68 LOG(INFO) << "AudioDeviceBuffer::ctor"; |
64 } | 69 } |
65 | 70 |
66 AudioDeviceBuffer::~AudioDeviceBuffer() { | 71 AudioDeviceBuffer::~AudioDeviceBuffer() { |
67 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 72 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
68 LOG(INFO) << "AudioDeviceBuffer::~dtor"; | 73 LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
69 | 74 |
70 size_t total_diff_time = 0; | 75 size_t total_diff_time = 0; |
71 int num_measurements = 0; | 76 int num_measurements = 0; |
72 LOG(INFO) << "[playout diff time => #measurements]"; | 77 LOG(INFO) << "[playout diff time => #measurements]"; |
73 for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) { | 78 for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) { |
74 uint32_t num_elements = playout_diff_times_[diff]; | 79 uint32_t num_elements = playout_diff_times_[diff]; |
75 if (num_elements > 0) { | 80 if (num_elements > 0) { |
76 total_diff_time += num_elements * diff; | 81 total_diff_time += num_elements * diff; |
77 num_measurements += num_elements; | 82 num_measurements += num_elements; |
78 LOG(INFO) << "[" << diff << " => " << num_elements << "]"; | 83 LOG(INFO) << "[" << diff << " => " << num_elements << "]"; |
79 } | 84 } |
80 } | 85 } |
81 if (num_measurements > 0) { | 86 if (num_measurements > 0) { |
82 LOG(INFO) << "total_diff_time: " << total_diff_time; | 87 LOG(INFO) << "total_diff_time: " << total_diff_time; |
83 LOG(INFO) << "num_measurements: " << num_measurements; | 88 LOG(INFO) << "num_measurements: " << num_measurements; |
84 LOG(INFO) << "average: " | 89 LOG(INFO) << "average: " |
85 << static_cast<float>(total_diff_time) / num_measurements; | 90 << static_cast<float>(total_diff_time) / num_measurements; |
86 } | 91 } |
92 | |
93 // Add UMA histogram to keep track of the case when only zeros have been | |
94 // recorded. Ensure that recording callbacks have started and that at least | |
95 // one timer event has been able to update |num_rec_level_is_zero_|. | |
96 // I am avoiding use of the task queue here since we are under destruction | |
97 // and reading these members on the creating thread feels safe. | |
98 if (rec_callbacks_ > 0 && num_stat_reports_ > 0) { | |
99 RTC_LOGGED_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", | |
100 static_cast<int>(num_stat_reports_ == num_rec_level_is_zero_)); | |
101 } | |
87 } | 102 } |
88 | 103 |
89 int32_t AudioDeviceBuffer::RegisterAudioCallback( | 104 int32_t AudioDeviceBuffer::RegisterAudioCallback( |
90 AudioTransport* audio_callback) { | 105 AudioTransport* audio_callback) { |
91 LOG(INFO) << __FUNCTION__; | 106 LOG(INFO) << __FUNCTION__; |
92 rtc::CritScope lock(&_critSectCb); | 107 rtc::CritScope lock(&_critSectCb); |
93 audio_transport_cb_ = audio_callback; | 108 audio_transport_cb_ = audio_callback; |
94 return 0; | 109 return 0; |
95 } | 110 } |
96 | 111 |
97 int32_t AudioDeviceBuffer::InitPlayout() { | 112 int32_t AudioDeviceBuffer::InitPlayout() { |
98 LOG(INFO) << __FUNCTION__; | 113 LOG(INFO) << __FUNCTION__; |
99 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 114 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
100 last_playout_time_ = rtc::TimeMillis(); | 115 ResetPlayStats(); |
101 if (!timer_has_started_) { | 116 if (!timer_has_started_) { |
102 StartTimer(); | 117 StartTimer(); |
103 timer_has_started_ = true; | 118 timer_has_started_ = true; |
104 } | 119 } |
105 return 0; | 120 return 0; |
106 } | 121 } |
107 | 122 |
108 int32_t AudioDeviceBuffer::InitRecording() { | 123 int32_t AudioDeviceBuffer::InitRecording() { |
109 LOG(INFO) << __FUNCTION__; | 124 LOG(INFO) << __FUNCTION__; |
110 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 125 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
126 ResetRecStats(); | |
111 if (!timer_has_started_) { | 127 if (!timer_has_started_) { |
112 StartTimer(); | 128 StartTimer(); |
113 timer_has_started_ = true; | 129 timer_has_started_ = true; |
114 } | 130 } |
115 return 0; | 131 return 0; |
116 } | 132 } |
117 | 133 |
118 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { | 134 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
119 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; | 135 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
120 rtc::CritScope lock(&_critSect); | 136 rtc::CritScope lock(&_critSect); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 for (size_t i = 0; i < rec_samples_per_10ms_; i++) { | 272 for (size_t i = 0; i < rec_samples_per_10ms_; i++) { |
257 *ptr16Out = *ptr16In; | 273 *ptr16Out = *ptr16In; |
258 ptr16Out++; | 274 ptr16Out++; |
259 ptr16In++; | 275 ptr16In++; |
260 ptr16In++; | 276 ptr16In++; |
261 } | 277 } |
262 } | 278 } |
263 | 279 |
264 // Update some stats but do it on the task queue to ensure that the members | 280 // Update some stats but do it on the task queue to ensure that the members |
265 // are modified and read on the same thread. | 281 // are modified and read on the same thread. |
266 task_queue_.PostTask( | 282 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, |
267 rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, num_samples)); | 283 audio_buffer, num_samples)); |
268 return 0; | 284 return 0; |
269 } | 285 } |
270 | 286 |
271 int32_t AudioDeviceBuffer::DeliverRecordedData() { | 287 int32_t AudioDeviceBuffer::DeliverRecordedData() { |
272 RTC_CHECK(rec_buffer_); | 288 RTC_CHECK(rec_buffer_); |
273 RTC_DCHECK(audio_transport_cb_); | 289 RTC_DCHECK(audio_transport_cb_); |
274 rtc::CritScope lock(&_critSectCb); | 290 rtc::CritScope lock(&_critSectCb); |
275 | 291 |
276 if (!audio_transport_cb_) { | 292 if (!audio_transport_cb_) { |
277 LOG(LS_WARNING) << "Invalid audio transport"; | 293 LOG(LS_WARNING) << "Invalid audio transport"; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 res = audio_transport_cb_->NeedMorePlayData( | 344 res = audio_transport_cb_->NeedMorePlayData( |
329 play_samples_per_10ms_, play_bytes_per_sample_, play_channels_, | 345 play_samples_per_10ms_, play_bytes_per_sample_, play_channels_, |
330 play_sample_rate_, &play_buffer_[0], num_samples_out, &elapsed_time_ms, | 346 play_sample_rate_, &play_buffer_[0], num_samples_out, &elapsed_time_ms, |
331 &ntp_time_ms); | 347 &ntp_time_ms); |
332 if (res != 0) { | 348 if (res != 0) { |
333 LOG(LS_ERROR) << "NeedMorePlayData() failed"; | 349 LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
334 } | 350 } |
335 | 351 |
336 // Update some stats but do it on the task queue to ensure that access of | 352 // Update some stats but do it on the task queue to ensure that access of |
337 // members is serialized hence avoiding usage of locks. | 353 // members is serialized hence avoiding usage of locks. |
338 task_queue_.PostTask( | 354 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, |
peah-webrtc
2016/09/09 09:06:27
Would it not be better to post the task once every
peah-webrtc
2016/09/09 09:06:27
How critical is the complexity here? Would it be o
henrika_webrtc
2016/09/09 11:55:01
Referring to off-line discussion.
henrika_webrtc
2016/09/09 11:55:01
Referring to off-line discussion.
| |
339 rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, num_samples_out)); | 355 &play_buffer_[0], num_samples_out)); |
340 return static_cast<int32_t>(num_samples_out); | 356 return static_cast<int32_t>(num_samples_out); |
341 } | 357 } |
342 | 358 |
343 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { | 359 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
344 rtc::CritScope lock(&_critSect); | 360 rtc::CritScope lock(&_critSect); |
345 memcpy(audio_buffer, &play_buffer_[0], play_bytes_per_10ms_); | 361 memcpy(audio_buffer, &play_buffer_[0], play_bytes_per_10ms_); |
346 return static_cast<int32_t>(play_samples_per_10ms_); | 362 return static_cast<int32_t>(play_samples_per_10ms_); |
347 } | 363 } |
348 | 364 |
349 void AudioDeviceBuffer::AllocatePlayoutBufferIfNeeded() { | 365 void AudioDeviceBuffer::AllocatePlayoutBufferIfNeeded() { |
(...skipping 22 matching lines...) Expand all Loading... | |
372 rec_samples_per_10ms_ = static_cast<size_t>(rec_sample_rate_ * 10 / 1000); | 388 rec_samples_per_10ms_ = static_cast<size_t>(rec_sample_rate_ * 10 / 1000); |
373 rec_bytes_per_10ms_ = rec_bytes_per_sample_ * rec_samples_per_10ms_; | 389 rec_bytes_per_10ms_ = rec_bytes_per_sample_ * rec_samples_per_10ms_; |
374 LOG(INFO) << "recorded samples per 10ms: " << rec_samples_per_10ms_; | 390 LOG(INFO) << "recorded samples per 10ms: " << rec_samples_per_10ms_; |
375 LOG(INFO) << "recorded bytes per 10ms: " << rec_bytes_per_10ms_; | 391 LOG(INFO) << "recorded bytes per 10ms: " << rec_bytes_per_10ms_; |
376 // Allocate memory for the recording audio buffer. It will always contain | 392 // Allocate memory for the recording audio buffer. It will always contain |
377 // audio samples corresponding to 10ms of audio. | 393 // audio samples corresponding to 10ms of audio. |
378 rec_buffer_.reset(new int8_t[rec_bytes_per_10ms_]); | 394 rec_buffer_.reset(new int8_t[rec_bytes_per_10ms_]); |
379 } | 395 } |
380 | 396 |
381 void AudioDeviceBuffer::StartTimer() { | 397 void AudioDeviceBuffer::StartTimer() { |
398 num_stat_reports_ = 0; | |
382 last_log_stat_time_ = rtc::TimeMillis(); | 399 last_log_stat_time_ = rtc::TimeMillis(); |
383 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), | 400 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
384 kTimerIntervalInMilliseconds); | 401 kTimerIntervalInMilliseconds); |
385 } | 402 } |
386 | 403 |
387 void AudioDeviceBuffer::LogStats() { | 404 void AudioDeviceBuffer::LogStats() { |
388 RTC_DCHECK(task_queue_.IsCurrent()); | 405 RTC_DCHECK(task_queue_.IsCurrent()); |
389 | 406 |
390 int64_t now_time = rtc::TimeMillis(); | 407 int64_t now_time = rtc::TimeMillis(); |
391 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; | 408 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
392 int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_); | 409 int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_); |
393 last_log_stat_time_ = now_time; | 410 last_log_stat_time_ = now_time; |
394 | 411 |
395 // Log the latest statistics but skip the first 10 seconds since we are not | 412 // Log the latest statistics but skip the first 10 seconds since we are not |
396 // sure of the exact starting point. I.e., the first log printout will be | 413 // sure of the exact starting point. I.e., the first log printout will be |
397 // after ~20 seconds. | 414 // after ~20 seconds. |
398 if (++num_stat_reports_ > 1) { | 415 if (++num_stat_reports_ > 1) { |
399 uint32_t diff_samples = rec_samples_ - last_rec_samples_; | 416 uint32_t diff_samples = rec_samples_ - last_rec_samples_; |
400 uint32_t rate = diff_samples / kTimerIntervalInSeconds; | 417 uint32_t rate = diff_samples / kTimerIntervalInSeconds; |
401 LOG(INFO) << "[REC : " << time_since_last << "msec, " | 418 LOG(INFO) << "[REC : " << time_since_last << "msec, " |
402 << rec_sample_rate_ / 1000 | 419 << rec_sample_rate_ / 1000 |
403 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ | 420 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ |
404 << ", " | 421 << ", " |
405 << "samples: " << diff_samples << ", " | 422 << "samples: " << diff_samples << ", " |
406 << "rate: " << rate; | 423 << "rate: " << rate << ", " |
424 << "level: " << max_rec_level_; | |
407 | 425 |
408 diff_samples = play_samples_ - last_play_samples_; | 426 diff_samples = play_samples_ - last_play_samples_; |
409 rate = diff_samples / kTimerIntervalInSeconds; | 427 rate = diff_samples / kTimerIntervalInSeconds; |
410 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " | 428 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
411 << play_sample_rate_ / 1000 | 429 << play_sample_rate_ / 1000 |
412 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ | 430 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ |
413 << ", " | 431 << ", " |
414 << "samples: " << diff_samples << ", " | 432 << "samples: " << diff_samples << ", " |
415 << "rate: " << rate; | 433 << "rate: " << rate << ", " |
434 << "level: " << max_play_level_; | |
435 } | |
436 | |
437 // Count number of times we detect "no audio" corresponding to a case where | |
438 // all level measurements have been zero. | |
439 if (max_rec_level_ == 0) { | |
440 ++num_rec_level_is_zero_; | |
416 } | 441 } |
417 | 442 |
418 last_rec_callbacks_ = rec_callbacks_; | 443 last_rec_callbacks_ = rec_callbacks_; |
419 last_play_callbacks_ = play_callbacks_; | 444 last_play_callbacks_ = play_callbacks_; |
420 last_rec_samples_ = rec_samples_; | 445 last_rec_samples_ = rec_samples_; |
421 last_play_samples_ = play_samples_; | 446 last_play_samples_ = play_samples_; |
447 max_rec_level_ = 0; | |
448 max_play_level_ = 0; | |
422 | 449 |
423 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); | 450 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
424 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; | 451 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
425 | 452 |
426 // Update some stats but do it on the task queue to ensure that access of | 453 // Update some stats but do it on the task queue to ensure that access of |
427 // members is serialized hence avoiding usage of locks. | 454 // members is serialized hence avoiding usage of locks. |
428 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), | 455 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
429 time_to_wait_ms); | 456 time_to_wait_ms); |
430 } | 457 } |
431 | 458 |
432 void AudioDeviceBuffer::UpdateRecStats(size_t num_samples) { | 459 void AudioDeviceBuffer::ResetRecStats() { |
460 rec_callbacks_ = 0; | |
461 last_rec_callbacks_ = 0; | |
462 rec_samples_ = 0; | |
463 last_rec_samples_ = 0; | |
464 max_rec_level_ = 0; | |
465 num_rec_level_is_zero_ = 0; | |
466 } | |
467 | |
468 void AudioDeviceBuffer::ResetPlayStats() { | |
469 last_playout_time_ = rtc::TimeMillis(); | |
470 play_callbacks_ = 0; | |
471 last_play_callbacks_ = 0; | |
472 play_samples_ = 0; | |
473 last_play_samples_ = 0; | |
474 max_play_level_ = 0; | |
475 } | |
476 | |
477 void AudioDeviceBuffer::UpdateRecStats(const void* audio_buffer, | |
478 size_t num_samples) { | |
433 RTC_DCHECK(task_queue_.IsCurrent()); | 479 RTC_DCHECK(task_queue_.IsCurrent()); |
434 ++rec_callbacks_; | 480 ++rec_callbacks_; |
435 rec_samples_ += num_samples; | 481 rec_samples_ += num_samples; |
482 | |
483 // Find the max absolute value in an audio packet twice per second and update | |
484 // |max_rec_level_| to track the largest value. | |
485 if (rec_callbacks_ % 50 == 0) { | |
486 int16_t max_abs = WebRtcSpl_MaxAbsValueW16( | |
487 static_cast<int16_t*>(const_cast<void*>(audio_buffer)), | |
peah-webrtc
2016/09/09 09:06:27
Do we know that these samples are always int16_t s
henrika_webrtc
2016/09/09 11:55:01
Yes. But I can add some more stuff around it.
| |
488 num_samples * rec_channels_); | |
489 if (max_abs > max_rec_level_) { | |
490 max_rec_level_ = max_abs; | |
491 } | |
492 } | |
436 } | 493 } |
437 | 494 |
438 void AudioDeviceBuffer::UpdatePlayStats(size_t num_samples) { | 495 void AudioDeviceBuffer::UpdatePlayStats(const void* audio_buffer, |
496 size_t num_samples) { | |
439 RTC_DCHECK(task_queue_.IsCurrent()); | 497 RTC_DCHECK(task_queue_.IsCurrent()); |
440 ++play_callbacks_; | 498 ++play_callbacks_; |
441 play_samples_ += num_samples; | 499 play_samples_ += num_samples; |
500 | |
501 // Find the max absolute value in an audio packet twice per second and update | |
502 // |max_play_level_| to track the largest value. | |
503 if (play_callbacks_ % 50 == 0) { | |
504 int16_t max_abs = WebRtcSpl_MaxAbsValueW16( | |
505 static_cast<int16_t*>(const_cast<void*>(audio_buffer)), | |
peah-webrtc
2016/09/09 09:06:27
Do we know that these samples are always int16_t s
henrika_webrtc
2016/09/09 11:55:01
Yes.
| |
506 num_samples * play_channels_); | |
507 if (max_abs > max_play_level_) { | |
508 max_play_level_ = max_abs; | |
509 } | |
510 } | |
442 } | 511 } |
443 | 512 |
444 } // namespace webrtc | 513 } // namespace webrtc |
OLD | NEW |