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