2013-01-29 12:09:21 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/neteq/red_payload_splitter.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <stddef.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2018-10-23 12:03:01 +02:00
|
|
|
#include <cstdint>
|
|
|
|
|
#include <list>
|
|
|
|
|
#include <utility>
|
2016-09-22 02:06:28 -07:00
|
|
|
#include <vector>
|
2013-01-29 12:09:21 +00:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/neteq/decoder_database.h"
|
2018-10-23 12:03:01 +02:00
|
|
|
#include "modules/audio_coding/neteq/packet.h"
|
|
|
|
|
#include "rtc_base/buffer.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/logging.h"
|
2017-11-22 10:42:26 +01:00
|
|
|
#include "rtc_base/numerics/safe_conversions.h"
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
// The method loops through a list of packets {A, B, C, ...}. Each packet is
|
|
|
|
|
// split into its corresponding RED payloads, {A1, A2, ...}, which is
|
2021-07-28 20:00:17 +02:00
|
|
|
// temporarily held in the list `new_packets`.
|
|
|
|
|
// When the first packet in `packet_list` has been processed, the original
|
|
|
|
|
// packet is replaced by the new ones in `new_packets`, so that `packet_list`
|
2020-09-29 10:51:42 +02:00
|
|
|
// becomes: {A1, A2, ..., B, C, ...}. The method then continues with B, and C,
|
|
|
|
|
// until all the original packets have been replaced by their split payloads.
|
2016-09-22 02:06:28 -07:00
|
|
|
bool RedPayloadSplitter::SplitRed(PacketList* packet_list) {
|
|
|
|
|
// Too many RED blocks indicates that something is wrong. Clamp it at some
|
|
|
|
|
// reasonable value.
|
|
|
|
|
const size_t kMaxRedBlocks = 32;
|
|
|
|
|
bool ret = true;
|
2013-01-29 12:09:21 +00:00
|
|
|
PacketList::iterator it = packet_list->begin();
|
|
|
|
|
while (it != packet_list->end()) {
|
2016-10-24 08:25:28 -07:00
|
|
|
const Packet& red_packet = *it;
|
2021-07-08 20:08:20 +02:00
|
|
|
RTC_DCHECK(!red_packet.payload.empty());
|
2016-10-24 08:25:28 -07:00
|
|
|
const uint8_t* payload_ptr = red_packet.payload.data();
|
2020-09-29 10:51:42 +02:00
|
|
|
size_t payload_length = red_packet.payload.size();
|
2013-01-29 12:09:21 +00:00
|
|
|
|
|
|
|
|
// Read RED headers (according to RFC 2198):
|
|
|
|
|
//
|
|
|
|
|
// 0 1 2 3
|
|
|
|
|
// 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
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// |F| block PT | timestamp offset | block length |
|
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
// Last RED header:
|
|
|
|
|
// 0 1 2 3 4 5 6 7
|
|
|
|
|
// +-+-+-+-+-+-+-+-+
|
|
|
|
|
// |0| Block PT |
|
|
|
|
|
// +-+-+-+-+-+-+-+-+
|
|
|
|
|
|
2016-08-31 08:51:13 -07:00
|
|
|
struct RedHeader {
|
|
|
|
|
uint8_t payload_type;
|
|
|
|
|
uint32_t timestamp;
|
|
|
|
|
size_t payload_length;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<RedHeader> new_headers;
|
2013-01-29 12:09:21 +00:00
|
|
|
bool last_block = false;
|
Use size_t more consistently for packet/payload lengths.
See design doc at https://docs.google.com/a/chromium.org/document/d/1I6nmE9D_BmCY-IoV6MDPY2V6WYpEI-dg2apWXTfZyUI/edit?usp=sharing for more information.
This CL was reviewed and approved in pieces in the following CLs:
https://webrtc-codereview.appspot.com/24209004/
https://webrtc-codereview.appspot.com/24229004/
https://webrtc-codereview.appspot.com/24259004/
https://webrtc-codereview.appspot.com/25109004/
https://webrtc-codereview.appspot.com/26099004/
https://webrtc-codereview.appspot.com/27069004/
https://webrtc-codereview.appspot.com/27969004/
https://webrtc-codereview.appspot.com/27989004/
https://webrtc-codereview.appspot.com/29009004/
https://webrtc-codereview.appspot.com/30929004/
https://webrtc-codereview.appspot.com/30939004/
https://webrtc-codereview.appspot.com/31999004/
Committing as TBR to the original reviewers.
BUG=chromium:81439
TEST=none
TBR=pthatcher,henrik.lundin,tina.legrand,stefan,tkchin,glaznev,kjellander,perkj,mflodman,henrika,asapersson,niklas.enbom
Review URL: https://webrtc-codereview.appspot.com/23129004
git-svn-id: http://webrtc.googlecode.com/svn/trunk@7726 4adac7df-926f-26a2-2b94-8c16560cd09d
2014-11-20 22:28:14 +00:00
|
|
|
size_t sum_length = 0;
|
2013-01-29 12:09:21 +00:00
|
|
|
while (!last_block) {
|
2020-09-29 10:51:42 +02:00
|
|
|
if (payload_length == 0) {
|
|
|
|
|
RTC_LOG(LS_WARNING) << "SplitRed header too short";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-08-31 08:51:13 -07:00
|
|
|
RedHeader new_header;
|
2013-01-29 12:09:21 +00:00
|
|
|
// Check the F bit. If F == 0, this was the last block.
|
|
|
|
|
last_block = ((*payload_ptr & 0x80) == 0);
|
|
|
|
|
// Bits 1 through 7 are payload type.
|
2016-08-31 08:51:13 -07:00
|
|
|
new_header.payload_type = payload_ptr[0] & 0x7F;
|
2013-01-29 12:09:21 +00:00
|
|
|
if (last_block) {
|
|
|
|
|
// No more header data to read.
|
2020-09-29 10:51:42 +02:00
|
|
|
sum_length += kRedLastHeaderLength; // Account for RED header size.
|
2016-10-24 08:25:28 -07:00
|
|
|
new_header.timestamp = red_packet.timestamp;
|
|
|
|
|
new_header.payload_length = red_packet.payload.size() - sum_length;
|
2020-09-29 10:51:42 +02:00
|
|
|
payload_ptr += kRedLastHeaderLength; // Advance to first payload byte.
|
|
|
|
|
payload_length -= kRedLastHeaderLength;
|
2013-01-29 12:09:21 +00:00
|
|
|
} else {
|
2020-09-29 10:51:42 +02:00
|
|
|
if (payload_length < kRedHeaderLength) {
|
|
|
|
|
RTC_LOG(LS_WARNING) << "SplitRed header too short";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-01-29 12:09:21 +00:00
|
|
|
// Bits 8 through 21 are timestamp offset.
|
2016-09-22 02:06:28 -07:00
|
|
|
int timestamp_offset =
|
|
|
|
|
(payload_ptr[1] << 6) + ((payload_ptr[2] & 0xFC) >> 2);
|
2016-10-24 08:25:28 -07:00
|
|
|
new_header.timestamp = red_packet.timestamp - timestamp_offset;
|
2013-01-29 12:09:21 +00:00
|
|
|
// Bits 22 through 31 are payload length.
|
2016-08-31 08:51:13 -07:00
|
|
|
new_header.payload_length =
|
|
|
|
|
((payload_ptr[2] & 0x03) << 8) + payload_ptr[3];
|
2020-09-29 10:51:42 +02:00
|
|
|
|
|
|
|
|
sum_length += new_header.payload_length;
|
|
|
|
|
sum_length += kRedHeaderLength; // Account for RED header size.
|
|
|
|
|
|
|
|
|
|
payload_ptr += kRedHeaderLength; // Advance to next RED header.
|
|
|
|
|
payload_length -= kRedHeaderLength;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
// Store in new list of packets.
|
2020-09-29 17:10:33 +02:00
|
|
|
if (new_header.payload_length > 0) {
|
|
|
|
|
new_headers.push_back(new_header);
|
|
|
|
|
}
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-22 02:06:28 -07:00
|
|
|
if (new_headers.size() <= kMaxRedBlocks) {
|
|
|
|
|
// Populate the new packets with payload data.
|
2021-07-28 20:00:17 +02:00
|
|
|
// `payload_ptr` now points at the first payload byte.
|
2016-09-22 02:06:28 -07:00
|
|
|
PacketList new_packets; // An empty list to store the split packets in.
|
|
|
|
|
for (size_t i = 0; i != new_headers.size(); ++i) {
|
|
|
|
|
const auto& new_header = new_headers[i];
|
|
|
|
|
size_t payload_length = new_header.payload_length;
|
|
|
|
|
if (payload_ptr + payload_length >
|
2016-10-24 08:25:28 -07:00
|
|
|
red_packet.payload.data() + red_packet.payload.size()) {
|
2016-09-22 02:06:28 -07:00
|
|
|
// The block lengths in the RED headers do not match the overall
|
|
|
|
|
// packet length. Something is corrupt. Discard this and the remaining
|
|
|
|
|
// payloads from this packet.
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "SplitRed length mismatch";
|
2016-09-22 02:06:28 -07:00
|
|
|
ret = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-24 08:25:28 -07:00
|
|
|
Packet new_packet;
|
|
|
|
|
new_packet.timestamp = new_header.timestamp;
|
|
|
|
|
new_packet.payload_type = new_header.payload_type;
|
|
|
|
|
new_packet.sequence_number = red_packet.sequence_number;
|
|
|
|
|
new_packet.priority.red_level =
|
2017-03-01 18:52:48 -08:00
|
|
|
rtc::dchecked_cast<int>((new_headers.size() - 1) - i);
|
2016-10-24 08:25:28 -07:00
|
|
|
new_packet.payload.SetData(payload_ptr, payload_length);
|
Reland "Reland "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker.""
This reverts commit fab3460a821abe336ab610c6d6dfc0d392dac263.
Reason for revert: fix downstream instead
Original change's description:
> Revert "Reland "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker.""
>
> This reverts commit 9973933d2e606d64fcdc753acb9ba3afd6e30569.
>
> Reason for revert: breaking downstream projects and not reviewed by direct owners
>
> Original change's description:
> > Reland "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker."
> >
> > This reverts commit 24192c267a40eb7d6b1850489ccdbf7a84f8ff0f.
> >
> > Reason for revert: Analyzed the performance regression in more detail.
> >
> > Most of the regression comes from the extra RtpPacketInfos-related memory allocations in every `NetEq::GetAudio()` call. Commit 1796a820f60cb9429bf4bcf13a40a41794ac8fb0 has removed roughly 2/3rds of the extra allocations from the impacted perf tests. Remaining perf impact is expected to be about "8 microseconds of CPU time per second" on the Linux benchmarking machines and "15 us per second" on Windows/Mac.
> >
> > There are options to optimize further but they are unlikely worth doing. Note for example that `NetEqPerformanceTest` uses the PCM codec while the real-world use cases would likely use the much heavier Opus codec. The numbers from `OpusSpeedTest` and `NetEqPerformanceTest` suggest that Opus decoding is about 10x as expensive as NetEq overall.
> >
> > Original change's description:
> > > Revert "Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker."
> > >
> > > This reverts commit 3e8ef940fe86cf6285afb80e68d2a0bedc631b9f.
> > >
> > > Reason for revert: This CL causes a performance regression in NetEq, see https://bugs.chromium.org/p/chromium/issues/detail?id=982260.
> > >
> > > Original change's description:
> > > > Add plumbing of RtpPacketInfos to each AudioFrame as input for SourceTracker.
> > > >
> > > > This change adds the plumbing of RtpPacketInfo from ChannelReceive::OnRtpPacket() to ChannelReceive::GetAudioFrameWithInfo() for audio. It is a step towards replacing the non-spec compliant ContributingSources that updates itself at packet-receive time, with the spec-compliant SourceTracker that will update itself at frame-delivery-to-track time.
> > > >
> > > > Bug: webrtc:10668
> > > > Change-Id: I03385d6865bbc7bfbef7634f88de820a934f787a
> > > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/139890
> > > > Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> > > > Reviewed-by: Minyue Li <minyue@webrtc.org>
> > > > Commit-Queue: Chen Xing <chxg@google.com>
> > > > Cr-Commit-Position: refs/heads/master@{#28434}
> > >
> > > TBR=kwiberg@webrtc.org,stefan@webrtc.org,minyue@webrtc.org,chxg@google.com
> > >
> > > Bug: webrtc:10668, chromium:982260
> > > Change-Id: I5e2cfde78c59d1123e21869564d76ed3f6193a5c
> > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/145339
> > > Reviewed-by: Ivo Creusen <ivoc@webrtc.org>
> > > Commit-Queue: Ivo Creusen <ivoc@webrtc.org>
> > > Cr-Commit-Position: refs/heads/master@{#28561}
> >
> > TBR=kwiberg@webrtc.org,stefan@webrtc.org,ivoc@webrtc.org,minyue@webrtc.org,chxg@google.com
> >
> > # Not skipping CQ checks because original CL landed > 1 day ago.
> >
> > Bug: webrtc:10668, chromium:982260
> > Change-Id: Ie375a0b327ee368317bf3a04b2f1415c3a974470
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146707
> > Reviewed-by: Stefan Holmer <stefan@webrtc.org>
> > Commit-Queue: Chen Xing <chxg@google.com>
> > Cr-Commit-Position: refs/heads/master@{#28664}
>
> TBR=kwiberg@webrtc.org,stefan@webrtc.org,ivoc@webrtc.org,minyue@webrtc.org,chxg@google.com
>
> Change-Id: I652cb0814d83b514d3bee34e65ca3bb693099b22
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:10668, chromium:982260
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146712
> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
> Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#28671}
TBR=alessiob@webrtc.org,kwiberg@webrtc.org,stefan@webrtc.org,ivoc@webrtc.org,minyue@webrtc.org,chxg@google.com
Change-Id: Id43b7b3da79b4f48004b41767482bae1c1fa1e16
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:10668, chromium:982260
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146713
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Commit-Queue: Alessio Bazzica <alessiob@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28672}
2019-07-24 16:47:02 +00:00
|
|
|
new_packet.packet_info = RtpPacketInfo(
|
|
|
|
|
/*ssrc=*/red_packet.packet_info.ssrc(),
|
|
|
|
|
/*csrcs=*/std::vector<uint32_t>(),
|
|
|
|
|
/*rtp_timestamp=*/new_packet.timestamp,
|
2021-04-30 13:10:56 +02:00
|
|
|
/*receive_time=*/red_packet.packet_info.receive_time());
|
2022-09-19 18:05:29 +02:00
|
|
|
new_packet.packet_info.set_audio_level(
|
|
|
|
|
red_packet.packet_info.audio_level());
|
2016-10-24 08:25:28 -07:00
|
|
|
new_packets.push_front(std::move(new_packet));
|
2016-09-22 02:06:28 -07:00
|
|
|
payload_ptr += payload_length;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
2016-09-22 02:06:28 -07:00
|
|
|
// Insert new packets into original list, before the element pointed to by
|
2021-07-28 20:00:17 +02:00
|
|
|
// iterator `it`.
|
2016-10-24 08:25:28 -07:00
|
|
|
packet_list->splice(it, std::move(new_packets));
|
2016-09-22 02:06:28 -07:00
|
|
|
} else {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size();
|
2016-09-22 02:06:28 -07:00
|
|
|
ret = false;
|
2013-01-29 12:09:21 +00:00
|
|
|
}
|
2021-07-28 20:00:17 +02:00
|
|
|
// 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
|
2013-01-29 12:09:21 +00:00
|
|
|
// increment it manually.
|
|
|
|
|
it = packet_list->erase(it);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 13:07:30 +02:00
|
|
|
void RedPayloadSplitter::CheckRedPayloads(
|
2016-09-22 02:06:28 -07:00
|
|
|
PacketList* packet_list,
|
|
|
|
|
const DecoderDatabase& decoder_database) {
|
2013-01-29 12:09:21 +00:00
|
|
|
int main_payload_type = -1;
|
2016-10-24 08:25:28 -07:00
|
|
|
for (auto it = packet_list->begin(); it != packet_list->end(); /* */) {
|
|
|
|
|
uint8_t this_payload_type = it->payload_type;
|
2018-07-03 13:07:30 +02:00
|
|
|
if (decoder_database.IsRed(this_payload_type)) {
|
|
|
|
|
it = packet_list->erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-01-29 12:09:21 +00:00
|
|
|
if (!decoder_database.IsDtmf(this_payload_type) &&
|
|
|
|
|
!decoder_database.IsComfortNoise(this_payload_type)) {
|
|
|
|
|
if (main_payload_type == -1) {
|
|
|
|
|
// This is the first packet in the list which is non-DTMF non-CNG.
|
|
|
|
|
main_payload_type = this_payload_type;
|
|
|
|
|
} else {
|
|
|
|
|
if (this_payload_type != main_payload_type) {
|
|
|
|
|
// We do not allow redundant payloads of a different type.
|
2021-07-28 20:00:17 +02:00
|
|
|
// Remove `it` from the packet list. This operation effectively
|
|
|
|
|
// moves the iterator `it` to the next packet in the list. Thus, we
|
2013-01-29 12:09:21 +00:00
|
|
|
// do not have to increment it manually.
|
|
|
|
|
it = packet_list->erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|