blob: d9850bf0f83308831b64e7c5dadb7ce77b3d920d [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <string.h>
14
15#include "api/array_view.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Rasmus Brandt13a8f202017-10-31 16:44:23 +010017#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/logging.h"
brandtra8b38552016-10-10 16:44:57 -070019
20namespace webrtc {
21
22namespace {
23
24using Packet = ForwardErrorCorrection::Packet;
25using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket;
26
27// Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet.
28constexpr size_t kMinFlexfecHeaderSize = 20;
29
30// How often to log the recovered packets to the text log.
31constexpr int kPacketLogIntervalMs = 10000;
32
33} // namespace
34
brandtrb29e6522016-12-21 06:37:18 -080035FlexfecReceiver::FlexfecReceiver(
36 uint32_t ssrc,
37 uint32_t protected_media_ssrc,
38 RecoveredPacketReceiver* recovered_packet_receiver)
Sebastian Jansson8026d602019-03-04 19:39:01 +010039 : FlexfecReceiver(Clock::GetRealTimeClock(),
40 ssrc,
41 protected_media_ssrc,
42 recovered_packet_receiver) {}
43
44FlexfecReceiver::FlexfecReceiver(
45 Clock* clock,
46 uint32_t ssrc,
47 uint32_t protected_media_ssrc,
48 RecoveredPacketReceiver* recovered_packet_receiver)
brandtr0a4c1612016-11-03 08:18:27 -070049 : ssrc_(ssrc),
brandtra8b38552016-10-10 16:44:57 -070050 protected_media_ssrc_(protected_media_ssrc),
brandtrd726a3f2017-06-29 02:45:35 -070051 erasure_code_(
52 ForwardErrorCorrection::CreateFlexfec(ssrc, protected_media_ssrc)),
brandtrb29e6522016-12-21 06:37:18 -080053 recovered_packet_receiver_(recovered_packet_receiver),
Sebastian Jansson8026d602019-03-04 19:39:01 +010054 clock_(clock),
brandtra8b38552016-10-10 16:44:57 -070055 last_recovered_packet_ms_(-1) {
56 // It's OK to create this object on a different thread/task queue than
57 // the one used during main operation.
58 sequence_checker_.Detach();
59}
60
brandtr0a4c1612016-11-03 08:18:27 -070061FlexfecReceiver::~FlexfecReceiver() = default;
brandtra8b38552016-10-10 16:44:57 -070062
nisse5c29a7a2017-02-16 06:52:32 -080063void FlexfecReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020064 RTC_DCHECK_RUN_ON(&sequence_checker_);
Rasmus Brandta00137c2017-12-12 10:01:20 +010065
66 // If this packet was recovered, it might be originating from
67 // ProcessReceivedPacket in this object. To avoid lifetime issues with
68 // |recovered_packets_|, we therefore break the cycle here.
69 // This might reduce decoding efficiency a bit, since we can't disambiguate
70 // recovered packets by RTX from recovered packets by FlexFEC.
71 if (packet.recovered())
72 return;
73
nissea5f043f2017-09-18 07:58:59 -070074 std::unique_ptr<ReceivedPacket> received_packet = AddReceivedPacket(packet);
75 if (!received_packet)
nisse5c29a7a2017-02-16 06:52:32 -080076 return;
nissea5f043f2017-09-18 07:58:59 -070077
78 ProcessReceivedPacket(*received_packet);
brandtra8b38552016-10-10 16:44:57 -070079}
80
brandtr0a4c1612016-11-03 08:18:27 -070081FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020082 RTC_DCHECK_RUN_ON(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070083 return packet_counter_;
84}
85
eladalonc0d481a2017-08-02 07:39:07 -070086// TODO(eladalon): Consider using packet.recovered() to avoid processing
87// recovered packets here.
nissea5f043f2017-09-18 07:58:59 -070088std::unique_ptr<ReceivedPacket> FlexfecReceiver::AddReceivedPacket(
89 const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020090 RTC_DCHECK_RUN_ON(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070091
92 // RTP packets with a full base header (12 bytes), but without payload,
93 // could conceivably be useful in the decoding. Therefore we check
brandtrb29e6522016-12-21 06:37:18 -080094 // with a non-strict inequality here.
95 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
brandtra8b38552016-10-10 16:44:57 -070096
97 // Demultiplex based on SSRC, and insert into erasure code decoder.
98 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
brandtrb29e6522016-12-21 06:37:18 -080099 received_packet->seq_num = packet.SequenceNumber();
100 received_packet->ssrc = packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -0700101 if (received_packet->ssrc == ssrc_) {
brandtrb29e6522016-12-21 06:37:18 -0800102 // This is a FlexFEC packet.
103 if (packet.payload_size() < kMinFlexfecHeaderSize) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100104 RTC_LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
nissea5f043f2017-09-18 07:58:59 -0700105 return nullptr;
brandtra8b38552016-10-10 16:44:57 -0700106 }
107 received_packet->is_fec = true;
108 ++packet_counter_.num_fec_packets;
brandtrb29e6522016-12-21 06:37:18 -0800109
brandtra8b38552016-10-10 16:44:57 -0700110 // Insert packet payload into erasure code.
111 // TODO(brandtr): Remove this memcpy when the FEC packet classes
112 // are using COW buffers internally.
113 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800114 auto payload = packet.payload();
danilchap96c15872016-11-21 01:35:29 -0800115 memcpy(received_packet->pkt->data, payload.data(), payload.size());
116 received_packet->pkt->length = payload.size();
brandtra8b38552016-10-10 16:44:57 -0700117 } else {
118 // This is a media packet, or a FlexFEC packet belonging to some
119 // other FlexFEC stream.
120 if (received_packet->ssrc != protected_media_ssrc_) {
nissea5f043f2017-09-18 07:58:59 -0700121 return nullptr;
brandtra8b38552016-10-10 16:44:57 -0700122 }
123 received_packet->is_fec = false;
brandtrb29e6522016-12-21 06:37:18 -0800124
brandtra8b38552016-10-10 16:44:57 -0700125 // Insert entire packet into erasure code.
126 // TODO(brandtr): Remove this memcpy too.
127 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800128 memcpy(received_packet->pkt->data, packet.data(), packet.size());
129 received_packet->pkt->length = packet.size();
brandtra8b38552016-10-10 16:44:57 -0700130 }
brandtrb29e6522016-12-21 06:37:18 -0800131
brandtra8b38552016-10-10 16:44:57 -0700132 ++packet_counter_.num_packets;
133
nissea5f043f2017-09-18 07:58:59 -0700134 return received_packet;
brandtra8b38552016-10-10 16:44:57 -0700135}
136
137// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700138// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700139// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700140// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700141// media packets through the callback. The latter behaviour makes sense
nisseb1f2ff92017-06-09 04:01:55 -0700142// for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver.
brandtra8b38552016-10-10 16:44:57 -0700143// Here, however, the received media pipeline is more decoupled from the
144// FlexFEC decoder, and we therefore do not interfere with the reception
145// of non-recovered media packets.
nissea5f043f2017-09-18 07:58:59 -0700146void FlexfecReceiver::ProcessReceivedPacket(
147 const ReceivedPacket& received_packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200148 RTC_DCHECK_RUN_ON(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -0700149
150 // Decode.
nissea5f043f2017-09-18 07:58:59 -0700151 erasure_code_->DecodeFec(received_packet, &recovered_packets_);
152
brandtra8b38552016-10-10 16:44:57 -0700153 // Return recovered packets through callback.
154 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandt13a8f202017-10-31 16:44:23 +0100155 RTC_CHECK(recovered_packet);
brandtra8b38552016-10-10 16:44:57 -0700156 if (recovered_packet->returned) {
157 continue;
158 }
159 ++packet_counter_.num_recovered_packets;
nissee4bcd6d2017-05-16 04:47:04 -0700160 // Set this flag first, since OnRecoveredPacket may end up here
161 // again, with the same packet.
162 recovered_packet->returned = true;
Rasmus Brandt13a8f202017-10-31 16:44:23 +0100163 RTC_CHECK(recovered_packet->pkt);
nissed2ef3142017-05-11 08:00:58 -0700164 recovered_packet_receiver_->OnRecoveredPacket(
165 recovered_packet->pkt->data, recovered_packet->pkt->length);
brandtra8b38552016-10-10 16:44:57 -0700166 // Periodically log the incoming packets.
167 int64_t now_ms = clock_->TimeInMilliseconds();
168 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) {
169 uint32_t media_ssrc =
170 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100171 RTC_LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc
172 << " from FlexFEC stream with SSRC: " << ssrc_ << ".";
brandtra8b38552016-10-10 16:44:57 -0700173 last_recovered_packet_ms_ = now_ms;
174 }
175 }
brandtra8b38552016-10-10 16:44:57 -0700176}
177
178} // namespace webrtc