blob: d4b9aa857b3160b4e9876f0acab13df5eb4cb323 [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_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"
aleloi616df1e2016-08-24 01:17:12 -070024#include "webrtc/voice_engine/level_indicator.h"
mflodman7056be92016-10-07 07:07:28 +020025#include "webrtc/voice_engine_configurations.h"
aleloi77ad3942016-07-04 06:33:02 -070026
27namespace webrtc {
aleloi77ad3942016-07-04 06:33:02 -070028
aleloi652ac892016-09-07 07:42:14 -070029typedef std::vector<AudioFrame*> AudioFrameList;
aleloi77ad3942016-07-04 06:33:02 -070030
aleloi5d167d62016-08-24 02:20:54 -070031class AudioMixerImpl : public AudioMixer {
aleloi77ad3942016-07-04 06:33:02 -070032 public:
aleloi4b8bfb82016-10-12 02:14:59 -070033 struct SourceStatus {
34 SourceStatus(Source* audio_source, bool is_mixed, float gain)
35 : audio_source(audio_source), is_mixed(is_mixed), gain(gain) {}
36 Source* audio_source = nullptr;
37 bool is_mixed = false;
38 float gain = 0.0f;
39 };
40
41 typedef std::vector<SourceStatus> SourceStatusList;
42
aleloi77ad3942016-07-04 06:33:02 -070043 // AudioProcessing only accepts 10 ms frames.
aleloi5d167d62016-08-24 02:20:54 -070044 static const int kFrameDurationInMs = 10;
aleloi77ad3942016-07-04 06:33:02 -070045
aleloi36542512016-10-07 05:28:32 -070046 static std::unique_ptr<AudioMixerImpl> Create(int id);
aleloi77ad3942016-07-04 06:33:02 -070047
aleloi5d167d62016-08-24 02:20:54 -070048 ~AudioMixerImpl() override;
aleloi70f866c2016-08-16 02:15:49 -070049
aleloi5d167d62016-08-24 02:20:54 -070050 // AudioMixer functions
aleloie8914152016-10-11 06:18:31 -070051 int32_t SetMixabilityStatus(Source* audio_source, bool mixable) override;
52 bool MixabilityStatus(const Source& audio_source) const override;
53 int32_t SetAnonymousMixabilityStatus(Source* audio_source,
aleloi77ad3942016-07-04 06:33:02 -070054 bool mixable) override;
aleloi44968092016-08-08 10:18:58 -070055 void Mix(int sample_rate,
56 size_t number_of_channels,
57 AudioFrame* audio_frame_for_mixing) override;
aleloie8914152016-10-11 06:18:31 -070058 bool AnonymousMixabilityStatus(const Source& audio_source) const override;
aleloi77ad3942016-07-04 06:33:02 -070059
aleloi36542512016-10-07 05:28:32 -070060 // Returns true if the source was mixed last round. Returns
61 // false and logs an error if the source was never added to the
62 // mixer.
aleloie8914152016-10-11 06:18:31 -070063 bool GetAudioSourceMixabilityStatusForTest(Source* audio_source);
aleloi36542512016-10-07 05:28:32 -070064
aleloi77ad3942016-07-04 06:33:02 -070065 private:
aleloi311525e2016-09-07 06:13:12 -070066 AudioMixerImpl(int id, std::unique_ptr<AudioProcessing> limiter);
67
aleloi77ad3942016-07-04 06:33:02 -070068 // Set/get mix frequency
69 int32_t SetOutputFrequency(const Frequency& frequency);
70 Frequency OutputFrequency() const;
71
aleloi652ac892016-09-07 07:42:14 -070072 // Compute what audio sources to mix from audio_source_list_. Ramp
73 // in and out. Update mixed status. Mixes up to
74 // kMaximumAmountOfMixedAudioSources audio sources.
aleloi36542512016-10-07 05:28:32 -070075 AudioFrameList GetNonAnonymousAudio() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070076
aleloi652ac892016-09-07 07:42:14 -070077 // Return the AudioFrames that should be mixed anonymously. Ramp in
78 // and out. Update mixed status.
aleloi36542512016-10-07 05:28:32 -070079 AudioFrameList GetAnonymousAudio() EXCLUSIVE_LOCKS_REQUIRED(crit_);
aleloi77ad3942016-07-04 06:33:02 -070080
81 // Add/remove the MixerAudioSource to the specified
82 // MixerAudioSource list.
aleloie8914152016-10-11 06:18:31 -070083 bool AddAudioSourceToList(Source* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -070084 SourceStatusList* audio_source_list) const;
aleloie8914152016-10-11 06:18:31 -070085 bool RemoveAudioSourceFromList(Source* remove_audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -070086 SourceStatusList* audio_source_list) const;
aleloi77ad3942016-07-04 06:33:02 -070087
aleloia4c21062016-09-08 01:25:46 -070088 bool LimitMixedAudio(AudioFrame* mixed_audio) const;
aleloi77ad3942016-07-04 06:33:02 -070089
aleloi616df1e2016-08-24 01:17:12 -070090 // Output level functions for VoEVolumeControl.
91 int GetOutputAudioLevel() override;
92
93 int GetOutputAudioLevelFullRange() override;
94
aleloi311525e2016-09-07 06:13:12 -070095 rtc::CriticalSection crit_;
aleloi77ad3942016-07-04 06:33:02 -070096
aleloi311525e2016-09-07 06:13:12 -070097 const int32_t id_;
aleloi77ad3942016-07-04 06:33:02 -070098
aleloi77ad3942016-07-04 06:33:02 -070099 // The current sample frequency and sample size when mixing.
aleloi311525e2016-09-07 06:13:12 -0700100 Frequency output_frequency_ ACCESS_ON(&thread_checker_);
101 size_t sample_size_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700102
aleloi09f45102016-07-28 03:52:15 -0700103 // List of all audio sources. Note all lists are disjunct
aleloi4b8bfb82016-10-12 02:14:59 -0700104 SourceStatusList audio_source_list_ GUARDED_BY(crit_); // May be mixed.
aleloia0db81f2016-07-28 06:36:22 -0700105
aleloia4c21062016-09-08 01:25:46 -0700106 // Always mixed, anonymously.
aleloi4b8bfb82016-10-12 02:14:59 -0700107 SourceStatusList additional_audio_source_list_ GUARDED_BY(crit_);
aleloi77ad3942016-07-04 06:33:02 -0700108
aleloi311525e2016-09-07 06:13:12 -0700109 size_t num_mixed_audio_sources_ GUARDED_BY(crit_);
aleloi77ad3942016-07-04 06:33:02 -0700110 // Determines if we will use a limiter for clipping protection during
111 // mixing.
aleloi311525e2016-09-07 06:13:12 -0700112 bool use_limiter_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700113
aleloi311525e2016-09-07 06:13:12 -0700114 uint32_t time_stamp_ ACCESS_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700115
aleloi8b2233f2016-07-28 06:24:14 -0700116 // Ensures that Mix is called from the same thread.
117 rtc::ThreadChecker thread_checker_;
aleloi77ad3942016-07-04 06:33:02 -0700118
119 // Used for inhibiting saturation in mixing.
aleloi311525e2016-09-07 06:13:12 -0700120 std::unique_ptr<AudioProcessing> limiter_ ACCESS_ON(&thread_checker_);
aleloi616df1e2016-08-24 01:17:12 -0700121
122 // Measures audio level for the combined signal.
aleloi311525e2016-09-07 06:13:12 -0700123 voe::AudioLevel audio_level_ ACCESS_ON(&thread_checker_);
aleloia4c21062016-09-08 01:25:46 -0700124
125 RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
aleloi77ad3942016-07-04 06:33:02 -0700126};
127} // namespace webrtc
128
aleloi5d167d62016-08-24 02:20:54 -0700129#endif // WEBRTC_MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_