aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 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 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 11 | #ifndef WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | #include <map> |
| 16 | #include <memory> |
aleloi | df9e4d9 | 2016-08-08 10:26:09 -0700 | [diff] [blame] | 17 | #include <vector> |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 18 | |
aleloi | 8b2233f | 2016-07-28 06:24:14 -0700 | [diff] [blame] | 19 | #include "webrtc/base/thread_checker.h" |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 20 | #include "webrtc/engine_configurations.h" |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 21 | #include "webrtc/modules/audio_mixer/audio_mixer.h" |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 22 | #include "webrtc/modules/include/module_common_types.h" |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 23 | #include "webrtc/voice_engine/level_indicator.h" |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | class AudioProcessing; |
| 27 | class CriticalSectionWrapper; |
| 28 | |
| 29 | struct FrameAndMuteInfo { |
| 30 | FrameAndMuteInfo(AudioFrame* f, bool m) : frame(f), muted(m) {} |
| 31 | AudioFrame* frame; |
| 32 | bool muted; |
| 33 | }; |
| 34 | |
| 35 | typedef std::list<FrameAndMuteInfo> AudioFrameList; |
| 36 | typedef std::list<MixerAudioSource*> MixerAudioSourceList; |
| 37 | |
| 38 | // Cheshire cat implementation of MixerAudioSource's non virtual functions. |
| 39 | class NewMixHistory { |
| 40 | public: |
| 41 | NewMixHistory(); |
| 42 | ~NewMixHistory(); |
| 43 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 44 | // Returns true if the audio source is being mixed. |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 45 | bool IsMixed() const; |
| 46 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 47 | // Returns true if the audio source was mixed previous mix |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 48 | // iteration. |
| 49 | bool WasMixed() const; |
| 50 | |
| 51 | // Updates the mixed status. |
| 52 | int32_t SetIsMixed(bool mixed); |
| 53 | |
| 54 | void ResetMixedStatus(); |
| 55 | |
| 56 | private: |
aleloi | 6382a19 | 2016-08-08 10:25:04 -0700 | [diff] [blame] | 57 | bool is_mixed_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 60 | class AudioMixerImpl : public AudioMixer { |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 61 | public: |
| 62 | // AudioProcessing only accepts 10 ms frames. |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 63 | static const int kFrameDurationInMs = 10; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 64 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 65 | explicit AudioMixerImpl(int id); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 66 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 67 | ~AudioMixerImpl() override; |
aleloi | 70f866c | 2016-08-16 02:15:49 -0700 | [diff] [blame] | 68 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 69 | // Must be called after ctor. |
| 70 | bool Init(); |
| 71 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 72 | // AudioMixer functions |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 73 | int32_t SetMixabilityStatus(MixerAudioSource* audio_source, |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 74 | bool mixable) override; |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 75 | bool MixabilityStatus(const MixerAudioSource& audio_source) const override; |
| 76 | int32_t SetAnonymousMixabilityStatus(MixerAudioSource* audio_source, |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 77 | bool mixable) override; |
aleloi | 4496809 | 2016-08-08 10:18:58 -0700 | [diff] [blame] | 78 | void Mix(int sample_rate, |
| 79 | size_t number_of_channels, |
| 80 | AudioFrame* audio_frame_for_mixing) override; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 81 | bool AnonymousMixabilityStatus( |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 82 | const MixerAudioSource& audio_source) const override; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 83 | |
| 84 | private: |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 85 | // Set/get mix frequency |
| 86 | int32_t SetOutputFrequency(const Frequency& frequency); |
| 87 | Frequency OutputFrequency() const; |
| 88 | |
aleloi | f388257 | 2016-07-29 02:12:41 -0700 | [diff] [blame] | 89 | // Compute what audio sources to mix from audio_source_list_. Ramp in |
| 90 | // and out. Update mixed status. maxAudioFrameCounter specifies how |
| 91 | // many participants are allowed to be mixed. |
| 92 | AudioFrameList UpdateToMix(size_t maxAudioFrameCounter) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 93 | |
| 94 | // Return the lowest mixing frequency that can be used without having to |
| 95 | // downsample any audio. |
| 96 | int32_t GetLowestMixingFrequency() const; |
| 97 | int32_t GetLowestMixingFrequencyFromList( |
| 98 | const MixerAudioSourceList& mixList) const; |
| 99 | |
| 100 | // Return the AudioFrames that should be mixed anonymously. |
| 101 | void GetAdditionalAudio(AudioFrameList* additionalFramesList) const; |
| 102 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 103 | // This function returns true if it finds the MixerAudioSource in the |
| 104 | // specified list of MixerAudioSources. |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 105 | bool IsAudioSourceInList(const MixerAudioSource& audio_source, |
| 106 | const MixerAudioSourceList& audioSourceList) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 107 | |
| 108 | // Add/remove the MixerAudioSource to the specified |
| 109 | // MixerAudioSource list. |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 110 | bool AddAudioSourceToList(MixerAudioSource* audio_source, |
| 111 | MixerAudioSourceList* audioSourceList) const; |
| 112 | bool RemoveAudioSourceFromList(MixerAudioSource* removeAudioSource, |
| 113 | MixerAudioSourceList* audioSourceList) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 114 | |
| 115 | // Mix the AudioFrames stored in audioFrameList into mixedAudio. |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 116 | static int32_t MixFromList(AudioFrame* mixedAudio, |
| 117 | const AudioFrameList& audioFrameList, |
| 118 | int32_t id, |
| 119 | bool use_limiter); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 120 | |
| 121 | // Mix the AudioFrames stored in audioFrameList into mixedAudio. No |
| 122 | // record will be kept of this mix (e.g. the corresponding MixerAudioSources |
| 123 | // will not be marked as IsMixed() |
| 124 | int32_t MixAnonomouslyFromList(AudioFrame* mixedAudio, |
| 125 | const AudioFrameList& audioFrameList) const; |
| 126 | |
| 127 | bool LimitMixedAudio(AudioFrame* mixedAudio) const; |
| 128 | |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 129 | // Output level functions for VoEVolumeControl. |
| 130 | int GetOutputAudioLevel() override; |
| 131 | |
| 132 | int GetOutputAudioLevelFullRange() override; |
| 133 | |
aleloi | 6382a19 | 2016-08-08 10:25:04 -0700 | [diff] [blame] | 134 | std::unique_ptr<CriticalSectionWrapper> crit_; |
| 135 | std::unique_ptr<CriticalSectionWrapper> cb_crit_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 136 | |
aleloi | 6382a19 | 2016-08-08 10:25:04 -0700 | [diff] [blame] | 137 | int32_t id_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 138 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 139 | // The current sample frequency and sample size when mixing. |
aleloi | 6382a19 | 2016-08-08 10:25:04 -0700 | [diff] [blame] | 140 | Frequency output_frequency_; |
| 141 | size_t sample_size_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 142 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 143 | // List of all audio sources. Note all lists are disjunct |
| 144 | MixerAudioSourceList audio_source_list_; // May be mixed. |
aleloi | a0db81f | 2016-07-28 06:36:22 -0700 | [diff] [blame] | 145 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 146 | // Always mixed, anonomously. |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 147 | MixerAudioSourceList additional_audio_source_list_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 148 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 149 | size_t num_mixed_audio_sources_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 150 | // Determines if we will use a limiter for clipping protection during |
| 151 | // mixing. |
| 152 | bool use_limiter_; |
| 153 | |
aleloi | 6382a19 | 2016-08-08 10:25:04 -0700 | [diff] [blame] | 154 | uint32_t time_stamp_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 155 | |
aleloi | 8b2233f | 2016-07-28 06:24:14 -0700 | [diff] [blame] | 156 | // Ensures that Mix is called from the same thread. |
| 157 | rtc::ThreadChecker thread_checker_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 158 | |
| 159 | // Used for inhibiting saturation in mixing. |
aleloi | 6382a19 | 2016-08-08 10:25:04 -0700 | [diff] [blame] | 160 | std::unique_ptr<AudioProcessing> limiter_; |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 161 | |
| 162 | // Measures audio level for the combined signal. |
| 163 | voe::AudioLevel audio_level_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 164 | }; |
| 165 | } // namespace webrtc |
| 166 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame^] | 167 | #endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |