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