niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +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 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame^] | 11 | #include "webrtc/voice_engine/output_mixer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame^] | 13 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 14 | #include "webrtc/modules/utility/interface/audio_frame_operations.h" |
| 15 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 16 | #include "webrtc/system_wrappers/interface/file_wrapper.h" |
| 17 | #include "webrtc/system_wrappers/interface/trace.h" |
| 18 | #include "webrtc/voice_engine/include/voe_external_media.h" |
| 19 | #include "webrtc/voice_engine/output_mixer_internal.h" |
| 20 | #include "webrtc/voice_engine/statistics.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | namespace voe { |
| 25 | |
| 26 | void |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 27 | OutputMixer::NewMixedAudio(const int32_t id, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | const AudioFrame& generalAudioFrame, |
| 29 | const AudioFrame** uniqueAudioFrames, |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 30 | const uint32_t size) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | { |
| 32 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 33 | "OutputMixer::NewMixedAudio(id=%d, size=%u)", id, size); |
| 34 | |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 35 | _audioFrame.CopyFrom(generalAudioFrame); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 36 | _audioFrame.id_ = id; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void OutputMixer::MixedParticipants( |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 40 | const int32_t id, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | const ParticipantStatistics* participantStatistics, |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 42 | const uint32_t size) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | { |
| 44 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 45 | "OutputMixer::MixedParticipants(id=%d, size=%u)", id, size); |
| 46 | } |
| 47 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 48 | void OutputMixer::VADPositiveParticipants(const int32_t id, |
| 49 | const ParticipantStatistics* participantStatistics, const uint32_t size) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | { |
| 51 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 52 | "OutputMixer::VADPositiveParticipants(id=%d, size=%u)", |
| 53 | id, size); |
| 54 | } |
| 55 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 56 | void OutputMixer::MixedAudioLevel(const int32_t id, const uint32_t level) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | { |
| 58 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 59 | "OutputMixer::MixedAudioLevel(id=%d, level=%u)", id, level); |
| 60 | } |
| 61 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 62 | void OutputMixer::PlayNotification(const int32_t id, const uint32_t durationMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | { |
| 64 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 65 | "OutputMixer::PlayNotification(id=%d, durationMs=%d)", |
| 66 | id, durationMs); |
| 67 | // Not implement yet |
| 68 | } |
| 69 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 70 | void OutputMixer::RecordNotification(const int32_t id, |
| 71 | const uint32_t durationMs) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | { |
| 73 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 74 | "OutputMixer::RecordNotification(id=%d, durationMs=%d)", |
| 75 | id, durationMs); |
| 76 | |
| 77 | // Not implement yet |
| 78 | } |
| 79 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 80 | void OutputMixer::PlayFileEnded(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | { |
| 82 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 83 | "OutputMixer::PlayFileEnded(id=%d)", id); |
| 84 | |
| 85 | // not needed |
| 86 | } |
| 87 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 88 | void OutputMixer::RecordFileEnded(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | { |
| 90 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 91 | "OutputMixer::RecordFileEnded(id=%d)", id); |
| 92 | assert(id == _instanceId); |
| 93 | |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 94 | CriticalSectionScoped cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | _outputFileRecording = false; |
| 96 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 97 | "OutputMixer::RecordFileEnded() =>" |
| 98 | "output file recorder module is shutdown"); |
| 99 | } |
| 100 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 101 | int32_t |
| 102 | OutputMixer::Create(OutputMixer*& mixer, const uint32_t instanceId) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | { |
| 104 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, instanceId, |
| 105 | "OutputMixer::Create(instanceId=%d)", instanceId); |
| 106 | mixer = new OutputMixer(instanceId); |
| 107 | if (mixer == NULL) |
| 108 | { |
| 109 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, instanceId, |
| 110 | "OutputMixer::Create() unable to allocate memory for" |
| 111 | "mixer"); |
| 112 | return -1; |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 117 | OutputMixer::OutputMixer(const uint32_t instanceId) : |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()), |
| 119 | _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()), |
andrew@webrtc.org | c4f129f | 2011-11-10 03:41:22 +0000 | [diff] [blame] | 120 | _mixerModule(*AudioConferenceMixer::Create(instanceId)), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | _audioLevel(), |
xians@google.com | 22963ab | 2011-08-03 12:40:23 +0000 | [diff] [blame] | 122 | _dtmfGenerator(instanceId), |
| 123 | _instanceId(instanceId), |
| 124 | _externalMediaCallbackPtr(NULL), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | _externalMedia(false), |
| 126 | _panLeft(1.0f), |
| 127 | _panRight(1.0f), |
xians@google.com | 22963ab | 2011-08-03 12:40:23 +0000 | [diff] [blame] | 128 | _mixingFrequencyHz(8000), |
| 129 | _outputFileRecorderPtr(NULL), |
| 130 | _outputFileRecording(false) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | { |
| 132 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,-1), |
| 133 | "OutputMixer::OutputMixer() - ctor"); |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 134 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | if ((_mixerModule.RegisterMixedStreamCallback(*this) == -1) || |
| 136 | (_mixerModule.RegisterMixerStatusCallback(*this, 100) == -1)) |
| 137 | { |
| 138 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1), |
| 139 | "OutputMixer::OutputMixer() failed to register mixer" |
| 140 | "callbacks"); |
| 141 | } |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 142 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | _dtmfGenerator.Init(); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | OutputMixer::Destroy(OutputMixer*& mixer) |
| 148 | { |
| 149 | if (mixer) |
| 150 | { |
| 151 | delete mixer; |
| 152 | mixer = NULL; |
| 153 | } |
| 154 | } |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 155 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | OutputMixer::~OutputMixer() |
| 157 | { |
| 158 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,-1), |
| 159 | "OutputMixer::~OutputMixer() - dtor"); |
| 160 | if (_externalMedia) |
| 161 | { |
| 162 | DeRegisterExternalMediaProcessing(); |
| 163 | } |
| 164 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 165 | CriticalSectionScoped cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | if (_outputFileRecorderPtr) |
| 167 | { |
| 168 | _outputFileRecorderPtr->RegisterModuleFileCallback(NULL); |
| 169 | _outputFileRecorderPtr->StopRecording(); |
| 170 | FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr); |
| 171 | _outputFileRecorderPtr = NULL; |
| 172 | } |
| 173 | } |
| 174 | _mixerModule.UnRegisterMixerStatusCallback(); |
| 175 | _mixerModule.UnRegisterMixedStreamCallback(); |
| 176 | delete &_mixerModule; |
| 177 | delete &_callbackCritSect; |
| 178 | delete &_fileCritSect; |
| 179 | } |
| 180 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 181 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | OutputMixer::SetEngineInformation(voe::Statistics& engineStatistics) |
| 183 | { |
| 184 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 185 | "OutputMixer::SetEngineInformation()"); |
| 186 | _engineStatisticsPtr = &engineStatistics; |
| 187 | return 0; |
| 188 | } |
| 189 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 190 | int32_t |
| 191 | OutputMixer::SetAudioProcessingModule(AudioProcessing* audioProcessingModule) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | { |
| 193 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 194 | "OutputMixer::SetAudioProcessingModule(" |
| 195 | "audioProcessingModule=0x%x)", audioProcessingModule); |
| 196 | _audioProcessingModulePtr = audioProcessingModule; |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int OutputMixer::RegisterExternalMediaProcessing( |
| 201 | VoEMediaProcess& proccess_object) |
| 202 | { |
| 203 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 204 | "OutputMixer::RegisterExternalMediaProcessing()"); |
| 205 | |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 206 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | _externalMediaCallbackPtr = &proccess_object; |
| 208 | _externalMedia = true; |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int OutputMixer::DeRegisterExternalMediaProcessing() |
| 214 | { |
| 215 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 216 | "OutputMixer::DeRegisterExternalMediaProcessing()"); |
| 217 | |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 218 | CriticalSectionScoped cs(&_callbackCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | _externalMedia = false; |
| 220 | _externalMediaCallbackPtr = NULL; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 225 | int OutputMixer::PlayDtmfTone(uint8_t eventCode, int lengthMs, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | int attenuationDb) |
| 227 | { |
| 228 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1), |
| 229 | "OutputMixer::PlayDtmfTone()"); |
| 230 | if (_dtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb) != 0) |
| 231 | { |
| 232 | _engineStatisticsPtr->SetLastError(VE_STILL_PLAYING_PREV_DTMF, |
| 233 | kTraceError, |
| 234 | "OutputMixer::PlayDtmfTone()"); |
| 235 | return -1; |
| 236 | } |
| 237 | return 0; |
| 238 | } |
| 239 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 240 | int OutputMixer::StartPlayingDtmfTone(uint8_t eventCode, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | int attenuationDb) |
| 242 | { |
| 243 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1), |
| 244 | "OutputMixer::StartPlayingDtmfTone()"); |
| 245 | if (_dtmfGenerator.StartTone(eventCode, attenuationDb) != 0) |
| 246 | { |
| 247 | _engineStatisticsPtr->SetLastError( |
| 248 | VE_STILL_PLAYING_PREV_DTMF, |
| 249 | kTraceError, |
| 250 | "OutputMixer::StartPlayingDtmfTone())"); |
| 251 | return -1; |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | int OutputMixer::StopPlayingDtmfTone() |
| 257 | { |
| 258 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, -1), |
| 259 | "OutputMixer::StopPlayingDtmfTone()"); |
| 260 | return (_dtmfGenerator.StopTone()); |
| 261 | } |
| 262 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 263 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | OutputMixer::SetMixabilityStatus(MixerParticipant& participant, |
| 265 | const bool mixable) |
| 266 | { |
| 267 | return _mixerModule.SetMixabilityStatus(participant, mixable); |
| 268 | } |
| 269 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 270 | int32_t |
henrike@webrtc.org | 066f9e5 | 2011-10-28 23:15:47 +0000 | [diff] [blame] | 271 | OutputMixer::SetAnonymousMixabilityStatus(MixerParticipant& participant, |
| 272 | const bool mixable) |
| 273 | { |
| 274 | return _mixerModule.SetAnonymousMixabilityStatus(participant,mixable); |
| 275 | } |
| 276 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 277 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | OutputMixer::MixActiveChannels() |
| 279 | { |
| 280 | return _mixerModule.Process(); |
| 281 | } |
| 282 | |
| 283 | int |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 284 | OutputMixer::GetSpeechOutputLevel(uint32_t& level) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 285 | { |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 286 | int8_t currentLevel = _audioLevel.Level(); |
| 287 | level = static_cast<uint32_t> (currentLevel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 289 | "GetSpeechOutputLevel() => level=%u", level); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | int |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 294 | OutputMixer::GetSpeechOutputLevelFullRange(uint32_t& level) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 295 | { |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 296 | int16_t currentLevel = _audioLevel.LevelFullRange(); |
| 297 | level = static_cast<uint32_t> (currentLevel); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 298 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 299 | "GetSpeechOutputLevelFullRange() => level=%u", level); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | int |
| 304 | OutputMixer::SetOutputVolumePan(float left, float right) |
| 305 | { |
| 306 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 307 | "OutputMixer::SetOutputVolumePan()"); |
| 308 | _panLeft = left; |
| 309 | _panRight = right; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int |
| 314 | OutputMixer::GetOutputVolumePan(float& left, float& right) |
| 315 | { |
| 316 | left = _panLeft; |
| 317 | right = _panRight; |
| 318 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 319 | "GetOutputVolumePan() => left=%2.1f, right=%2.1f", |
| 320 | left, right); |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | int OutputMixer::StartRecordingPlayout(const char* fileName, |
| 325 | const CodecInst* codecInst) |
| 326 | { |
| 327 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 328 | "OutputMixer::StartRecordingPlayout(fileName=%s)", fileName); |
| 329 | |
| 330 | if (_outputFileRecording) |
| 331 | { |
| 332 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1), |
| 333 | "StartRecordingPlayout() is already recording"); |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | FileFormats format; |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 338 | const uint32_t notificationTime(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 339 | CodecInst dummyCodec={100,"L16",16000,320,1,320000}; |
| 340 | |
niklas.enbom@webrtc.org | 40197d7 | 2012-03-26 08:45:47 +0000 | [diff] [blame] | 341 | if ((codecInst != NULL) && |
| 342 | ((codecInst->channels < 1) || (codecInst->channels > 2))) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 343 | { |
| 344 | _engineStatisticsPtr->SetLastError( |
| 345 | VE_BAD_ARGUMENT, kTraceError, |
| 346 | "StartRecordingPlayout() invalid compression"); |
| 347 | return(-1); |
| 348 | } |
| 349 | if(codecInst == NULL) |
| 350 | { |
| 351 | format = kFileFormatPcm16kHzFile; |
| 352 | codecInst=&dummyCodec; |
| 353 | } |
| 354 | else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) || |
| 355 | (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) || |
| 356 | (STR_CASE_CMP(codecInst->plname,"PCMA") == 0)) |
| 357 | { |
| 358 | format = kFileFormatWavFile; |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | format = kFileFormatCompressedFile; |
| 363 | } |
| 364 | |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 365 | CriticalSectionScoped cs(&_fileCritSect); |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 366 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | // Destroy the old instance |
| 368 | if (_outputFileRecorderPtr) |
| 369 | { |
| 370 | _outputFileRecorderPtr->RegisterModuleFileCallback(NULL); |
| 371 | FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr); |
| 372 | _outputFileRecorderPtr = NULL; |
| 373 | } |
| 374 | |
| 375 | _outputFileRecorderPtr = FileRecorder::CreateFileRecorder( |
| 376 | _instanceId, |
| 377 | (const FileFormats)format); |
| 378 | if (_outputFileRecorderPtr == NULL) |
| 379 | { |
| 380 | _engineStatisticsPtr->SetLastError( |
| 381 | VE_INVALID_ARGUMENT, kTraceError, |
| 382 | "StartRecordingPlayout() fileRecorder format isnot correct"); |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | if (_outputFileRecorderPtr->StartRecordingAudioFile( |
| 387 | fileName, |
| 388 | (const CodecInst&)*codecInst, |
| 389 | notificationTime) != 0) |
| 390 | { |
| 391 | _engineStatisticsPtr->SetLastError( |
| 392 | VE_BAD_FILE, kTraceError, |
| 393 | "StartRecordingAudioFile() failed to start file recording"); |
| 394 | _outputFileRecorderPtr->StopRecording(); |
| 395 | FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr); |
| 396 | _outputFileRecorderPtr = NULL; |
| 397 | return -1; |
| 398 | } |
| 399 | _outputFileRecorderPtr->RegisterModuleFileCallback(this); |
| 400 | _outputFileRecording = true; |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | int OutputMixer::StartRecordingPlayout(OutStream* stream, |
| 406 | const CodecInst* codecInst) |
| 407 | { |
| 408 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 409 | "OutputMixer::StartRecordingPlayout()"); |
| 410 | |
| 411 | if (_outputFileRecording) |
| 412 | { |
| 413 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1), |
| 414 | "StartRecordingPlayout() is already recording"); |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | FileFormats format; |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 419 | const uint32_t notificationTime(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 420 | CodecInst dummyCodec={100,"L16",16000,320,1,320000}; |
| 421 | |
| 422 | if (codecInst != NULL && codecInst->channels != 1) |
| 423 | { |
| 424 | _engineStatisticsPtr->SetLastError( |
| 425 | VE_BAD_ARGUMENT, kTraceError, |
| 426 | "StartRecordingPlayout() invalid compression"); |
| 427 | return(-1); |
| 428 | } |
| 429 | if(codecInst == NULL) |
| 430 | { |
| 431 | format = kFileFormatPcm16kHzFile; |
| 432 | codecInst=&dummyCodec; |
| 433 | } |
| 434 | else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) || |
| 435 | (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) || |
| 436 | (STR_CASE_CMP(codecInst->plname,"PCMA") == 0)) |
| 437 | { |
| 438 | format = kFileFormatWavFile; |
| 439 | } |
| 440 | else |
| 441 | { |
| 442 | format = kFileFormatCompressedFile; |
| 443 | } |
| 444 | |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 445 | CriticalSectionScoped cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 446 | |
| 447 | // Destroy the old instance |
| 448 | if (_outputFileRecorderPtr) |
| 449 | { |
| 450 | _outputFileRecorderPtr->RegisterModuleFileCallback(NULL); |
| 451 | FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr); |
| 452 | _outputFileRecorderPtr = NULL; |
| 453 | } |
| 454 | |
| 455 | _outputFileRecorderPtr = FileRecorder::CreateFileRecorder( |
| 456 | _instanceId, |
| 457 | (const FileFormats)format); |
| 458 | if (_outputFileRecorderPtr == NULL) |
| 459 | { |
| 460 | _engineStatisticsPtr->SetLastError( |
| 461 | VE_INVALID_ARGUMENT, kTraceError, |
| 462 | "StartRecordingPlayout() fileRecorder format isnot correct"); |
| 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, |
| 467 | *codecInst, |
| 468 | notificationTime) != 0) |
| 469 | { |
| 470 | _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError, |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 471 | "StartRecordingAudioFile() failed to start file recording"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 472 | _outputFileRecorderPtr->StopRecording(); |
| 473 | FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr); |
| 474 | _outputFileRecorderPtr = NULL; |
| 475 | return -1; |
| 476 | } |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 477 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | _outputFileRecorderPtr->RegisterModuleFileCallback(this); |
| 479 | _outputFileRecording = true; |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | int OutputMixer::StopRecordingPlayout() |
| 485 | { |
| 486 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 487 | "OutputMixer::StopRecordingPlayout()"); |
| 488 | |
| 489 | if (!_outputFileRecording) |
| 490 | { |
| 491 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1), |
| 492 | "StopRecordingPlayout() file isnot recording"); |
| 493 | return -1; |
| 494 | } |
| 495 | |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 496 | CriticalSectionScoped cs(&_fileCritSect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 497 | |
| 498 | if (_outputFileRecorderPtr->StopRecording() != 0) |
| 499 | { |
| 500 | _engineStatisticsPtr->SetLastError( |
| 501 | VE_STOP_RECORDING_FAILED, kTraceError, |
| 502 | "StopRecording(), could not stop recording"); |
| 503 | return -1; |
| 504 | } |
| 505 | _outputFileRecorderPtr->RegisterModuleFileCallback(NULL); |
| 506 | FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr); |
| 507 | _outputFileRecorderPtr = NULL; |
| 508 | _outputFileRecording = false; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 513 | int OutputMixer::GetMixedAudio(int sample_rate_hz, |
| 514 | int num_channels, |
| 515 | AudioFrame* frame) { |
| 516 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 517 | "OutputMixer::GetMixedAudio(sample_rate_hz=%d, num_channels=%d)", |
| 518 | sample_rate_hz, num_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 520 | // --- Record playout if enabled |
| 521 | { |
| 522 | CriticalSectionScoped cs(&_fileCritSect); |
| 523 | if (_outputFileRecording && _outputFileRecorderPtr) |
| 524 | _outputFileRecorderPtr->RecordAudioToFile(_audioFrame); |
| 525 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 526 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 527 | frame->num_channels_ = num_channels; |
| 528 | frame->sample_rate_hz_ = sample_rate_hz; |
| 529 | // TODO(andrew): Ideally the downmixing would occur much earlier, in |
| 530 | // AudioCodingModule. |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame^] | 531 | return RemixAndResample(_audioFrame, &resampler_, frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 532 | } |
| 533 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 534 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 535 | OutputMixer::DoOperationsOnCombinedSignal() |
| 536 | { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 537 | if (_audioFrame.sample_rate_hz_ != _mixingFrequencyHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 538 | { |
| 539 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 540 | "OutputMixer::DoOperationsOnCombinedSignal() => " |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 541 | "mixing frequency = %d", _audioFrame.sample_rate_hz_); |
| 542 | _mixingFrequencyHz = _audioFrame.sample_rate_hz_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // --- Insert inband Dtmf tone |
| 546 | if (_dtmfGenerator.IsAddingTone()) |
| 547 | { |
| 548 | InsertInbandDtmfTone(); |
| 549 | } |
| 550 | |
| 551 | // Scale left and/or right channel(s) if balance is active |
| 552 | if (_panLeft != 1.0 || _panRight != 1.0) |
| 553 | { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 554 | if (_audioFrame.num_channels_ == 1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 555 | { |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 556 | AudioFrameOperations::MonoToStereo(&_audioFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 557 | } |
| 558 | else |
| 559 | { |
| 560 | // Pure stereo mode (we are receiving a stereo signal). |
| 561 | } |
| 562 | |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 563 | assert(_audioFrame.num_channels_ == 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 564 | AudioFrameOperations::Scale(_panLeft, _panRight, _audioFrame); |
| 565 | } |
| 566 | |
| 567 | // --- Far-end Voice Quality Enhancement (AudioProcessing Module) |
| 568 | |
| 569 | APMAnalyzeReverseStream(); |
| 570 | |
| 571 | // --- External media processing |
| 572 | |
| 573 | if (_externalMedia) |
| 574 | { |
mflodman@webrtc.org | 9a065d1 | 2012-03-07 08:12:21 +0000 | [diff] [blame] | 575 | CriticalSectionScoped cs(&_callbackCritSect); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 576 | const bool isStereo = (_audioFrame.num_channels_ == 2); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 577 | if (_externalMediaCallbackPtr) |
| 578 | { |
| 579 | _externalMediaCallbackPtr->Process( |
| 580 | -1, |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 581 | kPlaybackAllChannelsMixed, |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 582 | (int16_t*)_audioFrame.data_, |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 583 | _audioFrame.samples_per_channel_, |
| 584 | _audioFrame.sample_rate_hz_, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 585 | isStereo); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | // --- Measure audio level (0-9) for the combined signal |
| 590 | _audioLevel.ComputeLevel(_audioFrame); |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | // ---------------------------------------------------------------------------- |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 596 | // Private methods |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 597 | // ---------------------------------------------------------------------------- |
| 598 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 599 | void OutputMixer::APMAnalyzeReverseStream() { |
| 600 | // Convert from mixing to AudioProcessing sample rate, determined by the send |
| 601 | // side. Downmix to mono. |
| 602 | AudioFrame frame; |
| 603 | frame.num_channels_ = 1; |
| 604 | frame.sample_rate_hz_ = _audioProcessingModulePtr->sample_rate_hz(); |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame^] | 605 | if (RemixAndResample(_audioFrame, &audioproc_resampler_, &frame) == -1) |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 606 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 607 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 608 | if (_audioProcessingModulePtr->AnalyzeReverseStream(&frame) == -1) { |
| 609 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1), |
| 610 | "AudioProcessingModule::AnalyzeReverseStream() => error"); |
| 611 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | int |
| 615 | OutputMixer::InsertInbandDtmfTone() |
| 616 | { |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 617 | uint16_t sampleRate(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 618 | _dtmfGenerator.GetSampleRate(sampleRate); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 619 | if (sampleRate != _audioFrame.sample_rate_hz_) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | { |
| 621 | // Update sample rate of Dtmf tone since the mixing frequency changed. |
| 622 | _dtmfGenerator.SetSampleRate( |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 623 | (uint16_t)(_audioFrame.sample_rate_hz_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 624 | // Reset the tone to be added taking the new sample rate into account. |
| 625 | _dtmfGenerator.ResetTone(); |
| 626 | } |
| 627 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 628 | int16_t toneBuffer[320]; |
| 629 | uint16_t toneSamples(0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 630 | if (_dtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1) |
| 631 | { |
| 632 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1), |
| 633 | "OutputMixer::InsertInbandDtmfTone() inserting Dtmf" |
| 634 | "tone failed"); |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | // replace mixed audio with Dtmf tone |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 639 | if (_audioFrame.num_channels_ == 1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 640 | { |
| 641 | // mono |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 642 | memcpy(_audioFrame.data_, toneBuffer, sizeof(int16_t) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 643 | * toneSamples); |
| 644 | } else |
| 645 | { |
| 646 | // stereo |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 647 | for (int i = 0; i < _audioFrame.samples_per_channel_; i++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 648 | { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 649 | _audioFrame.data_[2 * i] = toneBuffer[i]; |
| 650 | _audioFrame.data_[2 * i + 1] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 653 | assert(_audioFrame.samples_per_channel_ == toneSamples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | } // namespace voe |
| 659 | |
| 660 | } // namespace webrtc |