• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/audio/audio_frame.h"
20 #include "api/audio/audio_mixer.h"
21 #include "api/scoped_refptr.h"
22 #include "modules/audio_mixer/frame_combiner.h"
23 #include "modules/audio_mixer/output_rate_calculator.h"
24 #include "rtc_base/constructor_magic.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 typedef std::vector<AudioFrame*> AudioFrameList;
32 
33 class AudioMixerImpl : public AudioMixer {
34  public:
35   struct SourceStatus {
SourceStatusSourceStatus36     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;
41 
42     // A frame that will be passed to audio_source->GetAudioFrameWithInfo.
43     AudioFrame audio_frame;
44   };
45 
46   using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>;
47 
48   // AudioProcessing only accepts 10 ms frames.
49   static const int kFrameDurationInMs = 10;
50   enum : int { kMaximumAmountOfMixedAudioSources = 3 };
51 
52   static rtc::scoped_refptr<AudioMixerImpl> Create();
53 
54   static rtc::scoped_refptr<AudioMixerImpl> Create(
55       std::unique_ptr<OutputRateCalculator> output_rate_calculator,
56       bool use_limiter);
57 
58   ~AudioMixerImpl() override;
59 
60   // AudioMixer functions
61   bool AddSource(Source* audio_source) override;
62   void RemoveSource(Source* audio_source) override;
63 
64   void Mix(size_t number_of_channels,
65            AudioFrame* audio_frame_for_mixing) override
66       RTC_LOCKS_EXCLUDED(mutex_);
67 
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.
71   bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const;
72 
73  protected:
74   AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator,
75                  bool use_limiter);
76 
77  private:
78   // Set mixing frequency through OutputFrequencyCalculator.
79   void CalculateOutputFrequency();
80   // Get mixing frequency.
81   int OutputFrequency() const;
82 
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.
86   AudioFrameList GetAudioFromSources() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
87 
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.
91   mutable Mutex mutex_;
92   rtc::RaceChecker race_checker_;
93 
94   std::unique_ptr<OutputRateCalculator> output_rate_calculator_;
95   // The current sample frequency and sample size when mixing.
96   int output_frequency_ RTC_GUARDED_BY(race_checker_);
97   size_t sample_size_ RTC_GUARDED_BY(race_checker_);
98 
99   // List of all audio sources. Note all lists are disjunct
100   SourceStatusList audio_source_list_ RTC_GUARDED_BY(mutex_);  // May be mixed.
101 
102   // Component that handles actual adding of audio frames.
103   FrameCombiner frame_combiner_ RTC_GUARDED_BY(race_checker_);
104 
105   RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
106 };
107 }  // namespace webrtc
108 
109 #endif  // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
110