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