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 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h" |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 12 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 13 | #include "webrtc/base/logging.h" |
| 14 | #include "webrtc/base/scoped_ref_ptr.h" |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | using Packet = ForwardErrorCorrection::Packet; |
| 21 | using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; |
| 22 | |
| 23 | // Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet. |
| 24 | constexpr size_t kMinFlexfecHeaderSize = 20; |
| 25 | |
| 26 | // How often to log the recovered packets to the text log. |
| 27 | constexpr int kPacketLogIntervalMs = 10000; |
| 28 | |
| 29 | } // namespace |
| 30 | |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 31 | FlexfecReceiver::FlexfecReceiver( |
| 32 | uint32_t ssrc, |
| 33 | uint32_t protected_media_ssrc, |
| 34 | RecoveredPacketReceiver* recovered_packet_receiver) |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 35 | : ssrc_(ssrc), |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 36 | protected_media_ssrc_(protected_media_ssrc), |
| 37 | erasure_code_(ForwardErrorCorrection::CreateFlexfec()), |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 38 | recovered_packet_receiver_(recovered_packet_receiver), |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 39 | 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 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 46 | FlexfecReceiver::~FlexfecReceiver() = default; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 47 | |
nisse | 5c29a7a | 2017-02-16 06:52:32 -0800 | [diff] [blame] | 48 | void FlexfecReceiver::OnRtpPacket(const RtpPacketReceived& packet) { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 49 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
nisse | 5c29a7a | 2017-02-16 06:52:32 -0800 | [diff] [blame] | 50 | if (!AddReceivedPacket(packet)) { |
| 51 | return; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 52 | } |
nisse | 5c29a7a | 2017-02-16 06:52:32 -0800 | [diff] [blame] | 53 | ProcessReceivedPackets(); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 54 | } |
| 55 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 56 | FecPacketCounter FlexfecReceiver::GetPacketCounter() const { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 57 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 58 | return packet_counter_; |
| 59 | } |
| 60 | |
brandtr | fa5a368 | 2017-01-17 01:33:54 -0800 | [diff] [blame] | 61 | bool FlexfecReceiver::AddReceivedPacket(const RtpPacketReceived& packet) { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 62 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 63 | |
| 64 | // RTP packets with a full base header (12 bytes), but without payload, |
| 65 | // could conceivably be useful in the decoding. Therefore we check |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 66 | // with a non-strict inequality here. |
| 67 | RTC_DCHECK_GE(packet.size(), kRtpHeaderSize); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 68 | |
| 69 | // Demultiplex based on SSRC, and insert into erasure code decoder. |
| 70 | std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 71 | received_packet->seq_num = packet.SequenceNumber(); |
| 72 | received_packet->ssrc = packet.Ssrc(); |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 73 | if (received_packet->ssrc == ssrc_) { |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 74 | // This is a FlexFEC packet. |
| 75 | if (packet.payload_size() < kMinFlexfecHeaderSize) { |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 76 | LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; |
| 77 | return false; |
| 78 | } |
| 79 | received_packet->is_fec = true; |
| 80 | ++packet_counter_.num_fec_packets; |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 81 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 82 | // Insert packet payload into erasure code. |
| 83 | // TODO(brandtr): Remove this memcpy when the FEC packet classes |
| 84 | // are using COW buffers internally. |
| 85 | received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 86 | auto payload = packet.payload(); |
danilchap | 96c1587 | 2016-11-21 01:35:29 -0800 | [diff] [blame] | 87 | memcpy(received_packet->pkt->data, payload.data(), payload.size()); |
| 88 | received_packet->pkt->length = payload.size(); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 89 | } else { |
| 90 | // This is a media packet, or a FlexFEC packet belonging to some |
| 91 | // other FlexFEC stream. |
| 92 | if (received_packet->ssrc != protected_media_ssrc_) { |
| 93 | return false; |
| 94 | } |
| 95 | received_packet->is_fec = false; |
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 entire packet into erasure code. |
| 98 | // TODO(brandtr): Remove this memcpy too. |
| 99 | received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 100 | memcpy(received_packet->pkt->data, packet.data(), packet.size()); |
| 101 | received_packet->pkt->length = packet.size(); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 102 | } |
brandtr | b29e652 | 2016-12-21 06:37:18 -0800 | [diff] [blame] | 103 | |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 104 | received_packets_.push_back(std::move(received_packet)); |
| 105 | ++packet_counter_.num_packets; |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // Note that the implementation of this member function and the implementation |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 111 | // in UlpfecReceiver::ProcessReceivedFec() are slightly different. |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 112 | // This implementation only returns _recovered_ media packets through the |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 113 | // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 114 | // media packets through the callback. The latter behaviour makes sense |
| 115 | // for ULPFEC, since the ULPFEC receiver is owned by the RtpStreamReceiver. |
| 116 | // Here, however, the received media pipeline is more decoupled from the |
| 117 | // FlexFEC decoder, and we therefore do not interfere with the reception |
| 118 | // of non-recovered media packets. |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 119 | bool FlexfecReceiver::ProcessReceivedPackets() { |
brandtr | 8b5c345 | 2016-12-19 10:02:30 -0800 | [diff] [blame] | 120 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 121 | |
| 122 | // Decode. |
| 123 | if (!received_packets_.empty()) { |
| 124 | if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) != |
| 125 | 0) { |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | // Return recovered packets through callback. |
| 130 | for (const auto& recovered_packet : recovered_packets_) { |
| 131 | if (recovered_packet->returned) { |
| 132 | continue; |
| 133 | } |
| 134 | ++packet_counter_.num_recovered_packets; |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame^] | 135 | recovered_packet_receiver_->OnRecoveredPacket( |
| 136 | recovered_packet->pkt->data, recovered_packet->pkt->length); |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 137 | recovered_packet->returned = true; |
| 138 | // Periodically log the incoming packets. |
| 139 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 140 | if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { |
| 141 | uint32_t media_ssrc = |
| 142 | ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); |
brandtr | 658024e | 2017-01-10 06:49:58 -0800 | [diff] [blame] | 143 | LOG(LS_VERBOSE) << "Recovered media packet with SSRC: " << media_ssrc |
| 144 | << " from FlexFEC stream with SSRC: " << ssrc_ << "."; |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 145 | last_recovered_packet_ms_ = now_ms; |
| 146 | } |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | } // namespace webrtc |