blob: e7367d8a1a276fc3e2461bcf497093f7812c35c3 [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
Peter Boström415d2cd2015-10-26 11:35:17 +010015#include "webrtc/base/logging.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070016#include "webrtc/common_types.h"
Peter Boström9c017252016-02-26 16:26:20 +010017#include "webrtc/config.h"
mflodmanc0e58a32016-04-25 01:26:26 -070018#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000019#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/rtp_rtcp/include/fec_receiver.h"
21#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010022#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
23#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010024#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
25#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Peter Boström0b250722016-04-22 18:23:15 +020026#include "webrtc/modules/video_coding/video_coding_impl.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010027#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010028#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
29#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070030#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070031#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
33namespace webrtc {
34
mflodmanc0e58a32016-04-25 01:26:26 -070035std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
36 ReceiveStatistics* receive_statistics,
37 Transport* outgoing_transport,
38 RtcpRttStats* rtt_stats,
39 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
40 RemoteBitrateEstimator* remote_bitrate_estimator,
41 RtpPacketSender* paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +020042 TransportSequenceNumberAllocator* transport_sequence_number_allocator,
43 RateLimiter* retransmission_rate_limiter) {
mflodmanc0e58a32016-04-25 01:26:26 -070044 RtpRtcp::Configuration configuration;
45 configuration.audio = false;
46 configuration.receiver_only = true;
47 configuration.receive_statistics = receive_statistics;
48 configuration.outgoing_transport = outgoing_transport;
49 configuration.intra_frame_callback = nullptr;
50 configuration.rtt_stats = rtt_stats;
51 configuration.rtcp_packet_type_counter_observer =
52 rtcp_packet_type_counter_observer;
53 configuration.paced_sender = paced_sender;
54 configuration.transport_sequence_number_allocator =
55 transport_sequence_number_allocator;
56 configuration.send_bitrate_observer = nullptr;
57 configuration.send_frame_count_observer = nullptr;
58 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070059 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070060 configuration.bandwidth_callback = nullptr;
61 configuration.transport_feedback_callback = nullptr;
Erik Språng737336d2016-07-29 12:59:36 +020062 configuration.retransmission_rate_limiter = retransmission_rate_limiter;
mflodmanc0e58a32016-04-25 01:26:26 -070063
64 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
65 rtp_rtcp->SetSendingStatus(false);
66 rtp_rtcp->SetSendingMediaStatus(false);
67 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
68
69 return rtp_rtcp;
70}
71
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000072static const int kPacketLogIntervalMs = 10000;
73
mflodmanfa666592016-04-28 23:15:33 -070074RtpStreamReceiver::RtpStreamReceiver(
75 vcm::VideoReceiver* video_receiver,
76 RemoteBitrateEstimator* remote_bitrate_estimator,
77 Transport* transport,
78 RtcpRttStats* rtt_stats,
79 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070080 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070081 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020082 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070083 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020084 ProcessThread* process_thread,
85 RateLimiter* retransmission_rate_limiter)
Tommi97888bd2016-01-21 23:24:59 +010086 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020087 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +020088 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +000089 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -070090 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070091 remb_(remb),
92 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010093 ntp_estimator_(clock_),
94 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)),
95 rtp_header_parser_(RtpHeaderParser::Create()),
96 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
97 this,
mflodmanfa666592016-04-28 23:15:33 -070098 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +010099 &rtp_payload_registry_)),
100 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
101 fec_receiver_(FecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000102 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000103 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700104 last_packet_log_ms_(-1),
105 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
106 transport,
107 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700108 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700109 remote_bitrate_estimator_,
110 paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +0200111 packet_router,
112 retransmission_rate_limiter)) {
mflodmanc0e58a32016-04-25 01:26:26 -0700113 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700114 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
115 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
116
Tommi733b5472016-06-10 17:58:01 +0200117 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700118 << "A stream should not be configured with RTCP disabled. This value is "
119 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700120 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
121 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
122 RTC_DCHECK(config_.rtp.local_ssrc != 0);
123 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
124
Tommi733b5472016-06-10 17:58:01 +0200125 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
126 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700127 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200128 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700129 rtp_rtcp_->SetREMBStatus(true);
130 remb_->AddReceiveChannel(rtp_rtcp_.get());
131 }
132
Tommi733b5472016-06-10 17:58:01 +0200133 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
134 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
135 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700136 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700137
138 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200139 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
140 ? kMaxPacketAgeToNack
141 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700142 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700143
144 // TODO(pbos): Support multiple RTX, per video payload.
145 for (const auto& kv : config_.rtp.rtx) {
146 RTC_DCHECK(kv.second.ssrc != 0);
147 RTC_DCHECK(kv.second.payload_type != 0);
148
149 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
150 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
151 kv.first);
152 }
153
154 // If set to true, the RTX payload type mapping supplied in
155 // |SetRtxPayloadType| will be used when restoring RTX packets. Without it,
156 // RTX packets will always be restored to the last non-RTX packet payload type
157 // received.
158 // TODO(holmer): When Chrome no longer depends on this being false by default,
159 // always use the mapping and remove this whole codepath.
160 rtp_payload_registry_.set_use_rtx_payload_mapping_on_restore(
161 config_.rtp.use_rtx_payload_mapping_on_restore);
162
163 if (IsFecEnabled()) {
164 VideoCodec ulpfec_codec = {};
165 ulpfec_codec.codecType = kVideoCodecULPFEC;
166 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
167 ulpfec_codec.plType = config_.rtp.fec.ulpfec_payload_type;
168 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
169
170 VideoCodec red_codec = {};
171 red_codec.codecType = kVideoCodecRED;
172 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
173 red_codec.plType = config_.rtp.fec.red_payload_type;
174 RTC_CHECK(SetReceiveCodec(red_codec));
175 if (config_.rtp.fec.red_rtx_payload_type != -1) {
176 rtp_payload_registry_.SetRtxPayloadType(
177 config_.rtp.fec.red_rtx_payload_type,
178 config_.rtp.fec.red_payload_type);
179 }
philipelae284082016-05-09 12:14:29 +0200180
181 rtp_rtcp_->SetGenericFECStatus(true,
182 config_.rtp.fec.red_payload_type,
183 config_.rtp.fec.ulpfec_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700184 }
185
Tommi733b5472016-06-10 17:58:01 +0200186 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700187 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
188
189 // Stats callback for CNAME changes.
190 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
191
mflodmandc7d0d22016-05-06 05:32:22 -0700192 process_thread_->RegisterModule(rtp_rtcp_.get());
mflodmanc0e58a32016-04-25 01:26:26 -0700193}
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
mflodmanfa666592016-04-28 23:15:33 -0700195RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700196 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
mflodmandc7d0d22016-05-06 05:32:22 -0700198 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
199 rtp_rtcp_->SetREMBStatus(false);
200 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
201 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000202}
203
mflodmanfa666592016-04-28 23:15:33 -0700204bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000205 int8_t old_pltype = -1;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100206 if (rtp_payload_registry_.ReceivePayloadType(
207 video_codec.plName, kVideoPayloadTypeFrequency, 0,
208 video_codec.maxBitrate, &old_pltype) != -1) {
209 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000210 }
211
Peter Boström4fa7eca2016-03-02 15:05:53 +0100212 return rtp_receiver_->RegisterReceivePayload(
213 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency,
214 0, 0) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000215}
216
mflodmanfa666592016-04-28 23:15:33 -0700217uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000218 return rtp_receiver_->SSRC();
219}
220
mflodmanfa666592016-04-28 23:15:33 -0700221int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000222 return rtp_receiver_->CSRCs(csrcs);
223}
224
mflodmanfa666592016-04-28 23:15:33 -0700225RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000226 return rtp_receiver_.get();
227}
228
mflodmanfa666592016-04-28 23:15:33 -0700229int32_t RtpStreamReceiver::OnReceivedPayloadData(
230 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200231 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700232 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200233 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000234 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000235 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100236 ntp_estimator_.Estimate(rtp_header->header.timestamp);
Peter Boström0b250722016-04-22 18:23:15 +0200237 if (video_receiver_->IncomingPacket(payload_data, payload_size,
238 rtp_header_with_ntp) != 0) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000239 // Check this...
240 return -1;
241 }
242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
mflodmanfa666592016-04-28 23:15:33 -0700245bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
246 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247 RTPHeader header;
248 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249 return false;
250 }
251 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000252 bool in_order = IsPacketInOrder(header);
253 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000254}
255
mflodmanfa666592016-04-28 23:15:33 -0700256// TODO(pbos): Remove as soon as audio can handle a changing payload type
257// without this callback.
258int32_t RtpStreamReceiver::OnInitializeDecoder(
259 const int8_t payload_type,
260 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
261 const int frequency,
262 const size_t channels,
263 const uint32_t rate) {
264 RTC_NOTREACHED();
265 return 0;
266}
267
268void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
269 rtp_rtcp_->SetRemoteSSRC(ssrc);
270}
271
272bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
273 size_t rtp_packet_length,
274 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100275 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000276 {
Tommi97888bd2016-01-21 23:24:59 +0100277 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000278 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100279 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000280 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000281 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000282
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000283 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000284 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000285 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100286 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000287 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000288 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000289 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000290 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000291 if (packet_time.timestamp != -1)
292 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
293 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000294 arrival_time_ms = now_ms;
295
296 {
297 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100298 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000299 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
300 std::stringstream ss;
301 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
302 << static_cast<int>(header.payloadType) << ", timestamp: "
303 << header.timestamp << ", sequence number: " << header.sequenceNumber
304 << ", arrival time: " << arrival_time_ms;
305 if (header.extension.hasTransmissionTimeOffset)
306 ss << ", toffset: " << header.extension.transmissionTimeOffset;
307 if (header.extension.hasAbsoluteSendTime)
308 ss << ", abs send time: " << header.extension.absoluteSendTime;
309 LOG(LS_INFO) << ss.str();
310 last_packet_log_ms_ = now_ms;
311 }
312 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000313
Stefan Holmerff4ea932015-06-18 16:01:33 +0200314 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700315 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000316 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000317
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000318 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100319 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100320 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000321 // Update receive statistics after ReceivePacket.
322 // Receive statistics will be reset if the payload type changes (make sure
323 // that the first packet is included in the stats).
324 rtp_receive_statistics_->IncomingPacket(
325 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
326 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000327}
328
mflodmancfc8e3b2016-05-03 21:22:04 -0700329int32_t RtpStreamReceiver::RequestKeyFrame() {
330 return rtp_rtcp_->RequestKeyFrame();
331}
332
333int32_t RtpStreamReceiver::SliceLossIndicationRequest(
334 const uint64_t picture_id) {
335 return rtp_rtcp_->SendRTCPSliceLossIndication(
336 static_cast<uint8_t>(picture_id));
337}
338
mflodmandc7d0d22016-05-06 05:32:22 -0700339bool RtpStreamReceiver::IsFecEnabled() const {
340 return config_.rtp.fec.red_payload_type != -1 &&
341 config_.rtp.fec.ulpfec_payload_type != -1;
342}
343
344bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
345 return config_.rtp.nack.rtp_history_ms > 0;
346}
347
348void RtpStreamReceiver::RequestPacketRetransmit(
349 const std::vector<uint16_t>& sequence_numbers) {
350 rtp_rtcp_->SendNack(sequence_numbers);
351}
352
mflodmancfc8e3b2016-05-03 21:22:04 -0700353int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
354 uint16_t length) {
355 return rtp_rtcp_->SendNACK(sequence_numbers, length);
356}
357
mflodmanfa666592016-04-28 23:15:33 -0700358bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
359 size_t packet_length,
360 const RTPHeader& header,
361 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100362 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000363 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
364 }
365 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000366 assert(packet_length >= header.headerLength);
367 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000368 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100369 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
370 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000371 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000372 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000373 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
374 payload_specific, in_order);
375}
376
mflodmanfa666592016-04-28 23:15:33 -0700377bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
378 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100379 if (rtp_payload_registry_.IsRed(header)) {
380 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000381 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000382 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200383 // Notify video_receiver about received FEC packets to avoid NACKing these
384 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000385 NotifyReceiverOfFecPacket(header);
386 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000387 if (fec_receiver_->AddReceivedRedPacket(
sprang@webrtc.org0e932572014-01-23 10:00:39 +0000388 header, packet, packet_length, ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000389 return false;
390 }
391 return fec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100392 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000393 if (header.headerLength + header.paddingLength == packet_length) {
394 // This is an empty packet and should be silently dropped before trying to
395 // parse the RTX header.
396 return true;
397 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000398 // Remove the RTX header and parse the original RTP header.
399 if (packet_length < header.headerLength)
400 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000401 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000402 return false;
Tommi97888bd2016-01-21 23:24:59 +0100403 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000404 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000405 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000406 return false;
407 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100408 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700409 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
410 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100411 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
412 << header.ssrc << " payload type: "
413 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000414 return false;
415 }
416 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700417 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000418 restored_packet_in_use_ = false;
419 return ret;
420 }
421 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422}
423
mflodmanfa666592016-04-28 23:15:33 -0700424void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000425 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100426 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000427 if (last_media_payload_type < 0) {
428 LOG(LS_WARNING) << "Failed to get last media payload type.";
429 return;
430 }
431 // Fake an empty media packet.
432 WebRtcRTPHeader rtp_header = {};
433 rtp_header.header = header;
434 rtp_header.header.payloadType = last_media_payload_type;
435 rtp_header.header.paddingLength = 0;
436 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100437 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
438 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000439 LOG(LS_WARNING) << "Failed to get payload specifics.";
440 return;
441 }
442 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000443 rtp_header.type.Video.rotation = kVideoRotation_0;
444 if (header.extension.hasVideoRotation) {
445 rtp_header.type.Video.rotation =
446 ConvertCVOByteToVideoRotation(header.extension.videoRotation);
447 }
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