blob: daf24e7455a0a40b0e66bf6e1478ff7b41ae02d9 [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
11#include "webrtc/modules/audio_coding/neteq/red_payload_splitter.h"
12
13#include <assert.h>
14#include <vector>
15
ossua70695a2016-09-22 02:06:28 -070016#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/checks.h"
18#include "webrtc/rtc_base/logging.h"
19#include "webrtc/rtc_base/safe_conversions.h"
ossua70695a2016-09-22 02:06:28 -070020
21namespace 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.
30bool 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()) {
ossua73f6c92016-10-24 08:25:28 -070037 const Packet& red_packet = *it;
38 assert(!red_packet.payload.empty());
39 const uint8_t* payload_ptr = red_packet.payload.data();
ossua70695a2016-09-22 02:06:28 -070040
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.
ossua73f6c92016-10-24 08:25:28 -070072 new_header.timestamp = red_packet.timestamp;
73 new_header.payload_length = red_packet.payload.size() - sum_length;
ossua70695a2016-09-22 02:06:28 -070074 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);
ossua73f6c92016-10-24 08:25:28 -070079 new_header.timestamp = red_packet.timestamp - timestamp_offset;
ossua70695a2016-09-22 02:06:28 -070080 // 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 >
ossua73f6c92016-10-24 08:25:28 -070099 red_packet.payload.data() + red_packet.payload.size()) {
ossua70695a2016-09-22 02:06:28 -0700100 // 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
ossua73f6c92016-10-24 08:25:28 -0700108 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 =
kwibergd3edd772017-03-01 18:52:48 -0800113 rtc::dchecked_cast<int>((new_headers.size() - 1) - i);
ossua73f6c92016-10-24 08:25:28 -0700114 new_packet.payload.SetData(payload_ptr, payload_length);
115 new_packets.push_front(std::move(new_packet));
ossua70695a2016-09-22 02:06:28 -0700116 payload_ptr += payload_length;
117 }
118 // Insert new packets into original list, before the element pointed to by
119 // iterator |it|.
ossua73f6c92016-10-24 08:25:28 -0700120 packet_list->splice(it, std::move(new_packets));
ossua70695a2016-09-22 02:06:28 -0700121 } else {
122 LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size();
123 ret = false;
124 }
ossua70695a2016-09-22 02:06:28 -0700125 // 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
133int RedPayloadSplitter::CheckRedPayloads(
134 PacketList* packet_list,
135 const DecoderDatabase& decoder_database) {
ossua70695a2016-09-22 02:06:28 -0700136 int main_payload_type = -1;
137 int num_deleted_packets = 0;
ossua73f6c92016-10-24 08:25:28 -0700138 for (auto it = packet_list->begin(); it != packet_list->end(); /* */) {
139 uint8_t this_payload_type = it->payload_type;
ossua70695a2016-09-22 02:06:28 -0700140 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.
ossua70695a2016-09-22 02:06:28 -0700148 // 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