blob: 6ddf9ca5e8bde5c6954d458a6cfcdba8a902f9e7 [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
14#include <list>
15#include <map>
16#include <memory>
aleloidf9e4d92016-08-08 10:26:09 -070017#include <vector>
aleloi77ad3942016-07-04 06:33:02 -070018
aleloi8b2233f2016-07-28 06:24:14 -070019#include "webrtc/base/thread_checker.h"
aleloi77ad3942016-07-04 06:33:02 -070020#include "webrtc/engine_configurations.h"
aleloi5d167d62016-08-24 02:20:54 -070021#include "webrtc/modules/audio_mixer/audio_mixer.h"
aleloi77ad3942016-07-04 06:33:02 -070022#include "webrtc/modules/include/module_common_types.h"
aleloi616df1e2016-08-24 01:17:12 -070023#include "webrtc/voice_engine/level_indicator.h"
aleloi77ad3942016-07-04 06:33:02 -070024
25namespace webrtc {
26class AudioProcessing;
27class CriticalSectionWrapper;
28
29struct FrameAndMuteInfo {
30 FrameAndMuteInfo(AudioFrame* f, bool m) : frame(f), muted(m) {}
31 AudioFrame* frame;
32 bool muted;
33};
34
35typedef std::list<FrameAndMuteInfo> AudioFrameList;
36typedef std::list<MixerAudioSource*> MixerAudioSourceList;
37
38// Cheshire cat implementation of MixerAudioSource's non virtual functions.
39class NewMixHistory {
40 public:
41 NewMixHistory();
42 ~NewMixHistory();
43
aleloi09f45102016-07-28 03:52:15 -070044 // Returns true if the audio source is being mixed.
aleloi77ad3942016-07-04 06:33:02 -070045 bool IsMixed() const;
46
aleloi09f45102016-07-28 03:52:15 -070047 // Returns true if the audio source was mixed previous mix
aleloi77ad3942016-07-04 06:33:02 -070048 // iteration.
49 bool WasMixed() const;
50
51 // Updates the mixed status.
52 int32_t SetIsMixed(bool mixed);
53
54 void ResetMixedStatus();
55
56 private:
aleloi6382a192016-08-08 10:25:04 -070057 bool is_mixed_;
aleloi77ad3942016-07-04 06:33:02 -070058};
59
aleloi5d167d62016-08-24 02:20:54 -070060class AudioMixerImpl : public AudioMixer {
aleloi77ad3942016-07-04 06:33:02 -070061 public:
62 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070063 static const int kFrameDurationInMs = 10;
aleloi77ad3942016-07-04 06:33:02 -070064
aleloi5d167d62016-08-24 02:20:54 -070065 explicit AudioMixerImpl(int id);
aleloi77ad3942016-07-04 06:33:02 -070066
aleloi5d167d62016-08-24 02:20:54 -070067 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070068
aleloi77ad3942016-07-04 06:33:02 -070069 // Must be called after ctor.
70 bool Init();
71
aleloi5d167d62016-08-24 02:20:54 -070072 // AudioMixer functions
aleloi09f45102016-07-28 03:52:15 -070073 int32_t SetMixabilityStatus(MixerAudioSource* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070074 bool mixable) override;
aleloi09f45102016-07-28 03:52:15 -070075 bool MixabilityStatus(const MixerAudioSource& audio_source) const override;
76 int32_t SetAnonymousMixabilityStatus(MixerAudioSource* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070077 bool mixable) override;
aleloi44968092016-08-08 10:18:58 -070078 void Mix(int sample_rate,
79 size_t number_of_channels,
80 AudioFrame* audio_frame_for_mixing) override;
aleloi77ad3942016-07-04 06:33:02 -070081 bool AnonymousMixabilityStatus(
aleloi09f45102016-07-28 03:52:15 -070082 const MixerAudioSource& audio_source) const override;
aleloi77ad3942016-07-04 06:33:02 -070083
84 private:
aleloi77ad3942016-07-04 06:33:02 -070085 // Set/get mix frequency
86 int32_t SetOutputFrequency(const Frequency& frequency);
87 Frequency OutputFrequency() const;
88
aleloif3882572016-07-29 02:12:41 -070089 // Compute what audio sources to mix from audio_source_list_. Ramp in
90 // and out. Update mixed status. maxAudioFrameCounter specifies how
91 // many participants are allowed to be mixed.
92 AudioFrameList UpdateToMix(size_t maxAudioFrameCounter) const;
aleloi77ad3942016-07-04 06:33:02 -070093
94 // Return the lowest mixing frequency that can be used without having to
95 // downsample any audio.
96 int32_t GetLowestMixingFrequency() const;
97 int32_t GetLowestMixingFrequencyFromList(
98 const MixerAudioSourceList& mixList) const;
99
100 // Return the AudioFrames that should be mixed anonymously.
101 void GetAdditionalAudio(AudioFrameList* additionalFramesList) const;
102
aleloi77ad3942016-07-04 06:33:02 -0700103 // This function returns true if it finds the MixerAudioSource in the
104 // specified list of MixerAudioSources.
aleloi09f45102016-07-28 03:52:15 -0700105 bool IsAudioSourceInList(const MixerAudioSource& audio_source,
106 const MixerAudioSourceList& audioSourceList) const;
aleloi77ad3942016-07-04 06:33:02 -0700107
108 // Add/remove the MixerAudioSource to the specified
109 // MixerAudioSource list.
aleloi09f45102016-07-28 03:52:15 -0700110 bool AddAudioSourceToList(MixerAudioSource* audio_source,
111 MixerAudioSourceList* audioSourceList) const;
112 bool RemoveAudioSourceFromList(MixerAudioSource* removeAudioSource,
113 MixerAudioSourceList* audioSourceList) const;
aleloi77ad3942016-07-04 06:33:02 -0700114
115 // Mix the AudioFrames stored in audioFrameList into mixedAudio.
aleloi09f45102016-07-28 03:52:15 -0700116 static int32_t MixFromList(AudioFrame* mixedAudio,
117 const AudioFrameList& audioFrameList,
118 int32_t id,
119 bool use_limiter);
aleloi77ad3942016-07-04 06:33:02 -0700120
121 // Mix the AudioFrames stored in audioFrameList into mixedAudio. No
122 // record will be kept of this mix (e.g. the corresponding MixerAudioSources
123 // will not be marked as IsMixed()
124 int32_t MixAnonomouslyFromList(AudioFrame* mixedAudio,
125 const AudioFrameList& audioFrameList) const;
126
127 bool LimitMixedAudio(AudioFrame* mixedAudio) const;
128
aleloi616df1e2016-08-24 01:17:12 -0700129 // Output level functions for VoEVolumeControl.
130 int GetOutputAudioLevel() override;
131
132 int GetOutputAudioLevelFullRange() override;
133
aleloi6382a192016-08-08 10:25:04 -0700134 std::unique_ptr<CriticalSectionWrapper> crit_;
135 std::unique_ptr<CriticalSectionWrapper> cb_crit_;
aleloi77ad3942016-07-04 06:33:02 -0700136
aleloi6382a192016-08-08 10:25:04 -0700137 int32_t id_;
aleloi77ad3942016-07-04 06:33:02 -0700138
aleloi77ad3942016-07-04 06:33:02 -0700139 // The current sample frequency and sample size when mixing.
aleloi6382a192016-08-08 10:25:04 -0700140 Frequency output_frequency_;
141 size_t sample_size_;
aleloi77ad3942016-07-04 06:33:02 -0700142
aleloi09f45102016-07-28 03:52:15 -0700143 // List of all audio sources. Note all lists are disjunct
144 MixerAudioSourceList audio_source_list_; // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -0700145
aleloi77ad3942016-07-04 06:33:02 -0700146 // Always mixed, anonomously.
aleloi09f45102016-07-28 03:52:15 -0700147 MixerAudioSourceList additional_audio_source_list_;
aleloi77ad3942016-07-04 06:33:02 -0700148
aleloi09f45102016-07-28 03:52:15 -0700149 size_t num_mixed_audio_sources_;
aleloi77ad3942016-07-04 06:33:02 -0700150 // Determines if we will use a limiter for clipping protection during
151 // mixing.
152 bool use_limiter_;
153
aleloi6382a192016-08-08 10:25:04 -0700154 uint32_t time_stamp_;
aleloi77ad3942016-07-04 06:33:02 -0700155
aleloi8b2233f2016-07-28 06:24:14 -0700156 // Ensures that Mix is called from the same thread.
157 rtc::ThreadChecker thread_checker_;
aleloi77ad3942016-07-04 06:33:02 -0700158
159 // Used for inhibiting saturation in mixing.
aleloi6382a192016-08-08 10:25:04 -0700160 std::unique_ptr<AudioProcessing> limiter_;
aleloi616df1e2016-08-24 01:17:12 -0700161
162 // Measures audio level for the combined signal.
163 voe::AudioLevel audio_level_;
aleloi77ad3942016-07-04 06:33:02 -0700164};
165} // namespace webrtc
166
aleloi5d167d62016-08-24 02:20:54 -0700167#endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_