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