Line data Source code
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 : #include <memory>
17 :
18 : #include "webrtc/modules/audio_conference_mixer/include/audio_conference_mixer.h"
19 : #include "webrtc/modules/audio_conference_mixer/source/memory_pool.h"
20 : #include "webrtc/modules/audio_conference_mixer/source/time_scheduler.h"
21 : #include "webrtc/modules/include/module_common_types.h"
22 : #include "webrtc/typedefs.h"
23 :
24 : namespace webrtc {
25 : class AudioProcessing;
26 : class CriticalSectionWrapper;
27 :
28 : struct FrameAndMuteInfo {
29 0 : FrameAndMuteInfo(AudioFrame* f, bool m) : frame(f), muted(m) {}
30 : AudioFrame* frame;
31 : bool muted;
32 : };
33 :
34 : typedef std::list<FrameAndMuteInfo> AudioFrameList;
35 : typedef std::list<MixerParticipant*> MixerParticipantList;
36 :
37 : // Cheshire cat implementation of MixerParticipant's non virtual functions.
38 : class MixHistory
39 : {
40 : public:
41 : MixHistory();
42 : ~MixHistory();
43 :
44 : // Returns true if the participant is being mixed.
45 : bool IsMixed() const;
46 :
47 : // Returns true if the participant was mixed previous mix
48 : // iteration.
49 : bool WasMixed() const;
50 :
51 : // Updates the mixed status.
52 : int32_t SetIsMixed(bool mixed);
53 :
54 : void ResetMixedStatus();
55 : private:
56 : bool _isMixed;
57 : };
58 :
59 : class AudioConferenceMixerImpl : public AudioConferenceMixer
60 : {
61 : public:
62 : // AudioProcessing only accepts 10 ms frames.
63 : enum {kProcessPeriodicityInMs = 10};
64 :
65 : AudioConferenceMixerImpl(int id);
66 : ~AudioConferenceMixerImpl();
67 :
68 : // Must be called after ctor.
69 : bool Init();
70 :
71 : // Module functions
72 : int64_t TimeUntilNextProcess() override;
73 : void Process() override;
74 :
75 : // AudioConferenceMixer functions
76 : int32_t RegisterMixedStreamCallback(
77 : AudioMixerOutputReceiver* mixReceiver) override;
78 : int32_t UnRegisterMixedStreamCallback() override;
79 : int32_t SetMixabilityStatus(MixerParticipant* participant,
80 : bool mixable) override;
81 : bool MixabilityStatus(const MixerParticipant& participant) const override;
82 : int32_t SetMinimumMixingFrequency(Frequency freq) override;
83 : int32_t SetAnonymousMixabilityStatus(
84 : MixerParticipant* participant, bool mixable) override;
85 : bool AnonymousMixabilityStatus(
86 : const MixerParticipant& participant) const override;
87 :
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.
97 : // maxAudioFrameCounter both input and output specifies how many more
98 : // AudioFrames that are allowed to be mixed.
99 : // rampOutList contain AudioFrames corresponding to an audio stream that
100 : // used to be mixed but shouldn't be mixed any longer. These AudioFrames
101 : // should be ramped out over this AudioFrame to avoid audio discontinuities.
102 : void UpdateToMix(
103 : AudioFrameList* mixList,
104 : AudioFrameList* rampOutList,
105 : std::map<int, MixerParticipant*>* mixParticipantList,
106 : size_t* maxAudioFrameCounter) const;
107 :
108 : // Return the lowest mixing frequency that can be used without having to
109 : // downsample any audio.
110 : int32_t GetLowestMixingFrequency() const;
111 : int32_t GetLowestMixingFrequencyFromList(
112 : const MixerParticipantList& mixList) const;
113 :
114 : // Return the AudioFrames that should be mixed anonymously.
115 : void GetAdditionalAudio(AudioFrameList* additionalFramesList) const;
116 :
117 : // Update the MixHistory of all MixerParticipants. mixedParticipantsList
118 : // should contain a map of MixerParticipants that have been mixed.
119 : void UpdateMixedStatus(
120 : const std::map<int, MixerParticipant*>& mixedParticipantsList) const;
121 :
122 : // Clears audioFrameList and reclaims all memory associated with it.
123 : void ClearAudioFrameList(AudioFrameList* audioFrameList) const;
124 :
125 : // This function returns true if it finds the MixerParticipant in the
126 : // specified list of MixerParticipants.
127 : bool IsParticipantInList(const MixerParticipant& participant,
128 : const MixerParticipantList& participantList) const;
129 :
130 : // Add/remove the MixerParticipant to the specified
131 : // MixerParticipant list.
132 : bool AddParticipantToList(
133 : MixerParticipant* participant,
134 : MixerParticipantList* participantList) const;
135 : bool RemoveParticipantFromList(
136 : MixerParticipant* removeParticipant,
137 : MixerParticipantList* participantList) const;
138 :
139 : // Mix the AudioFrames stored in audioFrameList into mixedAudio.
140 : int32_t MixFromList(AudioFrame* mixedAudio,
141 : const AudioFrameList& audioFrameList) const;
142 :
143 : // Mix the AudioFrames stored in audioFrameList into mixedAudio. No
144 : // record will be kept of this mix (e.g. the corresponding MixerParticipants
145 : // will not be marked as IsMixed()
146 : int32_t MixAnonomouslyFromList(AudioFrame* mixedAudio,
147 : const AudioFrameList& audioFrameList) const;
148 :
149 : bool LimitMixedAudio(AudioFrame* mixedAudio) const;
150 :
151 : std::unique_ptr<CriticalSectionWrapper> _crit;
152 : std::unique_ptr<CriticalSectionWrapper> _cbCrit;
153 :
154 : int32_t _id;
155 :
156 : Frequency _minimumMixingFreq;
157 :
158 : // Mix result callback
159 : AudioMixerOutputReceiver* _mixReceiver;
160 :
161 : // The current sample frequency and sample size when mixing.
162 : Frequency _outputFrequency;
163 : size_t _sampleSize;
164 :
165 : // Memory pool to avoid allocating/deallocating AudioFrames
166 : MemoryPool<AudioFrame>* _audioFramePool;
167 :
168 : // List of all participants. Note all lists are disjunct
169 : MixerParticipantList _participantList; // May be mixed.
170 : // Always mixed, anonomously.
171 : MixerParticipantList _additionalParticipantList;
172 :
173 : size_t _numMixedParticipants;
174 : // Determines if we will use a limiter for clipping protection during
175 : // mixing.
176 : bool use_limiter_;
177 :
178 : uint32_t _timeStamp;
179 :
180 : // Metronome class.
181 : TimeScheduler _timeScheduler;
182 :
183 : // Counter keeping track of concurrent calls to process.
184 : // Note: should never be higher than 1 or lower than 0.
185 : int16_t _processCalls;
186 :
187 : // Used for inhibiting saturation in mixing.
188 : std::unique_ptr<AudioProcessing> _limiter;
189 : };
190 : } // namespace webrtc
191 :
192 : #endif // WEBRTC_MODULES_AUDIO_CONFERENCE_MIXER_SOURCE_AUDIO_CONFERENCE_MIXER_IMPL_H_
|