blob: ec35a2c06383e4face8059c7f6124a6f1aaa3cc8 [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_);
nissea5f043f2017-09-18 07:58:59 -070051 std::unique_ptr<ReceivedPacket> received_packet = AddReceivedPacket(packet);
52 if (!received_packet)
nisse5c29a7a2017-02-16 06:52:32 -080053 return;
nissea5f043f2017-09-18 07:58:59 -070054
55 ProcessReceivedPacket(*received_packet);
brandtra8b38552016-10-10 16:44:57 -070056}
57
brandtr0a4c1612016-11-03 08:18:27 -070058FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
brandtr8b5c3452016-12-19 10:02:30 -080059 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070060 return packet_counter_;
61}
62
eladalonc0d481a2017-08-02 07:39:07 -070063// TODO(eladalon): Consider using packet.recovered() to avoid processing
64// recovered packets here.
nissea5f043f2017-09-18 07:58:59 -070065std::unique_ptr<ReceivedPacket> FlexfecReceiver::AddReceivedPacket(
66 const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080067 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070068
69 // RTP packets with a full base header (12 bytes), but without payload,
70 // could conceivably be useful in the decoding. Therefore we check
brandtrb29e6522016-12-21 06:37:18 -080071 // with a non-strict inequality here.
72 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
brandtra8b38552016-10-10 16:44:57 -070073
74 // Demultiplex based on SSRC, and insert into erasure code decoder.
75 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
brandtrb29e6522016-12-21 06:37:18 -080076 received_packet->seq_num = packet.SequenceNumber();
77 received_packet->ssrc = packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -070078 if (received_packet->ssrc == ssrc_) {
brandtrb29e6522016-12-21 06:37:18 -080079 // This is a FlexFEC packet.
80 if (packet.payload_size() < kMinFlexfecHeaderSize) {
brandtra8b38552016-10-10 16:44:57 -070081 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
nissea5f043f2017-09-18 07:58:59 -070082 return nullptr;
brandtra8b38552016-10-10 16:44:57 -070083 }
84 received_packet->is_fec = true;
85 ++packet_counter_.num_fec_packets;
brandtrb29e6522016-12-21 06:37:18 -080086
brandtra8b38552016-10-10 16:44:57 -070087 // Insert packet payload into erasure code.
88 // TODO(brandtr): Remove this memcpy when the FEC packet classes
89 // are using COW buffers internally.
90 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -080091 auto payload = packet.payload();
danilchap96c15872016-11-21 01:35:29 -080092 memcpy(received_packet->pkt->data, payload.data(), payload.size());
93 received_packet->pkt->length = payload.size();
brandtra8b38552016-10-10 16:44:57 -070094 } else {
95 // This is a media packet, or a FlexFEC packet belonging to some
96 // other FlexFEC stream.
97 if (received_packet->ssrc != protected_media_ssrc_) {
nissea5f043f2017-09-18 07:58:59 -070098 return nullptr;
brandtra8b38552016-10-10 16:44:57 -070099 }
100 received_packet->is_fec = false;
brandtrb29e6522016-12-21 06:37:18 -0800101
brandtra8b38552016-10-10 16:44:57 -0700102 // Insert entire packet into erasure code.
103 // TODO(brandtr): Remove this memcpy too.
104 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800105 memcpy(received_packet->pkt->data, packet.data(), packet.size());
106 received_packet->pkt->length = packet.size();
brandtra8b38552016-10-10 16:44:57 -0700107 }
brandtrb29e6522016-12-21 06:37:18 -0800108
brandtra8b38552016-10-10 16:44:57 -0700109 ++packet_counter_.num_packets;
110
nissea5f043f2017-09-18 07:58:59 -0700111 return received_packet;
brandtra8b38552016-10-10 16:44:57 -0700112}
113
114// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700115// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700116// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700117// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700118// media packets through the callback. The latter behaviour makes sense
nisseb1f2ff92017-06-09 04:01:55 -0700119// for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver.
brandtra8b38552016-10-10 16:44:57 -0700120// Here, however, the received media pipeline is more decoupled from the
121// FlexFEC decoder, and we therefore do not interfere with the reception
122// of non-recovered media packets.
nissea5f043f2017-09-18 07:58:59 -0700123void FlexfecReceiver::ProcessReceivedPacket(
124 const ReceivedPacket& received_packet) {
brandtr8b5c3452016-12-19 10:02:30 -0800125 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -0700126
127 // Decode.
nissea5f043f2017-09-18 07:58:59 -0700128 erasure_code_->DecodeFec(received_packet, &recovered_packets_);
129
brandtra8b38552016-10-10 16:44:57 -0700130 // Return recovered packets through callback.
131 for (const auto& recovered_packet : recovered_packets_) {
132 if (recovered_packet->returned) {
133 continue;
134 }
135 ++packet_counter_.num_recovered_packets;
nissee4bcd6d2017-05-16 04:47:04 -0700136 // Set this flag first, since OnRecoveredPacket may end up here
137 // again, with the same packet.
138 recovered_packet->returned = true;
nissed2ef3142017-05-11 08:00:58 -0700139 recovered_packet_receiver_->OnRecoveredPacket(
140 recovered_packet->pkt->data, recovered_packet->pkt->length);
brandtra8b38552016-10-10 16:44:57 -0700141 // Periodically log the incoming packets.
142 int64_t now_ms = clock_->TimeInMilliseconds();
143 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) {
144 uint32_t media_ssrc =
145 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data);
brandtr658024e2017-01-10 06:49:58 -0800146 LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc
147 << " from FlexFEC stream with SSRC: " << ssrc_ << ".";
brandtra8b38552016-10-10 16:44:57 -0700148 last_recovered_packet_ms_ = now_ms;
149 }
150 }
brandtra8b38552016-10-10 16:44:57 -0700151}
152
153} // namespace webrtc