blob: 20acc06216d3c2400cde20aad56778033651f5bd [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org39e96592012-03-01 18:22:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
mflodmanfa666592016-04-28 23:15:33 -070011#include "webrtc/video/rtp_stream_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000013#include <vector>
14
mflodman4cd27902016-08-05 06:28:45 -070015#include "webrtc/base/checks.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010016#include "webrtc/base/logging.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070017#include "webrtc/common_types.h"
Peter Boström9c017252016-02-26 16:26:20 +010018#include "webrtc/config.h"
mflodmanc0e58a32016-04-25 01:26:26 -070019#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000020#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#include "webrtc/modules/rtp_rtcp/include/fec_receiver.h"
22#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
24#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
26#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Peter Boström0b250722016-04-22 18:23:15 +020027#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010029#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
30#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070031#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070032#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000033
34namespace webrtc {
35
mflodmanc0e58a32016-04-25 01:26:26 -070036std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
37 ReceiveStatistics* receive_statistics,
38 Transport* outgoing_transport,
39 RtcpRttStats* rtt_stats,
40 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
41 RemoteBitrateEstimator* remote_bitrate_estimator,
42 RtpPacketSender* paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +020043 TransportSequenceNumberAllocator* transport_sequence_number_allocator,
44 RateLimiter* retransmission_rate_limiter) {
mflodmanc0e58a32016-04-25 01:26:26 -070045 RtpRtcp::Configuration configuration;
46 configuration.audio = false;
47 configuration.receiver_only = true;
48 configuration.receive_statistics = receive_statistics;
49 configuration.outgoing_transport = outgoing_transport;
50 configuration.intra_frame_callback = nullptr;
51 configuration.rtt_stats = rtt_stats;
52 configuration.rtcp_packet_type_counter_observer =
53 rtcp_packet_type_counter_observer;
54 configuration.paced_sender = paced_sender;
55 configuration.transport_sequence_number_allocator =
56 transport_sequence_number_allocator;
57 configuration.send_bitrate_observer = nullptr;
58 configuration.send_frame_count_observer = nullptr;
59 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070060 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070061 configuration.bandwidth_callback = nullptr;
62 configuration.transport_feedback_callback = nullptr;
Erik Språng737336d2016-07-29 12:59:36 +020063 configuration.retransmission_rate_limiter = retransmission_rate_limiter;
mflodmanc0e58a32016-04-25 01:26:26 -070064
65 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
66 rtp_rtcp->SetSendingStatus(false);
67 rtp_rtcp->SetSendingMediaStatus(false);
68 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
69
70 return rtp_rtcp;
71}
72
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000073static const int kPacketLogIntervalMs = 10000;
74
mflodmanfa666592016-04-28 23:15:33 -070075RtpStreamReceiver::RtpStreamReceiver(
76 vcm::VideoReceiver* video_receiver,
77 RemoteBitrateEstimator* remote_bitrate_estimator,
78 Transport* transport,
79 RtcpRttStats* rtt_stats,
80 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070081 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070082 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020083 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070084 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020085 ProcessThread* process_thread,
86 RateLimiter* retransmission_rate_limiter)
Tommi97888bd2016-01-21 23:24:59 +010087 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020088 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +020089 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000090 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -070091 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070092 remb_(remb),
93 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010094 ntp_estimator_(clock_),
95 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)),
96 rtp_header_parser_(RtpHeaderParser::Create()),
97 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
98 this,
mflodmanfa666592016-04-28 23:15:33 -070099 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100100 &rtp_payload_registry_)),
101 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
102 fec_receiver_(FecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000103 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000104 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700105 last_packet_log_ms_(-1),
106 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
107 transport,
108 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700109 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700110 remote_bitrate_estimator_,
111 paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +0200112 packet_router,
113 retransmission_rate_limiter)) {
mflodmanc0e58a32016-04-25 01:26:26 -0700114 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700115 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
116 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
117
Tommi733b5472016-06-10 17:58:01 +0200118 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700119 << "A stream should not be configured with RTCP disabled. This value is "
120 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700121 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
122 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
123 RTC_DCHECK(config_.rtp.local_ssrc != 0);
124 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
125
Tommi733b5472016-06-10 17:58:01 +0200126 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
127 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700128 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200129 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700130 rtp_rtcp_->SetREMBStatus(true);
131 remb_->AddReceiveChannel(rtp_rtcp_.get());
132 }
133
Tommi733b5472016-06-10 17:58:01 +0200134 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
135 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
136 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700137 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700138
139 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200140 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
141 ? kMaxPacketAgeToNack
142 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700143 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700144
145 // TODO(pbos): Support multiple RTX, per video payload.
146 for (const auto& kv : config_.rtp.rtx) {
147 RTC_DCHECK(kv.second.ssrc != 0);
148 RTC_DCHECK(kv.second.payload_type != 0);
149
150 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
151 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
152 kv.first);
153 }
154
155 // If set to true, the RTX payload type mapping supplied in
156 // |SetRtxPayloadType| will be used when restoring RTX packets. Without it,
157 // RTX packets will always be restored to the last non-RTX packet payload type
158 // received.
159 // TODO(holmer): When Chrome no longer depends on this being false by default,
160 // always use the mapping and remove this whole codepath.
161 rtp_payload_registry_.set_use_rtx_payload_mapping_on_restore(
162 config_.rtp.use_rtx_payload_mapping_on_restore);
163
164 if (IsFecEnabled()) {
165 VideoCodec ulpfec_codec = {};
166 ulpfec_codec.codecType = kVideoCodecULPFEC;
167 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
168 ulpfec_codec.plType = config_.rtp.fec.ulpfec_payload_type;
169 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
170
171 VideoCodec red_codec = {};
172 red_codec.codecType = kVideoCodecRED;
173 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
174 red_codec.plType = config_.rtp.fec.red_payload_type;
175 RTC_CHECK(SetReceiveCodec(red_codec));
176 if (config_.rtp.fec.red_rtx_payload_type != -1) {
177 rtp_payload_registry_.SetRtxPayloadType(
178 config_.rtp.fec.red_rtx_payload_type,
179 config_.rtp.fec.red_payload_type);
180 }
philipelae284082016-05-09 12:14:29 +0200181
182 rtp_rtcp_->SetGenericFECStatus(true,
183 config_.rtp.fec.red_payload_type,
184 config_.rtp.fec.ulpfec_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700185 }
186
Tommi733b5472016-06-10 17:58:01 +0200187 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700188 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
189
190 // Stats callback for CNAME changes.
191 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
192
mflodmandc7d0d22016-05-06 05:32:22 -0700193 process_thread_->RegisterModule(rtp_rtcp_.get());
mflodmanc0e58a32016-04-25 01:26:26 -0700194}
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
mflodmanfa666592016-04-28 23:15:33 -0700196RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700197 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
mflodmandc7d0d22016-05-06 05:32:22 -0700199 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
200 rtp_rtcp_->SetREMBStatus(false);
201 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
202 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000203}
204
mflodmanfa666592016-04-28 23:15:33 -0700205bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000206 int8_t old_pltype = -1;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100207 if (rtp_payload_registry_.ReceivePayloadType(
208 video_codec.plName, kVideoPayloadTypeFrequency, 0,
209 video_codec.maxBitrate, &old_pltype) != -1) {
210 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000211 }
212
Peter Boström4fa7eca2016-03-02 15:05:53 +0100213 return rtp_receiver_->RegisterReceivePayload(
214 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency,
215 0, 0) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000216}
217
mflodmanfa666592016-04-28 23:15:33 -0700218uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000219 return rtp_receiver_->SSRC();
220}
221
mflodmanfa666592016-04-28 23:15:33 -0700222int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000223 return rtp_receiver_->CSRCs(csrcs);
224}
225
mflodmanfa666592016-04-28 23:15:33 -0700226RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000227 return rtp_receiver_.get();
228}
229
mflodmanfa666592016-04-28 23:15:33 -0700230int32_t RtpStreamReceiver::OnReceivedPayloadData(
231 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200232 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700233 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200234 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000235 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000236 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100237 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Peter Boström0b250722016-04-22 18:23:15 +0200238 if (video_receiver_->IncomingPacket(payload_data, payload_size,
239 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000240 // Check this...
241 return -1;
242 }
243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
mflodmanfa666592016-04-28 23:15:33 -0700246bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
247 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000248 RTPHeader header;
249 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000250 return false;
251 }
252 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000253 bool in_order = IsPacketInOrder(header);
254 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000255}
256
mflodmanfa666592016-04-28 23:15:33 -0700257// TODO(pbos): Remove as soon as audio can handle a changing payload type
258// without this callback.
259int32_t RtpStreamReceiver::OnInitializeDecoder(
260 const int8_t payload_type,
261 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
262 const int frequency,
263 const size_t channels,
264 const uint32_t rate) {
265 RTC_NOTREACHED();
266 return 0;
267}
268
269void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
270 rtp_rtcp_->SetRemoteSSRC(ssrc);
271}
272
273bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
274 size_t rtp_packet_length,
275 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100276 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000277 {
Tommi97888bd2016-01-21 23:24:59 +0100278 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000279 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100280 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000281 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000282 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000283
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000284 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000285 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000286 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100287 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000288 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000289 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000290 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000291 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000292 if (packet_time.timestamp != -1)
293 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
294 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000295 arrival_time_ms = now_ms;
296
297 {
298 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100299 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000300 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
301 std::stringstream ss;
302 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
303 << static_cast<int>(header.payloadType) << ", timestamp: "
304 << header.timestamp << ", sequence number: " << header.sequenceNumber
305 << ", arrival time: " << arrival_time_ms;
306 if (header.extension.hasTransmissionTimeOffset)
307 ss << ", toffset: " << header.extension.transmissionTimeOffset;
308 if (header.extension.hasAbsoluteSendTime)
309 ss << ", abs send time: " << header.extension.absoluteSendTime;
310 LOG(LS_INFO) << ss.str();
311 last_packet_log_ms_ = now_ms;
312 }
313 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000314
Stefan Holmerff4ea932015-06-18 16:01:33 +0200315 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700316 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000317 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000318
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000319 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100320 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100321 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000322 // Update receive statistics after ReceivePacket.
323 // Receive statistics will be reset if the payload type changes (make sure
324 // that the first packet is included in the stats).
325 rtp_receive_statistics_->IncomingPacket(
326 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
327 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000328}
329
mflodmancfc8e3b2016-05-03 21:22:04 -0700330int32_t RtpStreamReceiver::RequestKeyFrame() {
331 return rtp_rtcp_->RequestKeyFrame();
332}
333
334int32_t RtpStreamReceiver::SliceLossIndicationRequest(
335 const uint64_t picture_id) {
336 return rtp_rtcp_->SendRTCPSliceLossIndication(
337 static_cast<uint8_t>(picture_id));
338}
339
mflodmandc7d0d22016-05-06 05:32:22 -0700340bool RtpStreamReceiver::IsFecEnabled() const {
341 return config_.rtp.fec.red_payload_type != -1 &&
342 config_.rtp.fec.ulpfec_payload_type != -1;
343}
344
345bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
346 return config_.rtp.nack.rtp_history_ms > 0;
347}
348
349void RtpStreamReceiver::RequestPacketRetransmit(
350 const std::vector<uint16_t>& sequence_numbers) {
351 rtp_rtcp_->SendNack(sequence_numbers);
352}
353
mflodmancfc8e3b2016-05-03 21:22:04 -0700354int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
355 uint16_t length) {
356 return rtp_rtcp_->SendNACK(sequence_numbers, length);
357}
358
mflodmanfa666592016-04-28 23:15:33 -0700359bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
360 size_t packet_length,
361 const RTPHeader& header,
362 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100363 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000364 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
365 }
366 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000367 assert(packet_length >= header.headerLength);
368 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000369 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100370 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
371 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000372 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000373 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000374 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
375 payload_specific, in_order);
376}
377
mflodmanfa666592016-04-28 23:15:33 -0700378bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
379 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100380 if (rtp_payload_registry_.IsRed(header)) {
381 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000382 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000383 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200384 // Notify video_receiver about received FEC packets to avoid NACKing these
385 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000386 NotifyReceiverOfFecPacket(header);
387 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000388 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000389 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000390 return false;
391 }
392 return fec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100393 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000394 if (header.headerLength + header.paddingLength == packet_length) {
395 // This is an empty packet and should be silently dropped before trying to
396 // parse the RTX header.
397 return true;
398 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000399 // Remove the RTX header and parse the original RTP header.
400 if (packet_length < header.headerLength)
401 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000402 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000403 return false;
Tommi97888bd2016-01-21 23:24:59 +0100404 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000405 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000406 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000407 return false;
408 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100409 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700410 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
411 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100412 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
413 << header.ssrc << " payload type: "
414 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000415 return false;
416 }
417 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700418 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000419 restored_packet_in_use_ = false;
420 return ret;
421 }
422 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423}
424
mflodmanfa666592016-04-28 23:15:33 -0700425void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000426 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100427 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000428 if (last_media_payload_type < 0) {
429 LOG(LS_WARNING) << "Failed to get last media payload type.";
430 return;
431 }
432 // Fake an empty media packet.
433 WebRtcRTPHeader rtp_header = {};
434 rtp_header.header = header;
435 rtp_header.header.payloadType = last_media_payload_type;
436 rtp_header.header.paddingLength = 0;
437 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100438 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
439 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000440 LOG(LS_WARNING) << "Failed to get payload specifics.";
441 return;
442 }
443 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000444 rtp_header.type.Video.rotation = kVideoRotation_0;
445 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700446 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000447 }
isheriff6b4b5f32016-06-08 00:24:21 -0700448 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
449
Peter Boström74f6e9e2016-04-04 17:56:10 +0200450 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000451}
452
mflodmanfa666592016-04-28 23:15:33 -0700453bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
454 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000455 {
Tommi97888bd2016-01-21 23:24:59 +0100456 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000457 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100458 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000459 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100460 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
Per83d09102016-04-15 14:59:13 +0200462 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000463
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000464 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200465 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000466 if (rtt == 0) {
467 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100468 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000469 }
470 uint32_t ntp_secs = 0;
471 uint32_t ntp_frac = 0;
472 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200473 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
474 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000475 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100476 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000477 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100478 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000479
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100480 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000481}
482
mflodmandc7d0d22016-05-06 05:32:22 -0700483void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
484 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
485 : RtcpMode::kOff);
486}
487
mflodmanfa666592016-04-28 23:15:33 -0700488void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100489 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000490 receiving_ = true;
491}
492
mflodmanfa666592016-04-28 23:15:33 -0700493void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100494 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000495 receiving_ = false;
496}
497
mflodmanfa666592016-04-28 23:15:33 -0700498bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000499 StreamStatistician* statistician =
500 rtp_receive_statistics_->GetStatistician(header.ssrc);
501 if (!statistician)
502 return false;
503 return statistician->IsPacketInOrder(header.sequenceNumber);
504}
505
mflodmanfa666592016-04-28 23:15:33 -0700506bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
507 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000508 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100509 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000510 return false;
511 StreamStatistician* statistician =
512 rtp_receive_statistics_->GetStatistician(header.ssrc);
513 if (!statistician)
514 return false;
515 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000516 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200517 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000518 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000519 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000520}
mflodmandc7d0d22016-05-06 05:32:22 -0700521
522void RtpStreamReceiver::UpdateHistograms() {
523 FecPacketCounter counter = fec_receiver_->GetPacketCounter();
524 if (counter.num_packets > 0) {
525 RTC_LOGGED_HISTOGRAM_PERCENTAGE(
526 "WebRTC.Video.ReceivedFecPacketsInPercent",
527 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
528 }
529 if (counter.num_fec_packets > 0) {
530 RTC_LOGGED_HISTOGRAM_PERCENTAGE(
531 "WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
532 static_cast<int>(counter.num_recovered_packets * 100 /
533 counter.num_fec_packets));
534 }
535}
536
537void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
538 const std::string& extension, int id) {
539 // One-byte-extension local identifiers are in the range 1-14 inclusive.
540 RTC_DCHECK_GE(id, 1);
541 RTC_DCHECK_LE(id, 14);
542 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
543 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
544 StringToRtpExtensionType(extension), id));
545}
546
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000547} // namespace webrtc