blob: 7438f25301beb87329ff5bd596c3ff17eb288461 [file] [log] [blame]
ossua70695a2016-09-22 02:06:28 -07001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/red_payload_splitter.h"
ossua70695a2016-09-22 02:06:28 -070012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <cstdint>
16#include <list>
17#include <utility>
ossua70695a2016-09-22 02:06:28 -070018#include <vector>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/neteq/decoder_database.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "modules/audio_coding/neteq/packet.h"
22#include "rtc_base/buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010024#include "rtc_base/numerics/safe_conversions.h"
ossua70695a2016-09-22 02:06:28 -070025
26namespace webrtc {
27
28// The method loops through a list of packets {A, B, C, ...}. Each packet is
29// split into its corresponding RED payloads, {A1, A2, ...}, which is
Artem Titovd00ce742021-07-28 20:00:17 +020030// temporarily held in the list `new_packets`.
31// When the first packet in `packet_list` has been processed, the original
32// packet is replaced by the new ones in `new_packets`, so that `packet_list`
Philipp Hancke2291fb32020-09-29 10:51:42 +020033// becomes: {A1, A2, ..., B, C, ...}. The method then continues with B, and C,
34// until all the original packets have been replaced by their split payloads.
ossua70695a2016-09-22 02:06:28 -070035bool RedPayloadSplitter::SplitRed(PacketList* packet_list) {
36 // Too many RED blocks indicates that something is wrong. Clamp it at some
37 // reasonable value.
38 const size_t kMaxRedBlocks = 32;
39 bool ret = true;
40 PacketList::iterator it = packet_list->begin();
41 while (it != packet_list->end()) {
ossua73f6c92016-10-24 08:25:28 -070042 const Packet& red_packet = *it;
Mirko Bonadei25ab3222021-07-08 20:08:20 +020043 RTC_DCHECK(!red_packet.payload.empty());
ossua73f6c92016-10-24 08:25:28 -070044 const uint8_t* payload_ptr = red_packet.payload.data();
Philipp Hancke2291fb32020-09-29 10:51:42 +020045 size_t payload_length = red_packet.payload.size();
ossua70695a2016-09-22 02:06:28 -070046
47 // Read RED headers (according to RFC 2198):
48 //
49 // 0 1 2 3
50 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
51 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 // |F| block PT | timestamp offset | block length |
53 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 // Last RED header:
55 // 0 1 2 3 4 5 6 7
56 // +-+-+-+-+-+-+-+-+
57 // |0| Block PT |
58 // +-+-+-+-+-+-+-+-+
59
60 struct RedHeader {
61 uint8_t payload_type;
62 uint32_t timestamp;
63 size_t payload_length;
64 };
65
66 std::vector<RedHeader> new_headers;
67 bool last_block = false;
68 size_t sum_length = 0;
69 while (!last_block) {
Philipp Hancke2291fb32020-09-29 10:51:42 +020070 if (payload_length == 0) {
71 RTC_LOG(LS_WARNING) << "SplitRed header too short";
72 return false;
73 }
ossua70695a2016-09-22 02:06:28 -070074 RedHeader new_header;
75 // Check the F bit. If F == 0, this was the last block.
76 last_block = ((*payload_ptr & 0x80) == 0);
77 // Bits 1 through 7 are payload type.
78 new_header.payload_type = payload_ptr[0] & 0x7F;
79 if (last_block) {
80 // No more header data to read.
Philipp Hancke2291fb32020-09-29 10:51:42 +020081 sum_length += kRedLastHeaderLength; // Account for RED header size.
ossua73f6c92016-10-24 08:25:28 -070082 new_header.timestamp = red_packet.timestamp;
83 new_header.payload_length = red_packet.payload.size() - sum_length;
Philipp Hancke2291fb32020-09-29 10:51:42 +020084 payload_ptr += kRedLastHeaderLength; // Advance to first payload byte.
85 payload_length -= kRedLastHeaderLength;
ossua70695a2016-09-22 02:06:28 -070086 } else {
Philipp Hancke2291fb32020-09-29 10:51:42 +020087 if (payload_length < kRedHeaderLength) {
88 RTC_LOG(LS_WARNING) << "SplitRed header too short";
89 return false;
90 }
ossua70695a2016-09-22 02:06:28 -070091 // Bits 8 through 21 are timestamp offset.
92 int timestamp_offset =
93 (payload_ptr[1] << 6) + ((payload_ptr[2] & 0xFC) >> 2);
ossua73f6c92016-10-24 08:25:28 -070094 new_header.timestamp = red_packet.timestamp - timestamp_offset;
ossua70695a2016-09-22 02:06:28 -070095 // Bits 22 through 31 are payload length.
96 new_header.payload_length =
97 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3];
Philipp Hancke2291fb32020-09-29 10:51:42 +020098
99 sum_length += new_header.payload_length;
100 sum_length += kRedHeaderLength; // Account for RED header size.
101
102 payload_ptr += kRedHeaderLength; // Advance to next RED header.
103 payload_length -= kRedHeaderLength;
ossua70695a2016-09-22 02:06:28 -0700104 }
ossua70695a2016-09-22 02:06:28 -0700105 // Store in new list of packets.
Henrik Lundindf2a4652020-09-29 17:10:33 +0200106 if (new_header.payload_length > 0) {
107 new_headers.push_back(new_header);
108 }
ossua70695a2016-09-22 02:06:28 -0700109 }
110
111 if (new_headers.size() <= kMaxRedBlocks) {
112 // Populate the new packets with payload data.
Artem Titovd00ce742021-07-28 20:00:17 +0200113 // `payload_ptr` now points at the first payload byte.
ossua70695a2016-09-22 02:06:28 -0700114 PacketList new_packets; // An empty list to store the split packets in.
115 for (size_t i = 0; i != new_headers.size(); ++i) {
116 const auto& new_header = new_headers[i];
117 size_t payload_length = new_header.payload_length;
118 if (payload_ptr + payload_length >
ossua73f6c92016-10-24 08:25:28 -0700119 red_packet.payload.data() + red_packet.payload.size()) {
ossua70695a2016-09-22 02:06:28 -0700120 // The block lengths in the RED headers do not match the overall
121 // packet length. Something is corrupt. Discard this and the remaining
122 // payloads from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_WARNING) << "SplitRed length mismatch";
ossua70695a2016-09-22 02:06:28 -0700124 ret = false;
125 break;
126 }
127
ossua73f6c92016-10-24 08:25:28 -0700128 Packet new_packet;
129 new_packet.timestamp = new_header.timestamp;
130 new_packet.payload_type = new_header.payload_type;
131 new_packet.sequence_number = red_packet.sequence_number;
132 new_packet.priority.red_level =
kwibergd3edd772017-03-01 18:52:48 -0800133 rtc::dchecked_cast<int>((new_headers.size() - 1) - i);
ossua73f6c92016-10-24 08:25:28 -0700134 new_packet.payload.SetData(payload_ptr, payload_length);
Alessio Bazzica8f319a32019-07-24 16:47:02 +0000135 new_packet.packet_info = RtpPacketInfo(
136 /*ssrc=*/red_packet.packet_info.ssrc(),
137 /*csrcs=*/std::vector<uint32_t>(),
138 /*rtp_timestamp=*/new_packet.timestamp,
Philipp Hanckedeac4de2021-07-23 07:08:33 +0200139 red_packet.packet_info.audio_level(),
Chen Xinge08648d2019-08-05 16:29:13 +0200140 /*absolute_capture_time=*/absl::nullopt,
Johannes Kronf7de74c2021-04-30 13:10:56 +0200141 /*receive_time=*/red_packet.packet_info.receive_time());
ossua73f6c92016-10-24 08:25:28 -0700142 new_packets.push_front(std::move(new_packet));
ossua70695a2016-09-22 02:06:28 -0700143 payload_ptr += payload_length;
144 }
145 // Insert new packets into original list, before the element pointed to by
Artem Titovd00ce742021-07-28 20:00:17 +0200146 // iterator `it`.
ossua73f6c92016-10-24 08:25:28 -0700147 packet_list->splice(it, std::move(new_packets));
ossua70695a2016-09-22 02:06:28 -0700148 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size();
ossua70695a2016-09-22 02:06:28 -0700150 ret = false;
151 }
Artem Titovd00ce742021-07-28 20:00:17 +0200152 // Remove `it` from the packet list. This operation effectively moves the
153 // iterator `it` to the next packet in the list. Thus, we do not have to
ossua70695a2016-09-22 02:06:28 -0700154 // increment it manually.
155 it = packet_list->erase(it);
156 }
157 return ret;
158}
159
Henrik Lundindefa7a82018-07-03 13:07:30 +0200160void RedPayloadSplitter::CheckRedPayloads(
ossua70695a2016-09-22 02:06:28 -0700161 PacketList* packet_list,
162 const DecoderDatabase& decoder_database) {
ossua70695a2016-09-22 02:06:28 -0700163 int main_payload_type = -1;
ossua73f6c92016-10-24 08:25:28 -0700164 for (auto it = packet_list->begin(); it != packet_list->end(); /* */) {
165 uint8_t this_payload_type = it->payload_type;
Henrik Lundindefa7a82018-07-03 13:07:30 +0200166 if (decoder_database.IsRed(this_payload_type)) {
167 it = packet_list->erase(it);
168 continue;
169 }
ossua70695a2016-09-22 02:06:28 -0700170 if (!decoder_database.IsDtmf(this_payload_type) &&
171 !decoder_database.IsComfortNoise(this_payload_type)) {
172 if (main_payload_type == -1) {
173 // This is the first packet in the list which is non-DTMF non-CNG.
174 main_payload_type = this_payload_type;
175 } else {
176 if (this_payload_type != main_payload_type) {
177 // We do not allow redundant payloads of a different type.
Artem Titovd00ce742021-07-28 20:00:17 +0200178 // Remove `it` from the packet list. This operation effectively
179 // moves the iterator `it` to the next packet in the list. Thus, we
ossua70695a2016-09-22 02:06:28 -0700180 // do not have to increment it manually.
181 it = packet_list->erase(it);
ossua70695a2016-09-22 02:06:28 -0700182 continue;
183 }
184 }
185 }
186 ++it;
187 }
ossua70695a2016-09-22 02:06:28 -0700188}
189
190} // namespace webrtc