| 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 "webrtc/modules/audio_device/audio_device_buffer.h" | 11 #include "webrtc/modules/audio_device/audio_device_buffer.h" |
| 12 | 12 |
| 13 #include "webrtc/base/bind.h" |
| 13 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/base/format_macros.h" | 16 #include "webrtc/base/format_macros.h" |
| 17 #include "webrtc/base/timeutils.h" |
| 16 #include "webrtc/modules/audio_device/audio_device_config.h" | 18 #include "webrtc/modules/audio_device/audio_device_config.h" |
| 17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
| 18 | 19 |
| 19 namespace webrtc { | 20 namespace webrtc { |
| 20 | 21 |
| 21 static const int kHighDelayThresholdMs = 300; | 22 static const int kHighDelayThresholdMs = 300; |
| 22 static const int kLogHighDelayIntervalFrames = 500; // 5 seconds. | 23 static const int kLogHighDelayIntervalFrames = 500; // 5 seconds. |
| 23 | 24 |
| 25 static const char kTimerQueueName[] = "AudioDeviceBufferTimer"; |
| 26 |
| 27 // Time between two sucessive calls to LogStats(). |
| 28 static const size_t kTimerIntervalInSeconds = 10; |
| 29 static const size_t kTimerIntervalInMilliseconds = |
| 30 kTimerIntervalInSeconds * rtc::kNumMillisecsPerSec; |
| 31 |
| 24 AudioDeviceBuffer::AudioDeviceBuffer() | 32 AudioDeviceBuffer::AudioDeviceBuffer() |
| 25 : _critSect(*CriticalSectionWrapper::CreateCriticalSection()), | 33 : _ptrCbAudioTransport(nullptr), |
| 26 _critSectCb(*CriticalSectionWrapper::CreateCriticalSection()), | 34 task_queue_(kTimerQueueName), |
| 27 _ptrCbAudioTransport(nullptr), | 35 timer_has_started_(false), |
| 28 _recSampleRate(0), | 36 _recSampleRate(0), |
| 29 _playSampleRate(0), | 37 _playSampleRate(0), |
| 30 _recChannels(0), | 38 _recChannels(0), |
| 31 _playChannels(0), | 39 _playChannels(0), |
| 32 _recChannel(AudioDeviceModule::kChannelBoth), | 40 _recChannel(AudioDeviceModule::kChannelBoth), |
| 33 _recBytesPerSample(0), | 41 _recBytesPerSample(0), |
| 34 _playBytesPerSample(0), | 42 _playBytesPerSample(0), |
| 35 _recSamples(0), | 43 _recSamples(0), |
| 36 _recSize(0), | 44 _recSize(0), |
| 37 _playSamples(0), | 45 _playSamples(0), |
| 38 _playSize(0), | 46 _playSize(0), |
| 39 _recFile(*FileWrapper::Create()), | 47 _recFile(*FileWrapper::Create()), |
| 40 _playFile(*FileWrapper::Create()), | 48 _playFile(*FileWrapper::Create()), |
| 41 _currentMicLevel(0), | 49 _currentMicLevel(0), |
| 42 _newMicLevel(0), | 50 _newMicLevel(0), |
| 43 _typingStatus(false), | 51 _typingStatus(false), |
| 44 _playDelayMS(0), | 52 _playDelayMS(0), |
| 45 _recDelayMS(0), | 53 _recDelayMS(0), |
| 46 _clockDrift(0), | 54 _clockDrift(0), |
| 47 // Set to the interval in order to log on the first occurrence. | 55 // Set to the interval in order to log on the first occurrence. |
| 48 high_delay_counter_(kLogHighDelayIntervalFrames) { | 56 high_delay_counter_(kLogHighDelayIntervalFrames), |
| 57 num_stat_reports_(0), |
| 58 rec_callbacks_(0), |
| 59 last_rec_callbacks_(0), |
| 60 play_callbacks_(0), |
| 61 last_play_callbacks_(0), |
| 62 rec_samples_(0), |
| 63 last_rec_samples_(0), |
| 64 play_samples_(0), |
| 65 last_play_samples_(0), |
| 66 last_log_stat_time_(0) { |
| 49 LOG(INFO) << "AudioDeviceBuffer::ctor"; | 67 LOG(INFO) << "AudioDeviceBuffer::ctor"; |
| 50 memset(_recBuffer, 0, kMaxBufferSizeBytes); | 68 memset(_recBuffer, 0, kMaxBufferSizeBytes); |
| 51 memset(_playBuffer, 0, kMaxBufferSizeBytes); | 69 memset(_playBuffer, 0, kMaxBufferSizeBytes); |
| 52 } | 70 } |
| 53 | 71 |
| 54 AudioDeviceBuffer::~AudioDeviceBuffer() { | 72 AudioDeviceBuffer::~AudioDeviceBuffer() { |
| 73 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 LOG(INFO) << "AudioDeviceBuffer::~dtor"; | 74 LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
| 56 { | 75 _recFile.Flush(); |
| 57 CriticalSectionScoped lock(&_critSect); | 76 _recFile.CloseFile(); |
| 77 delete &_recFile; |
| 58 | 78 |
| 59 _recFile.Flush(); | 79 _playFile.Flush(); |
| 60 _recFile.CloseFile(); | 80 _playFile.CloseFile(); |
| 61 delete &_recFile; | 81 delete &_playFile; |
| 62 | |
| 63 _playFile.Flush(); | |
| 64 _playFile.CloseFile(); | |
| 65 delete &_playFile; | |
| 66 } | |
| 67 | |
| 68 delete &_critSect; | |
| 69 delete &_critSectCb; | |
| 70 } | 82 } |
| 71 | 83 |
| 72 int32_t AudioDeviceBuffer::RegisterAudioCallback( | 84 int32_t AudioDeviceBuffer::RegisterAudioCallback( |
| 73 AudioTransport* audioCallback) { | 85 AudioTransport* audioCallback) { |
| 74 LOG(INFO) << __FUNCTION__; | 86 LOG(INFO) << __FUNCTION__; |
| 75 CriticalSectionScoped lock(&_critSectCb); | 87 rtc::CritScope lock(&_critSectCb); |
| 76 _ptrCbAudioTransport = audioCallback; | 88 _ptrCbAudioTransport = audioCallback; |
| 77 return 0; | 89 return 0; |
| 78 } | 90 } |
| 79 | 91 |
| 80 int32_t AudioDeviceBuffer::InitPlayout() { | 92 int32_t AudioDeviceBuffer::InitPlayout() { |
| 93 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 LOG(INFO) << __FUNCTION__; | 94 LOG(INFO) << __FUNCTION__; |
| 95 if (!timer_has_started_) { |
| 96 StartTimer(); |
| 97 timer_has_started_ = true; |
| 98 } |
| 82 return 0; | 99 return 0; |
| 83 } | 100 } |
| 84 | 101 |
| 85 int32_t AudioDeviceBuffer::InitRecording() { | 102 int32_t AudioDeviceBuffer::InitRecording() { |
| 103 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 LOG(INFO) << __FUNCTION__; | 104 LOG(INFO) << __FUNCTION__; |
| 105 if (!timer_has_started_) { |
| 106 StartTimer(); |
| 107 timer_has_started_ = true; |
| 108 } |
| 87 return 0; | 109 return 0; |
| 88 } | 110 } |
| 89 | 111 |
| 90 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { | 112 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
| 91 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; | 113 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
| 92 CriticalSectionScoped lock(&_critSect); | 114 rtc::CritScope lock(&_critSect); |
| 93 _recSampleRate = fsHz; | 115 _recSampleRate = fsHz; |
| 94 return 0; | 116 return 0; |
| 95 } | 117 } |
| 96 | 118 |
| 97 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { | 119 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
| 98 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; | 120 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
| 99 CriticalSectionScoped lock(&_critSect); | 121 rtc::CritScope lock(&_critSect); |
| 100 _playSampleRate = fsHz; | 122 _playSampleRate = fsHz; |
| 101 return 0; | 123 return 0; |
| 102 } | 124 } |
| 103 | 125 |
| 104 int32_t AudioDeviceBuffer::RecordingSampleRate() const { | 126 int32_t AudioDeviceBuffer::RecordingSampleRate() const { |
| 105 return _recSampleRate; | 127 return _recSampleRate; |
| 106 } | 128 } |
| 107 | 129 |
| 108 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { | 130 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
| 109 return _playSampleRate; | 131 return _playSampleRate; |
| 110 } | 132 } |
| 111 | 133 |
| 112 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { | 134 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
| 113 CriticalSectionScoped lock(&_critSect); | 135 rtc::CritScope lock(&_critSect); |
| 114 _recChannels = channels; | 136 _recChannels = channels; |
| 115 _recBytesPerSample = | 137 _recBytesPerSample = |
| 116 2 * channels; // 16 bits per sample in mono, 32 bits in stereo | 138 2 * channels; // 16 bits per sample in mono, 32 bits in stereo |
| 117 return 0; | 139 return 0; |
| 118 } | 140 } |
| 119 | 141 |
| 120 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { | 142 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { |
| 121 CriticalSectionScoped lock(&_critSect); | 143 rtc::CritScope lock(&_critSect); |
| 122 _playChannels = channels; | 144 _playChannels = channels; |
| 123 // 16 bits per sample in mono, 32 bits in stereo | 145 // 16 bits per sample in mono, 32 bits in stereo |
| 124 _playBytesPerSample = 2 * channels; | 146 _playBytesPerSample = 2 * channels; |
| 125 return 0; | 147 return 0; |
| 126 } | 148 } |
| 127 | 149 |
| 128 int32_t AudioDeviceBuffer::SetRecordingChannel( | 150 int32_t AudioDeviceBuffer::SetRecordingChannel( |
| 129 const AudioDeviceModule::ChannelType channel) { | 151 const AudioDeviceModule::ChannelType channel) { |
| 130 CriticalSectionScoped lock(&_critSect); | 152 rtc::CritScope lock(&_critSect); |
| 131 | 153 |
| 132 if (_recChannels == 1) { | 154 if (_recChannels == 1) { |
| 133 return -1; | 155 return -1; |
| 134 } | 156 } |
| 135 | 157 |
| 136 if (channel == AudioDeviceModule::kChannelBoth) { | 158 if (channel == AudioDeviceModule::kChannelBoth) { |
| 137 // two bytes per channel | 159 // two bytes per channel |
| 138 _recBytesPerSample = 4; | 160 _recBytesPerSample = 4; |
| 139 } else { | 161 } else { |
| 140 // only utilize one out of two possible channels (left or right) | 162 // only utilize one out of two possible channels (left or right) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 } | 208 } |
| 187 } | 209 } |
| 188 | 210 |
| 189 _playDelayMS = playDelayMs; | 211 _playDelayMS = playDelayMs; |
| 190 _recDelayMS = recDelayMs; | 212 _recDelayMS = recDelayMs; |
| 191 _clockDrift = clockDrift; | 213 _clockDrift = clockDrift; |
| 192 } | 214 } |
| 193 | 215 |
| 194 int32_t AudioDeviceBuffer::StartInputFileRecording( | 216 int32_t AudioDeviceBuffer::StartInputFileRecording( |
| 195 const char fileName[kAdmMaxFileNameSize]) { | 217 const char fileName[kAdmMaxFileNameSize]) { |
| 196 CriticalSectionScoped lock(&_critSect); | 218 rtc::CritScope lock(&_critSect); |
| 197 | 219 |
| 198 _recFile.Flush(); | 220 _recFile.Flush(); |
| 199 _recFile.CloseFile(); | 221 _recFile.CloseFile(); |
| 200 | 222 |
| 201 return _recFile.OpenFile(fileName, false) ? 0 : -1; | 223 return _recFile.OpenFile(fileName, false) ? 0 : -1; |
| 202 } | 224 } |
| 203 | 225 |
| 204 int32_t AudioDeviceBuffer::StopInputFileRecording() { | 226 int32_t AudioDeviceBuffer::StopInputFileRecording() { |
| 205 CriticalSectionScoped lock(&_critSect); | 227 rtc::CritScope lock(&_critSect); |
| 206 | 228 |
| 207 _recFile.Flush(); | 229 _recFile.Flush(); |
| 208 _recFile.CloseFile(); | 230 _recFile.CloseFile(); |
| 209 | 231 |
| 210 return 0; | 232 return 0; |
| 211 } | 233 } |
| 212 | 234 |
| 213 int32_t AudioDeviceBuffer::StartOutputFileRecording( | 235 int32_t AudioDeviceBuffer::StartOutputFileRecording( |
| 214 const char fileName[kAdmMaxFileNameSize]) { | 236 const char fileName[kAdmMaxFileNameSize]) { |
| 215 CriticalSectionScoped lock(&_critSect); | 237 rtc::CritScope lock(&_critSect); |
| 216 | 238 |
| 217 _playFile.Flush(); | 239 _playFile.Flush(); |
| 218 _playFile.CloseFile(); | 240 _playFile.CloseFile(); |
| 219 | 241 |
| 220 return _playFile.OpenFile(fileName, false) ? 0 : -1; | 242 return _playFile.OpenFile(fileName, false) ? 0 : -1; |
| 221 } | 243 } |
| 222 | 244 |
| 223 int32_t AudioDeviceBuffer::StopOutputFileRecording() { | 245 int32_t AudioDeviceBuffer::StopOutputFileRecording() { |
| 224 CriticalSectionScoped lock(&_critSect); | 246 rtc::CritScope lock(&_critSect); |
| 225 | 247 |
| 226 _playFile.Flush(); | 248 _playFile.Flush(); |
| 227 _playFile.CloseFile(); | 249 _playFile.CloseFile(); |
| 228 | 250 |
| 229 return 0; | 251 return 0; |
| 230 } | 252 } |
| 231 | 253 |
| 232 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audioBuffer, | 254 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audioBuffer, |
| 233 size_t nSamples) { | 255 size_t nSamples) { |
| 234 CriticalSectionScoped lock(&_critSect); | 256 rtc::CritScope lock(&_critSect); |
| 235 | 257 |
| 236 if (_recBytesPerSample == 0) { | 258 if (_recBytesPerSample == 0) { |
| 237 assert(false); | 259 assert(false); |
| 238 return -1; | 260 return -1; |
| 239 } | 261 } |
| 240 | 262 |
| 241 _recSamples = nSamples; | 263 _recSamples = nSamples; |
| 242 _recSize = _recBytesPerSample * nSamples; // {2,4}*nSamples | 264 _recSize = _recBytesPerSample * nSamples; // {2,4}*nSamples |
| 243 if (_recSize > kMaxBufferSizeBytes) { | 265 if (_recSize > kMaxBufferSizeBytes) { |
| 244 assert(false); | 266 assert(false); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 263 ptr16In++; | 285 ptr16In++; |
| 264 ptr16In++; | 286 ptr16In++; |
| 265 } | 287 } |
| 266 } | 288 } |
| 267 | 289 |
| 268 if (_recFile.is_open()) { | 290 if (_recFile.is_open()) { |
| 269 // write to binary file in mono or stereo (interleaved) | 291 // write to binary file in mono or stereo (interleaved) |
| 270 _recFile.Write(&_recBuffer[0], _recSize); | 292 _recFile.Write(&_recBuffer[0], _recSize); |
| 271 } | 293 } |
| 272 | 294 |
| 295 // Update some stats but do it on the task queue to ensure that the members |
| 296 // are modified and read on the same thread. |
| 297 task_queue_.PostTask( |
| 298 rtc::Bind(&AudioDeviceBuffer::UpdateRecStats, this, nSamples)); |
| 299 |
| 273 return 0; | 300 return 0; |
| 274 } | 301 } |
| 275 | 302 |
| 276 int32_t AudioDeviceBuffer::DeliverRecordedData() { | 303 int32_t AudioDeviceBuffer::DeliverRecordedData() { |
| 277 CriticalSectionScoped lock(&_critSectCb); | 304 rtc::CritScope lock(&_critSectCb); |
| 278 // Ensure that user has initialized all essential members | 305 // Ensure that user has initialized all essential members |
| 279 if ((_recSampleRate == 0) || (_recSamples == 0) || | 306 if ((_recSampleRate == 0) || (_recSamples == 0) || |
| 280 (_recBytesPerSample == 0) || (_recChannels == 0)) { | 307 (_recBytesPerSample == 0) || (_recChannels == 0)) { |
| 281 RTC_NOTREACHED(); | 308 RTC_NOTREACHED(); |
| 282 return -1; | 309 return -1; |
| 283 } | 310 } |
| 284 | 311 |
| 285 if (!_ptrCbAudioTransport) { | 312 if (!_ptrCbAudioTransport) { |
| 286 LOG(LS_WARNING) << "Invalid audio transport"; | 313 LOG(LS_WARNING) << "Invalid audio transport"; |
| 287 return 0; | 314 return 0; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 302 } | 329 } |
| 303 | 330 |
| 304 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t nSamples) { | 331 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t nSamples) { |
| 305 uint32_t playSampleRate = 0; | 332 uint32_t playSampleRate = 0; |
| 306 size_t playBytesPerSample = 0; | 333 size_t playBytesPerSample = 0; |
| 307 size_t playChannels = 0; | 334 size_t playChannels = 0; |
| 308 | 335 |
| 309 // TOOD(henrika): improve bad locking model and make it more clear that only | 336 // TOOD(henrika): improve bad locking model and make it more clear that only |
| 310 // 10ms buffer sizes is supported in WebRTC. | 337 // 10ms buffer sizes is supported in WebRTC. |
| 311 { | 338 { |
| 312 CriticalSectionScoped lock(&_critSect); | 339 rtc::CritScope lock(&_critSect); |
| 313 | 340 |
| 314 // Store copies under lock and use copies hereafter to avoid race with | 341 // Store copies under lock and use copies hereafter to avoid race with |
| 315 // setter methods. | 342 // setter methods. |
| 316 playSampleRate = _playSampleRate; | 343 playSampleRate = _playSampleRate; |
| 317 playBytesPerSample = _playBytesPerSample; | 344 playBytesPerSample = _playBytesPerSample; |
| 318 playChannels = _playChannels; | 345 playChannels = _playChannels; |
| 319 | 346 |
| 320 // Ensure that user has initialized all essential members | 347 // Ensure that user has initialized all essential members |
| 321 if ((playBytesPerSample == 0) || (playChannels == 0) || | 348 if ((playBytesPerSample == 0) || (playChannels == 0) || |
| 322 (playSampleRate == 0)) { | 349 (playSampleRate == 0)) { |
| 323 RTC_NOTREACHED(); | 350 RTC_NOTREACHED(); |
| 324 return -1; | 351 return -1; |
| 325 } | 352 } |
| 326 | 353 |
| 327 _playSamples = nSamples; | 354 _playSamples = nSamples; |
| 328 _playSize = playBytesPerSample * nSamples; // {2,4}*nSamples | 355 _playSize = playBytesPerSample * nSamples; // {2,4}*nSamples |
| 329 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); | 356 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); |
| 330 RTC_CHECK_EQ(nSamples, _playSamples); | 357 RTC_CHECK_EQ(nSamples, _playSamples); |
| 331 } | 358 } |
| 332 | 359 |
| 333 size_t nSamplesOut(0); | 360 size_t nSamplesOut(0); |
| 334 | 361 |
| 335 CriticalSectionScoped lock(&_critSectCb); | 362 rtc::CritScope lock(&_critSectCb); |
| 336 | 363 |
| 337 // It is currently supported to start playout without a valid audio | 364 // It is currently supported to start playout without a valid audio |
| 338 // transport object. Leads to warning and silence. | 365 // transport object. Leads to warning and silence. |
| 339 if (!_ptrCbAudioTransport) { | 366 if (!_ptrCbAudioTransport) { |
| 340 LOG(LS_WARNING) << "Invalid audio transport"; | 367 LOG(LS_WARNING) << "Invalid audio transport"; |
| 341 return 0; | 368 return 0; |
| 342 } | 369 } |
| 343 | 370 |
| 344 uint32_t res(0); | 371 uint32_t res(0); |
| 345 int64_t elapsed_time_ms = -1; | 372 int64_t elapsed_time_ms = -1; |
| 346 int64_t ntp_time_ms = -1; | 373 int64_t ntp_time_ms = -1; |
| 347 res = _ptrCbAudioTransport->NeedMorePlayData( | 374 res = _ptrCbAudioTransport->NeedMorePlayData( |
| 348 _playSamples, playBytesPerSample, playChannels, playSampleRate, | 375 _playSamples, playBytesPerSample, playChannels, playSampleRate, |
| 349 &_playBuffer[0], nSamplesOut, &elapsed_time_ms, &ntp_time_ms); | 376 &_playBuffer[0], nSamplesOut, &elapsed_time_ms, &ntp_time_ms); |
| 350 if (res != 0) { | 377 if (res != 0) { |
| 351 LOG(LS_ERROR) << "NeedMorePlayData() failed"; | 378 LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
| 352 } | 379 } |
| 353 | 380 |
| 381 // Update some stats but do it on the task queue to ensure that access of |
| 382 // members is serialized hence avoiding usage of locks. |
| 383 task_queue_.PostTask( |
| 384 rtc::Bind(&AudioDeviceBuffer::UpdatePlayStats, this, nSamplesOut)); |
| 385 |
| 354 return static_cast<int32_t>(nSamplesOut); | 386 return static_cast<int32_t>(nSamplesOut); |
| 355 } | 387 } |
| 356 | 388 |
| 357 int32_t AudioDeviceBuffer::GetPlayoutData(void* audioBuffer) { | 389 int32_t AudioDeviceBuffer::GetPlayoutData(void* audioBuffer) { |
| 358 CriticalSectionScoped lock(&_critSect); | 390 rtc::CritScope lock(&_critSect); |
| 359 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); | 391 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); |
| 360 | 392 |
| 361 memcpy(audioBuffer, &_playBuffer[0], _playSize); | 393 memcpy(audioBuffer, &_playBuffer[0], _playSize); |
| 362 | 394 |
| 363 if (_playFile.is_open()) { | 395 if (_playFile.is_open()) { |
| 364 // write to binary file in mono or stereo (interleaved) | 396 // write to binary file in mono or stereo (interleaved) |
| 365 _playFile.Write(&_playBuffer[0], _playSize); | 397 _playFile.Write(&_playBuffer[0], _playSize); |
| 366 } | 398 } |
| 367 | 399 |
| 368 return static_cast<int32_t>(_playSamples); | 400 return static_cast<int32_t>(_playSamples); |
| 369 } | 401 } |
| 370 | 402 |
| 403 void AudioDeviceBuffer::StartTimer() { |
| 404 last_log_stat_time_ = rtc::TimeMillis(); |
| 405 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
| 406 kTimerIntervalInMilliseconds); |
| 407 } |
| 408 |
| 409 void AudioDeviceBuffer::LogStats() { |
| 410 RTC_DCHECK(task_queue_.IsCurrent()); |
| 411 |
| 412 int64_t now_time = rtc::TimeMillis(); |
| 413 int64_t next_callback_time = now_time + kTimerIntervalInMilliseconds; |
| 414 int64_t time_since_last = rtc::TimeDiff(now_time, last_log_stat_time_); |
| 415 last_log_stat_time_ = now_time; |
| 416 |
| 417 // Log the latest statistics but skip the first 10 seconds since we are not |
| 418 // sure of the exact starting point. I.e., the first log printout will be |
| 419 // after ~20 seconds. |
| 420 if (++num_stat_reports_ > 1) { |
| 421 uint32_t diff_samples = rec_samples_ - last_rec_samples_; |
| 422 uint32_t rate = diff_samples / kTimerIntervalInSeconds; |
| 423 LOG(INFO) << "[REC : " << time_since_last << "msec, " |
| 424 << _recSampleRate / 1000 |
| 425 << "kHz] callbacks: " << rec_callbacks_ - last_rec_callbacks_ |
| 426 << ", " |
| 427 << "samples: " << diff_samples << ", " |
| 428 << "rate: " << rate; |
| 429 |
| 430 diff_samples = play_samples_ - last_play_samples_; |
| 431 rate = diff_samples / kTimerIntervalInSeconds; |
| 432 LOG(INFO) << "[PLAY: " << time_since_last << "msec, " |
| 433 << _playSampleRate / 1000 |
| 434 << "kHz] callbacks: " << play_callbacks_ - last_play_callbacks_ |
| 435 << ", " |
| 436 << "samples: " << diff_samples << ", " |
| 437 << "rate: " << rate; |
| 438 } |
| 439 |
| 440 last_rec_callbacks_ = rec_callbacks_; |
| 441 last_play_callbacks_ = play_callbacks_; |
| 442 last_rec_samples_ = rec_samples_; |
| 443 last_play_samples_ = play_samples_; |
| 444 |
| 445 int64_t time_to_wait_ms = next_callback_time - rtc::TimeMillis(); |
| 446 RTC_DCHECK_GT(time_to_wait_ms, 0) << "Invalid timer interval"; |
| 447 |
| 448 // Update some stats but do it on the task queue to ensure that access of |
| 449 // members is serialized hence avoiding usage of locks. |
| 450 task_queue_.PostDelayedTask(rtc::Bind(&AudioDeviceBuffer::LogStats, this), |
| 451 time_to_wait_ms); |
| 452 } |
| 453 |
| 454 void AudioDeviceBuffer::UpdateRecStats(size_t num_samples) { |
| 455 RTC_DCHECK(task_queue_.IsCurrent()); |
| 456 ++rec_callbacks_; |
| 457 rec_samples_ += num_samples; |
| 458 } |
| 459 |
| 460 void AudioDeviceBuffer::UpdatePlayStats(size_t num_samples) { |
| 461 RTC_DCHECK(task_queue_.IsCurrent()); |
| 462 ++play_callbacks_; |
| 463 play_samples_ += num_samples; |
| 464 } |
| 465 |
| 371 } // namespace webrtc | 466 } // namespace webrtc |
| OLD | NEW |