ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/neteq/red_payload_splitter.h" |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <cstdint> |
| 17 | #include <list> |
| 18 | #include <utility> |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/audio_coding/neteq/decoder_database.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include "modules/audio_coding/neteq/packet.h" |
| 23 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 25 | #include "rtc_base/numerics/safe_conversions.h" |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | // The method loops through a list of packets {A, B, C, ...}. Each packet is |
| 30 | // split into its corresponding RED payloads, {A1, A2, ...}, which is |
| 31 | // temporarily held in the list |new_packets|. |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 32 | // When the first packet in |packet_list| has been processed, the original |
| 33 | // packet is replaced by the new ones in |new_packets|, so that |packet_list| |
| 34 | // becomes: {A1, A2, ..., B, C, ...}. The method then continues with B, and C, |
| 35 | // until all the original packets have been replaced by their split payloads. |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 36 | bool RedPayloadSplitter::SplitRed(PacketList* packet_list) { |
| 37 | // Too many RED blocks indicates that something is wrong. Clamp it at some |
| 38 | // reasonable value. |
| 39 | const size_t kMaxRedBlocks = 32; |
| 40 | bool ret = true; |
| 41 | PacketList::iterator it = packet_list->begin(); |
| 42 | while (it != packet_list->end()) { |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 43 | const Packet& red_packet = *it; |
| 44 | assert(!red_packet.payload.empty()); |
| 45 | const uint8_t* payload_ptr = red_packet.payload.data(); |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 46 | size_t payload_length = red_packet.payload.size(); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 47 | |
| 48 | // Read RED headers (according to RFC 2198): |
| 49 | // |
| 50 | // 0 1 2 3 |
| 51 | // 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 |
| 52 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 53 | // |F| block PT | timestamp offset | block length | |
| 54 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 55 | // Last RED header: |
| 56 | // 0 1 2 3 4 5 6 7 |
| 57 | // +-+-+-+-+-+-+-+-+ |
| 58 | // |0| Block PT | |
| 59 | // +-+-+-+-+-+-+-+-+ |
| 60 | |
| 61 | struct RedHeader { |
| 62 | uint8_t payload_type; |
| 63 | uint32_t timestamp; |
| 64 | size_t payload_length; |
| 65 | }; |
| 66 | |
| 67 | std::vector<RedHeader> new_headers; |
| 68 | bool last_block = false; |
| 69 | size_t sum_length = 0; |
| 70 | while (!last_block) { |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 71 | if (payload_length == 0) { |
| 72 | RTC_LOG(LS_WARNING) << "SplitRed header too short"; |
| 73 | return false; |
| 74 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 75 | RedHeader new_header; |
| 76 | // Check the F bit. If F == 0, this was the last block. |
| 77 | last_block = ((*payload_ptr & 0x80) == 0); |
| 78 | // Bits 1 through 7 are payload type. |
| 79 | new_header.payload_type = payload_ptr[0] & 0x7F; |
| 80 | if (last_block) { |
| 81 | // No more header data to read. |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 82 | sum_length += kRedLastHeaderLength; // Account for RED header size. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 83 | new_header.timestamp = red_packet.timestamp; |
| 84 | new_header.payload_length = red_packet.payload.size() - sum_length; |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 85 | payload_ptr += kRedLastHeaderLength; // Advance to first payload byte. |
| 86 | payload_length -= kRedLastHeaderLength; |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 87 | } else { |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 88 | if (payload_length < kRedHeaderLength) { |
| 89 | RTC_LOG(LS_WARNING) << "SplitRed header too short"; |
| 90 | return false; |
| 91 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 92 | // Bits 8 through 21 are timestamp offset. |
| 93 | int timestamp_offset = |
| 94 | (payload_ptr[1] << 6) + ((payload_ptr[2] & 0xFC) >> 2); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 95 | new_header.timestamp = red_packet.timestamp - timestamp_offset; |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 96 | // Bits 22 through 31 are payload length. |
| 97 | new_header.payload_length = |
| 98 | ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; |
Philipp Hancke | 2291fb3 | 2020-09-29 10:51:42 +0200 | [diff] [blame] | 99 | |
| 100 | sum_length += new_header.payload_length; |
| 101 | sum_length += kRedHeaderLength; // Account for RED header size. |
| 102 | |
| 103 | payload_ptr += kRedHeaderLength; // Advance to next RED header. |
| 104 | payload_length -= kRedHeaderLength; |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 105 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 106 | // Store in new list of packets. |
Henrik Lundin | df2a465 | 2020-09-29 17:10:33 +0200 | [diff] [blame] | 107 | if (new_header.payload_length > 0) { |
| 108 | new_headers.push_back(new_header); |
| 109 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (new_headers.size() <= kMaxRedBlocks) { |
| 113 | // Populate the new packets with payload data. |
| 114 | // |payload_ptr| now points at the first payload byte. |
| 115 | PacketList new_packets; // An empty list to store the split packets in. |
| 116 | for (size_t i = 0; i != new_headers.size(); ++i) { |
| 117 | const auto& new_header = new_headers[i]; |
| 118 | size_t payload_length = new_header.payload_length; |
| 119 | if (payload_ptr + payload_length > |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 120 | red_packet.payload.data() + red_packet.payload.size()) { |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 121 | // The block lengths in the RED headers do not match the overall |
| 122 | // packet length. Something is corrupt. Discard this and the remaining |
| 123 | // payloads from this packet. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 124 | RTC_LOG(LS_WARNING) << "SplitRed length mismatch"; |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 125 | ret = false; |
| 126 | break; |
| 127 | } |
| 128 | |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 129 | Packet new_packet; |
| 130 | new_packet.timestamp = new_header.timestamp; |
| 131 | new_packet.payload_type = new_header.payload_type; |
| 132 | new_packet.sequence_number = red_packet.sequence_number; |
| 133 | new_packet.priority.red_level = |
kwiberg | d3edd77 | 2017-03-01 18:52:48 -0800 | [diff] [blame] | 134 | rtc::dchecked_cast<int>((new_headers.size() - 1) - i); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 135 | new_packet.payload.SetData(payload_ptr, payload_length); |
Alessio Bazzica | 8f319a3 | 2019-07-24 16:47:02 +0000 | [diff] [blame] | 136 | new_packet.packet_info = RtpPacketInfo( |
| 137 | /*ssrc=*/red_packet.packet_info.ssrc(), |
| 138 | /*csrcs=*/std::vector<uint32_t>(), |
| 139 | /*rtp_timestamp=*/new_packet.timestamp, |
| 140 | /*audio_level=*/absl::nullopt, |
Chen Xing | e08648d | 2019-08-05 16:29:13 +0200 | [diff] [blame] | 141 | /*absolute_capture_time=*/absl::nullopt, |
Johannes Kron | f7de74c | 2021-04-30 13:10:56 +0200 | [diff] [blame^] | 142 | /*receive_time=*/red_packet.packet_info.receive_time()); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 143 | new_packets.push_front(std::move(new_packet)); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 144 | payload_ptr += payload_length; |
| 145 | } |
| 146 | // Insert new packets into original list, before the element pointed to by |
| 147 | // iterator |it|. |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 148 | packet_list->splice(it, std::move(new_packets)); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 149 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 150 | RTC_LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size(); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 151 | ret = false; |
| 152 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 153 | // Remove |it| from the packet list. This operation effectively moves the |
| 154 | // iterator |it| to the next packet in the list. Thus, we do not have to |
| 155 | // increment it manually. |
| 156 | it = packet_list->erase(it); |
| 157 | } |
| 158 | return ret; |
| 159 | } |
| 160 | |
Henrik Lundin | defa7a8 | 2018-07-03 13:07:30 +0200 | [diff] [blame] | 161 | void RedPayloadSplitter::CheckRedPayloads( |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 162 | PacketList* packet_list, |
| 163 | const DecoderDatabase& decoder_database) { |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 164 | int main_payload_type = -1; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 165 | for (auto it = packet_list->begin(); it != packet_list->end(); /* */) { |
| 166 | uint8_t this_payload_type = it->payload_type; |
Henrik Lundin | defa7a8 | 2018-07-03 13:07:30 +0200 | [diff] [blame] | 167 | if (decoder_database.IsRed(this_payload_type)) { |
| 168 | it = packet_list->erase(it); |
| 169 | continue; |
| 170 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 171 | if (!decoder_database.IsDtmf(this_payload_type) && |
| 172 | !decoder_database.IsComfortNoise(this_payload_type)) { |
| 173 | if (main_payload_type == -1) { |
| 174 | // This is the first packet in the list which is non-DTMF non-CNG. |
| 175 | main_payload_type = this_payload_type; |
| 176 | } else { |
| 177 | if (this_payload_type != main_payload_type) { |
| 178 | // We do not allow redundant payloads of a different type. |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 179 | // Remove |it| from the packet list. This operation effectively |
| 180 | // moves the iterator |it| to the next packet in the list. Thus, we |
| 181 | // do not have to increment it manually. |
| 182 | it = packet_list->erase(it); |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | ++it; |
| 188 | } |
ossu | a70695a | 2016-09-22 02:06:28 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | } // namespace webrtc |