niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 20aabbb | 2012-02-20 09:17:41 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_device/audio_device_buffer.h" |
andrew@webrtc.org | 2553450 | 2013-09-13 00:02:13 +0000 | [diff] [blame] | 12 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 13 | #include "webrtc/base/bind.h" |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 14 | #include "webrtc/base/checks.h" |
| 15 | #include "webrtc/base/logging.h" |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 16 | #include "webrtc/base/format_macros.h" |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 17 | #include "webrtc/base/timeutils.h" |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_device/audio_device_config.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 22 | static const int kHighDelayThresholdMs = 300; |
| 23 | static const int kLogHighDelayIntervalFrames = 500; // 5 seconds. |
| 24 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 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 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 32 | AudioDeviceBuffer::AudioDeviceBuffer() |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 33 | : _ptrCbAudioTransport(nullptr), |
| 34 | task_queue_(kTimerQueueName), |
| 35 | timer_has_started_(false), |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 36 | _recSampleRate(0), |
| 37 | _playSampleRate(0), |
| 38 | _recChannels(0), |
| 39 | _playChannels(0), |
| 40 | _recChannel(AudioDeviceModule::kChannelBoth), |
| 41 | _recBytesPerSample(0), |
| 42 | _playBytesPerSample(0), |
| 43 | _recSamples(0), |
| 44 | _recSize(0), |
| 45 | _playSamples(0), |
| 46 | _playSize(0), |
| 47 | _recFile(*FileWrapper::Create()), |
| 48 | _playFile(*FileWrapper::Create()), |
| 49 | _currentMicLevel(0), |
| 50 | _newMicLevel(0), |
| 51 | _typingStatus(false), |
| 52 | _playDelayMS(0), |
| 53 | _recDelayMS(0), |
| 54 | _clockDrift(0), |
| 55 | // Set to the interval in order to log on the first occurrence. |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 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) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 67 | LOG(INFO) << "AudioDeviceBuffer::ctor"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 68 | memset(_recBuffer, 0, kMaxBufferSizeBytes); |
| 69 | memset(_playBuffer, 0, kMaxBufferSizeBytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 72 | AudioDeviceBuffer::~AudioDeviceBuffer() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 73 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 74 | LOG(INFO) << "AudioDeviceBuffer::~dtor"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 75 | _recFile.Flush(); |
| 76 | _recFile.CloseFile(); |
| 77 | delete &_recFile; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 79 | _playFile.Flush(); |
| 80 | _playFile.CloseFile(); |
| 81 | delete &_playFile; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | } |
| 83 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 84 | int32_t AudioDeviceBuffer::RegisterAudioCallback( |
| 85 | AudioTransport* audioCallback) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 86 | LOG(INFO) << __FUNCTION__; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 87 | rtc::CritScope lock(&_critSectCb); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 88 | _ptrCbAudioTransport = audioCallback; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 89 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | } |
| 91 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 92 | int32_t AudioDeviceBuffer::InitPlayout() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 93 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 94 | LOG(INFO) << __FUNCTION__; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 95 | if (!timer_has_started_) { |
| 96 | StartTimer(); |
| 97 | timer_has_started_ = true; |
| 98 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 99 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | } |
| 101 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 102 | int32_t AudioDeviceBuffer::InitRecording() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 103 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 104 | LOG(INFO) << __FUNCTION__; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 105 | if (!timer_has_started_) { |
| 106 | StartTimer(); |
| 107 | timer_has_started_ = true; |
| 108 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 109 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | } |
| 111 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 112 | int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 113 | LOG(INFO) << "SetRecordingSampleRate(" << fsHz << ")"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 114 | rtc::CritScope lock(&_critSect); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 115 | _recSampleRate = fsHz; |
| 116 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 119 | int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 120 | LOG(INFO) << "SetPlayoutSampleRate(" << fsHz << ")"; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 121 | rtc::CritScope lock(&_critSect); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 122 | _playSampleRate = fsHz; |
| 123 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 126 | int32_t AudioDeviceBuffer::RecordingSampleRate() const { |
| 127 | return _recSampleRate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 130 | int32_t AudioDeviceBuffer::PlayoutSampleRate() const { |
| 131 | return _playSampleRate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 134 | int32_t AudioDeviceBuffer::SetRecordingChannels(size_t channels) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 135 | rtc::CritScope lock(&_critSect); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 136 | _recChannels = channels; |
| 137 | _recBytesPerSample = |
| 138 | 2 * channels; // 16 bits per sample in mono, 32 bits in stereo |
| 139 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
| 141 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 142 | int32_t AudioDeviceBuffer::SetPlayoutChannels(size_t channels) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 143 | rtc::CritScope lock(&_critSect); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 144 | _playChannels = channels; |
| 145 | // 16 bits per sample in mono, 32 bits in stereo |
| 146 | _playBytesPerSample = 2 * channels; |
| 147 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 150 | int32_t AudioDeviceBuffer::SetRecordingChannel( |
| 151 | const AudioDeviceModule::ChannelType channel) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 152 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 154 | if (_recChannels == 1) { |
| 155 | return -1; |
| 156 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 158 | if (channel == AudioDeviceModule::kChannelBoth) { |
| 159 | // two bytes per channel |
| 160 | _recBytesPerSample = 4; |
| 161 | } else { |
| 162 | // only utilize one out of two possible channels (left or right) |
| 163 | _recBytesPerSample = 2; |
| 164 | } |
| 165 | _recChannel = channel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 167 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | } |
| 169 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 170 | int32_t AudioDeviceBuffer::RecordingChannel( |
| 171 | AudioDeviceModule::ChannelType& channel) const { |
| 172 | channel = _recChannel; |
| 173 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 176 | size_t AudioDeviceBuffer::RecordingChannels() const { |
| 177 | return _recChannels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | } |
| 179 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 180 | size_t AudioDeviceBuffer::PlayoutChannels() const { |
| 181 | return _playChannels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 184 | int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) { |
| 185 | _currentMicLevel = level; |
| 186 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 189 | int32_t AudioDeviceBuffer::SetTypingStatus(bool typingStatus) { |
| 190 | _typingStatus = typingStatus; |
| 191 | return 0; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 194 | uint32_t AudioDeviceBuffer::NewMicLevel() const { |
| 195 | return _newMicLevel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | } |
| 197 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 198 | void AudioDeviceBuffer::SetVQEData(int playDelayMs, |
| 199 | int recDelayMs, |
andrew@webrtc.org | 5eb997a | 2013-09-12 01:01:42 +0000 | [diff] [blame] | 200 | int clockDrift) { |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 201 | if (high_delay_counter_ < kLogHighDelayIntervalFrames) { |
| 202 | ++high_delay_counter_; |
| 203 | } else { |
| 204 | if (playDelayMs + recDelayMs > kHighDelayThresholdMs) { |
| 205 | high_delay_counter_ = 0; |
| 206 | LOG(LS_WARNING) << "High audio device delay reported (render=" |
| 207 | << playDelayMs << " ms, capture=" << recDelayMs << " ms)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | } |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 209 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 211 | _playDelayMS = playDelayMs; |
| 212 | _recDelayMS = recDelayMs; |
| 213 | _clockDrift = clockDrift; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | } |
| 215 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 216 | int32_t AudioDeviceBuffer::StartInputFileRecording( |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 217 | const char fileName[kAdmMaxFileNameSize]) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 218 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 220 | _recFile.Flush(); |
| 221 | _recFile.CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 223 | return _recFile.OpenFile(fileName, false) ? 0 : -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 226 | int32_t AudioDeviceBuffer::StopInputFileRecording() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 227 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 229 | _recFile.Flush(); |
| 230 | _recFile.CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 232 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | } |
| 234 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 235 | int32_t AudioDeviceBuffer::StartOutputFileRecording( |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 236 | const char fileName[kAdmMaxFileNameSize]) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 237 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 238 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 239 | _playFile.Flush(); |
| 240 | _playFile.CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 242 | return _playFile.OpenFile(fileName, false) ? 0 : -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | } |
| 244 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 245 | int32_t AudioDeviceBuffer::StopOutputFileRecording() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 246 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 248 | _playFile.Flush(); |
| 249 | _playFile.CloseFile(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 251 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | } |
| 253 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 254 | int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audioBuffer, |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 255 | size_t nSamples) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 256 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 257 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 258 | if (_recBytesPerSample == 0) { |
| 259 | assert(false); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | _recSamples = nSamples; |
| 264 | _recSize = _recBytesPerSample * nSamples; // {2,4}*nSamples |
| 265 | if (_recSize > kMaxBufferSizeBytes) { |
| 266 | assert(false); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | if (_recChannel == AudioDeviceModule::kChannelBoth) { |
| 271 | // (default) copy the complete input buffer to the local buffer |
| 272 | memcpy(&_recBuffer[0], audioBuffer, _recSize); |
| 273 | } else { |
| 274 | int16_t* ptr16In = (int16_t*)audioBuffer; |
| 275 | int16_t* ptr16Out = (int16_t*)&_recBuffer[0]; |
| 276 | |
| 277 | if (AudioDeviceModule::kChannelRight == _recChannel) { |
| 278 | ptr16In++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | } |
| 280 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 281 | // exctract left or right channel from input buffer to the local buffer |
| 282 | for (size_t i = 0; i < _recSamples; i++) { |
| 283 | *ptr16Out = *ptr16In; |
| 284 | ptr16Out++; |
| 285 | ptr16In++; |
| 286 | ptr16In++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | } |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 288 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 290 | if (_recFile.is_open()) { |
| 291 | // write to binary file in mono or stereo (interleaved) |
| 292 | _recFile.Write(&_recBuffer[0], _recSize); |
| 293 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 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 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 300 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | } |
| 302 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 303 | int32_t AudioDeviceBuffer::DeliverRecordedData() { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 304 | rtc::CritScope lock(&_critSectCb); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 305 | // Ensure that user has initialized all essential members |
| 306 | if ((_recSampleRate == 0) || (_recSamples == 0) || |
| 307 | (_recBytesPerSample == 0) || (_recChannels == 0)) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 308 | RTC_NOTREACHED(); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 309 | return -1; |
| 310 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 312 | if (!_ptrCbAudioTransport) { |
| 313 | LOG(LS_WARNING) << "Invalid audio transport"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | return 0; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | int32_t res(0); |
| 318 | uint32_t newMicLevel(0); |
| 319 | uint32_t totalDelayMS = _playDelayMS + _recDelayMS; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 320 | res = _ptrCbAudioTransport->RecordedDataIsAvailable( |
| 321 | &_recBuffer[0], _recSamples, _recBytesPerSample, _recChannels, |
| 322 | _recSampleRate, totalDelayMS, _clockDrift, _currentMicLevel, |
| 323 | _typingStatus, newMicLevel); |
| 324 | if (res != -1) { |
| 325 | _newMicLevel = newMicLevel; |
| 326 | } |
| 327 | |
| 328 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 329 | } |
| 330 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 331 | int32_t AudioDeviceBuffer::RequestPlayoutData(size_t nSamples) { |
| 332 | uint32_t playSampleRate = 0; |
| 333 | size_t playBytesPerSample = 0; |
| 334 | size_t playChannels = 0; |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 335 | |
| 336 | // TOOD(henrika): improve bad locking model and make it more clear that only |
| 337 | // 10ms buffer sizes is supported in WebRTC. |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 338 | { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 339 | rtc::CritScope lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 341 | // Store copies under lock and use copies hereafter to avoid race with |
| 342 | // setter methods. |
| 343 | playSampleRate = _playSampleRate; |
| 344 | playBytesPerSample = _playBytesPerSample; |
| 345 | playChannels = _playChannels; |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 346 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 347 | // Ensure that user has initialized all essential members |
| 348 | if ((playBytesPerSample == 0) || (playChannels == 0) || |
| 349 | (playSampleRate == 0)) { |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 350 | RTC_NOTREACHED(); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 351 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 352 | } |
| 353 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 354 | _playSamples = nSamples; |
| 355 | _playSize = playBytesPerSample * nSamples; // {2,4}*nSamples |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 356 | RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); |
| 357 | RTC_CHECK_EQ(nSamples, _playSamples); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 358 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 359 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 360 | size_t nSamplesOut(0); |
| 361 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 362 | rtc::CritScope lock(&_critSectCb); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 363 | |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 364 | // It is currently supported to start playout without a valid audio |
| 365 | // transport object. Leads to warning and silence. |
| 366 | if (!_ptrCbAudioTransport) { |
| 367 | LOG(LS_WARNING) << "Invalid audio transport"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 371 | uint32_t res(0); |
| 372 | int64_t elapsed_time_ms = -1; |
| 373 | int64_t ntp_time_ms = -1; |
| 374 | res = _ptrCbAudioTransport->NeedMorePlayData( |
| 375 | _playSamples, playBytesPerSample, playChannels, playSampleRate, |
| 376 | &_playBuffer[0], nSamplesOut, &elapsed_time_ms, &ntp_time_ms); |
| 377 | if (res != 0) { |
| 378 | LOG(LS_ERROR) << "NeedMorePlayData() failed"; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 379 | } |
| 380 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 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 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 386 | return static_cast<int32_t>(nSamplesOut); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 387 | } |
| 388 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 389 | int32_t AudioDeviceBuffer::GetPlayoutData(void* audioBuffer) { |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 390 | rtc::CritScope lock(&_critSect); |
henrika | 3f33e2a | 2016-07-06 00:33:57 -0700 | [diff] [blame] | 391 | RTC_CHECK_LE(_playSize, kMaxBufferSizeBytes); |
punyabrata@webrtc.org | c980146 | 2011-11-29 18:49:54 +0000 | [diff] [blame] | 392 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 393 | memcpy(audioBuffer, &_playBuffer[0], _playSize); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 394 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 395 | if (_playFile.is_open()) { |
| 396 | // write to binary file in mono or stereo (interleaved) |
| 397 | _playFile.Write(&_playBuffer[0], _playSize); |
| 398 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 400 | return static_cast<int32_t>(_playSamples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | } |
| 402 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 466 | } // namespace webrtc |