Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-22 14:04:28 +01:00
parent 81b91f4139
commit f8c34fa5ee
22732 changed files with 4815320 additions and 2 deletions

View file

@ -0,0 +1,279 @@
/*
* Copyright (c) 2014 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_coding/codecs/red/audio_encoder_copy_red.h"
#include <string.h>
#include <utility>
#include <vector>
#include "absl/strings/string_view.h"
#include "rtc_base/byte_order.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
namespace webrtc {
static constexpr const int kRedMaxPacketSize =
1 << 10; // RED packets must be less than 1024 bytes to fit the 10 bit
// block length.
static constexpr const size_t kRedMaxTimestampDelta =
1 << 14; // RED packets can encode a timestamp delta of 14 bits.
static constexpr const size_t kAudioMaxRtpPacketLen =
1200; // The typical MTU is 1200 bytes.
static constexpr size_t kRedHeaderLength = 4; // 4 bytes RED header.
static constexpr size_t kRedLastHeaderLength =
1; // reduced size for last RED header.
static constexpr size_t kRedNumberOfRedundantEncodings =
1; // The level of redundancy we support.
AudioEncoderCopyRed::Config::Config() = default;
AudioEncoderCopyRed::Config::Config(Config&&) = default;
AudioEncoderCopyRed::Config::~Config() = default;
size_t GetMaxRedundancyFromFieldTrial(const FieldTrialsView& field_trials) {
const std::string red_trial =
field_trials.Lookup("WebRTC-Audio-Red-For-Opus");
size_t redundancy = 0;
if (sscanf(red_trial.c_str(), "Enabled-%zu", &redundancy) != 1 ||
redundancy > 9) {
return kRedNumberOfRedundantEncodings;
}
return redundancy;
}
AudioEncoderCopyRed::AudioEncoderCopyRed(Config&& config,
const FieldTrialsView& field_trials)
: speech_encoder_(std::move(config.speech_encoder)),
primary_encoded_(0, kAudioMaxRtpPacketLen),
max_packet_length_(kAudioMaxRtpPacketLen),
red_payload_type_(config.payload_type) {
RTC_CHECK(speech_encoder_) << "Speech encoder not provided.";
auto number_of_redundant_encodings =
GetMaxRedundancyFromFieldTrial(field_trials);
for (size_t i = 0; i < number_of_redundant_encodings; i++) {
std::pair<EncodedInfo, rtc::Buffer> redundant;
redundant.second.EnsureCapacity(kAudioMaxRtpPacketLen);
redundant_encodings_.push_front(std::move(redundant));
}
}
AudioEncoderCopyRed::~AudioEncoderCopyRed() = default;
int AudioEncoderCopyRed::SampleRateHz() const {
return speech_encoder_->SampleRateHz();
}
size_t AudioEncoderCopyRed::NumChannels() const {
return speech_encoder_->NumChannels();
}
int AudioEncoderCopyRed::RtpTimestampRateHz() const {
return speech_encoder_->RtpTimestampRateHz();
}
size_t AudioEncoderCopyRed::Num10MsFramesInNextPacket() const {
return speech_encoder_->Num10MsFramesInNextPacket();
}
size_t AudioEncoderCopyRed::Max10MsFramesInAPacket() const {
return speech_encoder_->Max10MsFramesInAPacket();
}
int AudioEncoderCopyRed::GetTargetBitrate() const {
return speech_encoder_->GetTargetBitrate();
}
AudioEncoder::EncodedInfo AudioEncoderCopyRed::EncodeImpl(
uint32_t rtp_timestamp,
rtc::ArrayView<const int16_t> audio,
rtc::Buffer* encoded) {
primary_encoded_.Clear();
EncodedInfo info =
speech_encoder_->Encode(rtp_timestamp, audio, &primary_encoded_);
RTC_CHECK(info.redundant.empty()) << "Cannot use nested redundant encoders.";
RTC_DCHECK_EQ(primary_encoded_.size(), info.encoded_bytes);
if (info.encoded_bytes == 0) {
return info;
}
if (info.encoded_bytes >= kRedMaxPacketSize) {
// Fallback to the primary encoding if the encoded size is more than
// what RED can encode as redundancy (1024 bytes). This can happen with
// Opus stereo at the highest bitrate which consumes up to 1276 bytes.
encoded->AppendData(primary_encoded_);
return info;
}
RTC_DCHECK_GT(max_packet_length_, info.encoded_bytes);
size_t header_length_bytes = kRedLastHeaderLength;
size_t bytes_available = max_packet_length_ - info.encoded_bytes;
auto it = redundant_encodings_.begin();
// Determine how much redundancy we can fit into our packet by
// iterating forward. This is determined both by the length as well
// as the timestamp difference. The latter can occur with opus DTX which
// has timestamp gaps of 400ms which exceeds REDs timestamp delta field size.
for (; it != redundant_encodings_.end(); it++) {
if (bytes_available < kRedHeaderLength + it->first.encoded_bytes) {
break;
}
if (it->first.encoded_bytes == 0) {
break;
}
if (rtp_timestamp - it->first.encoded_timestamp >= kRedMaxTimestampDelta) {
break;
}
bytes_available -= kRedHeaderLength + it->first.encoded_bytes;
header_length_bytes += kRedHeaderLength;
}
// Allocate room for RFC 2198 header.
encoded->SetSize(header_length_bytes);
// Iterate backwards and append the data.
size_t header_offset = 0;
while (it-- != redundant_encodings_.begin()) {
encoded->AppendData(it->second);
const uint32_t timestamp_delta =
info.encoded_timestamp - it->first.encoded_timestamp;
encoded->data()[header_offset] = it->first.payload_type | 0x80;
rtc::SetBE16(static_cast<uint8_t*>(encoded->data()) + header_offset + 1,
(timestamp_delta << 2) | (it->first.encoded_bytes >> 8));
encoded->data()[header_offset + 3] = it->first.encoded_bytes & 0xff;
header_offset += kRedHeaderLength;
info.redundant.push_back(it->first);
}
// `info` will be implicitly cast to an EncodedInfoLeaf struct, effectively
// discarding the (empty) vector of redundant information. This is
// intentional.
if (header_length_bytes > kRedHeaderLength) {
info.redundant.push_back(info);
RTC_DCHECK_EQ(info.speech,
info.redundant[info.redundant.size() - 1].speech);
}
encoded->AppendData(primary_encoded_);
RTC_DCHECK_EQ(header_offset, header_length_bytes - 1);
encoded->data()[header_offset] = info.payload_type;
// Shift the redundant encodings.
auto rit = redundant_encodings_.rbegin();
for (auto next = std::next(rit); next != redundant_encodings_.rend();
rit++, next = std::next(rit)) {
rit->first = next->first;
rit->second.SetData(next->second);
}
it = redundant_encodings_.begin();
if (it != redundant_encodings_.end()) {
it->first = info;
it->second.SetData(primary_encoded_);
}
// Update main EncodedInfo.
info.payload_type = red_payload_type_;
info.encoded_bytes = encoded->size();
return info;
}
void AudioEncoderCopyRed::Reset() {
speech_encoder_->Reset();
auto number_of_redundant_encodings = redundant_encodings_.size();
redundant_encodings_.clear();
for (size_t i = 0; i < number_of_redundant_encodings; i++) {
std::pair<EncodedInfo, rtc::Buffer> redundant;
redundant.second.EnsureCapacity(kAudioMaxRtpPacketLen);
redundant_encodings_.push_front(std::move(redundant));
}
}
bool AudioEncoderCopyRed::SetFec(bool enable) {
return speech_encoder_->SetFec(enable);
}
bool AudioEncoderCopyRed::SetDtx(bool enable) {
return speech_encoder_->SetDtx(enable);
}
bool AudioEncoderCopyRed::GetDtx() const {
return speech_encoder_->GetDtx();
}
bool AudioEncoderCopyRed::SetApplication(Application application) {
return speech_encoder_->SetApplication(application);
}
void AudioEncoderCopyRed::SetMaxPlaybackRate(int frequency_hz) {
speech_encoder_->SetMaxPlaybackRate(frequency_hz);
}
bool AudioEncoderCopyRed::EnableAudioNetworkAdaptor(
const std::string& config_string,
RtcEventLog* event_log) {
return speech_encoder_->EnableAudioNetworkAdaptor(config_string, event_log);
}
void AudioEncoderCopyRed::DisableAudioNetworkAdaptor() {
speech_encoder_->DisableAudioNetworkAdaptor();
}
void AudioEncoderCopyRed::OnReceivedUplinkPacketLossFraction(
float uplink_packet_loss_fraction) {
speech_encoder_->OnReceivedUplinkPacketLossFraction(
uplink_packet_loss_fraction);
}
void AudioEncoderCopyRed::OnReceivedUplinkBandwidth(
int target_audio_bitrate_bps,
absl::optional<int64_t> bwe_period_ms) {
speech_encoder_->OnReceivedUplinkBandwidth(target_audio_bitrate_bps,
bwe_period_ms);
}
void AudioEncoderCopyRed::OnReceivedUplinkAllocation(
BitrateAllocationUpdate update) {
speech_encoder_->OnReceivedUplinkAllocation(update);
}
absl::optional<std::pair<TimeDelta, TimeDelta>>
AudioEncoderCopyRed::GetFrameLengthRange() const {
return speech_encoder_->GetFrameLengthRange();
}
void AudioEncoderCopyRed::OnReceivedRtt(int rtt_ms) {
speech_encoder_->OnReceivedRtt(rtt_ms);
}
void AudioEncoderCopyRed::OnReceivedOverhead(size_t overhead_bytes_per_packet) {
max_packet_length_ = kAudioMaxRtpPacketLen - overhead_bytes_per_packet;
return speech_encoder_->OnReceivedOverhead(overhead_bytes_per_packet);
}
void AudioEncoderCopyRed::SetReceiverFrameLengthRange(int min_frame_length_ms,
int max_frame_length_ms) {
return speech_encoder_->SetReceiverFrameLengthRange(min_frame_length_ms,
max_frame_length_ms);
}
ANAStats AudioEncoderCopyRed::GetANAStats() const {
return speech_encoder_->GetANAStats();
}
rtc::ArrayView<std::unique_ptr<AudioEncoder>>
AudioEncoderCopyRed::ReclaimContainedEncoders() {
return rtc::ArrayView<std::unique_ptr<AudioEncoder>>(&speech_encoder_, 1);
}
} // namespace webrtc

View file

@ -0,0 +1,102 @@
/*
* Copyright (c) 2014 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.
*/
#ifndef MODULES_AUDIO_CODING_CODECS_RED_AUDIO_ENCODER_COPY_RED_H_
#define MODULES_AUDIO_CODING_CODECS_RED_AUDIO_ENCODER_COPY_RED_H_
#include <stddef.h>
#include <stdint.h>
#include <list>
#include <memory>
#include <utility>
#include "absl/types/optional.h"
#include "api/array_view.h"
#include "api/audio_codecs/audio_encoder.h"
#include "api/field_trials_view.h"
#include "api/units/time_delta.h"
#include "rtc_base/buffer.h"
namespace webrtc {
// This class implements redundant audio coding as described in
// https://tools.ietf.org/html/rfc2198
// The class object will have an underlying AudioEncoder object that performs
// the actual encodings. The current class will gather the N latest encodings
// from the underlying codec into one packet. Currently N is hard-coded to 2.
class AudioEncoderCopyRed final : public AudioEncoder {
public:
struct Config {
Config();
Config(Config&&);
~Config();
int payload_type;
std::unique_ptr<AudioEncoder> speech_encoder;
};
AudioEncoderCopyRed(Config&& config, const FieldTrialsView& field_trials);
~AudioEncoderCopyRed() override;
AudioEncoderCopyRed(const AudioEncoderCopyRed&) = delete;
AudioEncoderCopyRed& operator=(const AudioEncoderCopyRed&) = delete;
int SampleRateHz() const override;
size_t NumChannels() const override;
int RtpTimestampRateHz() const override;
size_t Num10MsFramesInNextPacket() const override;
size_t Max10MsFramesInAPacket() const override;
int GetTargetBitrate() const override;
void Reset() override;
bool SetFec(bool enable) override;
bool SetDtx(bool enable) override;
bool GetDtx() const override;
bool SetApplication(Application application) override;
void SetMaxPlaybackRate(int frequency_hz) override;
bool EnableAudioNetworkAdaptor(const std::string& config_string,
RtcEventLog* event_log) override;
void DisableAudioNetworkAdaptor() override;
void OnReceivedUplinkPacketLossFraction(
float uplink_packet_loss_fraction) override;
void OnReceivedUplinkBandwidth(
int target_audio_bitrate_bps,
absl::optional<int64_t> bwe_period_ms) override;
void OnReceivedUplinkAllocation(BitrateAllocationUpdate update) override;
void OnReceivedRtt(int rtt_ms) override;
void OnReceivedOverhead(size_t overhead_bytes_per_packet) override;
void SetReceiverFrameLengthRange(int min_frame_length_ms,
int max_frame_length_ms) override;
ANAStats GetANAStats() const override;
absl::optional<std::pair<TimeDelta, TimeDelta>> GetFrameLengthRange()
const override;
rtc::ArrayView<std::unique_ptr<AudioEncoder>> ReclaimContainedEncoders()
override;
protected:
EncodedInfo EncodeImpl(uint32_t rtp_timestamp,
rtc::ArrayView<const int16_t> audio,
rtc::Buffer* encoded) override;
private:
std::unique_ptr<AudioEncoder> speech_encoder_;
rtc::Buffer primary_encoded_;
size_t max_packet_length_;
int red_payload_type_;
std::list<std::pair<EncodedInfo, rtc::Buffer>> redundant_encodings_;
};
} // namespace webrtc
#endif // MODULES_AUDIO_CODING_CODECS_RED_AUDIO_ENCODER_COPY_RED_H_

View file

@ -0,0 +1,658 @@
/*
* Copyright (c) 2014 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_coding/codecs/red/audio_encoder_copy_red.h"
#include <memory>
#include <vector>
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "test/field_trial.h"
#include "test/gtest.h"
#include "test/mock_audio_encoder.h"
#include "test/scoped_key_value_config.h"
#include "test/testsupport/rtc_expect_death.h"
using ::testing::_;
using ::testing::Eq;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::MockFunction;
using ::testing::Not;
using ::testing::Optional;
using ::testing::Return;
using ::testing::SetArgPointee;
namespace webrtc {
namespace {
static const size_t kMaxNumSamples = 48 * 10 * 2; // 10 ms @ 48 kHz stereo.
static const size_t kRedLastHeaderLength =
1; // 1 byte RED header for the last element.
} // namespace
class AudioEncoderCopyRedTest : public ::testing::Test {
protected:
AudioEncoderCopyRedTest()
: mock_encoder_(new MockAudioEncoder),
timestamp_(4711),
sample_rate_hz_(16000),
num_audio_samples_10ms(sample_rate_hz_ / 100),
red_payload_type_(63) {
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type_;
config.speech_encoder = std::unique_ptr<AudioEncoder>(mock_encoder_);
red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials_));
memset(audio_, 0, sizeof(audio_));
EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(1U));
EXPECT_CALL(*mock_encoder_, SampleRateHz())
.WillRepeatedly(Return(sample_rate_hz_));
}
void TearDown() override { red_.reset(); }
void Encode() {
ASSERT_TRUE(red_.get() != NULL);
encoded_.Clear();
encoded_info_ = red_->Encode(
timestamp_,
rtc::ArrayView<const int16_t>(audio_, num_audio_samples_10ms),
&encoded_);
timestamp_ += rtc::checked_cast<uint32_t>(num_audio_samples_10ms);
}
test::ScopedKeyValueConfig field_trials_;
MockAudioEncoder* mock_encoder_;
std::unique_ptr<AudioEncoderCopyRed> red_;
uint32_t timestamp_;
int16_t audio_[kMaxNumSamples];
const int sample_rate_hz_;
size_t num_audio_samples_10ms;
rtc::Buffer encoded_;
AudioEncoder::EncodedInfo encoded_info_;
const int red_payload_type_;
};
TEST_F(AudioEncoderCopyRedTest, CreateAndDestroy) {}
TEST_F(AudioEncoderCopyRedTest, CheckSampleRatePropagation) {
EXPECT_CALL(*mock_encoder_, SampleRateHz()).WillOnce(Return(17));
EXPECT_EQ(17, red_->SampleRateHz());
}
TEST_F(AudioEncoderCopyRedTest, CheckNumChannelsPropagation) {
EXPECT_CALL(*mock_encoder_, NumChannels()).WillOnce(Return(17U));
EXPECT_EQ(17U, red_->NumChannels());
}
TEST_F(AudioEncoderCopyRedTest, CheckFrameSizePropagation) {
EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
.WillOnce(Return(17U));
EXPECT_EQ(17U, red_->Num10MsFramesInNextPacket());
}
TEST_F(AudioEncoderCopyRedTest, CheckMaxFrameSizePropagation) {
EXPECT_CALL(*mock_encoder_, Max10MsFramesInAPacket()).WillOnce(Return(17U));
EXPECT_EQ(17U, red_->Max10MsFramesInAPacket());
}
TEST_F(AudioEncoderCopyRedTest, CheckTargetAudioBitratePropagation) {
EXPECT_CALL(*mock_encoder_,
OnReceivedUplinkBandwidth(4711, absl::optional<int64_t>()));
red_->OnReceivedUplinkBandwidth(4711, absl::nullopt);
}
TEST_F(AudioEncoderCopyRedTest, CheckPacketLossFractionPropagation) {
EXPECT_CALL(*mock_encoder_, OnReceivedUplinkPacketLossFraction(0.5));
red_->OnReceivedUplinkPacketLossFraction(0.5);
}
TEST_F(AudioEncoderCopyRedTest, CheckGetFrameLengthRangePropagation) {
auto expected_range =
std::make_pair(TimeDelta::Millis(20), TimeDelta::Millis(20));
EXPECT_CALL(*mock_encoder_, GetFrameLengthRange())
.WillRepeatedly(Return(absl::make_optional(expected_range)));
EXPECT_THAT(red_->GetFrameLengthRange(), Optional(Eq(expected_range)));
}
// Checks that the an Encode() call is immediately propagated to the speech
// encoder.
TEST_F(AudioEncoderCopyRedTest, CheckImmediateEncode) {
// Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
// check ensures that exactly one call to EncodeImpl happens in each
// Encode call.
InSequence s;
MockFunction<void(int check_point_id)> check;
for (int i = 1; i <= 6; ++i) {
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillRepeatedly(Return(AudioEncoder::EncodedInfo()));
EXPECT_CALL(check, Call(i));
Encode();
check.Call(i);
}
}
// Checks that no output is produced if the underlying codec doesn't emit any
// new data, even if the RED codec is loaded with a secondary encoding.
TEST_F(AudioEncoderCopyRedTest, CheckNoOutput) {
static const size_t kEncodedSize = 17;
static const size_t kHeaderLenBytes = 5;
{
InSequence s;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(kEncodedSize)))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(0)))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(kEncodedSize)));
}
// Start with one Encode() call that will produce output.
Encode();
// First call is a special case, since it does not include a secondary
// payload.
EXPECT_EQ(0u, encoded_info_.redundant.size());
EXPECT_EQ(kEncodedSize + kRedLastHeaderLength, encoded_info_.encoded_bytes);
// Next call to the speech encoder will not produce any output.
Encode();
EXPECT_EQ(0u, encoded_info_.encoded_bytes);
// Final call to the speech encoder will produce output.
Encode();
EXPECT_EQ(2 * kEncodedSize + kHeaderLenBytes, encoded_info_.encoded_bytes);
ASSERT_EQ(2u, encoded_info_.redundant.size());
}
// Checks that the correct payload sizes are populated into the redundancy
// information for a redundancy level of 1.
TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes1) {
// Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
// of calls.
static const int kNumPackets = 10;
InSequence s;
for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
}
// First call is a special case, since it does not include a secondary
// payload.
Encode();
EXPECT_EQ(0u, encoded_info_.redundant.size());
EXPECT_EQ(kRedLastHeaderLength + 1u, encoded_info_.encoded_bytes);
for (size_t i = 2; i <= kNumPackets; ++i) {
Encode();
ASSERT_EQ(2u, encoded_info_.redundant.size());
EXPECT_EQ(i, encoded_info_.redundant[1].encoded_bytes);
EXPECT_EQ(i - 1, encoded_info_.redundant[0].encoded_bytes);
EXPECT_EQ(5 + i + (i - 1), encoded_info_.encoded_bytes);
}
}
// Checks that the correct payload sizes are populated into the redundancy
// information for a redundancy level of 0.
TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes0) {
webrtc::test::ScopedKeyValueConfig field_trials(
field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-0/");
// Recreate the RED encoder to take the new field trial setting into account.
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type_;
config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
// Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
// of calls.
static const int kNumPackets = 10;
InSequence s;
for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
}
for (size_t i = 1; i <= kNumPackets; ++i) {
Encode();
ASSERT_EQ(0u, encoded_info_.redundant.size());
EXPECT_EQ(1 + i, encoded_info_.encoded_bytes);
}
}
// Checks that the correct payload sizes are populated into the redundancy
// information for a redundancy level of 2.
TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes2) {
webrtc::test::ScopedKeyValueConfig field_trials(
field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-2/");
// Recreate the RED encoder to take the new field trial setting into account.
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type_;
config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
// Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
// of calls.
static const int kNumPackets = 10;
InSequence s;
for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
}
// First call is a special case, since it does not include a secondary
// payload.
Encode();
EXPECT_EQ(0u, encoded_info_.redundant.size());
EXPECT_EQ(kRedLastHeaderLength + 1u, encoded_info_.encoded_bytes);
// Second call is also special since it does not include a tertiary
// payload.
Encode();
EXPECT_EQ(2u, encoded_info_.redundant.size());
EXPECT_EQ(8u, encoded_info_.encoded_bytes);
for (size_t i = 3; i <= kNumPackets; ++i) {
Encode();
ASSERT_EQ(3u, encoded_info_.redundant.size());
EXPECT_EQ(i, encoded_info_.redundant[2].encoded_bytes);
EXPECT_EQ(i - 1, encoded_info_.redundant[1].encoded_bytes);
EXPECT_EQ(i - 2, encoded_info_.redundant[0].encoded_bytes);
EXPECT_EQ(9 + i + (i - 1) + (i - 2), encoded_info_.encoded_bytes);
}
}
// Checks that the correct payload sizes are populated into the redundancy
// information for a redundancy level of 3.
TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes3) {
webrtc::test::ScopedKeyValueConfig field_trials(
field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-3/");
// Recreate the RED encoder to take the new field trial setting into account.
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type_;
config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials_));
// Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
// of calls.
static const int kNumPackets = 10;
InSequence s;
for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
}
// First call is a special case, since it does not include a secondary
// payload.
Encode();
EXPECT_EQ(0u, encoded_info_.redundant.size());
EXPECT_EQ(kRedLastHeaderLength + 1u, encoded_info_.encoded_bytes);
// Second call is also special since it does not include a tertiary
// payload.
Encode();
EXPECT_EQ(2u, encoded_info_.redundant.size());
EXPECT_EQ(8u, encoded_info_.encoded_bytes);
// Third call is also special since it does not include a quaternary
// payload.
Encode();
EXPECT_EQ(3u, encoded_info_.redundant.size());
EXPECT_EQ(15u, encoded_info_.encoded_bytes);
for (size_t i = 4; i <= kNumPackets; ++i) {
Encode();
ASSERT_EQ(4u, encoded_info_.redundant.size());
EXPECT_EQ(i, encoded_info_.redundant[3].encoded_bytes);
EXPECT_EQ(i - 1, encoded_info_.redundant[2].encoded_bytes);
EXPECT_EQ(i - 2, encoded_info_.redundant[1].encoded_bytes);
EXPECT_EQ(i - 3, encoded_info_.redundant[0].encoded_bytes);
EXPECT_EQ(13 + i + (i - 1) + (i - 2) + (i - 3),
encoded_info_.encoded_bytes);
}
}
// Checks that packets encoded larger than REDs 1024 maximum are returned as-is.
TEST_F(AudioEncoderCopyRedTest, VeryLargePacket) {
AudioEncoder::EncodedInfo info;
info.payload_type = 63;
info.encoded_bytes =
1111; // Must be > 1024 which is the maximum size encodable by RED.
info.encoded_timestamp = timestamp_;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
ASSERT_EQ(0u, encoded_info_.redundant.size());
ASSERT_EQ(info.encoded_bytes, encoded_info_.encoded_bytes);
ASSERT_EQ(info.payload_type, encoded_info_.payload_type);
}
// Checks that the correct timestamps are returned.
TEST_F(AudioEncoderCopyRedTest, CheckTimestamps) {
uint32_t primary_timestamp = timestamp_;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 17;
info.encoded_timestamp = timestamp_;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
// First call is a special case, since it does not include a secondary
// payload.
Encode();
EXPECT_EQ(primary_timestamp, encoded_info_.encoded_timestamp);
uint32_t secondary_timestamp = primary_timestamp;
primary_timestamp = timestamp_;
info.encoded_timestamp = timestamp_;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
ASSERT_EQ(2u, encoded_info_.redundant.size());
EXPECT_EQ(primary_timestamp, encoded_info_.redundant[1].encoded_timestamp);
EXPECT_EQ(secondary_timestamp, encoded_info_.redundant[0].encoded_timestamp);
EXPECT_EQ(primary_timestamp, encoded_info_.encoded_timestamp);
}
// Checks that the primary and secondary payloads are written correctly.
TEST_F(AudioEncoderCopyRedTest, CheckPayloads) {
// Let the mock encoder write payloads with increasing values. The first
// payload will have values 0, 1, 2, ..., kPayloadLenBytes - 1.
static const size_t kPayloadLenBytes = 5;
static const size_t kHeaderLenBytes = 5;
uint8_t payload[kPayloadLenBytes];
for (uint8_t i = 0; i < kPayloadLenBytes; ++i) {
payload[i] = i;
}
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillRepeatedly(Invoke(MockAudioEncoder::CopyEncoding(payload)));
// First call is a special case, since it does not include a secondary
// payload.
Encode();
EXPECT_EQ(kRedLastHeaderLength + kPayloadLenBytes,
encoded_info_.encoded_bytes);
for (size_t i = 0; i < kPayloadLenBytes; ++i) {
EXPECT_EQ(i, encoded_.data()[kRedLastHeaderLength + i]);
}
for (int j = 0; j < 1; ++j) {
// Increment all values of the payload by 10.
for (size_t i = 0; i < kPayloadLenBytes; ++i)
payload[i] += 10;
Encode();
ASSERT_EQ(2u, encoded_info_.redundant.size());
EXPECT_EQ(kPayloadLenBytes, encoded_info_.redundant[0].encoded_bytes);
EXPECT_EQ(kPayloadLenBytes, encoded_info_.redundant[1].encoded_bytes);
for (size_t i = 0; i < kPayloadLenBytes; ++i) {
// Check secondary payload.
EXPECT_EQ(j * 10 + i, encoded_.data()[kHeaderLenBytes + i]);
// Check primary payload.
EXPECT_EQ((j + 1) * 10 + i,
encoded_.data()[kHeaderLenBytes + i + kPayloadLenBytes]);
}
}
}
// Checks correct propagation of payload type.
TEST_F(AudioEncoderCopyRedTest, CheckPayloadType) {
const int primary_payload_type = red_payload_type_ + 1;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 17;
info.payload_type = primary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
// First call is a special case, since it does not include a secondary
// payload.
Encode();
ASSERT_EQ(0u, encoded_info_.redundant.size());
const int secondary_payload_type = red_payload_type_ + 2;
info.payload_type = secondary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
ASSERT_EQ(2u, encoded_info_.redundant.size());
EXPECT_EQ(secondary_payload_type, encoded_info_.redundant[1].payload_type);
EXPECT_EQ(primary_payload_type, encoded_info_.redundant[0].payload_type);
EXPECT_EQ(red_payload_type_, encoded_info_.payload_type);
}
TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header) {
const int primary_payload_type = red_payload_type_ + 1;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 10;
info.encoded_timestamp = timestamp_;
info.payload_type = primary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
info.encoded_timestamp = timestamp_; // update timestamp.
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Second call will produce a redundant encoding.
EXPECT_EQ(encoded_.size(),
5u + 2 * 10u); // header size + two encoded payloads.
EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
uint32_t timestamp_delta = encoded_info_.encoded_timestamp -
encoded_info_.redundant[0].encoded_timestamp;
// Timestamp delta is encoded as a 14 bit value.
EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
// Redundant length is encoded as 10 bit value.
EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
EXPECT_EQ(encoded_[4], primary_payload_type);
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Third call will produce a redundant encoding with double
// redundancy.
EXPECT_EQ(encoded_.size(),
5u + 2 * 10u); // header size + two encoded payloads.
EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
timestamp_delta = encoded_info_.encoded_timestamp -
encoded_info_.redundant[0].encoded_timestamp;
// Timestamp delta is encoded as a 14 bit value.
EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
// Redundant length is encoded as 10 bit value.
EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
EXPECT_EQ(encoded_[4], primary_payload_type);
timestamp_delta = encoded_info_.encoded_timestamp -
encoded_info_.redundant[1].encoded_timestamp;
}
// Variant with a redundancy of 0.
TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header0) {
webrtc::test::ScopedKeyValueConfig field_trials(
field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-0/");
// Recreate the RED encoder to take the new field trial setting into account.
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type_;
config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
const int primary_payload_type = red_payload_type_ + 1;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 10;
info.encoded_timestamp = timestamp_;
info.payload_type = primary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
info.encoded_timestamp = timestamp_; // update timestamp.
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Second call will not produce a redundant encoding.
EXPECT_EQ(encoded_.size(),
1u + 1 * 10u); // header size + one encoded payloads.
EXPECT_EQ(encoded_[0], primary_payload_type);
}
// Variant with a redundancy of 2.
TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header2) {
webrtc::test::ScopedKeyValueConfig field_trials(
field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-2/");
// Recreate the RED encoder to take the new field trial setting into account.
AudioEncoderCopyRed::Config config;
config.payload_type = red_payload_type_;
config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
const int primary_payload_type = red_payload_type_ + 1;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 10;
info.encoded_timestamp = timestamp_;
info.payload_type = primary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
info.encoded_timestamp = timestamp_; // update timestamp.
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Second call will produce a redundant encoding.
EXPECT_EQ(encoded_.size(),
5u + 2 * 10u); // header size + two encoded payloads.
EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
uint32_t timestamp_delta = encoded_info_.encoded_timestamp -
encoded_info_.redundant[0].encoded_timestamp;
// Timestamp delta is encoded as a 14 bit value.
EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
// Redundant length is encoded as 10 bit value.
EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
EXPECT_EQ(encoded_[4], primary_payload_type);
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Third call will produce a redundant encoding with double
// redundancy.
EXPECT_EQ(encoded_.size(),
9u + 3 * 10u); // header size + three encoded payloads.
EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
timestamp_delta = encoded_info_.encoded_timestamp -
encoded_info_.redundant[0].encoded_timestamp;
// Timestamp delta is encoded as a 14 bit value.
EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
// Redundant length is encoded as 10 bit value.
EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
EXPECT_EQ(encoded_[4], primary_payload_type | 0x80);
timestamp_delta = encoded_info_.encoded_timestamp -
encoded_info_.redundant[1].encoded_timestamp;
// Timestamp delta is encoded as a 14 bit value.
EXPECT_EQ(encoded_[5], timestamp_delta >> 6);
EXPECT_EQ(static_cast<uint8_t>(encoded_[6] >> 2), timestamp_delta & 0x3f);
// Redundant length is encoded as 10 bit value.
EXPECT_EQ(encoded_[6] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
EXPECT_EQ(encoded_[7], encoded_info_.redundant[1].encoded_bytes & 0xff);
EXPECT_EQ(encoded_[8], primary_payload_type);
}
TEST_F(AudioEncoderCopyRedTest, RespectsPayloadMTU) {
const int primary_payload_type = red_payload_type_ + 1;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 600;
info.encoded_timestamp = timestamp_;
info.payload_type = primary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
info.encoded_timestamp = timestamp_; // update timestamp.
info.encoded_bytes = 500;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Second call will produce a redundant encoding.
EXPECT_EQ(encoded_.size(), 5u + 600u + 500u);
info.encoded_timestamp = timestamp_; // update timestamp.
info.encoded_bytes = 400;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode(); // Third call will drop the oldest packet.
EXPECT_EQ(encoded_.size(), 5u + 500u + 400u);
}
TEST_F(AudioEncoderCopyRedTest, LargeTimestampGap) {
const int primary_payload_type = red_payload_type_ + 1;
AudioEncoder::EncodedInfo info;
info.encoded_bytes = 100;
info.encoded_timestamp = timestamp_;
info.payload_type = primary_payload_type;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
// Update timestamp to simulate a 400ms gap like the one
// opus DTX causes.
timestamp_ += 19200;
info.encoded_timestamp = timestamp_; // update timestamp.
info.encoded_bytes = 200;
EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
.WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
Encode();
// The old packet will be dropped.
EXPECT_EQ(encoded_.size(), 1u + 200u);
}
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
// This test fixture tests various error conditions that makes the
// AudioEncoderCng die via CHECKs.
class AudioEncoderCopyRedDeathTest : public AudioEncoderCopyRedTest {
protected:
AudioEncoderCopyRedDeathTest() : AudioEncoderCopyRedTest() {}
};
TEST_F(AudioEncoderCopyRedDeathTest, WrongFrameSize) {
num_audio_samples_10ms *= 2; // 20 ms frame.
RTC_EXPECT_DEATH(Encode(), "");
num_audio_samples_10ms = 0; // Zero samples.
RTC_EXPECT_DEATH(Encode(), "");
}
TEST_F(AudioEncoderCopyRedDeathTest, NullSpeechEncoder) {
test::ScopedKeyValueConfig field_trials;
AudioEncoderCopyRed* red = NULL;
AudioEncoderCopyRed::Config config;
config.speech_encoder = NULL;
RTC_EXPECT_DEATH(
red = new AudioEncoderCopyRed(std::move(config), field_trials),
"Speech encoder not provided.");
// The delete operation is needed to avoid leak reports from memcheck.
delete red;
}
#endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
} // namespace webrtc