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

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

Issue 2466613002: Adds thread safety annotations to the AudioDeviceBuffer class (Closed)
Patch Set: Adds race checker and removes test of ResetAudioDevice Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 28 matching lines...) Expand all
39 39
40 AudioDeviceBuffer::AudioDeviceBuffer() 40 AudioDeviceBuffer::AudioDeviceBuffer()
41 : audio_transport_cb_(nullptr), 41 : audio_transport_cb_(nullptr),
42 task_queue_(kTimerQueueName), 42 task_queue_(kTimerQueueName),
43 playing_(false), 43 playing_(false),
44 recording_(false), 44 recording_(false),
45 rec_sample_rate_(0), 45 rec_sample_rate_(0),
46 play_sample_rate_(0), 46 play_sample_rate_(0),
47 rec_channels_(0), 47 rec_channels_(0),
48 play_channels_(0), 48 play_channels_(0),
49 rec_bytes_per_sample_(0),
50 play_bytes_per_sample_(0),
51 current_mic_level_(0), 49 current_mic_level_(0),
52 new_mic_level_(0), 50 new_mic_level_(0),
53 typing_status_(false), 51 typing_status_(false),
54 play_delay_ms_(0), 52 play_delay_ms_(0),
55 rec_delay_ms_(0), 53 rec_delay_ms_(0),
56 clock_drift_(0), 54 clock_drift_(0),
57 num_stat_reports_(0), 55 num_stat_reports_(0),
58 rec_callbacks_(0), 56 rec_callbacks_(0),
59 last_rec_callbacks_(0), 57 last_rec_callbacks_(0),
60 play_callbacks_(0), 58 play_callbacks_(0),
(...skipping 16 matching lines...) Expand all
77 AudioDeviceBuffer::~AudioDeviceBuffer() { 75 AudioDeviceBuffer::~AudioDeviceBuffer() {
78 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 76 RTC_DCHECK(thread_checker_.CalledOnValidThread());
79 RTC_DCHECK(!playing_); 77 RTC_DCHECK(!playing_);
80 RTC_DCHECK(!recording_); 78 RTC_DCHECK(!recording_);
81 LOG(INFO) << "AudioDeviceBuffer::~dtor"; 79 LOG(INFO) << "AudioDeviceBuffer::~dtor";
82 } 80 }
83 81
84 int32_t AudioDeviceBuffer::RegisterAudioCallback( 82 int32_t AudioDeviceBuffer::RegisterAudioCallback(
85 AudioTransport* audio_callback) { 83 AudioTransport* audio_callback) {
86 LOG(INFO) << __FUNCTION__; 84 LOG(INFO) << __FUNCTION__;
87 rtc::CritScope lock(&lock_cb_); 85 if (playing_ || recording_) {
86 LOG(LS_ERROR) << "Failed to set audio transport since media was active";
87 return -1;
88 }
89 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
88 audio_transport_cb_ = audio_callback; 90 audio_transport_cb_ = audio_callback;
89 return 0; 91 return 0;
90 } 92 }
91 93
92 void AudioDeviceBuffer::StartPlayout() { 94 void AudioDeviceBuffer::StartPlayout() {
93 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 95 RTC_DCHECK(thread_checker_.CalledOnValidThread());
94 // TODO(henrika): allow for usage of DCHECK(!playing_) here instead. Today the 96 // 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 97 // ADM allows calling Start(), Start() by ignoring the second call but it
96 // makes more sense to only allow one call. 98 // makes more sense to only allow one call.
97 if (playing_) { 99 if (playing_) {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 const int only_zeros = static_cast<int>(only_silence_recorded_); 199 const int only_zeros = static_cast<int>(only_silence_recorded_);
198 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros); 200 RTC_HISTOGRAM_BOOLEAN("WebRTC.Audio.RecordedOnlyZeros", only_zeros);
199 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros; 201 LOG(INFO) << "HISTOGRAM(WebRTC.Audio.RecordedOnlyZeros): " << only_zeros;
200 } 202 }
201 LOG(INFO) << "total recording time: " << time_since_start; 203 LOG(INFO) << "total recording time: " << time_since_start;
202 } 204 }
203 205
204 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { 206 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) {
205 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; 207 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")";
206 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 208 RTC_DCHECK(thread_checker_.CalledOnValidThread());
209 rtc::CritScope lock(&lock_);
kwiberg-webrtc 2016/11/01 15:53:50 If every call to this function is on the same thre
henrika_webrtc 2016/11/02 10:29:17 Please correct me if I am wrong but: I want to pro
kwiberg-webrtc 2016/11/02 11:27:56 Yes, for that situation you need a lock. I don't
207 rec_sample_rate_ = fsHz; 210 rec_sample_rate_ = fsHz;
208 return 0; 211 return 0;
209 } 212 }
210 213
211 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { 214 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) {
212 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; 215 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")";
213 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 216 RTC_DCHECK(thread_checker_.CalledOnValidThread());
217 rtc::CritScope lock(&lock_);
kwiberg-webrtc 2016/11/01 15:53:50 Again, you shouldn't need both the thread checker
henrika_webrtc 2016/11/02 10:29:18 see above
214 play_sample_rate_ = fsHz; 218 play_sample_rate_ = fsHz;
215 return 0; 219 return 0;
216 } 220 }
217 221
218 int32_t AudioDeviceBuffer::RecordingSampleRate() const { 222 int32_t AudioDeviceBuffer::RecordingSampleRate() const {
223 rtc::CritScope lock(&lock_);
219 return rec_sample_rate_; 224 return rec_sample_rate_;
220 } 225 }
221 226
222 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { 227 int32_t AudioDeviceBuffer::PlayoutSampleRate() const {
228 rtc::CritScope lock(&lock_);
223 return play_sample_rate_; 229 return play_sample_rate_;
224 } 230 }
225 231
226 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { 232 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) {
227 LOG(INFO) << "SetRecordingChannels(" << channels << ")"; 233 LOG(INFO) << "SetRecordingChannels(" << channels << ")";
234 RTC_DCHECK(thread_checker_.CalledOnValidThread());
228 rtc::CritScope lock(&lock_); 235 rtc::CritScope lock(&lock_);
229 rec_channels_ = channels; 236 rec_channels_ = channels;
230 rec_bytes_per_sample_ = sizeof(int16_t) * channels;
231 return 0; 237 return 0;
232 } 238 }
233 239
234 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { 240 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) {
235 LOG(INFO) << "SetPlayoutChannels(" << channels << ")"; 241 LOG(INFO) << "SetPlayoutChannels(" << channels << ")";
242 RTC_DCHECK(thread_checker_.CalledOnValidThread());
236 rtc::CritScope lock(&lock_); 243 rtc::CritScope lock(&lock_);
237 play_channels_ = channels; 244 play_channels_ = channels;
238 play_bytes_per_sample_ = sizeof(int16_t) * channels;
239 return 0; 245 return 0;
240 } 246 }
241 247
242 int32_t AudioDeviceBuffer::SetRecordingChannel( 248 int32_t AudioDeviceBuffer::SetRecordingChannel(
243 const AudioDeviceModule::ChannelType channel) { 249 const AudioDeviceModule::ChannelType channel) {
244 LOG(INFO) << "SetRecordingChannel(" << channel << ")"; 250 LOG(INFO) << "SetRecordingChannel(" << channel << ")";
245 LOG(LS_WARNING) << "Not implemented"; 251 LOG(LS_WARNING) << "Not implemented";
246 // Add DCHECK to ensure that user does not try to use this API with a non- 252 // Add DCHECK to ensure that user does not try to use this API with a non-
247 // default parameter. 253 // default parameter.
248 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth); 254 RTC_DCHECK_EQ(channel, AudioDeviceModule::kChannelBoth);
249 return -1; 255 return -1;
250 } 256 }
251 257
252 int32_t AudioDeviceBuffer::RecordingChannel( 258 int32_t AudioDeviceBuffer::RecordingChannel(
253 AudioDeviceModule::ChannelType& channel) const { 259 AudioDeviceModule::ChannelType& channel) const {
254 LOG(LS_WARNING) << "Not implemented"; 260 LOG(LS_WARNING) << "Not implemented";
255 return -1; 261 return -1;
256 } 262 }
257 263
258 size_t AudioDeviceBuffer::RecordingChannels() const { 264 size_t AudioDeviceBuffer::RecordingChannels() const {
265 rtc::CritScope lock(&lock_);
259 return rec_channels_; 266 return rec_channels_;
260 } 267 }
261 268
262 size_t AudioDeviceBuffer::PlayoutChannels() const { 269 size_t AudioDeviceBuffer::PlayoutChannels() const {
270 rtc::CritScope lock(&lock_);
263 return play_channels_; 271 return play_channels_;
264 } 272 }
265 273
266 int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) { 274 int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) {
267 current_mic_level_ = level; 275 current_mic_level_ = level;
268 return 0; 276 return 0;
269 } 277 }
270 278
271 int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) { 279 int32_t AudioDeviceBuffer::SetTypingStatus(bool typing_status) {
272 typing_status_ = typing_status; 280 typing_status_ = typing_status;
(...skipping 29 matching lines...) Expand all
302 return 0; 310 return 0;
303 } 311 }
304 312
305 int32_t AudioDeviceBuffer::StopOutputFileRecording() { 313 int32_t AudioDeviceBuffer::StopOutputFileRecording() {
306 LOG(LS_WARNING) << "Not implemented"; 314 LOG(LS_WARNING) << "Not implemented";
307 return 0; 315 return 0;
308 } 316 }
309 317
310 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer, 318 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audio_buffer,
311 size_t num_samples) { 319 size_t num_samples) {
320 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
312 const size_t rec_channels = [&] { 321 const size_t rec_channels = [&] {
313 rtc::CritScope lock(&lock_); 322 rtc::CritScope lock(&lock_);
314 return rec_channels_; 323 return rec_channels_;
315 }(); 324 }();
316 // Copy the complete input buffer to the local buffer. 325 // Copy the complete input buffer to the local buffer.
317 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t); 326 const size_t size_in_bytes = num_samples * rec_channels * sizeof(int16_t);
318 const size_t old_size = rec_buffer_.size(); 327 const size_t old_size = rec_buffer_.size();
319 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes); 328 rec_buffer_.SetData(static_cast<const uint8_t*>(audio_buffer), size_in_bytes);
320 // Keep track of the size of the recording buffer. Only updated when the 329 // Keep track of the size of the recording buffer. Only updated when the
321 // size changes, which is a rare event. 330 // size changes, which is a rare event.
(...skipping 19 matching lines...) Expand all
341 // Update some stats but do it on the task queue to ensure that the members 350 // Update some stats but do it on the task queue to ensure that the members
342 // are modified and read on the same thread. Note that |max_abs| will be 351 // are modified and read on the same thread. Note that |max_abs| will be
343 // zero in most calls and then have no effect of the stats. It is only updated 352 // zero in most calls and then have no effect of the stats. It is only updated
344 // approximately two times per second and can then change the stats. 353 // approximately two times per second and can then change the stats.
345 task_queue_.PostTask( 354 task_queue_.PostTask(
346 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); }); 355 [this, max_abs, num_samples] { UpdateRecStats(max_abs, num_samples); });
347 return 0; 356 return 0;
348 } 357 }
349 358
350 int32_t AudioDeviceBuffer::DeliverRecordedData() { 359 int32_t AudioDeviceBuffer::DeliverRecordedData() {
351 rtc::CritScope lock(&lock_cb_); 360 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
352 if (!audio_transport_cb_) { 361 if (!audio_transport_cb_) {
353 LOG(LS_WARNING) << "Invalid audio transport"; 362 LOG(LS_WARNING) << "Invalid audio transport";
354 return 0; 363 return 0;
355 } 364 }
356 const size_t rec_bytes_per_sample = [&] { 365 const size_t rec_channels = [&] {
357 rtc::CritScope lock(&lock_); 366 rtc::CritScope lock(&lock_);
358 return rec_bytes_per_sample_; 367 return rec_channels_;
359 }(); 368 }();
369 const size_t rec_sample_rate = [&] {
370 rtc::CritScope lock(&lock_);
371 return rec_sample_rate_;
372 }();
kwiberg-webrtc 2016/11/01 15:53:50 By taking the lock twice like this, you pay twice
henrika_webrtc 2016/11/02 10:29:18 Removed lambda.
373 const size_t rec_bytes_per_sample = rec_channels * sizeof(int16_t);
360 uint32_t new_mic_level(0); 374 uint32_t new_mic_level(0);
361 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_; 375 uint32_t total_delay_ms = play_delay_ms_ + rec_delay_ms_;
362 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample; 376 size_t num_samples = rec_buffer_.size() / rec_bytes_per_sample;
363 int32_t res = audio_transport_cb_->RecordedDataIsAvailable( 377 int32_t res = audio_transport_cb_->RecordedDataIsAvailable(
364 rec_buffer_.data(), num_samples, rec_bytes_per_sample_, rec_channels_, 378 rec_buffer_.data(), num_samples, rec_bytes_per_sample, rec_channels,
365 rec_sample_rate_, total_delay_ms, clock_drift_, current_mic_level_, 379 rec_sample_rate, total_delay_ms, clock_drift_, current_mic_level_,
366 typing_status_, new_mic_level); 380 typing_status_, new_mic_level);
367 if (res != -1) { 381 if (res != -1) {
368 new_mic_level_ = new_mic_level; 382 new_mic_level_ = new_mic_level;
369 } else { 383 } else {
370 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed"; 384 LOG(LS_ERROR) << "RecordedDataIsAvailable() failed";
371 } 385 }
372 return 0; 386 return 0;
373 } 387 }
374 388
375 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) { 389 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t num_samples) {
390 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
376 // Measure time since last function call and update an array where the 391 // Measure time since last function call and update an array where the
377 // position/index corresponds to time differences (in milliseconds) between 392 // position/index corresponds to time differences (in milliseconds) between
378 // two successive playout callbacks, and the stored value is the number of 393 // two successive playout callbacks, and the stored value is the number of
379 // times a given time difference was found. 394 // times a given time difference was found.
380 int64_t now_time = rtc::TimeMillis(); 395 int64_t now_time = rtc::TimeMillis();
381 size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_); 396 size_t diff_time = rtc::TimeDiff(now_time, last_playout_time_);
382 // Truncate at 500ms to limit the size of the array. 397 // Truncate at 500ms to limit the size of the array.
383 diff_time = std::min(kMaxDeltaTimeInMs, diff_time); 398 diff_time = std::min(kMaxDeltaTimeInMs, diff_time);
384 last_playout_time_ = now_time; 399 last_playout_time_ = now_time;
385 playout_diff_times_[diff_time]++; 400 playout_diff_times_[diff_time]++;
386 401
387 const size_t play_channels = [&] { 402 const size_t play_channels = [&] {
388 rtc::CritScope lock(&lock_); 403 rtc::CritScope lock(&lock_);
389 return play_channels_; 404 return play_channels_;
390 }(); 405 }();
406 const size_t play_sample_rate = [&] {
407 rtc::CritScope lock(&lock_);
408 return play_sample_rate_;
409 }();
391 410
392 // The consumer can change the request size on the fly and we therefore 411 // The consumer can change the request size on the fly and we therefore
393 // resize the buffer accordingly. Also takes place at the first call to this 412 // resize the buffer accordingly. Also takes place at the first call to this
394 // method. 413 // method.
395 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t); 414 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t);
396 const size_t size_in_bytes = num_samples * play_bytes_per_sample; 415 const size_t size_in_bytes = num_samples * play_bytes_per_sample;
397 if (play_buffer_.size() != size_in_bytes) { 416 if (play_buffer_.size() != size_in_bytes) {
398 play_buffer_.SetSize(size_in_bytes); 417 play_buffer_.SetSize(size_in_bytes);
399 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size(); 418 LOG(LS_INFO) << "Size of playout buffer: " << play_buffer_.size();
400 } 419 }
401 420
402 size_t num_samples_out(0); 421 size_t num_samples_out(0);
403 { 422 // It is currently supported to start playout without a valid audio
404 rtc::CritScope lock(&lock_cb_); 423 // transport object. Leads to warning and silence.
424 if (!audio_transport_cb_) {
425 LOG(LS_WARNING) << "Invalid audio transport";
426 return 0;
427 }
405 428
406 // It is currently supported to start playout without a valid audio 429 // Retrieve new 16-bit PCM audio data using the audio transport instance.
407 // transport object. Leads to warning and silence. 430 int64_t elapsed_time_ms = -1;
408 if (!audio_transport_cb_) { 431 int64_t ntp_time_ms = -1;
409 LOG(LS_WARNING) << "Invalid audio transport"; 432 uint32_t res = audio_transport_cb_->NeedMorePlayData(
410 return 0; 433 num_samples, play_bytes_per_sample, play_channels, play_sample_rate,
411 } 434 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
412 435 if (res != 0) {
413 // Retrieve new 16-bit PCM audio data using the audio transport instance. 436 LOG(LS_ERROR) << "NeedMorePlayData() failed";
414 int64_t elapsed_time_ms = -1;
415 int64_t ntp_time_ms = -1;
416 uint32_t res = audio_transport_cb_->NeedMorePlayData(
417 num_samples, play_bytes_per_sample_, play_channels, play_sample_rate_,
418 play_buffer_.data(), num_samples_out, &elapsed_time_ms, &ntp_time_ms);
419 if (res != 0) {
420 LOG(LS_ERROR) << "NeedMorePlayData() failed";
421 }
422 } 437 }
423 438
424 // Derive a new level value twice per second. 439 // Derive a new level value twice per second.
425 int16_t max_abs = 0; 440 int16_t max_abs = 0;
426 RTC_DCHECK_LT(play_stat_count_, 50); 441 RTC_DCHECK_LT(play_stat_count_, 50);
427 if (++play_stat_count_ >= 50) { 442 if (++play_stat_count_ >= 50) {
428 const size_t size = num_samples * play_channels; 443 const size_t size = num_samples * play_channels;
429 // Returns the largest absolute value in a signed 16-bit vector. 444 // Returns the largest absolute value in a signed 16-bit vector.
430 max_abs = WebRtcSpl_MaxAbsValueW16( 445 max_abs = WebRtcSpl_MaxAbsValueW16(
431 reinterpret_cast<const int16_t*>(play_buffer_.data()), size); 446 reinterpret_cast<const int16_t*>(play_buffer_.data()), size);
432 play_stat_count_ = 0; 447 play_stat_count_ = 0;
433 } 448 }
434 // Update some stats but do it on the task queue to ensure that the members 449 // Update some stats but do it on the task queue to ensure that the members
435 // are modified and read on the same thread. Note that |max_abs| will be 450 // are modified and read on the same thread. Note that |max_abs| will be
436 // zero in most calls and then have no effect of the stats. It is only updated 451 // zero in most calls and then have no effect of the stats. It is only updated
437 // approximately two times per second and can then change the stats. 452 // approximately two times per second and can then change the stats.
438 task_queue_.PostTask([this, max_abs, num_samples_out] { 453 task_queue_.PostTask([this, max_abs, num_samples_out] {
439 UpdatePlayStats(max_abs, num_samples_out); 454 UpdatePlayStats(max_abs, num_samples_out);
440 }); 455 });
441 return static_cast<int32_t>(num_samples_out); 456 return static_cast<int32_t>(num_samples_out);
442 } 457 }
443 458
444 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) { 459 int32_t AudioDeviceBuffer::GetPlayoutData(void* audio_buffer) {
460 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
445 RTC_DCHECK_GT(play_buffer_.size(), 0u); 461 RTC_DCHECK_GT(play_buffer_.size(), 0u);
446 const size_t play_bytes_per_sample = [&] { 462 const size_t play_channels = [&] {
447 rtc::CritScope lock(&lock_); 463 rtc::CritScope lock(&lock_);
448 return play_bytes_per_sample_; 464 return play_channels_;
449 }(); 465 }();
466 const size_t play_bytes_per_sample = play_channels * sizeof(int16_t);
450 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size()); 467 memcpy(audio_buffer, play_buffer_.data(), play_buffer_.size());
451 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample); 468 return static_cast<int32_t>(play_buffer_.size() / play_bytes_per_sample);
452 } 469 }
453 470
454 void AudioDeviceBuffer::StartPeriodicLogging() { 471 void AudioDeviceBuffer::StartPeriodicLogging() {
455 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this, 472 task_queue_.PostTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this,
456 AudioDeviceBuffer::LOG_START)); 473 AudioDeviceBuffer::LOG_START));
457 } 474 }
458 475
459 void AudioDeviceBuffer::StopPeriodicLogging() { 476 void AudioDeviceBuffer::StopPeriodicLogging() {
(...skipping 13 matching lines...) Expand all
473 // Stop logging and posting new tasks. 490 // Stop logging and posting new tasks.
474 return; 491 return;
475 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) { 492 } else if (state == AudioDeviceBuffer::LOG_ACTIVE) {
476 // Default state. Just keep on logging. 493 // Default state. Just keep on logging.
477 } 494 }
478 495
479 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; 496 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds;
480 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_); 497 int64_t time_since_last = rtc::TimeDiff(now_time, last_timer_task_time_);
481 last_timer_task_time_ = now_time; 498 last_timer_task_time_ = now_time;
482 499
500 // Read |play_sample_rate_| and |rec_sample_rate_| under exclusive lock.
kwiberg-webrtc 2016/11/01 15:53:50 The comment isn't necessary.
henrika_webrtc 2016/11/02 10:29:18 Done.
501 const size_t play_sample_rate = [&] {
502 rtc::CritScope lock(&lock_);
503 return play_sample_rate_;
504 }();
505 const size_t rec_sample_rate = [&] {
506 rtc::CritScope lock(&lock_);
507 return rec_sample_rate_;
508 }();
509
483 // Log the latest statistics but skip the first round just after state was 510 // Log the latest statistics but skip the first round just after state was
484 // set to LOG_START. Hence, first printed log will be after ~10 seconds. 511 // set to LOG_START. Hence, first printed log will be after ~10 seconds.
485 if (++num_stat_reports_ > 1 && time_since_last > 0) { 512 if (++num_stat_reports_ > 1 && time_since_last > 0) {
486 uint32_t diff_samples = rec_samples_ - last_rec_samples_; 513 uint32_t diff_samples = rec_samples_ - last_rec_samples_;
487 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); 514 float rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
488 LOG(INFO) << "[REC : " << time_since_last << "msec, " 515 LOG(INFO) << "[REC : " << time_since_last << "msec, "
489 << rec_sample_rate_ / 1000 516 << rec_sample_rate / 1000
490 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ 517 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_
491 << ", " 518 << ", "
492 << "samples: " << diff_samples << ", " 519 << "samples: " << diff_samples << ", "
493 << "rate: " << static_cast<int>(rate + 0.5) << ", " 520 << "rate: " << static_cast<int>(rate + 0.5) << ", "
494 << "level: " << max_rec_level_; 521 << "level: " << max_rec_level_;
495 522
496 diff_samples = play_samples_ - last_play_samples_; 523 diff_samples = play_samples_ - last_play_samples_;
497 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0); 524 rate = diff_samples / (static_cast<float>(time_since_last) / 1000.0);
498 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " 525 LOG(INFO) << "[PLAY: " << time_since_last << "msec, "
499 << play_sample_rate_ / 1000 526 << play_sample_rate / 1000
500 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ 527 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_
501 << ", " 528 << ", "
502 << "samples: " << diff_samples << ", " 529 << "samples: " << diff_samples << ", "
503 << "rate: " << static_cast<int>(rate + 0.5) << ", " 530 << "rate: " << static_cast<int>(rate + 0.5) << ", "
504 << "level: " << max_play_level_; 531 << "level: " << max_play_level_;
505 } 532 }
506 533
507 last_rec_callbacks_ = rec_callbacks_; 534 last_rec_callbacks_ = rec_callbacks_;
508 last_play_callbacks_ = play_callbacks_; 535 last_play_callbacks_ = play_callbacks_;
509 last_rec_samples_ = rec_samples_; 536 last_rec_samples_ = rec_samples_;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) { 577 void AudioDeviceBuffer::UpdatePlayStats(int16_t max_abs, size_t num_samples) {
551 RTC_DCHECK(task_queue_.IsCurrent()); 578 RTC_DCHECK(task_queue_.IsCurrent());
552 ++play_callbacks_; 579 ++play_callbacks_;
553 play_samples_ += num_samples; 580 play_samples_ += num_samples;
554 if (max_abs > max_play_level_) { 581 if (max_abs > max_play_level_) {
555 max_play_level_ = max_abs; 582 max_play_level_ = max_abs;
556 } 583 }
557 } 584 }
558 585
559 } // namespace webrtc 586 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698