blob: 942196afd21526a7c3c5b87f83a5a8cdf4a0b7fd [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
aleloidc7669a2016-10-04 04:06:20 -070018#include "webrtc/base/thread_annotations.h"
aleloi8b2233f2016-07-28 06:24:14 -070019#include "webrtc/base/thread_checker.h"
aleloi5d167d62016-08-24 02:20:54 -070020#include "webrtc/modules/audio_mixer/audio_mixer.h"
aleloidc7669a2016-10-04 04:06:20 -070021#include "webrtc/modules/audio_mixer/audio_mixer_defines.h"
22#include "webrtc/modules/audio_processing/include/audio_processing.h"
aleloi77ad3942016-07-04 06:33:02 -070023#include "webrtc/modules/include/module_common_types.h"
aleloidc7669a2016-10-04 04:06:20 -070024#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
aleloi616df1e2016-08-24 01:17:12 -070025#include "webrtc/voice_engine/level_indicator.h"
mflodman7056be92016-10-07 07:07:28 +020026#include "webrtc/voice_engine_configurations.h"
aleloi77ad3942016-07-04 06:33:02 -070027
28namespace webrtc {
aleloi77ad3942016-07-04 06:33:02 -070029
aleloi652ac892016-09-07 07:42:14 -070030typedef std::vector<AudioFrame*> AudioFrameList;
31typedef std::vector<MixerAudioSource*> MixerAudioSourceList;
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:
35 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070036 static const int kFrameDurationInMs = 10;
aleloi77ad3942016-07-04 06:33:02 -070037
aleloi311525e2016-09-07 06:13:12 -070038 static std::unique_ptr<AudioMixer> Create(int id);
aleloi77ad3942016-07-04 06:33:02 -070039
aleloi5d167d62016-08-24 02:20:54 -070040 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070041
aleloi5d167d62016-08-24 02:20:54 -070042 // AudioMixer functions
aleloi09f45102016-07-28 03:52:15 -070043 int32_t SetMixabilityStatus(MixerAudioSource* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070044 bool mixable) override;
aleloi09f45102016-07-28 03:52:15 -070045 bool MixabilityStatus(const MixerAudioSource& audio_source) const override;
46 int32_t SetAnonymousMixabilityStatus(MixerAudioSource* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070047 bool mixable) override;
aleloi44968092016-08-08 10:18:58 -070048 void Mix(int sample_rate,
49 size_t number_of_channels,
50 AudioFrame* audio_frame_for_mixing) override;
aleloi77ad3942016-07-04 06:33:02 -070051 bool AnonymousMixabilityStatus(
aleloi09f45102016-07-28 03:52:15 -070052 const MixerAudioSource& audio_source) const override;
aleloi77ad3942016-07-04 06:33:02 -070053
54 private:
aleloi311525e2016-09-07 06:13:12 -070055 AudioMixerImpl(int id, std::unique_ptr<AudioProcessing> limiter);
56
aleloi77ad3942016-07-04 06:33:02 -070057 // Set/get mix frequency
58 int32_t SetOutputFrequency(const Frequency& frequency);
59 Frequency OutputFrequency() const;
60
aleloi652ac892016-09-07 07:42:14 -070061 // Compute what audio sources to mix from audio_source_list_. Ramp
62 // in and out. Update mixed status. Mixes up to
63 // kMaximumAmountOfMixedAudioSources audio sources.
64 AudioFrameList GetNonAnonymousAudio() const EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070065
aleloi652ac892016-09-07 07:42:14 -070066 // Return the AudioFrames that should be mixed anonymously. Ramp in
67 // and out. Update mixed status.
68 AudioFrameList GetAnonymousAudio() const EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070069
aleloi77ad3942016-07-04 06:33:02 -070070 // This function returns true if it finds the MixerAudioSource in the
71 // specified list of MixerAudioSources.
aleloi09f45102016-07-28 03:52:15 -070072 bool IsAudioSourceInList(const MixerAudioSource& audio_source,
aleloia4c21062016-09-08 01:25:46 -070073 const MixerAudioSourceList& audio_source_list) const;
aleloi77ad3942016-07-04 06:33:02 -070074
75 // Add/remove the MixerAudioSource to the specified
76 // MixerAudioSource list.
aleloi09f45102016-07-28 03:52:15 -070077 bool AddAudioSourceToList(MixerAudioSource* audio_source,
aleloia4c21062016-09-08 01:25:46 -070078 MixerAudioSourceList* audio_source_list) const;
79 bool RemoveAudioSourceFromList(MixerAudioSource* remove_audio_source,
80 MixerAudioSourceList* audio_source_list) const;
aleloi77ad3942016-07-04 06:33:02 -070081
aleloia4c21062016-09-08 01:25:46 -070082 bool LimitMixedAudio(AudioFrame* mixed_audio) const;
aleloi77ad3942016-07-04 06:33:02 -070083
aleloi616df1e2016-08-24 01:17:12 -070084 // Output level functions for VoEVolumeControl.
85 int GetOutputAudioLevel() override;
86
87 int GetOutputAudioLevelFullRange() override;
88
aleloi311525e2016-09-07 06:13:12 -070089 rtc::CriticalSection crit_;
aleloi77ad3942016-07-04 06:33:02 -070090
aleloi311525e2016-09-07 06:13:12 -070091 const int32_t id_;
aleloi77ad3942016-07-04 06:33:02 -070092
aleloi77ad3942016-07-04 06:33:02 -070093 // The current sample frequency and sample size when mixing.
aleloi311525e2016-09-07 06:13:12 -070094 Frequency output_frequency_ ACCESS_ON(&thread_checker_);
95 size_t sample_size_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -070096
aleloi09f45102016-07-28 03:52:15 -070097 // List of all audio sources. Note all lists are disjunct
aleloi311525e2016-09-07 06:13:12 -070098 MixerAudioSourceList audio_source_list_ GUARDED_BY(crit_); // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -070099
aleloia4c21062016-09-08 01:25:46 -0700100 // Always mixed, anonymously.
aleloi311525e2016-09-07 06:13:12 -0700101 MixerAudioSourceList additional_audio_source_list_ GUARDED_BY(crit_);
aleloi77ad3942016-07-04 06:33:02 -0700102
aleloi311525e2016-09-07 06:13:12 -0700103 size_t num_mixed_audio_sources_ GUARDED_BY(crit_);
aleloi77ad3942016-07-04 06:33:02 -0700104 // Determines if we will use a limiter for clipping protection during
105 // mixing.
aleloi311525e2016-09-07 06:13:12 -0700106 bool use_limiter_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700107
aleloi311525e2016-09-07 06:13:12 -0700108 uint32_t time_stamp_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700109
aleloi8b2233f2016-07-28 06:24:14 -0700110 // Ensures that Mix is called from the same thread.
111 rtc::ThreadChecker thread_checker_;
aleloi77ad3942016-07-04 06:33:02 -0700112
113 // Used for inhibiting saturation in mixing.
aleloi311525e2016-09-07 06:13:12 -0700114 std::unique_ptr<AudioProcessing> limiter_ ACCESS_ON(&thread_checker_);
aleloi616df1e2016-08-24 01:17:12 -0700115
116 // Measures audio level for the combined signal.
aleloi311525e2016-09-07 06:13:12 -0700117 voe::AudioLevel audio_level_ ACCESS_ON(&thread_checker_);
aleloia4c21062016-09-08 01:25:46 -0700118
119 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
aleloi77ad3942016-07-04 06:33:02 -0700120};
121} // namespace webrtc
122
aleloi5d167d62016-08-24 02:20:54 -0700123#endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_