blob: 5a625d89a20302b260ebac7feb79d37d9385d82c [file] [log] [blame]
brandtra8b38552016-10-10 16:44:57 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/include/flexfec_receiver.h"
brandtra8b38552016-10-10 16:44:57 -070012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/logging.h"
14#include "rtc_base/scoped_ref_ptr.h"
brandtra8b38552016-10-10 16:44:57 -070015
16namespace webrtc {
17
18namespace {
19
20using Packet = ForwardErrorCorrection::Packet;
21using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket;
22
23// Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet.
24constexpr size_t kMinFlexfecHeaderSize = 20;
25
26// How often to log the recovered packets to the text log.
27constexpr int kPacketLogIntervalMs = 10000;
28
29} // namespace
30
brandtrb29e6522016-12-21 06:37:18 -080031FlexfecReceiver::FlexfecReceiver(
32 uint32_t ssrc,
33 uint32_t protected_media_ssrc,
34 RecoveredPacketReceiver* recovered_packet_receiver)
brandtr0a4c1612016-11-03 08:18:27 -070035 : ssrc_(ssrc),
brandtra8b38552016-10-10 16:44:57 -070036 protected_media_ssrc_(protected_media_ssrc),
brandtrd726a3f2017-06-29 02:45:35 -070037 erasure_code_(
38 ForwardErrorCorrection::CreateFlexfec(ssrc, protected_media_ssrc)),
brandtrb29e6522016-12-21 06:37:18 -080039 recovered_packet_receiver_(recovered_packet_receiver),
brandtra8b38552016-10-10 16:44:57 -070040 clock_(Clock::GetRealTimeClock()),
41 last_recovered_packet_ms_(-1) {
42 // It's OK to create this object on a different thread/task queue than
43 // the one used during main operation.
44 sequence_checker_.Detach();
45}
46
brandtr0a4c1612016-11-03 08:18:27 -070047FlexfecReceiver::~FlexfecReceiver() = default;
brandtra8b38552016-10-10 16:44:57 -070048
nisse5c29a7a2017-02-16 06:52:32 -080049void FlexfecReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080050 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
nisse5c29a7a2017-02-16 06:52:32 -080051 if (!AddReceivedPacket(packet)) {
52 return;
brandtra8b38552016-10-10 16:44:57 -070053 }
nisse5c29a7a2017-02-16 06:52:32 -080054 ProcessReceivedPackets();
brandtra8b38552016-10-10 16:44:57 -070055}
56
brandtr0a4c1612016-11-03 08:18:27 -070057FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
brandtr8b5c3452016-12-19 10:02:30 -080058 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070059 return packet_counter_;
60}
61
eladalonc0d481a2017-08-02 07:39:07 -070062// TODO(eladalon): Consider using packet.recovered() to avoid processing
63// recovered packets here.
brandtrfa5a3682017-01-17 01:33:54 -080064bool FlexfecReceiver::AddReceivedPacket(const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080065 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070066
67 // RTP packets with a full base header (12 bytes), but without payload,
68 // could conceivably be useful in the decoding. Therefore we check
brandtrb29e6522016-12-21 06:37:18 -080069 // with a non-strict inequality here.
70 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
brandtra8b38552016-10-10 16:44:57 -070071
72 // Demultiplex based on SSRC, and insert into erasure code decoder.
73 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
brandtrb29e6522016-12-21 06:37:18 -080074 received_packet->seq_num = packet.SequenceNumber();
75 received_packet->ssrc = packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -070076 if (received_packet->ssrc == ssrc_) {
brandtrb29e6522016-12-21 06:37:18 -080077 // This is a FlexFEC packet.
78 if (packet.payload_size() < kMinFlexfecHeaderSize) {
brandtra8b38552016-10-10 16:44:57 -070079 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
80 return false;
81 }
82 received_packet->is_fec = true;
83 ++packet_counter_.num_fec_packets;
brandtrb29e6522016-12-21 06:37:18 -080084
brandtra8b38552016-10-10 16:44:57 -070085 // Insert packet payload into erasure code.
86 // TODO(brandtr): Remove this memcpy when the FEC packet classes
87 // are using COW buffers internally.
88 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -080089 auto payload = packet.payload();
danilchap96c15872016-11-21 01:35:29 -080090 memcpy(received_packet->pkt->data, payload.data(), payload.size());
91 received_packet->pkt->length = payload.size();
brandtra8b38552016-10-10 16:44:57 -070092 } else {
93 // This is a media packet, or a FlexFEC packet belonging to some
94 // other FlexFEC stream.
95 if (received_packet->ssrc != protected_media_ssrc_) {
96 return false;
97 }
98 received_packet->is_fec = false;
brandtrb29e6522016-12-21 06:37:18 -080099
brandtra8b38552016-10-10 16:44:57 -0700100 // Insert entire packet into erasure code.
101 // TODO(brandtr): Remove this memcpy too.
102 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800103 memcpy(received_packet->pkt->data, packet.data(), packet.size());
104 received_packet->pkt->length = packet.size();
brandtra8b38552016-10-10 16:44:57 -0700105 }
brandtrb29e6522016-12-21 06:37:18 -0800106
brandtra8b38552016-10-10 16:44:57 -0700107 received_packets_.push_back(std::move(received_packet));
108 ++packet_counter_.num_packets;
109
110 return true;
111}
112
113// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700114// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700115// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700116// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700117// media packets through the callback. The latter behaviour makes sense
nisseb1f2ff92017-06-09 04:01:55 -0700118// for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver.
brandtra8b38552016-10-10 16:44:57 -0700119// Here, however, the received media pipeline is more decoupled from the
120// FlexFEC decoder, and we therefore do not interfere with the reception
121// of non-recovered media packets.
brandtr0a4c1612016-11-03 08:18:27 -0700122bool FlexfecReceiver::ProcessReceivedPackets() {
brandtr8b5c3452016-12-19 10:02:30 -0800123 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -0700124
125 // Decode.
126 if (!received_packets_.empty()) {
127 if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) !=
128 0) {
129 return false;
130 }
131 }
132 // Return recovered packets through callback.
133 for (const auto& recovered_packet : recovered_packets_) {
134 if (recovered_packet->returned) {
135 continue;
136 }
137 ++packet_counter_.num_recovered_packets;
nissee4bcd6d2017-05-16 04:47:04 -0700138 // Set this flag first, since OnRecoveredPacket may end up here
139 // again, with the same packet.
140 recovered_packet->returned = true;
nissed2ef3142017-05-11 08:00:58 -0700141 recovered_packet_receiver_->OnRecoveredPacket(
142 recovered_packet->pkt->data, recovered_packet->pkt->length);
brandtra8b38552016-10-10 16:44:57 -0700143 // Periodically log the incoming packets.
144 int64_t now_ms = clock_->TimeInMilliseconds();
145 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) {
146 uint32_t media_ssrc =
147 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data);
brandtr658024e2017-01-10 06:49:58 -0800148 LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc
149 << " from FlexFEC stream with SSRC: " << ssrc_ << ".";
brandtra8b38552016-10-10 16:44:57 -0700150 last_recovered_packet_ms_ = now_ms;
151 }
152 }
153 return true;
154}
155
156} // namespace webrtc