OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 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/video/vie_receiver.h" | |
12 | |
13 #include <vector> | |
14 | |
15 #include "webrtc/base/logging.h" | |
16 #include "webrtc/config.h" | |
17 #include "webrtc/modules/pacing/packet_router.h" | |
18 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimat
or.h" | |
19 #include "webrtc/modules/rtp_rtcp/include/fec_receiver.h" | |
20 #include "webrtc/modules/rtp_rtcp/include/receive_statistics.h" | |
21 #include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h" | |
22 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" | |
23 #include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h" | |
24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h" | |
25 #include "webrtc/modules/video_coding/video_coding_impl.h" | |
26 #include "webrtc/system_wrappers/include/metrics.h" | |
27 #include "webrtc/system_wrappers/include/tick_util.h" | |
28 #include "webrtc/system_wrappers/include/timestamp_extrapolator.h" | |
29 #include "webrtc/system_wrappers/include/trace.h" | |
30 | |
31 namespace webrtc { | |
32 | |
33 std::unique_ptr<RtpRtcp> CreateRtpRtcpModule( | |
34 ReceiveStatistics* receive_statistics, | |
35 Transport* outgoing_transport, | |
36 RtcpRttStats* rtt_stats, | |
37 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer, | |
38 RemoteBitrateEstimator* remote_bitrate_estimator, | |
39 RtpPacketSender* paced_sender, | |
40 TransportSequenceNumberAllocator* transport_sequence_number_allocator) { | |
41 RtpRtcp::Configuration configuration; | |
42 configuration.audio = false; | |
43 configuration.receiver_only = true; | |
44 configuration.receive_statistics = receive_statistics; | |
45 configuration.outgoing_transport = outgoing_transport; | |
46 configuration.intra_frame_callback = nullptr; | |
47 configuration.rtt_stats = rtt_stats; | |
48 configuration.rtcp_packet_type_counter_observer = | |
49 rtcp_packet_type_counter_observer; | |
50 configuration.paced_sender = paced_sender; | |
51 configuration.transport_sequence_number_allocator = | |
52 transport_sequence_number_allocator; | |
53 configuration.send_bitrate_observer = nullptr; | |
54 configuration.send_frame_count_observer = nullptr; | |
55 configuration.send_side_delay_observer = nullptr; | |
56 configuration.bandwidth_callback = nullptr; | |
57 configuration.transport_feedback_callback = nullptr; | |
58 | |
59 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration)); | |
60 rtp_rtcp->SetSendingStatus(false); | |
61 rtp_rtcp->SetSendingMediaStatus(false); | |
62 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound); | |
63 | |
64 return rtp_rtcp; | |
65 } | |
66 | |
67 | |
68 static const int kPacketLogIntervalMs = 10000; | |
69 | |
70 ViEReceiver::ViEReceiver(vcm::VideoReceiver* video_receiver, | |
71 RemoteBitrateEstimator* remote_bitrate_estimator, | |
72 RtpFeedback* rtp_feedback, | |
73 Transport* transport, | |
74 RtcpRttStats* rtt_stats, | |
75 PacedSender* paced_sender, | |
76 PacketRouter* packet_router) | |
77 : clock_(Clock::GetRealTimeClock()), | |
78 video_receiver_(video_receiver), | |
79 remote_bitrate_estimator_(remote_bitrate_estimator), | |
80 packet_router_(packet_router), | |
81 ntp_estimator_(clock_), | |
82 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)), | |
83 rtp_header_parser_(RtpHeaderParser::Create()), | |
84 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_, | |
85 this, | |
86 rtp_feedback, | |
87 &rtp_payload_registry_)), | |
88 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)), | |
89 fec_receiver_(FecReceiver::Create(this)), | |
90 receiving_(false), | |
91 restored_packet_in_use_(false), | |
92 last_packet_log_ms_(-1), | |
93 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(), | |
94 transport, | |
95 rtt_stats, | |
96 &rtcp_packet_type_counter_observer_, | |
97 remote_bitrate_estimator_, | |
98 paced_sender, | |
99 packet_router)) { | |
100 packet_router_->AddRtpModule(rtp_rtcp_.get()); | |
101 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp); | |
102 } | |
103 | |
104 ViEReceiver::~ViEReceiver() { | |
105 packet_router_->RemoveRtpModule(rtp_rtcp_.get()); | |
106 UpdateHistograms(); | |
107 } | |
108 | |
109 void ViEReceiver::UpdateHistograms() { | |
110 FecPacketCounter counter = fec_receiver_->GetPacketCounter(); | |
111 if (counter.num_packets > 0) { | |
112 RTC_LOGGED_HISTOGRAM_PERCENTAGE( | |
113 "WebRTC.Video.ReceivedFecPacketsInPercent", | |
114 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets)); | |
115 } | |
116 if (counter.num_fec_packets > 0) { | |
117 RTC_LOGGED_HISTOGRAM_PERCENTAGE( | |
118 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec", | |
119 static_cast<int>(counter.num_recovered_packets * 100 / | |
120 counter.num_fec_packets)); | |
121 } | |
122 } | |
123 | |
124 bool ViEReceiver::SetReceiveCodec(const VideoCodec& video_codec) { | |
125 int8_t old_pltype = -1; | |
126 if (rtp_payload_registry_.ReceivePayloadType( | |
127 video_codec.plName, kVideoPayloadTypeFrequency, 0, | |
128 video_codec.maxBitrate, &old_pltype) != -1) { | |
129 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype); | |
130 } | |
131 | |
132 return rtp_receiver_->RegisterReceivePayload( | |
133 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency, | |
134 0, 0) == 0; | |
135 } | |
136 | |
137 void ViEReceiver::SetNackStatus(bool enable, | |
138 int max_nack_reordering_threshold) { | |
139 if (!enable) { | |
140 // Reset the threshold back to the lower default threshold when NACK is | |
141 // disabled since we no longer will be receiving retransmissions. | |
142 max_nack_reordering_threshold = kDefaultMaxReorderingThreshold; | |
143 } | |
144 rtp_receive_statistics_->SetMaxReorderingThreshold( | |
145 max_nack_reordering_threshold); | |
146 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff); | |
147 } | |
148 | |
149 void ViEReceiver::SetRtxPayloadType(int payload_type, | |
150 int associated_payload_type) { | |
151 rtp_payload_registry_.SetRtxPayloadType(payload_type, | |
152 associated_payload_type); | |
153 } | |
154 | |
155 void ViEReceiver::SetUseRtxPayloadMappingOnRestore(bool val) { | |
156 rtp_payload_registry_.set_use_rtx_payload_mapping_on_restore(val); | |
157 } | |
158 | |
159 void ViEReceiver::SetRtxSsrc(uint32_t ssrc) { | |
160 rtp_payload_registry_.SetRtxSsrc(ssrc); | |
161 } | |
162 | |
163 bool ViEReceiver::GetRtxSsrc(uint32_t* ssrc) const { | |
164 return rtp_payload_registry_.GetRtxSsrc(ssrc); | |
165 } | |
166 | |
167 bool ViEReceiver::IsFecEnabled() const { | |
168 return rtp_payload_registry_.ulpfec_payload_type() > -1; | |
169 } | |
170 | |
171 uint32_t ViEReceiver::GetRemoteSsrc() const { | |
172 return rtp_receiver_->SSRC(); | |
173 } | |
174 | |
175 int ViEReceiver::GetCsrcs(uint32_t* csrcs) const { | |
176 return rtp_receiver_->CSRCs(csrcs); | |
177 } | |
178 | |
179 RtpReceiver* ViEReceiver::GetRtpReceiver() const { | |
180 return rtp_receiver_.get(); | |
181 } | |
182 | |
183 void ViEReceiver::EnableReceiveRtpHeaderExtension(const std::string& extension, | |
184 int id) { | |
185 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); | |
186 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension( | |
187 StringToRtpExtensionType(extension), id)); | |
188 } | |
189 | |
190 void ViEReceiver::RegisterRtcpPacketTypeCounterObserver( | |
191 RtcpPacketTypeCounterObserver* observer) { | |
192 rtcp_packet_type_counter_observer_.Set(observer); | |
193 } | |
194 | |
195 | |
196 int32_t ViEReceiver::OnReceivedPayloadData(const uint8_t* payload_data, | |
197 const size_t payload_size, | |
198 const WebRtcRTPHeader* rtp_header) { | |
199 RTC_DCHECK(video_receiver_); | |
200 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header; | |
201 rtp_header_with_ntp.ntp_time_ms = | |
202 ntp_estimator_.Estimate(rtp_header->header.timestamp); | |
203 if (video_receiver_->IncomingPacket(payload_data, payload_size, | |
204 rtp_header_with_ntp) != 0) { | |
205 // Check this... | |
206 return -1; | |
207 } | |
208 return 0; | |
209 } | |
210 | |
211 bool ViEReceiver::OnRecoveredPacket(const uint8_t* rtp_packet, | |
212 size_t rtp_packet_length) { | |
213 RTPHeader header; | |
214 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) { | |
215 return false; | |
216 } | |
217 header.payload_type_frequency = kVideoPayloadTypeFrequency; | |
218 bool in_order = IsPacketInOrder(header); | |
219 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order); | |
220 } | |
221 | |
222 bool ViEReceiver::DeliverRtp(const uint8_t* rtp_packet, | |
223 size_t rtp_packet_length, | |
224 const PacketTime& packet_time) { | |
225 RTC_DCHECK(remote_bitrate_estimator_); | |
226 { | |
227 rtc::CritScope lock(&receive_cs_); | |
228 if (!receiving_) { | |
229 return false; | |
230 } | |
231 } | |
232 | |
233 RTPHeader header; | |
234 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, | |
235 &header)) { | |
236 return false; | |
237 } | |
238 size_t payload_length = rtp_packet_length - header.headerLength; | |
239 int64_t arrival_time_ms; | |
240 int64_t now_ms = clock_->TimeInMilliseconds(); | |
241 if (packet_time.timestamp != -1) | |
242 arrival_time_ms = (packet_time.timestamp + 500) / 1000; | |
243 else | |
244 arrival_time_ms = now_ms; | |
245 | |
246 { | |
247 // Periodically log the RTP header of incoming packets. | |
248 rtc::CritScope lock(&receive_cs_); | |
249 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) { | |
250 std::stringstream ss; | |
251 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: " | |
252 << static_cast<int>(header.payloadType) << ", timestamp: " | |
253 << header.timestamp << ", sequence number: " << header.sequenceNumber | |
254 << ", arrival time: " << arrival_time_ms; | |
255 if (header.extension.hasTransmissionTimeOffset) | |
256 ss << ", toffset: " << header.extension.transmissionTimeOffset; | |
257 if (header.extension.hasAbsoluteSendTime) | |
258 ss << ", abs send time: " << header.extension.absoluteSendTime; | |
259 LOG(LS_INFO) << ss.str(); | |
260 last_packet_log_ms_ = now_ms; | |
261 } | |
262 } | |
263 | |
264 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length, | |
265 header, true); | |
266 header.payload_type_frequency = kVideoPayloadTypeFrequency; | |
267 | |
268 bool in_order = IsPacketInOrder(header); | |
269 rtp_payload_registry_.SetIncomingPayloadType(header); | |
270 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order); | |
271 // Update receive statistics after ReceivePacket. | |
272 // Receive statistics will be reset if the payload type changes (make sure | |
273 // that the first packet is included in the stats). | |
274 rtp_receive_statistics_->IncomingPacket( | |
275 header, rtp_packet_length, IsPacketRetransmitted(header, in_order)); | |
276 return ret; | |
277 } | |
278 | |
279 bool ViEReceiver::ReceivePacket(const uint8_t* packet, | |
280 size_t packet_length, | |
281 const RTPHeader& header, | |
282 bool in_order) { | |
283 if (rtp_payload_registry_.IsEncapsulated(header)) { | |
284 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header); | |
285 } | |
286 const uint8_t* payload = packet + header.headerLength; | |
287 assert(packet_length >= header.headerLength); | |
288 size_t payload_length = packet_length - header.headerLength; | |
289 PayloadUnion payload_specific; | |
290 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType, | |
291 &payload_specific)) { | |
292 return false; | |
293 } | |
294 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length, | |
295 payload_specific, in_order); | |
296 } | |
297 | |
298 bool ViEReceiver::ParseAndHandleEncapsulatingHeader(const uint8_t* packet, | |
299 size_t packet_length, | |
300 const RTPHeader& header) { | |
301 if (rtp_payload_registry_.IsRed(header)) { | |
302 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type(); | |
303 if (packet[header.headerLength] == ulpfec_pt) { | |
304 rtp_receive_statistics_->FecPacketReceived(header, packet_length); | |
305 // Notify video_receiver about received FEC packets to avoid NACKing these | |
306 // packets. | |
307 NotifyReceiverOfFecPacket(header); | |
308 } | |
309 if (fec_receiver_->AddReceivedRedPacket( | |
310 header, packet, packet_length, ulpfec_pt) != 0) { | |
311 return false; | |
312 } | |
313 return fec_receiver_->ProcessReceivedFec() == 0; | |
314 } else if (rtp_payload_registry_.IsRtx(header)) { | |
315 if (header.headerLength + header.paddingLength == packet_length) { | |
316 // This is an empty packet and should be silently dropped before trying to | |
317 // parse the RTX header. | |
318 return true; | |
319 } | |
320 // Remove the RTX header and parse the original RTP header. | |
321 if (packet_length < header.headerLength) | |
322 return false; | |
323 if (packet_length > sizeof(restored_packet_)) | |
324 return false; | |
325 rtc::CritScope lock(&receive_cs_); | |
326 if (restored_packet_in_use_) { | |
327 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet."; | |
328 return false; | |
329 } | |
330 if (!rtp_payload_registry_.RestoreOriginalPacket( | |
331 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(), | |
332 header)) { | |
333 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: " | |
334 << header.ssrc << " payload type: " | |
335 << static_cast<int>(header.payloadType); | |
336 return false; | |
337 } | |
338 restored_packet_in_use_ = true; | |
339 bool ret = OnRecoveredPacket(restored_packet_, packet_length); | |
340 restored_packet_in_use_ = false; | |
341 return ret; | |
342 } | |
343 return false; | |
344 } | |
345 | |
346 void ViEReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) { | |
347 int8_t last_media_payload_type = | |
348 rtp_payload_registry_.last_received_media_payload_type(); | |
349 if (last_media_payload_type < 0) { | |
350 LOG(LS_WARNING) << "Failed to get last media payload type."; | |
351 return; | |
352 } | |
353 // Fake an empty media packet. | |
354 WebRtcRTPHeader rtp_header = {}; | |
355 rtp_header.header = header; | |
356 rtp_header.header.payloadType = last_media_payload_type; | |
357 rtp_header.header.paddingLength = 0; | |
358 PayloadUnion payload_specific; | |
359 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type, | |
360 &payload_specific)) { | |
361 LOG(LS_WARNING) << "Failed to get payload specifics."; | |
362 return; | |
363 } | |
364 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType; | |
365 rtp_header.type.Video.rotation = kVideoRotation_0; | |
366 if (header.extension.hasVideoRotation) { | |
367 rtp_header.type.Video.rotation = | |
368 ConvertCVOByteToVideoRotation(header.extension.videoRotation); | |
369 } | |
370 OnReceivedPayloadData(nullptr, 0, &rtp_header); | |
371 } | |
372 | |
373 bool ViEReceiver::DeliverRtcp(const uint8_t* rtcp_packet, | |
374 size_t rtcp_packet_length) { | |
375 { | |
376 rtc::CritScope lock(&receive_cs_); | |
377 if (!receiving_) { | |
378 return false; | |
379 } | |
380 } | |
381 | |
382 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length); | |
383 | |
384 int64_t rtt = 0; | |
385 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr); | |
386 if (rtt == 0) { | |
387 // Waiting for valid rtt. | |
388 return true; | |
389 } | |
390 uint32_t ntp_secs = 0; | |
391 uint32_t ntp_frac = 0; | |
392 uint32_t rtp_timestamp = 0; | |
393 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr, | |
394 &rtp_timestamp) != 0) { | |
395 // Waiting for RTCP. | |
396 return true; | |
397 } | |
398 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); | |
399 | |
400 return true; | |
401 } | |
402 | |
403 void ViEReceiver::StartReceive() { | |
404 rtc::CritScope lock(&receive_cs_); | |
405 receiving_ = true; | |
406 } | |
407 | |
408 void ViEReceiver::StopReceive() { | |
409 rtc::CritScope lock(&receive_cs_); | |
410 receiving_ = false; | |
411 } | |
412 | |
413 ReceiveStatistics* ViEReceiver::GetReceiveStatistics() const { | |
414 return rtp_receive_statistics_.get(); | |
415 } | |
416 | |
417 bool ViEReceiver::IsPacketInOrder(const RTPHeader& header) const { | |
418 StreamStatistician* statistician = | |
419 rtp_receive_statistics_->GetStatistician(header.ssrc); | |
420 if (!statistician) | |
421 return false; | |
422 return statistician->IsPacketInOrder(header.sequenceNumber); | |
423 } | |
424 | |
425 bool ViEReceiver::IsPacketRetransmitted(const RTPHeader& header, | |
426 bool in_order) const { | |
427 // Retransmissions are handled separately if RTX is enabled. | |
428 if (rtp_payload_registry_.RtxEnabled()) | |
429 return false; | |
430 StreamStatistician* statistician = | |
431 rtp_receive_statistics_->GetStatistician(header.ssrc); | |
432 if (!statistician) | |
433 return false; | |
434 // Check if this is a retransmission. | |
435 int64_t min_rtt = 0; | |
436 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr); | |
437 return !in_order && | |
438 statistician->IsRetransmitOfOldPacket(header, min_rtt); | |
439 } | |
440 } // namespace webrtc | |
OLD | NEW |