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