Moved codec-specific audio packet splitting into decoders.
There's still some code run specifically for Opus w/ FEC. It will be addressed in a separate CL. BUG=webrtc:5805 Review-Url: https://codereview.webrtc.org/2326003002 Cr-Commit-Position: refs/heads/master@{#14319}
This commit is contained in:
@ -212,214 +212,4 @@ int PayloadSplitter::CheckRedPayloads(PacketList* packet_list,
|
||||
return num_deleted_packets;
|
||||
}
|
||||
|
||||
int PayloadSplitter::SplitAudio(PacketList* packet_list,
|
||||
const DecoderDatabase& decoder_database) {
|
||||
PacketList::iterator it = packet_list->begin();
|
||||
// Iterate through all packets in |packet_list|.
|
||||
while (it != packet_list->end()) {
|
||||
Packet* packet = (*it); // Just to make the notation more intuitive.
|
||||
// Get codec type for this payload.
|
||||
const DecoderDatabase::DecoderInfo* info =
|
||||
decoder_database.GetDecoderInfo(packet->header.payloadType);
|
||||
if (!info) {
|
||||
LOG(LS_WARNING) << "SplitAudio unknown payload type";
|
||||
return kUnknownPayloadType;
|
||||
}
|
||||
PacketList new_packets;
|
||||
switch (info->codec_type) {
|
||||
case NetEqDecoder::kDecoderPCMu:
|
||||
case NetEqDecoder::kDecoderPCMa: {
|
||||
// 8 bytes per ms; 8 timestamps per ms.
|
||||
SplitBySamples(packet, 8, 8, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCMu_2ch:
|
||||
case NetEqDecoder::kDecoderPCMa_2ch: {
|
||||
// 2 * 8 bytes per ms; 8 timestamps per ms.
|
||||
SplitBySamples(packet, 2 * 8, 8, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderG722: {
|
||||
// 8 bytes per ms; 16 timestamps per ms.
|
||||
SplitBySamples(packet, 8, 16, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16B: {
|
||||
// 16 bytes per ms; 8 timestamps per ms.
|
||||
SplitBySamples(packet, 16, 8, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16Bwb: {
|
||||
// 32 bytes per ms; 16 timestamps per ms.
|
||||
SplitBySamples(packet, 32, 16, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16Bswb32kHz: {
|
||||
// 64 bytes per ms; 32 timestamps per ms.
|
||||
SplitBySamples(packet, 64, 32, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16Bswb48kHz: {
|
||||
// 96 bytes per ms; 48 timestamps per ms.
|
||||
SplitBySamples(packet, 96, 48, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16B_2ch: {
|
||||
// 2 * 16 bytes per ms; 8 timestamps per ms.
|
||||
SplitBySamples(packet, 2 * 16, 8, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16Bwb_2ch: {
|
||||
// 2 * 32 bytes per ms; 16 timestamps per ms.
|
||||
SplitBySamples(packet, 2 * 32, 16, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch: {
|
||||
// 2 * 64 bytes per ms; 32 timestamps per ms.
|
||||
SplitBySamples(packet, 2 * 64, 32, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16Bswb48kHz_2ch: {
|
||||
// 2 * 96 bytes per ms; 48 timestamps per ms.
|
||||
SplitBySamples(packet, 2 * 96, 48, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderPCM16B_5ch: {
|
||||
// 5 * 16 bytes per ms; 8 timestamps per ms.
|
||||
SplitBySamples(packet, 5 * 16, 8, &new_packets);
|
||||
break;
|
||||
}
|
||||
case NetEqDecoder::kDecoderILBC: {
|
||||
size_t bytes_per_frame;
|
||||
int timestamps_per_frame;
|
||||
if (packet->payload.size() >= 950) {
|
||||
LOG(LS_WARNING) << "SplitAudio too large iLBC payload";
|
||||
return kTooLargePayload;
|
||||
}
|
||||
if (packet->payload.size() % 38 == 0) {
|
||||
// 20 ms frames.
|
||||
bytes_per_frame = 38;
|
||||
timestamps_per_frame = 160;
|
||||
} else if (packet->payload.size() % 50 == 0) {
|
||||
// 30 ms frames.
|
||||
bytes_per_frame = 50;
|
||||
timestamps_per_frame = 240;
|
||||
} else {
|
||||
LOG(LS_WARNING) << "SplitAudio invalid iLBC payload";
|
||||
return kFrameSplitError;
|
||||
}
|
||||
int ret = SplitByFrames(packet, bytes_per_frame, timestamps_per_frame,
|
||||
&new_packets);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
} else if (ret == kNoSplit) {
|
||||
// Do not split at all. Simply advance to the next packet in the list.
|
||||
++it;
|
||||
// We do not have any new packets to insert, and should not delete the
|
||||
// old one. Skip the code after the switch case, and jump straight to
|
||||
// the next packet in the while loop.
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
// Do not split at all. Simply advance to the next packet in the list.
|
||||
++it;
|
||||
// We do not have any new packets to insert, and should not delete the
|
||||
// old one. Skip the code after the switch case, and jump straight to
|
||||
// the next packet in the while loop.
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Insert new packets into original list, before the element pointed to by
|
||||
// iterator |it|.
|
||||
packet_list->splice(it, new_packets, new_packets.begin(),
|
||||
new_packets.end());
|
||||
// Delete old packet payload.
|
||||
delete (*it);
|
||||
// Remove |it| from the packet list. This operation effectively moves the
|
||||
// iterator |it| to the next packet in the list. Thus, we do not have to
|
||||
// increment it manually.
|
||||
it = packet_list->erase(it);
|
||||
}
|
||||
return kOK;
|
||||
}
|
||||
|
||||
void PayloadSplitter::SplitBySamples(const Packet* packet,
|
||||
size_t bytes_per_ms,
|
||||
uint32_t timestamps_per_ms,
|
||||
PacketList* new_packets) {
|
||||
assert(packet);
|
||||
assert(new_packets);
|
||||
|
||||
size_t split_size_bytes = packet->payload.size();
|
||||
|
||||
// Find a "chunk size" >= 20 ms and < 40 ms.
|
||||
size_t min_chunk_size = bytes_per_ms * 20;
|
||||
// Reduce the split size by half as long as |split_size_bytes| is at least
|
||||
// twice the minimum chunk size (so that the resulting size is at least as
|
||||
// large as the minimum chunk size).
|
||||
while (split_size_bytes >= 2 * min_chunk_size) {
|
||||
split_size_bytes >>= 1;
|
||||
}
|
||||
uint32_t timestamps_per_chunk = static_cast<uint32_t>(
|
||||
split_size_bytes * timestamps_per_ms / bytes_per_ms);
|
||||
uint32_t timestamp = packet->header.timestamp;
|
||||
|
||||
const uint8_t* payload_ptr = packet->payload.data();
|
||||
size_t len = packet->payload.size();
|
||||
while (len >= (2 * split_size_bytes)) {
|
||||
Packet* new_packet = new Packet;
|
||||
new_packet->header = packet->header;
|
||||
new_packet->header.timestamp = timestamp;
|
||||
timestamp += timestamps_per_chunk;
|
||||
new_packet->primary = packet->primary;
|
||||
new_packet->payload.SetData(payload_ptr, split_size_bytes);
|
||||
payload_ptr += split_size_bytes;
|
||||
new_packets->push_back(new_packet);
|
||||
len -= split_size_bytes;
|
||||
}
|
||||
|
||||
if (len > 0) {
|
||||
Packet* new_packet = new Packet;
|
||||
new_packet->header = packet->header;
|
||||
new_packet->header.timestamp = timestamp;
|
||||
new_packet->primary = packet->primary;
|
||||
new_packet->payload.SetData(payload_ptr, len);
|
||||
new_packets->push_back(new_packet);
|
||||
}
|
||||
}
|
||||
|
||||
int PayloadSplitter::SplitByFrames(const Packet* packet,
|
||||
size_t bytes_per_frame,
|
||||
uint32_t timestamps_per_frame,
|
||||
PacketList* new_packets) {
|
||||
if (packet->payload.size() % bytes_per_frame != 0) {
|
||||
LOG(LS_WARNING) << "SplitByFrames length mismatch";
|
||||
return kFrameSplitError;
|
||||
}
|
||||
|
||||
if (packet->payload.size() == bytes_per_frame) {
|
||||
// Special case. Do not split the payload.
|
||||
return kNoSplit;
|
||||
}
|
||||
|
||||
uint32_t timestamp = packet->header.timestamp;
|
||||
const uint8_t* payload_ptr = packet->payload.data();
|
||||
size_t len = packet->payload.size();
|
||||
while (len > 0) {
|
||||
assert(len >= bytes_per_frame);
|
||||
Packet* new_packet = new Packet;
|
||||
new_packet->header = packet->header;
|
||||
new_packet->header.timestamp = timestamp;
|
||||
timestamp += timestamps_per_frame;
|
||||
new_packet->primary = packet->primary;
|
||||
new_packet->payload.SetData(payload_ptr, bytes_per_frame);
|
||||
payload_ptr += bytes_per_frame;
|
||||
new_packets->push_back(new_packet);
|
||||
len -= bytes_per_frame;
|
||||
}
|
||||
return kOK;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
Reference in New Issue
Block a user