blob: 70a6f1d6b8b64fe1364ec5efd3e177eb5953dc95 [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
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
aleloi77ad3942016-07-04 06:33:02 -070016#include <memory>
aleloidf9e4d92016-08-08 10:26:09 -070017#include <vector>
aleloi77ad3942016-07-04 06:33:02 -070018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/audio/audio_frame.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/audio/audio_mixer.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010021#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_mixer/frame_combiner.h"
23#include "modules/audio_mixer/output_rate_calculator.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/constructor_magic.h"
25#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/race_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread_annotations.h"
aleloi77ad3942016-07-04 06:33:02 -070028
29namespace webrtc {
aleloi77ad3942016-07-04 06:33:02 -070030
aleloi652ac892016-09-07 07:42:14 -070031typedef std::vector<AudioFrame*> AudioFrameList;
aleloi77ad3942016-07-04 06:33:02 -070032
aleloi5d167d62016-08-24 02:20:54 -070033class AudioMixerImpl : public AudioMixer {
aleloi77ad3942016-07-04 06:33:02 -070034 public:
aleloi4b8bfb82016-10-12 02:14:59 -070035 struct SourceStatus {
36 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;
aleloi6c278492016-10-20 14:24:39 -070041
42 // A frame that will be passed to audio_source->GetAudioFrameWithInfo.
43 AudioFrame audio_frame;
aleloi4b8bfb82016-10-12 02:14:59 -070044 };
45
aleloi6c278492016-10-20 14:24:39 -070046 using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>;
aleloi4b8bfb82016-10-12 02:14:59 -070047
aleloi77ad3942016-07-04 06:33:02 -070048 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070049 static const int kFrameDurationInMs = 10;
aleloi116ec6d2016-10-12 06:07:07 -070050 static const int kMaximumAmountOfMixedAudioSources = 3;
aleloi77ad3942016-07-04 06:33:02 -070051
aleloi116ec6d2016-10-12 06:07:07 -070052 static rtc::scoped_refptr<AudioMixerImpl> Create();
aleloi24899e52017-02-21 05:06:29 -080053
aleloi087613c2017-02-21 08:27:08 -080054 static rtc::scoped_refptr<AudioMixerImpl> Create(
aleloi24899e52017-02-21 05:06:29 -080055 std::unique_ptr<OutputRateCalculator> output_rate_calculator,
56 bool use_limiter);
57
aleloi5d167d62016-08-24 02:20:54 -070058 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070059
aleloi5d167d62016-08-24 02:20:54 -070060 // AudioMixer functions
aleloi116ec6d2016-10-12 06:07:07 -070061 bool AddSource(Source* audio_source) override;
aleloi76b30492016-11-18 02:03:04 -080062 void RemoveSource(Source* audio_source) override;
aleloi116ec6d2016-10-12 06:07:07 -070063
aleloi95611832016-11-08 06:39:50 -080064 void Mix(size_t number_of_channels,
danilchap56359be2017-09-07 07:53:45 -070065 AudioFrame* audio_frame_for_mixing) override
66 RTC_LOCKS_EXCLUDED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070067
aleloi36542512016-10-07 05:28:32 -070068 // 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.
aleloie97974d2016-10-12 03:06:09 -070071 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const;
aleloi36542512016-10-07 05:28:32 -070072
aleloi116ec6d2016-10-12 06:07:07 -070073 protected:
aleloi24899e52017-02-21 05:06:29 -080074 AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator,
75 bool use_limiter);
aleloi311525e2016-09-07 06:13:12 -070076
aleloi116ec6d2016-10-12 06:07:07 -070077 private:
aleloi623427c2016-12-08 02:37:58 -080078 // Set mixing frequency through OutputFrequencyCalculator.
79 void CalculateOutputFrequency();
80 // Get mixing frequency.
aleloie97974d2016-10-12 03:06:09 -070081 int OutputFrequency() const;
aleloi77ad3942016-07-04 06:33:02 -070082
aleloi652ac892016-09-07 07:42:14 -070083 // 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.
danilchap56359be2017-09-07 07:53:45 -070086 AudioFrameList GetAudioFromSources() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070087
aleloi920d30b2016-10-20 14:23:24 -070088 // 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.
aleloi311525e2016-09-07 06:13:12 -070091 rtc::CriticalSection crit_;
aleloi920d30b2016-10-20 14:23:24 -070092 rtc::RaceChecker race_checker_;
aleloi77ad3942016-07-04 06:33:02 -070093
aleloi623427c2016-12-08 02:37:58 -080094 std::unique_ptr<OutputRateCalculator> output_rate_calculator_;
aleloi77ad3942016-07-04 06:33:02 -070095 // The current sample frequency and sample size when mixing.
danilchap56359be2017-09-07 07:53:45 -070096 int output_frequency_ RTC_GUARDED_BY(race_checker_);
97 size_t sample_size_ RTC_GUARDED_BY(race_checker_);
aleloi77ad3942016-07-04 06:33:02 -070098
aleloi09f45102016-07-28 03:52:15 -070099 // List of all audio sources. Note all lists are disjunct
danilchap56359be2017-09-07 07:53:45 -0700100 SourceStatusList audio_source_list_ RTC_GUARDED_BY(crit_); // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -0700101
aleloi24899e52017-02-21 05:06:29 -0800102 // Component that handles actual adding of audio frames.
danilchap56359be2017-09-07 07:53:45 -0700103 FrameCombiner frame_combiner_ RTC_GUARDED_BY(race_checker_);
aleloi616df1e2016-08-24 01:17:12 -0700104
aleloia4c21062016-09-08 01:25:46 -0700105 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
aleloi77ad3942016-07-04 06:33:02 -0700106};
107} // namespace webrtc
108
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200109#endif // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_