blob: e2f55fce400ae76dae5d301e8cd1c26103ee9900 [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
aleloi5d167d62016-08-24 02:20:54 -070011#ifndef WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
12#define WEBRTC_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
aleloi201dfe92016-10-20 05:06:39 -070017#include "webrtc/api/audio/audio_mixer.h"
aleloi116ec6d2016-10-12 06:07:07 -070018#include "webrtc/base/scoped_ref_ptr.h"
aleloidc7669a2016-10-04 04:06:20 -070019#include "webrtc/base/thread_annotations.h"
aleloi920d30b2016-10-20 14:23:24 -070020#include "webrtc/base/race_checker.h"
aleloidc7669a2016-10-04 04:06:20 -070021#include "webrtc/modules/audio_processing/include/audio_processing.h"
aleloi77ad3942016-07-04 06:33:02 -070022#include "webrtc/modules/include/module_common_types.h"
aleloidc7669a2016-10-04 04:06:20 -070023#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
mflodman7056be92016-10-07 07:07:28 +020024#include "webrtc/voice_engine_configurations.h"
aleloi77ad3942016-07-04 06:33:02 -070025
26namespace webrtc {
aleloi77ad3942016-07-04 06:33:02 -070027
aleloi652ac892016-09-07 07:42:14 -070028typedef std::vector<AudioFrame*> AudioFrameList;
aleloi77ad3942016-07-04 06:33:02 -070029
aleloi5d167d62016-08-24 02:20:54 -070030class AudioMixerImpl : public AudioMixer {
aleloi77ad3942016-07-04 06:33:02 -070031 public:
aleloi4b8bfb82016-10-12 02:14:59 -070032 struct SourceStatus {
33 SourceStatus(Source* audio_source, bool is_mixed, float gain)
34 : audio_source(audio_source), is_mixed(is_mixed), gain(gain) {}
35 Source* audio_source = nullptr;
36 bool is_mixed = false;
37 float gain = 0.0f;
aleloi6c278492016-10-20 14:24:39 -070038
39 // A frame that will be passed to audio_source->GetAudioFrameWithInfo.
40 AudioFrame audio_frame;
aleloi4b8bfb82016-10-12 02:14:59 -070041 };
42
aleloi6c278492016-10-20 14:24:39 -070043 using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>;
aleloi4b8bfb82016-10-12 02:14:59 -070044
aleloi77ad3942016-07-04 06:33:02 -070045 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070046 static const int kFrameDurationInMs = 10;
aleloi116ec6d2016-10-12 06:07:07 -070047 static const int kMaximumAmountOfMixedAudioSources = 3;
aleloie97974d2016-10-12 03:06:09 -070048 static const int kDefaultFrequency = 48000;
aleloi77ad3942016-07-04 06:33:02 -070049
aleloi116ec6d2016-10-12 06:07:07 -070050 static rtc::scoped_refptr<AudioMixerImpl> Create();
aleloi77ad3942016-07-04 06:33:02 -070051
aleloi5d167d62016-08-24 02:20:54 -070052 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070053
aleloi5d167d62016-08-24 02:20:54 -070054 // AudioMixer functions
aleloi116ec6d2016-10-12 06:07:07 -070055 bool AddSource(Source* audio_source) override;
56 bool RemoveSource(Source* audio_source) override;
57
58 void Mix(int sample_rate_hz,
aleloi44968092016-08-08 10:18:58 -070059 size_t number_of_channels,
aleloi1655e452016-10-24 06:56:56 -070060 AudioFrame* audio_frame_for_mixing) override LOCKS_EXCLUDED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070061
aleloi36542512016-10-07 05:28:32 -070062 // Returns true if the source was mixed last round. Returns
63 // false and logs an error if the source was never added to the
64 // mixer.
aleloie97974d2016-10-12 03:06:09 -070065 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const;
aleloi36542512016-10-07 05:28:32 -070066
aleloi116ec6d2016-10-12 06:07:07 -070067 protected:
aleloie97974d2016-10-12 03:06:09 -070068 explicit AudioMixerImpl(std::unique_ptr<AudioProcessing> limiter);
aleloi311525e2016-09-07 06:13:12 -070069
aleloi116ec6d2016-10-12 06:07:07 -070070 private:
aleloi77ad3942016-07-04 06:33:02 -070071 // Set/get mix frequency
aleloie97974d2016-10-12 03:06:09 -070072 void SetOutputFrequency(int frequency);
73 int OutputFrequency() const;
aleloi77ad3942016-07-04 06:33:02 -070074
aleloi652ac892016-09-07 07:42:14 -070075 // Compute what audio sources to mix from audio_source_list_. Ramp
76 // in and out. Update mixed status. Mixes up to
77 // kMaximumAmountOfMixedAudioSources audio sources.
aleloi116ec6d2016-10-12 06:07:07 -070078 AudioFrameList GetAudioFromSources() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070079
80 // Add/remove the MixerAudioSource to the specified
81 // MixerAudioSource list.
aleloie8914152016-10-11 06:18:31 -070082 bool AddAudioSourceToList(Source* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -070083 SourceStatusList* audio_source_list) const;
aleloie8914152016-10-11 06:18:31 -070084 bool RemoveAudioSourceFromList(Source* remove_audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -070085 SourceStatusList* audio_source_list) const;
aleloi77ad3942016-07-04 06:33:02 -070086
aleloia4c21062016-09-08 01:25:46 -070087 bool LimitMixedAudio(AudioFrame* mixed_audio) const;
aleloi77ad3942016-07-04 06:33:02 -070088
aleloi920d30b2016-10-20 14:23:24 -070089 // The critical section lock guards audio source insertion and
90 // removal, which can be done from any thread. The race checker
91 // checks that mixing is done sequentially.
aleloi311525e2016-09-07 06:13:12 -070092 rtc::CriticalSection crit_;
aleloi920d30b2016-10-20 14:23:24 -070093 rtc::RaceChecker race_checker_;
aleloi77ad3942016-07-04 06:33:02 -070094
aleloi77ad3942016-07-04 06:33:02 -070095 // The current sample frequency and sample size when mixing.
aleloi920d30b2016-10-20 14:23:24 -070096 int output_frequency_ GUARDED_BY(race_checker_);
97 size_t sample_size_ 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
aleloi4b8bfb82016-10-12 02:14:59 -0700100 SourceStatusList audio_source_list_ GUARDED_BY(crit_); // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -0700101
aleloi77ad3942016-07-04 06:33:02 -0700102 // Determines if we will use a limiter for clipping protection during
103 // mixing.
aleloi920d30b2016-10-20 14:23:24 -0700104 bool use_limiter_ GUARDED_BY(race_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700105
aleloi920d30b2016-10-20 14:23:24 -0700106 uint32_t time_stamp_ GUARDED_BY(race_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700107
108 // Used for inhibiting saturation in mixing.
aleloi920d30b2016-10-20 14:23:24 -0700109 std::unique_ptr<AudioProcessing> limiter_ GUARDED_BY(race_checker_);
aleloi616df1e2016-08-24 01:17:12 -0700110
aleloia4c21062016-09-08 01:25:46 -0700111 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
aleloi77ad3942016-07-04 06:33:02 -0700112};
113} // namespace webrtc
114
aleloi5d167d62016-08-24 02:20:54 -0700115#endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_