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