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 | 201dfe9 | 2016-10-20 05:06:39 -0700 | [diff] [blame] | 17 | #include "webrtc/api/audio/audio_mixer.h" |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ref_ptr.h" |
aleloi | dc7669a | 2016-10-04 04:06:20 -0700 | [diff] [blame] | 19 | #include "webrtc/base/thread_annotations.h" |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 20 | #include "webrtc/base/race_checker.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" |
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 | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 44 | static const int kMaximumAmountOfMixedAudioSources = 3; |
aleloi | e97974d | 2016-10-12 03:06:09 -0700 | [diff] [blame] | 45 | static const int kDefaultFrequency = 48000; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 46 | |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 47 | static rtc::scoped_refptr<AudioMixerImpl> Create(); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 48 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 49 | ~AudioMixerImpl() override; |
aleloi | 70f866c | 2016-08-16 02:15:49 -0700 | [diff] [blame] | 50 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 51 | // AudioMixer functions |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 52 | bool AddSource(Source* audio_source) override; |
| 53 | bool RemoveSource(Source* audio_source) override; |
| 54 | |
| 55 | void Mix(int sample_rate_hz, |
aleloi | 4496809 | 2016-08-08 10:18:58 -0700 | [diff] [blame] | 56 | size_t number_of_channels, |
| 57 | AudioFrame* audio_frame_for_mixing) override; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 58 | |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 59 | // Returns true if the source was mixed last round. Returns |
| 60 | // false and logs an error if the source was never added to the |
| 61 | // mixer. |
aleloi | e97974d | 2016-10-12 03:06:09 -0700 | [diff] [blame] | 62 | bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const; |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 63 | |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 64 | protected: |
aleloi | e97974d | 2016-10-12 03:06:09 -0700 | [diff] [blame] | 65 | explicit AudioMixerImpl(std::unique_ptr<AudioProcessing> limiter); |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 66 | |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 67 | private: |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 68 | // Set/get mix frequency |
aleloi | e97974d | 2016-10-12 03:06:09 -0700 | [diff] [blame] | 69 | void SetOutputFrequency(int frequency); |
| 70 | int OutputFrequency() const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 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 | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 75 | AudioFrameList GetAudioFromSources() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 76 | |
| 77 | // Add/remove the MixerAudioSource to the specified |
| 78 | // MixerAudioSource list. |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 79 | bool AddAudioSourceToList(Source* audio_source, |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 80 | SourceStatusList* audio_source_list) const; |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 81 | bool RemoveAudioSourceFromList(Source* remove_audio_source, |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 82 | SourceStatusList* audio_source_list) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 83 | |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 84 | bool LimitMixedAudio(AudioFrame* mixed_audio) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 85 | |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 86 | // The critical section lock guards audio source insertion and |
| 87 | // removal, which can be done from any thread. The race checker |
| 88 | // checks that mixing is done sequentially. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 89 | rtc::CriticalSection crit_; |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 90 | rtc::RaceChecker race_checker_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 91 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 92 | // The current sample frequency and sample size when mixing. |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 93 | int output_frequency_ GUARDED_BY(race_checker_); |
| 94 | size_t sample_size_ GUARDED_BY(race_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 95 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 96 | // List of all audio sources. Note all lists are disjunct |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 97 | SourceStatusList audio_source_list_ GUARDED_BY(crit_); // May be mixed. |
aleloi | a0db81f | 2016-07-28 06:36:22 -0700 | [diff] [blame] | 98 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 99 | // Determines if we will use a limiter for clipping protection during |
| 100 | // mixing. |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 101 | bool use_limiter_ GUARDED_BY(race_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 102 | |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 103 | uint32_t time_stamp_ GUARDED_BY(race_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 104 | |
| 105 | // Used for inhibiting saturation in mixing. |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame^] | 106 | std::unique_ptr<AudioProcessing> limiter_ GUARDED_BY(race_checker_); |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 107 | |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 108 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 109 | }; |
| 110 | } // namespace webrtc |
| 111 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 112 | #endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |