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 | |
| 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 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | using Packet = ForwardErrorCorrection::Packet; |
| 24 | using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; |
| 25 | |
| 26 | // Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet. |
| 27 | constexpr size_t kMinFlexfecHeaderSize = 20; |
| 28 | |
| 29 | // How often to log the recovered packets to the text log. |
| 30 | constexpr int kPacketLogIntervalMs = 10000; |
| 31 | |
| 32 | } // namespace |
| 33 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 34 | FlexfecReceiver::FlexfecReceiver(uint32_t ssrc, |
| 35 | uint32_t protected_media_ssrc, |
| 36 | RecoveredPacketReceiver* callback) |
| 37 | : ssrc_(ssrc), |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 38 | 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 | |
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 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 50 | bool FlexfecReceiver::AddAndProcessReceivedPacket(const uint8_t* packet, |
| 51 | size_t packet_length) { |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 52 | RTC_DCHECK(sequence_checker_.CalledSequentially()); |
| 53 | |
| 54 | if (!AddReceivedPacket(packet, packet_length)) { |
| 55 | return false; |
| 56 | } |
| 57 | return ProcessReceivedPackets(); |
| 58 | } |
| 59 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 60 | FecPacketCounter FlexfecReceiver::GetPacketCounter() const { |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 61 | RTC_DCHECK(sequence_checker_.CalledSequentially()); |
| 62 | return packet_counter_; |
| 63 | } |
| 64 | |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 65 | bool FlexfecReceiver::AddReceivedPacket(const uint8_t* packet, |
| 66 | size_t packet_length) { |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 67 | 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(); |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 88 | if (received_packet->ssrc == ssrc_) { |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 89 | // 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 |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 124 | // in UlpfecReceiver::ProcessReceivedFec() are slightly different. |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 125 | // This implementation only returns _recovered_ media packets through the |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 126 | // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 127 | // 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. |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 132 | bool FlexfecReceiver::ProcessReceivedPackets() { |
brandtr | a8b3855 | 2016-10-10 16:44:57 -0700 | [diff] [blame] | 133 | 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); |
brandtr | 0a4c161 | 2016-11-03 08:18:27 -0700 | [diff] [blame] | 158 | LOG(LS_INFO) << "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 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | } // namespace webrtc |