2016-07-04 06:33:02 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#ifndef MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
|
|
|
|
|
#define MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <stddef.h>
|
2016-07-04 06:33:02 -07:00
|
|
|
#include <memory>
|
2016-08-08 10:26:09 -07:00
|
|
|
#include <vector>
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "api/audio/audio_frame.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/audio/audio_mixer.h"
|
2019-01-25 20:26:48 +01:00
|
|
|
#include "api/scoped_refptr.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_mixer/frame_combiner.h"
|
|
|
|
|
#include "modules/audio_mixer/output_rate_calculator.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/constructor_magic.h"
|
|
|
|
|
#include "rtc_base/critical_section.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/race_checker.h"
|
|
|
|
|
#include "rtc_base/thread_annotations.h"
|
2016-07-04 06:33:02 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-09-07 07:42:14 -07:00
|
|
|
typedef std::vector<AudioFrame*> AudioFrameList;
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-08-24 02:20:54 -07:00
|
|
|
class AudioMixerImpl : public AudioMixer {
|
2016-07-04 06:33:02 -07:00
|
|
|
public:
|
2016-10-12 02:14:59 -07:00
|
|
|
struct SourceStatus {
|
|
|
|
|
SourceStatus(Source* audio_source, bool is_mixed, float gain)
|
|
|
|
|
: audio_source(audio_source), is_mixed(is_mixed), gain(gain) {}
|
|
|
|
|
Source* audio_source = nullptr;
|
|
|
|
|
bool is_mixed = false;
|
|
|
|
|
float gain = 0.0f;
|
2016-10-20 14:24:39 -07:00
|
|
|
|
|
|
|
|
// A frame that will be passed to audio_source->GetAudioFrameWithInfo.
|
|
|
|
|
AudioFrame audio_frame;
|
2016-10-12 02:14:59 -07:00
|
|
|
};
|
|
|
|
|
|
2016-10-20 14:24:39 -07:00
|
|
|
using SourceStatusList = std::vector<std::unique_ptr<SourceStatus>>;
|
2016-10-12 02:14:59 -07:00
|
|
|
|
2016-07-04 06:33:02 -07:00
|
|
|
// AudioProcessing only accepts 10 ms frames.
|
2016-08-24 02:20:54 -07:00
|
|
|
static const int kFrameDurationInMs = 10;
|
2016-10-12 06:07:07 -07:00
|
|
|
static const int kMaximumAmountOfMixedAudioSources = 3;
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-10-12 06:07:07 -07:00
|
|
|
static rtc::scoped_refptr<AudioMixerImpl> Create();
|
2017-02-21 05:06:29 -08:00
|
|
|
|
2017-02-21 08:27:08 -08:00
|
|
|
static rtc::scoped_refptr<AudioMixerImpl> Create(
|
2017-02-21 05:06:29 -08:00
|
|
|
std::unique_ptr<OutputRateCalculator> output_rate_calculator,
|
|
|
|
|
bool use_limiter);
|
|
|
|
|
|
2016-08-24 02:20:54 -07:00
|
|
|
~AudioMixerImpl() override;
|
2016-08-16 02:15:49 -07:00
|
|
|
|
2016-08-24 02:20:54 -07:00
|
|
|
// AudioMixer functions
|
2016-10-12 06:07:07 -07:00
|
|
|
bool AddSource(Source* audio_source) override;
|
2016-11-18 02:03:04 -08:00
|
|
|
void RemoveSource(Source* audio_source) override;
|
2016-10-12 06:07:07 -07:00
|
|
|
|
2016-11-08 06:39:50 -08:00
|
|
|
void Mix(size_t number_of_channels,
|
2017-09-07 07:53:45 -07:00
|
|
|
AudioFrame* audio_frame_for_mixing) override
|
|
|
|
|
RTC_LOCKS_EXCLUDED(crit_);
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-10-07 05:28:32 -07:00
|
|
|
// Returns true if the source was mixed last round. Returns
|
|
|
|
|
// false and logs an error if the source was never added to the
|
|
|
|
|
// mixer.
|
2016-10-12 03:06:09 -07:00
|
|
|
bool GetAudioSourceMixabilityStatusForTest(Source* audio_source) const;
|
2016-10-07 05:28:32 -07:00
|
|
|
|
2016-10-12 06:07:07 -07:00
|
|
|
protected:
|
2017-02-21 05:06:29 -08:00
|
|
|
AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator,
|
|
|
|
|
bool use_limiter);
|
2016-09-07 06:13:12 -07:00
|
|
|
|
2016-10-12 06:07:07 -07:00
|
|
|
private:
|
2016-12-08 02:37:58 -08:00
|
|
|
// Set mixing frequency through OutputFrequencyCalculator.
|
|
|
|
|
void CalculateOutputFrequency();
|
|
|
|
|
// Get mixing frequency.
|
2016-10-12 03:06:09 -07:00
|
|
|
int OutputFrequency() const;
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-09-07 07:42:14 -07:00
|
|
|
// Compute what audio sources to mix from audio_source_list_. Ramp
|
|
|
|
|
// in and out. Update mixed status. Mixes up to
|
|
|
|
|
// kMaximumAmountOfMixedAudioSources audio sources.
|
2017-09-07 07:53:45 -07:00
|
|
|
AudioFrameList GetAudioFromSources() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-10-20 14:23:24 -07:00
|
|
|
// The critical section lock guards audio source insertion and
|
|
|
|
|
// removal, which can be done from any thread. The race checker
|
|
|
|
|
// checks that mixing is done sequentially.
|
2016-09-07 06:13:12 -07:00
|
|
|
rtc::CriticalSection crit_;
|
2016-10-20 14:23:24 -07:00
|
|
|
rtc::RaceChecker race_checker_;
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-12-08 02:37:58 -08:00
|
|
|
std::unique_ptr<OutputRateCalculator> output_rate_calculator_;
|
2016-07-04 06:33:02 -07:00
|
|
|
// The current sample frequency and sample size when mixing.
|
2017-09-07 07:53:45 -07:00
|
|
|
int output_frequency_ RTC_GUARDED_BY(race_checker_);
|
|
|
|
|
size_t sample_size_ RTC_GUARDED_BY(race_checker_);
|
2016-07-04 06:33:02 -07:00
|
|
|
|
2016-07-28 03:52:15 -07:00
|
|
|
// List of all audio sources. Note all lists are disjunct
|
2017-09-07 07:53:45 -07:00
|
|
|
SourceStatusList audio_source_list_ RTC_GUARDED_BY(crit_); // May be mixed.
|
2016-07-28 06:36:22 -07:00
|
|
|
|
2017-02-21 05:06:29 -08:00
|
|
|
// Component that handles actual adding of audio frames.
|
2017-09-07 07:53:45 -07:00
|
|
|
FrameCombiner frame_combiner_ RTC_GUARDED_BY(race_checker_);
|
2016-08-24 01:17:12 -07:00
|
|
|
|
2016-09-08 01:25:46 -07:00
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(AudioMixerImpl);
|
2016-07-04 06:33:02 -07:00
|
|
|
};
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#endif // MODULES_AUDIO_MIXER_AUDIO_MIXER_IMPL_H_
|