blob: 8d49e913e488e41b41a44495dfb68fb26c539a11 [file] [log] [blame]
aleloi77ad3942016-07-04 06:33:02 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
12#define MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
aleloi77ad3942016-07-04 06:33:02 -070013
aleloi77ad3942016-07-04 06:33:02 -070014#include <memory>
aleloidf9e4d92016-08-08 10:26:09 -070015#include <vector>
aleloi77ad3942016-07-04 06:33:02 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio/audio_mixer.h"
18#include "modules/audio_mixer/frame_combiner.h"
19#include "modules/audio_mixer/output_rate_calculator.h"
20#include "modules/audio_processing/include/audio_processing.h"
21#include "modules/include/module_common_types.h"
22#include "rtc_base/race_checker.h"
23#include "rtc_base/scoped_ref_ptr.h"
24#include "rtc_base/thread_annotations.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "typedefs.h" // NOLINT(build/include)
aleloi77ad3942016-07-04 06:33:02 -070026
27namespace webrtc {
aleloi77ad3942016-07-04 06:33:02 -070028
aleloi652ac892016-09-07 07:42:14 -070029typedef std::vector<AudioFrame*> AudioFrameList;
aleloi77ad3942016-07-04 06:33:02 -070030
aleloi5d167d62016-08-24 02:20:54 -070031class AudioMixerImpl : public AudioMixer {
aleloi77ad3942016-07-04 06:33:02 -070032 public:
aleloi4b8bfb82016-10-12 02:14:59 -070033 struct SourceStatus {
34 SourceStatus(Source* audio_source, bool is_mixed, float gain)
35 : 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;
aleloi6c278492016-10-20 14:24:39 -070039
40 // A frame that will be passed to audio_source->GetAudioFrameWithInfo.
41 AudioFrame audio_frame;
aleloi4b8bfb82016-10-12 02:14:59 -070042 };
43
aleloi6c278492016-10-20 14:24:39 -070044 using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>;
aleloi4b8bfb82016-10-12 02:14:59 -070045
aleloi77ad3942016-07-04 06:33:02 -070046 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070047 static const int kFrameDurationInMs = 10;
aleloi116ec6d2016-10-12 06:07:07 -070048 static const int kMaximumAmountOfMixedAudioSources = 3;
aleloi77ad3942016-07-04 06:33:02 -070049
aleloi116ec6d2016-10-12 06:07:07 -070050 static rtc::scoped_refptr<AudioMixerImpl> Create();
aleloi24899e52017-02-21 05:06:29 -080051
aleloi087613c2017-02-21 08:27:08 -080052 static rtc::scoped_refptr<AudioMixerImpl> Create(
aleloi24899e52017-02-21 05:06:29 -080053 std::unique_ptr<OutputRateCalculator> output_rate_calculator,
54 bool use_limiter);
55
aleloi5d167d62016-08-24 02:20:54 -070056 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070057
aleloi5d167d62016-08-24 02:20:54 -070058 // AudioMixer functions
aleloi116ec6d2016-10-12 06:07:07 -070059 bool AddSource(Source* audio_source) override;
aleloi76b30492016-11-18 02:03:04 -080060 void RemoveSource(Source* audio_source) override;
aleloi116ec6d2016-10-12 06:07:07 -070061
aleloi95611832016-11-08 06:39:50 -080062 void Mix(size_t number_of_channels,
danilchap56359be2017-09-07 07:53:45 -070063 AudioFrame* audio_frame_for_mixing) override
64 RTC_LOCKS_EXCLUDED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070065
aleloi36542512016-10-07 05:28:32 -070066 // Returns true if the source was mixed last round. Returns
67 // false and logs an error if the source was never added to the
68 // mixer.
aleloie97974d2016-10-12 03:06:09 -070069 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const;
aleloi36542512016-10-07 05:28:32 -070070
aleloi116ec6d2016-10-12 06:07:07 -070071 protected:
aleloi24899e52017-02-21 05:06:29 -080072 AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator,
73 bool use_limiter);
aleloi311525e2016-09-07 06:13:12 -070074
aleloi116ec6d2016-10-12 06:07:07 -070075 private:
aleloi623427c2016-12-08 02:37:58 -080076 // Set mixing frequency through OutputFrequencyCalculator.
77 void CalculateOutputFrequency();
78 // Get mixing frequency.
aleloie97974d2016-10-12 03:06:09 -070079 int OutputFrequency() const;
aleloi77ad3942016-07-04 06:33:02 -070080
aleloi652ac892016-09-07 07:42:14 -070081 // Compute what audio sources to mix from audio_source_list_. Ramp
82 // in and out. Update mixed status. Mixes up to
83 // kMaximumAmountOfMixedAudioSources audio sources.
danilchap56359be2017-09-07 07:53:45 -070084 AudioFrameList GetAudioFromSources() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070085
86 // Add/remove the MixerAudioSource to the specified
87 // MixerAudioSource list.
aleloie8914152016-10-11 06:18:31 -070088 bool AddAudioSourceToList(Source* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -070089 SourceStatusList* audio_source_list) const;
aleloie8914152016-10-11 06:18:31 -070090 bool RemoveAudioSourceFromList(Source* remove_audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -070091 SourceStatusList* audio_source_list) const;
aleloi77ad3942016-07-04 06:33:02 -070092
aleloi920d30b2016-10-20 14:23:24 -070093 // The critical section lock guards audio source insertion and
94 // removal, which can be done from any thread. The race checker
95 // checks that mixing is done sequentially.
aleloi311525e2016-09-07 06:13:12 -070096 rtc::CriticalSection crit_;
aleloi920d30b2016-10-20 14:23:24 -070097 rtc::RaceChecker race_checker_;
aleloi77ad3942016-07-04 06:33:02 -070098
aleloi623427c2016-12-08 02:37:58 -080099 std::unique_ptr<OutputRateCalculator> output_rate_calculator_;
aleloi77ad3942016-07-04 06:33:02 -0700100 // The current sample frequency and sample size when mixing.
danilchap56359be2017-09-07 07:53:45 -0700101 int output_frequency_ RTC_GUARDED_BY(race_checker_);
102 size_t sample_size_ RTC_GUARDED_BY(race_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700103
aleloi09f45102016-07-28 03:52:15 -0700104 // List of all audio sources. Note all lists are disjunct
danilchap56359be2017-09-07 07:53:45 -0700105 SourceStatusList audio_source_list_ RTC_GUARDED_BY(crit_); // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -0700106
aleloi24899e52017-02-21 05:06:29 -0800107 // Component that handles actual adding of audio frames.
danilchap56359be2017-09-07 07:53:45 -0700108 FrameCombiner frame_combiner_ RTC_GUARDED_BY(race_checker_);
aleloi616df1e2016-08-24 01:17:12 -0700109
aleloia4c21062016-09-08 01:25:46 -0700110 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
aleloi77ad3942016-07-04 06:33:02 -0700111};
112} // namespace webrtc
113
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200114#endif // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_