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