blob: b81a0399548dd67527d411abda6d2156bbaaaa05 [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
brandtr0a4c1612016-11-03 08:18:27 -070011#include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h"
brandtra8b38552016-10-10 16:44:57 -070012
brandtra8b38552016-10-10 16:44:57 -070013#include "webrtc/base/logging.h"
14#include "webrtc/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),
37 erasure_code_(ForwardErrorCorrection::CreateFlexfec()),
brandtrb29e6522016-12-21 06:37:18 -080038 recovered_packet_receiver_(recovered_packet_receiver),
brandtra8b38552016-10-10 16:44:57 -070039 clock_(Clock::GetRealTimeClock()),
40 last_recovered_packet_ms_(-1) {
41 // It's OK to create this object on a different thread/task queue than
42 // the one used during main operation.
43 sequence_checker_.Detach();
44}
45
brandtr0a4c1612016-11-03 08:18:27 -070046FlexfecReceiver::~FlexfecReceiver() = default;
brandtra8b38552016-10-10 16:44:57 -070047
brandtrfa5a3682017-01-17 01:33:54 -080048bool FlexfecReceiver::AddAndProcessReceivedPacket(
49 const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080050 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtrb29e6522016-12-21 06:37:18 -080051 if (!AddReceivedPacket(std::move(packet))) {
brandtra8b38552016-10-10 16:44:57 -070052 return false;
53 }
54 return ProcessReceivedPackets();
55}
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
brandtrfa5a3682017-01-17 01:33:54 -080062bool FlexfecReceiver::AddReceivedPacket(const RtpPacketReceived& packet) {
brandtr8b5c3452016-12-19 10:02:30 -080063 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -070064
65 // RTP packets with a full base header (12 bytes), but without payload,
66 // could conceivably be useful in the decoding. Therefore we check
brandtrb29e6522016-12-21 06:37:18 -080067 // with a non-strict inequality here.
68 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
brandtra8b38552016-10-10 16:44:57 -070069
70 // Demultiplex based on SSRC, and insert into erasure code decoder.
71 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
brandtrb29e6522016-12-21 06:37:18 -080072 received_packet->seq_num = packet.SequenceNumber();
73 received_packet->ssrc = packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -070074 if (received_packet->ssrc == ssrc_) {
brandtrb29e6522016-12-21 06:37:18 -080075 // This is a FlexFEC packet.
76 if (packet.payload_size() < kMinFlexfecHeaderSize) {
brandtra8b38552016-10-10 16:44:57 -070077 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
78 return false;
79 }
80 received_packet->is_fec = true;
81 ++packet_counter_.num_fec_packets;
brandtrb29e6522016-12-21 06:37:18 -080082
brandtra8b38552016-10-10 16:44:57 -070083 // Insert packet payload into erasure code.
84 // TODO(brandtr): Remove this memcpy when the FEC packet classes
85 // are using COW buffers internally.
86 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -080087 auto payload = packet.payload();
danilchap96c15872016-11-21 01:35:29 -080088 memcpy(received_packet->pkt->data, payload.data(), payload.size());
89 received_packet->pkt->length = payload.size();
brandtra8b38552016-10-10 16:44:57 -070090 } else {
91 // This is a media packet, or a FlexFEC packet belonging to some
92 // other FlexFEC stream.
93 if (received_packet->ssrc != protected_media_ssrc_) {
94 return false;
95 }
96 received_packet->is_fec = false;
brandtrb29e6522016-12-21 06:37:18 -080097
brandtra8b38552016-10-10 16:44:57 -070098 // Insert entire packet into erasure code.
99 // TODO(brandtr): Remove this memcpy too.
100 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
brandtrb29e6522016-12-21 06:37:18 -0800101 memcpy(received_packet->pkt->data, packet.data(), packet.size());
102 received_packet->pkt->length = packet.size();
brandtra8b38552016-10-10 16:44:57 -0700103 }
brandtrb29e6522016-12-21 06:37:18 -0800104
brandtra8b38552016-10-10 16:44:57 -0700105 received_packets_.push_back(std::move(received_packet));
106 ++packet_counter_.num_packets;
107
108 return true;
109}
110
111// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700112// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700113// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700114// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700115// media packets through the callback. The latter behaviour makes sense
116// for ULPFEC, since the ULPFEC receiver is owned by the RtpStreamReceiver.
117// Here, however, the received media pipeline is more decoupled from the
118// FlexFEC decoder, and we therefore do not interfere with the reception
119// of non-recovered media packets.
brandtr0a4c1612016-11-03 08:18:27 -0700120bool FlexfecReceiver::ProcessReceivedPackets() {
brandtr8b5c3452016-12-19 10:02:30 -0800121 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_);
brandtra8b38552016-10-10 16:44:57 -0700122
123 // Decode.
124 if (!received_packets_.empty()) {
125 if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) !=
126 0) {
127 return false;
128 }
129 }
130 // 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;
brandtrb29e6522016-12-21 06:37:18 -0800136 if (!recovered_packet_receiver_->OnRecoveredPacket(
137 recovered_packet->pkt->data, recovered_packet->pkt->length)) {
brandtra8b38552016-10-10 16:44:57 -0700138 return false;
139 }
140 recovered_packet->returned = true;
141 // 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 }
151 return true;
152}
153
154} // namespace webrtc