| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ | 12 #define WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "webrtc/api/audio/audio_mixer.h" | 17 #include "webrtc/api/audio/audio_mixer.h" |
| 18 #include "webrtc/base/scoped_ref_ptr.h" | 18 #include "webrtc/base/scoped_ref_ptr.h" |
| 19 #include "webrtc/base/thread_annotations.h" | 19 #include "webrtc/base/thread_annotations.h" |
| 20 #include "webrtc/base/race_checker.h" | 20 #include "webrtc/base/race_checker.h" |
| 21 #include "webrtc/modules/audio_mixer/output_rate_calculator.h" |
| 21 #include "webrtc/modules/audio_processing/include/audio_processing.h" | 22 #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 22 #include "webrtc/modules/include/module_common_types.h" | 23 #include "webrtc/modules/include/module_common_types.h" |
| 23 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 24 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 24 #include "webrtc/voice_engine_configurations.h" | 25 #include "webrtc/voice_engine_configurations.h" |
| 25 | 26 |
| 26 namespace webrtc { | 27 namespace webrtc { |
| 27 | 28 |
| 28 typedef std::vector<AudioFrame*> AudioFrameList; | 29 typedef std::vector<AudioFrame*> AudioFrameList; |
| 29 | 30 |
| 30 class AudioMixerImpl : public AudioMixer { | 31 class AudioMixerImpl : public AudioMixer { |
| 31 public: | 32 public: |
| 32 struct SourceStatus { | 33 struct SourceStatus { |
| 33 SourceStatus(Source* audio_source, bool is_mixed, float gain) | 34 SourceStatus(Source* audio_source, bool is_mixed, float gain) |
| 34 : audio_source(audio_source), is_mixed(is_mixed), gain(gain) {} | 35 : audio_source(audio_source), is_mixed(is_mixed), gain(gain) {} |
| 35 Source* audio_source = nullptr; | 36 Source* audio_source = nullptr; |
| 36 bool is_mixed = false; | 37 bool is_mixed = false; |
| 37 float gain = 0.0f; | 38 float gain = 0.0f; |
| 38 | 39 |
| 39 // A frame that will be passed to audio_source->GetAudioFrameWithInfo. | 40 // A frame that will be passed to audio_source->GetAudioFrameWithInfo. |
| 40 AudioFrame audio_frame; | 41 AudioFrame audio_frame; |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>; | 44 using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>; |
| 44 | 45 |
| 45 // AudioProcessing only accepts 10 ms frames. | 46 // AudioProcessing only accepts 10 ms frames. |
| 46 static const int kFrameDurationInMs = 10; | 47 static const int kFrameDurationInMs = 10; |
| 47 static const int kMaximumAmountOfMixedAudioSources = 3; | 48 static const int kMaximumAmountOfMixedAudioSources = 3; |
| 48 static const int kDefaultFrequency = 48000; | |
| 49 | 49 |
| 50 static rtc::scoped_refptr<AudioMixerImpl> Create(); | 50 static rtc::scoped_refptr<AudioMixerImpl> Create(); |
| 51 static rtc::scoped_refptr<AudioMixerImpl> CreateWithOutputRateCalculator( |
| 52 std::unique_ptr<OutputRateCalculator> output_rate_calculator); |
| 51 | 53 |
| 52 ~AudioMixerImpl() override; | 54 ~AudioMixerImpl() override; |
| 53 | 55 |
| 54 // AudioMixer functions | 56 // AudioMixer functions |
| 55 bool AddSource(Source* audio_source) override; | 57 bool AddSource(Source* audio_source) override; |
| 56 void RemoveSource(Source* audio_source) override; | 58 void RemoveSource(Source* audio_source) override; |
| 57 | 59 |
| 58 void Mix(size_t number_of_channels, | 60 void Mix(size_t number_of_channels, |
| 59 AudioFrame* audio_frame_for_mixing) override LOCKS_EXCLUDED(crit_); | 61 AudioFrame* audio_frame_for_mixing) override LOCKS_EXCLUDED(crit_); |
| 60 | 62 |
| 61 // Returns true if the source was mixed last round. Returns | 63 // Returns true if the source was mixed last round. Returns |
| 62 // false and logs an error if the source was never added to the | 64 // false and logs an error if the source was never added to the |
| 63 // mixer. | 65 // mixer. |
| 64 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const; | 66 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const; |
| 65 | 67 |
| 66 protected: | 68 protected: |
| 67 explicit AudioMixerImpl(std::unique_ptr<AudioProcessing> limiter); | 69 AudioMixerImpl(std::unique_ptr<AudioProcessing> limiter, |
| 70 std::unique_ptr<OutputRateCalculator> output_rate_calculator); |
| 68 | 71 |
| 69 private: | 72 private: |
| 70 // Set/get mix frequency | 73 // Set mixing frequency through OutputFrequencyCalculator. |
| 71 void SetOutputFrequency(int frequency); | 74 void CalculateOutputFrequency(); |
| 75 // Get mixing frequency. |
| 72 int OutputFrequency() const; | 76 int OutputFrequency() const; |
| 73 | 77 |
| 74 // Compute what audio sources to mix from audio_source_list_. Ramp | 78 // Compute what audio sources to mix from audio_source_list_. Ramp |
| 75 // in and out. Update mixed status. Mixes up to | 79 // in and out. Update mixed status. Mixes up to |
| 76 // kMaximumAmountOfMixedAudioSources audio sources. | 80 // kMaximumAmountOfMixedAudioSources audio sources. |
| 77 AudioFrameList GetAudioFromSources() EXCLUSIVE_LOCKS_REQUIRED(crit_); | 81 AudioFrameList GetAudioFromSources() EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 78 | 82 |
| 79 // Add/remove the MixerAudioSource to the specified | 83 // Add/remove the MixerAudioSource to the specified |
| 80 // MixerAudioSource list. | 84 // MixerAudioSource list. |
| 81 bool AddAudioSourceToList(Source* audio_source, | 85 bool AddAudioSourceToList(Source* audio_source, |
| 82 SourceStatusList* audio_source_list) const; | 86 SourceStatusList* audio_source_list) const; |
| 83 bool RemoveAudioSourceFromList(Source* remove_audio_source, | 87 bool RemoveAudioSourceFromList(Source* remove_audio_source, |
| 84 SourceStatusList* audio_source_list) const; | 88 SourceStatusList* audio_source_list) const; |
| 85 | 89 |
| 86 bool LimitMixedAudio(AudioFrame* mixed_audio) const; | 90 bool LimitMixedAudio(AudioFrame* mixed_audio) const; |
| 87 | 91 |
| 88 // The critical section lock guards audio source insertion and | 92 // The critical section lock guards audio source insertion and |
| 89 // removal, which can be done from any thread. The race checker | 93 // removal, which can be done from any thread. The race checker |
| 90 // checks that mixing is done sequentially. | 94 // checks that mixing is done sequentially. |
| 91 rtc::CriticalSection crit_; | 95 rtc::CriticalSection crit_; |
| 92 rtc::RaceChecker race_checker_; | 96 rtc::RaceChecker race_checker_; |
| 93 | 97 |
| 98 std::unique_ptr<OutputRateCalculator> output_rate_calculator_; |
| 94 // The current sample frequency and sample size when mixing. | 99 // The current sample frequency and sample size when mixing. |
| 95 int output_frequency_ GUARDED_BY(race_checker_); | 100 int output_frequency_ GUARDED_BY(race_checker_); |
| 96 size_t sample_size_ GUARDED_BY(race_checker_); | 101 size_t sample_size_ GUARDED_BY(race_checker_); |
| 97 | 102 |
| 98 // List of all audio sources. Note all lists are disjunct | 103 // List of all audio sources. Note all lists are disjunct |
| 99 SourceStatusList audio_source_list_ GUARDED_BY(crit_); // May be mixed. | 104 SourceStatusList audio_source_list_ GUARDED_BY(crit_); // May be mixed. |
| 100 | 105 |
| 101 // Determines if we will use a limiter for clipping protection during | 106 // Determines if we will use a limiter for clipping protection during |
| 102 // mixing. | 107 // mixing. |
| 103 bool use_limiter_ GUARDED_BY(race_checker_); | 108 bool use_limiter_ GUARDED_BY(race_checker_); |
| 104 | 109 |
| 105 uint32_t time_stamp_ GUARDED_BY(race_checker_); | 110 uint32_t time_stamp_ GUARDED_BY(race_checker_); |
| 106 | 111 |
| 107 // Used for inhibiting saturation in mixing. | 112 // Used for inhibiting saturation in mixing. |
| 108 std::unique_ptr<AudioProcessing> limiter_ GUARDED_BY(race_checker_); | 113 std::unique_ptr<AudioProcessing> limiter_ GUARDED_BY(race_checker_); |
| 109 | 114 |
| 110 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl); | 115 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl); |
| 111 }; | 116 }; |
| 112 } // namespace webrtc | 117 } // namespace webrtc |
| 113 | 118 |
| 114 #endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ | 119 #endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ |
| OLD | NEW |