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