brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/rtp_rtcp/include/flexfec_receiver.h" |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 13 | #include <string.h> |
| 14 | |
| 15 | #include "api/array_view.h" |
Rasmus Brandt | 13a8f20 | 2017-10-31 16:44:23 +0100 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
| 18 | #include "rtc_base/scoped_ref_ptr.h" |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | using Packet = ForwardErrorCorrection::Packet; |
| 25 | using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; |
| 26 | |
| 27 | // Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet. |
| 28 | constexpr size_t kMinFlexfecHeaderSize = 20; |
| 29 | |
| 30 | // How often to log the recovered packets to the text log. |
| 31 | constexpr int kPacketLogIntervalMs = 10000; |
| 32 | |
| 33 | } // namespace |
| 34 | |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 35 | FlexfecReceiver::FlexfecReceiver( |
| 36 | uint32_t ssrc, |
| 37 | uint32_t protected_media_ssrc, |
| 38 | RecoveredPacketReceiver* recovered_packet_receiver) |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 39 | : ssrc_(ssrc), |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 40 | protected_media_ssrc_(protected_media_ssrc), |
brandtr | d726a3f | 2017-06-29 02:45:35 -0700 | [diff] [blame] | 41 | erasure_code_( |
| 42 | ForwardErrorCorrection::CreateFlexfec(ssrc, protected_media_ssrc)), |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 43 | recovered_packet_receiver_(recovered_packet_receiver), |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 44 | clock_(Clock::GetRealTimeClock()), |
| 45 | last_recovered_packet_ms_(-1) { |
| 46 | // It's OK to create this object on a different thread/task queue than |
| 47 | // the one used during main operation. |
| 48 | sequence_checker_.Detach(); |
| 49 | } |
| 50 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 51 | FlexfecReceiver::~FlexfecReceiver() = default; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 52 | |
nisse | 5c29a7a | 2017-02-16 06:52:32 -0800 | [diff] [blame] | 53 | void FlexfecReceiver::OnRtpPacket(const RtpPacketReceived& packet) { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 54 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
Rasmus Brandt | a00137c | 2017-12-12 10:01:20 +0100 | [diff] [blame] | 55 | |
| 56 | // If this packet was recovered, it might be originating from |
| 57 | // ProcessReceivedPacket in this object. To avoid lifetime issues with |
| 58 | // |recovered_packets_|, we therefore break the cycle here. |
| 59 | // This might reduce decoding efficiency a bit, since we can't disambiguate |
| 60 | // recovered packets by RTX from recovered packets by FlexFEC. |
| 61 | if (packet.recovered()) |
| 62 | return; |
| 63 | |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 64 | std::unique_ptr<ReceivedPacket> received_packet = AddReceivedPacket(packet); |
| 65 | if (!received_packet) |
nisse | 5c29a7a | 2017-02-16 06:52:32 -0800 | [diff] [blame] | 66 | return; |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 67 | |
| 68 | ProcessReceivedPacket(*received_packet); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 69 | } |
| 70 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 71 | FecPacketCounter FlexfecReceiver::GetPacketCounter() const { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 72 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 73 | return packet_counter_; |
| 74 | } |
| 75 | |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 76 | // TODO(eladalon): Consider using packet.recovered() to avoid processing |
| 77 | // recovered packets here. |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 78 | std::unique_ptr<ReceivedPacket> FlexfecReceiver::AddReceivedPacket( |
| 79 | const RtpPacketReceived& packet) { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 80 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 81 | |
| 82 | // RTP packets with a full base header (12 bytes), but without payload, |
| 83 | // could conceivably be useful in the decoding. Therefore we check |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 84 | // with a non-strict inequality here. |
| 85 | RTC_DCHECK_GE(packet.size(), kRtpHeaderSize); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 86 | |
| 87 | // Demultiplex based on SSRC, and insert into erasure code decoder. |
| 88 | std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 89 | received_packet->seq_num = packet.SequenceNumber(); |
| 90 | received_packet->ssrc = packet.Ssrc(); |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 91 | if (received_packet->ssrc == ssrc_) { |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 92 | // This is a FlexFEC packet. |
| 93 | if (packet.payload_size() < kMinFlexfecHeaderSize) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 94 | RTC_LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 95 | return nullptr; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 96 | } |
| 97 | received_packet->is_fec = true; |
| 98 | ++packet_counter_.num_fec_packets; |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 99 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 100 | // Insert packet payload into erasure code. |
| 101 | // TODO(brandtr): Remove this memcpy when the FEC packet classes |
| 102 | // are using COW buffers internally. |
| 103 | received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 104 | auto payload = packet.payload(); |
danilchap | 96c1587 | 2016-11-21 01:35:29 -0800 | [diff] [blame] | 105 | memcpy(received_packet->pkt->data, payload.data(), payload.size()); |
| 106 | received_packet->pkt->length = payload.size(); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 107 | } else { |
| 108 | // This is a media packet, or a FlexFEC packet belonging to some |
| 109 | // other FlexFEC stream. |
| 110 | if (received_packet->ssrc != protected_media_ssrc_) { |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 111 | return nullptr; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 112 | } |
| 113 | received_packet->is_fec = false; |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 114 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 115 | // Insert entire packet into erasure code. |
| 116 | // TODO(brandtr): Remove this memcpy too. |
| 117 | received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 118 | memcpy(received_packet->pkt->data, packet.data(), packet.size()); |
| 119 | received_packet->pkt->length = packet.size(); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 120 | } |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 121 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 122 | ++packet_counter_.num_packets; |
| 123 | |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 124 | return received_packet; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Note that the implementation of this member function and the implementation |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 128 | // in UlpfecReceiver::ProcessReceivedFec() are slightly different. |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 129 | // This implementation only returns _recovered_ media packets through the |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 130 | // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 131 | // media packets through the callback. The latter behaviour makes sense |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 132 | // for ULPFEC, since the ULPFEC receiver is owned by the RtpVideoStreamReceiver. |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 133 | // Here, however, the received media pipeline is more decoupled from the |
| 134 | // FlexFEC decoder, and we therefore do not interfere with the reception |
| 135 | // of non-recovered media packets. |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 136 | void FlexfecReceiver::ProcessReceivedPacket( |
| 137 | const ReceivedPacket& received_packet) { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 138 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 139 | |
| 140 | // Decode. |
nisse | a5f043f | 2017-09-18 07:58:59 -0700 | [diff] [blame] | 141 | erasure_code_->DecodeFec(received_packet, &recovered_packets_); |
| 142 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 143 | // Return recovered packets through callback. |
| 144 | for (const auto& recovered_packet : recovered_packets_) { |
Rasmus Brandt | 13a8f20 | 2017-10-31 16:44:23 +0100 | [diff] [blame] | 145 | RTC_CHECK(recovered_packet); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 146 | if (recovered_packet->returned) { |
| 147 | continue; |
| 148 | } |
| 149 | ++packet_counter_.num_recovered_packets; |
nisse | e4bcd6d | 2017-05-16 04:47:04 -0700 | [diff] [blame] | 150 | // Set this flag first, since OnRecoveredPacket may end up here |
| 151 | // again, with the same packet. |
| 152 | recovered_packet->returned = true; |
Rasmus Brandt | 13a8f20 | 2017-10-31 16:44:23 +0100 | [diff] [blame] | 153 | RTC_CHECK(recovered_packet->pkt); |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 154 | recovered_packet_receiver_->OnRecoveredPacket( |
| 155 | recovered_packet->pkt->data, recovered_packet->pkt->length); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 156 | // Periodically log the incoming packets. |
| 157 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 158 | if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { |
| 159 | uint32_t media_ssrc = |
| 160 | ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 161 | RTC_LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc |
| 162 | << " from FlexFEC stream with SSRC: " << ssrc_ << "."; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 163 | last_recovered_packet_ms_ = now_ms; |
| 164 | } |
| 165 | } |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | } // namespace webrtc |