blob: 2f27d8dc9cc6d2440089e34eb175c26ca8c62dc7 [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>
philipelfd5a20f2016-11-15 00:57:57 -080014#include <utility>
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000015
mflodman4cd27902016-08-05 06:28:45 -070016#include "webrtc/base/checks.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010017#include "webrtc/base/logging.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070018#include "webrtc/common_types.h"
Peter Boström9c017252016-02-26 16:26:20 +010019#include "webrtc/config.h"
mflodmanc0e58a32016-04-25 01:26:26 -070020#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000021#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010022#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"
brandtrd55c3f62016-10-31 04:51:33 -070027#include "webrtc/modules/rtp_rtcp/include/ulpfec_receiver.h"
philipelfd5a20f2016-11-15 00:57:57 -080028#include "webrtc/modules/video_coding/frame_object.h"
29#include "webrtc/modules/video_coding/h264_sps_pps_tracker.h"
30#include "webrtc/modules/video_coding/packet_buffer.h"
Peter Boström0b250722016-04-22 18:23:15 +020031#include "webrtc/modules/video_coding/video_coding_impl.h"
philipelfd5a20f2016-11-15 00:57:57 -080032#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010033#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010034#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
35#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070036#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070037#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000038
39namespace webrtc {
40
philipelfd5a20f2016-11-15 00:57:57 -080041namespace {
42constexpr int kPacketBufferStartSize = 32;
43constexpr int kPacketBufferMaxSixe = 2048;
44}
45
mflodmanc0e58a32016-04-25 01:26:26 -070046std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
47 ReceiveStatistics* receive_statistics,
48 Transport* outgoing_transport,
49 RtcpRttStats* rtt_stats,
50 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
51 RemoteBitrateEstimator* remote_bitrate_estimator,
52 RtpPacketSender* paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +020053 TransportSequenceNumberAllocator* transport_sequence_number_allocator,
54 RateLimiter* retransmission_rate_limiter) {
mflodmanc0e58a32016-04-25 01:26:26 -070055 RtpRtcp::Configuration configuration;
56 configuration.audio = false;
57 configuration.receiver_only = true;
58 configuration.receive_statistics = receive_statistics;
59 configuration.outgoing_transport = outgoing_transport;
60 configuration.intra_frame_callback = nullptr;
61 configuration.rtt_stats = rtt_stats;
62 configuration.rtcp_packet_type_counter_observer =
63 rtcp_packet_type_counter_observer;
64 configuration.paced_sender = paced_sender;
65 configuration.transport_sequence_number_allocator =
66 transport_sequence_number_allocator;
67 configuration.send_bitrate_observer = nullptr;
68 configuration.send_frame_count_observer = nullptr;
69 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070070 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070071 configuration.bandwidth_callback = nullptr;
72 configuration.transport_feedback_callback = nullptr;
Erik Språng737336d2016-07-29 12:59:36 +020073 configuration.retransmission_rate_limiter = retransmission_rate_limiter;
mflodmanc0e58a32016-04-25 01:26:26 -070074
75 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
76 rtp_rtcp->SetSendingStatus(false);
77 rtp_rtcp->SetSendingMediaStatus(false);
78 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
79
80 return rtp_rtcp;
81}
82
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000083static const int kPacketLogIntervalMs = 10000;
84
mflodmanfa666592016-04-28 23:15:33 -070085RtpStreamReceiver::RtpStreamReceiver(
86 vcm::VideoReceiver* video_receiver,
87 RemoteBitrateEstimator* remote_bitrate_estimator,
88 Transport* transport,
89 RtcpRttStats* rtt_stats,
90 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070091 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070092 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020093 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070094 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020095 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080096 RateLimiter* retransmission_rate_limiter,
97 NackSender* nack_sender,
98 KeyFrameRequestSender* keyframe_request_sender,
99 video_coding::OnCompleteFrameCallback* complete_frame_callback,
100 VCMTiming* timing)
Tommi97888bd2016-01-21 23:24:59 +0100101 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +0200102 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +0200103 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000104 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -0700105 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700106 remb_(remb),
107 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100108 ntp_estimator_(clock_),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100109 rtp_header_parser_(RtpHeaderParser::Create()),
110 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
111 this,
mflodmanfa666592016-04-28 23:15:33 -0700112 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100113 &rtp_payload_registry_)),
114 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtrd55c3f62016-10-31 04:51:33 -0700115 ulpfec_receiver_(UlpfecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000116 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000117 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700118 last_packet_log_ms_(-1),
119 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
120 transport,
121 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700122 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700123 remote_bitrate_estimator_,
124 paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +0200125 packet_router,
philipelfd5a20f2016-11-15 00:57:57 -0800126 retransmission_rate_limiter)),
127 complete_frame_callback_(complete_frame_callback),
128 keyframe_request_sender_(keyframe_request_sender),
129 timing_(timing) {
mflodmanc0e58a32016-04-25 01:26:26 -0700130 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700131 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
132 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
133
Tommi733b5472016-06-10 17:58:01 +0200134 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700135 << "A stream should not be configured with RTCP disabled. This value is "
136 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700137 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
138 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
139 RTC_DCHECK(config_.rtp.local_ssrc != 0);
140 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
141
Tommi733b5472016-06-10 17:58:01 +0200142 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
143 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700144 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200145 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700146 rtp_rtcp_->SetREMBStatus(true);
147 remb_->AddReceiveChannel(rtp_rtcp_.get());
148 }
149
Tommi733b5472016-06-10 17:58:01 +0200150 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
151 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
152 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700153 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700154
155 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200156 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
157 ? kMaxPacketAgeToNack
158 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700159 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700160
161 // TODO(pbos): Support multiple RTX, per video payload.
162 for (const auto& kv : config_.rtp.rtx) {
163 RTC_DCHECK(kv.second.ssrc != 0);
164 RTC_DCHECK(kv.second.payload_type != 0);
165
166 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
167 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
168 kv.first);
169 }
170
mflodmandc7d0d22016-05-06 05:32:22 -0700171 if (IsFecEnabled()) {
172 VideoCodec ulpfec_codec = {};
173 ulpfec_codec.codecType = kVideoCodecULPFEC;
174 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700175 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
mflodmandc7d0d22016-05-06 05:32:22 -0700176 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
brandtre6f98c72016-11-11 03:28:30 -0800177 }
mflodmandc7d0d22016-05-06 05:32:22 -0700178
brandtre6f98c72016-11-11 03:28:30 -0800179 if (IsRedEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700180 VideoCodec red_codec = {};
181 red_codec.codecType = kVideoCodecRED;
182 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700183 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
mflodmandc7d0d22016-05-06 05:32:22 -0700184 RTC_CHECK(SetReceiveCodec(red_codec));
brandtrb5f2c3f2016-10-04 23:28:39 -0700185 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
mflodmandc7d0d22016-05-06 05:32:22 -0700186 rtp_payload_registry_.SetRtxPayloadType(
brandtrb5f2c3f2016-10-04 23:28:39 -0700187 config_.rtp.ulpfec.red_rtx_payload_type,
188 config_.rtp.ulpfec.red_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700189 }
190 }
191
Tommi733b5472016-06-10 17:58:01 +0200192 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700193 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
194
195 // Stats callback for CNAME changes.
196 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
197
mflodmandc7d0d22016-05-06 05:32:22 -0700198 process_thread_->RegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800199
200 jitter_buffer_experiment_ =
201 field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") == "Enabled";
202
203 if (jitter_buffer_experiment_) {
204 nack_module_.reset(
205 new NackModule(clock_, nack_sender, keyframe_request_sender));
206 process_thread_->RegisterModule(nack_module_.get());
207
208 packet_buffer_ = video_coding::PacketBuffer::Create(
209 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
210 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
211 }
mflodmanc0e58a32016-04-25 01:26:26 -0700212}
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
mflodmanfa666592016-04-28 23:15:33 -0700214RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700215 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000216
philipelfd5a20f2016-11-15 00:57:57 -0800217 if (jitter_buffer_experiment_)
218 process_thread_->DeRegisterModule(nack_module_.get());
219
mflodmandc7d0d22016-05-06 05:32:22 -0700220 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
221 rtp_rtcp_->SetREMBStatus(false);
222 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
223 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000224}
225
mflodmanfa666592016-04-28 23:15:33 -0700226bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000227 int8_t old_pltype = -1;
magjed56124bd2016-11-24 09:34:46 -0800228 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
229 -1) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100230 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000231 }
232
magjed56124bd2016-11-24 09:34:46 -0800233 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000234}
235
mflodmanfa666592016-04-28 23:15:33 -0700236uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000237 return rtp_receiver_->SSRC();
238}
239
mflodmanfa666592016-04-28 23:15:33 -0700240int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000241 return rtp_receiver_->CSRCs(csrcs);
242}
243
mflodmanfa666592016-04-28 23:15:33 -0700244RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245 return rtp_receiver_.get();
246}
247
mflodmanfa666592016-04-28 23:15:33 -0700248int32_t RtpStreamReceiver::OnReceivedPayloadData(
249 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200250 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700251 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200252 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000253 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000254 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100255 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipelfd5a20f2016-11-15 00:57:57 -0800256 if (jitter_buffer_experiment_) {
257 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
258 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds());
259 packet.timesNacked = nack_module_->OnReceivedPacket(packet);
260
261 if (packet.codec == kVideoCodecH264) {
262 switch (tracker_.CopyAndFixBitstream(&packet)) {
263 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
264 keyframe_request_sender_->RequestKeyFrame();
265 FALLTHROUGH();
266 case video_coding::H264SpsPpsTracker::kDrop:
267 return 0;
268 case video_coding::H264SpsPpsTracker::kInsert:
269 break;
270 }
271 } else {
272 uint8_t* data = new uint8_t[packet.sizeBytes];
273 memcpy(data, packet.dataPtr, packet.sizeBytes);
274 packet.dataPtr = data;
275 }
276
philipel759e0b72016-11-30 01:32:05 -0800277 packet_buffer_->InsertPacket(&packet);
philipelfd5a20f2016-11-15 00:57:57 -0800278 } else {
279 if (video_receiver_->IncomingPacket(payload_data, payload_size,
280 rtp_header_with_ntp) != 0) {
281 // Check this...
282 return -1;
283 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000284 }
285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286}
287
mflodmanfa666592016-04-28 23:15:33 -0700288bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
289 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000290 RTPHeader header;
291 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 return false;
293 }
294 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000295 bool in_order = IsPacketInOrder(header);
296 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000297}
298
mflodmanfa666592016-04-28 23:15:33 -0700299// TODO(pbos): Remove as soon as audio can handle a changing payload type
300// without this callback.
301int32_t RtpStreamReceiver::OnInitializeDecoder(
302 const int8_t payload_type,
303 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
304 const int frequency,
305 const size_t channels,
306 const uint32_t rate) {
307 RTC_NOTREACHED();
308 return 0;
309}
310
311void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
312 rtp_rtcp_->SetRemoteSSRC(ssrc);
313}
314
315bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
316 size_t rtp_packet_length,
317 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100318 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000319 {
Tommi97888bd2016-01-21 23:24:59 +0100320 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000321 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100322 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000323 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000324 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000325
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000326 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000327 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000328 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100329 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000330 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000331 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000332 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000333 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000334 if (packet_time.timestamp != -1)
335 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
336 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000337 arrival_time_ms = now_ms;
338
339 {
340 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100341 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000342 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
343 std::stringstream ss;
344 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
345 << static_cast<int>(header.payloadType) << ", timestamp: "
346 << header.timestamp << ", sequence number: " << header.sequenceNumber
347 << ", arrival time: " << arrival_time_ms;
348 if (header.extension.hasTransmissionTimeOffset)
349 ss << ", toffset: " << header.extension.transmissionTimeOffset;
350 if (header.extension.hasAbsoluteSendTime)
351 ss << ", abs send time: " << header.extension.absoluteSendTime;
352 LOG(LS_INFO) << ss.str();
353 last_packet_log_ms_ = now_ms;
354 }
355 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000356
Stefan Holmerff4ea932015-06-18 16:01:33 +0200357 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700358 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000359 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000360
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000361 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100362 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100363 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000364 // Update receive statistics after ReceivePacket.
365 // Receive statistics will be reset if the payload type changes (make sure
366 // that the first packet is included in the stats).
367 rtp_receive_statistics_->IncomingPacket(
368 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
369 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000370}
371
mflodmancfc8e3b2016-05-03 21:22:04 -0700372int32_t RtpStreamReceiver::RequestKeyFrame() {
373 return rtp_rtcp_->RequestKeyFrame();
374}
375
376int32_t RtpStreamReceiver::SliceLossIndicationRequest(
377 const uint64_t picture_id) {
378 return rtp_rtcp_->SendRTCPSliceLossIndication(
379 static_cast<uint8_t>(picture_id));
380}
381
mflodmandc7d0d22016-05-06 05:32:22 -0700382bool RtpStreamReceiver::IsFecEnabled() const {
brandtre6f98c72016-11-11 03:28:30 -0800383 return config_.rtp.ulpfec.ulpfec_payload_type != -1;
384}
385
386bool RtpStreamReceiver::IsRedEnabled() const {
387 return config_.rtp.ulpfec.red_payload_type != -1;
mflodmandc7d0d22016-05-06 05:32:22 -0700388}
389
390bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
391 return config_.rtp.nack.rtp_history_ms > 0;
392}
393
394void RtpStreamReceiver::RequestPacketRetransmit(
395 const std::vector<uint16_t>& sequence_numbers) {
396 rtp_rtcp_->SendNack(sequence_numbers);
397}
398
mflodmancfc8e3b2016-05-03 21:22:04 -0700399int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
400 uint16_t length) {
401 return rtp_rtcp_->SendNACK(sequence_numbers, length);
402}
403
philipelfd5a20f2016-11-15 00:57:57 -0800404void RtpStreamReceiver::OnReceivedFrame(
405 std::unique_ptr<video_coding::RtpFrameObject> frame) {
406 reference_finder_->ManageFrame(std::move(frame));
407}
408
409void RtpStreamReceiver::OnCompleteFrame(
410 std::unique_ptr<video_coding::FrameObject> frame) {
411 {
412 rtc::CritScope lock(&last_seq_num_cs_);
413 video_coding::RtpFrameObject* rtp_frame =
414 static_cast<video_coding::RtpFrameObject*>(frame.get());
415 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
416 }
417 complete_frame_callback_->OnCompleteFrame(std::move(frame));
418}
419
420void RtpStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
421 if (jitter_buffer_experiment_)
422 nack_module_->UpdateRtt(max_rtt_ms);
423}
424
mflodmanfa666592016-04-28 23:15:33 -0700425bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
426 size_t packet_length,
427 const RTPHeader& header,
428 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100429 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000430 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
431 }
432 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000433 assert(packet_length >= header.headerLength);
434 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000435 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100436 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
437 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000438 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000439 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000440 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
441 payload_specific, in_order);
442}
443
mflodmanfa666592016-04-28 23:15:33 -0700444bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
445 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100446 if (rtp_payload_registry_.IsRed(header)) {
447 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000448 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000449 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200450 // Notify video_receiver about received FEC packets to avoid NACKing these
451 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000452 NotifyReceiverOfFecPacket(header);
453 }
brandtrd55c3f62016-10-31 04:51:33 -0700454 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
455 ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000456 return false;
457 }
brandtrd55c3f62016-10-31 04:51:33 -0700458 return ulpfec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100459 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000460 if (header.headerLength + header.paddingLength == packet_length) {
461 // This is an empty packet and should be silently dropped before trying to
462 // parse the RTX header.
463 return true;
464 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000465 // Remove the RTX header and parse the original RTP header.
466 if (packet_length < header.headerLength)
467 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000468 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000469 return false;
Tommi97888bd2016-01-21 23:24:59 +0100470 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000471 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000472 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000473 return false;
474 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100475 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700476 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
477 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100478 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
479 << header.ssrc << " payload type: "
480 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000481 return false;
482 }
483 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700484 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000485 restored_packet_in_use_ = false;
486 return ret;
487 }
488 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000489}
490
mflodmanfa666592016-04-28 23:15:33 -0700491void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000492 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100493 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000494 if (last_media_payload_type < 0) {
495 LOG(LS_WARNING) << "Failed to get last media payload type.";
496 return;
497 }
498 // Fake an empty media packet.
499 WebRtcRTPHeader rtp_header = {};
500 rtp_header.header = header;
501 rtp_header.header.payloadType = last_media_payload_type;
502 rtp_header.header.paddingLength = 0;
503 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100504 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
505 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000506 LOG(LS_WARNING) << "Failed to get payload specifics.";
507 return;
508 }
509 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000510 rtp_header.type.Video.rotation = kVideoRotation_0;
511 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700512 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000513 }
isheriff6b4b5f32016-06-08 00:24:21 -0700514 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
515
Peter Boström74f6e9e2016-04-04 17:56:10 +0200516 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000517}
518
mflodmanfa666592016-04-28 23:15:33 -0700519bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
520 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000521 {
Tommi97888bd2016-01-21 23:24:59 +0100522 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000523 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100524 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000525 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100526 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
Per83d09102016-04-15 14:59:13 +0200528 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000529
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000530 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200531 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000532 if (rtt == 0) {
533 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100534 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000535 }
536 uint32_t ntp_secs = 0;
537 uint32_t ntp_frac = 0;
538 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200539 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
540 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000541 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100542 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000543 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100544 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000545
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100546 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000547}
548
philipelfd5a20f2016-11-15 00:57:57 -0800549void RtpStreamReceiver::FrameContinuous(uint16_t picture_id) {
550 if (jitter_buffer_experiment_) {
551 int seq_num = -1;
552 {
553 rtc::CritScope lock(&last_seq_num_cs_);
554 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
555 if (seq_num_it != last_seq_num_for_pic_id_.end())
556 seq_num = seq_num_it->second;
557 }
558 if (seq_num != -1)
559 nack_module_->ClearUpTo(seq_num);
560 }
561}
562
563void RtpStreamReceiver::FrameDecoded(uint16_t picture_id) {
564 if (jitter_buffer_experiment_) {
565 int seq_num = -1;
566 {
567 rtc::CritScope lock(&last_seq_num_cs_);
568 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
569 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
570 seq_num = seq_num_it->second;
571 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
572 ++seq_num_it);
573 }
574 }
575 if (seq_num != -1) {
576 packet_buffer_->ClearTo(seq_num);
577 reference_finder_->ClearTo(seq_num);
578 }
579 }
580}
581
mflodmandc7d0d22016-05-06 05:32:22 -0700582void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
583 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
584 : RtcpMode::kOff);
585}
586
mflodmanfa666592016-04-28 23:15:33 -0700587void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100588 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000589 receiving_ = true;
590}
591
mflodmanfa666592016-04-28 23:15:33 -0700592void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100593 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000594 receiving_ = false;
595}
596
mflodmanfa666592016-04-28 23:15:33 -0700597bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000598 StreamStatistician* statistician =
599 rtp_receive_statistics_->GetStatistician(header.ssrc);
600 if (!statistician)
601 return false;
602 return statistician->IsPacketInOrder(header.sequenceNumber);
603}
604
mflodmanfa666592016-04-28 23:15:33 -0700605bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
606 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000607 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100608 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000609 return false;
610 StreamStatistician* statistician =
611 rtp_receive_statistics_->GetStatistician(header.ssrc);
612 if (!statistician)
613 return false;
614 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000615 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200616 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000617 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000618 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000619}
mflodmandc7d0d22016-05-06 05:32:22 -0700620
621void RtpStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700622 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
mflodmandc7d0d22016-05-06 05:32:22 -0700623 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700624 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700625 "WebRTC.Video.ReceivedFecPacketsInPercent",
626 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
627 }
628 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700629 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
630 static_cast<int>(counter.num_recovered_packets *
631 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700632 }
633}
634
635void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
636 const std::string& extension, int id) {
637 // One-byte-extension local identifiers are in the range 1-14 inclusive.
638 RTC_DCHECK_GE(id, 1);
639 RTC_DCHECK_LE(id, 14);
640 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
641 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
642 StringToRtpExtensionType(extension), id));
643}
644
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000645} // namespace webrtc