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/source/flexfec_receiver_impl.h" | 11 #include "webrtc/modules/rtp_rtcp/include/flexfec_receiver.h" |
12 | 12 |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
16 #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" | 17 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h" |
18 | 18 |
19 namespace webrtc { | 19 namespace webrtc { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 using Packet = ForwardErrorCorrection::Packet; | 23 using Packet = ForwardErrorCorrection::Packet; |
24 using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; | 24 using ReceivedPacket = ForwardErrorCorrection::ReceivedPacket; |
25 | 25 |
26 // 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. |
27 constexpr size_t kMinFlexfecHeaderSize = 20; | 27 constexpr size_t kMinFlexfecHeaderSize = 20; |
28 | 28 |
29 // How often to log the recovered packets to the text log. | 29 // How often to log the recovered packets to the text log. |
30 constexpr int kPacketLogIntervalMs = 10000; | 30 constexpr int kPacketLogIntervalMs = 10000; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 std::unique_ptr<FlexfecReceiver> FlexfecReceiver::Create( | 34 FlexfecReceiver::FlexfecReceiver(uint32_t ssrc, |
35 uint32_t flexfec_ssrc, | 35 uint32_t protected_media_ssrc, |
36 uint32_t protected_media_ssrc, | 36 RecoveredPacketReceiver* callback) |
37 RecoveredPacketReceiver* callback) { | 37 : ssrc_(ssrc), |
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), | 38 protected_media_ssrc_(protected_media_ssrc), |
49 erasure_code_(ForwardErrorCorrection::CreateFlexfec()), | 39 erasure_code_(ForwardErrorCorrection::CreateFlexfec()), |
50 callback_(callback), | 40 callback_(callback), |
51 clock_(Clock::GetRealTimeClock()), | 41 clock_(Clock::GetRealTimeClock()), |
52 last_recovered_packet_ms_(-1) { | 42 last_recovered_packet_ms_(-1) { |
53 // 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 |
54 // the one used during main operation. | 44 // the one used during main operation. |
55 sequence_checker_.Detach(); | 45 sequence_checker_.Detach(); |
56 } | 46 } |
57 | 47 |
58 FlexfecReceiverImpl::~FlexfecReceiverImpl() = default; | 48 FlexfecReceiver::~FlexfecReceiver() = default; |
59 | 49 |
60 bool FlexfecReceiverImpl::AddAndProcessReceivedPacket(const uint8_t* packet, | 50 bool FlexfecReceiver::AddAndProcessReceivedPacket(const uint8_t* packet, |
61 size_t packet_length) { | 51 size_t packet_length) { |
62 RTC_DCHECK(sequence_checker_.CalledSequentially()); | 52 RTC_DCHECK(sequence_checker_.CalledSequentially()); |
63 | 53 |
64 if (!AddReceivedPacket(packet, packet_length)) { | 54 if (!AddReceivedPacket(packet, packet_length)) { |
65 return false; | 55 return false; |
66 } | 56 } |
67 return ProcessReceivedPackets(); | 57 return ProcessReceivedPackets(); |
68 } | 58 } |
69 | 59 |
70 FecPacketCounter FlexfecReceiverImpl::GetPacketCounter() const { | 60 FecPacketCounter FlexfecReceiver::GetPacketCounter() const { |
71 RTC_DCHECK(sequence_checker_.CalledSequentially()); | 61 RTC_DCHECK(sequence_checker_.CalledSequentially()); |
72 return packet_counter_; | 62 return packet_counter_; |
73 } | 63 } |
74 | 64 |
75 bool FlexfecReceiverImpl::AddReceivedPacket(const uint8_t* packet, | 65 bool FlexfecReceiver::AddReceivedPacket(const uint8_t* packet, |
76 size_t packet_length) { | 66 size_t packet_length) { |
77 RTC_DCHECK(sequence_checker_.CalledSequentially()); | 67 RTC_DCHECK(sequence_checker_.CalledSequentially()); |
78 | 68 |
79 // RTP packets with a full base header (12 bytes), but without payload, | 69 // RTP packets with a full base header (12 bytes), but without payload, |
80 // could conceivably be useful in the decoding. Therefore we check | 70 // could conceivably be useful in the decoding. Therefore we check |
81 // with a strict inequality here. | 71 // with a strict inequality here. |
82 if (packet_length < kRtpHeaderSize) { | 72 if (packet_length < kRtpHeaderSize) { |
83 LOG(LS_WARNING) << "Truncated packet, discarding."; | 73 LOG(LS_WARNING) << "Truncated packet, discarding."; |
84 return false; | 74 return false; |
85 } | 75 } |
86 | 76 |
87 // TODO(brandtr): Consider how to handle received FlexFEC packets and | 77 // TODO(brandtr): Consider how to handle received FlexFEC packets and |
88 // the bandwidth estimator. | 78 // the bandwidth estimator. |
89 RtpPacketReceived parsed_packet; | 79 RtpPacketReceived parsed_packet; |
90 if (!parsed_packet.Parse(packet, packet_length)) { | 80 if (!parsed_packet.Parse(packet, packet_length)) { |
91 return false; | 81 return false; |
92 } | 82 } |
93 | 83 |
94 // Demultiplex based on SSRC, and insert into erasure code decoder. | 84 // Demultiplex based on SSRC, and insert into erasure code decoder. |
95 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); | 85 std::unique_ptr<ReceivedPacket> received_packet(new ReceivedPacket()); |
96 received_packet->seq_num = parsed_packet.SequenceNumber(); | 86 received_packet->seq_num = parsed_packet.SequenceNumber(); |
97 received_packet->ssrc = parsed_packet.Ssrc(); | 87 received_packet->ssrc = parsed_packet.Ssrc(); |
98 if (received_packet->ssrc == flexfec_ssrc_) { | 88 if (received_packet->ssrc == ssrc_) { |
99 // This is a FEC packet belonging to this FlexFEC stream. | 89 // This is a FEC packet belonging to this FlexFEC stream. |
100 if (parsed_packet.payload_size() < kMinFlexfecHeaderSize) { | 90 if (parsed_packet.payload_size() < kMinFlexfecHeaderSize) { |
101 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; | 91 LOG(LS_WARNING) << "Truncated FlexFEC packet, discarding."; |
102 return false; | 92 return false; |
103 } | 93 } |
104 received_packet->is_fec = true; | 94 received_packet->is_fec = true; |
105 ++packet_counter_.num_fec_packets; | 95 ++packet_counter_.num_fec_packets; |
106 // Insert packet payload into erasure code. | 96 // Insert packet payload into erasure code. |
107 // TODO(brandtr): Remove this memcpy when the FEC packet classes | 97 // TODO(brandtr): Remove this memcpy when the FEC packet classes |
108 // are using COW buffers internally. | 98 // are using COW buffers internally. |
(...skipping 23 matching lines...) Expand all Loading... |
132 | 122 |
133 // Note that the implementation of this member function and the implementation | 123 // Note that the implementation of this member function and the implementation |
134 // in UlpfecReceiver::ProcessReceivedFec() are slightly different. | 124 // in UlpfecReceiver::ProcessReceivedFec() are slightly different. |
135 // This implementation only returns _recovered_ media packets through the | 125 // This implementation only returns _recovered_ media packets through the |
136 // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ | 126 // callback, whereas the implementation in UlpfecReceiver returns _all inserted_ |
137 // media packets through the callback. The latter behaviour makes sense | 127 // media packets through the callback. The latter behaviour makes sense |
138 // for ULPFEC, since the ULPFEC receiver is owned by the RtpStreamReceiver. | 128 // for ULPFEC, since the ULPFEC receiver is owned by the RtpStreamReceiver. |
139 // Here, however, the received media pipeline is more decoupled from the | 129 // Here, however, the received media pipeline is more decoupled from the |
140 // FlexFEC decoder, and we therefore do not interfere with the reception | 130 // FlexFEC decoder, and we therefore do not interfere with the reception |
141 // of non-recovered media packets. | 131 // of non-recovered media packets. |
142 bool FlexfecReceiverImpl::ProcessReceivedPackets() { | 132 bool FlexfecReceiver::ProcessReceivedPackets() { |
143 RTC_DCHECK(sequence_checker_.CalledSequentially()); | 133 RTC_DCHECK(sequence_checker_.CalledSequentially()); |
144 | 134 |
145 // Decode. | 135 // Decode. |
146 if (!received_packets_.empty()) { | 136 if (!received_packets_.empty()) { |
147 if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) != | 137 if (erasure_code_->DecodeFec(&received_packets_, &recovered_packets_) != |
148 0) { | 138 0) { |
149 return false; | 139 return false; |
150 } | 140 } |
151 } | 141 } |
152 // Return recovered packets through callback. | 142 // Return recovered packets through callback. |
153 for (const auto& recovered_packet : recovered_packets_) { | 143 for (const auto& recovered_packet : recovered_packets_) { |
154 if (recovered_packet->returned) { | 144 if (recovered_packet->returned) { |
155 continue; | 145 continue; |
156 } | 146 } |
157 ++packet_counter_.num_recovered_packets; | 147 ++packet_counter_.num_recovered_packets; |
158 if (!callback_->OnRecoveredPacket(recovered_packet->pkt->data, | 148 if (!callback_->OnRecoveredPacket(recovered_packet->pkt->data, |
159 recovered_packet->pkt->length)) { | 149 recovered_packet->pkt->length)) { |
160 return false; | 150 return false; |
161 } | 151 } |
162 recovered_packet->returned = true; | 152 recovered_packet->returned = true; |
163 // Periodically log the incoming packets. | 153 // Periodically log the incoming packets. |
164 int64_t now_ms = clock_->TimeInMilliseconds(); | 154 int64_t now_ms = clock_->TimeInMilliseconds(); |
165 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { | 155 if (now_ms - last_recovered_packet_ms_ > kPacketLogIntervalMs) { |
166 uint32_t media_ssrc = | 156 uint32_t media_ssrc = |
167 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); | 157 ForwardErrorCorrection::ParseSsrc(recovered_packet->pkt->data); |
168 std::stringstream ss; | 158 LOG(LS_INFO) << "Recovered media packet with SSRC: " << media_ssrc |
169 ss << "Recovered media packet with SSRC: " << media_ssrc | 159 << " from FlexFEC stream with SSRC: " << ssrc_ << "."; |
170 << " from FlexFEC stream with SSRC: " << flexfec_ssrc_ << "."; | |
171 LOG(LS_INFO) << ss.str(); | |
172 last_recovered_packet_ms_ = now_ms; | 160 last_recovered_packet_ms_ = now_ms; |
173 } | 161 } |
174 } | 162 } |
175 return true; | 163 return true; |
176 } | 164 } |
177 | 165 |
178 } // namespace webrtc | 166 } // namespace webrtc |
OLD | NEW |