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