| 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" | |
| 14 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
| 16 #include "webrtc/base/format_macros.h" | 15 #include "webrtc/base/format_macros.h" |
| 17 #include "webrtc/base/timeutils.h" | |
| 18 #include "webrtc/modules/audio_device/audio_device_config.h" | 16 #include "webrtc/modules/audio_device/audio_device_config.h" |
| 17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 19 | 18 |
| 20 namespace webrtc { | 19 namespace webrtc { |
| 21 | 20 |
| 22 static const int kHighDelayThresholdMs = 300; | 21 static const int kHighDelayThresholdMs = 300; |
| 23 static const int kLogHighDelayIntervalFrames = 500; // 5 seconds. | 22 static const int kLogHighDelayIntervalFrames = 500; // 5 seconds. |
| 24 | 23 |
| 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 | |
| 32 AudioDeviceBuffer::AudioDeviceBuffer() | 24 AudioDeviceBuffer::AudioDeviceBuffer() |
| 33 : _ptrCbAudioTransport(nullptr), | 25 : _critSect(*CriticalSectionWrapper::CreateCriticalSection()), |
| 34 task_queue_(kTimerQueueName), | 26 _critSectCb(*CriticalSectionWrapper::CreateCriticalSection()), |
| 35 timer_has_started_(false), | 27 _ptrCbAudioTransport(nullptr), |
| 36 _recSampleRate(0), | 28 _recSampleRate(0), |
| 37 _playSampleRate(0), | 29 _playSampleRate(0), |
| 38 _recChannels(0), | 30 _recChannels(0), |
| 39 _playChannels(0), | 31 _playChannels(0), |
| 40 _recChannel(AudioDeviceModule::kChannelBoth), | 32 _recChannel(AudioDeviceModule::kChannelBoth), |
| 41 _recBytesPerSample(0), | 33 _recBytesPerSample(0), |
| 42 _playBytesPerSample(0), | 34 _playBytesPerSample(0), |
| 43 _recSamples(0), | 35 _recSamples(0), |
| 44 _recSize(0), | 36 _recSize(0), |
| 45 _playSamples(0), | 37 _playSamples(0), |
| 46 _playSize(0), | 38 _playSize(0), |
| 47 _recFile(*FileWrapper::Create()), | 39 _recFile(*FileWrapper::Create()), |
| 48 _playFile(*FileWrapper::Create()), | 40 _playFile(*FileWrapper::Create()), |
| 49 _currentMicLevel(0), | 41 _currentMicLevel(0), |
| 50 _newMicLevel(0), | 42 _newMicLevel(0), |
| 51 _typingStatus(false), | 43 _typingStatus(false), |
| 52 _playDelayMS(0), | 44 _playDelayMS(0), |
| 53 _recDelayMS(0), | 45 _recDelayMS(0), |
| 54 _clockDrift(0), | 46 _clockDrift(0), |
| 55 // Set to the interval in order to log on the first occurrence. | 47 // Set to the interval in order to log on the first occurrence. |
| 56 high_delay_counter_(kLogHighDelayIntervalFrames), | 48 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) { | |
| 67 LOG(INFO) << "AudioDeviceBuffer::ctor"; | 49 LOG(INFO) << "AudioDeviceBuffer::ctor"; |
| 68 memset(_recBuffer, 0, kMaxBufferSizeBytes); | 50 memset(_recBuffer, 0, kMaxBufferSizeBytes); |
| 69 memset(_playBuffer, 0, kMaxBufferSizeBytes); | 51 memset(_playBuffer, 0, kMaxBufferSizeBytes); |
| 70 } | 52 } |
| 71 | 53 |
| 72 AudioDeviceBuffer::~AudioDeviceBuffer() { | 54 AudioDeviceBuffer::~AudioDeviceBuffer() { |
| 73 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 74 LOG(INFO) << "AudioDeviceBuffer::~dtor"; | 55 LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
| 75 _recFile.Flush(); | 56 { |
| 76 _recFile.CloseFile(); | 57 CriticalSectionScoped lock(&_critSect); |
| 77 delete &_recFile; | |
| 78 | 58 |
| 79 _playFile.Flush(); | 59 _recFile.Flush(); |
| 80 _playFile.CloseFile(); | 60 _recFile.CloseFile(); |
| 81 delete &_playFile; | 61 delete &_recFile; |
| 62 |
| 63 _playFile.Flush(); |
| 64 _playFile.CloseFile(); |
| 65 delete &_playFile; |
| 66 } |
| 67 |
| 68 delete &_critSect; |
| 69 delete &_critSectCb; |
| 82 } | 70 } |
| 83 | 71 |
| 84 int32_t AudioDeviceBuffer::RegisterAudioCallback( | 72 int32_t AudioDeviceBuffer::RegisterAudioCallback( |
| 85 AudioTransport* audioCallback) { | 73 AudioTransport* audioCallback) { |
| 86 LOG(INFO) << __FUNCTION__; | 74 LOG(INFO) << __FUNCTION__; |
| 87 rtc::CritScope lock(&_critSectCb); | 75 CriticalSectionScoped lock(&_critSectCb); |
| 88 _ptrCbAudioTransport = audioCallback; | 76 _ptrCbAudioTransport = audioCallback; |
| 89 return 0; | 77 return 0; |
| 90 } | 78 } |
| 91 | 79 |
| 92 int32_t AudioDeviceBuffer::InitPlayout() { | 80 int32_t AudioDeviceBuffer::InitPlayout() { |
| 93 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 94 LOG(INFO) << __FUNCTION__; | 81 LOG(INFO) << __FUNCTION__; |
| 95 if (!timer_has_started_) { | |
| 96 StartTimer(); | |
| 97 timer_has_started_ = true; | |
| 98 } | |
| 99 return 0; | 82 return 0; |
| 100 } | 83 } |
| 101 | 84 |
| 102 int32_t AudioDeviceBuffer::InitRecording() { | 85 int32_t AudioDeviceBuffer::InitRecording() { |
| 103 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 104 LOG(INFO) << __FUNCTION__; | 86 LOG(INFO) << __FUNCTION__; |
| 105 if (!timer_has_started_) { | |
| 106 StartTimer(); | |
| 107 timer_has_started_ = true; | |
| 108 } | |
| 109 return 0; | 87 return 0; |
| 110 } | 88 } |
| 111 | 89 |
| 112 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { | 90 int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
| 113 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; | 91 LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
| 114 rtc::CritScope lock(&_critSect); | 92 CriticalSectionScoped lock(&_critSect); |
| 115 _recSampleRate = fsHz; | 93 _recSampleRate = fsHz; |
| 116 return 0; | 94 return 0; |
| 117 } | 95 } |
| 118 | 96 |
| 119 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { | 97 int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
| 120 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; | 98 LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
| 121 rtc::CritScope lock(&_critSect); | 99 CriticalSectionScoped lock(&_critSect); |
| 122 _playSampleRate = fsHz; | 100 _playSampleRate = fsHz; |
| 123 return 0; | 101 return 0; |
| 124 } | 102 } |
| 125 | 103 |
| 126 int32_t AudioDeviceBuffer::RecordingSampleRate() const { | 104 int32_t AudioDeviceBuffer::RecordingSampleRate() const { |
| 127 return _recSampleRate; | 105 return _recSampleRate; |
| 128 } | 106 } |
| 129 | 107 |
| 130 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { | 108 int32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
| 131 return _playSampleRate; | 109 return _playSampleRate; |
| 132 } | 110 } |
| 133 | 111 |
| 134 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { | 112 int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
| 135 rtc::CritScope lock(&_critSect); | 113 CriticalSectionScoped lock(&_critSect); |
| 136 _recChannels = channels; | 114 _recChannels = channels; |
| 137 _recBytesPerSample = | 115 _recBytesPerSample = |
| 138 2 * channels; // 16 bits per sample in mono, 32 bits in stereo | 116 2 * channels; // 16 bits per sample in mono, 32 bits in stereo |
| 139 return 0; | 117 return 0; |
| 140 } | 118 } |
| 141 | 119 |
| 142 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { | 120 int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { |
| 143 rtc::CritScope lock(&_critSect); | 121 CriticalSectionScoped lock(&_critSect); |
| 144 _playChannels = channels; | 122 _playChannels = channels; |
| 145 // 16 bits per sample in mono, 32 bits in stereo | 123 // 16 bits per sample in mono, 32 bits in stereo |
| 146 _playBytesPerSample = 2 * channels; | 124 _playBytesPerSample = 2 * channels; |
| 147 return 0; | 125 return 0; |
| 148 } | 126 } |
| 149 | 127 |
| 150 int32_t AudioDeviceBuffer::SetRecordingChannel( | 128 int32_t AudioDeviceBuffer::SetRecordingChannel( |
| 151 const AudioDeviceModule::ChannelType channel) { | 129 const AudioDeviceModule::ChannelType channel) { |
| 152 rtc::CritScope lock(&_critSect); | 130 CriticalSectionScoped lock(&_critSect); |
| 153 | 131 |
| 154 if (_recChannels == 1) { | 132 if (_recChannels == 1) { |
| 155 return -1; | 133 return -1; |
| 156 } | 134 } |
| 157 | 135 |
| 158 if (channel == AudioDeviceModule::kChannelBoth) { | 136 if (channel == AudioDeviceModule::kChannelBoth) { |
| 159 // two bytes per channel | 137 // two bytes per channel |
| 160 _recBytesPerSample = 4; | 138 _recBytesPerSample = 4; |
| 161 } else { | 139 } else { |
| 162 // only utilize one out of two possible channels (left or right) | 140 // only utilize one out of two possible channels (left or right) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 } | 186 } |
| 209 } | 187 } |
| 210 | 188 |
| 211 _playDelayMS = playDelayMs; | 189 _playDelayMS = playDelayMs; |
| 212 _recDelayMS = recDelayMs; | 190 _recDelayMS = recDelayMs; |
| 213 _clockDrift = clockDrift; | 191 _clockDrift = clockDrift; |
| 214 } | 192 } |
| 215 | 193 |
| 216 int32_t AudioDeviceBuffer::StartInputFileRecording( | 194 int32_t AudioDeviceBuffer::StartInputFileRecording( |
| 217 const char fileName[kAdmMaxFileNameSize]) { | 195 const char fileName[kAdmMaxFileNameSize]) { |
| 218 rtc::CritScope lock(&_critSect); | 196 CriticalSectionScoped lock(&_critSect); |
| 219 | 197 |
| 220 _recFile.Flush(); | 198 _recFile.Flush(); |
| 221 _recFile.CloseFile(); | 199 _recFile.CloseFile(); |
| 222 | 200 |
| 223 return _recFile.OpenFile(fileName, false) ? 0 : -1; | 201 return _recFile.OpenFile(fileName, false) ? 0 : -1; |
| 224 } | 202 } |
| 225 | 203 |
| 226 int32_t AudioDeviceBuffer::StopInputFileRecording() { | 204 int32_t AudioDeviceBuffer::StopInputFileRecording() { |
| 227 rtc::CritScope lock(&_critSect); | 205 CriticalSectionScoped lock(&_critSect); |
| 228 | 206 |
| 229 _recFile.Flush(); | 207 _recFile.Flush(); |
| 230 _recFile.CloseFile(); | 208 _recFile.CloseFile(); |
| 231 | 209 |
| 232 return 0; | 210 return 0; |
| 233 } | 211 } |
| 234 | 212 |
| 235 int32_t AudioDeviceBuffer::StartOutputFileRecording( | 213 int32_t AudioDeviceBuffer::StartOutputFileRecording( |
| 236 const char fileName[kAdmMaxFileNameSize]) { | 214 const char fileName[kAdmMaxFileNameSize]) { |
| 237 rtc::CritScope lock(&_critSect); | 215 CriticalSectionScoped lock(&_critSect); |
| 238 | 216 |
| 239 _playFile.Flush(); | 217 _playFile.Flush(); |
| 240 _playFile.CloseFile(); | 218 _playFile.CloseFile(); |
| 241 | 219 |
| 242 return _playFile.OpenFile(fileName, false) ? 0 : -1; | 220 return _playFile.OpenFile(fileName, false) ? 0 : -1; |
| 243 } | 221 } |
| 244 | 222 |
| 245 int32_t AudioDeviceBuffer::StopOutputFileRecording() { | 223 int32_t AudioDeviceBuffer::StopOutputFileRecording() { |
| 246 rtc::CritScope lock(&_critSect); | 224 CriticalSectionScoped lock(&_critSect); |
| 247 | 225 |
| 248 _playFile.Flush(); | 226 _playFile.Flush(); |
| 249 _playFile.CloseFile(); | 227 _playFile.CloseFile(); |
| 250 | 228 |
| 251 return 0; | 229 return 0; |
| 252 } | 230 } |
| 253 | 231 |
| 254 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audioBuffer, | 232 int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audioBuffer, |
| 255 size_t nSamples) { | 233 size_t nSamples) { |
| 256 rtc::CritScope lock(&_critSect); | 234 CriticalSectionScoped lock(&_critSect); |
| 257 | 235 |
| 258 if (_recBytesPerSample == 0) { | 236 if (_recBytesPerSample == 0) { |
| 259 assert(false); | 237 assert(false); |
| 260 return -1; | 238 return -1; |
| 261 } | 239 } |
| 262 | 240 |
| 263 _recSamples = nSamples; | 241 _recSamples = nSamples; |
| 264 _recSize = _recBytesPerSample * nSamples; // {2,4}*nSamples | 242 _recSize = _recBytesPerSample * nSamples; // {2,4}*nSamples |
| 265 if (_recSize > kMaxBufferSizeBytes) { | 243 if (_recSize > kMaxBufferSizeBytes) { |
| 266 assert(false); | 244 assert(false); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 285 ptr16In++; | 263 ptr16In++; |
| 286 ptr16In++; | 264 ptr16In++; |
| 287 } | 265 } |
| 288 } | 266 } |
| 289 | 267 |
| 290 if (_recFile.is_open()) { | 268 if (_recFile.is_open()) { |
| 291 // write to binary file in mono or stereo (interleaved) | 269 // write to binary file in mono or stereo (interleaved) |
| 292 _recFile.Write(&_recBuffer[0], _recSize); | 270 _recFile.Write(&_recBuffer[0], _recSize); |
| 293 } | 271 } |
| 294 | 272 |
| 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 | |
| 300 return 0; | 273 return 0; |
| 301 } | 274 } |
| 302 | 275 |
| 303 int32_t AudioDeviceBuffer::DeliverRecordedData() { | 276 int32_t AudioDeviceBuffer::DeliverRecordedData() { |
| 304 rtc::CritScope lock(&_critSectCb); | 277 CriticalSectionScoped lock(&_critSectCb); |
| 305 // Ensure that user has initialized all essential members | 278 // Ensure that user has initialized all essential members |
| 306 if ((_recSampleRate == 0) || (_recSamples == 0) || | 279 if ((_recSampleRate == 0) || (_recSamples == 0) || |
| 307 (_recBytesPerSample == 0) || (_recChannels == 0)) { | 280 (_recBytesPerSample == 0) || (_recChannels == 0)) { |
| 308 RTC_NOTREACHED(); | 281 RTC_NOTREACHED(); |
| 309 return -1; | 282 return -1; |
| 310 } | 283 } |
| 311 | 284 |
| 312 if (!_ptrCbAudioTransport) { | 285 if (!_ptrCbAudioTransport) { |
| 313 LOG(LS_WARNING) << "Invalid audio transport"; | 286 LOG(LS_WARNING) << "Invalid audio transport"; |
| 314 return 0; | 287 return 0; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 329 } | 302 } |
| 330 | 303 |
| 331 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t nSamples) { | 304 int32_t AudioDeviceBuffer::RequestPlayoutData(size_t nSamples) { |
| 332 uint32_t playSampleRate = 0; | 305 uint32_t playSampleRate = 0; |
| 333 size_t playBytesPerSample = 0; | 306 size_t playBytesPerSample = 0; |
| 334 size_t playChannels = 0; | 307 size_t playChannels = 0; |
| 335 | 308 |
| 336 // TOOD(henrika): improve bad locking model and make it more clear that only | 309 // TOOD(henrika): improve bad locking model and make it more clear that only |
| 337 // 10ms buffer sizes is supported in WebRTC. | 310 // 10ms buffer sizes is supported in WebRTC. |
| 338 { | 311 { |
| 339 rtc::CritScope lock(&_critSect); | 312 CriticalSectionScoped lock(&_critSect); |
| 340 | 313 |
| 341 // Store copies under lock and use copies hereafter to avoid race with | 314 // Store copies under lock and use copies hereafter to avoid race with |
| 342 // setter methods. | 315 // setter methods. |
| 343 playSampleRate = _playSampleRate; | 316 playSampleRate = _playSampleRate; |
| 344 playBytesPerSample = _playBytesPerSample; | 317 playBytesPerSample = _playBytesPerSample; |
| 345 playChannels = _playChannels; | 318 playChannels = _playChannels; |
| 346 | 319 |
| 347 // Ensure that user has initialized all essential members | 320 // Ensure that user has initialized all essential members |
| 348 if ((playBytesPerSample == 0) || (playChannels == 0) || | 321 if ((playBytesPerSample == 0) || (playChannels == 0) || |
| 349 (playSampleRate == 0)) { | 322 (playSampleRate == 0)) { |
| 350 RTC_NOTREACHED(); | 323 RTC_NOTREACHED(); |
| 351 return -1; | 324 return -1; |
| 352 } | 325 } |
| 353 | 326 |
| 354 _playSamples = nSamples; | 327 _playSamples = nSamples; |
| 355 _playSize = playBytesPerSample * nSamples; // {2,4}*nSamples | 328 _playSize = playBytesPerSample * nSamples; // {2,4}*nSamples |
| 356 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); | 329 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); |
| 357 RTC_CHECK_EQ(nSamples, _playSamples); | 330 RTC_CHECK_EQ(nSamples, _playSamples); |
| 358 } | 331 } |
| 359 | 332 |
| 360 size_t nSamplesOut(0); | 333 size_t nSamplesOut(0); |
| 361 | 334 |
| 362 rtc::CritScope lock(&_critSectCb); | 335 CriticalSectionScoped lock(&_critSectCb); |
| 363 | 336 |
| 364 // It is currently supported to start playout without a valid audio | 337 // It is currently supported to start playout without a valid audio |
| 365 // transport object. Leads to warning and silence. | 338 // transport object. Leads to warning and silence. |
| 366 if (!_ptrCbAudioTransport) { | 339 if (!_ptrCbAudioTransport) { |
| 367 LOG(LS_WARNING) << "Invalid audio transport"; | 340 LOG(LS_WARNING) << "Invalid audio transport"; |
| 368 return 0; | 341 return 0; |
| 369 } | 342 } |
| 370 | 343 |
| 371 uint32_t res(0); | 344 uint32_t res(0); |
| 372 int64_t elapsed_time_ms = -1; | 345 int64_t elapsed_time_ms = -1; |
| 373 int64_t ntp_time_ms = -1; | 346 int64_t ntp_time_ms = -1; |
| 374 res = _ptrCbAudioTransport->NeedMorePlayData( | 347 res = _ptrCbAudioTransport->NeedMorePlayData( |
| 375 _playSamples, playBytesPerSample, playChannels, playSampleRate, | 348 _playSamples, playBytesPerSample, playChannels, playSampleRate, |
| 376 &_playBuffer[0], nSamplesOut, &elapsed_time_ms, &ntp_time_ms); | 349 &_playBuffer[0], nSamplesOut, &elapsed_time_ms, &ntp_time_ms); |
| 377 if (res != 0) { | 350 if (res != 0) { |
| 378 LOG(LS_ERROR) << "NeedMorePlayData() failed"; | 351 LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
| 379 } | 352 } |
| 380 | 353 |
| 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 | |
| 386 return static_cast<int32_t>(nSamplesOut); | 354 return static_cast<int32_t>(nSamplesOut); |
| 387 } | 355 } |
| 388 | 356 |
| 389 int32_t AudioDeviceBuffer::GetPlayoutData(void* audioBuffer) { | 357 int32_t AudioDeviceBuffer::GetPlayoutData(void* audioBuffer) { |
| 390 rtc::CritScope lock(&_critSect); | 358 CriticalSectionScoped lock(&_critSect); |
| 391 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); | 359 RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); |
| 392 | 360 |
| 393 memcpy(audioBuffer, &_playBuffer[0], _playSize); | 361 memcpy(audioBuffer, &_playBuffer[0], _playSize); |
| 394 | 362 |
| 395 if (_playFile.is_open()) { | 363 if (_playFile.is_open()) { |
| 396 // write to binary file in mono or stereo (interleaved) | 364 // write to binary file in mono or stereo (interleaved) |
| 397 _playFile.Write(&_playBuffer[0], _playSize); | 365 _playFile.Write(&_playBuffer[0], _playSize); |
| 398 } | 366 } |
| 399 | 367 |
| 400 return static_cast<int32_t>(_playSamples); | 368 return static_cast<int32_t>(_playSamples); |
| 401 } | 369 } |
| 402 | 370 |
| 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 | |
| 466 } // namespace webrtc | 371 } // namespace webrtc |
| OLD | NEW |