2017-02-21 05:06:29 -08:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_mixer/frame_combiner.h"
|
2017-02-21 05:06:29 -08:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <array>
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <iterator>
|
2019-09-17 17:06:18 +02:00
|
|
|
#include <memory>
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <string>
|
2017-02-21 05:06:29 -08:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/array_view.h"
|
2018-02-27 13:51:47 +01:00
|
|
|
#include "common_audio/include/audio_util.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_mixer/audio_frame_manipulator.h"
|
|
|
|
|
#include "modules/audio_mixer/audio_mixer_impl.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "modules/audio_processing/include/audio_frame_view.h"
|
2018-06-21 12:04:05 +02:00
|
|
|
#include "modules/audio_processing/include/audio_processing.h"
|
2018-02-27 13:51:47 +01:00
|
|
|
#include "modules/audio_processing/logging/apm_data_dumper.h"
|
2018-03-14 12:27:05 +01:00
|
|
|
#include "rtc_base/arraysize.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2018-03-14 12:27:05 +01:00
|
|
|
#include "system_wrappers/include/metrics.h"
|
2017-02-21 05:06:29 -08:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace {
|
|
|
|
|
|
2019-01-28 16:38:38 +01:00
|
|
|
using MixingBuffer =
|
|
|
|
|
std::array<std::array<float, FrameCombiner::kMaximumChannelSize>,
|
|
|
|
|
FrameCombiner::kMaximumNumberOfChannels>;
|
2017-02-21 05:06:29 -08:00
|
|
|
|
2018-02-27 13:51:47 +01:00
|
|
|
void SetAudioFrameFields(const std::vector<AudioFrame*>& 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 = static_cast<size_t>(
|
|
|
|
|
(sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000);
|
|
|
|
|
|
|
|
|
|
// 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 if (mix_list.size() == 1) {
|
|
|
|
|
audio_frame_for_mixing->timestamp_ = mix_list[0]->timestamp_;
|
|
|
|
|
audio_frame_for_mixing->elapsed_time_ms_ = mix_list[0]->elapsed_time_ms_;
|
2018-10-04 16:21:47 -07:00
|
|
|
audio_frame_for_mixing->ntp_time_ms_ = mix_list[0]->ntp_time_ms_;
|
Reland "Reland "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker.""
This reverts commit fab3460a821abe336ab610c6d6dfc0d392dac263.
Reason for revert: fix downstream instead
Original change's description:
> Revert "Reland "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker.""
>
> This reverts commit 9973933d2e606d64fcdc753acb9ba3afd6e30569.
>
> Reason for revert: breaking downstream projects and not reviewed by direct owners
>
> Original change's description:
> > Reland "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker."
> >
> > This reverts commit 24192c267a40eb7d6b1850489ccdbf7a84f8ff0f.
> >
> > Reason for revert: Analyzed the performance regression in more detail.
> >
> > Most of the regression comes from the extra RtpPacketInfos-related memory allocations in every `NetEq::GetAudio()` call. Commit 1796a820f60cb9429bf4bcf13a40a41794ac8fb0 has removed roughly 2/3rds of the extra allocations from the impacted perf tests. Remaining perf impact is expected to be about "8 microseconds of CPU time per second" on the Linux benchmarking machines and "15 us per second" on Windows/Mac.
> >
> > There are options to optimize further but they are unlikely worth doing. Note for example that `NetEqPerformanceTest` uses the PCM codec while the real-world use cases would likely use the much heavier Opus codec. The numbers from `OpusSpeedTest` and `NetEqPerformanceTest` suggest that Opus decoding is about 10x as expensive as NetEq overall.
> >
> > Original change's description:
> > > Revert "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker."
> > >
> > > This reverts commit 3e8ef940fe86cf6285afb80e68d2a0bedc631b9f.
> > >
> > > Reason for revert: This CL causes a performance regression in NetEq, see https://bugs.chromium.org/p/chromium/issues/detail?id=982260.
> > >
> > > Original change's description:
> > > > Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker.
> > > >
> > > > This change adds the plumbing of RtpPacketInfo from ChannelReceive::OnRtpPacket() to ChannelReceive::GetAudioFrameWithInfo() for audio. It is a step towards replacing the non-spec compliant ContributingSources that updates itself at packet-receive time, with the spec-compliant SourceTracker that will update itself at frame-delivery-to-track time.
> > > >
> > > > Bug: webrtc:10668
> > > > Change-Id: I03385d6865bbc7bfbef7634f88de820a934f787a
> > > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/139890
> > > > Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> > > > Reviewed-by: Minyue Li <minyue@webrtc.org>
> > > > Commit-Queue: Chen Xing <chxg@google.com>
> > > > Cr-Commit-Position: refs/heads/master@{#28434}
> > >
> > > TBR=kwiberg@webrtc.org,stefan@webrtc.org,minyue@webrtc.org,chxg@google.com
> > >
> > > Bug: webrtc:10668, chromium:982260
> > > Change-Id: I5e2cfde78c59d1123e21869564d76ed3f6193a5c
> > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/145339
> > > Reviewed-by: Ivo Creusen <ivoc@webrtc.org>
> > > Commit-Queue: Ivo Creusen <ivoc@webrtc.org>
> > > Cr-Commit-Position: refs/heads/master@{#28561}
> >
> > TBR=kwiberg@webrtc.org,stefan@webrtc.org,ivoc@webrtc.org,minyue@webrtc.org,chxg@google.com
> >
> > # Not skipping CQ checks because original CL landed > 1 day ago.
> >
> > Bug: webrtc:10668, chromium:982260
> > Change-Id: Ie375a0b327ee368317bf3a04b2f1415c3a974470
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146707
> > Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> > Commit-Queue: Chen Xing <chxg@google.com>
> > Cr-Commit-Position: refs/heads/master@{#28664}
>
> TBR=kwiberg@webrtc.org,stefan@webrtc.org,ivoc@webrtc.org,minyue@webrtc.org,chxg@google.com
>
> Change-Id: I652cb0814d83b514d3bee34e65ca3bb693099b22
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:10668, chromium:982260
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146712
> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
> Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#28671}
TBR=alessiob@webrtc.org,kwiberg@webrtc.org,stefan@webrtc.org,ivoc@webrtc.org,minyue@webrtc.org,chxg@google.com
Change-Id: Id43b7b3da79b4f48004b41767482bae1c1fa1e16
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:10668, chromium:982260
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146713
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28672}
2019-07-24 16:47:02 +00:00
|
|
|
audio_frame_for_mixing->packet_infos_ = mix_list[0]->packet_infos_;
|
2018-02-27 13:51:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MixFewFramesWithNoLimiter(const std::vector<AudioFrame*>& mix_list,
|
|
|
|
|
AudioFrame* audio_frame_for_mixing) {
|
|
|
|
|
if (mix_list.empty()) {
|
|
|
|
|
audio_frame_for_mixing->Mute();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
RTC_DCHECK_LE(mix_list.size(), 1);
|
|
|
|
|
std::copy(mix_list[0]->data(),
|
|
|
|
|
mix_list[0]->data() +
|
|
|
|
|
mix_list[0]->num_channels_ * mix_list[0]->samples_per_channel_,
|
|
|
|
|
audio_frame_for_mixing->mutable_data());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 16:38:38 +01:00
|
|
|
void MixToFloatFrame(const std::vector<AudioFrame*>& mix_list,
|
|
|
|
|
size_t samples_per_channel,
|
|
|
|
|
size_t number_of_channels,
|
|
|
|
|
MixingBuffer* mixing_buffer) {
|
|
|
|
|
RTC_DCHECK_LE(samples_per_channel, FrameCombiner::kMaximumChannelSize);
|
|
|
|
|
RTC_DCHECK_LE(number_of_channels, FrameCombiner::kMaximumNumberOfChannels);
|
|
|
|
|
// Clear the mixing buffer.
|
|
|
|
|
for (auto& one_channel_buffer : *mixing_buffer) {
|
|
|
|
|
std::fill(one_channel_buffer.begin(), one_channel_buffer.end(), 0.f);
|
|
|
|
|
}
|
2018-02-27 13:51:47 +01:00
|
|
|
|
2019-01-28 16:38:38 +01:00
|
|
|
// Convert to FloatS16 and mix.
|
2018-02-27 13:51:47 +01:00
|
|
|
for (size_t i = 0; i < mix_list.size(); ++i) {
|
|
|
|
|
const AudioFrame* const frame = mix_list[i];
|
2019-01-28 16:38:38 +01:00
|
|
|
for (size_t j = 0; j < std::min(number_of_channels,
|
|
|
|
|
FrameCombiner::kMaximumNumberOfChannels);
|
|
|
|
|
++j) {
|
|
|
|
|
for (size_t k = 0; k < std::min(samples_per_channel,
|
|
|
|
|
FrameCombiner::kMaximumChannelSize);
|
|
|
|
|
++k) {
|
|
|
|
|
(*mixing_buffer)[j][k] += frame->data()[number_of_channels * k + j];
|
2018-02-27 13:51:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-01 21:31:38 +01:00
|
|
|
void RunLimiter(AudioFrameView<float> mixing_buffer_view, Limiter* limiter) {
|
2018-02-27 13:51:47 +01:00
|
|
|
const size_t sample_rate = mixing_buffer_view.samples_per_channel() * 1000 /
|
|
|
|
|
AudioMixerImpl::kFrameDurationInMs;
|
2018-11-01 21:31:38 +01:00
|
|
|
// TODO(alessiob): Avoid calling SetSampleRate every time.
|
2018-06-21 12:04:05 +02:00
|
|
|
limiter->SetSampleRate(sample_rate);
|
|
|
|
|
limiter->Process(mixing_buffer_view);
|
2018-02-27 13:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Both interleaves and rounds.
|
|
|
|
|
void InterleaveToAudioFrame(AudioFrameView<const float> mixing_buffer_view,
|
|
|
|
|
AudioFrame* audio_frame_for_mixing) {
|
|
|
|
|
const size_t number_of_channels = mixing_buffer_view.num_channels();
|
|
|
|
|
const size_t samples_per_channel = mixing_buffer_view.samples_per_channel();
|
|
|
|
|
// Put data in the result frame.
|
|
|
|
|
for (size_t i = 0; i < number_of_channels; ++i) {
|
|
|
|
|
for (size_t j = 0; j < samples_per_channel; ++j) {
|
|
|
|
|
audio_frame_for_mixing->mutable_data()[number_of_channels * j + i] =
|
|
|
|
|
FloatS16ToS16(mixing_buffer_view.channel(i)[j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-21 05:06:29 -08:00
|
|
|
} // namespace
|
|
|
|
|
|
2019-01-28 16:38:38 +01:00
|
|
|
constexpr size_t FrameCombiner::kMaximumNumberOfChannels;
|
|
|
|
|
constexpr size_t FrameCombiner::kMaximumChannelSize;
|
|
|
|
|
|
2018-02-27 13:51:47 +01:00
|
|
|
FrameCombiner::FrameCombiner(bool use_limiter)
|
2018-06-21 12:04:05 +02:00
|
|
|
: data_dumper_(new ApmDataDumper(0)),
|
2019-01-28 16:38:38 +01:00
|
|
|
mixing_buffer_(
|
2019-09-17 17:06:18 +02:00
|
|
|
std::make_unique<std::array<std::array<float, kMaximumChannelSize>,
|
|
|
|
|
kMaximumNumberOfChannels>>()),
|
2018-11-01 21:31:38 +01:00
|
|
|
limiter_(static_cast<size_t>(48000), data_dumper_.get(), "AudioMixer"),
|
2019-01-28 16:38:38 +01:00
|
|
|
use_limiter_(use_limiter) {
|
|
|
|
|
static_assert(kMaximumChannelSize * kMaximumNumberOfChannels <=
|
|
|
|
|
AudioFrame::kMaxDataSizeSamples,
|
|
|
|
|
"");
|
|
|
|
|
}
|
2017-02-21 05:06:29 -08:00
|
|
|
|
|
|
|
|
FrameCombiner::~FrameCombiner() = default;
|
|
|
|
|
|
|
|
|
|
void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list,
|
|
|
|
|
size_t number_of_channels,
|
|
|
|
|
int sample_rate,
|
2017-03-29 04:25:16 -07:00
|
|
|
size_t number_of_streams,
|
2018-02-27 13:51:47 +01:00
|
|
|
AudioFrame* audio_frame_for_mixing) {
|
2017-02-21 05:06:29 -08:00
|
|
|
RTC_DCHECK(audio_frame_for_mixing);
|
2018-03-19 14:32:05 +01:00
|
|
|
|
|
|
|
|
LogMixingStats(mix_list, sample_rate, number_of_streams);
|
|
|
|
|
|
2018-02-27 13:51:47 +01:00
|
|
|
SetAudioFrameFields(mix_list, number_of_channels, sample_rate,
|
|
|
|
|
number_of_streams, audio_frame_for_mixing);
|
|
|
|
|
|
2017-02-21 05:06:29 -08:00
|
|
|
const size_t samples_per_channel = static_cast<size_t>(
|
|
|
|
|
(sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000);
|
|
|
|
|
|
|
|
|
|
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_);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 13:51:47 +01:00
|
|
|
// The 'num_channels_' field of frames in 'mix_list' could be
|
|
|
|
|
// different from 'number_of_channels'.
|
2017-02-21 05:06:29 -08:00
|
|
|
for (auto* frame : mix_list) {
|
|
|
|
|
RemixFrame(number_of_channels, frame);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 13:51:47 +01:00
|
|
|
if (number_of_streams <= 1) {
|
|
|
|
|
MixFewFramesWithNoLimiter(mix_list, audio_frame_for_mixing);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-03-29 04:25:16 -07:00
|
|
|
|
2019-01-28 16:38:38 +01:00
|
|
|
MixToFloatFrame(mix_list, samples_per_channel, number_of_channels,
|
|
|
|
|
mixing_buffer_.get());
|
|
|
|
|
|
|
|
|
|
const size_t output_number_of_channels =
|
|
|
|
|
std::min(number_of_channels, kMaximumNumberOfChannels);
|
|
|
|
|
const size_t output_samples_per_channel =
|
|
|
|
|
std::min(samples_per_channel, kMaximumChannelSize);
|
2018-02-26 13:25:39 +01:00
|
|
|
|
2018-02-27 13:51:47 +01:00
|
|
|
// Put float data in an AudioFrameView.
|
2019-01-28 16:38:38 +01:00
|
|
|
std::array<float*, kMaximumNumberOfChannels> channel_pointers{};
|
|
|
|
|
for (size_t i = 0; i < output_number_of_channels; ++i) {
|
|
|
|
|
channel_pointers[i] = &(*mixing_buffer_.get())[i][0];
|
2018-02-27 13:51:47 +01:00
|
|
|
}
|
2019-01-28 16:38:38 +01:00
|
|
|
AudioFrameView<float> mixing_buffer_view(&channel_pointers[0],
|
|
|
|
|
output_number_of_channels,
|
|
|
|
|
output_samples_per_channel);
|
2018-02-27 13:51:47 +01:00
|
|
|
|
2018-06-21 12:04:05 +02:00
|
|
|
if (use_limiter_) {
|
|
|
|
|
RunLimiter(mixing_buffer_view, &limiter_);
|
2017-02-21 05:06:29 -08:00
|
|
|
}
|
2018-02-27 13:51:47 +01:00
|
|
|
|
|
|
|
|
InterleaveToAudioFrame(mixing_buffer_view, audio_frame_for_mixing);
|
2018-03-14 12:27:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FrameCombiner::LogMixingStats(const std::vector<AudioFrame*>& mix_list,
|
|
|
|
|
int sample_rate,
|
|
|
|
|
size_t number_of_streams) const {
|
|
|
|
|
// Log every second.
|
|
|
|
|
uma_logging_counter_++;
|
|
|
|
|
if (uma_logging_counter_ > 1000 / AudioMixerImpl::kFrameDurationInMs) {
|
|
|
|
|
uma_logging_counter_ = 0;
|
|
|
|
|
RTC_HISTOGRAM_COUNTS_100("WebRTC.Audio.AudioMixer.NumIncomingStreams",
|
|
|
|
|
static_cast<int>(number_of_streams));
|
|
|
|
|
RTC_HISTOGRAM_ENUMERATION(
|
|
|
|
|
"WebRTC.Audio.AudioMixer.NumIncomingActiveStreams",
|
|
|
|
|
static_cast<int>(mix_list.size()),
|
|
|
|
|
AudioMixerImpl::kMaximumAmountOfMixedAudioSources);
|
|
|
|
|
|
|
|
|
|
using NativeRate = AudioProcessing::NativeRate;
|
|
|
|
|
static constexpr NativeRate native_rates[] = {
|
|
|
|
|
NativeRate::kSampleRate8kHz, NativeRate::kSampleRate16kHz,
|
|
|
|
|
NativeRate::kSampleRate32kHz, NativeRate::kSampleRate48kHz};
|
|
|
|
|
const auto* rate_position = std::lower_bound(
|
|
|
|
|
std::begin(native_rates), std::end(native_rates), sample_rate);
|
|
|
|
|
|
|
|
|
|
RTC_HISTOGRAM_ENUMERATION(
|
|
|
|
|
"WebRTC.Audio.AudioMixer.MixingRate",
|
|
|
|
|
std::distance(std::begin(native_rates), rate_position),
|
|
|
|
|
arraysize(native_rates));
|
|
|
|
|
}
|
2017-02-21 05:06:29 -08:00
|
|
|
}
|
2017-06-12 12:45:32 -07:00
|
|
|
|
2017-02-21 05:06:29 -08:00
|
|
|
} // namespace webrtc
|