blob: fc8400761086e4851cfdcda9981f23d0ab46f5ec [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_),
109 rtp_payload_registry_(RTPPayloadStrategy::CreateStrategy(false)),
110 rtp_header_parser_(RtpHeaderParser::Create()),
111 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
112 this,
mflodmanfa666592016-04-28 23:15:33 -0700113 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100114 &rtp_payload_registry_)),
115 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtrd55c3f62016-10-31 04:51:33 -0700116 ulpfec_receiver_(UlpfecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000117 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000118 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700119 last_packet_log_ms_(-1),
120 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
121 transport,
122 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700123 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700124 remote_bitrate_estimator_,
125 paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +0200126 packet_router,
philipelfd5a20f2016-11-15 00:57:57 -0800127 retransmission_rate_limiter)),
128 complete_frame_callback_(complete_frame_callback),
129 keyframe_request_sender_(keyframe_request_sender),
130 timing_(timing) {
mflodmanc0e58a32016-04-25 01:26:26 -0700131 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700132 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
133 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
134
Tommi733b5472016-06-10 17:58:01 +0200135 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700136 << "A stream should not be configured with RTCP disabled. This value is "
137 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700138 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
139 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
140 RTC_DCHECK(config_.rtp.local_ssrc != 0);
141 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
142
Tommi733b5472016-06-10 17:58:01 +0200143 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
144 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700145 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200146 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700147 rtp_rtcp_->SetREMBStatus(true);
148 remb_->AddReceiveChannel(rtp_rtcp_.get());
149 }
150
Tommi733b5472016-06-10 17:58:01 +0200151 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
152 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
153 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700154 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700155
156 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200157 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
158 ? kMaxPacketAgeToNack
159 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700160 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700161
162 // TODO(pbos): Support multiple RTX, per video payload.
163 for (const auto& kv : config_.rtp.rtx) {
164 RTC_DCHECK(kv.second.ssrc != 0);
165 RTC_DCHECK(kv.second.payload_type != 0);
166
167 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
168 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
169 kv.first);
170 }
171
mflodmandc7d0d22016-05-06 05:32:22 -0700172 if (IsFecEnabled()) {
173 VideoCodec ulpfec_codec = {};
174 ulpfec_codec.codecType = kVideoCodecULPFEC;
175 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700176 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
mflodmandc7d0d22016-05-06 05:32:22 -0700177 RTC_CHECK(SetReceiveCodec(ulpfec_codec));
brandtre6f98c72016-11-11 03:28:30 -0800178 }
mflodmandc7d0d22016-05-06 05:32:22 -0700179
brandtre6f98c72016-11-11 03:28:30 -0800180 if (IsRedEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700181 VideoCodec red_codec = {};
182 red_codec.codecType = kVideoCodecRED;
183 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700184 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
mflodmandc7d0d22016-05-06 05:32:22 -0700185 RTC_CHECK(SetReceiveCodec(red_codec));
brandtrb5f2c3f2016-10-04 23:28:39 -0700186 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
mflodmandc7d0d22016-05-06 05:32:22 -0700187 rtp_payload_registry_.SetRtxPayloadType(
brandtrb5f2c3f2016-10-04 23:28:39 -0700188 config_.rtp.ulpfec.red_rtx_payload_type,
189 config_.rtp.ulpfec.red_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700190 }
191 }
192
Tommi733b5472016-06-10 17:58:01 +0200193 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700194 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
195
196 // Stats callback for CNAME changes.
197 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
198
mflodmandc7d0d22016-05-06 05:32:22 -0700199 process_thread_->RegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800200
201 jitter_buffer_experiment_ =
202 field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") == "Enabled";
203
204 if (jitter_buffer_experiment_) {
205 nack_module_.reset(
206 new NackModule(clock_, nack_sender, keyframe_request_sender));
207 process_thread_->RegisterModule(nack_module_.get());
208
209 packet_buffer_ = video_coding::PacketBuffer::Create(
210 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
211 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
212 }
mflodmanc0e58a32016-04-25 01:26:26 -0700213}
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
mflodmanfa666592016-04-28 23:15:33 -0700215RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700216 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000217
philipelfd5a20f2016-11-15 00:57:57 -0800218 if (jitter_buffer_experiment_)
219 process_thread_->DeRegisterModule(nack_module_.get());
220
mflodmandc7d0d22016-05-06 05:32:22 -0700221 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
222 rtp_rtcp_->SetREMBStatus(false);
223 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
224 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000225}
226
mflodmanfa666592016-04-28 23:15:33 -0700227bool RtpStreamReceiver::SetReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000228 int8_t old_pltype = -1;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100229 if (rtp_payload_registry_.ReceivePayloadType(
230 video_codec.plName, kVideoPayloadTypeFrequency, 0,
231 video_codec.maxBitrate, &old_pltype) != -1) {
232 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000233 }
234
Peter Boström4fa7eca2016-03-02 15:05:53 +0100235 return rtp_receiver_->RegisterReceivePayload(
236 video_codec.plName, video_codec.plType, kVideoPayloadTypeFrequency,
237 0, 0) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000238}
239
mflodmanfa666592016-04-28 23:15:33 -0700240uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000241 return rtp_receiver_->SSRC();
242}
243
mflodmanfa666592016-04-28 23:15:33 -0700244int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245 return rtp_receiver_->CSRCs(csrcs);
246}
247
mflodmanfa666592016-04-28 23:15:33 -0700248RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249 return rtp_receiver_.get();
250}
251
mflodmanfa666592016-04-28 23:15:33 -0700252int32_t RtpStreamReceiver::OnReceivedPayloadData(
253 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200254 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700255 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200256 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000257 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000258 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100259 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipelfd5a20f2016-11-15 00:57:57 -0800260 if (jitter_buffer_experiment_) {
261 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
262 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds());
263 packet.timesNacked = nack_module_->OnReceivedPacket(packet);
264
265 if (packet.codec == kVideoCodecH264) {
266 switch (tracker_.CopyAndFixBitstream(&packet)) {
267 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
268 keyframe_request_sender_->RequestKeyFrame();
269 FALLTHROUGH();
270 case video_coding::H264SpsPpsTracker::kDrop:
271 return 0;
272 case video_coding::H264SpsPpsTracker::kInsert:
273 break;
274 }
275 } else {
276 uint8_t* data = new uint8_t[packet.sizeBytes];
277 memcpy(data, packet.dataPtr, packet.sizeBytes);
278 packet.dataPtr = data;
279 }
280
281 packet_buffer_->InsertPacket(packet);
282 } else {
283 if (video_receiver_->IncomingPacket(payload_data, payload_size,
284 rtp_header_with_ntp) != 0) {
285 // Check this...
286 return -1;
287 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000288 }
289 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290}
291
mflodmanfa666592016-04-28 23:15:33 -0700292bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
293 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000294 RTPHeader header;
295 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 return false;
297 }
298 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000299 bool in_order = IsPacketInOrder(header);
300 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000301}
302
mflodmanfa666592016-04-28 23:15:33 -0700303// TODO(pbos): Remove as soon as audio can handle a changing payload type
304// without this callback.
305int32_t RtpStreamReceiver::OnInitializeDecoder(
306 const int8_t payload_type,
307 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
308 const int frequency,
309 const size_t channels,
310 const uint32_t rate) {
311 RTC_NOTREACHED();
312 return 0;
313}
314
315void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
316 rtp_rtcp_->SetRemoteSSRC(ssrc);
317}
318
319bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
320 size_t rtp_packet_length,
321 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100322 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000323 {
Tommi97888bd2016-01-21 23:24:59 +0100324 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000325 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100326 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000327 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000328 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000329
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000330 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000331 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000332 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100333 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000334 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000335 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000336 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000337 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000338 if (packet_time.timestamp != -1)
339 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
340 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000341 arrival_time_ms = now_ms;
342
343 {
344 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100345 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000346 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
347 std::stringstream ss;
348 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
349 << static_cast<int>(header.payloadType) << ", timestamp: "
350 << header.timestamp << ", sequence number: " << header.sequenceNumber
351 << ", arrival time: " << arrival_time_ms;
352 if (header.extension.hasTransmissionTimeOffset)
353 ss << ", toffset: " << header.extension.transmissionTimeOffset;
354 if (header.extension.hasAbsoluteSendTime)
355 ss << ", abs send time: " << header.extension.absoluteSendTime;
356 LOG(LS_INFO) << ss.str();
357 last_packet_log_ms_ = now_ms;
358 }
359 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000360
Stefan Holmerff4ea932015-06-18 16:01:33 +0200361 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700362 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000363 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000364
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000365 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100366 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100367 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000368 // Update receive statistics after ReceivePacket.
369 // Receive statistics will be reset if the payload type changes (make sure
370 // that the first packet is included in the stats).
371 rtp_receive_statistics_->IncomingPacket(
372 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
373 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000374}
375
mflodmancfc8e3b2016-05-03 21:22:04 -0700376int32_t RtpStreamReceiver::RequestKeyFrame() {
377 return rtp_rtcp_->RequestKeyFrame();
378}
379
380int32_t RtpStreamReceiver::SliceLossIndicationRequest(
381 const uint64_t picture_id) {
382 return rtp_rtcp_->SendRTCPSliceLossIndication(
383 static_cast<uint8_t>(picture_id));
384}
385
mflodmandc7d0d22016-05-06 05:32:22 -0700386bool RtpStreamReceiver::IsFecEnabled() const {
brandtre6f98c72016-11-11 03:28:30 -0800387 return config_.rtp.ulpfec.ulpfec_payload_type != -1;
388}
389
390bool RtpStreamReceiver::IsRedEnabled() const {
391 return config_.rtp.ulpfec.red_payload_type != -1;
mflodmandc7d0d22016-05-06 05:32:22 -0700392}
393
394bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
395 return config_.rtp.nack.rtp_history_ms > 0;
396}
397
398void RtpStreamReceiver::RequestPacketRetransmit(
399 const std::vector<uint16_t>& sequence_numbers) {
400 rtp_rtcp_->SendNack(sequence_numbers);
401}
402
mflodmancfc8e3b2016-05-03 21:22:04 -0700403int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
404 uint16_t length) {
405 return rtp_rtcp_->SendNACK(sequence_numbers, length);
406}
407
philipelfd5a20f2016-11-15 00:57:57 -0800408void RtpStreamReceiver::OnReceivedFrame(
409 std::unique_ptr<video_coding::RtpFrameObject> frame) {
410 reference_finder_->ManageFrame(std::move(frame));
411}
412
413void RtpStreamReceiver::OnCompleteFrame(
414 std::unique_ptr<video_coding::FrameObject> frame) {
415 {
416 rtc::CritScope lock(&last_seq_num_cs_);
417 video_coding::RtpFrameObject* rtp_frame =
418 static_cast<video_coding::RtpFrameObject*>(frame.get());
419 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
420 }
421 complete_frame_callback_->OnCompleteFrame(std::move(frame));
422}
423
424void RtpStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
425 if (jitter_buffer_experiment_)
426 nack_module_->UpdateRtt(max_rtt_ms);
427}
428
mflodmanfa666592016-04-28 23:15:33 -0700429bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
430 size_t packet_length,
431 const RTPHeader& header,
432 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100433 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000434 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
435 }
436 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000437 assert(packet_length >= header.headerLength);
438 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000439 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100440 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
441 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000442 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000443 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000444 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
445 payload_specific, in_order);
446}
447
mflodmanfa666592016-04-28 23:15:33 -0700448bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
449 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100450 if (rtp_payload_registry_.IsRed(header)) {
451 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000452 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000453 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200454 // Notify video_receiver about received FEC packets to avoid NACKing these
455 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000456 NotifyReceiverOfFecPacket(header);
457 }
brandtrd55c3f62016-10-31 04:51:33 -0700458 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
459 ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000460 return false;
461 }
brandtrd55c3f62016-10-31 04:51:33 -0700462 return ulpfec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100463 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000464 if (header.headerLength + header.paddingLength == packet_length) {
465 // This is an empty packet and should be silently dropped before trying to
466 // parse the RTX header.
467 return true;
468 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000469 // Remove the RTX header and parse the original RTP header.
470 if (packet_length < header.headerLength)
471 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000472 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000473 return false;
Tommi97888bd2016-01-21 23:24:59 +0100474 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000475 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000476 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000477 return false;
478 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100479 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700480 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
481 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100482 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
483 << header.ssrc << " payload type: "
484 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000485 return false;
486 }
487 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700488 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000489 restored_packet_in_use_ = false;
490 return ret;
491 }
492 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000493}
494
mflodmanfa666592016-04-28 23:15:33 -0700495void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000496 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100497 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000498 if (last_media_payload_type < 0) {
499 LOG(LS_WARNING) << "Failed to get last media payload type.";
500 return;
501 }
502 // Fake an empty media packet.
503 WebRtcRTPHeader rtp_header = {};
504 rtp_header.header = header;
505 rtp_header.header.payloadType = last_media_payload_type;
506 rtp_header.header.paddingLength = 0;
507 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100508 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
509 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000510 LOG(LS_WARNING) << "Failed to get payload specifics.";
511 return;
512 }
513 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000514 rtp_header.type.Video.rotation = kVideoRotation_0;
515 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700516 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000517 }
isheriff6b4b5f32016-06-08 00:24:21 -0700518 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
519
Peter Boström74f6e9e2016-04-04 17:56:10 +0200520 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000521}
522
mflodmanfa666592016-04-28 23:15:33 -0700523bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
524 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000525 {
Tommi97888bd2016-01-21 23:24:59 +0100526 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000527 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100528 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000529 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100530 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
Per83d09102016-04-15 14:59:13 +0200532 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000533
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000534 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200535 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000536 if (rtt == 0) {
537 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100538 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000539 }
540 uint32_t ntp_secs = 0;
541 uint32_t ntp_frac = 0;
542 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200543 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
544 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000545 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100546 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000547 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100548 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000549
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100550 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000551}
552
philipelfd5a20f2016-11-15 00:57:57 -0800553void RtpStreamReceiver::FrameContinuous(uint16_t picture_id) {
554 if (jitter_buffer_experiment_) {
555 int seq_num = -1;
556 {
557 rtc::CritScope lock(&last_seq_num_cs_);
558 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
559 if (seq_num_it != last_seq_num_for_pic_id_.end())
560 seq_num = seq_num_it->second;
561 }
562 if (seq_num != -1)
563 nack_module_->ClearUpTo(seq_num);
564 }
565}
566
567void RtpStreamReceiver::FrameDecoded(uint16_t picture_id) {
568 if (jitter_buffer_experiment_) {
569 int seq_num = -1;
570 {
571 rtc::CritScope lock(&last_seq_num_cs_);
572 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
573 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
574 seq_num = seq_num_it->second;
575 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
576 ++seq_num_it);
577 }
578 }
579 if (seq_num != -1) {
580 packet_buffer_->ClearTo(seq_num);
581 reference_finder_->ClearTo(seq_num);
582 }
583 }
584}
585
mflodmandc7d0d22016-05-06 05:32:22 -0700586void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
587 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
588 : RtcpMode::kOff);
589}
590
mflodmanfa666592016-04-28 23:15:33 -0700591void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100592 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000593 receiving_ = true;
594}
595
mflodmanfa666592016-04-28 23:15:33 -0700596void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100597 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000598 receiving_ = false;
599}
600
mflodmanfa666592016-04-28 23:15:33 -0700601bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000602 StreamStatistician* statistician =
603 rtp_receive_statistics_->GetStatistician(header.ssrc);
604 if (!statistician)
605 return false;
606 return statistician->IsPacketInOrder(header.sequenceNumber);
607}
608
mflodmanfa666592016-04-28 23:15:33 -0700609bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
610 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000611 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100612 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000613 return false;
614 StreamStatistician* statistician =
615 rtp_receive_statistics_->GetStatistician(header.ssrc);
616 if (!statistician)
617 return false;
618 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000619 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200620 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000621 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000622 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000623}
mflodmandc7d0d22016-05-06 05:32:22 -0700624
625void RtpStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700626 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
mflodmandc7d0d22016-05-06 05:32:22 -0700627 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700628 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700629 "WebRTC.Video.ReceivedFecPacketsInPercent",
630 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
631 }
632 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700633 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
634 static_cast<int>(counter.num_recovered_packets *
635 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700636 }
637}
638
639void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
640 const std::string& extension, int id) {
641 // One-byte-extension local identifiers are in the range 1-14 inclusive.
642 RTC_DCHECK_GE(id, 1);
643 RTC_DCHECK_LE(id, 14);
644 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
645 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
646 StringToRtpExtensionType(extension), id));
647}
648
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000649} // namespace webrtc