blob: 8777535a25abb3f98e0b6d80e0a509383666f3d8 [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,
nisse15389c02017-01-24 02:36:58 -080054 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
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;
mflodmanc0e58a32016-04-25 01:26:26 -070064 configuration.transport_sequence_number_allocator =
65 transport_sequence_number_allocator;
66 configuration.send_bitrate_observer = nullptr;
67 configuration.send_frame_count_observer = nullptr;
68 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070069 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070070 configuration.bandwidth_callback = nullptr;
71 configuration.transport_feedback_callback = nullptr;
72
73 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
74 rtp_rtcp->SetSendingStatus(false);
75 rtp_rtcp->SetSendingMediaStatus(false);
76 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
77
78 return rtp_rtcp;
79}
80
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000081static const int kPacketLogIntervalMs = 10000;
82
mflodmanfa666592016-04-28 23:15:33 -070083RtpStreamReceiver::RtpStreamReceiver(
84 vcm::VideoReceiver* video_receiver,
85 RemoteBitrateEstimator* remote_bitrate_estimator,
86 Transport* transport,
87 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070088 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070089 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020090 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070091 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020092 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080093 NackSender* nack_sender,
94 KeyFrameRequestSender* keyframe_request_sender,
95 video_coding::OnCompleteFrameCallback* complete_frame_callback,
96 VCMTiming* timing)
Tommi97888bd2016-01-21 23:24:59 +010097 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020098 config_(*config),
Peter Boström0b250722016-04-22 18:23:15 +020099 video_receiver_(video_receiver),
stefan@webrtc.org976a7e62012-09-21 13:20:21 +0000100 remote_bitrate_estimator_(remote_bitrate_estimator),
mflodmanc0e58a32016-04-25 01:26:26 -0700101 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700102 remb_(remb),
103 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100104 ntp_estimator_(clock_),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100105 rtp_header_parser_(RtpHeaderParser::Create()),
106 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
107 this,
mflodmanfa666592016-04-28 23:15:33 -0700108 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100109 &rtp_payload_registry_)),
110 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtrd55c3f62016-10-31 04:51:33 -0700111 ulpfec_receiver_(UlpfecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000112 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000113 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700114 last_packet_log_ms_(-1),
115 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
116 transport,
117 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700118 receive_stats_proxy,
mflodmanc0e58a32016-04-25 01:26:26 -0700119 remote_bitrate_estimator_,
nisse15389c02017-01-24 02:36:58 -0800120 packet_router)),
philipelfd5a20f2016-11-15 00:57:57 -0800121 complete_frame_callback_(complete_frame_callback),
122 keyframe_request_sender_(keyframe_request_sender),
123 timing_(timing) {
mflodmanc0e58a32016-04-25 01:26:26 -0700124 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700125 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
126 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
127
Tommi733b5472016-06-10 17:58:01 +0200128 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700129 << "A stream should not be configured with RTCP disabled. This value is "
130 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700131 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
132 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
133 RTC_DCHECK(config_.rtp.local_ssrc != 0);
134 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
135
Tommi733b5472016-06-10 17:58:01 +0200136 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
137 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700138 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200139 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700140 rtp_rtcp_->SetREMBStatus(true);
141 remb_->AddReceiveChannel(rtp_rtcp_.get());
142 }
143
Tommi733b5472016-06-10 17:58:01 +0200144 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
145 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
146 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700147 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700148
149 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200150 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
151 ? kMaxPacketAgeToNack
152 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700153 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700154
155 // TODO(pbos): Support multiple RTX, per video payload.
156 for (const auto& kv : config_.rtp.rtx) {
157 RTC_DCHECK(kv.second.ssrc != 0);
158 RTC_DCHECK(kv.second.payload_type != 0);
159
160 rtp_payload_registry_.SetRtxSsrc(kv.second.ssrc);
161 rtp_payload_registry_.SetRtxPayloadType(kv.second.payload_type,
162 kv.first);
163 }
164
brandtrf7c6d722016-12-08 08:25:47 -0800165 if (IsUlpfecEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700166 VideoCodec ulpfec_codec = {};
167 ulpfec_codec.codecType = kVideoCodecULPFEC;
168 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700169 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
johan07e276c2016-12-13 02:23:03 -0800170 RTC_CHECK(AddReceiveCodec(ulpfec_codec));
brandtre6f98c72016-11-11 03:28:30 -0800171 }
mflodmandc7d0d22016-05-06 05:32:22 -0700172
brandtre6f98c72016-11-11 03:28:30 -0800173 if (IsRedEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700174 VideoCodec red_codec = {};
175 red_codec.codecType = kVideoCodecRED;
176 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700177 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
johan07e276c2016-12-13 02:23:03 -0800178 RTC_CHECK(AddReceiveCodec(red_codec));
brandtrb5f2c3f2016-10-04 23:28:39 -0700179 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
mflodmandc7d0d22016-05-06 05:32:22 -0700180 rtp_payload_registry_.SetRtxPayloadType(
brandtrb5f2c3f2016-10-04 23:28:39 -0700181 config_.rtp.ulpfec.red_rtx_payload_type,
182 config_.rtp.ulpfec.red_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700183 }
184 }
185
Tommi733b5472016-06-10 17:58:01 +0200186 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700187 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
188
189 // Stats callback for CNAME changes.
190 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
191
mflodmandc7d0d22016-05-06 05:32:22 -0700192 process_thread_->RegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800193
kjellander04926b82017-01-19 00:06:17 -0800194 jitter_buffer_experiment_ =
195 field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") == "Enabled";
philipelfd5a20f2016-11-15 00:57:57 -0800196
kjellander04926b82017-01-19 00:06:17 -0800197 if (jitter_buffer_experiment_) {
198 nack_module_.reset(
199 new NackModule(clock_, nack_sender, keyframe_request_sender));
200 process_thread_->RegisterModule(nack_module_.get());
201
202 packet_buffer_ = video_coding::PacketBuffer::Create(
203 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
204 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
205 }
mflodmanc0e58a32016-04-25 01:26:26 -0700206}
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
mflodmanfa666592016-04-28 23:15:33 -0700208RtpStreamReceiver::~RtpStreamReceiver() {
mflodmandc7d0d22016-05-06 05:32:22 -0700209 process_thread_->DeRegisterModule(rtp_rtcp_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
kjellander04926b82017-01-19 00:06:17 -0800211 if (jitter_buffer_experiment_)
212 process_thread_->DeRegisterModule(nack_module_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800213
mflodmandc7d0d22016-05-06 05:32:22 -0700214 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
215 rtp_rtcp_->SetREMBStatus(false);
johan62d02c32017-01-24 04:38:27 -0800216 if (config_.rtp.remb) {
217 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
218 }
mflodmandc7d0d22016-05-06 05:32:22 -0700219 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000220}
221
philipel022b54e2016-12-20 04:15:59 -0800222bool RtpStreamReceiver::AddReceiveCodec(
223 const VideoCodec& video_codec,
224 const std::map<std::string, std::string>& codec_params) {
225 pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
226 return AddReceiveCodec(video_codec);
227}
228
johan07e276c2016-12-13 02:23:03 -0800229bool RtpStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000230 int8_t old_pltype = -1;
magjed56124bd2016-11-24 09:34:46 -0800231 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
232 -1) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100233 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000234 }
magjed56124bd2016-11-24 09:34:46 -0800235 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000236}
237
mflodmanfa666592016-04-28 23:15:33 -0700238uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000239 return rtp_receiver_->SSRC();
240}
241
mflodmanfa666592016-04-28 23:15:33 -0700242int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000243 return rtp_receiver_->CSRCs(csrcs);
244}
245
mflodmanfa666592016-04-28 23:15:33 -0700246RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000247 return rtp_receiver_.get();
248}
249
mflodmanfa666592016-04-28 23:15:33 -0700250int32_t RtpStreamReceiver::OnReceivedPayloadData(
251 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200252 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700253 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000254 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000255 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100256 ntp_estimator_.Estimate(rtp_header->header.timestamp);
kjellander04926b82017-01-19 00:06:17 -0800257 if (jitter_buffer_experiment_) {
258 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
259 timing_->IncomingTimestamp(packet.timestamp, clock_->TimeInMilliseconds());
260 packet.timesNacked = nack_module_->OnReceivedPacket(packet);
philipelfd5a20f2016-11-15 00:57:57 -0800261
kjellander04926b82017-01-19 00:06:17 -0800262 if (packet.codec == kVideoCodecH264) {
263 // Only when we start to receive packets will we know what payload type
264 // that will be used. When we know the payload type insert the correct
265 // sps/pps into the tracker.
266 if (packet.payloadType != last_payload_type_) {
267 last_payload_type_ = packet.payloadType;
268 InsertSpsPpsIntoTracker(packet.payloadType);
269 }
270
271 switch (tracker_.CopyAndFixBitstream(&packet)) {
272 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
273 keyframe_request_sender_->RequestKeyFrame();
274 FALLTHROUGH();
275 case video_coding::H264SpsPpsTracker::kDrop:
276 return 0;
277 case video_coding::H264SpsPpsTracker::kInsert:
278 break;
279 }
280 } else {
281 uint8_t* data = new uint8_t[packet.sizeBytes];
282 memcpy(data, packet.dataPtr, packet.sizeBytes);
283 packet.dataPtr = data;
philipelfd5a20f2016-11-15 00:57:57 -0800284 }
285
kjellander04926b82017-01-19 00:06:17 -0800286 packet_buffer_->InsertPacket(&packet);
philipelfd5a20f2016-11-15 00:57:57 -0800287 } else {
johan62d02c32017-01-24 04:38:27 -0800288 RTC_DCHECK(video_receiver_);
kjellander04926b82017-01-19 00:06:17 -0800289 if (video_receiver_->IncomingPacket(payload_data, payload_size,
290 rtp_header_with_ntp) != 0) {
291 // Check this...
292 return -1;
293 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000294 }
295 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000296}
297
mflodmanfa666592016-04-28 23:15:33 -0700298bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
299 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000300 RTPHeader header;
301 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000302 return false;
303 }
304 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000305 bool in_order = IsPacketInOrder(header);
306 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000307}
308
mflodmanfa666592016-04-28 23:15:33 -0700309// TODO(pbos): Remove as soon as audio can handle a changing payload type
310// without this callback.
311int32_t RtpStreamReceiver::OnInitializeDecoder(
312 const int8_t payload_type,
313 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
314 const int frequency,
315 const size_t channels,
316 const uint32_t rate) {
317 RTC_NOTREACHED();
318 return 0;
319}
320
321void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
322 rtp_rtcp_->SetRemoteSSRC(ssrc);
323}
324
325bool RtpStreamReceiver::DeliverRtp(const uint8_t* rtp_packet,
326 size_t rtp_packet_length,
327 const PacketTime& packet_time) {
Peter Boström8c66a002016-02-11 13:51:10 +0100328 RTC_DCHECK(remote_bitrate_estimator_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000329 {
Tommi97888bd2016-01-21 23:24:59 +0100330 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000331 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100332 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000333 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000334 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000335
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000336 RTPHeader header;
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000337 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length,
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000338 &header)) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100339 return false;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000340 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000341 size_t payload_length = rtp_packet_length - header.headerLength;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000342 int64_t arrival_time_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000343 int64_t now_ms = clock_->TimeInMilliseconds();
wu@webrtc.orga9890802013-12-13 00:21:03 +0000344 if (packet_time.timestamp != -1)
345 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
346 else
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000347 arrival_time_ms = now_ms;
348
349 {
350 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100351 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000352 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
353 std::stringstream ss;
354 ss << "Packet received on SSRC: " << header.ssrc << " with payload type: "
355 << static_cast<int>(header.payloadType) << ", timestamp: "
356 << header.timestamp << ", sequence number: " << header.sequenceNumber
357 << ", arrival time: " << arrival_time_ms;
358 if (header.extension.hasTransmissionTimeOffset)
359 ss << ", toffset: " << header.extension.transmissionTimeOffset;
360 if (header.extension.hasAbsoluteSendTime)
361 ss << ", abs send time: " << header.extension.absoluteSendTime;
362 LOG(LS_INFO) << ss.str();
363 last_packet_log_ms_ = now_ms;
364 }
365 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000366
Stefan Holmerff4ea932015-06-18 16:01:33 +0200367 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_length,
pbos2169d8b2016-06-20 11:53:02 -0700368 header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000369 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000370
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000371 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100372 rtp_payload_registry_.SetIncomingPayloadType(header);
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100373 bool ret = ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000374 // Update receive statistics after ReceivePacket.
375 // Receive statistics will be reset if the payload type changes (make sure
376 // that the first packet is included in the stats).
377 rtp_receive_statistics_->IncomingPacket(
378 header, rtp_packet_length, IsPacketRetransmitted(header, in_order));
379 return ret;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000380}
381
mflodmancfc8e3b2016-05-03 21:22:04 -0700382int32_t RtpStreamReceiver::RequestKeyFrame() {
383 return rtp_rtcp_->RequestKeyFrame();
384}
385
386int32_t RtpStreamReceiver::SliceLossIndicationRequest(
387 const uint64_t picture_id) {
388 return rtp_rtcp_->SendRTCPSliceLossIndication(
389 static_cast<uint8_t>(picture_id));
390}
391
brandtrf7c6d722016-12-08 08:25:47 -0800392bool RtpStreamReceiver::IsUlpfecEnabled() const {
brandtre6f98c72016-11-11 03:28:30 -0800393 return config_.rtp.ulpfec.ulpfec_payload_type != -1;
394}
395
396bool RtpStreamReceiver::IsRedEnabled() const {
397 return config_.rtp.ulpfec.red_payload_type != -1;
mflodmandc7d0d22016-05-06 05:32:22 -0700398}
399
400bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
401 return config_.rtp.nack.rtp_history_ms > 0;
402}
403
404void RtpStreamReceiver::RequestPacketRetransmit(
405 const std::vector<uint16_t>& sequence_numbers) {
406 rtp_rtcp_->SendNack(sequence_numbers);
407}
408
mflodmancfc8e3b2016-05-03 21:22:04 -0700409int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
410 uint16_t length) {
411 return rtp_rtcp_->SendNACK(sequence_numbers, length);
412}
413
philipelfd5a20f2016-11-15 00:57:57 -0800414void RtpStreamReceiver::OnReceivedFrame(
415 std::unique_ptr<video_coding::RtpFrameObject> frame) {
416 reference_finder_->ManageFrame(std::move(frame));
417}
418
419void RtpStreamReceiver::OnCompleteFrame(
420 std::unique_ptr<video_coding::FrameObject> frame) {
421 {
422 rtc::CritScope lock(&last_seq_num_cs_);
423 video_coding::RtpFrameObject* rtp_frame =
424 static_cast<video_coding::RtpFrameObject*>(frame.get());
425 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
426 }
427 complete_frame_callback_->OnCompleteFrame(std::move(frame));
428}
429
430void RtpStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
kjellander04926b82017-01-19 00:06:17 -0800431 if (jitter_buffer_experiment_)
432 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800433}
434
mflodmanfa666592016-04-28 23:15:33 -0700435bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
436 size_t packet_length,
437 const RTPHeader& header,
438 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100439 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000440 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
441 }
442 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000443 assert(packet_length >= header.headerLength);
444 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000445 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100446 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
447 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000448 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000449 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000450 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
451 payload_specific, in_order);
452}
453
mflodmanfa666592016-04-28 23:15:33 -0700454bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
455 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100456 if (rtp_payload_registry_.IsRed(header)) {
457 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000458 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000459 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200460 // Notify video_receiver about received FEC packets to avoid NACKing these
461 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000462 NotifyReceiverOfFecPacket(header);
463 }
brandtrd55c3f62016-10-31 04:51:33 -0700464 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
465 ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000466 return false;
467 }
brandtrd55c3f62016-10-31 04:51:33 -0700468 return ulpfec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100469 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000470 if (header.headerLength + header.paddingLength == packet_length) {
471 // This is an empty packet and should be silently dropped before trying to
472 // parse the RTX header.
473 return true;
474 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000475 // Remove the RTX header and parse the original RTP header.
476 if (packet_length < header.headerLength)
477 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000478 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000479 return false;
Tommi97888bd2016-01-21 23:24:59 +0100480 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000481 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000482 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000483 return false;
484 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100485 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700486 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
487 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100488 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
489 << header.ssrc << " payload type: "
490 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000491 return false;
492 }
493 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700494 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000495 restored_packet_in_use_ = false;
496 return ret;
497 }
498 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000499}
500
mflodmanfa666592016-04-28 23:15:33 -0700501void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000502 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100503 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000504 if (last_media_payload_type < 0) {
505 LOG(LS_WARNING) << "Failed to get last media payload type.";
506 return;
507 }
508 // Fake an empty media packet.
509 WebRtcRTPHeader rtp_header = {};
510 rtp_header.header = header;
511 rtp_header.header.payloadType = last_media_payload_type;
512 rtp_header.header.paddingLength = 0;
513 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100514 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
515 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000516 LOG(LS_WARNING) << "Failed to get payload specifics.";
517 return;
518 }
519 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000520 rtp_header.type.Video.rotation = kVideoRotation_0;
521 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700522 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000523 }
isheriff6b4b5f32016-06-08 00:24:21 -0700524 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
525
Peter Boström74f6e9e2016-04-04 17:56:10 +0200526 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000527}
528
mflodmanfa666592016-04-28 23:15:33 -0700529bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
530 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000531 {
Tommi97888bd2016-01-21 23:24:59 +0100532 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000533 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100534 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000535 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100536 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000537
Per83d09102016-04-15 14:59:13 +0200538 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000539
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000540 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200541 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000542 if (rtt == 0) {
543 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100544 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000545 }
546 uint32_t ntp_secs = 0;
547 uint32_t ntp_frac = 0;
548 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200549 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
550 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000551 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100552 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000553 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100554 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000555
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100556 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000557}
558
philipelfd5a20f2016-11-15 00:57:57 -0800559void RtpStreamReceiver::FrameContinuous(uint16_t picture_id) {
kjellander04926b82017-01-19 00:06:17 -0800560 if (jitter_buffer_experiment_) {
561 int seq_num = -1;
562 {
563 rtc::CritScope lock(&last_seq_num_cs_);
564 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
565 if (seq_num_it != last_seq_num_for_pic_id_.end())
566 seq_num = seq_num_it->second;
567 }
568 if (seq_num != -1)
569 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800570 }
571}
572
573void RtpStreamReceiver::FrameDecoded(uint16_t picture_id) {
kjellander04926b82017-01-19 00:06:17 -0800574 if (jitter_buffer_experiment_) {
575 int seq_num = -1;
576 {
577 rtc::CritScope lock(&last_seq_num_cs_);
578 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
579 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
580 seq_num = seq_num_it->second;
581 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
582 ++seq_num_it);
583 }
philipelfd5a20f2016-11-15 00:57:57 -0800584 }
kjellander04926b82017-01-19 00:06:17 -0800585 if (seq_num != -1) {
586 packet_buffer_->ClearTo(seq_num);
587 reference_finder_->ClearTo(seq_num);
588 }
philipelfd5a20f2016-11-15 00:57:57 -0800589 }
590}
591
mflodmandc7d0d22016-05-06 05:32:22 -0700592void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
593 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
594 : RtcpMode::kOff);
595}
596
mflodmanfa666592016-04-28 23:15:33 -0700597void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100598 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000599 receiving_ = true;
600}
601
mflodmanfa666592016-04-28 23:15:33 -0700602void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100603 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000604 receiving_ = false;
605}
606
mflodmanfa666592016-04-28 23:15:33 -0700607bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000608 StreamStatistician* statistician =
609 rtp_receive_statistics_->GetStatistician(header.ssrc);
610 if (!statistician)
611 return false;
612 return statistician->IsPacketInOrder(header.sequenceNumber);
613}
614
mflodmanfa666592016-04-28 23:15:33 -0700615bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
616 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000617 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100618 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000619 return false;
620 StreamStatistician* statistician =
621 rtp_receive_statistics_->GetStatistician(header.ssrc);
622 if (!statistician)
623 return false;
624 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000625 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200626 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000627 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000628 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000629}
mflodmandc7d0d22016-05-06 05:32:22 -0700630
631void RtpStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700632 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800633 if (counter.first_packet_time_ms == -1)
634 return;
635
636 int64_t elapsed_sec =
637 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
638 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
639 return;
640
mflodmandc7d0d22016-05-06 05:32:22 -0700641 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700642 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700643 "WebRTC.Video.ReceivedFecPacketsInPercent",
644 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
645 }
646 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700647 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
648 static_cast<int>(counter.num_recovered_packets *
649 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700650 }
651}
652
653void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
654 const std::string& extension, int id) {
655 // One-byte-extension local identifiers are in the range 1-14 inclusive.
656 RTC_DCHECK_GE(id, 1);
657 RTC_DCHECK_LE(id, 14);
658 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
659 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
660 StringToRtpExtensionType(extension), id));
661}
662
philipel022b54e2016-12-20 04:15:59 -0800663void RtpStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
664 auto codec_params_it = pt_codec_params_.find(payload_type);
665 if (codec_params_it == pt_codec_params_.end())
666 return;
667
668 LOG(LS_INFO) << "Found out of band supplied codec parameters for"
johan62d02c32017-01-24 04:38:27 -0800669 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800670
671 H264SpropParameterSets sprop_decoder;
672 auto sprop_base64_it =
673 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
674
675 if (sprop_base64_it == codec_params_it->second.end())
676 return;
677
johan62d02c32017-01-24 04:38:27 -0800678 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800679 return;
680
johand2b092f2017-01-24 02:38:17 -0800681 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
682 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800683}
684
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000685} // namespace webrtc