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 11 #ifndef MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ 12 #define MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ 13 14 #include <stddef.h> 15 16 #include <memory> 17 #include <vector> 18 19 #include "api/array_view.h" 20 #include "api/audio/audio_frame.h" 21 #include "api/audio/audio_mixer.h" 22 #include "api/scoped_refptr.h" 23 #include "modules/audio_mixer/frame_combiner.h" 24 #include "modules/audio_mixer/output_rate_calculator.h" 25 #include "rtc_base/race_checker.h" 26 #include "rtc_base/synchronization/mutex.h" 27 #include "rtc_base/thread_annotations.h" 28 29 namespace webrtc { 30 31 class AudioMixerImpl : public AudioMixer { 32 public: 33 struct SourceStatus; 34 35 // AudioProcessing only accepts 10 ms frames. 36 static const int kFrameDurationInMs = 10; 37 38 static const int kDefaultNumberOfMixedAudioSources = 3; 39 40 static rtc::scoped_refptr<AudioMixerImpl> Create( 41 int max_sources_to_mix = kDefaultNumberOfMixedAudioSources); 42 43 static rtc::scoped_refptr<AudioMixerImpl> Create( 44 std::unique_ptr<OutputRateCalculator> output_rate_calculator, 45 bool use_limiter, 46 int max_sources_to_mix = kDefaultNumberOfMixedAudioSources); 47 48 ~AudioMixerImpl() override; 49 50 AudioMixerImpl(const AudioMixerImpl&) = delete; 51 AudioMixerImpl& operator=(const AudioMixerImpl&) = delete; 52 53 // AudioMixer functions 54 bool AddSource(Source* audio_source) override; 55 void RemoveSource(Source* audio_source) override; 56 57 void Mix(size_t number_of_channels, 58 AudioFrame* audio_frame_for_mixing) override 59 RTC_LOCKS_EXCLUDED(mutex_); 60 61 // Returns true if the source was mixed last round. Returns 62 // false and logs an error if the source was never added to the 63 // mixer. 64 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const; 65 66 protected: 67 AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator, 68 bool use_limiter, 69 int max_sources_to_mix); 70 71 private: 72 struct HelperContainers; 73 74 // Compute what audio sources to mix from audio_source_list_. Ramp 75 // in and out. Update mixed status. Mixes up to 76 // kMaximumAmountOfMixedAudioSources audio sources. 77 rtc::ArrayView<AudioFrame* const> GetAudioFromSources(int output_frequency) 78 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 79 80 // The critical section lock guards audio source insertion and 81 // removal, which can be done from any thread. The race checker 82 // checks that mixing is done sequentially. 83 mutable Mutex mutex_; 84 85 const int max_sources_to_mix_; 86 87 std::unique_ptr<OutputRateCalculator> output_rate_calculator_; 88 89 // List of all audio sources. 90 std::vector<std::unique_ptr<SourceStatus>> audio_source_list_ 91 RTC_GUARDED_BY(mutex_); 92 const std::unique_ptr<HelperContainers> helper_containers_ 93 RTC_GUARDED_BY(mutex_); 94 95 // Component that handles actual adding of audio frames. 96 FrameCombiner frame_combiner_; 97 }; 98 } // namespace webrtc 99 100 #endif // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_ 101