| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h" | 11 #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h" |
| 12 | 12 |
| 13 #include <utility> |
| 14 |
| 13 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/base/scoped_ref_ptr.h" | 16 #include "webrtc/base/scoped_ref_ptr.h" |
| 17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
| 15 | 18 |
| 16 namespace webrtc { | 19 namespace webrtc { |
| 17 | 20 |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 using Packet = ForwardErrorCorrection::Packet; | 23 using Packet = ForwardErrorCorrection::Packet; |
| 21 using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; | 24 using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; |
| 22 | 25 |
| 23 // Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet. | 26 // Minimum header size (in bytes) of a well-formed non-singular FlexFEC packet. |
| 24 constexpr size_t kMinFlexfecHeaderSize = 20; | 27 constexpr size_t kMinFlexfecHeaderSize = 20; |
| 25 | 28 |
| 26 // How often to log the recovered packets to the text log. | 29 // How often to log the recovered packets to the text log. |
| 27 constexpr int kPacketLogIntervalMs = 10000; | 30 constexpr int kPacketLogIntervalMs = 10000; |
| 28 | 31 |
| 29 } // namespace | 32 } // namespace |
| 30 | 33 |
| 31 FlexfecReceiver::FlexfecReceiver( | 34 FlexfecReceiver::FlexfecReceiver(uint32_t ssrc, |
| 32 uint32_t ssrc, | 35 uint32_t protected_media_ssrc, |
| 33 uint32_t protected_media_ssrc, | 36 RecoveredPacketReceiver* callback) |
| 34 RecoveredPacketReceiver* recovered_packet_receiver) | |
| 35 : ssrc_(ssrc), | 37 : ssrc_(ssrc), |
| 36 protected_media_ssrc_(protected_media_ssrc), | 38 protected_media_ssrc_(protected_media_ssrc), |
| 37 erasure_code_(ForwardErrorCorrection::CreateFlexfec()), | 39 erasure_code_(ForwardErrorCorrection::CreateFlexfec()), |
| 38 recovered_packet_receiver_(recovered_packet_receiver), | 40 callback_(callback), |
| 39 clock_(Clock::GetRealTimeClock()), | 41 clock_(Clock::GetRealTimeClock()), |
| 40 last_recovered_packet_ms_(-1) { | 42 last_recovered_packet_ms_(-1) { |
| 41 // It's OK to create this object on a different thread/task queue than | 43 // It's OK to create this object on a different thread/task queue than |
| 42 // the one used during main operation. | 44 // the one used during main operation. |
| 43 sequence_checker_.Detach(); | 45 sequence_checker_.Detach(); |
| 44 } | 46 } |
| 45 | 47 |
| 46 FlexfecReceiver::~FlexfecReceiver() = default; | 48 FlexfecReceiver::~FlexfecReceiver() = default; |
| 47 | 49 |
| 48 bool FlexfecReceiver::AddAndProcessReceivedPacket(RtpPacketReceived packet) { | 50 bool FlexfecReceiver::AddAndProcessReceivedPacket(const uint8_t* packet, |
| 51 size_t packet_length) { |
| 49 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | 52 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 50 if (!AddReceivedPacket(std::move(packet))) { | 53 if (!AddReceivedPacket(packet, packet_length)) { |
| 51 return false; | 54 return false; |
| 52 } | 55 } |
| 53 return ProcessReceivedPackets(); | 56 return ProcessReceivedPackets(); |
| 54 } | 57 } |
| 55 | 58 |
| 56 FecPacketCounter FlexfecReceiver::GetPacketCounter() const { | 59 FecPacketCounter FlexfecReceiver::GetPacketCounter() const { |
| 57 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | 60 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 58 return packet_counter_; | 61 return packet_counter_; |
| 59 } | 62 } |
| 60 | 63 |
| 61 bool FlexfecReceiver::AddReceivedPacket(RtpPacketReceived packet) { | 64 bool FlexfecReceiver::AddReceivedPacket(const uint8_t* packet, |
| 65 size_t packet_length) { |
| 62 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); | 66 RTC_DCHECK_CALLED_SEQUENTIALLY(&sequence_checker_); |
| 63 | 67 |
| 64 // RTP packets with a full base header (12 bytes), but without payload, | 68 // RTP packets with a full base header (12 bytes), but without payload, |
| 65 // could conceivably be useful in the decoding. Therefore we check | 69 // could conceivably be useful in the decoding. Therefore we check |
| 66 // with a non-strict inequality here. | 70 // with a strict inequality here. |
| 67 RTC_DCHECK_GE(packet.size(), kRtpHeaderSize); | 71 if (packet_length < kRtpHeaderSize) { |
| 72 LOG(LS_WARNING) << "Truncated packet, discarding."; |
| 73 return false; |
| 74 } |
| 75 |
| 76 // TODO(brandtr): Consider how to handle received FlexFEC packets and |
| 77 // the bandwidth estimator. |
| 78 RtpPacketReceived parsed_packet; |
| 79 if (!parsed_packet.Parse(packet, packet_length)) { |
| 80 return false; |
| 81 } |
| 68 | 82 |
| 69 // Demultiplex based on SSRC, and insert into erasure code decoder. | 83 // Demultiplex based on SSRC, and insert into erasure code decoder. |
| 70 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); | 84 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); |
| 71 received_packet->seq_num = packet.SequenceNumber(); | 85 received_packet->seq_num = parsed_packet.SequenceNumber(); |
| 72 received_packet->ssrc = packet.Ssrc(); | 86 received_packet->ssrc = parsed_packet.Ssrc(); |
| 73 if (received_packet->ssrc == ssrc_) { | 87 if (received_packet->ssrc == ssrc_) { |
| 74 // This is a FlexFEC packet. | 88 // This is a FEC packet belonging to this FlexFEC stream. |
| 75 if (packet.payload_size() < kMinFlexfecHeaderSize) { | 89 if (parsed_packet.payload_size() < kMinFlexfecHeaderSize) { |
| 76 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; | 90 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; |
| 77 return false; | 91 return false; |
| 78 } | 92 } |
| 79 received_packet->is_fec = true; | 93 received_packet->is_fec = true; |
| 80 ++packet_counter_.num_fec_packets; | 94 ++packet_counter_.num_fec_packets; |
| 81 | |
| 82 // Insert packet payload into erasure code. | 95 // Insert packet payload into erasure code. |
| 83 // TODO(brandtr): Remove this memcpy when the FEC packet classes | 96 // TODO(brandtr): Remove this memcpy when the FEC packet classes |
| 84 // are using COW buffers internally. | 97 // are using COW buffers internally. |
| 85 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); | 98 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
| 86 auto payload = packet.payload(); | 99 auto payload = parsed_packet.payload(); |
| 87 memcpy(received_packet->pkt->data, payload.data(), payload.size()); | 100 memcpy(received_packet->pkt->data, payload.data(), payload.size()); |
| 88 received_packet->pkt->length = payload.size(); | 101 received_packet->pkt->length = payload.size(); |
| 89 } else { | 102 } else { |
| 90 // This is a media packet, or a FlexFEC packet belonging to some | 103 // This is a media packet, or a FlexFEC packet belonging to some |
| 91 // other FlexFEC stream. | 104 // other FlexFEC stream. |
| 92 if (received_packet->ssrc != protected_media_ssrc_) { | 105 if (received_packet->ssrc != protected_media_ssrc_) { |
| 93 return false; | 106 return false; |
| 94 } | 107 } |
| 95 received_packet->is_fec = false; | 108 received_packet->is_fec = false; |
| 96 | |
| 97 // Insert entire packet into erasure code. | 109 // Insert entire packet into erasure code. |
| 98 // TODO(brandtr): Remove this memcpy too. | 110 // TODO(brandtr): Remove this memcpy too. |
| 99 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); | 111 received_packet->pkt = rtc::scoped_refptr<Packet>(new Packet()); |
| 100 memcpy(received_packet->pkt->data, packet.data(), packet.size()); | 112 memcpy(received_packet->pkt->data, parsed_packet.data(), |
| 101 received_packet->pkt->length = packet.size(); | 113 parsed_packet.size()); |
| 114 received_packet->pkt->length = parsed_packet.size(); |
| 102 } | 115 } |
| 103 | |
| 104 received_packets_.push_back(std::move(received_packet)); | 116 received_packets_.push_back(std::move(received_packet)); |
| 105 ++packet_counter_.num_packets; | 117 ++packet_counter_.num_packets; |
| 106 | 118 |
| 107 return true; | 119 return true; |
| 108 } | 120 } |
| 109 | 121 |
| 110 // Note that the implementation of this member function and the implementation | 122 // Note that the implementation of this member function and the implementation |
| 111 // in UlpfecReceiver::ProcessReceivedFec() are slightly different. | 123 // in UlpfecReceiver::ProcessReceivedFec() are slightly different. |
| 112 // This implementation only returns _recovered_ media packets through the | 124 // This implementation only returns _recovered_ media packets through the |
| 113 // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ | 125 // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 125 0) { | 137 0) { |
| 126 return false; | 138 return false; |
| 127 } | 139 } |
| 128 } | 140 } |
| 129 // Return recovered packets through callback. | 141 // Return recovered packets through callback. |
| 130 for (const auto& recovered_packet : recovered_packets_) { | 142 for (const auto& recovered_packet : recovered_packets_) { |
| 131 if (recovered_packet->returned) { | 143 if (recovered_packet->returned) { |
| 132 continue; | 144 continue; |
| 133 } | 145 } |
| 134 ++packet_counter_.num_recovered_packets; | 146 ++packet_counter_.num_recovered_packets; |
| 135 if (!recovered_packet_receiver_->OnRecoveredPacket( | 147 if (!callback_->OnRecoveredPacket(recovered_packet->pkt->data, |
| 136 recovered_packet->pkt->data, recovered_packet->pkt->length)) { | 148 recovered_packet->pkt->length)) { |
| 137 return false; | 149 return false; |
| 138 } | 150 } |
| 139 recovered_packet->returned = true; | 151 recovered_packet->returned = true; |
| 140 // Periodically log the incoming packets. | 152 // Periodically log the incoming packets. |
| 141 int64_t now_ms = clock_->TimeInMilliseconds(); | 153 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 142 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { | 154 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { |
| 143 uint32_t media_ssrc = | 155 uint32_t media_ssrc = |
| 144 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); | 156 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); |
| 145 LOG(LS_INFO) << "Recovered media packet with SSRC: " << media_ssrc | 157 LOG(LS_INFO) << "Recovered media packet with SSRC: " << media_ssrc |
| 146 << " from FlexFEC stream with SSRC: " << ssrc_ << "."; | 158 << " from FlexFEC stream with SSRC: " << ssrc_ << "."; |
| 147 last_recovered_packet_ms_ = now_ms; | 159 last_recovered_packet_ms_ = now_ms; |
| 148 } | 160 } |
| 149 } | 161 } |
| 150 return true; | 162 return true; |
| 151 } | 163 } |
| 152 | 164 |
| 153 } // namespace webrtc | 165 } // namespace webrtc |
| OLD | NEW |