OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_coding/neteq/payload_splitter.h" | 11 #include "webrtc/modules/audio_coding/neteq/red_payload_splitter.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <vector> |
14 | 15 |
15 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/base/safe_conversions.h" |
17 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | 19 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
18 | 20 |
19 namespace webrtc { | 21 namespace webrtc { |
20 | 22 |
21 // The method loops through a list of packets {A, B, C, ...}. Each packet is | 23 // The method loops through a list of packets {A, B, C, ...}. Each packet is |
22 // split into its corresponding RED payloads, {A1, A2, ...}, which is | 24 // split into its corresponding RED payloads, {A1, A2, ...}, which is |
23 // temporarily held in the list |new_packets|. | 25 // temporarily held in the list |new_packets|. |
24 // When the first packet in |packet_list| has been processed, the orignal packet | 26 // When the first packet in |packet_list| has been processed, the orignal packet |
25 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes: | 27 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes: |
26 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all | 28 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all |
27 // the original packets have been replaced by their split payloads. | 29 // the original packets have been replaced by their split payloads. |
28 int PayloadSplitter::SplitRed(PacketList* packet_list) { | 30 bool RedPayloadSplitter::SplitRed(PacketList* packet_list) { |
29 int ret = kOK; | 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; |
30 PacketList::iterator it = packet_list->begin(); | 35 PacketList::iterator it = packet_list->begin(); |
31 while (it != packet_list->end()) { | 36 while (it != packet_list->end()) { |
32 const Packet* red_packet = (*it); | 37 const Packet* red_packet = (*it); |
33 assert(!red_packet->payload.empty()); | 38 assert(!red_packet->payload.empty()); |
34 const uint8_t* payload_ptr = red_packet->payload.data(); | 39 const uint8_t* payload_ptr = red_packet->payload.data(); |
35 | 40 |
36 // Read RED headers (according to RFC 2198): | 41 // Read RED headers (according to RFC 2198): |
37 // | 42 // |
38 // 0 1 2 3 | 43 // 0 1 2 3 |
39 // 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 | 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 |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 45 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
41 // |F| block PT | timestamp offset | block length | | 46 // |F| block PT | timestamp offset | block length | |
42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | 47 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
43 // Last RED header: | 48 // Last RED header: |
44 // 0 1 2 3 4 5 6 7 | 49 // 0 1 2 3 4 5 6 7 |
45 // +-+-+-+-+-+-+-+-+ | 50 // +-+-+-+-+-+-+-+-+ |
46 // |0| Block PT | | 51 // |0| Block PT | |
47 // +-+-+-+-+-+-+-+-+ | 52 // +-+-+-+-+-+-+-+-+ |
48 | 53 |
49 struct RedHeader { | 54 struct RedHeader { |
50 uint8_t payload_type; | 55 uint8_t payload_type; |
51 uint32_t timestamp; | 56 uint32_t timestamp; |
52 size_t payload_length; | 57 size_t payload_length; |
53 bool primary; | |
54 }; | 58 }; |
55 | 59 |
56 std::vector<RedHeader> new_headers; | 60 std::vector<RedHeader> new_headers; |
57 bool last_block = false; | 61 bool last_block = false; |
58 size_t sum_length = 0; | 62 size_t sum_length = 0; |
59 while (!last_block) { | 63 while (!last_block) { |
60 RedHeader new_header; | 64 RedHeader new_header; |
61 // Check the F bit. If F == 0, this was the last block. | 65 // Check the F bit. If F == 0, this was the last block. |
62 last_block = ((*payload_ptr & 0x80) == 0); | 66 last_block = ((*payload_ptr & 0x80) == 0); |
63 // Bits 1 through 7 are payload type. | 67 // Bits 1 through 7 are payload type. |
64 new_header.payload_type = payload_ptr[0] & 0x7F; | 68 new_header.payload_type = payload_ptr[0] & 0x7F; |
65 if (last_block) { | 69 if (last_block) { |
66 // No more header data to read. | 70 // No more header data to read. |
67 ++sum_length; // Account for RED header size of 1 byte. | 71 ++sum_length; // Account for RED header size of 1 byte. |
68 new_header.timestamp = red_packet->header.timestamp; | 72 new_header.timestamp = red_packet->header.timestamp; |
69 new_header.payload_length = red_packet->payload.size() - sum_length; | 73 new_header.payload_length = red_packet->payload.size() - sum_length; |
70 new_header.primary = true; // Last block is always primary. | |
71 payload_ptr += 1; // Advance to first payload byte. | 74 payload_ptr += 1; // Advance to first payload byte. |
72 } else { | 75 } else { |
73 // Bits 8 through 21 are timestamp offset. | 76 // Bits 8 through 21 are timestamp offset. |
74 int timestamp_offset = (payload_ptr[1] << 6) + | 77 int timestamp_offset = |
75 ((payload_ptr[2] & 0xFC) >> 2); | 78 (payload_ptr[1] << 6) + ((payload_ptr[2] & 0xFC) >> 2); |
76 new_header.timestamp = red_packet->header.timestamp - timestamp_offset; | 79 new_header.timestamp = red_packet->header.timestamp - timestamp_offset; |
77 // Bits 22 through 31 are payload length. | 80 // Bits 22 through 31 are payload length. |
78 new_header.payload_length = | 81 new_header.payload_length = |
79 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; | 82 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; |
80 new_header.primary = false; | |
81 payload_ptr += 4; // Advance to next RED header. | 83 payload_ptr += 4; // Advance to next RED header. |
82 } | 84 } |
83 sum_length += new_header.payload_length; | 85 sum_length += new_header.payload_length; |
84 sum_length += 4; // Account for RED header size of 4 bytes. | 86 sum_length += 4; // Account for RED header size of 4 bytes. |
85 // Store in new list of packets. | 87 // Store in new list of packets. |
86 new_headers.push_back(new_header); | 88 new_headers.push_back(new_header); |
87 } | 89 } |
88 | 90 |
89 // Populate the new packets with payload data. | 91 if (new_headers.size() <= kMaxRedBlocks) { |
90 // |payload_ptr| now points at the first payload byte. | 92 // Populate the new packets with payload data. |
91 PacketList new_packets; // An empty list to store the split packets in. | 93 // |payload_ptr| now points at the first payload byte. |
92 for (const auto& new_header : new_headers) { | 94 PacketList new_packets; // An empty list to store the split packets in. |
93 size_t payload_length = new_header.payload_length; | 95 for (size_t i = 0; i != new_headers.size(); ++i) { |
94 if (payload_ptr + payload_length > | 96 const auto& new_header = new_headers[i]; |
95 red_packet->payload.data() + red_packet->payload.size()) { | 97 size_t payload_length = new_header.payload_length; |
96 // The block lengths in the RED headers do not match the overall packet | 98 if (payload_ptr + payload_length > |
97 // length. Something is corrupt. Discard this and the remaining | 99 red_packet->payload.data() + red_packet->payload.size()) { |
98 // payloads from this packet. | 100 // The block lengths in the RED headers do not match the overall |
99 LOG(LS_WARNING) << "SplitRed length mismatch"; | 101 // packet length. Something is corrupt. Discard this and the remaining |
100 ret = kRedLengthMismatch; | 102 // payloads from this packet. |
101 break; | 103 LOG(LS_WARNING) << "SplitRed length mismatch"; |
| 104 ret = false; |
| 105 break; |
| 106 } |
| 107 |
| 108 Packet* new_packet = new Packet; |
| 109 new_packet->header = red_packet->header; |
| 110 new_packet->header.timestamp = new_header.timestamp; |
| 111 new_packet->header.payloadType = new_header.payload_type; |
| 112 new_packet->priority.red_level = |
| 113 rtc::checked_cast<int>((new_headers.size() - 1) - i); |
| 114 new_packet->payload.SetData(payload_ptr, payload_length); |
| 115 new_packets.push_front(new_packet); |
| 116 payload_ptr += payload_length; |
102 } | 117 } |
103 Packet* new_packet = new Packet; | 118 // Insert new packets into original list, before the element pointed to by |
104 new_packet->header = red_packet->header; | 119 // iterator |it|. |
105 new_packet->header.timestamp = new_header.timestamp; | 120 packet_list->splice(it, new_packets, new_packets.begin(), |
106 new_packet->header.payloadType = new_header.payload_type; | 121 new_packets.end()); |
107 new_packet->primary = new_header.primary; | 122 } else { |
108 new_packet->payload.SetData(payload_ptr, payload_length); | 123 LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size(); |
109 new_packets.push_front(new_packet); | 124 ret = false; |
110 payload_ptr += payload_length; | |
111 } | 125 } |
112 // Insert new packets into original list, before the element pointed to by | |
113 // iterator |it|. | |
114 packet_list->splice(it, new_packets, new_packets.begin(), | |
115 new_packets.end()); | |
116 // Delete old packet payload. | 126 // Delete old packet payload. |
117 delete (*it); | 127 delete (*it); |
118 // Remove |it| from the packet list. This operation effectively moves the | 128 // Remove |it| from the packet list. This operation effectively moves the |
119 // iterator |it| to the next packet in the list. Thus, we do not have to | 129 // iterator |it| to the next packet in the list. Thus, we do not have to |
120 // increment it manually. | 130 // increment it manually. |
121 it = packet_list->erase(it); | 131 it = packet_list->erase(it); |
122 } | 132 } |
123 return ret; | 133 return ret; |
124 } | 134 } |
125 | 135 |
126 int PayloadSplitter::SplitFec(PacketList* packet_list, | 136 int RedPayloadSplitter::CheckRedPayloads( |
127 DecoderDatabase* decoder_database) { | 137 PacketList* packet_list, |
128 PacketList::iterator it = packet_list->begin(); | 138 const DecoderDatabase& decoder_database) { |
129 // Iterate through all packets in |packet_list|. | |
130 while (it != packet_list->end()) { | |
131 Packet* packet = (*it); // Just to make the notation more intuitive. | |
132 // Get codec type for this payload. | |
133 uint8_t payload_type = packet->header.payloadType; | |
134 const DecoderDatabase::DecoderInfo* info = | |
135 decoder_database->GetDecoderInfo(payload_type); | |
136 if (!info) { | |
137 LOG(LS_WARNING) << "SplitFec unknown payload type"; | |
138 return kUnknownPayloadType; | |
139 } | |
140 | |
141 // Not an FEC packet. | |
142 AudioDecoder* decoder = decoder_database->GetDecoder(payload_type); | |
143 // decoder should not return NULL, except for comfort noise payloads which | |
144 // are handled separately. | |
145 assert(decoder != NULL || decoder_database->IsComfortNoise(payload_type)); | |
146 if (!decoder || | |
147 !decoder->PacketHasFec(packet->payload.data(), | |
148 packet->payload.size())) { | |
149 ++it; | |
150 continue; | |
151 } | |
152 | |
153 switch (info->codec_type) { | |
154 case NetEqDecoder::kDecoderOpus: | |
155 case NetEqDecoder::kDecoderOpus_2ch: { | |
156 // The main payload of this packet should be decoded as a primary | |
157 // payload, even if it comes as a secondary payload in a RED packet. | |
158 packet->primary = true; | |
159 | |
160 Packet* new_packet = new Packet; | |
161 new_packet->header = packet->header; | |
162 int duration = decoder->PacketDurationRedundant(packet->payload.data(), | |
163 packet->payload.size()); | |
164 new_packet->header.timestamp -= duration; | |
165 new_packet->payload.SetData(packet->payload); | |
166 new_packet->primary = false; | |
167 // Waiting time should not be set here. | |
168 RTC_DCHECK(!packet->waiting_time); | |
169 | |
170 packet_list->insert(it, new_packet); | |
171 break; | |
172 } | |
173 default: { | |
174 LOG(LS_WARNING) << "SplitFec wrong payload type"; | |
175 return kFecSplitError; | |
176 } | |
177 } | |
178 | |
179 ++it; | |
180 } | |
181 return kOK; | |
182 } | |
183 | |
184 int PayloadSplitter::CheckRedPayloads(PacketList* packet_list, | |
185 const DecoderDatabase& decoder_database) { | |
186 PacketList::iterator it = packet_list->begin(); | 139 PacketList::iterator it = packet_list->begin(); |
187 int main_payload_type = -1; | 140 int main_payload_type = -1; |
188 int num_deleted_packets = 0; | 141 int num_deleted_packets = 0; |
189 while (it != packet_list->end()) { | 142 while (it != packet_list->end()) { |
190 uint8_t this_payload_type = (*it)->header.payloadType; | 143 uint8_t this_payload_type = (*it)->header.payloadType; |
191 if (!decoder_database.IsDtmf(this_payload_type) && | 144 if (!decoder_database.IsDtmf(this_payload_type) && |
192 !decoder_database.IsComfortNoise(this_payload_type)) { | 145 !decoder_database.IsComfortNoise(this_payload_type)) { |
193 if (main_payload_type == -1) { | 146 if (main_payload_type == -1) { |
194 // This is the first packet in the list which is non-DTMF non-CNG. | 147 // This is the first packet in the list which is non-DTMF non-CNG. |
195 main_payload_type = this_payload_type; | 148 main_payload_type = this_payload_type; |
(...skipping 10 matching lines...) Expand all Loading... |
206 continue; | 159 continue; |
207 } | 160 } |
208 } | 161 } |
209 } | 162 } |
210 ++it; | 163 ++it; |
211 } | 164 } |
212 return num_deleted_packets; | 165 return num_deleted_packets; |
213 } | 166 } |
214 | 167 |
215 } // namespace webrtc | 168 } // namespace webrtc |
OLD | NEW |