blob: c84da552087cfe958b362c0137f63f48d49596a3 [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
13#include <utility>
14
15#include "webrtc/base/logging.h"
16#include "webrtc/base/scoped_ref_ptr.h"
17#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
18
19namespace webrtc {
20
21namespace {
22
23using Packet = ForwardErrorCorrection::Packet;
24using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket;
25
26// Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet.
27constexpr size_t kMinFlexfecHeaderSize = 20;
28
29// How often to log the recovered packets to the text log.
30constexpr int kPacketLogIntervalMs = 10000;
31
32} // namespace
33
brandtr0a4c1612016-11-03 08:18:27 -070034FlexfecReceiver::FlexfecReceiver(uint32_t ssrc,
35 uint32_t protected_media_ssrc,
36 RecoveredPacketReceiver* callback)
37 : ssrc_(ssrc),
brandtra8b38552016-10-10 16:44:57 -070038 protected_media_ssrc_(protected_media_ssrc),
39 erasure_code_(ForwardErrorCorrection::CreateFlexfec()),
40 callback_(callback),
41 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
brandtr0a4c1612016-11-03 08:18:27 -070050bool FlexfecReceiver::AddAndProcessReceivedPacket(const uint8_t* packet,
51 size_t packet_length) {
brandtra8b38552016-10-10 16:44:57 -070052 RTC_DCHECK(sequence_checker_.CalledSequentially());
53
54 if (!AddReceivedPacket(packet, packet_length)) {
55 return false;
56 }
57 return ProcessReceivedPackets();
58}
59
brandtr0a4c1612016-11-03 08:18:27 -070060FecPacketCounter FlexfecReceiver::GetPacketCounter() const {
brandtra8b38552016-10-10 16:44:57 -070061 RTC_DCHECK(sequence_checker_.CalledSequentially());
62 return packet_counter_;
63}
64
brandtr0a4c1612016-11-03 08:18:27 -070065bool FlexfecReceiver::AddReceivedPacket(const uint8_t* packet,
66 size_t packet_length) {
brandtra8b38552016-10-10 16:44:57 -070067 RTC_DCHECK(sequence_checker_.CalledSequentially());
68
69 // RTP packets with a full base header (12 bytes), but without payload,
70 // could conceivably be useful in the decoding. Therefore we check
71 // with a strict inequality here.
72 if (packet_length < kRtpHeaderSize) {
73 LOG(LS_WARNING) << "Truncated packet, discarding.";
74 return false;
75 }
76
77 // TODO(brandtr): Consider how to handle received FlexFEC packets and
78 // the bandwidth estimator.
79 RtpPacketReceived parsed_packet;
80 if (!parsed_packet.Parse(packet, packet_length)) {
81 return false;
82 }
83
84 // Demultiplex based on SSRC, and insert into erasure code decoder.
85 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket());
86 received_packet->seq_num = parsed_packet.SequenceNumber();
87 received_packet->ssrc = parsed_packet.Ssrc();
brandtr0a4c1612016-11-03 08:18:27 -070088 if (received_packet->ssrc == ssrc_) {
brandtra8b38552016-10-10 16:44:57 -070089 // This is a FEC packet belonging to this FlexFEC stream.
90 if (parsed_packet.payload_size() < kMinFlexfecHeaderSize) {
91 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding.";
92 return false;
93 }
94 received_packet->is_fec = true;
95 ++packet_counter_.num_fec_packets;
96 // Insert packet payload into erasure code.
97 // TODO(brandtr): Remove this memcpy when the FEC packet classes
98 // are using COW buffers internally.
99 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
100 memcpy(received_packet->pkt->data, parsed_packet.payload(),
101 parsed_packet.payload_size());
102 received_packet->pkt->length = parsed_packet.payload_size();
103 } else {
104 // This is a media packet, or a FlexFEC packet belonging to some
105 // other FlexFEC stream.
106 if (received_packet->ssrc != protected_media_ssrc_) {
107 return false;
108 }
109 received_packet->is_fec = false;
110 // Insert entire packet into erasure code.
111 // TODO(brandtr): Remove this memcpy too.
112 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet());
113 memcpy(received_packet->pkt->data, parsed_packet.data(),
114 parsed_packet.size());
115 received_packet->pkt->length = parsed_packet.size();
116 }
117 received_packets_.push_back(std::move(received_packet));
118 ++packet_counter_.num_packets;
119
120 return true;
121}
122
123// Note that the implementation of this member function and the implementation
brandtrd55c3f62016-10-31 04:51:33 -0700124// in UlpfecReceiver::ProcessReceivedFec() are slightly different.
brandtra8b38552016-10-10 16:44:57 -0700125// This implementation only returns _recovered_ media packets through the
brandtrd55c3f62016-10-31 04:51:33 -0700126// callback, whereas the implementation in UlpfecReceiver returns _all inserted_
brandtra8b38552016-10-10 16:44:57 -0700127// media packets through the callback. The latter behaviour makes sense
128// for ULPFEC, since the ULPFEC receiver is owned by the RtpStreamReceiver.
129// Here, however, the received media pipeline is more decoupled from the
130// FlexFEC decoder, and we therefore do not interfere with the reception
131// of non-recovered media packets.
brandtr0a4c1612016-11-03 08:18:27 -0700132bool FlexfecReceiver::ProcessReceivedPackets() {
brandtra8b38552016-10-10 16:44:57 -0700133 RTC_DCHECK(sequence_checker_.CalledSequentially());
134
135 // Decode.
136 if (!received_packets_.empty()) {
137 if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) !=
138 0) {
139 return false;
140 }
141 }
142 // Return recovered packets through callback.
143 for (const auto& recovered_packet : recovered_packets_) {
144 if (recovered_packet->returned) {
145 continue;
146 }
147 ++packet_counter_.num_recovered_packets;
148 if (!callback_->OnRecoveredPacket(recovered_packet->pkt->data,
149 recovered_packet->pkt->length)) {
150 return false;
151 }
152 recovered_packet->returned = true;
153 // 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);
brandtr0a4c1612016-11-03 08:18:27 -0700158 LOG(LS_INFO) << "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 }
163 return true;
164}
165
166} // namespace webrtc