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