• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rtc_base/byte_order.h"
19 #include "rtc_base/checks.h"
20 
21 namespace webrtc {
22 static const int kRedMaxPacketSize = 1 << 10;
23 
24 AudioEncoderCopyRed::Config::Config() = default;
25 AudioEncoderCopyRed::Config::Config(Config&&) = default;
26 AudioEncoderCopyRed::Config::~Config() = default;
27 
AudioEncoderCopyRed(Config && config)28 AudioEncoderCopyRed::AudioEncoderCopyRed(Config&& config)
29     : speech_encoder_(std::move(config.speech_encoder)),
30       red_payload_type_(config.payload_type) {
31   RTC_CHECK(speech_encoder_) << "Speech encoder not provided.";
32 }
33 
34 AudioEncoderCopyRed::~AudioEncoderCopyRed() = default;
35 
SampleRateHz() const36 int AudioEncoderCopyRed::SampleRateHz() const {
37   return speech_encoder_->SampleRateHz();
38 }
39 
NumChannels() const40 size_t AudioEncoderCopyRed::NumChannels() const {
41   return speech_encoder_->NumChannels();
42 }
43 
RtpTimestampRateHz() const44 int AudioEncoderCopyRed::RtpTimestampRateHz() const {
45   return speech_encoder_->RtpTimestampRateHz();
46 }
47 
Num10MsFramesInNextPacket() const48 size_t AudioEncoderCopyRed::Num10MsFramesInNextPacket() const {
49   return speech_encoder_->Num10MsFramesInNextPacket();
50 }
51 
Max10MsFramesInAPacket() const52 size_t AudioEncoderCopyRed::Max10MsFramesInAPacket() const {
53   return speech_encoder_->Max10MsFramesInAPacket();
54 }
55 
GetTargetBitrate() const56 int AudioEncoderCopyRed::GetTargetBitrate() const {
57   return speech_encoder_->GetTargetBitrate();
58 }
59 
CalculateHeaderLength() const60 size_t AudioEncoderCopyRed::CalculateHeaderLength() const {
61   size_t header_size = 1;
62   if (secondary_info_.encoded_bytes > 0) {
63     header_size += 4;
64   }
65   if (tertiary_info_.encoded_bytes > 0) {
66     header_size += 4;
67   }
68   return header_size > 1 ? header_size : 0;
69 }
70 
EncodeImpl(uint32_t rtp_timestamp,rtc::ArrayView<const int16_t> audio,rtc::Buffer * encoded)71 AudioEncoder::EncodedInfo AudioEncoderCopyRed::EncodeImpl(
72     uint32_t rtp_timestamp,
73     rtc::ArrayView<const int16_t> audio,
74     rtc::Buffer* encoded) {
75   rtc::Buffer primary_encoded;
76   EncodedInfo info =
77       speech_encoder_->Encode(rtp_timestamp, audio, &primary_encoded);
78   RTC_CHECK(info.redundant.empty()) << "Cannot use nested redundant encoders.";
79   RTC_DCHECK_EQ(primary_encoded.size(), info.encoded_bytes);
80 
81   if (info.encoded_bytes == 0) {
82     return info;
83   }
84 
85   // Allocate room for RFC 2198 header if there is redundant data.
86   // Otherwise this will send the primary payload type without
87   // wrapping in RED.
88   const size_t header_length_bytes = CalculateHeaderLength();
89   encoded->SetSize(header_length_bytes);
90 
91   size_t header_offset = 0;
92   if (tertiary_info_.encoded_bytes > 0 &&
93       tertiary_info_.encoded_bytes < kRedMaxPacketSize) {
94     encoded->AppendData(tertiary_encoded_);
95 
96     const uint32_t timestamp_delta =
97         info.encoded_timestamp - tertiary_info_.encoded_timestamp;
98 
99     encoded->data()[header_offset] = tertiary_info_.payload_type | 0x80;
100     rtc::SetBE16(static_cast<uint8_t*>(encoded->data()) + header_offset + 1,
101                  (timestamp_delta << 2) | (tertiary_info_.encoded_bytes >> 8));
102     encoded->data()[header_offset + 3] = tertiary_info_.encoded_bytes & 0xff;
103     header_offset += 4;
104   }
105 
106   if (secondary_info_.encoded_bytes > 0 &&
107       secondary_info_.encoded_bytes < kRedMaxPacketSize) {
108     encoded->AppendData(secondary_encoded_);
109 
110     const uint32_t timestamp_delta =
111         info.encoded_timestamp - secondary_info_.encoded_timestamp;
112 
113     encoded->data()[header_offset] = secondary_info_.payload_type | 0x80;
114     rtc::SetBE16(static_cast<uint8_t*>(encoded->data()) + header_offset + 1,
115                  (timestamp_delta << 2) | (secondary_info_.encoded_bytes >> 8));
116     encoded->data()[header_offset + 3] = secondary_info_.encoded_bytes & 0xff;
117     header_offset += 4;
118   }
119 
120   encoded->AppendData(primary_encoded);
121   if (header_length_bytes > 0) {
122     RTC_DCHECK_EQ(header_offset, header_length_bytes - 1);
123     encoded->data()[header_offset] = info.payload_type;
124   }
125 
126   // |info| will be implicitly cast to an EncodedInfoLeaf struct, effectively
127   // discarding the (empty) vector of redundant information. This is
128   // intentional.
129   info.redundant.push_back(info);
130   RTC_DCHECK_EQ(info.redundant.size(), 1);
131   RTC_DCHECK_EQ(info.speech, info.redundant[0].speech);
132   if (secondary_info_.encoded_bytes > 0) {
133     info.redundant.push_back(secondary_info_);
134     RTC_DCHECK_EQ(info.redundant.size(), 2);
135   }
136   if (tertiary_info_.encoded_bytes > 0) {
137     info.redundant.push_back(tertiary_info_);
138     RTC_DCHECK_EQ(info.redundant.size(),
139                   2 + (secondary_info_.encoded_bytes > 0 ? 1 : 0));
140   }
141 
142   // Save secondary to tertiary.
143   tertiary_encoded_.SetData(secondary_encoded_);
144   tertiary_info_ = secondary_info_;
145 
146   // Save primary to secondary.
147   secondary_encoded_.SetData(primary_encoded);
148   secondary_info_ = info;
149 
150   // Update main EncodedInfo.
151   if (header_length_bytes > 0) {
152     info.payload_type = red_payload_type_;
153   }
154   info.encoded_bytes = encoded->size();
155   return info;
156 }
157 
Reset()158 void AudioEncoderCopyRed::Reset() {
159   speech_encoder_->Reset();
160   secondary_encoded_.Clear();
161   secondary_info_.encoded_bytes = 0;
162 }
163 
SetFec(bool enable)164 bool AudioEncoderCopyRed::SetFec(bool enable) {
165   return speech_encoder_->SetFec(enable);
166 }
167 
SetDtx(bool enable)168 bool AudioEncoderCopyRed::SetDtx(bool enable) {
169   return speech_encoder_->SetDtx(enable);
170 }
171 
SetApplication(Application application)172 bool AudioEncoderCopyRed::SetApplication(Application application) {
173   return speech_encoder_->SetApplication(application);
174 }
175 
SetMaxPlaybackRate(int frequency_hz)176 void AudioEncoderCopyRed::SetMaxPlaybackRate(int frequency_hz) {
177   speech_encoder_->SetMaxPlaybackRate(frequency_hz);
178 }
179 
180 rtc::ArrayView<std::unique_ptr<AudioEncoder>>
ReclaimContainedEncoders()181 AudioEncoderCopyRed::ReclaimContainedEncoders() {
182   return rtc::ArrayView<std::unique_ptr<AudioEncoder>>(&speech_encoder_, 1);
183 }
184 
OnReceivedUplinkPacketLossFraction(float uplink_packet_loss_fraction)185 void AudioEncoderCopyRed::OnReceivedUplinkPacketLossFraction(
186     float uplink_packet_loss_fraction) {
187   speech_encoder_->OnReceivedUplinkPacketLossFraction(
188       uplink_packet_loss_fraction);
189 }
190 
OnReceivedUplinkBandwidth(int target_audio_bitrate_bps,absl::optional<int64_t> bwe_period_ms)191 void AudioEncoderCopyRed::OnReceivedUplinkBandwidth(
192     int target_audio_bitrate_bps,
193     absl::optional<int64_t> bwe_period_ms) {
194   speech_encoder_->OnReceivedUplinkBandwidth(target_audio_bitrate_bps,
195                                              bwe_period_ms);
196 }
197 
198 absl::optional<std::pair<TimeDelta, TimeDelta>>
GetFrameLengthRange() const199 AudioEncoderCopyRed::GetFrameLengthRange() const {
200   return speech_encoder_->GetFrameLengthRange();
201 }
202 
203 }  // namespace webrtc
204