Reland "Refactor FEC code to use COW buffers"

Reland with fixes for fuzzer found crashes.

This refactoring helps to reduce unnecessary memcpy calls on the receive side.

This CL replaces |uint8 data[IP_PACKET_SIZE]| with |rtc::CopyOnWriteBuffer data| in Packet class, removes |length| field there, and does necessary changes.

Original Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/145332

Bug: webrtc:10750
Change-Id: I6775a701bcb2ae25ec1666e1db90041cd49013b7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/151131
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29116}
This commit is contained in:
Ilya Nikolaevskiy
2019-09-03 11:07:37 +02:00
committed by Commit Bot
parent 4d7dac6d3b
commit a5d952f4be
25 changed files with 358 additions and 328 deletions

View File

@ -107,13 +107,12 @@ FlexfecReceiver::AddReceivedPacket(const RtpPacketReceived& packet) {
++packet_counter_.num_fec_packets;
// Insert packet payload into erasure code.
// TODO(brandtr): Remove this memcpy when the FEC packet classes
// are using COW buffers internally.
received_packet->pkt = rtc::scoped_refptr<ForwardErrorCorrection::Packet>(
new ForwardErrorCorrection::Packet());
// TODO(ilnik): after slice capability is added to COW, use it here instead
// of initializing COW buffer with ArrayView.
auto payload = packet.payload();
memcpy(received_packet->pkt->data, payload.data(), payload.size());
received_packet->pkt->length = payload.size();
received_packet->pkt->data.SetData(payload.data(), payload.size());
} else {
// This is a media packet, or a FlexFEC packet belonging to some
// other FlexFEC stream.
@ -123,11 +122,12 @@ FlexfecReceiver::AddReceivedPacket(const RtpPacketReceived& packet) {
received_packet->is_fec = false;
// Insert entire packet into erasure code.
// Create a copy and fill with zeros all mutable extensions.
received_packet->pkt = rtc::scoped_refptr<ForwardErrorCorrection::Packet>(
new ForwardErrorCorrection::Packet());
// Create a copy and fill with zeros all mutable extensions.
packet.CopyAndZeroMutableExtensions(received_packet->pkt->data);
received_packet->pkt->length = packet.size();
RtpPacketReceived packet_copy(packet);
packet_copy.ZeroMutableExtensions();
received_packet->pkt->data = packet_copy.Buffer();
}
++packet_counter_.num_packets;
@ -161,14 +161,15 @@ void FlexfecReceiver::ProcessReceivedPacket(
// Set this flag first, since OnRecoveredPacket may end up here
// again, with the same packet.
recovered_packet->returned = true;
RTC_CHECK(recovered_packet->pkt);
RTC_CHECK_GT(recovered_packet->pkt->data.size(), 0);
recovered_packet_receiver_->OnRecoveredPacket(
recovered_packet->pkt->data, recovered_packet->pkt->length);
recovered_packet->pkt->data.cdata(),
recovered_packet->pkt->data.size());
// Periodically log the incoming packets.
int64_t now_ms = clock_->TimeInMilliseconds();
if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) {
uint32_t media_ssrc =
ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data);
ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data.data());
RTC_LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc
<< " from FlexFEC stream with SSRC: " << ssrc_ << ".";
last_recovered_packet_ms_ = now_ms;