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