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 12 matching lines...) Expand all Loading... | |
| 23 #include "webrtc/system_wrappers/include/metrics.h" | 23 #include "webrtc/system_wrappers/include/metrics.h" |
| 24 | 24 |
| 25 namespace webrtc { | 25 namespace webrtc { |
| 26 | 26 |
| 27 static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; | 27 static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; |
| 28 | 28 |
| 29 // Time between two sucessive calls to LogStats(). | 29 // Time between two sucessive calls to LogStats(). |
| 30 static const size_t kTimerIntervalInSeconds = 10; | 30 static const size_t kTimerIntervalInSeconds = 10; |
| 31 static const size_t kTimerIntervalInMilliseconds = | 31 static const size_t kTimerIntervalInMilliseconds = |
| 32 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; | 32 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; |
| 33 // Min time required to qualify an audio session as a "call". If playout or | |
| 34 // recording has been active for less than this time we will not store any | |
| 35 // logs or UMA stats but instead consider the call as too short. | |
| 36 static const size_t kMinValidCallTimeTimeInSeconds = 10; | |
| 37 static const size_t kMinValidCallTimeTimeInMilliseconds = | |
| 38 kMinValidCallTimeTimeInSeconds * rtc::kNumMillisecsPerSec; | |
| 33 | 39 |
| 34 AudioDeviceBuffer::AudioDeviceBuffer() | 40 AudioDeviceBuffer::AudioDeviceBuffer() |
| 35 : audio_transport_cb_(nullptr), | 41 : audio_transport_cb_(nullptr), |
| 36 task_queue_(kTimerQueueName), | 42 task_queue_(kTimerQueueName), |
| 37 timer_has_started_(false), | 43 playing_(false), |
| 44 recording_(false), | |
| 38 rec_sample_rate_(0), | 45 rec_sample_rate_(0), |
| 39 play_sample_rate_(0), | 46 play_sample_rate_(0), |
| 40 rec_channels_(0), | 47 rec_channels_(0), |
| 41 play_channels_(0), | 48 play_channels_(0), |
| 42 rec_bytes_per_sample_(0), | 49 rec_bytes_per_sample_(0), |
| 43 play_bytes_per_sample_(0), | 50 play_bytes_per_sample_(0), |
| 44 current_mic_level_(0), | 51 current_mic_level_(0), |
| 45 new_mic_level_(0), | 52 new_mic_level_(0), |
| 46 typing_status_(false), | 53 typing_status_(false), |
| 47 play_delay_ms_(0), | 54 play_delay_ms_(0), |
| 48 rec_delay_ms_(0), | 55 rec_delay_ms_(0), |
| 49 clock_drift_(0), | 56 clock_drift_(0), |
| 50 num_stat_reports_(0), | 57 num_stat_reports_(0), |
| 51 rec_callbacks_(0), | 58 rec_callbacks_(0), |
| 52 last_rec_callbacks_(0), | 59 last_rec_callbacks_(0), |
| 53 play_callbacks_(0), | 60 play_callbacks_(0), |
| 54 last_play_callbacks_(0), | 61 last_play_callbacks_(0), |
| 55 rec_samples_(0), | 62 rec_samples_(0), |
| 56 last_rec_samples_(0), | 63 last_rec_samples_(0), |
| 57 play_samples_(0), | 64 play_samples_(0), |
| 58 last_play_samples_(0), | 65 last_play_samples_(0), |
| 59 last_log_stat_time_(0), | 66 last_timer_task_time_(0), |
| 60 max_rec_level_(0), | 67 max_rec_level_(0), |
| 61 max_play_level_(0), | 68 max_play_level_(0), |
| 62 num_rec_level_is_zero_(0), | |
| 63 rec_stat_count_(0), | 69 rec_stat_count_(0), |
| 64 play_stat_count_(0) { | 70 play_stat_count_(0), |
| 71 play_start_time_(0), | |
| 72 rec_start_time_(0), | |
| 73 only_silence_recorded_(true) { | |
| 65 LOG(INFO) << "AudioDeviceBuffer::ctor"; | 74 LOG(INFO) << "AudioDeviceBuffer::ctor"; |
| 66 } | 75 } |
| 67 | 76 |
| 68 AudioDeviceBuffer::~AudioDeviceBuffer() { | 77 AudioDeviceBuffer::~AudioDeviceBuffer() { |
| 69 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 78 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 RTC_DCHECK(!playing_); | |
| 80 RTC_DCHECK(!recording_); | |
| 70 LOG(INFO) << "AudioDeviceBuffer::~dtor"; | 81 LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
| 71 | |
| 72 size_t total_diff_time = 0; | |
| 73 int num_measurements = 0; | |
| 74 LOG(INFO) << "[playout diff time => #measurements]"; | |
| 75 for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) { | |
| 76 uint32_t num_elements = playout_diff_times_[diff]; | |
| 77 if (num_elements > 0) { | |
| 78 total_diff_time += num_elements * diff; | |
| 79 num_measurements += num_elements; | |
| 80 LOG(INFO) << "[" << diff << " => " << num_elements << "]"; | |
| 81 } | |
| 82 } | |
| 83 if (num_measurements > 0) { | |
| 84 LOG(INFO) << "total_diff_time: " << total_diff_time; | |
| 85 LOG(INFO) << "num_measurements: " << num_measurements; | |
| 86 LOG(INFO) << "average: " | |
| 87 << static_cast<float>(total_diff_time) / num_measurements; | |
| 88 } | |
| 89 | |
| 90 // Add UMA histogram to keep track of the case when only zeros have been | |
| 91 // recorded. Ensure that recording callbacks have started and that at least | |
| 92 // one timer event has been able to update |num_rec_level_is_zero_|. | |
| 93 // I am avoiding use of the task queue here since we are under destruction | |
| 94 // and reading these members on the creating thread feels safe. | |
| 95 if (rec_callbacks_ > 0 && num_stat_reports_ > 0) { | |
| 96 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", | |
| 97 static_cast<int>(num_stat_reports_ == num_rec_level_is_zero_)); | |
| 98 } | |
| 99 } | 82 } |
| 100 | 83 |
| 101 int32_t AudioDeviceBuffer::RegisterAudioCallback( | 84 int32_t AudioDeviceBuffer::RegisterAudioCallback( |
| 102 AudioTransport* audio_callback) { | 85 AudioTransport* audio_callback) { |
| 103 LOG(INFO) << __FUNCTION__; | 86 LOG(INFO) << __FUNCTION__; |
| 104 rtc::CritScope lock(&lock_cb_); | 87 rtc::CritScope lock(&lock_cb_); |
| 105 audio_transport_cb_ = audio_callback; | 88 audio_transport_cb_ = audio_callback; |
| 106 return 0; | 89 return 0; |
| 107 } | 90 } |
| 108 | 91 |
| 109 int32_t AudioDeviceBuffer::InitPlayout() { | 92 void AudioDeviceBuffer::StartPlayout() { |
| 93 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 94 if (playing_) { | |
| 95 return; | |
| 96 } | |
| 110 LOG(INFO) << __FUNCTION__; | 97 LOG(INFO) << __FUNCTION__; |
| 111 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 98 // Clear members tracking playout stats and do it on the task queue. |
| 112 ResetPlayStats(); | 99 task_queue_.PostTask([this] { ResetPlayStats(); }); |
| 113 if (!timer_has_started_) { | 100 // Start a periodic timer based on task queue if not already done by the |
| 114 StartTimer(); | 101 // recording side. |
| 115 timer_has_started_ = true; | 102 if (!recording_) { |
| 103 StartPeriodicLogging(); | |
| 116 } | 104 } |
| 117 return 0; | 105 const uint64_t now_time = rtc::TimeMillis(); |
| 106 // Clear members that are only touched on the main (creating) thread. | |
| 107 play_start_time_ = now_time; | |
| 108 last_playout_time_ = now_time; | |
| 109 playing_ = true; | |
| 118 } | 110 } |
| 119 | 111 |
| 120 int32_t AudioDeviceBuffer::InitRecording() { | 112 void AudioDeviceBuffer::StartRecording() { |
| 113 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 114 if (recording_) { | |
| 115 return; | |
| 116 } | |
| 121 LOG(INFO) << __FUNCTION__; | 117 LOG(INFO) << __FUNCTION__; |
| 118 // Clear members tracking recording stats and do it on the task queue. | |
| 119 task_queue_.PostTask([this] { ResetRecStats(); }); | |
| 120 // Start a periodic timer based on task queue if not already done by the | |
| 121 // playout side. | |
| 122 if (!playing_) { | |
| 123 StartPeriodicLogging(); | |
| 124 } | |
| 125 // Clear members that will be touched on the main (creating) thread. | |
| 126 rec_start_time_ = rtc::TimeMillis(); | |
| 127 recording_ = true; | |
| 128 // And finally a member which can be modified on the native audio thread. | |
| 129 // It is safe to do so since we know by design that the owning ADM has not | |
| 130 // yet started the native audio recording. | |
| 131 only_silence_recorded_ = true; | |
| 132 } | |
| 133 | |
| 134 void AudioDeviceBuffer::StopPlayout() { | |
| 122 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 135 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 123 ResetRecStats(); | 136 if (!playing_) { |
| 124 if (!timer_has_started_) { | 137 return; |
| 125 StartTimer(); | |
| 126 timer_has_started_ = true; | |
| 127 } | 138 } |
| 128 return 0; | 139 LOG(INFO) << __FUNCTION__; |
| 140 playing_ = false; | |
| 141 // Stop periodic logging if no more media is active. | |
| 142 if (!recording_) { | |
| 143 StopPeriodicLogging(); | |
| 144 } | |
| 145 // Add diagnostic logging of delta times for playout callbacks. We are doing | |
| 146 // this wihout a lock since playout should be stopped by now and it a minor | |
| 147 // conflict during stop will not have a great impact on the total statistics. | |
| 148 const size_t time_since_start = rtc::TimeSince(play_start_time_); | |
| 149 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) { | |
| 150 size_t total_diff_time = 0; | |
| 151 int num_measurements = 0; | |
| 152 LOG(INFO) << "[playout diff time => #measurements]"; | |
| 153 for (size_t diff = 0; diff < arraysize(playout_diff_times_); ++diff) { | |
| 154 uint32_t num_elements = playout_diff_times_[diff]; | |
| 155 if (num_elements > 0) { | |
| 156 total_diff_time += num_elements * diff; | |
| 157 num_measurements += num_elements; | |
| 158 LOG(INFO) << "[" << diff << " => " << num_elements << "]"; | |
| 159 } | |
| 160 } | |
| 161 if (num_measurements > 0) { | |
| 162 LOG(INFO) << "total_diff_time: " << total_diff_time << ", " | |
| 163 << "num_measurements: " << num_measurements << ", " | |
| 164 << "average: " | |
| 165 << static_cast<float>(total_diff_time) / num_measurements; | |
| 166 } | |
| 167 } | |
| 168 LOG(INFO) << "total playout time: " << time_since_start; | |
| 169 } | |
| 170 | |
| 171 void AudioDeviceBuffer::StopRecording() { | |
| 172 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 173 if (!recording_) { | |
| 174 return; | |
| 175 } | |
| 176 LOG(INFO) << __FUNCTION__; | |
| 177 recording_ = false; | |
| 178 // Stop periodic logging if no more media is active. | |
| 179 if (!playing_) { | |
| 180 StopPeriodicLogging(); | |
| 181 } | |
| 182 // Add UMA histogram to keep track of the case when only zeros have been | |
| 183 // recorded. Measurements (max of absolute level) are taken twice per second, | |
| 184 // which means that if e.g 10 seconds of audio has been recorded, a total of | |
| 185 // 20 level estimates must all be identical to zero to trigger the histogram. | |
| 186 // |only_silence_recorded_| can only be cleared on the native audio thread | |
| 187 // that drives audio capture but we know by design that the audio has stopped | |
| 188 // when this method is called, hence there should not be aby conflicts. Also, | |
| 189 // the fact that |only_silence_recorded_| can be affected during the complete | |
| 190 // call makes chances of conflicts with potentially one last callback very | |
| 191 // small. | |
| 192 const size_t time_since_start = rtc::TimeSince(rec_start_time_); | |
| 193 if (time_since_start > kMinValidCallTimeTimeInMilliseconds) { | |
| 194 const int only_zeros = static_cast<int>(only_silence_recorded_); | |
| 195 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros); | |
| 196 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros; | |
| 197 } | |
| 198 LOG(INFO) << "total recording time: " << time_since_start; | |
| 129 } | 199 } |
| 130 | 200 |
| 131 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { | 201 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
| 132 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; | 202 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
| 133 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 203 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 134 rec_sample_rate_ = fsHz; | 204 rec_sample_rate_ = fsHz; |
| 135 return 0; | 205 return 0; |
| 136 } | 206 } |
| 137 | 207 |
| 138 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { | 208 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 }(); | 312 }(); |
| 243 // Copy the complete input buffer to the local buffer. | 313 // Copy the complete input buffer to the local buffer. |
| 244 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t); | 314 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t); |
| 245 const size_t old_size = rec_buffer_.size(); | 315 const size_t old_size = rec_buffer_.size(); |
| 246 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); | 316 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); |
| 247 // Keep track of the size of the recording buffer. Only updated when the | 317 // Keep track of the size of the recording buffer. Only updated when the |
| 248 // size changes, which is a rare event. | 318 // size changes, which is a rare event. |
| 249 if (old_size != rec_buffer_.size()) { | 319 if (old_size != rec_buffer_.size()) { |
| 250 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size(); | 320 LOG(LS_INFO) << "Size of recording buffer: " << rec_buffer_.size(); |
| 251 } | 321 } |
| 252 // Derive a new level value twice per second. | 322 // Derive a new level value twice per second and check if it is non-zero. |
| 253 int16_t max_abs = 0; | 323 int16_t max_abs = 0; |
| 254 RTC_DCHECK_LT(rec_stat_count_, 50); | 324 RTC_DCHECK_LT(rec_stat_count_, 50); |
| 255 if (++rec_stat_count_ >= 50) { | 325 if (++rec_stat_count_ >= 50) { |
| 256 const size_t size = num_samples * rec_channels; | 326 const size_t size = num_samples * rec_channels; |
| 257 // Returns the largest absolute value in a signed 16-bit vector. | 327 // Returns the largest absolute value in a signed 16-bit vector. |
| 258 max_abs = WebRtcSpl_MaxAbsValueW16( | 328 max_abs = WebRtcSpl_MaxAbsValueW16( |
| 259 reinterpret_cast<const int16_t*>(rec_buffer_.data()), size); | 329 reinterpret_cast<const int16_t*>(rec_buffer_.data()), size); |
| 260 rec_stat_count_ = 0; | 330 rec_stat_count_ = 0; |
| 331 // Set |only_silence_recorded_| to false as soon as at least one detection | |
| 332 // of a non-zero audio packet is found. It can only be restored to true | |
| 333 // again by restarting the call. | |
| 334 if (max_abs > 0) { | |
| 335 only_silence_recorded_ = false; | |
| 336 } | |
| 261 } | 337 } |
| 262 // Update some stats but do it on the task queue to ensure that the members | 338 // Update some stats but do it on the task queue to ensure that the members |
| 263 // are modified and read on the same thread. Note that |max_abs| will be | 339 // are modified and read on the same thread. Note that |max_abs| will be |
| 264 // zero in most calls and then have no effect of the stats. It is only updated | 340 // zero in most calls and then have no effect of the stats. It is only updated |
| 265 // approximately two times per second and can then change the stats. | 341 // approximately two times per second and can then change the stats. |
| 266 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, | 342 task_queue_.PostTask( |
| 267 max_abs, num_samples)); | 343 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); |
| 268 return 0; | 344 return 0; |
| 269 } | 345 } |
| 270 | 346 |
| 271 int32_t AudioDeviceBuffer::DeliverRecordedData() { | 347 int32_t AudioDeviceBuffer::DeliverRecordedData() { |
| 272 rtc::CritScope lock(&lock_cb_); | 348 rtc::CritScope lock(&lock_cb_); |
| 273 if (!audio_transport_cb_) { | 349 if (!audio_transport_cb_) { |
| 274 LOG(LS_WARNING) << "Invalid audio transport"; | 350 LOG(LS_WARNING) << "Invalid audio transport"; |
| 275 return 0; | 351 return 0; |
| 276 } | 352 } |
| 277 const size_t rec_bytes_per_sample = [&] { | 353 const size_t rec_bytes_per_sample = [&] { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 // The consumer can change the request size on the fly and we therefore | 389 // The consumer can change the request size on the fly and we therefore |
| 314 // resize the buffer accordingly. Also takes place at the first call to this | 390 // resize the buffer accordingly. Also takes place at the first call to this |
| 315 // method. | 391 // method. |
| 316 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); | 392 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); |
| 317 const size_t size_in_bytes = num_samples * play_bytes_per_sample; | 393 const size_t size_in_bytes = num_samples * play_bytes_per_sample; |
| 318 if (play_buffer_.size() != size_in_bytes) { | 394 if (play_buffer_.size() != size_in_bytes) { |
| 319 play_buffer_.SetSize(size_in_bytes); | 395 play_buffer_.SetSize(size_in_bytes); |
| 320 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); | 396 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); |
| 321 } | 397 } |
| 322 | 398 |
| 323 rtc::CritScope lock(&lock_cb_); | 399 size_t num_samples_out(0); |
| 400 { | |
| 401 rtc::CritScope lock(&lock_cb_); | |
| 324 | 402 |
| 325 // It is currently supported to start playout without a valid audio | 403 // It is currently supported to start playout without a valid audio |
| 326 // transport object. Leads to warning and silence. | 404 // transport object. Leads to warning and silence. |
| 327 if (!audio_transport_cb_) { | 405 if (!audio_transport_cb_) { |
| 328 LOG(LS_WARNING) << "Invalid audio transport"; | 406 LOG(LS_WARNING) << "Invalid audio transport"; |
| 329 return 0; | 407 return 0; |
| 330 } | 408 } |
| 331 | 409 |
| 332 // Retrieve new 16-bit PCM audio data using the audio transport instance. | 410 // Retrieve new 16-bit PCM audio data using the audio transport instance. |
| 333 int64_t elapsed_time_ms = -1; | 411 int64_t elapsed_time_ms = -1; |
| 334 int64_t ntp_time_ms = -1; | 412 int64_t ntp_time_ms = -1; |
| 335 size_t num_samples_out(0); | 413 uint32_t res = audio_transport_cb_->NeedMorePlayData( |
| 336 uint32_t res = audio_transport_cb_->NeedMorePlayData( | 414 num_samples, play_bytes_per_sample_, play_channels, play_sample_rate_, |
| 337 num_samples, play_bytes_per_sample_, play_channels, play_sample_rate_, | 415 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); |
| 338 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms); | 416 if (res != 0) { |
| 339 if (res != 0) { | 417 LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
| 340 LOG(LS_ERROR) << "NeedMorePlayData() failed"; | 418 } |
| 341 } | 419 } |
| 342 | 420 |
| 343 // Derive a new level value twice per second. | 421 // Derive a new level value twice per second. |
| 344 int16_t max_abs = 0; | 422 int16_t max_abs = 0; |
| 345 RTC_DCHECK_LT(play_stat_count_, 50); | 423 RTC_DCHECK_LT(play_stat_count_, 50); |
| 346 if (++play_stat_count_ >= 50) { | 424 if (++play_stat_count_ >= 50) { |
| 347 const size_t size = num_samples * play_channels; | 425 const size_t size = num_samples * play_channels; |
| 348 // Returns the largest absolute value in a signed 16-bit vector. | 426 // Returns the largest absolute value in a signed 16-bit vector. |
| 349 max_abs = WebRtcSpl_MaxAbsValueW16( | 427 max_abs = WebRtcSpl_MaxAbsValueW16( |
| 350 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); | 428 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); |
| 351 play_stat_count_ = 0; | 429 play_stat_count_ = 0; |
| 352 } | 430 } |
| 353 // Update some stats but do it on the task queue to ensure that the members | 431 // Update some stats but do it on the task queue to ensure that the members |
| 354 // are modified and read on the same thread. Note that |max_abs| will be | 432 // are modified and read on the same thread. Note that |max_abs| will be |
| 355 // zero in most calls and then have no effect of the stats. It is only updated | 433 // zero in most calls and then have no effect of the stats. It is only updated |
| 356 // approximately two times per second and can then change the stats. | 434 // approximately two times per second and can then change the stats. |
| 357 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, | 435 task_queue_.PostTask([this, max_abs, num_samples_out] { |
| 358 max_abs, num_samples_out)); | 436 UpdatePlayStats(max_abs, num_samples_out); |
| 437 }); | |
| 359 return static_cast<int32_t>(num_samples_out); | 438 return static_cast<int32_t>(num_samples_out); |
| 360 } | 439 } |
| 361 | 440 |
| 362 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { | 441 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { |
| 363 RTC_DCHECK_GT(play_buffer_.size(), 0u); | 442 RTC_DCHECK_GT(play_buffer_.size(), 0u); |
| 364 const size_t play_bytes_per_sample = [&] { | 443 const size_t play_bytes_per_sample = [&] { |
| 365 rtc::CritScope lock(&lock_); | 444 rtc::CritScope lock(&lock_); |
| 366 return play_bytes_per_sample_; | 445 return play_bytes_per_sample_; |
| 367 }(); | 446 }(); |
| 368 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); | 447 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); |
| 369 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); | 448 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); |
| 370 } | 449 } |
| 371 | 450 |
| 372 void AudioDeviceBuffer::StartTimer() { | 451 void AudioDeviceBuffer::StartPeriodicLogging() { |
| 373 num_stat_reports_ = 0; | 452 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
| 374 last_log_stat_time_ = rtc::TimeMillis(); | 453 AudioDeviceBuffer::LOG_START)); |
| 375 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), | |
| 376 kTimerIntervalInMilliseconds); | |
| 377 } | 454 } |
| 378 | 455 |
| 379 void AudioDeviceBuffer::LogStats() { | 456 void AudioDeviceBuffer::StopPeriodicLogging() { |
| 457 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, | |
| 458 AudioDeviceBuffer::LOG_STOP)); | |
| 459 } | |
| 460 | |
| 461 void AudioDeviceBuffer::LogStats(LogState state) { | |
| 380 RTC_DCHECK(task_queue_.IsCurrent()); | 462 RTC_DCHECK(task_queue_.IsCurrent()); |
| 463 int64_t now_time = rtc::TimeMillis(); | |
| 464 if (state == AudioDeviceBuffer::LOG_START) { | |
| 465 // Reset counters at start. We will not add any logging in this state but | |
| 466 // the timer will started by posting a new (delayed) task. | |
| 467 num_stat_reports_ = 0; | |
| 468 last_timer_task_time_ = now_time; | |
| 469 } else if (state == AudioDeviceBuffer::LOG_STOP) { | |
| 470 // Stop logging and posting new tasks. | |
| 471 return; | |
| 472 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) { | |
| 473 // Default state. Just keep on logging. | |
| 474 } | |
| 381 | 475 |
| 382 int64_t now_time = rtc::TimeMillis(); | |
| 383 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; | 476 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
| 384 int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_); | 477 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); |
| 385 last_log_stat_time_ = now_time; | 478 last_timer_task_time_ = now_time; |
| 386 | 479 |
| 387 // Log the latest statistics but skip the first 10 seconds since we are not | 480 // Log the latest statistics but skip the first round just after state was |
| 388 // sure of the exact starting point. I.e., the first log printout will be | 481 // set to kLogStart. Hence, first printed log will be after ~10 seconds. |
|
tommi
2016/10/31 14:18:03
LOG_START
henrika_webrtc
2016/10/31 14:52:48
Done.
| |
| 389 // after ~20 seconds. | |
| 390 if (++num_stat_reports_ > 1 && time_since_last > 0) { | 482 if (++num_stat_reports_ > 1 && time_since_last > 0) { |
| 391 uint32_t diff_samples = rec_samples_ - last_rec_samples_; | 483 uint32_t diff_samples = rec_samples_ - last_rec_samples_; |
| 392 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); | 484 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
| 393 LOG(INFO) << "[REC : " << time_since_last << "msec, " | 485 LOG(INFO) << "[REC : " << time_since_last << "msec, " |
| 394 << rec_sample_rate_ / 1000 | 486 << rec_sample_rate_ / 1000 |
| 395 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ | 487 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ |
| 396 << ", " | 488 << ", " |
| 397 << "samples: " << diff_samples << ", " | 489 << "samples: " << diff_samples << ", " |
| 398 << "rate: " << static_cast<int>(rate + 0.5) << ", " | 490 << "rate: " << static_cast<int>(rate + 0.5) << ", " |
| 399 << "level: " << max_rec_level_; | 491 << "level: " << max_rec_level_; |
| 400 | 492 |
| 401 diff_samples = play_samples_ - last_play_samples_; | 493 diff_samples = play_samples_ - last_play_samples_; |
| 402 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); | 494 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); |
| 403 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " | 495 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
| 404 << play_sample_rate_ / 1000 | 496 << play_sample_rate_ / 1000 |
| 405 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ | 497 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ |
| 406 << ", " | 498 << ", " |
| 407 << "samples: " << diff_samples << ", " | 499 << "samples: " << diff_samples << ", " |
| 408 << "rate: " << static_cast<int>(rate + 0.5) << ", " | 500 << "rate: " << static_cast<int>(rate + 0.5) << ", " |
| 409 << "level: " << max_play_level_; | 501 << "level: " << max_play_level_; |
| 410 } | 502 } |
| 411 | 503 |
| 412 // Count number of times we detect "no audio" corresponding to a case where | |
| 413 // all level measurements have been zero. | |
| 414 if (max_rec_level_ == 0) { | |
| 415 ++num_rec_level_is_zero_; | |
| 416 } | |
| 417 | |
| 418 last_rec_callbacks_ = rec_callbacks_; | 504 last_rec_callbacks_ = rec_callbacks_; |
| 419 last_play_callbacks_ = play_callbacks_; | 505 last_play_callbacks_ = play_callbacks_; |
| 420 last_rec_samples_ = rec_samples_; | 506 last_rec_samples_ = rec_samples_; |
| 421 last_play_samples_ = play_samples_; | 507 last_play_samples_ = play_samples_; |
| 422 max_rec_level_ = 0; | 508 max_rec_level_ = 0; |
| 423 max_play_level_ = 0; | 509 max_play_level_ = 0; |
| 424 | 510 |
| 425 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); | 511 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
| 426 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; | 512 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
| 427 | 513 |
| 428 // Update some stats but do it on the task queue to ensure that access of | 514 // Keep posting new (delayed) tasks until state is changed to kLogStop. |
| 429 // members is serialized hence avoiding usage of locks. | 515 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, |
| 430 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), | 516 AudioDeviceBuffer::LOG_ACTIVE), |
| 431 time_to_wait_ms); | 517 time_to_wait_ms); |
| 432 } | 518 } |
| 433 | 519 |
| 434 void AudioDeviceBuffer::ResetRecStats() { | 520 void AudioDeviceBuffer::ResetRecStats() { |
| 521 RTC_DCHECK(task_queue_.IsCurrent()); | |
| 435 rec_callbacks_ = 0; | 522 rec_callbacks_ = 0; |
| 436 last_rec_callbacks_ = 0; | 523 last_rec_callbacks_ = 0; |
| 437 rec_samples_ = 0; | 524 rec_samples_ = 0; |
| 438 last_rec_samples_ = 0; | 525 last_rec_samples_ = 0; |
| 439 max_rec_level_ = 0; | 526 max_rec_level_ = 0; |
| 440 num_rec_level_is_zero_ = 0; | |
| 441 } | 527 } |
| 442 | 528 |
| 443 void AudioDeviceBuffer::ResetPlayStats() { | 529 void AudioDeviceBuffer::ResetPlayStats() { |
| 444 last_playout_time_ = rtc::TimeMillis(); | 530 RTC_DCHECK(task_queue_.IsCurrent()); |
| 445 play_callbacks_ = 0; | 531 play_callbacks_ = 0; |
| 446 last_play_callbacks_ = 0; | 532 last_play_callbacks_ = 0; |
| 447 play_samples_ = 0; | 533 play_samples_ = 0; |
| 448 last_play_samples_ = 0; | 534 last_play_samples_ = 0; |
| 449 max_play_level_ = 0; | 535 max_play_level_ = 0; |
| 450 } | 536 } |
| 451 | 537 |
| 452 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { | 538 void AudioDeviceBuffer::UpdateRecStats(int16_t max_abs, size_t num_samples) { |
| 453 RTC_DCHECK(task_queue_.IsCurrent()); | 539 RTC_DCHECK(task_queue_.IsCurrent()); |
| 454 ++rec_callbacks_; | 540 ++rec_callbacks_; |
| 455 rec_samples_ += num_samples; | 541 rec_samples_ += num_samples; |
| 456 if (max_abs > max_rec_level_) { | 542 if (max_abs > max_rec_level_) { |
| 457 max_rec_level_ = max_abs; | 543 max_rec_level_ = max_abs; |
| 458 } | 544 } |
| 459 } | 545 } |
| 460 | 546 |
| 461 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { | 547 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { |
| 462 RTC_DCHECK(task_queue_.IsCurrent()); | 548 RTC_DCHECK(task_queue_.IsCurrent()); |
| 463 ++play_callbacks_; | 549 ++play_callbacks_; |
| 464 play_samples_ += num_samples; | 550 play_samples_ += num_samples; |
| 465 if (max_abs > max_play_level_) { | 551 if (max_abs > max_play_level_) { |
| 466 max_play_level_ = max_abs; | 552 max_play_level_ = max_abs; |
| 467 } | 553 } |
| 468 } | 554 } |
| 469 | 555 |
| 470 } // namespace webrtc | 556 } // namespace webrtc |
| OLD | NEW |