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 | |
| 13 | #include <assert.h> |
| 14 | #include <string.h> |
| 15 | |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_device/audio_device_config.h" |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 17 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 18 | #include "webrtc/system_wrappers/interface/logging.h" |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | namespace webrtc { |
| 22 | |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 23 | static const int kHighDelayThresholdMs = 300; |
| 24 | static const int kLogHighDelayIntervalFrames = 500; // 5 seconds. |
| 25 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | // ---------------------------------------------------------------------------- |
| 27 | // ctor |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | AudioDeviceBuffer::AudioDeviceBuffer() : |
| 31 | _id(-1), |
| 32 | _critSect(*CriticalSectionWrapper::CreateCriticalSection()), |
| 33 | _critSectCb(*CriticalSectionWrapper::CreateCriticalSection()), |
| 34 | _ptrCbAudioTransport(NULL), |
| 35 | _recSampleRate(0), |
| 36 | _playSampleRate(0), |
| 37 | _recChannels(0), |
| 38 | _playChannels(0), |
| 39 | _recChannel(AudioDeviceModule::kChannelBoth), |
| 40 | _recBytesPerSample(0), |
| 41 | _playBytesPerSample(0), |
| 42 | _recSamples(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | _recSize(0), |
xians@google.com | 88bd440 | 2011-08-04 15:33:30 +0000 | [diff] [blame] | 44 | _playSamples(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | _playSize(0), |
| 46 | _recFile(*FileWrapper::Create()), |
| 47 | _playFile(*FileWrapper::Create()), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | _currentMicLevel(0), |
xians@google.com | 88bd440 | 2011-08-04 15:33:30 +0000 | [diff] [blame] | 49 | _newMicLevel(0), |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 50 | _typingStatus(false), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | _playDelayMS(0), |
| 52 | _recDelayMS(0), |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 53 | _clockDrift(0), |
| 54 | // Set to the interval in order to log on the first occurrence. |
| 55 | high_delay_counter_(kLogHighDelayIntervalFrames) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | // valid ID will be set later by SetId, use -1 for now |
| 57 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s created", __FUNCTION__); |
xians@webrtc.org | eff3c89 | 2011-12-06 10:02:56 +0000 | [diff] [blame] | 58 | memset(_recBuffer, 0, kMaxBufferSizeBytes); |
| 59 | memset(_playBuffer, 0, kMaxBufferSizeBytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | // dtor |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | AudioDeviceBuffer::~AudioDeviceBuffer() |
| 67 | { |
| 68 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s destroyed", __FUNCTION__); |
| 69 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 70 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | |
| 72 | _recFile.Flush(); |
| 73 | _recFile.CloseFile(); |
| 74 | delete &_recFile; |
| 75 | |
| 76 | _playFile.Flush(); |
| 77 | _playFile.CloseFile(); |
| 78 | delete &_playFile; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | delete &_critSect; |
| 82 | delete &_critSectCb; |
| 83 | } |
| 84 | |
| 85 | // ---------------------------------------------------------------------------- |
| 86 | // SetId |
| 87 | // ---------------------------------------------------------------------------- |
| 88 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 89 | void AudioDeviceBuffer::SetId(uint32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | { |
| 91 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, id, "AudioDeviceBuffer::SetId(id=%d)", id); |
| 92 | _id = id; |
| 93 | } |
| 94 | |
| 95 | // ---------------------------------------------------------------------------- |
| 96 | // RegisterAudioCallback |
| 97 | // ---------------------------------------------------------------------------- |
| 98 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 99 | int32_t AudioDeviceBuffer::RegisterAudioCallback(AudioTransport* audioCallback) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 101 | CriticalSectionScoped lock(&_critSectCb); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | _ptrCbAudioTransport = audioCallback; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | // ---------------------------------------------------------------------------- |
| 108 | // InitPlayout |
| 109 | // ---------------------------------------------------------------------------- |
| 110 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 111 | int32_t AudioDeviceBuffer::InitPlayout() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | { |
| 113 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s", __FUNCTION__); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | // ---------------------------------------------------------------------------- |
| 118 | // InitRecording |
| 119 | // ---------------------------------------------------------------------------- |
| 120 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 121 | int32_t AudioDeviceBuffer::InitRecording() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | { |
| 123 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s", __FUNCTION__); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | // ---------------------------------------------------------------------------- |
| 128 | // SetRecordingSampleRate |
| 129 | // ---------------------------------------------------------------------------- |
| 130 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 131 | int32_t AudioDeviceBuffer::SetRecordingSampleRate(uint32_t fsHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 133 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | _recSampleRate = fsHz; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | // SetPlayoutSampleRate |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 142 | int32_t AudioDeviceBuffer::SetPlayoutSampleRate(uint32_t fsHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 144 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | _playSampleRate = fsHz; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | // ---------------------------------------------------------------------------- |
| 150 | // RecordingSampleRate |
| 151 | // ---------------------------------------------------------------------------- |
| 152 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 153 | int32_t AudioDeviceBuffer::RecordingSampleRate() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | { |
| 155 | return _recSampleRate; |
| 156 | } |
| 157 | |
| 158 | // ---------------------------------------------------------------------------- |
| 159 | // PlayoutSampleRate |
| 160 | // ---------------------------------------------------------------------------- |
| 161 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 162 | int32_t AudioDeviceBuffer::PlayoutSampleRate() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | { |
| 164 | return _playSampleRate; |
| 165 | } |
| 166 | |
| 167 | // ---------------------------------------------------------------------------- |
| 168 | // SetRecordingChannels |
| 169 | // ---------------------------------------------------------------------------- |
| 170 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 171 | int32_t AudioDeviceBuffer::SetRecordingChannels(uint8_t channels) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 173 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 174 | _recChannels = channels; |
| 175 | _recBytesPerSample = 2*channels; // 16 bits per sample in mono, 32 bits in stereo |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | // ---------------------------------------------------------------------------- |
| 180 | // SetPlayoutChannels |
| 181 | // ---------------------------------------------------------------------------- |
| 182 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 183 | int32_t AudioDeviceBuffer::SetPlayoutChannels(uint8_t channels) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 185 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | _playChannels = channels; |
| 187 | // 16 bits per sample in mono, 32 bits in stereo |
| 188 | _playBytesPerSample = 2*channels; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | // ---------------------------------------------------------------------------- |
| 193 | // SetRecordingChannel |
| 194 | // |
| 195 | // Select which channel to use while recording. |
| 196 | // This API requires that stereo is enabled. |
| 197 | // |
| 198 | // Note that, the nChannel parameter in RecordedDataIsAvailable will be |
| 199 | // set to 2 even for kChannelLeft and kChannelRight. However, nBytesPerSample |
| 200 | // will be 2 instead of 4 four these cases. |
| 201 | // ---------------------------------------------------------------------------- |
| 202 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 203 | int32_t AudioDeviceBuffer::SetRecordingChannel(const AudioDeviceModule::ChannelType channel) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 205 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | |
| 207 | if (_recChannels == 1) |
| 208 | { |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | if (channel == AudioDeviceModule::kChannelBoth) |
| 213 | { |
| 214 | // two bytes per channel |
| 215 | _recBytesPerSample = 4; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | // only utilize one out of two possible channels (left or right) |
| 220 | _recBytesPerSample = 2; |
| 221 | } |
| 222 | _recChannel = channel; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | // ---------------------------------------------------------------------------- |
| 228 | // RecordingChannel |
| 229 | // ---------------------------------------------------------------------------- |
| 230 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 231 | int32_t AudioDeviceBuffer::RecordingChannel(AudioDeviceModule::ChannelType& channel) const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | { |
| 233 | channel = _recChannel; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | // ---------------------------------------------------------------------------- |
| 238 | // RecordingChannels |
| 239 | // ---------------------------------------------------------------------------- |
| 240 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 241 | uint8_t AudioDeviceBuffer::RecordingChannels() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | { |
| 243 | return _recChannels; |
| 244 | } |
| 245 | |
| 246 | // ---------------------------------------------------------------------------- |
| 247 | // PlayoutChannels |
| 248 | // ---------------------------------------------------------------------------- |
| 249 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 250 | uint8_t AudioDeviceBuffer::PlayoutChannels() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | { |
| 252 | return _playChannels; |
| 253 | } |
| 254 | |
| 255 | // ---------------------------------------------------------------------------- |
| 256 | // SetCurrentMicLevel |
| 257 | // ---------------------------------------------------------------------------- |
| 258 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 259 | int32_t AudioDeviceBuffer::SetCurrentMicLevel(uint32_t level) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | { |
| 261 | _currentMicLevel = level; |
| 262 | return 0; |
| 263 | } |
| 264 | |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 265 | int32_t AudioDeviceBuffer::SetTypingStatus(bool typingStatus) |
| 266 | { |
| 267 | _typingStatus = typingStatus; |
| 268 | return 0; |
| 269 | } |
| 270 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 271 | // ---------------------------------------------------------------------------- |
| 272 | // NewMicLevel |
| 273 | // ---------------------------------------------------------------------------- |
| 274 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 275 | uint32_t AudioDeviceBuffer::NewMicLevel() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 276 | { |
| 277 | return _newMicLevel; |
| 278 | } |
| 279 | |
| 280 | // ---------------------------------------------------------------------------- |
| 281 | // SetVQEData |
| 282 | // ---------------------------------------------------------------------------- |
| 283 | |
andrew@webrtc.org | 5eb997a | 2013-09-12 01:01:42 +0000 | [diff] [blame] | 284 | void AudioDeviceBuffer::SetVQEData(int playDelayMs, int recDelayMs, |
| 285 | int clockDrift) { |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 286 | if (high_delay_counter_ < kLogHighDelayIntervalFrames) { |
| 287 | ++high_delay_counter_; |
| 288 | } else { |
| 289 | if (playDelayMs + recDelayMs > kHighDelayThresholdMs) { |
| 290 | high_delay_counter_ = 0; |
| 291 | LOG(LS_WARNING) << "High audio device delay reported (render=" |
| 292 | << playDelayMs << " ms, capture=" << recDelayMs << " ms)"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | } |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 294 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 295 | |
andrew@webrtc.org | 8f94013 | 2013-09-11 22:35:00 +0000 | [diff] [blame] | 296 | _playDelayMS = playDelayMs; |
| 297 | _recDelayMS = recDelayMs; |
| 298 | _clockDrift = clockDrift; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // ---------------------------------------------------------------------------- |
| 302 | // StartInputFileRecording |
| 303 | // ---------------------------------------------------------------------------- |
| 304 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 305 | int32_t AudioDeviceBuffer::StartInputFileRecording( |
leozwang@webrtc.org | 28f3913 | 2012-03-01 18:01:48 +0000 | [diff] [blame] | 306 | const char fileName[kAdmMaxFileNameSize]) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 307 | { |
| 308 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s", __FUNCTION__); |
| 309 | |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 310 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | |
| 312 | _recFile.Flush(); |
| 313 | _recFile.CloseFile(); |
| 314 | |
| 315 | return (_recFile.OpenFile(fileName, false, false, false)); |
| 316 | } |
| 317 | |
| 318 | // ---------------------------------------------------------------------------- |
| 319 | // StopInputFileRecording |
| 320 | // ---------------------------------------------------------------------------- |
| 321 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 322 | int32_t AudioDeviceBuffer::StopInputFileRecording() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 323 | { |
| 324 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s", __FUNCTION__); |
| 325 | |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 326 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 327 | |
| 328 | _recFile.Flush(); |
| 329 | _recFile.CloseFile(); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | // ---------------------------------------------------------------------------- |
| 335 | // StartOutputFileRecording |
| 336 | // ---------------------------------------------------------------------------- |
| 337 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 338 | int32_t AudioDeviceBuffer::StartOutputFileRecording( |
leozwang@webrtc.org | 28f3913 | 2012-03-01 18:01:48 +0000 | [diff] [blame] | 339 | const char fileName[kAdmMaxFileNameSize]) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 340 | { |
| 341 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s", __FUNCTION__); |
| 342 | |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 343 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 344 | |
| 345 | _playFile.Flush(); |
| 346 | _playFile.CloseFile(); |
| 347 | |
| 348 | return (_playFile.OpenFile(fileName, false, false, false)); |
| 349 | } |
| 350 | |
| 351 | // ---------------------------------------------------------------------------- |
| 352 | // StopOutputFileRecording |
| 353 | // ---------------------------------------------------------------------------- |
| 354 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 355 | int32_t AudioDeviceBuffer::StopOutputFileRecording() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 356 | { |
| 357 | WEBRTC_TRACE(kTraceMemory, kTraceAudioDevice, _id, "%s", __FUNCTION__); |
| 358 | |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 359 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 360 | |
| 361 | _playFile.Flush(); |
| 362 | _playFile.CloseFile(); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | // ---------------------------------------------------------------------------- |
| 368 | // SetRecordedBuffer |
| 369 | // |
| 370 | // Store recorded audio buffer in local memory ready for the actual |
| 371 | // "delivery" using a callback. |
| 372 | // |
| 373 | // This method can also parse out left or right channel from a stereo |
| 374 | // input signal, i.e., emulate mono. |
| 375 | // |
| 376 | // Examples: |
| 377 | // |
| 378 | // 16-bit,48kHz mono, 10ms => nSamples=480 => _recSize=2*480=960 bytes |
braveyao@webrtc.org | 0a18522 | 2011-11-25 02:45:39 +0000 | [diff] [blame] | 379 | // 16-bit,48kHz stereo,10ms => nSamples=480 => _recSize=4*480=1920 bytes |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 380 | // ---------------------------------------------------------------------------- |
| 381 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 382 | int32_t AudioDeviceBuffer::SetRecordedBuffer(const void* audioBuffer, |
| 383 | uint32_t nSamples) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 384 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 385 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 386 | |
| 387 | if (_recBytesPerSample == 0) |
| 388 | { |
| 389 | assert(false); |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | _recSamples = nSamples; |
| 394 | _recSize = _recBytesPerSample*nSamples; // {2,4}*nSamples |
braveyao@webrtc.org | 0a18522 | 2011-11-25 02:45:39 +0000 | [diff] [blame] | 395 | if (_recSize > kMaxBufferSizeBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 396 | { |
| 397 | assert(false); |
| 398 | return -1; |
| 399 | } |
| 400 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | if (_recChannel == AudioDeviceModule::kChannelBoth) |
| 402 | { |
| 403 | // (default) copy the complete input buffer to the local buffer |
| 404 | memcpy(&_recBuffer[0], audioBuffer, _recSize); |
| 405 | } |
| 406 | else |
| 407 | { |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 408 | int16_t* ptr16In = (int16_t*)audioBuffer; |
| 409 | int16_t* ptr16Out = (int16_t*)&_recBuffer[0]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | |
| 411 | if (AudioDeviceModule::kChannelRight == _recChannel) |
| 412 | { |
| 413 | ptr16In++; |
| 414 | } |
| 415 | |
| 416 | // exctract left or right channel from input buffer to the local buffer |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 417 | for (uint32_t i = 0; i < _recSamples; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 418 | { |
| 419 | *ptr16Out = *ptr16In; |
| 420 | ptr16Out++; |
| 421 | ptr16In++; |
| 422 | ptr16In++; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | if (_recFile.Open()) |
| 427 | { |
| 428 | // write to binary file in mono or stereo (interleaved) |
| 429 | _recFile.Write(&_recBuffer[0], _recSize); |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | // ---------------------------------------------------------------------------- |
| 436 | // DeliverRecordedData |
| 437 | // ---------------------------------------------------------------------------- |
| 438 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 439 | int32_t AudioDeviceBuffer::DeliverRecordedData() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 440 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 441 | CriticalSectionScoped lock(&_critSectCb); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 442 | |
| 443 | // Ensure that user has initialized all essential members |
| 444 | if ((_recSampleRate == 0) || |
| 445 | (_recSamples == 0) || |
| 446 | (_recBytesPerSample == 0) || |
| 447 | (_recChannels == 0)) |
| 448 | { |
| 449 | assert(false); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | if (_ptrCbAudioTransport == NULL) |
| 454 | { |
| 455 | WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "failed to deliver recorded data (AudioTransport does not exist)"); |
| 456 | return 0; |
| 457 | } |
| 458 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 459 | int32_t res(0); |
| 460 | uint32_t newMicLevel(0); |
| 461 | uint32_t totalDelayMS = _playDelayMS +_recDelayMS; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 462 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 463 | res = _ptrCbAudioTransport->RecordedDataIsAvailable(&_recBuffer[0], |
| 464 | _recSamples, |
| 465 | _recBytesPerSample, |
| 466 | _recChannels, |
| 467 | _recSampleRate, |
| 468 | totalDelayMS, |
| 469 | _clockDrift, |
| 470 | _currentMicLevel, |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 471 | _typingStatus, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 472 | newMicLevel); |
| 473 | if (res != -1) |
| 474 | { |
| 475 | _newMicLevel = newMicLevel; |
| 476 | } |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | // ---------------------------------------------------------------------------- |
| 482 | // RequestPlayoutData |
| 483 | // ---------------------------------------------------------------------------- |
| 484 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 485 | int32_t AudioDeviceBuffer::RequestPlayoutData(uint32_t nSamples) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 486 | { |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 487 | uint32_t playSampleRate = 0; |
| 488 | uint8_t playBytesPerSample = 0; |
| 489 | uint8_t playChannels = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 490 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 491 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 492 | |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 493 | // Store copies under lock and use copies hereafter to avoid race with |
| 494 | // setter methods. |
| 495 | playSampleRate = _playSampleRate; |
| 496 | playBytesPerSample = _playBytesPerSample; |
| 497 | playChannels = _playChannels; |
| 498 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 499 | // Ensure that user has initialized all essential members |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 500 | if ((playBytesPerSample == 0) || |
| 501 | (playChannels == 0) || |
| 502 | (playSampleRate == 0)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 503 | { |
| 504 | assert(false); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | _playSamples = nSamples; |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 509 | _playSize = playBytesPerSample * nSamples; // {2,4}*nSamples |
braveyao@webrtc.org | 0a18522 | 2011-11-25 02:45:39 +0000 | [diff] [blame] | 510 | if (_playSize > kMaxBufferSizeBytes) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 511 | { |
| 512 | assert(false); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | if (nSamples != _playSamples) |
| 517 | { |
| 518 | WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "invalid number of samples to be played out (%d)", nSamples); |
| 519 | return -1; |
| 520 | } |
| 521 | } |
| 522 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 523 | uint32_t nSamplesOut(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 524 | |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 525 | CriticalSectionScoped lock(&_critSectCb); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 526 | |
| 527 | if (_ptrCbAudioTransport == NULL) |
| 528 | { |
| 529 | WEBRTC_TRACE(kTraceWarning, kTraceAudioDevice, _id, "failed to feed data to playout (AudioTransport does not exist)"); |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | if (_ptrCbAudioTransport) |
| 534 | { |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 535 | uint32_t res(0); |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 536 | int64_t elapsed_time_ms = -1; |
| 537 | int64_t ntp_time_ms = -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 538 | res = _ptrCbAudioTransport->NeedMorePlayData(_playSamples, |
henrika@webrtc.org | 19da719 | 2013-04-05 14:34:57 +0000 | [diff] [blame] | 539 | playBytesPerSample, |
| 540 | playChannels, |
| 541 | playSampleRate, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 542 | &_playBuffer[0], |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 543 | nSamplesOut, |
wu@webrtc.org | 94454b7 | 2014-06-05 20:34:08 +0000 | [diff] [blame] | 544 | &elapsed_time_ms, |
wu@webrtc.org | cb711f7 | 2014-05-19 17:39:11 +0000 | [diff] [blame] | 545 | &ntp_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 546 | if (res != 0) |
| 547 | { |
| 548 | WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "NeedMorePlayData() failed"); |
| 549 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 552 | return static_cast<int32_t>(nSamplesOut); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | // ---------------------------------------------------------------------------- |
| 556 | // GetPlayoutData |
| 557 | // ---------------------------------------------------------------------------- |
| 558 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 559 | int32_t AudioDeviceBuffer::GetPlayoutData(void* audioBuffer) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 560 | { |
mflodman@webrtc.org | a014ecc | 2012-04-12 12:15:51 +0000 | [diff] [blame] | 561 | CriticalSectionScoped lock(&_critSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 562 | |
punyabrata@webrtc.org | c980146 | 2011-11-29 18:49:54 +0000 | [diff] [blame] | 563 | if (_playSize > kMaxBufferSizeBytes) |
| 564 | { |
Peter Kasting | 728d903 | 2015-06-11 14:31:38 -0700 | [diff] [blame] | 565 | WEBRTC_TRACE(kTraceError, kTraceUtility, _id, |
| 566 | "_playSize %i exceeds kMaxBufferSizeBytes in " |
| 567 | "AudioDeviceBuffer::GetPlayoutData", _playSize); |
punyabrata@webrtc.org | c980146 | 2011-11-29 18:49:54 +0000 | [diff] [blame] | 568 | assert(false); |
leozwang@webrtc.org | 28f3913 | 2012-03-01 18:01:48 +0000 | [diff] [blame] | 569 | return -1; |
| 570 | } |
punyabrata@webrtc.org | c980146 | 2011-11-29 18:49:54 +0000 | [diff] [blame] | 571 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 572 | memcpy(audioBuffer, &_playBuffer[0], _playSize); |
| 573 | |
| 574 | if (_playFile.Open()) |
| 575 | { |
| 576 | // write to binary file in mono or stereo (interleaved) |
| 577 | _playFile.Write(&_playBuffer[0], _playSize); |
| 578 | } |
| 579 | |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 580 | return static_cast<int32_t>(_playSamples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 581 | } |
| 582 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 583 | } // namespace webrtc |