blob: 864ecc2064cdeb771f97f9eb26f815c9cd529ed2 [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 <map>
15#include <memory>
aleloidf9e4d92016-08-08 10:26:09 -070016#include <vector>
aleloi77ad3942016-07-04 06:33:02 -070017
aleloi8b2233f2016-07-28 06:24:14 -070018#include "webrtc/base/thread_checker.h"
aleloi77ad3942016-07-04 06:33:02 -070019#include "webrtc/engine_configurations.h"
aleloi5d167d62016-08-24 02:20:54 -070020#include "webrtc/modules/audio_mixer/audio_mixer.h"
aleloi77ad3942016-07-04 06:33:02 -070021#include "webrtc/modules/include/module_common_types.h"
aleloi616df1e2016-08-24 01:17:12 -070022#include "webrtc/voice_engine/level_indicator.h"
aleloi77ad3942016-07-04 06:33:02 -070023
24namespace webrtc {
25class AudioProcessing;
26class CriticalSectionWrapper;
27
aleloi652ac892016-09-07 07:42:14 -070028typedef std::vector<AudioFrame*> AudioFrameList;
29typedef std::vector<MixerAudioSource*> MixerAudioSourceList;
aleloi77ad3942016-07-04 06:33:02 -070030
31// Cheshire cat implementation of MixerAudioSource's non virtual functions.
32class NewMixHistory {
33 public:
34 NewMixHistory();
35 ~NewMixHistory();
36
aleloi09f45102016-07-28 03:52:15 -070037 // Returns true if the audio source is being mixed.
aleloi77ad3942016-07-04 06:33:02 -070038 bool IsMixed() const;
39
aleloi09f45102016-07-28 03:52:15 -070040 // Returns true if the audio source was mixed previous mix
aleloi77ad3942016-07-04 06:33:02 -070041 // iteration.
42 bool WasMixed() const;
43
44 // Updates the mixed status.
45 int32_t SetIsMixed(bool mixed);
46
47 void ResetMixedStatus();
48
49 private:
aleloi6382a192016-08-08 10:25:04 -070050 bool is_mixed_;
aleloi77ad3942016-07-04 06:33:02 -070051};
52
aleloi5d167d62016-08-24 02:20:54 -070053class AudioMixerImpl : public AudioMixer {
aleloi77ad3942016-07-04 06:33:02 -070054 public:
55 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070056 static const int kFrameDurationInMs = 10;
aleloi77ad3942016-07-04 06:33:02 -070057
aleloi311525e2016-09-07 06:13:12 -070058 static std::unique_ptr<AudioMixer> Create(int id);
aleloi77ad3942016-07-04 06:33:02 -070059
aleloi5d167d62016-08-24 02:20:54 -070060 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070061
aleloi5d167d62016-08-24 02:20:54 -070062 // AudioMixer functions
aleloi09f45102016-07-28 03:52:15 -070063 int32_t SetMixabilityStatus(MixerAudioSource* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070064 bool mixable) override;
aleloi09f45102016-07-28 03:52:15 -070065 bool MixabilityStatus(const MixerAudioSource& audio_source) const override;
66 int32_t SetAnonymousMixabilityStatus(MixerAudioSource* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070067 bool mixable) override;
aleloi44968092016-08-08 10:18:58 -070068 void Mix(int sample_rate,
69 size_t number_of_channels,
70 AudioFrame* audio_frame_for_mixing) override;
aleloi77ad3942016-07-04 06:33:02 -070071 bool AnonymousMixabilityStatus(
aleloi09f45102016-07-28 03:52:15 -070072 const MixerAudioSource& audio_source) const override;
aleloi77ad3942016-07-04 06:33:02 -070073
74 private:
aleloi311525e2016-09-07 06:13:12 -070075 AudioMixerImpl(int id, std::unique_ptr<AudioProcessing> limiter);
76
aleloi77ad3942016-07-04 06:33:02 -070077 // Set/get mix frequency
78 int32_t SetOutputFrequency(const Frequency& frequency);
79 Frequency OutputFrequency() const;
80
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.
84 AudioFrameList GetNonAnonymousAudio() const EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070085
86 // Return the lowest mixing frequency that can be used without having to
87 // downsample any audio.
88 int32_t GetLowestMixingFrequency() const;
89 int32_t GetLowestMixingFrequencyFromList(
90 const MixerAudioSourceList& mixList) const;
91
aleloi652ac892016-09-07 07:42:14 -070092 // Return the AudioFrames that should be mixed anonymously. Ramp in
93 // and out. Update mixed status.
94 AudioFrameList GetAnonymousAudio() const EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070095
aleloi77ad3942016-07-04 06:33:02 -070096 // This function returns true if it finds the MixerAudioSource in the
97 // specified list of MixerAudioSources.
aleloi09f45102016-07-28 03:52:15 -070098 bool IsAudioSourceInList(const MixerAudioSource& audio_source,
99 const MixerAudioSourceList& audioSourceList) const;
aleloi77ad3942016-07-04 06:33:02 -0700100
101 // Add/remove the MixerAudioSource to the specified
102 // MixerAudioSource list.
aleloi09f45102016-07-28 03:52:15 -0700103 bool AddAudioSourceToList(MixerAudioSource* audio_source,
104 MixerAudioSourceList* audioSourceList) const;
105 bool RemoveAudioSourceFromList(MixerAudioSource* removeAudioSource,
106 MixerAudioSourceList* audioSourceList) const;
aleloi77ad3942016-07-04 06:33:02 -0700107
108 // Mix the AudioFrames stored in audioFrameList into mixedAudio.
aleloi09f45102016-07-28 03:52:15 -0700109 static int32_t MixFromList(AudioFrame* mixedAudio,
110 const AudioFrameList& audioFrameList,
111 int32_t id,
112 bool use_limiter);
aleloi77ad3942016-07-04 06:33:02 -0700113
aleloi77ad3942016-07-04 06:33:02 -0700114 bool LimitMixedAudio(AudioFrame* mixedAudio) const;
115
aleloi616df1e2016-08-24 01:17:12 -0700116 // Output level functions for VoEVolumeControl.
117 int GetOutputAudioLevel() override;
118
119 int GetOutputAudioLevelFullRange() override;
120
aleloi311525e2016-09-07 06:13:12 -0700121 rtc::CriticalSection crit_;
aleloi77ad3942016-07-04 06:33:02 -0700122
aleloi311525e2016-09-07 06:13:12 -0700123 const int32_t id_;
aleloi77ad3942016-07-04 06:33:02 -0700124
aleloi77ad3942016-07-04 06:33:02 -0700125 // The current sample frequency and sample size when mixing.
aleloi311525e2016-09-07 06:13:12 -0700126 Frequency output_frequency_ ACCESS_ON(&thread_checker_);
127 size_t sample_size_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700128
aleloi09f45102016-07-28 03:52:15 -0700129 // List of all audio sources. Note all lists are disjunct
aleloi311525e2016-09-07 06:13:12 -0700130 MixerAudioSourceList audio_source_list_ GUARDED_BY(crit_); // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -0700131
aleloi77ad3942016-07-04 06:33:02 -0700132 // Always mixed, anonomously.
aleloi311525e2016-09-07 06:13:12 -0700133 MixerAudioSourceList additional_audio_source_list_ GUARDED_BY(crit_);
aleloi77ad3942016-07-04 06:33:02 -0700134
aleloi311525e2016-09-07 06:13:12 -0700135 size_t num_mixed_audio_sources_ GUARDED_BY(crit_);
aleloi77ad3942016-07-04 06:33:02 -0700136 // Determines if we will use a limiter for clipping protection during
137 // mixing.
aleloi311525e2016-09-07 06:13:12 -0700138 bool use_limiter_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700139
aleloi311525e2016-09-07 06:13:12 -0700140 uint32_t time_stamp_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700141
aleloi8b2233f2016-07-28 06:24:14 -0700142 // Ensures that Mix is called from the same thread.
143 rtc::ThreadChecker thread_checker_;
aleloi77ad3942016-07-04 06:33:02 -0700144
145 // Used for inhibiting saturation in mixing.
aleloi311525e2016-09-07 06:13:12 -0700146 std::unique_ptr<AudioProcessing> limiter_ ACCESS_ON(&thread_checker_);
aleloi616df1e2016-08-24 01:17:12 -0700147
148 // Measures audio level for the combined signal.
aleloi311525e2016-09-07 06:13:12 -0700149 voe::AudioLevel audio_level_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700150};
151} // namespace webrtc
152
aleloi5d167d62016-08-24 02:20:54 -0700153#endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_