blob: e26a51b39cbc776ce9c7bfcf9d137392a4ec70b3 [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_);
nissea5f043f2017-09-18 07:58:59 -070052 std::unique_ptr<ReceivedPacket> received_packet = AddReceivedPacket(packet);
53 if (!received_packet)
nisse5c29a7a2017-02-16 06:52:32 -080054 return;
nissea5f043f2017-09-18 07:58:59 -070055
56 ProcessReceivedPacket(*received_packet);
brandtra8b38552016-10-10 16:44:57 -070057}
58
brandtr0a4c1612016-11-03 08:18:27 -070059FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
brandtr8b5c3452016-12-19 10:02:30 -080060 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070061 return packet_counter_;
62}
63
eladalonc0d481a2017-08-02 07:39:07 -070064// TODO(eladalon): Consider using packet.recovered() to avoid processing
65// recovered packets here.
nissea5f043f2017-09-18 07:58:59 -070066std::unique_ptr<ReceivedPacket> FlexfecReceiver::AddReceivedPacket(
67 const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080068 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070069
70 // RTP packets with a full base header (12 bytes), but without payload,
71 // could conceivably be useful in the decoding. Therefore we check
brandtrb29e6522016-12-21 06:37:18 -080072 // with a non-strict inequality here.
73 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
brandtra8b38552016-10-10 16:44:57 -070074
75 // Demultiplex based on SSRC, and insert into erasure code decoder.
76 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
brandtrb29e6522016-12-21 06:37:18 -080077 received_packet->seq_num = packet.SequenceNumber();
78 received_packet->ssrc = packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -070079 if (received_packet->ssrc == ssrc_) {
brandtrb29e6522016-12-21 06:37:18 -080080 // This is a FlexFEC packet.
81 if (packet.payload_size() < kMinFlexfecHeaderSize) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010082 RTC_LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
nissea5f043f2017-09-18 07:58:59 -070083 return nullptr;
brandtra8b38552016-10-10 16:44:57 -070084 }
85 received_packet->is_fec = true;
86 ++packet_counter_.num_fec_packets;
brandtrb29e6522016-12-21 06:37:18 -080087
brandtra8b38552016-10-10 16:44:57 -070088 // Insert packet payload into erasure code.
89 // TODO(brandtr): Remove this memcpy when the FEC packet classes
90 // are using COW buffers internally.
91 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -080092 auto payload = packet.payload();
danilchap96c15872016-11-21 01:35:29 -080093 memcpy(received_packet->pkt->data, payload.data(), payload.size());
94 received_packet->pkt->length = payload.size();
brandtra8b38552016-10-10 16:44:57 -070095 } else {
96 // This is a media packet, or a FlexFEC packet belonging to some
97 // other FlexFEC stream.
98 if (received_packet->ssrc != protected_media_ssrc_) {
nissea5f043f2017-09-18 07:58:59 -070099 return nullptr;
brandtra8b38552016-10-10 16:44:57 -0700100 }
101 received_packet->is_fec = false;
brandtrb29e6522016-12-21 06:37:18 -0800102
brandtra8b38552016-10-10 16:44:57 -0700103 // Insert entire packet into erasure code.
104 // TODO(brandtr): Remove this memcpy too.
105 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800106 memcpy(received_packet->pkt->data, packet.data(), packet.size());
107 received_packet->pkt->length = packet.size();
brandtra8b38552016-10-10 16:44:57 -0700108 }
brandtrb29e6522016-12-21 06:37:18 -0800109
brandtra8b38552016-10-10 16:44:57 -0700110 ++packet_counter_.num_packets;
111
nissea5f043f2017-09-18 07:58:59 -0700112 return received_packet;
brandtra8b38552016-10-10 16:44:57 -0700113}
114
115// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700116// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700117// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700118// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700119// media packets through the callback. The latter behaviour makes sense
nisseb1f2ff92017-06-09 04:01:55 -0700120// for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver.
brandtra8b38552016-10-10 16:44:57 -0700121// Here, however, the received media pipeline is more decoupled from the
122// FlexFEC decoder, and we therefore do not interfere with the reception
123// of non-recovered media packets.
nissea5f043f2017-09-18 07:58:59 -0700124void FlexfecReceiver::ProcessReceivedPacket(
125 const ReceivedPacket& received_packet) {
brandtr8b5c3452016-12-19 10:02:30 -0800126 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -0700127
128 // Decode.
nissea5f043f2017-09-18 07:58:59 -0700129 erasure_code_->DecodeFec(received_packet, &recovered_packets_);
130
brandtra8b38552016-10-10 16:44:57 -0700131 // Return recovered packets through callback.
132 for (const auto& recovered_packet : recovered_packets_) {
Rasmus Brandt13a8f202017-10-31 16:44:23 +0100133 RTC_CHECK(recovered_packet);
brandtra8b38552016-10-10 16:44:57 -0700134 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;
Rasmus Brandt13a8f202017-10-31 16:44:23 +0100141 RTC_CHECK(recovered_packet->pkt);
nissed2ef3142017-05-11 08:00:58 -0700142 recovered_packet_receiver_->OnRecoveredPacket(
143 recovered_packet->pkt->data, recovered_packet->pkt->length);
brandtra8b38552016-10-10 16:44:57 -0700144 // Periodically log the incoming packets.
145 int64_t now_ms = clock_->TimeInMilliseconds();
146 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) {
147 uint32_t media_ssrc =
148 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc
150 << " from FlexFEC stream with SSRC: " << ssrc_ << ".";
brandtra8b38552016-10-10 16:44:57 -0700151 last_recovered_packet_ms_ = now_ms;
152 }
153 }
brandtra8b38552016-10-10 16:44:57 -0700154}
155
156} // namespace webrtc