OLD | NEW |
| (Empty) |
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/payload_splitter.h" | |
12 | |
13 #include <assert.h> | |
14 | |
15 #include "webrtc/base/checks.h" | |
16 #include "webrtc/base/logging.h" | |
17 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" | |
18 | |
19 namespace webrtc { | |
20 | |
21 // 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 | |
23 // temporarily held in the list |new_packets|. | |
24 // 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: | |
26 // {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. | |
28 int PayloadSplitter::SplitRed(PacketList* packet_list) { | |
29 int ret = kOK; | |
30 PacketList::iterator it = packet_list->begin(); | |
31 while (it != packet_list->end()) { | |
32 const Packet* red_packet = (*it); | |
33 assert(!red_packet->payload.empty()); | |
34 const uint8_t* payload_ptr = red_packet->payload.data(); | |
35 | |
36 // Read RED headers (according to RFC 2198): | |
37 // | |
38 // 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 | |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
41 // |F| block PT | timestamp offset | block length | | |
42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
43 // Last RED header: | |
44 // 0 1 2 3 4 5 6 7 | |
45 // +-+-+-+-+-+-+-+-+ | |
46 // |0| Block PT | | |
47 // +-+-+-+-+-+-+-+-+ | |
48 | |
49 struct RedHeader { | |
50 uint8_t payload_type; | |
51 uint32_t timestamp; | |
52 size_t payload_length; | |
53 bool primary; | |
54 }; | |
55 | |
56 std::vector<RedHeader> new_headers; | |
57 bool last_block = false; | |
58 size_t sum_length = 0; | |
59 while (!last_block) { | |
60 RedHeader new_header; | |
61 // Check the F bit. If F == 0, this was the last block. | |
62 last_block = ((*payload_ptr & 0x80) == 0); | |
63 // Bits 1 through 7 are payload type. | |
64 new_header.payload_type = payload_ptr[0] & 0x7F; | |
65 if (last_block) { | |
66 // No more header data to read. | |
67 ++sum_length; // Account for RED header size of 1 byte. | |
68 new_header.timestamp = red_packet->header.timestamp; | |
69 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. | |
72 } else { | |
73 // Bits 8 through 21 are timestamp offset. | |
74 int timestamp_offset = (payload_ptr[1] << 6) + | |
75 ((payload_ptr[2] & 0xFC) >> 2); | |
76 new_header.timestamp = red_packet->header.timestamp - timestamp_offset; | |
77 // Bits 22 through 31 are payload length. | |
78 new_header.payload_length = | |
79 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; | |
80 new_header.primary = false; | |
81 payload_ptr += 4; // Advance to next RED header. | |
82 } | |
83 sum_length += new_header.payload_length; | |
84 sum_length += 4; // Account for RED header size of 4 bytes. | |
85 // Store in new list of packets. | |
86 new_headers.push_back(new_header); | |
87 } | |
88 | |
89 // Populate the new packets with payload data. | |
90 // |payload_ptr| now points at the first payload byte. | |
91 PacketList new_packets; // An empty list to store the split packets in. | |
92 for (const auto& new_header : new_headers) { | |
93 size_t payload_length = new_header.payload_length; | |
94 if (payload_ptr + payload_length > | |
95 red_packet->payload.data() + red_packet->payload.size()) { | |
96 // The block lengths in the RED headers do not match the overall packet | |
97 // length. Something is corrupt. Discard this and the remaining | |
98 // payloads from this packet. | |
99 LOG(LS_WARNING) << "SplitRed length mismatch"; | |
100 ret = kRedLengthMismatch; | |
101 break; | |
102 } | |
103 Packet* new_packet = new Packet; | |
104 new_packet->header = red_packet->header; | |
105 new_packet->header.timestamp = new_header.timestamp; | |
106 new_packet->header.payloadType = new_header.payload_type; | |
107 new_packet->primary = new_header.primary; | |
108 new_packet->payload.SetData(payload_ptr, payload_length); | |
109 new_packets.push_front(new_packet); | |
110 payload_ptr += payload_length; | |
111 } | |
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. | |
117 delete (*it); | |
118 // 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 | |
120 // increment it manually. | |
121 it = packet_list->erase(it); | |
122 } | |
123 return ret; | |
124 } | |
125 | |
126 int PayloadSplitter::SplitFec(PacketList* packet_list, | |
127 DecoderDatabase* decoder_database) { | |
128 PacketList::iterator it = packet_list->begin(); | |
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(); | |
187 int main_payload_type = -1; | |
188 int num_deleted_packets = 0; | |
189 while (it != packet_list->end()) { | |
190 uint8_t this_payload_type = (*it)->header.payloadType; | |
191 if (!decoder_database.IsDtmf(this_payload_type) && | |
192 !decoder_database.IsComfortNoise(this_payload_type)) { | |
193 if (main_payload_type == -1) { | |
194 // This is the first packet in the list which is non-DTMF non-CNG. | |
195 main_payload_type = this_payload_type; | |
196 } else { | |
197 if (this_payload_type != main_payload_type) { | |
198 // We do not allow redundant payloads of a different type. | |
199 // Discard this payload. | |
200 delete (*it); | |
201 // Remove |it| from the packet list. This operation effectively | |
202 // moves the iterator |it| to the next packet in the list. Thus, we | |
203 // do not have to increment it manually. | |
204 it = packet_list->erase(it); | |
205 ++num_deleted_packets; | |
206 continue; | |
207 } | |
208 } | |
209 } | |
210 ++it; | |
211 } | |
212 return num_deleted_packets; | |
213 } | |
214 | |
215 } // namespace webrtc | |
OLD | NEW |