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