2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_AUDIO_CONFERENCE_MIXER_IMPL_H_
12 #define WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_AUDIO_CONFERENCE_MIXER_IMPL_H_
17 #include "webrtc/engine_configurations.h"
18 #include "webrtc/modules/audio_conference_mixer/interface/audio_conference_mixer.h"
19 #include "webrtc/modules/audio_conference_mixer/source/level_indicator.h"
20 #include "webrtc/modules/audio_conference_mixer/source/memory_pool.h"
21 #include "webrtc/modules/audio_conference_mixer/source/time_scheduler.h"
22 #include "webrtc/modules/interface/module_common_types.h"
23 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
26 class AudioProcessing;
27 class CriticalSectionWrapper;
29 typedef std::list<AudioFrame*> AudioFrameList;
30 typedef std::list<MixerParticipant*> MixerParticipantList;
32 // Cheshire cat implementation of MixerParticipant's non virtual functions.
39 // MixerParticipant function
40 int32_t IsMixed(bool& mixed) const;
42 // Sets wasMixed to true if the participant was mixed previous mix
44 int32_t WasMixed(bool& wasMixed) const;
46 // Updates the mixed status.
47 int32_t SetIsMixed(const bool mixed);
49 void ResetMixedStatus();
54 class AudioConferenceMixerImpl : public AudioConferenceMixer
57 // AudioProcessing only accepts 10 ms frames.
58 enum {kProcessPeriodicityInMs = 10};
60 AudioConferenceMixerImpl(int id);
61 ~AudioConferenceMixerImpl();
63 // Must be called after ctor.
67 virtual int32_t ChangeUniqueId(const int32_t id) OVERRIDE;
68 virtual int32_t TimeUntilNextProcess() OVERRIDE;
69 virtual int32_t Process() OVERRIDE;
71 // AudioConferenceMixer functions
72 virtual int32_t RegisterMixedStreamCallback(
73 AudioMixerOutputReceiver& mixReceiver) OVERRIDE;
74 virtual int32_t UnRegisterMixedStreamCallback() OVERRIDE;
75 virtual int32_t RegisterMixerStatusCallback(
76 AudioMixerStatusReceiver& mixerStatusCallback,
77 const uint32_t amountOf10MsBetweenCallbacks) OVERRIDE;
78 virtual int32_t UnRegisterMixerStatusCallback() OVERRIDE;
79 virtual int32_t SetMixabilityStatus(MixerParticipant& participant,
80 bool mixable) OVERRIDE;
81 virtual int32_t MixabilityStatus(MixerParticipant& participant,
82 bool& mixable) OVERRIDE;
83 virtual int32_t SetMinimumMixingFrequency(Frequency freq) OVERRIDE;
84 virtual int32_t SetAnonymousMixabilityStatus(
85 MixerParticipant& participant, const bool mixable) OVERRIDE;
86 virtual int32_t AnonymousMixabilityStatus(
87 MixerParticipant& participant, bool& mixable) OVERRIDE;
89 enum{DEFAULT_AUDIO_FRAME_POOLSIZE = 50};
91 // Set/get mix frequency
92 int32_t SetOutputFrequency(const Frequency frequency);
93 Frequency OutputFrequency() const;
95 // Fills mixList with the AudioFrames pointers that should be used when
96 // mixing. Fills mixParticipantList with ParticipantStatistics for the
97 // participants who's AudioFrames are inside mixList.
98 // maxAudioFrameCounter both input and output specifies how many more
99 // AudioFrames that are allowed to be mixed.
100 // rampOutList contain AudioFrames corresponding to an audio stream that
101 // used to be mixed but shouldn't be mixed any longer. These AudioFrames
102 // should be ramped out over this AudioFrame to avoid audio discontinuities.
104 AudioFrameList* mixList,
105 AudioFrameList* rampOutList,
106 std::map<int, MixerParticipant*>* mixParticipantList,
107 size_t& maxAudioFrameCounter);
109 // Return the lowest mixing frequency that can be used without having to
110 // downsample any audio.
111 int32_t GetLowestMixingFrequency();
112 int32_t GetLowestMixingFrequencyFromList(MixerParticipantList* mixList);
114 // Return the AudioFrames that should be mixed anonymously.
115 void GetAdditionalAudio(AudioFrameList* additionalFramesList);
117 // Update the MixHistory of all MixerParticipants. mixedParticipantsList
118 // should contain a map of MixerParticipants that have been mixed.
119 void UpdateMixedStatus(
120 std::map<int, MixerParticipant*>& mixedParticipantsList);
122 // Clears audioFrameList and reclaims all memory associated with it.
123 void ClearAudioFrameList(AudioFrameList* audioFrameList);
125 // Update the list of MixerParticipants who have a positive VAD. mixList
126 // should be a list of AudioFrames
127 void UpdateVADPositiveParticipants(
128 AudioFrameList* mixList);
130 // This function returns true if it finds the MixerParticipant in the
131 // specified list of MixerParticipants.
132 bool IsParticipantInList(
133 MixerParticipant& participant,
134 MixerParticipantList* participantList) const;
136 // Add/remove the MixerParticipant to the specified
137 // MixerParticipant list.
138 bool AddParticipantToList(
139 MixerParticipant& participant,
140 MixerParticipantList* participantList);
141 bool RemoveParticipantFromList(
142 MixerParticipant& removeParticipant,
143 MixerParticipantList* participantList);
145 // Mix the AudioFrames stored in audioFrameList into mixedAudio.
147 AudioFrame& mixedAudio,
148 const AudioFrameList* audioFrameList);
149 // Mix the AudioFrames stored in audioFrameList into mixedAudio. No
150 // record will be kept of this mix (e.g. the corresponding MixerParticipants
151 // will not be marked as IsMixed()
152 int32_t MixAnonomouslyFromList(AudioFrame& mixedAudio,
153 const AudioFrameList* audioFrameList);
155 bool LimitMixedAudio(AudioFrame& mixedAudio);
158 // Note that the scratch memory may only be touched in the scope of
160 size_t _scratchParticipantsToMixAmount;
161 ParticipantStatistics _scratchMixedParticipants[
162 kMaximumAmountOfMixedParticipants];
163 uint32_t _scratchVadPositiveParticipantsAmount;
164 ParticipantStatistics _scratchVadPositiveParticipants[
165 kMaximumAmountOfMixedParticipants];
167 scoped_ptr<CriticalSectionWrapper> _crit;
168 scoped_ptr<CriticalSectionWrapper> _cbCrit;
172 Frequency _minimumMixingFreq;
174 // Mix result callback
175 AudioMixerOutputReceiver* _mixReceiver;
177 AudioMixerStatusReceiver* _mixerStatusCallback;
178 uint32_t _amountOf10MsBetweenCallbacks;
179 uint32_t _amountOf10MsUntilNextCallback;
182 // The current sample frequency and sample size when mixing.
183 Frequency _outputFrequency;
184 uint16_t _sampleSize;
186 // Memory pool to avoid allocating/deallocating AudioFrames
187 MemoryPool<AudioFrame>* _audioFramePool;
189 // List of all participants. Note all lists are disjunct
190 MixerParticipantList _participantList; // May be mixed.
191 // Always mixed, anonomously.
192 MixerParticipantList _additionalParticipantList;
194 size_t _numMixedParticipants;
195 // Determines if we will use a limiter for clipping protection during
202 TimeScheduler _timeScheduler;
204 // Smooth level indicator.
205 LevelIndicator _mixedAudioLevel;
207 // Counter keeping track of concurrent calls to process.
208 // Note: should never be higher than 1 or lower than 0.
209 int16_t _processCalls;
211 // Used for inhibiting saturation in mixing.
212 scoped_ptr<AudioProcessing> _limiter;
214 } // namespace webrtc
216 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_AUDIO_CONFERENCE_MIXER_IMPL_H_