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 | dc7669a | 2016-10-04 04:06:20 -0700 | [diff] [blame] | 18 | #include "webrtc/base/thread_annotations.h" |
aleloi | 8b2233f | 2016-07-28 06:24:14 -0700 | [diff] [blame] | 19 | #include "webrtc/base/thread_checker.h" |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/audio_mixer/audio_mixer.h" |
aleloi | dc7669a | 2016-10-04 04:06:20 -0700 | [diff] [blame] | 21 | #include "webrtc/modules/audio_mixer/audio_mixer_defines.h" |
| 22 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 23 | #include "webrtc/modules/include/module_common_types.h" |
aleloi | dc7669a | 2016-10-04 04:06:20 -0700 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 25 | #include "webrtc/voice_engine/level_indicator.h" |
mflodman | 7056be9 | 2016-10-07 07:07:28 +0200 | [diff] [blame^] | 26 | #include "webrtc/voice_engine_configurations.h" |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 29 | |
aleloi | 652ac89 | 2016-09-07 07:42:14 -0700 | [diff] [blame] | 30 | typedef std::vector<AudioFrame*> AudioFrameList; |
| 31 | typedef std::vector<MixerAudioSource*> MixerAudioSourceList; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 32 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 33 | class AudioMixerImpl : public AudioMixer { |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 34 | public: |
| 35 | // AudioProcessing only accepts 10 ms frames. |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 36 | static const int kFrameDurationInMs = 10; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 37 | |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 38 | static std::unique_ptr<AudioMixer> Create(int id); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 39 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 40 | ~AudioMixerImpl() override; |
aleloi | 70f866c | 2016-08-16 02:15:49 -0700 | [diff] [blame] | 41 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 42 | // AudioMixer functions |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 43 | int32_t SetMixabilityStatus(MixerAudioSource* audio_source, |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 44 | bool mixable) override; |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 45 | bool MixabilityStatus(const MixerAudioSource& audio_source) const override; |
| 46 | int32_t SetAnonymousMixabilityStatus(MixerAudioSource* audio_source, |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 47 | bool mixable) override; |
aleloi | 4496809 | 2016-08-08 10:18:58 -0700 | [diff] [blame] | 48 | void Mix(int sample_rate, |
| 49 | size_t number_of_channels, |
| 50 | AudioFrame* audio_frame_for_mixing) override; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 51 | bool AnonymousMixabilityStatus( |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 52 | const MixerAudioSource& audio_source) const override; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 55 | AudioMixerImpl(int id, std::unique_ptr<AudioProcessing> limiter); |
| 56 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 57 | // Set/get mix frequency |
| 58 | int32_t SetOutputFrequency(const Frequency& frequency); |
| 59 | Frequency OutputFrequency() const; |
| 60 | |
aleloi | 652ac89 | 2016-09-07 07:42:14 -0700 | [diff] [blame] | 61 | // Compute what audio sources to mix from audio_source_list_. Ramp |
| 62 | // in and out. Update mixed status. Mixes up to |
| 63 | // kMaximumAmountOfMixedAudioSources audio sources. |
| 64 | AudioFrameList GetNonAnonymousAudio() const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 65 | |
aleloi | 652ac89 | 2016-09-07 07:42:14 -0700 | [diff] [blame] | 66 | // Return the AudioFrames that should be mixed anonymously. Ramp in |
| 67 | // and out. Update mixed status. |
| 68 | AudioFrameList GetAnonymousAudio() const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 69 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 70 | // This function returns true if it finds the MixerAudioSource in the |
| 71 | // specified list of MixerAudioSources. |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 72 | bool IsAudioSourceInList(const MixerAudioSource& audio_source, |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 73 | const MixerAudioSourceList& audio_source_list) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 74 | |
| 75 | // Add/remove the MixerAudioSource to the specified |
| 76 | // MixerAudioSource list. |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 77 | bool AddAudioSourceToList(MixerAudioSource* audio_source, |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 78 | MixerAudioSourceList* audio_source_list) const; |
| 79 | bool RemoveAudioSourceFromList(MixerAudioSource* remove_audio_source, |
| 80 | MixerAudioSourceList* audio_source_list) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 81 | |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 82 | bool LimitMixedAudio(AudioFrame* mixed_audio) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 83 | |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 84 | // Output level functions for VoEVolumeControl. |
| 85 | int GetOutputAudioLevel() override; |
| 86 | |
| 87 | int GetOutputAudioLevelFullRange() override; |
| 88 | |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 89 | rtc::CriticalSection crit_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 90 | |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 91 | const int32_t id_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 92 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 93 | // The current sample frequency and sample size when mixing. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 94 | Frequency output_frequency_ ACCESS_ON(&thread_checker_); |
| 95 | size_t sample_size_ ACCESS_ON(&thread_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 96 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 97 | // List of all audio sources. Note all lists are disjunct |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 98 | MixerAudioSourceList audio_source_list_ GUARDED_BY(crit_); // May be mixed. |
aleloi | a0db81f | 2016-07-28 06:36:22 -0700 | [diff] [blame] | 99 | |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 100 | // Always mixed, anonymously. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 101 | MixerAudioSourceList additional_audio_source_list_ GUARDED_BY(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 102 | |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 103 | size_t num_mixed_audio_sources_ GUARDED_BY(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 104 | // Determines if we will use a limiter for clipping protection during |
| 105 | // mixing. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 106 | bool use_limiter_ ACCESS_ON(&thread_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 107 | |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 108 | uint32_t time_stamp_ ACCESS_ON(&thread_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 109 | |
aleloi | 8b2233f | 2016-07-28 06:24:14 -0700 | [diff] [blame] | 110 | // Ensures that Mix is called from the same thread. |
| 111 | rtc::ThreadChecker thread_checker_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 112 | |
| 113 | // Used for inhibiting saturation in mixing. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 114 | std::unique_ptr<AudioProcessing> limiter_ ACCESS_ON(&thread_checker_); |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 115 | |
| 116 | // Measures audio level for the combined signal. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 117 | voe::AudioLevel audio_level_ ACCESS_ON(&thread_checker_); |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 118 | |
| 119 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 120 | }; |
| 121 | } // namespace webrtc |
| 122 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 123 | #endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |