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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |
| 12 | #define 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/audio/audio_mixer.h" |
| 18 | #include "modules/audio_mixer/frame_combiner.h" |
| 19 | #include "modules/audio_mixer/output_rate_calculator.h" |
| 20 | #include "modules/audio_processing/include/audio_processing.h" |
| 21 | #include "modules/include/module_common_types.h" |
| 22 | #include "rtc_base/race_checker.h" |
| 23 | #include "rtc_base/scoped_ref_ptr.h" |
| 24 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 25 | #include "typedefs.h" // NOLINT(build/include) |
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; |
aleloi | 6c27849 | 2016-10-20 14:24:39 -0700 | [diff] [blame] | 39 | |
| 40 | // A frame that will be passed to audio_source->GetAudioFrameWithInfo. |
| 41 | AudioFrame audio_frame; |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
aleloi | 6c27849 | 2016-10-20 14:24:39 -0700 | [diff] [blame] | 44 | using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>; |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 45 | |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 46 | // AudioProcessing only accepts 10 ms frames. |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 47 | static const int kFrameDurationInMs = 10; |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 48 | static const int kMaximumAmountOfMixedAudioSources = 3; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 49 | |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 50 | static rtc::scoped_refptr<AudioMixerImpl> Create(); |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 51 | |
aleloi | 087613c | 2017-02-21 08:27:08 -0800 | [diff] [blame] | 52 | static rtc::scoped_refptr<AudioMixerImpl> Create( |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 53 | std::unique_ptr<OutputRateCalculator> output_rate_calculator, |
| 54 | bool use_limiter); |
| 55 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 56 | ~AudioMixerImpl() override; |
aleloi | 70f866c | 2016-08-16 02:15:49 -0700 | [diff] [blame] | 57 | |
aleloi | 5d167d6 | 2016-08-24 02:20:54 -0700 | [diff] [blame] | 58 | // AudioMixer functions |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 59 | bool AddSource(Source* audio_source) override; |
aleloi | 76b3049 | 2016-11-18 02:03:04 -0800 | [diff] [blame] | 60 | void RemoveSource(Source* audio_source) override; |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 61 | |
aleloi | 9561183 | 2016-11-08 06:39:50 -0800 | [diff] [blame] | 62 | void Mix(size_t number_of_channels, |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 63 | AudioFrame* audio_frame_for_mixing) override |
| 64 | RTC_LOCKS_EXCLUDED(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 65 | |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 66 | // Returns true if the source was mixed last round. Returns |
| 67 | // false and logs an error if the source was never added to the |
| 68 | // mixer. |
aleloi | e97974d | 2016-10-12 03:06:09 -0700 | [diff] [blame] | 69 | bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const; |
aleloi | 3654251 | 2016-10-07 05:28:32 -0700 | [diff] [blame] | 70 | |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 71 | protected: |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 72 | AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator, |
| 73 | bool use_limiter); |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 74 | |
aleloi | 116ec6d | 2016-10-12 06:07:07 -0700 | [diff] [blame] | 75 | private: |
aleloi | 623427c | 2016-12-08 02:37:58 -0800 | [diff] [blame] | 76 | // Set mixing frequency through OutputFrequencyCalculator. |
| 77 | void CalculateOutputFrequency(); |
| 78 | // Get mixing frequency. |
aleloi | e97974d | 2016-10-12 03:06:09 -0700 | [diff] [blame] | 79 | int OutputFrequency() const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 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. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 84 | AudioFrameList GetAudioFromSources() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 85 | |
| 86 | // Add/remove the MixerAudioSource to the specified |
| 87 | // MixerAudioSource list. |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 88 | bool AddAudioSourceToList(Source* audio_source, |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 89 | SourceStatusList* audio_source_list) const; |
aleloi | e891415 | 2016-10-11 06:18:31 -0700 | [diff] [blame] | 90 | bool RemoveAudioSourceFromList(Source* remove_audio_source, |
aleloi | 4b8bfb8 | 2016-10-12 02:14:59 -0700 | [diff] [blame] | 91 | SourceStatusList* audio_source_list) const; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 92 | |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame] | 93 | // The critical section lock guards audio source insertion and |
| 94 | // removal, which can be done from any thread. The race checker |
| 95 | // checks that mixing is done sequentially. |
aleloi | 311525e | 2016-09-07 06:13:12 -0700 | [diff] [blame] | 96 | rtc::CriticalSection crit_; |
aleloi | 920d30b | 2016-10-20 14:23:24 -0700 | [diff] [blame] | 97 | rtc::RaceChecker race_checker_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 98 | |
aleloi | 623427c | 2016-12-08 02:37:58 -0800 | [diff] [blame] | 99 | std::unique_ptr<OutputRateCalculator> output_rate_calculator_; |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 100 | // The current sample frequency and sample size when mixing. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 101 | int output_frequency_ RTC_GUARDED_BY(race_checker_); |
| 102 | size_t sample_size_ RTC_GUARDED_BY(race_checker_); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 103 | |
aleloi | 09f4510 | 2016-07-28 03:52:15 -0700 | [diff] [blame] | 104 | // List of all audio sources. Note all lists are disjunct |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 105 | SourceStatusList audio_source_list_ RTC_GUARDED_BY(crit_); // May be mixed. |
aleloi | a0db81f | 2016-07-28 06:36:22 -0700 | [diff] [blame] | 106 | |
aleloi | 24899e5 | 2017-02-21 05:06:29 -0800 | [diff] [blame] | 107 | // Component that handles actual adding of audio frames. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 108 | FrameCombiner frame_combiner_ RTC_GUARDED_BY(race_checker_); |
aleloi | 616df1e | 2016-08-24 01:17:12 -0700 | [diff] [blame] | 109 | |
aleloi | a4c2106 | 2016-09-08 01:25:46 -0700 | [diff] [blame] | 110 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl); |
aleloi | 77ad394 | 2016-07-04 06:33:02 -0700 | [diff] [blame] | 111 | }; |
| 112 | } // namespace webrtc |
| 113 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 114 | #endif // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |