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