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