blob: 8d1e31d35c54df4363a3ab56c12435350f235835 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "voice_engine/output_mixer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_processing/include/audio_processing.h"
14#include "rtc_base/format_macros.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "system_wrappers/include/trace.h"
16#include "voice_engine/statistics.h"
17#include "voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000020namespace voe {
21
22void
pbos@webrtc.org92135212013-05-14 08:31:39 +000023OutputMixer::NewMixedAudio(int32_t id,
niklase@google.com470e71d2011-07-07 08:21:25 +000024 const AudioFrame& generalAudioFrame,
25 const AudioFrame** uniqueAudioFrames,
pbos@webrtc.org92135212013-05-14 08:31:39 +000026 uint32_t size)
niklase@google.com470e71d2011-07-07 08:21:25 +000027{
28 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1),
29 "OutputMixer::NewMixedAudio(id=%d, size=%u)", id, size);
30
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000031 _audioFrame.CopyFrom(generalAudioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +000032 _audioFrame.id_ = id;
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
pbos@webrtc.org6141e132013-04-09 10:09:10 +000035int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +000036OutputMixer::Create(OutputMixer*& mixer, uint32_t instanceId)
niklase@google.com470e71d2011-07-07 08:21:25 +000037{
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.org92135212013-05-14 08:31:39 +000051OutputMixer::OutputMixer(uint32_t instanceId) :
andrew@webrtc.orgc4f129f2011-11-10 03:41:22 +000052 _mixerModule(*AudioConferenceMixer::Create(instanceId)),
xians@google.com22963ab2011-08-03 12:40:23 +000053 _instanceId(instanceId),
solenbergb63310a2017-09-18 03:04:12 -070054 _mixingFrequencyHz(8000)
niklase@google.com470e71d2011-07-07 08:21:25 +000055{
56 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,-1),
57 "OutputMixer::OutputMixer() - ctor");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000058
minyuel0f4b3732015-08-31 16:04:32 +020059 if (_mixerModule.RegisterMixedStreamCallback(this) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +000060 {
61 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
62 "OutputMixer::OutputMixer() failed to register mixer"
63 "callbacks");
64 }
niklase@google.com470e71d2011-07-07 08:21:25 +000065}
66
67void
68OutputMixer::Destroy(OutputMixer*& mixer)
69{
70 if (mixer)
71 {
72 delete mixer;
73 mixer = NULL;
74 }
75}
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000076
niklase@google.com470e71d2011-07-07 08:21:25 +000077OutputMixer::~OutputMixer()
78{
79 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,-1),
80 "OutputMixer::~OutputMixer() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +000081 _mixerModule.UnRegisterMixedStreamCallback();
82 delete &_mixerModule;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
pbos@webrtc.org6141e132013-04-09 10:09:10 +000085int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +000086OutputMixer::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.org6141e132013-04-09 10:09:10 +000094int32_t
95OutputMixer::SetAudioProcessingModule(AudioProcessing* audioProcessingModule)
niklase@google.com470e71d2011-07-07 08:21:25 +000096{
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.org6141e132013-04-09 10:09:10 +0000104int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000105OutputMixer::SetMixabilityStatus(MixerParticipant& participant,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000106 bool mixable)
niklase@google.com470e71d2011-07-07 08:21:25 +0000107{
minyuel0f4b3732015-08-31 16:04:32 +0200108 return _mixerModule.SetMixabilityStatus(&participant, mixable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000111int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000112OutputMixer::MixActiveChannels()
113{
pbosa26ac922016-02-25 04:50:01 -0800114 _mixerModule.Process();
115 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}
117
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000118int OutputMixer::GetMixedAudio(int sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800119 size_t num_channels,
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000120 AudioFrame* frame) {
Peter Kasting69558702016-01-12 16:26:35 -0800121 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.com470e71d2011-07-07 08:21:25 +0000125
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000126 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.org40ee3d02014-04-03 21:56:01 +0000130 RemixAndResample(_audioFrame, &resampler_, frame);
131 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000134int32_t
xians@webrtc.org56925312014-04-14 10:50:37 +0000135OutputMixer::DoOperationsOnCombinedSignal(bool feed_data_to_apm)
niklase@google.com470e71d2011-07-07 08:21:25 +0000136{
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000137 if (_audioFrame.sample_rate_hz_ != _mixingFrequencyHz)
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 {
139 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,-1),
140 "OutputMixer::DoOperationsOnCombinedSignal() => "
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000141 "mixing frequency = %d", _audioFrame.sample_rate_hz_);
142 _mixingFrequencyHz = _audioFrame.sample_rate_hz_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 }
144
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 // --- Far-end Voice Quality Enhancement (AudioProcessing Module)
peah66085be2015-12-16 02:02:20 -0800146 if (feed_data_to_apm) {
aluebsda116c42016-03-17 16:43:29 -0700147 if (_audioProcessingModulePtr->ProcessReverseStream(&_audioFrame) != 0) {
peah66085be2015-12-16 02:02:20 -0800148 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, -1),
aluebsb0319552016-03-17 20:39:53 -0700149 "AudioProcessingModule::ProcessReverseStream() => error");
nisseeb4ca4e2017-01-12 02:24:27 -0800150 RTC_NOTREACHED();
peah66085be2015-12-16 02:02:20 -0800151 }
152 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
niklase@google.com470e71d2011-07-07 08:21:25 +0000154 return 0;
155}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000156} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000157} // namespace webrtc