1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 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 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_ 13 14 #include <list> 15 #include <map> 16 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" 24 25 namespace webrtc { 26 class AudioProcessing; 27 class CriticalSectionWrapper; 28 29 typedef std::list<AudioFrame*> AudioFrameList; 30 typedef std::list<MixerParticipant*> MixerParticipantList; 31 32 // Cheshire cat implementation of MixerParticipant's non virtual functions. 33 class MixHistory 34 { 35 public: 36 MixHistory(); 37 ~MixHistory(); 38 39 // MixerParticipant function 40 int32_t IsMixed(bool& mixed) const; 41 42 // Sets wasMixed to true if the participant was mixed previous mix 43 // iteration. 44 int32_t WasMixed(bool& wasMixed) const; 45 46 // Updates the mixed status. 47 int32_t SetIsMixed(const bool mixed); 48 49 void ResetMixedStatus(); 50 private: 51 bool _isMixed; 52 }; 53 54 class AudioConferenceMixerImpl : public AudioConferenceMixer 55 { 56 public: 57 // AudioProcessing only accepts 10 ms frames. 58 enum {kProcessPeriodicityInMs = 10}; 59 60 AudioConferenceMixerImpl(int id); 61 ~AudioConferenceMixerImpl(); 62 63 // Must be called after ctor. 64 bool Init(); 65 66 // Module functions 67 virtual int32_t ChangeUniqueId(const int32_t id) OVERRIDE; 68 virtual int32_t TimeUntilNextProcess() OVERRIDE; 69 virtual int32_t Process() OVERRIDE; 70 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; 88 private: 89 enum{DEFAULT_AUDIO_FRAME_POOLSIZE = 50}; 90 91 // Set/get mix frequency 92 int32_t SetOutputFrequency(const Frequency frequency); 93 Frequency OutputFrequency() const; 94 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. 103 void UpdateToMix( 104 AudioFrameList* mixList, 105 AudioFrameList* rampOutList, 106 std::map<int, MixerParticipant*>* mixParticipantList, 107 size_t& maxAudioFrameCounter); 108 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); 113 114 // Return the AudioFrames that should be mixed anonymously. 115 void GetAdditionalAudio(AudioFrameList* additionalFramesList); 116 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); 121 122 // Clears audioFrameList and reclaims all memory associated with it. 123 void ClearAudioFrameList(AudioFrameList* audioFrameList); 124 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); 129 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; 135 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); 144 145 // Mix the AudioFrames stored in audioFrameList into mixedAudio. 146 int32_t MixFromList( 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); 154 155 bool LimitMixedAudio(AudioFrame& mixedAudio); 156 157 // Scratch memory 158 // Note that the scratch memory may only be touched in the scope of 159 // Process(). 160 size_t _scratchParticipantsToMixAmount; 161 ParticipantStatistics _scratchMixedParticipants[ 162 kMaximumAmountOfMixedParticipants]; 163 uint32_t _scratchVadPositiveParticipantsAmount; 164 ParticipantStatistics _scratchVadPositiveParticipants[ 165 kMaximumAmountOfMixedParticipants]; 166 167 scoped_ptr<CriticalSectionWrapper> _crit; 168 scoped_ptr<CriticalSectionWrapper> _cbCrit; 169 170 int32_t _id; 171 172 Frequency _minimumMixingFreq; 173 174 // Mix result callback 175 AudioMixerOutputReceiver* _mixReceiver; 176 177 AudioMixerStatusReceiver* _mixerStatusCallback; 178 uint32_t _amountOf10MsBetweenCallbacks; 179 uint32_t _amountOf10MsUntilNextCallback; 180 bool _mixerStatusCb; 181 182 // The current sample frequency and sample size when mixing. 183 Frequency _outputFrequency; 184 uint16_t _sampleSize; 185 186 // Memory pool to avoid allocating/deallocating AudioFrames 187 MemoryPool<AudioFrame>* _audioFramePool; 188 189 // List of all participants. Note all lists are disjunct 190 MixerParticipantList _participantList; // May be mixed. 191 // Always mixed, anonomously. 192 MixerParticipantList _additionalParticipantList; 193 194 size_t _numMixedParticipants; 195 // Determines if we will use a limiter for clipping protection during 196 // mixing. 197 bool use_limiter_; 198 199 uint32_t _timeStamp; 200 201 // Metronome class. 202 TimeScheduler _timeScheduler; 203 204 // Smooth level indicator. 205 LevelIndicator _mixedAudioLevel; 206 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; 210 211 // Used for inhibiting saturation in mixing. 212 scoped_ptr<AudioProcessing> _limiter; 213 }; 214 } // namespace webrtc 215 216 #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_AUDIO_CONFERENCE_MIXER_IMPL_H_ 217