henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/payload_splitter.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 15 | #include "webrtc/base/checks.h" |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 16 | #include "webrtc/base/logging.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 17 | #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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()) { |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 32 | const Packet* red_packet = (*it); |
| 33 | assert(!red_packet->payload.empty()); |
| 34 | const uint8_t* payload_ptr = red_packet->payload.data(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 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; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 57 | bool last_block = false; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 58 | size_t sum_length = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 59 | while (!last_block) { |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 60 | RedHeader new_header; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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. |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 64 | new_header.payload_type = payload_ptr[0] & 0x7F; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 65 | if (last_block) { |
| 66 | // No more header data to read. |
| 67 | ++sum_length; // Account for RED header size of 1 byte. |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 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. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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); |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 76 | new_header.timestamp = red_packet->header.timestamp - timestamp_offset; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | // Bits 22 through 31 are payload length. |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 78 | new_header.payload_length = |
| 79 | ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; |
| 80 | new_header.primary = false; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 81 | payload_ptr += 4; // Advance to next RED header. |
| 82 | } |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 83 | sum_length += new_header.payload_length; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 84 | sum_length += 4; // Account for RED header size of 4 bytes. |
| 85 | // Store in new list of packets. |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 86 | new_headers.push_back(new_header); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Populate the new packets with payload data. |
| 90 | // |payload_ptr| now points at the first payload byte. |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 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; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | if (payload_ptr + payload_length > |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 95 | red_packet->payload.data() + red_packet->payload.size()) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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. |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 99 | LOG(LS_WARNING) << "SplitRed length mismatch"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 100 | ret = kRedLengthMismatch; |
| 101 | break; |
| 102 | } |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 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); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 110 | payload_ptr += payload_length; |
| 111 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 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) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 137 | LOG(LS_WARNING) << "SplitFec unknown payload type"; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 138 | return kUnknownPayloadType; |
| 139 | } |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 140 | |
| 141 | // Not an FEC packet. |
| 142 | AudioDecoder* decoder = decoder_database->GetDecoder(payload_type); |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 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)); |
minyue@webrtc.org | 7549ff4 | 2014-04-02 15:03:01 +0000 | [diff] [blame] | 146 | if (!decoder || |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 147 | !decoder->PacketHasFec(packet->payload.data(), |
| 148 | packet->payload.size())) { |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 149 | ++it; |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | switch (info->codec_type) { |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 154 | case NetEqDecoder::kDecoderOpus: |
| 155 | case NetEqDecoder::kDecoderOpus_2ch: { |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 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; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 159 | |
minyue@webrtc.org | a8cc344 | 2015-02-13 14:01:54 +0000 | [diff] [blame] | 160 | Packet* new_packet = new Packet; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 161 | new_packet->header = packet->header; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 162 | int duration = decoder->PacketDurationRedundant(packet->payload.data(), |
| 163 | packet->payload.size()); |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 164 | new_packet->header.timestamp -= duration; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 165 | new_packet->payload.SetData(packet->payload); |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 166 | new_packet->primary = false; |
henrik.lundin | 84f8cd6 | 2016-04-26 07:45:16 -0700 | [diff] [blame] | 167 | // Waiting time should not be set here. |
| 168 | RTC_DCHECK(!packet->waiting_time); |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 169 | |
| 170 | packet_list->insert(it, new_packet); |
| 171 | break; |
| 172 | } |
| 173 | default: { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 174 | LOG(LS_WARNING) << "SplitFec wrong payload type"; |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 175 | return kFecSplitError; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | ++it; |
| 180 | } |
| 181 | return kOK; |
| 182 | } |
| 183 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | int PayloadSplitter::SplitAudio(PacketList* packet_list, |
| 216 | const DecoderDatabase& decoder_database) { |
| 217 | PacketList::iterator it = packet_list->begin(); |
| 218 | // Iterate through all packets in |packet_list|. |
| 219 | while (it != packet_list->end()) { |
| 220 | Packet* packet = (*it); // Just to make the notation more intuitive. |
| 221 | // Get codec type for this payload. |
| 222 | const DecoderDatabase::DecoderInfo* info = |
| 223 | decoder_database.GetDecoderInfo(packet->header.payloadType); |
| 224 | if (!info) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 225 | LOG(LS_WARNING) << "SplitAudio unknown payload type"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 226 | return kUnknownPayloadType; |
| 227 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 228 | PacketList new_packets; |
| 229 | switch (info->codec_type) { |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 230 | case NetEqDecoder::kDecoderPCMu: |
| 231 | case NetEqDecoder::kDecoderPCMa: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 232 | // 8 bytes per ms; 8 timestamps per ms. |
| 233 | SplitBySamples(packet, 8, 8, &new_packets); |
| 234 | break; |
| 235 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 236 | case NetEqDecoder::kDecoderPCMu_2ch: |
| 237 | case NetEqDecoder::kDecoderPCMa_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | // 2 * 8 bytes per ms; 8 timestamps per ms. |
| 239 | SplitBySamples(packet, 2 * 8, 8, &new_packets); |
| 240 | break; |
| 241 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 242 | case NetEqDecoder::kDecoderG722: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 243 | // 8 bytes per ms; 16 timestamps per ms. |
| 244 | SplitBySamples(packet, 8, 16, &new_packets); |
| 245 | break; |
| 246 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 247 | case NetEqDecoder::kDecoderPCM16B: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 248 | // 16 bytes per ms; 8 timestamps per ms. |
| 249 | SplitBySamples(packet, 16, 8, &new_packets); |
| 250 | break; |
| 251 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 252 | case NetEqDecoder::kDecoderPCM16Bwb: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 253 | // 32 bytes per ms; 16 timestamps per ms. |
| 254 | SplitBySamples(packet, 32, 16, &new_packets); |
| 255 | break; |
| 256 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 257 | case NetEqDecoder::kDecoderPCM16Bswb32kHz: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 258 | // 64 bytes per ms; 32 timestamps per ms. |
| 259 | SplitBySamples(packet, 64, 32, &new_packets); |
| 260 | break; |
| 261 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 262 | case NetEqDecoder::kDecoderPCM16Bswb48kHz: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 263 | // 96 bytes per ms; 48 timestamps per ms. |
| 264 | SplitBySamples(packet, 96, 48, &new_packets); |
| 265 | break; |
| 266 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 267 | case NetEqDecoder::kDecoderPCM16B_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 268 | // 2 * 16 bytes per ms; 8 timestamps per ms. |
| 269 | SplitBySamples(packet, 2 * 16, 8, &new_packets); |
| 270 | break; |
| 271 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 272 | case NetEqDecoder::kDecoderPCM16Bwb_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 273 | // 2 * 32 bytes per ms; 16 timestamps per ms. |
| 274 | SplitBySamples(packet, 2 * 32, 16, &new_packets); |
| 275 | break; |
| 276 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 277 | case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 278 | // 2 * 64 bytes per ms; 32 timestamps per ms. |
| 279 | SplitBySamples(packet, 2 * 64, 32, &new_packets); |
| 280 | break; |
| 281 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 282 | case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 283 | // 2 * 96 bytes per ms; 48 timestamps per ms. |
| 284 | SplitBySamples(packet, 2 * 96, 48, &new_packets); |
| 285 | break; |
| 286 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 287 | case NetEqDecoder::kDecoderPCM16B_5ch: { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 288 | // 5 * 16 bytes per ms; 8 timestamps per ms. |
| 289 | SplitBySamples(packet, 5 * 16, 8, &new_packets); |
| 290 | break; |
| 291 | } |
kwiberg | ee1879c | 2015-10-29 06:20:28 -0700 | [diff] [blame] | 292 | case NetEqDecoder::kDecoderILBC: { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 293 | size_t bytes_per_frame; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 294 | int timestamps_per_frame; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 295 | if (packet->payload.size() >= 950) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 296 | LOG(LS_WARNING) << "SplitAudio too large iLBC payload"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 297 | return kTooLargePayload; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 298 | } |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 299 | if (packet->payload.size() % 38 == 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 300 | // 20 ms frames. |
| 301 | bytes_per_frame = 38; |
| 302 | timestamps_per_frame = 160; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 303 | } else if (packet->payload.size() % 50 == 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 304 | // 30 ms frames. |
| 305 | bytes_per_frame = 50; |
| 306 | timestamps_per_frame = 240; |
| 307 | } else { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 308 | LOG(LS_WARNING) << "SplitAudio invalid iLBC payload"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 309 | return kFrameSplitError; |
| 310 | } |
| 311 | int ret = SplitByFrames(packet, bytes_per_frame, timestamps_per_frame, |
| 312 | &new_packets); |
| 313 | if (ret < 0) { |
| 314 | return ret; |
| 315 | } else if (ret == kNoSplit) { |
| 316 | // Do not split at all. Simply advance to the next packet in the list. |
| 317 | ++it; |
| 318 | // We do not have any new packets to insert, and should not delete the |
| 319 | // old one. Skip the code after the switch case, and jump straight to |
| 320 | // the next packet in the while loop. |
| 321 | continue; |
| 322 | } |
| 323 | break; |
| 324 | } |
| 325 | default: { |
| 326 | // Do not split at all. Simply advance to the next packet in the list. |
| 327 | ++it; |
| 328 | // We do not have any new packets to insert, and should not delete the |
| 329 | // old one. Skip the code after the switch case, and jump straight to |
| 330 | // the next packet in the while loop. |
| 331 | continue; |
| 332 | } |
| 333 | } |
| 334 | // Insert new packets into original list, before the element pointed to by |
| 335 | // iterator |it|. |
| 336 | packet_list->splice(it, new_packets, new_packets.begin(), |
| 337 | new_packets.end()); |
| 338 | // Delete old packet payload. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 339 | delete (*it); |
| 340 | // Remove |it| from the packet list. This operation effectively moves the |
| 341 | // iterator |it| to the next packet in the list. Thus, we do not have to |
| 342 | // increment it manually. |
| 343 | it = packet_list->erase(it); |
| 344 | } |
minyue@webrtc.org | b28bfa7 | 2014-03-21 12:07:40 +0000 | [diff] [blame] | 345 | return kOK; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void PayloadSplitter::SplitBySamples(const Packet* packet, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 349 | size_t bytes_per_ms, |
| 350 | uint32_t timestamps_per_ms, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 351 | PacketList* new_packets) { |
| 352 | assert(packet); |
| 353 | assert(new_packets); |
| 354 | |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 355 | size_t split_size_bytes = packet->payload.size(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 356 | |
| 357 | // Find a "chunk size" >= 20 ms and < 40 ms. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 358 | size_t min_chunk_size = bytes_per_ms * 20; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 359 | // Reduce the split size by half as long as |split_size_bytes| is at least |
| 360 | // twice the minimum chunk size (so that the resulting size is at least as |
| 361 | // large as the minimum chunk size). |
| 362 | while (split_size_bytes >= 2 * min_chunk_size) { |
| 363 | split_size_bytes >>= 1; |
| 364 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 365 | uint32_t timestamps_per_chunk = static_cast<uint32_t>( |
| 366 | split_size_bytes * timestamps_per_ms / bytes_per_ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 367 | uint32_t timestamp = packet->header.timestamp; |
| 368 | |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 369 | const uint8_t* payload_ptr = packet->payload.data(); |
| 370 | size_t len = packet->payload.size(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 371 | while (len >= (2 * split_size_bytes)) { |
| 372 | Packet* new_packet = new Packet; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 373 | new_packet->header = packet->header; |
| 374 | new_packet->header.timestamp = timestamp; |
| 375 | timestamp += timestamps_per_chunk; |
| 376 | new_packet->primary = packet->primary; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 377 | new_packet->payload.SetData(payload_ptr, split_size_bytes); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 378 | payload_ptr += split_size_bytes; |
| 379 | new_packets->push_back(new_packet); |
| 380 | len -= split_size_bytes; |
| 381 | } |
| 382 | |
| 383 | if (len > 0) { |
| 384 | Packet* new_packet = new Packet; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 385 | new_packet->header = packet->header; |
| 386 | new_packet->header.timestamp = timestamp; |
| 387 | new_packet->primary = packet->primary; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 388 | new_packet->payload.SetData(payload_ptr, len); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 389 | new_packets->push_back(new_packet); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | int PayloadSplitter::SplitByFrames(const Packet* packet, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 394 | size_t bytes_per_frame, |
| 395 | uint32_t timestamps_per_frame, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 396 | PacketList* new_packets) { |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 397 | if (packet->payload.size() % bytes_per_frame != 0) { |
Henrik Lundin | d67a219 | 2015-08-03 12:54:37 +0200 | [diff] [blame] | 398 | LOG(LS_WARNING) << "SplitByFrames length mismatch"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 399 | return kFrameSplitError; |
| 400 | } |
| 401 | |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 402 | if (packet->payload.size() == bytes_per_frame) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 403 | // Special case. Do not split the payload. |
| 404 | return kNoSplit; |
| 405 | } |
| 406 | |
| 407 | uint32_t timestamp = packet->header.timestamp; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 408 | const uint8_t* payload_ptr = packet->payload.data(); |
| 409 | size_t len = packet->payload.size(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 410 | while (len > 0) { |
| 411 | assert(len >= bytes_per_frame); |
| 412 | Packet* new_packet = new Packet; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 413 | new_packet->header = packet->header; |
| 414 | new_packet->header.timestamp = timestamp; |
| 415 | timestamp += timestamps_per_frame; |
| 416 | new_packet->primary = packet->primary; |
ossu | dc431ce | 2016-08-31 08:51:13 -0700 | [diff] [blame] | 417 | new_packet->payload.SetData(payload_ptr, bytes_per_frame); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 418 | payload_ptr += bytes_per_frame; |
| 419 | new_packets->push_back(new_packet); |
| 420 | len -= bytes_per_frame; |
| 421 | } |
| 422 | return kOK; |
| 423 | } |
| 424 | |
| 425 | } // namespace webrtc |