1 /*
2 * Copyright (c) 2014 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
11 #include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"
12
13 #include <string.h>
14
15 #include <utility>
16 #include <vector>
17
18 #include "absl/strings/string_view.h"
19 #include "rtc_base/byte_order.h"
20 #include "rtc_base/checks.h"
21 #include "rtc_base/logging.h"
22
23 namespace webrtc {
24 static constexpr const int kRedMaxPacketSize =
25 1 << 10; // RED packets must be less than 1024 bytes to fit the 10 bit
26 // block length.
27 static constexpr const size_t kRedMaxTimestampDelta =
28 1 << 14; // RED packets can encode a timestamp delta of 14 bits.
29 static constexpr const size_t kAudioMaxRtpPacketLen =
30 1200; // The typical MTU is 1200 bytes.
31
32 static constexpr size_t kRedHeaderLength = 4; // 4 bytes RED header.
33 static constexpr size_t kRedLastHeaderLength =
34 1; // reduced size for last RED header.
35
36 static constexpr size_t kRedNumberOfRedundantEncodings =
37 1; // The level of redundancy we support.
38
39 AudioEncoderCopyRed::Config::Config() = default;
40 AudioEncoderCopyRed::Config::Config(Config&&) = default;
41 AudioEncoderCopyRed::Config::~Config() = default;
42
GetMaxRedundancyFromFieldTrial(const FieldTrialsView & field_trials)43 size_t GetMaxRedundancyFromFieldTrial(const FieldTrialsView& field_trials) {
44 const std::string red_trial =
45 field_trials.Lookup("WebRTC-Audio-Red-For-Opus");
46 size_t redundancy = 0;
47 if (sscanf(red_trial.c_str(), "Enabled-%zu", &redundancy) != 1 ||
48 redundancy > 9) {
49 return kRedNumberOfRedundantEncodings;
50 }
51 return redundancy;
52 }
53
AudioEncoderCopyRed(Config && config,const FieldTrialsView & field_trials)54 AudioEncoderCopyRed::AudioEncoderCopyRed(Config&& config,
55 const FieldTrialsView& field_trials)
56 : speech_encoder_(std::move(config.speech_encoder)),
57 primary_encoded_(0, kAudioMaxRtpPacketLen),
58 max_packet_length_(kAudioMaxRtpPacketLen),
59 red_payload_type_(config.payload_type) {
60 RTC_CHECK(speech_encoder_) << "Speech encoder not provided.";
61
62 auto number_of_redundant_encodings =
63 GetMaxRedundancyFromFieldTrial(field_trials);
64 for (size_t i = 0; i < number_of_redundant_encodings; i++) {
65 std::pair<EncodedInfo, rtc::Buffer> redundant;
66 redundant.second.EnsureCapacity(kAudioMaxRtpPacketLen);
67 redundant_encodings_.push_front(std::move(redundant));
68 }
69 }
70
71 AudioEncoderCopyRed::~AudioEncoderCopyRed() = default;
72
SampleRateHz() const73 int AudioEncoderCopyRed::SampleRateHz() const {
74 return speech_encoder_->SampleRateHz();
75 }
76
NumChannels() const77 size_t AudioEncoderCopyRed::NumChannels() const {
78 return speech_encoder_->NumChannels();
79 }
80
RtpTimestampRateHz() const81 int AudioEncoderCopyRed::RtpTimestampRateHz() const {
82 return speech_encoder_->RtpTimestampRateHz();
83 }
84
Num10MsFramesInNextPacket() const85 size_t AudioEncoderCopyRed::Num10MsFramesInNextPacket() const {
86 return speech_encoder_->Num10MsFramesInNextPacket();
87 }
88
Max10MsFramesInAPacket() const89 size_t AudioEncoderCopyRed::Max10MsFramesInAPacket() const {
90 return speech_encoder_->Max10MsFramesInAPacket();
91 }
92
GetTargetBitrate() const93 int AudioEncoderCopyRed::GetTargetBitrate() const {
94 return speech_encoder_->GetTargetBitrate();
95 }
96
EncodeImpl(uint32_t rtp_timestamp,rtc::ArrayView<const int16_t> audio,rtc::Buffer * encoded)97 AudioEncoder::EncodedInfo AudioEncoderCopyRed::EncodeImpl(
98 uint32_t rtp_timestamp,
99 rtc::ArrayView<const int16_t> audio,
100 rtc::Buffer* encoded) {
101 primary_encoded_.Clear();
102 EncodedInfo info =
103 speech_encoder_->Encode(rtp_timestamp, audio, &primary_encoded_);
104 RTC_CHECK(info.redundant.empty()) << "Cannot use nested redundant encoders.";
105 RTC_DCHECK_EQ(primary_encoded_.size(), info.encoded_bytes);
106
107 if (info.encoded_bytes == 0 || info.encoded_bytes >= kRedMaxPacketSize) {
108 return info;
109 }
110 RTC_DCHECK_GT(max_packet_length_, info.encoded_bytes);
111
112 size_t header_length_bytes = kRedLastHeaderLength;
113 size_t bytes_available = max_packet_length_ - info.encoded_bytes;
114 auto it = redundant_encodings_.begin();
115
116 // Determine how much redundancy we can fit into our packet by
117 // iterating forward. This is determined both by the length as well
118 // as the timestamp difference. The latter can occur with opus DTX which
119 // has timestamp gaps of 400ms which exceeds REDs timestamp delta field size.
120 for (; it != redundant_encodings_.end(); it++) {
121 if (bytes_available < kRedHeaderLength + it->first.encoded_bytes) {
122 break;
123 }
124 if (it->first.encoded_bytes == 0) {
125 break;
126 }
127 if (rtp_timestamp - it->first.encoded_timestamp >= kRedMaxTimestampDelta) {
128 break;
129 }
130 bytes_available -= kRedHeaderLength + it->first.encoded_bytes;
131 header_length_bytes += kRedHeaderLength;
132 }
133
134 // Allocate room for RFC 2198 header.
135 encoded->SetSize(header_length_bytes);
136
137 // Iterate backwards and append the data.
138 size_t header_offset = 0;
139 while (it-- != redundant_encodings_.begin()) {
140 encoded->AppendData(it->second);
141
142 const uint32_t timestamp_delta =
143 info.encoded_timestamp - it->first.encoded_timestamp;
144 encoded->data()[header_offset] = it->first.payload_type | 0x80;
145 rtc::SetBE16(static_cast<uint8_t*>(encoded->data()) + header_offset + 1,
146 (timestamp_delta << 2) | (it->first.encoded_bytes >> 8));
147 encoded->data()[header_offset + 3] = it->first.encoded_bytes & 0xff;
148 header_offset += kRedHeaderLength;
149 info.redundant.push_back(it->first);
150 }
151
152 // `info` will be implicitly cast to an EncodedInfoLeaf struct, effectively
153 // discarding the (empty) vector of redundant information. This is
154 // intentional.
155 if (header_length_bytes > kRedHeaderLength) {
156 info.redundant.push_back(info);
157 RTC_DCHECK_EQ(info.speech,
158 info.redundant[info.redundant.size() - 1].speech);
159 }
160
161 encoded->AppendData(primary_encoded_);
162 RTC_DCHECK_EQ(header_offset, header_length_bytes - 1);
163 encoded->data()[header_offset] = info.payload_type;
164
165 // Shift the redundant encodings.
166 auto rit = redundant_encodings_.rbegin();
167 for (auto next = std::next(rit); next != redundant_encodings_.rend();
168 rit++, next = std::next(rit)) {
169 rit->first = next->first;
170 rit->second.SetData(next->second);
171 }
172 it = redundant_encodings_.begin();
173 if (it != redundant_encodings_.end()) {
174 it->first = info;
175 it->second.SetData(primary_encoded_);
176 }
177
178 // Update main EncodedInfo.
179 info.payload_type = red_payload_type_;
180 info.encoded_bytes = encoded->size();
181 return info;
182 }
183
Reset()184 void AudioEncoderCopyRed::Reset() {
185 speech_encoder_->Reset();
186 auto number_of_redundant_encodings = redundant_encodings_.size();
187 redundant_encodings_.clear();
188 for (size_t i = 0; i < number_of_redundant_encodings; i++) {
189 std::pair<EncodedInfo, rtc::Buffer> redundant;
190 redundant.second.EnsureCapacity(kAudioMaxRtpPacketLen);
191 redundant_encodings_.push_front(std::move(redundant));
192 }
193 }
194
SetFec(bool enable)195 bool AudioEncoderCopyRed::SetFec(bool enable) {
196 return speech_encoder_->SetFec(enable);
197 }
198
SetDtx(bool enable)199 bool AudioEncoderCopyRed::SetDtx(bool enable) {
200 return speech_encoder_->SetDtx(enable);
201 }
202
GetDtx() const203 bool AudioEncoderCopyRed::GetDtx() const {
204 return speech_encoder_->GetDtx();
205 }
206
SetApplication(Application application)207 bool AudioEncoderCopyRed::SetApplication(Application application) {
208 return speech_encoder_->SetApplication(application);
209 }
210
SetMaxPlaybackRate(int frequency_hz)211 void AudioEncoderCopyRed::SetMaxPlaybackRate(int frequency_hz) {
212 speech_encoder_->SetMaxPlaybackRate(frequency_hz);
213 }
214
EnableAudioNetworkAdaptor(const std::string & config_string,RtcEventLog * event_log)215 bool AudioEncoderCopyRed::EnableAudioNetworkAdaptor(
216 const std::string& config_string,
217 RtcEventLog* event_log) {
218 return speech_encoder_->EnableAudioNetworkAdaptor(config_string, event_log);
219 }
220
DisableAudioNetworkAdaptor()221 void AudioEncoderCopyRed::DisableAudioNetworkAdaptor() {
222 speech_encoder_->DisableAudioNetworkAdaptor();
223 }
224
OnReceivedUplinkPacketLossFraction(float uplink_packet_loss_fraction)225 void AudioEncoderCopyRed::OnReceivedUplinkPacketLossFraction(
226 float uplink_packet_loss_fraction) {
227 speech_encoder_->OnReceivedUplinkPacketLossFraction(
228 uplink_packet_loss_fraction);
229 }
230
OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,absl::optional<int64_t> bwe_period_ms)231 void AudioEncoderCopyRed::OnReceivedUplinkBandwidth(
232 int target_audio_bitrate_bps,
233 absl::optional<int64_t> bwe_period_ms) {
234 speech_encoder_->OnReceivedUplinkBandwidth(target_audio_bitrate_bps,
235 bwe_period_ms);
236 }
237
OnReceivedUplinkAllocation(BitrateAllocationUpdate update)238 void AudioEncoderCopyRed::OnReceivedUplinkAllocation(
239 BitrateAllocationUpdate update) {
240 speech_encoder_->OnReceivedUplinkAllocation(update);
241 }
242
243 absl::optional<std::pair<TimeDelta, TimeDelta>>
GetFrameLengthRange() const244 AudioEncoderCopyRed::GetFrameLengthRange() const {
245 return speech_encoder_->GetFrameLengthRange();
246 }
247
OnReceivedRtt(int rtt_ms)248 void AudioEncoderCopyRed::OnReceivedRtt(int rtt_ms) {
249 speech_encoder_->OnReceivedRtt(rtt_ms);
250 }
251
OnReceivedOverhead(size_t overhead_bytes_per_packet)252 void AudioEncoderCopyRed::OnReceivedOverhead(size_t overhead_bytes_per_packet) {
253 max_packet_length_ = kAudioMaxRtpPacketLen - overhead_bytes_per_packet;
254 return speech_encoder_->OnReceivedOverhead(overhead_bytes_per_packet);
255 }
256
SetReceiverFrameLengthRange(int min_frame_length_ms,int max_frame_length_ms)257 void AudioEncoderCopyRed::SetReceiverFrameLengthRange(int min_frame_length_ms,
258 int max_frame_length_ms) {
259 return speech_encoder_->SetReceiverFrameLengthRange(min_frame_length_ms,
260 max_frame_length_ms);
261 }
262
GetANAStats() const263 ANAStats AudioEncoderCopyRed::GetANAStats() const {
264 return speech_encoder_->GetANAStats();
265 }
266
267 rtc::ArrayView<std::unique_ptr<AudioEncoder>>
ReclaimContainedEncoders()268 AudioEncoderCopyRed::ReclaimContainedEncoders() {
269 return rtc::ArrayView<std::unique_ptr<AudioEncoder>>(&speech_encoder_, 1);
270 }
271
272 } // namespace webrtc
273