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