1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* Copyright (c) 2017 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.
*/
#include "modules/audio_mixer/frame_combiner.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "api/array_view.h"
#include "api/audio/audio_frame.h"
#include "api/audio/audio_view.h"
#include "api/rtp_packet_info.h"
#include "api/rtp_packet_infos.h"
#include "common_audio/include/audio_util.h"
#include "modules/audio_mixer/audio_frame_manipulator.h"
#include "modules/audio_processing/agc2/limiter.h"
#include "modules/audio_processing/logging/apm_data_dumper.h"
#include "rtc_base/checks.h"
namespace webrtc {
namespace {
void SetAudioFrameFields(ArrayView<const AudioFrame* const> mix_list,
size_t number_of_channels,
int sample_rate,
size_t /* number_of_streams */,
AudioFrame* audio_frame_for_mixing) {
const size_t samples_per_channel =
SampleRateToDefaultChannelSize(sample_rate);
// TODO(minyue): Issue bugs.webrtc.org/3390.
// Audio frame timestamp. The 'timestamp_' field is set to dummy
// value '0', because it is only supported in the one channel case and
// is then updated in the helper functions.
audio_frame_for_mixing->UpdateFrame(
0, nullptr, samples_per_channel, sample_rate, AudioFrame::kUndefined,
AudioFrame::kVadUnknown, number_of_channels);
if (mix_list.empty()) {
audio_frame_for_mixing->elapsed_time_ms_ = -1;
} else {
audio_frame_for_mixing->timestamp_ = mix_list[0]->timestamp_;
audio_frame_for_mixing->elapsed_time_ms_ = mix_list[0]->elapsed_time_ms_;
audio_frame_for_mixing->ntp_time_ms_ = mix_list[0]->ntp_time_ms_;
std::vector<RtpPacketInfo> packet_infos;
for (const auto& frame : mix_list) {
audio_frame_for_mixing->timestamp_ =
std::min(audio_frame_for_mixing->timestamp_, frame->timestamp_);
audio_frame_for_mixing->ntp_time_ms_ =
std::min(audio_frame_for_mixing->ntp_time_ms_, frame->ntp_time_ms_);
audio_frame_for_mixing->elapsed_time_ms_ = std::max(
audio_frame_for_mixing->elapsed_time_ms_, frame->elapsed_time_ms_);
packet_infos.insert(packet_infos.end(), frame->packet_infos_.begin(),
frame->packet_infos_.end());
}
audio_frame_for_mixing->packet_infos_ =
RtpPacketInfos(std::move(packet_infos));
}
}
void MixFewFramesWithNoLimiter(ArrayView<const AudioFrame* const> mix_list,
AudioFrame* audio_frame_for_mixing) {
if (mix_list.empty()) {
audio_frame_for_mixing->Mute();
return;
}
RTC_DCHECK_LE(mix_list.size(), 1);
InterleavedView<int16_t> dst = audio_frame_for_mixing->mutable_data(
mix_list[0]->samples_per_channel_, mix_list[0]->num_channels_);
CopySamples(dst, mix_list[0]->data_view());
}
void MixToFloatFrame(ArrayView<const AudioFrame* const> mix_list,
DeinterleavedView<float>& mixing_buffer) {
const size_t number_of_channels = NumChannels(mixing_buffer);
// Clear the mixing buffer.
mixing_buffer.Clear();
// Convert to FloatS16 and mix.
for (size_t i = 0; i < mix_list.size(); ++i) {
InterleavedView<const int16_t> frame_data = mix_list[i]->data_view();
RTC_CHECK(!frame_data.empty());
for (size_t j = 0; j < number_of_channels; ++j) {
MonoView<float> channel = mixing_buffer[j];
for (size_t k = 0; k < SamplesPerChannel(channel); ++k) {
channel[k] += frame_data[number_of_channels * k + j];
}
}
}
}
void RunLimiter(DeinterleavedView<float> deinterleaved, Limiter* limiter) {
limiter->SetSamplesPerChannel(deinterleaved.samples_per_channel());
limiter->Process(deinterleaved);
}
// Both interleaves and rounds.
void InterleaveToAudioFrame(DeinterleavedView<float> deinterleaved,
AudioFrame* audio_frame_for_mixing) {
InterleavedView<int16_t> mixing_data = audio_frame_for_mixing->mutable_data(
deinterleaved.samples_per_channel(), deinterleaved.num_channels());
// Put data in the result frame.
for (size_t i = 0; i < mixing_data.num_channels(); ++i) {
auto channel = deinterleaved[i];
for (size_t j = 0; j < mixing_data.samples_per_channel(); ++j) {
mixing_data[mixing_data.num_channels() * j + i] =
FloatS16ToS16(channel[j]);
}
}
}
} // namespace
constexpr size_t FrameCombiner::kMaximumNumberOfChannels;
constexpr size_t FrameCombiner::kMaximumChannelSize;
FrameCombiner::FrameCombiner(bool use_limiter)
: data_dumper_(new ApmDataDumper(0)),
limiter_(data_dumper_.get(), kMaximumChannelSize, "AudioMixer"),
use_limiter_(use_limiter) {
static_assert(kMaximumChannelSize * kMaximumNumberOfChannels <=
AudioFrame::kMaxDataSizeSamples,
"");
}
FrameCombiner::~FrameCombiner() = default;
void FrameCombiner::Combine(ArrayView<AudioFrame* const> mix_list,
size_t number_of_channels,
int sample_rate,
size_t number_of_streams,
AudioFrame* audio_frame_for_mixing) {
RTC_DCHECK(audio_frame_for_mixing);
RTC_DCHECK_GT(sample_rate, 0);
// Note: `mix_list` is allowed to be empty.
// See FrameCombiner.CombiningZeroFramesShouldProduceSilence.
// Make sure to cap `number_of_channels` to the kMaximumNumberOfChannels
// limits since processing from hereon out will be bound by them.
number_of_channels = std::min(number_of_channels, kMaximumNumberOfChannels);
SetAudioFrameFields(mix_list, number_of_channels, sample_rate,
number_of_streams, audio_frame_for_mixing);
size_t samples_per_channel = SampleRateToDefaultChannelSize(sample_rate);
#if RTC_DCHECK_IS_ON
for (const auto* frame : mix_list) {
RTC_DCHECK_EQ(samples_per_channel, frame->samples_per_channel_);
RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_);
}
#endif
// The 'num_channels_' field of frames in 'mix_list' could be
// different from 'number_of_channels'.
for (auto* frame : mix_list) {
RemixFrame(number_of_channels, frame);
}
if (number_of_streams <= 1) {
MixFewFramesWithNoLimiter(mix_list, audio_frame_for_mixing);
return;
}
// Make sure that the size of the view based on the desired
// `samples_per_channel` and `number_of_channels` doesn't exceed the size of
// the `mixing_buffer_` buffer.
RTC_DCHECK_LE(samples_per_channel, kMaximumChannelSize);
// Since the above check is a DCHECK only, clamp down on `samples_per_channel`
// to make sure we don't exceed the buffer size in non-dcheck builds.
// See also FrameCombinerDeathTest.DebugBuildCrashesWithHighRate.
samples_per_channel = std::min(samples_per_channel, kMaximumChannelSize);
DeinterleavedView<float> deinterleaved(
mixing_buffer_.data(), samples_per_channel, number_of_channels);
MixToFloatFrame(mix_list, deinterleaved);
if (use_limiter_) {
RunLimiter(deinterleaved, &limiter_);
}
InterleaveToAudioFrame(deinterleaved, audio_frame_for_mixing);
}
} // namespace webrtc
|