blob: d2360858729993b3432006b746bc0c54516d729b [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"
philipel022b54e2016-12-20 04:15:59 -080020#include "webrtc/media/base/mediaconstants.h"
mflodmanc0e58a32016-04-25 01:26:26 -070021#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000022#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010023#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010024#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
25#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010026#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
27#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
brandtrd55c3f62016-10-31 04:51:33 -070028#include "webrtc/modules/rtp_rtcp/include/ulpfec_receiver.h"
philipelfd5a20f2016-11-15 00:57:57 -080029#include "webrtc/modules/video_coding/frame_object.h"
philipel022b54e2016-12-20 04:15:59 -080030#include "webrtc/modules/video_coding/h264_sprop_parameter_sets.h"
philipelfd5a20f2016-11-15 00:57:57 -080031#include "webrtc/modules/video_coding/h264_sps_pps_tracker.h"
32#include "webrtc/modules/video_coding/packet_buffer.h"
Peter Boström0b250722016-04-22 18:23:15 +020033#include "webrtc/modules/video_coding/video_coding_impl.h"
philipelfd5a20f2016-11-15 00:57:57 -080034#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010035#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010036#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
37#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070038#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070039#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000040
41namespace webrtc {
42
philipelfd5a20f2016-11-15 00:57:57 -080043namespace {
44constexpr int kPacketBufferStartSize = 32;
45constexpr int kPacketBufferMaxSixe = 2048;
46}
47
mflodmanc0e58a32016-04-25 01:26:26 -070048std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
49 ReceiveStatistics* receive_statistics,
50 Transport* outgoing_transport,
51 RtcpRttStats* rtt_stats,
52 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
53 RemoteBitrateEstimator* remote_bitrate_estimator,
54 RtpPacketSender* paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +020055 TransportSequenceNumberAllocator* transport_sequence_number_allocator,
56 RateLimiter* retransmission_rate_limiter) {
mflodmanc0e58a32016-04-25 01:26:26 -070057 RtpRtcp::Configuration configuration;
58 configuration.audio = false;
59 configuration.receiver_only = true;
60 configuration.receive_statistics = receive_statistics;
61 configuration.outgoing_transport = outgoing_transport;
62 configuration.intra_frame_callback = nullptr;
63 configuration.rtt_stats = rtt_stats;
64 configuration.rtcp_packet_type_counter_observer =
65 rtcp_packet_type_counter_observer;
66 configuration.paced_sender = paced_sender;
67 configuration.transport_sequence_number_allocator =
68 transport_sequence_number_allocator;
69 configuration.send_bitrate_observer = nullptr;
70 configuration.send_frame_count_observer = nullptr;
71 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070072 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070073 configuration.bandwidth_callback = nullptr;
74 configuration.transport_feedback_callback = nullptr;
Erik Språng737336d2016-07-29 12:59:36 +020075 configuration.retransmission_rate_limiter = retransmission_rate_limiter;
mflodmanc0e58a32016-04-25 01:26:26 -070076
77 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
78 rtp_rtcp->SetSendingStatus(false);
79 rtp_rtcp->SetSendingMediaStatus(false);
80 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
81
82 return rtp_rtcp;
83}
84
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000085static const int kPacketLogIntervalMs = 10000;
86
mflodmanfa666592016-04-28 23:15:33 -070087RtpStreamReceiver::RtpStreamReceiver(
88 vcm::VideoReceiver* video_receiver,
89 RemoteBitrateEstimator* remote_bitrate_estimator,
90 Transport* transport,
91 RtcpRttStats* rtt_stats,
92 PacedSender* paced_sender,
mflodmancfc8e3b2016-05-03 21:22:04 -070093 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070094 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020095 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070096 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020097 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080098 RateLimiter* retransmission_rate_limiter,
99 NackSender* nack_sender,
100 KeyFrameRequestSender* keyframe_request_sender,
101 video_coding::OnCompleteFrameCallback* complete_frame_callback,
102 VCMTiming* timing)
Tommi97888bd2016-01-21 23:24:59 +0100103 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +0200104 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +0200105 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000106 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -0700107 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700108 remb_(remb),
109 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100110 ntp_estimator_(clock_),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100111 rtp_header_parser_(RtpHeaderParser::Create()),
112 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
113 this,
mflodmanfa666592016-04-28 23:15:33 -0700114 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100115 &rtp_payload_registry_)),
116 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtrd55c3f62016-10-31 04:51:33 -0700117 ulpfec_receiver_(UlpfecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000118 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000119 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700120 last_packet_log_ms_(-1),
121 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
122 transport,
123 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700124 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700125 remote_bitrate_estimator_,
126 paced_sender,
Erik Språng737336d2016-07-29 12:59:36 +0200127 packet_router,
philipelfd5a20f2016-11-15 00:57:57 -0800128 retransmission_rate_limiter)),
129 complete_frame_callback_(complete_frame_callback),
130 keyframe_request_sender_(keyframe_request_sender),
131 timing_(timing) {
mflodmanc0e58a32016-04-25 01:26:26 -0700132 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700133 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
134 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
135
Tommi733b5472016-06-10 17:58:01 +0200136 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700137 << "A stream should not be configured with RTCP disabled. This value is "
138 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700139 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
140 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
141 RTC_DCHECK(config_.rtp.local_ssrc != 0);
142 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
143
Tommi733b5472016-06-10 17:58:01 +0200144 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
145 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700146 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200147 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700148 rtp_rtcp_->SetREMBStatus(true);
149 remb_->AddReceiveChannel(rtp_rtcp_.get());
150 }
151
Tommi733b5472016-06-10 17:58:01 +0200152 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
153 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
154 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700155 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700156
157 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200158 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
159 ? kMaxPacketAgeToNack
160 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700161 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700162
163 // TODO(pbos): Support multiple RTX, per video payload.
164 for (const auto& kv : config_.rtp.rtx) {
165 RTC_DCHECK(kv.second.ssrc != 0);
166 RTC_DCHECK(kv.second.payload_type != 0);
167
168 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
169 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
170 kv.first);
171 }
172
brandtrf7c6d722016-12-08 08:25:47 -0800173 if (IsUlpfecEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700174 VideoCodec ulpfec_codec = {};
175 ulpfec_codec.codecType = kVideoCodecULPFEC;
176 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700177 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
johan07e276c2016-12-13 02:23:03 -0800178 RTC_CHECK(AddReceiveCodec(ulpfec_codec));
brandtre6f98c72016-11-11 03:28:30 -0800179 }
mflodmandc7d0d22016-05-06 05:32:22 -0700180
brandtre6f98c72016-11-11 03:28:30 -0800181 if (IsRedEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700182 VideoCodec red_codec = {};
183 red_codec.codecType = kVideoCodecRED;
184 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700185 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
johan07e276c2016-12-13 02:23:03 -0800186 RTC_CHECK(AddReceiveCodec(red_codec));
brandtrb5f2c3f2016-10-04 23:28:39 -0700187 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
mflodmandc7d0d22016-05-06 05:32:22 -0700188 rtp_payload_registry_.SetRtxPayloadType(
brandtrb5f2c3f2016-10-04 23:28:39 -0700189 config_.rtp.ulpfec.red_rtx_payload_type,
190 config_.rtp.ulpfec.red_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700191 }
192 }
193
Tommi733b5472016-06-10 17:58:01 +0200194 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700195 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
196
197 // Stats callback for CNAME changes.
198 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
199
mflodmandc7d0d22016-05-06 05:32:22 -0700200 process_thread_->RegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800201
202 jitter_buffer_experiment_ =
203 field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") == "Enabled";
204
205 if (jitter_buffer_experiment_) {
206 nack_module_.reset(
207 new NackModule(clock_, nack_sender, keyframe_request_sender));
208 process_thread_->RegisterModule(nack_module_.get());
209
210 packet_buffer_ = video_coding::PacketBuffer::Create(
211 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
212 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
213 }
mflodmanc0e58a32016-04-25 01:26:26 -0700214}
niklase@google.com470e71d2011-07-07 08:21:25 +0000215
mflodmanfa666592016-04-28 23:15:33 -0700216RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700217 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
philipelfd5a20f2016-11-15 00:57:57 -0800219 if (jitter_buffer_experiment_)
220 process_thread_->DeRegisterModule(nack_module_.get());
221
mflodmandc7d0d22016-05-06 05:32:22 -0700222 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
223 rtp_rtcp_->SetREMBStatus(false);
224 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
225 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000226}
227
philipel022b54e2016-12-20 04:15:59 -0800228bool RtpStreamReceiver::AddReceiveCodec(
229 const VideoCodec& video_codec,
230 const std::map<std::string, std::string>& codec_params) {
231 pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
232 return AddReceiveCodec(video_codec);
233}
234
johan07e276c2016-12-13 02:23:03 -0800235bool RtpStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000236 int8_t old_pltype = -1;
magjed56124bd2016-11-24 09:34:46 -0800237 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
238 -1) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100239 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000240 }
magjed56124bd2016-11-24 09:34:46 -0800241 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000242}
243
mflodmanfa666592016-04-28 23:15:33 -0700244uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000245 return rtp_receiver_->SSRC();
246}
247
mflodmanfa666592016-04-28 23:15:33 -0700248int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000249 return rtp_receiver_->CSRCs(csrcs);
250}
251
mflodmanfa666592016-04-28 23:15:33 -0700252RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000253 return rtp_receiver_.get();
254}
255
mflodmanfa666592016-04-28 23:15:33 -0700256int32_t RtpStreamReceiver::OnReceivedPayloadData(
257 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200258 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700259 const WebRtcRTPHeader* rtp_header) {
Peter Boström0b250722016-04-22 18:23:15 +0200260 RTC_DCHECK(video_receiver_);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000261 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000262 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100263 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipelfd5a20f2016-11-15 00:57:57 -0800264 if (jitter_buffer_experiment_) {
265 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
266 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds());
267 packet.timesNacked = nack_module_->OnReceivedPacket(packet);
268
269 if (packet.codec == kVideoCodecH264) {
philipel022b54e2016-12-20 04:15:59 -0800270 // Only when we start to receive packets will we know what payload type
271 // that will be used. When we know the payload type insert the correct
272 // sps/pps into the tracker.
273 if (packet.payloadType != last_payload_type_) {
274 last_payload_type_ = packet.payloadType;
275 InsertSpsPpsIntoTracker(packet.payloadType);
276 }
277
philipelfd5a20f2016-11-15 00:57:57 -0800278 switch (tracker_.CopyAndFixBitstream(&packet)) {
279 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
280 keyframe_request_sender_->RequestKeyFrame();
281 FALLTHROUGH();
282 case video_coding::H264SpsPpsTracker::kDrop:
283 return 0;
284 case video_coding::H264SpsPpsTracker::kInsert:
285 break;
286 }
287 } else {
288 uint8_t* data = new uint8_t[packet.sizeBytes];
289 memcpy(data, packet.dataPtr, packet.sizeBytes);
290 packet.dataPtr = data;
291 }
292
philipel759e0b72016-11-30 01:32:05 -0800293 packet_buffer_->InsertPacket(&packet);
philipelfd5a20f2016-11-15 00:57:57 -0800294 } else {
295 if (video_receiver_->IncomingPacket(payload_data, payload_size,
296 rtp_header_with_ntp) != 0) {
297 // Check this...
298 return -1;
299 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000300 }
301 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000302}
303
mflodmanfa666592016-04-28 23:15:33 -0700304bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
305 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000306 RTPHeader header;
307 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000308 return false;
309 }
310 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000311 bool in_order = IsPacketInOrder(header);
312 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000313}
314
mflodmanfa666592016-04-28 23:15:33 -0700315// TODO(pbos): Remove as soon as audio can handle a changing payload type
316// without this callback.
317int32_t RtpStreamReceiver::OnInitializeDecoder(
318 const int8_t payload_type,
319 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
320 const int frequency,
321 const size_t channels,
322 const uint32_t rate) {
323 RTC_NOTREACHED();
324 return 0;
325}
326
327void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
328 rtp_rtcp_->SetRemoteSSRC(ssrc);
329}
330
331bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
332 size_t rtp_packet_length,
333 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100334 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000335 {
Tommi97888bd2016-01-21 23:24:59 +0100336 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000337 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100338 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000339 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000340 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000341
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000342 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000343 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000344 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100345 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000346 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000347 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000348 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000349 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000350 if (packet_time.timestamp != -1)
351 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
352 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000353 arrival_time_ms = now_ms;
354
355 {
356 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100357 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000358 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
359 std::stringstream ss;
360 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
361 << static_cast<int>(header.payloadType) << ", timestamp: "
362 << header.timestamp << ", sequence number: " << header.sequenceNumber
363 << ", arrival time: " << arrival_time_ms;
364 if (header.extension.hasTransmissionTimeOffset)
365 ss << ", toffset: " << header.extension.transmissionTimeOffset;
366 if (header.extension.hasAbsoluteSendTime)
367 ss << ", abs send time: " << header.extension.absoluteSendTime;
368 LOG(LS_INFO) << ss.str();
369 last_packet_log_ms_ = now_ms;
370 }
371 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000372
Stefan Holmerff4ea932015-06-18 16:01:33 +0200373 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700374 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000375 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000376
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000377 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100378 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100379 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000380 // Update receive statistics after ReceivePacket.
381 // Receive statistics will be reset if the payload type changes (make sure
382 // that the first packet is included in the stats).
383 rtp_receive_statistics_->IncomingPacket(
384 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
385 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000386}
387
mflodmancfc8e3b2016-05-03 21:22:04 -0700388int32_t RtpStreamReceiver::RequestKeyFrame() {
389 return rtp_rtcp_->RequestKeyFrame();
390}
391
392int32_t RtpStreamReceiver::SliceLossIndicationRequest(
393 const uint64_t picture_id) {
394 return rtp_rtcp_->SendRTCPSliceLossIndication(
395 static_cast<uint8_t>(picture_id));
396}
397
brandtrf7c6d722016-12-08 08:25:47 -0800398bool RtpStreamReceiver::IsUlpfecEnabled() const {
brandtre6f98c72016-11-11 03:28:30 -0800399 return config_.rtp.ulpfec.ulpfec_payload_type != -1;
400}
401
402bool RtpStreamReceiver::IsRedEnabled() const {
403 return config_.rtp.ulpfec.red_payload_type != -1;
mflodmandc7d0d22016-05-06 05:32:22 -0700404}
405
406bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
407 return config_.rtp.nack.rtp_history_ms > 0;
408}
409
410void RtpStreamReceiver::RequestPacketRetransmit(
411 const std::vector<uint16_t>& sequence_numbers) {
412 rtp_rtcp_->SendNack(sequence_numbers);
413}
414
mflodmancfc8e3b2016-05-03 21:22:04 -0700415int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
416 uint16_t length) {
417 return rtp_rtcp_->SendNACK(sequence_numbers, length);
418}
419
philipelfd5a20f2016-11-15 00:57:57 -0800420void RtpStreamReceiver::OnReceivedFrame(
421 std::unique_ptr<video_coding::RtpFrameObject> frame) {
422 reference_finder_->ManageFrame(std::move(frame));
423}
424
425void RtpStreamReceiver::OnCompleteFrame(
426 std::unique_ptr<video_coding::FrameObject> frame) {
427 {
428 rtc::CritScope lock(&last_seq_num_cs_);
429 video_coding::RtpFrameObject* rtp_frame =
430 static_cast<video_coding::RtpFrameObject*>(frame.get());
431 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
432 }
433 complete_frame_callback_->OnCompleteFrame(std::move(frame));
434}
435
436void RtpStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
437 if (jitter_buffer_experiment_)
438 nack_module_->UpdateRtt(max_rtt_ms);
439}
440
mflodmanfa666592016-04-28 23:15:33 -0700441bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
442 size_t packet_length,
443 const RTPHeader& header,
444 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100445 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000446 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
447 }
448 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000449 assert(packet_length >= header.headerLength);
450 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000451 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100452 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
453 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000454 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000455 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000456 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
457 payload_specific, in_order);
458}
459
mflodmanfa666592016-04-28 23:15:33 -0700460bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
461 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100462 if (rtp_payload_registry_.IsRed(header)) {
463 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000464 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000465 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200466 // Notify video_receiver about received FEC packets to avoid NACKing these
467 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000468 NotifyReceiverOfFecPacket(header);
469 }
brandtrd55c3f62016-10-31 04:51:33 -0700470 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
471 ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000472 return false;
473 }
brandtrd55c3f62016-10-31 04:51:33 -0700474 return ulpfec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100475 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000476 if (header.headerLength + header.paddingLength == packet_length) {
477 // This is an empty packet and should be silently dropped before trying to
478 // parse the RTX header.
479 return true;
480 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000481 // Remove the RTX header and parse the original RTP header.
482 if (packet_length < header.headerLength)
483 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000484 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000485 return false;
Tommi97888bd2016-01-21 23:24:59 +0100486 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000487 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000488 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000489 return false;
490 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100491 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700492 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
493 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100494 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
495 << header.ssrc << " payload type: "
496 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000497 return false;
498 }
499 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700500 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000501 restored_packet_in_use_ = false;
502 return ret;
503 }
504 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505}
506
mflodmanfa666592016-04-28 23:15:33 -0700507void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000508 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100509 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000510 if (last_media_payload_type < 0) {
511 LOG(LS_WARNING) << "Failed to get last media payload type.";
512 return;
513 }
514 // Fake an empty media packet.
515 WebRtcRTPHeader rtp_header = {};
516 rtp_header.header = header;
517 rtp_header.header.payloadType = last_media_payload_type;
518 rtp_header.header.paddingLength = 0;
519 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100520 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
521 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000522 LOG(LS_WARNING) << "Failed to get payload specifics.";
523 return;
524 }
525 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000526 rtp_header.type.Video.rotation = kVideoRotation_0;
527 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700528 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000529 }
isheriff6b4b5f32016-06-08 00:24:21 -0700530 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
531
Peter Boström74f6e9e2016-04-04 17:56:10 +0200532 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000533}
534
mflodmanfa666592016-04-28 23:15:33 -0700535bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
536 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000537 {
Tommi97888bd2016-01-21 23:24:59 +0100538 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000539 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100540 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000541 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100542 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000543
Per83d09102016-04-15 14:59:13 +0200544 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000545
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000546 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200547 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000548 if (rtt == 0) {
549 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100550 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000551 }
552 uint32_t ntp_secs = 0;
553 uint32_t ntp_frac = 0;
554 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200555 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
556 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000557 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100558 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000559 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100560 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000561
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100562 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000563}
564
philipelfd5a20f2016-11-15 00:57:57 -0800565void RtpStreamReceiver::FrameContinuous(uint16_t picture_id) {
566 if (jitter_buffer_experiment_) {
567 int seq_num = -1;
568 {
569 rtc::CritScope lock(&last_seq_num_cs_);
570 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
571 if (seq_num_it != last_seq_num_for_pic_id_.end())
572 seq_num = seq_num_it->second;
573 }
574 if (seq_num != -1)
575 nack_module_->ClearUpTo(seq_num);
576 }
577}
578
579void RtpStreamReceiver::FrameDecoded(uint16_t picture_id) {
580 if (jitter_buffer_experiment_) {
581 int seq_num = -1;
582 {
583 rtc::CritScope lock(&last_seq_num_cs_);
584 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
585 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
586 seq_num = seq_num_it->second;
587 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
588 ++seq_num_it);
589 }
590 }
591 if (seq_num != -1) {
592 packet_buffer_->ClearTo(seq_num);
593 reference_finder_->ClearTo(seq_num);
594 }
595 }
596}
597
mflodmandc7d0d22016-05-06 05:32:22 -0700598void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
599 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
600 : RtcpMode::kOff);
601}
602
mflodmanfa666592016-04-28 23:15:33 -0700603void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100604 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000605 receiving_ = true;
606}
607
mflodmanfa666592016-04-28 23:15:33 -0700608void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100609 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000610 receiving_ = false;
611}
612
mflodmanfa666592016-04-28 23:15:33 -0700613bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000614 StreamStatistician* statistician =
615 rtp_receive_statistics_->GetStatistician(header.ssrc);
616 if (!statistician)
617 return false;
618 return statistician->IsPacketInOrder(header.sequenceNumber);
619}
620
mflodmanfa666592016-04-28 23:15:33 -0700621bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
622 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000623 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100624 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000625 return false;
626 StreamStatistician* statistician =
627 rtp_receive_statistics_->GetStatistician(header.ssrc);
628 if (!statistician)
629 return false;
630 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000631 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200632 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000633 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000634 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000635}
mflodmandc7d0d22016-05-06 05:32:22 -0700636
637void RtpStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700638 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800639 if (counter.first_packet_time_ms == -1)
640 return;
641
642 int64_t elapsed_sec =
643 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
644 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
645 return;
646
mflodmandc7d0d22016-05-06 05:32:22 -0700647 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700648 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700649 "WebRTC.Video.ReceivedFecPacketsInPercent",
650 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
651 }
652 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700653 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
654 static_cast<int>(counter.num_recovered_packets *
655 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700656 }
657}
658
659void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
660 const std::string& extension, int id) {
661 // One-byte-extension local identifiers are in the range 1-14 inclusive.
662 RTC_DCHECK_GE(id, 1);
663 RTC_DCHECK_LE(id, 14);
664 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
665 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
666 StringToRtpExtensionType(extension), id));
667}
668
philipel022b54e2016-12-20 04:15:59 -0800669void RtpStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
670 auto codec_params_it = pt_codec_params_.find(payload_type);
671 if (codec_params_it == pt_codec_params_.end())
672 return;
673
674 LOG(LS_INFO) << "Found out of band supplied codec parameters for"
675 << " payload type: " << payload_type;
676
677 H264SpropParameterSets sprop_decoder;
678 auto sprop_base64_it =
679 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
680
681 if (sprop_base64_it == codec_params_it->second.end())
682 return;
683
684 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second))
685 return;
686
687 tracker_.InsertSpsPps(sprop_decoder.sps_nalu(), sprop_decoder.pps_nalu());
688}
689
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000690} // namespace webrtc