| OLD | NEW |
| (Empty) |
| 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 | |
| 11 #include "webrtc/modules/rtp_rtcp/source/flexfec_receiver_impl.h" | |
| 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 | |
| 34 std::unique_ptr<FlexfecReceiver> FlexfecReceiver::Create( | |
| 35 uint32_t flexfec_ssrc, | |
| 36 uint32_t protected_media_ssrc, | |
| 37 RecoveredPacketReceiver* callback) { | |
| 38 return std::unique_ptr<FlexfecReceiver>( | |
| 39 new FlexfecReceiverImpl(flexfec_ssrc, protected_media_ssrc, callback)); | |
| 40 } | |
| 41 | |
| 42 FlexfecReceiver::~FlexfecReceiver() = default; | |
| 43 | |
| 44 FlexfecReceiverImpl::FlexfecReceiverImpl(uint32_t flexfec_ssrc, | |
| 45 uint32_t protected_media_ssrc, | |
| 46 RecoveredPacketReceiver* callback) | |
| 47 : flexfec_ssrc_(flexfec_ssrc), | |
| 48 protected_media_ssrc_(protected_media_ssrc), | |
| 49 erasure_code_(ForwardErrorCorrection::CreateFlexfec()), | |
| 50 callback_(callback), | |
| 51 clock_(Clock::GetRealTimeClock()), | |
| 52 last_recovered_packet_ms_(-1) { | |
| 53 // It's OK to create this object on a different thread/task queue than | |
| 54 // the one used during main operation. | |
| 55 sequence_checker_.Detach(); | |
| 56 } | |
| 57 | |
| 58 FlexfecReceiverImpl::~FlexfecReceiverImpl() = default; | |
| 59 | |
| 60 bool FlexfecReceiverImpl::AddAndProcessReceivedPacket(const uint8_t* packet, | |
| 61 size_t packet_length) { | |
| 62 RTC_DCHECK(sequence_checker_.CalledSequentially()); | |
| 63 | |
| 64 if (!AddReceivedPacket(packet, packet_length)) { | |
| 65 return false; | |
| 66 } | |
| 67 return ProcessReceivedPackets(); | |
| 68 } | |
| 69 | |
| 70 FecPacketCounter FlexfecReceiverImpl::GetPacketCounter() const { | |
| 71 RTC_DCHECK(sequence_checker_.CalledSequentially()); | |
| 72 return packet_counter_; | |
| 73 } | |
| 74 | |
| 75 bool FlexfecReceiverImpl::AddReceivedPacket(const uint8_t* packet, | |
| 76 size_t packet_length) { | |
| 77 RTC_DCHECK(sequence_checker_.CalledSequentially()); | |
| 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 | |
| 81 // with a strict inequality here. | |
| 82 if (packet_length < kRtpHeaderSize) { | |
| 83 LOG(LS_WARNING) << "Truncated packet, discarding."; | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 // TODO(brandtr): Consider how to handle received FlexFEC packets and | |
| 88 // the bandwidth estimator. | |
| 89 RtpPacketReceived parsed_packet; | |
| 90 if (!parsed_packet.Parse(packet, packet_length)) { | |
| 91 return false; | |
| 92 } | |
| 93 | |
| 94 // Demultiplex based on SSRC, and insert into erasure code decoder. | |
| 95 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); | |
| 96 received_packet->seq_num = parsed_packet.SequenceNumber(); | |
| 97 received_packet->ssrc = parsed_packet.Ssrc(); | |
| 98 if (received_packet->ssrc == flexfec_ssrc_) { | |
| 99 // This is a FEC packet belonging to this FlexFEC stream. | |
| 100 if (parsed_packet.payload_size() < kMinFlexfecHeaderSize) { | |
| 101 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; | |
| 102 return false; | |
| 103 } | |
| 104 received_packet->is_fec = true; | |
| 105 ++packet_counter_.num_fec_packets; | |
| 106 // Insert packet payload into erasure code. | |
| 107 // TODO(brandtr): Remove this memcpy when the FEC packet classes | |
| 108 // are using COW buffers internally. | |
| 109 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); | |
| 110 memcpy(received_packet->pkt->data, parsed_packet.payload(), | |
| 111 parsed_packet.payload_size()); | |
| 112 received_packet->pkt->length = parsed_packet.payload_size(); | |
| 113 } else { | |
| 114 // This is a media packet, or a FlexFEC packet belonging to some | |
| 115 // other FlexFEC stream. | |
| 116 if (received_packet->ssrc != protected_media_ssrc_) { | |
| 117 return false; | |
| 118 } | |
| 119 received_packet->is_fec = false; | |
| 120 // Insert entire packet into erasure code. | |
| 121 // TODO(brandtr): Remove this memcpy too. | |
| 122 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); | |
| 123 memcpy(received_packet->pkt->data, parsed_packet.data(), | |
| 124 parsed_packet.size()); | |
| 125 received_packet->pkt->length = parsed_packet.size(); | |
| 126 } | |
| 127 received_packets_.push_back(std::move(received_packet)); | |
| 128 ++packet_counter_.num_packets; | |
| 129 | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 // Note that the implementation of this member function and the implementation | |
| 134 // in UlpfecReceiver::ProcessReceivedFec() are slightly different. | |
| 135 // This implementation only returns _recovered_ media packets through the | |
| 136 // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ | |
| 137 // media packets through the callback. The latter behaviour makes sense | |
| 138 // for ULPFEC, since the ULPFEC receiver is owned by the RtpStreamReceiver. | |
| 139 // Here, however, the received media pipeline is more decoupled from the | |
| 140 // FlexFEC decoder, and we therefore do not interfere with the reception | |
| 141 // of non-recovered media packets. | |
| 142 bool FlexfecReceiverImpl::ProcessReceivedPackets() { | |
| 143 RTC_DCHECK(sequence_checker_.CalledSequentially()); | |
| 144 | |
| 145 // Decode. | |
| 146 if (!received_packets_.empty()) { | |
| 147 if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) != | |
| 148 0) { | |
| 149 return false; | |
| 150 } | |
| 151 } | |
| 152 // Return recovered packets through callback. | |
| 153 for (const auto& recovered_packet : recovered_packets_) { | |
| 154 if (recovered_packet->returned) { | |
| 155 continue; | |
| 156 } | |
| 157 ++packet_counter_.num_recovered_packets; | |
| 158 if (!callback_->OnRecoveredPacket(recovered_packet->pkt->data, | |
| 159 recovered_packet->pkt->length)) { | |
| 160 return false; | |
| 161 } | |
| 162 recovered_packet->returned = true; | |
| 163 // Periodically log the incoming packets. | |
| 164 int64_t now_ms = clock_->TimeInMilliseconds(); | |
| 165 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { | |
| 166 uint32_t media_ssrc = | |
| 167 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); | |
| 168 std::stringstream ss; | |
| 169 ss << "Recovered media packet with SSRC: " << media_ssrc | |
| 170 << " from FlexFEC stream with SSRC: " << flexfec_ssrc_ << "."; | |
| 171 LOG(LS_INFO) << ss.str(); | |
| 172 last_recovered_packet_ms_ = now_ms; | |
| 173 } | |
| 174 } | |
| 175 return true; | |
| 176 } | |
| 177 | |
| 178 } // namespace webrtc | |
| OLD | NEW |