xref: /aosp_15_r20/external/webrtc/modules/rtp_rtcp/source/flexfec_receiver.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "modules/rtp_rtcp/include/flexfec_receiver.h"
12 
13 #include <string.h>
14 
15 #include "api/array_view.h"
16 #include "api/scoped_refptr.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19 
20 namespace webrtc {
21 
22 namespace {
23 
24 // Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet.
25 constexpr size_t kMinFlexfecHeaderSize = 20;
26 
27 // How often to log the recovered packets to the text log.
28 constexpr int kPacketLogIntervalMs = 10000;
29 
30 }  // namespace
31 
FlexfecReceiver(uint32_t ssrc,uint32_t protected_media_ssrc,RecoveredPacketReceiver * recovered_packet_receiver)32 FlexfecReceiver::FlexfecReceiver(
33     uint32_t ssrc,
34     uint32_t protected_media_ssrc,
35     RecoveredPacketReceiver* recovered_packet_receiver)
36     : FlexfecReceiver(Clock::GetRealTimeClock(),
37                       ssrc,
38                       protected_media_ssrc,
39                       recovered_packet_receiver) {}
40 
FlexfecReceiver(Clock * clock,uint32_t ssrc,uint32_t protected_media_ssrc,RecoveredPacketReceiver * recovered_packet_receiver)41 FlexfecReceiver::FlexfecReceiver(
42     Clock* clock,
43     uint32_t ssrc,
44     uint32_t protected_media_ssrc,
45     RecoveredPacketReceiver* recovered_packet_receiver)
46     : ssrc_(ssrc),
47       protected_media_ssrc_(protected_media_ssrc),
48       erasure_code_(
49           ForwardErrorCorrection::CreateFlexfec(ssrc, protected_media_ssrc)),
50       recovered_packet_receiver_(recovered_packet_receiver),
51       clock_(clock),
52       last_recovered_packet_ms_(-1) {
53   // It's OK to create this object on a different thread/task queue than
54   // the one used during main operation.
55   sequence_checker_.Detach();
56 }
57 
58 FlexfecReceiver::~FlexfecReceiver() = default;
59 
OnRtpPacket(const RtpPacketReceived & packet)60 void FlexfecReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
61   RTC_DCHECK_RUN_ON(&sequence_checker_);
62 
63   // If this packet was recovered, it might be originating from
64   // ProcessReceivedPacket in this object. To avoid lifetime issues with
65   // `recovered_packets_`, we therefore break the cycle here.
66   // This might reduce decoding efficiency a bit, since we can't disambiguate
67   // recovered packets by RTX from recovered packets by FlexFEC.
68   if (packet.recovered())
69     return;
70 
71   std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet =
72       AddReceivedPacket(packet);
73   if (!received_packet)
74     return;
75 
76   ProcessReceivedPacket(*received_packet);
77 }
78 
GetPacketCounter() const79 FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
80   RTC_DCHECK_RUN_ON(&sequence_checker_);
81   return packet_counter_;
82 }
83 
84 // TODO(eladalon): Consider using packet.recovered() to avoid processing
85 // recovered packets here.
86 std::unique_ptr<ForwardErrorCorrection::ReceivedPacket>
AddReceivedPacket(const RtpPacketReceived & packet)87 FlexfecReceiver::AddReceivedPacket(const RtpPacketReceived& packet) {
88   RTC_DCHECK_RUN_ON(&sequence_checker_);
89 
90   // RTP packets with a full base header (12 bytes), but without payload,
91   // could conceivably be useful in the decoding. Therefore we check
92   // with a non-strict inequality here.
93   RTC_DCHECK_GE(packet.size(), kRtpHeaderSize);
94 
95   // Demultiplex based on SSRC, and insert into erasure code decoder.
96   std::unique_ptr<ForwardErrorCorrection::ReceivedPacket> received_packet(
97       new ForwardErrorCorrection::ReceivedPacket());
98   received_packet->seq_num = packet.SequenceNumber();
99   received_packet->ssrc = packet.Ssrc();
100   if (received_packet->ssrc == ssrc_) {
101     // This is a FlexFEC packet.
102     if (packet.payload_size() < kMinFlexfecHeaderSize) {
103       RTC_LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
104       return nullptr;
105     }
106     received_packet->is_fec = true;
107     ++packet_counter_.num_fec_packets;
108 
109     // Insert packet payload into erasure code.
110     received_packet->pkt = rtc::scoped_refptr<ForwardErrorCorrection::Packet>(
111         new ForwardErrorCorrection::Packet());
112     received_packet->pkt->data =
113         packet.Buffer().Slice(packet.headers_size(), packet.payload_size());
114   } else {
115     // This is a media packet, or a FlexFEC packet belonging to some
116     // other FlexFEC stream.
117     if (received_packet->ssrc != protected_media_ssrc_) {
118       return nullptr;
119     }
120     received_packet->is_fec = false;
121 
122     // Insert entire packet into erasure code.
123     // Create a copy and fill with zeros all mutable extensions.
124     received_packet->pkt = rtc::scoped_refptr<ForwardErrorCorrection::Packet>(
125         new ForwardErrorCorrection::Packet());
126     RtpPacketReceived packet_copy(packet);
127     packet_copy.ZeroMutableExtensions();
128     received_packet->pkt->data = packet_copy.Buffer();
129   }
130 
131   ++packet_counter_.num_packets;
132 
133   return received_packet;
134 }
135 
136 // Note that the implementation of this member function and the implementation
137 // in UlpfecReceiver::ProcessReceivedFec() are slightly different.
138 // This implementation only returns _recovered_ media packets through the
139 // callback, whereas the implementation in UlpfecReceiver returns _all inserted_
140 // media packets through the callback. The latter behaviour makes sense
141 // for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver.
142 // Here, however, the received media pipeline is more decoupled from the
143 // FlexFEC decoder, and we therefore do not interfere with the reception
144 // of non-recovered media packets.
ProcessReceivedPacket(const ForwardErrorCorrection::ReceivedPacket & received_packet)145 void FlexfecReceiver::ProcessReceivedPacket(
146     const ForwardErrorCorrection::ReceivedPacket& received_packet) {
147   RTC_DCHECK_RUN_ON(&sequence_checker_);
148 
149   // Decode.
150   erasure_code_->DecodeFec(received_packet, &recovered_packets_);
151 
152   // Return recovered packets through callback.
153   for (const auto& recovered_packet : recovered_packets_) {
154     RTC_CHECK(recovered_packet);
155     if (recovered_packet->returned) {
156       continue;
157     }
158     ++packet_counter_.num_recovered_packets;
159     // Set this flag first, since OnRecoveredPacket may end up here
160     // again, with the same packet.
161     recovered_packet->returned = true;
162     RTC_CHECK_GE(recovered_packet->pkt->data.size(), kRtpHeaderSize);
163     recovered_packet_receiver_->OnRecoveredPacket(
164         recovered_packet->pkt->data.cdata(),
165         recovered_packet->pkt->data.size());
166     uint32_t media_ssrc =
167         ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data.data());
168     uint16_t media_seq_num = ForwardErrorCorrection::ParseSequenceNumber(
169         recovered_packet->pkt->data.data());
170     // Periodically log the incoming packets at LS_INFO.
171     int64_t now_ms = clock_->TimeInMilliseconds();
172     bool should_log_periodically =
173         now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs;
174     if (RTC_LOG_CHECK_LEVEL(LS_VERBOSE) || should_log_periodically) {
175       rtc::LoggingSeverity level =
176           should_log_periodically ? rtc::LS_INFO : rtc::LS_VERBOSE;
177       RTC_LOG_V(level) << "Recovered media packet with SSRC: " << media_ssrc
178                        << " seq " << media_seq_num << " recovered length "
179                        << recovered_packet->pkt->data.size()
180                        << " from FlexFEC stream with SSRC: " << ssrc_;
181       if (should_log_periodically) {
182         last_recovered_packet_ms_ = now_ms;
183       }
184     }
185   }
186 }
187 
188 }  // namespace webrtc
189