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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "voice_engine/output_mixer.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/include/audio_processing.h" |
| 14 | #include "rtc_base/format_macros.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "system_wrappers/include/trace.h" |
| 16 | #include "voice_engine/statistics.h" |
| 17 | #include "voice_engine/utility.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | namespace voe { |
| 21 | |
| 22 | void |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 23 | OutputMixer::NewMixedAudio(int32_t id, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | const AudioFrame& generalAudioFrame, |
| 25 | const AudioFrame** uniqueAudioFrames, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 26 | uint32_t size) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | { |
| 28 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 29 | "OutputMixer::NewMixedAudio(id=%d, size=%u)", id, size); |
| 30 | |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 31 | _audioFrame.CopyFrom(generalAudioFrame); |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 32 | _audioFrame.id_ = id; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 35 | int32_t |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 36 | OutputMixer::Create(OutputMixer*& mixer, uint32_t instanceId) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | { |
| 38 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, instanceId, |
| 39 | "OutputMixer::Create(instanceId=%d)", instanceId); |
| 40 | mixer = new OutputMixer(instanceId); |
| 41 | if (mixer == NULL) |
| 42 | { |
| 43 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, instanceId, |
| 44 | "OutputMixer::Create() unable to allocate memory for" |
| 45 | "mixer"); |
| 46 | return -1; |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 51 | OutputMixer::OutputMixer(uint32_t instanceId) : |
andrew@webrtc.org | c4f129f | 2011-11-10 03:41:22 +0000 | [diff] [blame] | 52 | _mixerModule(*AudioConferenceMixer::Create(instanceId)), |
xians@google.com | 22963ab | 2011-08-03 12:40:23 +0000 | [diff] [blame] | 53 | _instanceId(instanceId), |
solenberg | b63310a | 2017-09-18 03:04:12 -0700 | [diff] [blame] | 54 | _mixingFrequencyHz(8000) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | { |
| 56 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,-1), |
| 57 | "OutputMixer::OutputMixer() - ctor"); |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 58 | |
minyuel | 0f4b373 | 2015-08-31 16:04:32 +0200 | [diff] [blame] | 59 | if (_mixerModule.RegisterMixedStreamCallback(this) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | { |
| 61 | WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1), |
| 62 | "OutputMixer::OutputMixer() failed to register mixer" |
| 63 | "callbacks"); |
| 64 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
| 68 | OutputMixer::Destroy(OutputMixer*& mixer) |
| 69 | { |
| 70 | if (mixer) |
| 71 | { |
| 72 | delete mixer; |
| 73 | mixer = NULL; |
| 74 | } |
| 75 | } |
andrew@webrtc.org | ae1a58b | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 76 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | OutputMixer::~OutputMixer() |
| 78 | { |
| 79 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,-1), |
| 80 | "OutputMixer::~OutputMixer() - dtor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | _mixerModule.UnRegisterMixedStreamCallback(); |
| 82 | delete &_mixerModule; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 85 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | OutputMixer::SetEngineInformation(voe::Statistics& engineStatistics) |
| 87 | { |
| 88 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 89 | "OutputMixer::SetEngineInformation()"); |
| 90 | _engineStatisticsPtr = &engineStatistics; |
| 91 | return 0; |
| 92 | } |
| 93 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 94 | int32_t |
| 95 | OutputMixer::SetAudioProcessingModule(AudioProcessing* audioProcessingModule) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | { |
| 97 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1), |
| 98 | "OutputMixer::SetAudioProcessingModule(" |
| 99 | "audioProcessingModule=0x%x)", audioProcessingModule); |
| 100 | _audioProcessingModulePtr = audioProcessingModule; |
| 101 | return 0; |
| 102 | } |
| 103 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 104 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | OutputMixer::SetMixabilityStatus(MixerParticipant& participant, |
pbos@webrtc.org | 9213521 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 106 | bool mixable) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | { |
minyuel | 0f4b373 | 2015-08-31 16:04:32 +0200 | [diff] [blame] | 108 | return _mixerModule.SetMixabilityStatus(&participant, mixable); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | } |
| 110 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 111 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | OutputMixer::MixActiveChannels() |
| 113 | { |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 114 | _mixerModule.Process(); |
| 115 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | } |
| 117 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 118 | int OutputMixer::GetMixedAudio(int sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 119 | size_t num_channels, |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 120 | AudioFrame* frame) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 121 | WEBRTC_TRACE( |
| 122 | kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 123 | "OutputMixer::GetMixedAudio(sample_rate_hz=%d, num_channels=%" PRIuS ")", |
| 124 | sample_rate_hz, num_channels); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
andrew@webrtc.org | 4ecea3e | 2012-06-27 03:25:31 +0000 | [diff] [blame] | 126 | frame->num_channels_ = num_channels; |
| 127 | frame->sample_rate_hz_ = sample_rate_hz; |
| 128 | // TODO(andrew): Ideally the downmixing would occur much earlier, in |
| 129 | // AudioCodingModule. |
andrew@webrtc.org | 40ee3d0 | 2014-04-03 21:56:01 +0000 | [diff] [blame] | 130 | RemixAndResample(_audioFrame, &resampler_, frame); |
| 131 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
pbos@webrtc.org | 6141e13 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 134 | int32_t |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 135 | OutputMixer::DoOperationsOnCombinedSignal(bool feed_data_to_apm) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 137 | if (_audioFrame.sample_rate_hz_ != _mixingFrequencyHz) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | { |
| 139 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1), |
| 140 | "OutputMixer::DoOperationsOnCombinedSignal() => " |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 141 | "mixing frequency = %d", _audioFrame.sample_rate_hz_); |
| 142 | _mixingFrequencyHz = _audioFrame.sample_rate_hz_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | // --- Far-end Voice Quality Enhancement (AudioProcessing Module) |
peah | 66085be | 2015-12-16 02:02:20 -0800 | [diff] [blame] | 146 | if (feed_data_to_apm) { |
aluebs | da116c4 | 2016-03-17 16:43:29 -0700 | [diff] [blame] | 147 | if (_audioProcessingModulePtr->ProcessReverseStream(&_audioFrame) != 0) { |
peah | 66085be | 2015-12-16 02:02:20 -0800 | [diff] [blame] | 148 | WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1), |
aluebs | b031955 | 2016-03-17 20:39:53 -0700 | [diff] [blame] | 149 | "AudioProcessingModule::ProcessReverseStream() => error"); |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 150 | RTC_NOTREACHED(); |
peah | 66085be | 2015-12-16 02:02:20 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 156 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 157 | } // namespace webrtc |