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