blob: c3ed4d53b2a43ff9e6dec909cfde4b165951d3a9 [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
Rasmus Brandt13a8f202017-10-31 16:44:23 +010013#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/logging.h"
15#include "rtc_base/scoped_ref_ptr.h"
brandtra8b38552016-10-10 16:44:57 -070016
17namespace webrtc {
18
19namespace {
20
21using Packet = ForwardErrorCorrection::Packet;
22using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket;
23
24// Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet.
25constexpr size_t kMinFlexfecHeaderSize = 20;
26
27// How often to log the recovered packets to the text log.
28constexpr int kPacketLogIntervalMs = 10000;
29
30} // namespace
31
brandtrb29e6522016-12-21 06:37:18 -080032FlexfecReceiver::FlexfecReceiver(
33 uint32_t ssrc,
34 uint32_t protected_media_ssrc,
35 RecoveredPacketReceiver* recovered_packet_receiver)
brandtr0a4c1612016-11-03 08:18:27 -070036 : ssrc_(ssrc),
brandtra8b38552016-10-10 16:44:57 -070037 protected_media_ssrc_(protected_media_ssrc),
brandtrd726a3f2017-06-29 02:45:35 -070038 erasure_code_(
39 ForwardErrorCorrection::CreateFlexfec(ssrc, protected_media_ssrc)),
brandtrb29e6522016-12-21 06:37:18 -080040 recovered_packet_receiver_(recovered_packet_receiver),
brandtra8b38552016-10-10 16:44:57 -070041 clock_(Clock::GetRealTimeClock()),
42 last_recovered_packet_ms_(-1) {
43 // It's OK to create this object on a different thread/task queue than
44 // the one used during main operation.
45 sequence_checker_.Detach();
46}
47
brandtr0a4c1612016-11-03 08:18:27 -070048FlexfecReceiver::~FlexfecReceiver() = default;
brandtra8b38552016-10-10 16:44:57 -070049
nisse5c29a7a2017-02-16 06:52:32 -080050void FlexfecReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080051 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
Rasmus Brandta00137c2017-12-12 10:01:20 +010052
53 // If this packet was recovered, it might be originating from
54 // ProcessReceivedPacket in this object. To avoid lifetime issues with
55 // |recovered_packets_|, we therefore break the cycle here.
56 // This might reduce decoding efficiency a bit, since we can't disambiguate
57 // recovered packets by RTX from recovered packets by FlexFEC.
58 if (packet.recovered())
59 return;
60
nissea5f043f2017-09-18 07:58:59 -070061 std::unique_ptr<ReceivedPacket> received_packet = AddReceivedPacket(packet);
62 if (!received_packet)
nisse5c29a7a2017-02-16 06:52:32 -080063 return;
nissea5f043f2017-09-18 07:58:59 -070064
65 ProcessReceivedPacket(*received_packet);
brandtra8b38552016-10-10 16:44:57 -070066}
67
brandtr0a4c1612016-11-03 08:18:27 -070068FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
brandtr8b5c3452016-12-19 10:02:30 -080069 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070070 return packet_counter_;
71}
72
eladalonc0d481a2017-08-02 07:39:07 -070073// TODO(eladalon): Consider using packet.recovered() to avoid processing
74// recovered packets here.
nissea5f043f2017-09-18 07:58:59 -070075std::unique_ptr<ReceivedPacket> FlexfecReceiver::AddReceivedPacket(
76 const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080077 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070078
79 // RTP packets with a full base header (12 bytes), but without payload,
80 // could conceivably be useful in the decoding. Therefore we check
brandtrb29e6522016-12-21 06:37:18 -080081 // with a non-strict inequality here.
82 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
brandtra8b38552016-10-10 16:44:57 -070083
84 // Demultiplex based on SSRC, and insert into erasure code decoder.
85 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
brandtrb29e6522016-12-21 06:37:18 -080086 received_packet->seq_num = packet.SequenceNumber();
87 received_packet->ssrc = packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -070088 if (received_packet->ssrc == ssrc_) {
brandtrb29e6522016-12-21 06:37:18 -080089 // This is a FlexFEC packet.
90 if (packet.payload_size() < kMinFlexfecHeaderSize) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
nissea5f043f2017-09-18 07:58:59 -070092 return nullptr;
brandtra8b38552016-10-10 16:44:57 -070093 }
94 received_packet->is_fec = true;
95 ++packet_counter_.num_fec_packets;
brandtrb29e6522016-12-21 06:37:18 -080096
brandtra8b38552016-10-10 16:44:57 -070097 // Insert packet payload into erasure code.
98 // TODO(brandtr): Remove this memcpy when the FEC packet classes
99 // are using COW buffers internally.
100 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800101 auto payload = packet.payload();
danilchap96c15872016-11-21 01:35:29 -0800102 memcpy(received_packet->pkt->data, payload.data(), payload.size());
103 received_packet->pkt->length = payload.size();
brandtra8b38552016-10-10 16:44:57 -0700104 } else {
105 // This is a media packet, or a FlexFEC packet belonging to some
106 // other FlexFEC stream.
107 if (received_packet->ssrc != protected_media_ssrc_) {
nissea5f043f2017-09-18 07:58:59 -0700108 return nullptr;
brandtra8b38552016-10-10 16:44:57 -0700109 }
110 received_packet->is_fec = false;
brandtrb29e6522016-12-21 06:37:18 -0800111
brandtra8b38552016-10-10 16:44:57 -0700112 // Insert entire packet into erasure code.
113 // TODO(brandtr): Remove this memcpy too.
114 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800115 memcpy(received_packet->pkt->data, packet.data(), packet.size());
116 received_packet->pkt->length = packet.size();
brandtra8b38552016-10-10 16:44:57 -0700117 }
brandtrb29e6522016-12-21 06:37:18 -0800118
brandtra8b38552016-10-10 16:44:57 -0700119 ++packet_counter_.num_packets;
120
nissea5f043f2017-09-18 07:58:59 -0700121 return received_packet;
brandtra8b38552016-10-10 16:44:57 -0700122}
123
124// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700125// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700126// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700127// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700128// media packets through the callback. The latter behaviour makes sense
nisseb1f2ff92017-06-09 04:01:55 -0700129// for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver.
brandtra8b38552016-10-10 16:44:57 -0700130// Here, however, the received media pipeline is more decoupled from the
131// FlexFEC decoder, and we therefore do not interfere with the reception
132// of non-recovered media packets.
nissea5f043f2017-09-18 07:58:59 -0700133void FlexfecReceiver::ProcessReceivedPacket(
134 const ReceivedPacket& received_packet) {
brandtr8b5c3452016-12-19 10:02:30 -0800135 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -0700136
137 // Decode.
nissea5f043f2017-09-18 07:58:59 -0700138 erasure_code_->DecodeFec(received_packet, &recovered_packets_);
139
brandtra8b38552016-10-10 16:44:57 -0700140 // Return recovered packets through callback.
141 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandt13a8f202017-10-31 16:44:23 +0100142 RTC_CHECK(recovered_packet);
brandtra8b38552016-10-10 16:44:57 -0700143 if (recovered_packet->returned) {
144 continue;
145 }
146 ++packet_counter_.num_recovered_packets;
nissee4bcd6d2017-05-16 04:47:04 -0700147 // Set this flag first, since OnRecoveredPacket may end up here
148 // again, with the same packet.
149 recovered_packet->returned = true;
Rasmus Brandt13a8f202017-10-31 16:44:23 +0100150 RTC_CHECK(recovered_packet->pkt);
nissed2ef3142017-05-11 08:00:58 -0700151 recovered_packet_receiver_->OnRecoveredPacket(
152 recovered_packet->pkt->data, recovered_packet->pkt->length);
brandtra8b38552016-10-10 16:44:57 -0700153 // Periodically log the incoming packets.
154 int64_t now_ms = clock_->TimeInMilliseconds();
155 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) {
156 uint32_t media_ssrc =
157 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100158 RTC_LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc
159 << " from FlexFEC stream with SSRC: " << ssrc_ << ".";
brandtra8b38552016-10-10 16:44:57 -0700160 last_recovered_packet_ms_ = now_ms;
161 }
162 }
brandtra8b38552016-10-10 16:44:57 -0700163}
164
165} // namespace webrtc