blob: 499ee0953e69ed90e6f2de6d1d14af47d79e5ace [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"
tommidea489f2017-03-03 03:20:24 -080017#include "webrtc/base/location.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010018#include "webrtc/base/logging.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070019#include "webrtc/common_types.h"
Peter Boström9c017252016-02-26 16:26:20 +010020#include "webrtc/config.h"
philipel022b54e2016-12-20 04:15:59 -080021#include "webrtc/media/base/mediaconstants.h"
mflodmanc0e58a32016-04-25 01:26:26 -070022#include "webrtc/modules/pacing/packet_router.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000023#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010024#include "webrtc/modules/rtp_rtcp/include/receive_statistics.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010025#include "webrtc/modules/rtp_rtcp/include/rtp_cvo.h"
26#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010027#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
28#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
brandtrd55c3f62016-10-31 04:51:33 -070029#include "webrtc/modules/rtp_rtcp/include/ulpfec_receiver.h"
nisse38cc1d62017-02-13 05:59:46 -080030#include "webrtc/modules/rtp_rtcp/source/rtp_header_extensions.h"
31#include "webrtc/modules/rtp_rtcp/source/rtp_packet_received.h"
philipelfd5a20f2016-11-15 00:57:57 -080032#include "webrtc/modules/video_coding/frame_object.h"
philipel022b54e2016-12-20 04:15:59 -080033#include "webrtc/modules/video_coding/h264_sprop_parameter_sets.h"
philipelfd5a20f2016-11-15 00:57:57 -080034#include "webrtc/modules/video_coding/h264_sps_pps_tracker.h"
35#include "webrtc/modules/video_coding/packet_buffer.h"
Peter Boström0b250722016-04-22 18:23:15 +020036#include "webrtc/modules/video_coding/video_coding_impl.h"
philipelfd5a20f2016-11-15 00:57:57 -080037#include "webrtc/system_wrappers/include/field_trial.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010038#include "webrtc/system_wrappers/include/metrics.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010039#include "webrtc/system_wrappers/include/timestamp_extrapolator.h"
40#include "webrtc/system_wrappers/include/trace.h"
mflodmancfc8e3b2016-05-03 21:22:04 -070041#include "webrtc/video/receive_statistics_proxy.h"
mflodmandc7d0d22016-05-06 05:32:22 -070042#include "webrtc/video/vie_remb.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000043
44namespace webrtc {
45
philipelfd5a20f2016-11-15 00:57:57 -080046namespace {
47constexpr int kPacketBufferStartSize = 32;
48constexpr int kPacketBufferMaxSixe = 2048;
49}
50
mflodmanc0e58a32016-04-25 01:26:26 -070051std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
52 ReceiveStatistics* receive_statistics,
53 Transport* outgoing_transport,
54 RtcpRttStats* rtt_stats,
55 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
nisse15389c02017-01-24 02:36:58 -080056 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
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;
mflodmanc0e58a32016-04-25 01:26:26 -070066 configuration.transport_sequence_number_allocator =
67 transport_sequence_number_allocator;
68 configuration.send_bitrate_observer = nullptr;
69 configuration.send_frame_count_observer = nullptr;
70 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070071 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070072 configuration.bandwidth_callback = nullptr;
73 configuration.transport_feedback_callback = nullptr;
74
75 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
mflodmanc0e58a32016-04-25 01:26:26 -070076 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(
mflodmanfa666592016-04-28 23:15:33 -070084 Transport* transport,
85 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070086 PacketRouter* packet_router,
mflodmandc7d0d22016-05-06 05:32:22 -070087 VieRemb* remb,
Tommi733b5472016-06-10 17:58:01 +020088 const VideoReceiveStream::Config* config,
mflodmandc7d0d22016-05-06 05:32:22 -070089 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020090 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080091 NackSender* nack_sender,
92 KeyFrameRequestSender* keyframe_request_sender,
93 video_coding::OnCompleteFrameCallback* complete_frame_callback,
94 VCMTiming* timing)
Tommi97888bd2016-01-21 23:24:59 +010095 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020096 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -070097 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070098 remb_(remb),
99 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100100 ntp_estimator_(clock_),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100101 rtp_header_parser_(RtpHeaderParser::Create()),
102 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
103 this,
mflodmanfa666592016-04-28 23:15:33 -0700104 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100105 &rtp_payload_registry_)),
106 rtp_receive_statistics_(ReceiveStatistics::Create(clock_)),
brandtrd55c3f62016-10-31 04:51:33 -0700107 ulpfec_receiver_(UlpfecReceiver::Create(this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000108 receiving_(false),
solenberg@webrtc.org3fb8f7b2014-03-24 20:28:11 +0000109 restored_packet_in_use_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700110 last_packet_log_ms_(-1),
111 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_.get(),
112 transport,
113 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700114 receive_stats_proxy,
nisse15389c02017-01-24 02:36:58 -0800115 packet_router)),
philipelfd5a20f2016-11-15 00:57:57 -0800116 complete_frame_callback_(complete_frame_callback),
117 keyframe_request_sender_(keyframe_request_sender),
118 timing_(timing) {
mflodmanc0e58a32016-04-25 01:26:26 -0700119 packet_router_->AddRtpModule(rtp_rtcp_.get());
mflodmancfc8e3b2016-05-03 21:22:04 -0700120 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
121 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
122
Tommi733b5472016-06-10 17:58:01 +0200123 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700124 << "A stream should not be configured with RTCP disabled. This value is "
125 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700126 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
127 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
128 RTC_DCHECK(config_.rtp.local_ssrc != 0);
129 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
130
Tommi733b5472016-06-10 17:58:01 +0200131 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
132 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700133 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
Tommi733b5472016-06-10 17:58:01 +0200134 if (config_.rtp.remb) {
mflodmandc7d0d22016-05-06 05:32:22 -0700135 rtp_rtcp_->SetREMBStatus(true);
136 remb_->AddReceiveChannel(rtp_rtcp_.get());
137 }
138
Tommi733b5472016-06-10 17:58:01 +0200139 for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) {
140 EnableReceiveRtpHeaderExtension(config_.rtp.extensions[i].uri,
141 config_.rtp.extensions[i].id);
mflodmandc7d0d22016-05-06 05:32:22 -0700142 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700143
144 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200145 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
146 ? kMaxPacketAgeToNack
147 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700148 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700149
brandtr14742122017-01-27 04:53:07 -0800150 if (config_.rtp.rtx_ssrc) {
151 rtp_payload_registry_.SetRtxSsrc(config_.rtp.rtx_ssrc);
mflodmandc7d0d22016-05-06 05:32:22 -0700152
brandtr14742122017-01-27 04:53:07 -0800153 for (const auto& kv : config_.rtp.rtx_payload_types) {
154 RTC_DCHECK(kv.second != 0);
155 rtp_payload_registry_.SetRtxPayloadType(kv.second, kv.first);
156 }
mflodmandc7d0d22016-05-06 05:32:22 -0700157 }
158
brandtrf7c6d722016-12-08 08:25:47 -0800159 if (IsUlpfecEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700160 VideoCodec ulpfec_codec = {};
161 ulpfec_codec.codecType = kVideoCodecULPFEC;
162 strncpy(ulpfec_codec.plName, "ulpfec", sizeof(ulpfec_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700163 ulpfec_codec.plType = config_.rtp.ulpfec.ulpfec_payload_type;
johan07e276c2016-12-13 02:23:03 -0800164 RTC_CHECK(AddReceiveCodec(ulpfec_codec));
brandtre6f98c72016-11-11 03:28:30 -0800165 }
mflodmandc7d0d22016-05-06 05:32:22 -0700166
brandtre6f98c72016-11-11 03:28:30 -0800167 if (IsRedEnabled()) {
mflodmandc7d0d22016-05-06 05:32:22 -0700168 VideoCodec red_codec = {};
169 red_codec.codecType = kVideoCodecRED;
170 strncpy(red_codec.plName, "red", sizeof(red_codec.plName));
brandtrb5f2c3f2016-10-04 23:28:39 -0700171 red_codec.plType = config_.rtp.ulpfec.red_payload_type;
johan07e276c2016-12-13 02:23:03 -0800172 RTC_CHECK(AddReceiveCodec(red_codec));
brandtrb5f2c3f2016-10-04 23:28:39 -0700173 if (config_.rtp.ulpfec.red_rtx_payload_type != -1) {
mflodmandc7d0d22016-05-06 05:32:22 -0700174 rtp_payload_registry_.SetRtxPayloadType(
brandtrb5f2c3f2016-10-04 23:28:39 -0700175 config_.rtp.ulpfec.red_rtx_payload_type,
176 config_.rtp.ulpfec.red_payload_type);
mflodmandc7d0d22016-05-06 05:32:22 -0700177 }
178 }
179
Tommi733b5472016-06-10 17:58:01 +0200180 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700181 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
182
183 // Stats callback for CNAME changes.
184 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
185
tommidea489f2017-03-03 03:20:24 -0800186 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800187
tommif284b7f2017-02-27 01:59:36 -0800188 if (config_.rtp.nack.rtp_history_ms != 0) {
189 nack_module_.reset(
190 new NackModule(clock_, nack_sender, keyframe_request_sender));
tommidea489f2017-03-03 03:20:24 -0800191 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800192 }
philipelfd5a20f2016-11-15 00:57:57 -0800193
philipela45102f2017-02-22 05:30:39 -0800194 packet_buffer_ = video_coding::PacketBuffer::Create(
195 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
196 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
mflodmanc0e58a32016-04-25 01:26:26 -0700197}
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
mflodmanfa666592016-04-28 23:15:33 -0700199RtpStreamReceiver::~RtpStreamReceiver() {
tommif284b7f2017-02-27 01:59:36 -0800200 if (nack_module_) {
201 process_thread_->DeRegisterModule(nack_module_.get());
202 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
tommif284b7f2017-02-27 01:59:36 -0800204 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800205
mflodmandc7d0d22016-05-06 05:32:22 -0700206 packet_router_->RemoveRtpModule(rtp_rtcp_.get());
207 rtp_rtcp_->SetREMBStatus(false);
johan62d02c32017-01-24 04:38:27 -0800208 if (config_.rtp.remb) {
209 remb_->RemoveReceiveChannel(rtp_rtcp_.get());
210 }
mflodmandc7d0d22016-05-06 05:32:22 -0700211 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000212}
213
philipel022b54e2016-12-20 04:15:59 -0800214bool RtpStreamReceiver::AddReceiveCodec(
215 const VideoCodec& video_codec,
216 const std::map<std::string, std::string>& codec_params) {
217 pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
218 return AddReceiveCodec(video_codec);
219}
220
johan07e276c2016-12-13 02:23:03 -0800221bool RtpStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000222 int8_t old_pltype = -1;
magjed56124bd2016-11-24 09:34:46 -0800223 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
224 -1) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100225 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000226 }
magjed56124bd2016-11-24 09:34:46 -0800227 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000228}
229
mflodmanfa666592016-04-28 23:15:33 -0700230uint32_t RtpStreamReceiver::GetRemoteSsrc() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000231 return rtp_receiver_->SSRC();
232}
233
mflodmanfa666592016-04-28 23:15:33 -0700234int RtpStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000235 return rtp_receiver_->CSRCs(csrcs);
236}
237
mflodmanfa666592016-04-28 23:15:33 -0700238RtpReceiver* RtpStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000239 return rtp_receiver_.get();
240}
241
mflodmanfa666592016-04-28 23:15:33 -0700242int32_t RtpStreamReceiver::OnReceivedPayloadData(
243 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200244 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700245 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000246 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000247 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100248 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipela45102f2017-02-22 05:30:39 -0800249 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
tommif284b7f2017-02-27 01:59:36 -0800250 packet.timesNacked =
251 nack_module_ ? nack_module_->OnReceivedPacket(packet) : -1;
philipelfd5a20f2016-11-15 00:57:57 -0800252
philipel54ca9192017-03-21 05:45:18 -0700253 // In the case of a video stream without picture ids and no rtx the
254 // RtpFrameReferenceFinder will need to know about padding to
255 // correctly calculate frame references.
256 if (packet.sizeBytes == 0) {
257 reference_finder_->PaddingReceived(packet.seqNum);
258 return 0;
259 }
260
philipela45102f2017-02-22 05:30:39 -0800261 if (packet.codec == kVideoCodecH264) {
262 // Only when we start to receive packets will we know what payload type
263 // that will be used. When we know the payload type insert the correct
264 // sps/pps into the tracker.
265 if (packet.payloadType != last_payload_type_) {
266 last_payload_type_ = packet.payloadType;
267 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800268 }
269
philipela45102f2017-02-22 05:30:39 -0800270 switch (tracker_.CopyAndFixBitstream(&packet)) {
271 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
272 keyframe_request_sender_->RequestKeyFrame();
273 FALLTHROUGH();
274 case video_coding::H264SpsPpsTracker::kDrop:
275 return 0;
276 case video_coding::H264SpsPpsTracker::kInsert:
277 break;
278 }
279
philipelfd5a20f2016-11-15 00:57:57 -0800280 } else {
philipela45102f2017-02-22 05:30:39 -0800281 uint8_t* data = new uint8_t[packet.sizeBytes];
282 memcpy(data, packet.dataPtr, packet.sizeBytes);
283 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000284 }
philipela45102f2017-02-22 05:30:39 -0800285
286 packet_buffer_->InsertPacket(&packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000287 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000288}
289
mflodmanfa666592016-04-28 23:15:33 -0700290bool RtpStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
291 size_t rtp_packet_length) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000292 RTPHeader header;
293 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000294 return false;
295 }
296 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org01581da2014-09-04 06:48:14 +0000297 bool in_order = IsPacketInOrder(header);
298 return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000299}
300
mflodmanfa666592016-04-28 23:15:33 -0700301// TODO(pbos): Remove as soon as audio can handle a changing payload type
302// without this callback.
303int32_t RtpStreamReceiver::OnInitializeDecoder(
304 const int8_t payload_type,
305 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
306 const int frequency,
307 const size_t channels,
308 const uint32_t rate) {
309 RTC_NOTREACHED();
310 return 0;
311}
312
313void RtpStreamReceiver::OnIncomingSSRCChanged(const uint32_t ssrc) {
314 rtp_rtcp_->SetRemoteSSRC(ssrc);
315}
316
nisse38cc1d62017-02-13 05:59:46 -0800317void RtpStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000318 {
Tommi97888bd2016-01-21 23:24:59 +0100319 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000320 if (!receiving_) {
nisse38cc1d62017-02-13 05:59:46 -0800321 return;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000322 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000323 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000324
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000325 int64_t now_ms = clock_->TimeInMilliseconds();
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000326
327 {
328 // Periodically log the RTP header of incoming packets.
Tommi97888bd2016-01-21 23:24:59 +0100329 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000330 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
331 std::stringstream ss;
nisse38cc1d62017-02-13 05:59:46 -0800332 ss << "Packet received on SSRC: " << packet.Ssrc()
333 << " with payload type: " << static_cast<int>(packet.PayloadType())
334 << ", timestamp: " << packet.Timestamp()
335 << ", sequence number: " << packet.SequenceNumber()
336 << ", arrival time: " << packet.arrival_time_ms();
337 int32_t time_offset;
338 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
339 ss << ", toffset: " << time_offset;
340 }
341 uint32_t send_time;
342 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
343 ss << ", abs send time: " << send_time;
344 }
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000345 LOG(LS_INFO) << ss.str();
346 last_packet_log_ms_ = now_ms;
347 }
348 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000349
nisse38cc1d62017-02-13 05:59:46 -0800350 // TODO(nisse): Delete use of GetHeader, but needs refactoring of
351 // ReceivePacket and IncomingPacket methods below.
352 RTPHeader header;
353 packet.GetHeader(&header);
354
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000355 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000356
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000357 bool in_order = IsPacketInOrder(header);
Peter Boström4fa7eca2016-03-02 15:05:53 +0100358 rtp_payload_registry_.SetIncomingPayloadType(header);
nisse38cc1d62017-02-13 05:59:46 -0800359 ReceivePacket(packet.data(), packet.size(), header, in_order);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000360 // Update receive statistics after ReceivePacket.
361 // Receive statistics will be reset if the payload type changes (make sure
362 // that the first packet is included in the stats).
363 rtp_receive_statistics_->IncomingPacket(
nisse38cc1d62017-02-13 05:59:46 -0800364 header, packet.size(), IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000365}
366
mflodmancfc8e3b2016-05-03 21:22:04 -0700367int32_t RtpStreamReceiver::RequestKeyFrame() {
368 return rtp_rtcp_->RequestKeyFrame();
369}
370
brandtrf7c6d722016-12-08 08:25:47 -0800371bool RtpStreamReceiver::IsUlpfecEnabled() const {
brandtre6f98c72016-11-11 03:28:30 -0800372 return config_.rtp.ulpfec.ulpfec_payload_type != -1;
373}
374
375bool RtpStreamReceiver::IsRedEnabled() const {
376 return config_.rtp.ulpfec.red_payload_type != -1;
mflodmandc7d0d22016-05-06 05:32:22 -0700377}
378
379bool RtpStreamReceiver::IsRetransmissionsEnabled() const {
380 return config_.rtp.nack.rtp_history_ms > 0;
381}
382
383void RtpStreamReceiver::RequestPacketRetransmit(
384 const std::vector<uint16_t>& sequence_numbers) {
385 rtp_rtcp_->SendNack(sequence_numbers);
386}
387
mflodmancfc8e3b2016-05-03 21:22:04 -0700388int32_t RtpStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
389 uint16_t length) {
390 return rtp_rtcp_->SendNACK(sequence_numbers, length);
391}
392
philipelfd5a20f2016-11-15 00:57:57 -0800393void RtpStreamReceiver::OnReceivedFrame(
394 std::unique_ptr<video_coding::RtpFrameObject> frame) {
philipelbd26ba72017-01-29 04:04:47 -0800395 if (!frame->delayed_by_retransmission())
396 timing_->IncomingTimestamp(frame->timestamp, clock_->TimeInMilliseconds());
philipelfd5a20f2016-11-15 00:57:57 -0800397 reference_finder_->ManageFrame(std::move(frame));
398}
399
400void RtpStreamReceiver::OnCompleteFrame(
401 std::unique_ptr<video_coding::FrameObject> frame) {
402 {
403 rtc::CritScope lock(&last_seq_num_cs_);
404 video_coding::RtpFrameObject* rtp_frame =
405 static_cast<video_coding::RtpFrameObject*>(frame.get());
406 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
407 }
408 complete_frame_callback_->OnCompleteFrame(std::move(frame));
409}
410
411void RtpStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800412 if (nack_module_)
413 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800414}
415
nisse38cc1d62017-02-13 05:59:46 -0800416// TODO(nisse): Drop return value.
mflodmanfa666592016-04-28 23:15:33 -0700417bool RtpStreamReceiver::ReceivePacket(const uint8_t* packet,
418 size_t packet_length,
419 const RTPHeader& header,
420 bool in_order) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100421 if (rtp_payload_registry_.IsEncapsulated(header)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000422 return ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
423 }
424 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000425 assert(packet_length >= header.headerLength);
426 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000427 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100428 if (!rtp_payload_registry_.GetPayloadSpecifics(header.payloadType,
429 &payload_specific)) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000430 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000431 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000432 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
433 payload_specific, in_order);
434}
435
mflodmanfa666592016-04-28 23:15:33 -0700436bool RtpStreamReceiver::ParseAndHandleEncapsulatingHeader(
437 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100438 if (rtp_payload_registry_.IsRed(header)) {
439 int8_t ulpfec_pt = rtp_payload_registry_.ulpfec_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000440 if (packet[header.headerLength] == ulpfec_pt) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000441 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200442 // Notify video_receiver about received FEC packets to avoid NACKing these
443 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000444 NotifyReceiverOfFecPacket(header);
445 }
brandtrd55c3f62016-10-31 04:51:33 -0700446 if (ulpfec_receiver_->AddReceivedRedPacket(header, packet, packet_length,
447 ulpfec_pt) != 0) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000448 return false;
449 }
brandtrd55c3f62016-10-31 04:51:33 -0700450 return ulpfec_receiver_->ProcessReceivedFec() == 0;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100451 } else if (rtp_payload_registry_.IsRtx(header)) {
stefan@webrtc.org7c6ff2d2014-03-19 18:14:52 +0000452 if (header.headerLength + header.paddingLength == packet_length) {
453 // This is an empty packet and should be silently dropped before trying to
454 // parse the RTX header.
455 return true;
456 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000457 // Remove the RTX header and parse the original RTP header.
458 if (packet_length < header.headerLength)
459 return false;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000460 if (packet_length > sizeof(restored_packet_))
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000461 return false;
Tommi97888bd2016-01-21 23:24:59 +0100462 rtc::CritScope lock(&receive_cs_);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000463 if (restored_packet_in_use_) {
pbos@webrtc.org4e2806d2014-05-14 08:02:22 +0000464 LOG(LS_WARNING) << "Multiple RTX headers detected, dropping packet.";
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000465 return false;
466 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100467 if (!rtp_payload_registry_.RestoreOriginalPacket(
noahric65220a72015-10-14 11:29:49 -0700468 restored_packet_, packet, &packet_length, rtp_receiver_->SSRC(),
469 header)) {
Stefan Holmer10880012016-02-03 13:29:59 +0100470 LOG(LS_WARNING) << "Incoming RTX packet: Invalid RTP header ssrc: "
471 << header.ssrc << " payload type: "
472 << static_cast<int>(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000473 return false;
474 }
475 restored_packet_in_use_ = true;
noahric65220a72015-10-14 11:29:49 -0700476 bool ret = OnRecoveredPacket(restored_packet_, packet_length);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000477 restored_packet_in_use_ = false;
478 return ret;
479 }
480 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
mflodmanfa666592016-04-28 23:15:33 -0700483void RtpStreamReceiver::NotifyReceiverOfFecPacket(const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000484 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100485 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000486 if (last_media_payload_type < 0) {
487 LOG(LS_WARNING) << "Failed to get last media payload type.";
488 return;
489 }
490 // Fake an empty media packet.
491 WebRtcRTPHeader rtp_header = {};
492 rtp_header.header = header;
493 rtp_header.header.payloadType = last_media_payload_type;
494 rtp_header.header.paddingLength = 0;
495 PayloadUnion payload_specific;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100496 if (!rtp_payload_registry_.GetPayloadSpecifics(last_media_payload_type,
497 &payload_specific)) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000498 LOG(LS_WARNING) << "Failed to get payload specifics.";
499 return;
500 }
501 rtp_header.type.Video.codec = payload_specific.Video.videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000502 rtp_header.type.Video.rotation = kVideoRotation_0;
503 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700504 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000505 }
isheriff6b4b5f32016-06-08 00:24:21 -0700506 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
507
Peter Boström74f6e9e2016-04-04 17:56:10 +0200508 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000509}
510
mflodmanfa666592016-04-28 23:15:33 -0700511bool RtpStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
512 size_t rtcp_packet_length) {
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000513 {
Tommi97888bd2016-01-21 23:24:59 +0100514 rtc::CritScope lock(&receive_cs_);
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000515 if (!receiving_) {
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100516 return false;
braveyao@webrtc.orgb6433b72013-07-26 09:02:46 +0000517 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100518 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
Per83d09102016-04-15 14:59:13 +0200520 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000521
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000522 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200523 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000524 if (rtt == 0) {
525 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100526 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000527 }
528 uint32_t ntp_secs = 0;
529 uint32_t ntp_frac = 0;
530 uint32_t rtp_timestamp = 0;
Per83d09102016-04-15 14:59:13 +0200531 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, nullptr, nullptr,
532 &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000533 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100534 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000535 }
Peter Boström4fa7eca2016-03-02 15:05:53 +0100536 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000537
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100538 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000539}
540
philipelfd5a20f2016-11-15 00:57:57 -0800541void RtpStreamReceiver::FrameContinuous(uint16_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800542 if (!nack_module_)
543 return;
544
philipela45102f2017-02-22 05:30:39 -0800545 int seq_num = -1;
546 {
547 rtc::CritScope lock(&last_seq_num_cs_);
548 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
549 if (seq_num_it != last_seq_num_for_pic_id_.end())
550 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800551 }
philipela45102f2017-02-22 05:30:39 -0800552 if (seq_num != -1)
553 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800554}
555
556void RtpStreamReceiver::FrameDecoded(uint16_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800557 int seq_num = -1;
558 {
559 rtc::CritScope lock(&last_seq_num_cs_);
560 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
561 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
562 seq_num = seq_num_it->second;
563 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
564 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800565 }
philipela45102f2017-02-22 05:30:39 -0800566 }
567 if (seq_num != -1) {
568 packet_buffer_->ClearTo(seq_num);
569 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800570 }
571}
572
mflodmandc7d0d22016-05-06 05:32:22 -0700573void RtpStreamReceiver::SignalNetworkState(NetworkState state) {
574 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
575 : RtcpMode::kOff);
576}
577
mflodmanfa666592016-04-28 23:15:33 -0700578void RtpStreamReceiver::StartReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100579 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000580 receiving_ = true;
581}
582
mflodmanfa666592016-04-28 23:15:33 -0700583void RtpStreamReceiver::StopReceive() {
Tommi97888bd2016-01-21 23:24:59 +0100584 rtc::CritScope lock(&receive_cs_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000585 receiving_ = false;
586}
587
mflodmanfa666592016-04-28 23:15:33 -0700588bool RtpStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000589 StreamStatistician* statistician =
590 rtp_receive_statistics_->GetStatistician(header.ssrc);
591 if (!statistician)
592 return false;
593 return statistician->IsPacketInOrder(header.sequenceNumber);
594}
595
mflodmanfa666592016-04-28 23:15:33 -0700596bool RtpStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
597 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000598 // Retransmissions are handled separately if RTX is enabled.
Peter Boström4fa7eca2016-03-02 15:05:53 +0100599 if (rtp_payload_registry_.RtxEnabled())
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000600 return false;
601 StreamStatistician* statistician =
602 rtp_receive_statistics_->GetStatistician(header.ssrc);
603 if (!statistician)
604 return false;
605 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000606 int64_t min_rtt = 0;
Per83d09102016-04-15 14:59:13 +0200607 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000608 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000609 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000610}
mflodmandc7d0d22016-05-06 05:32:22 -0700611
612void RtpStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700613 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800614 if (counter.first_packet_time_ms == -1)
615 return;
616
617 int64_t elapsed_sec =
618 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
619 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
620 return;
621
mflodmandc7d0d22016-05-06 05:32:22 -0700622 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700623 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700624 "WebRTC.Video.ReceivedFecPacketsInPercent",
625 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
626 }
627 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700628 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
629 static_cast<int>(counter.num_recovered_packets *
630 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700631 }
632}
633
634void RtpStreamReceiver::EnableReceiveRtpHeaderExtension(
635 const std::string& extension, int id) {
636 // One-byte-extension local identifiers are in the range 1-14 inclusive.
637 RTC_DCHECK_GE(id, 1);
638 RTC_DCHECK_LE(id, 14);
639 RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension));
640 RTC_CHECK(rtp_header_parser_->RegisterRtpHeaderExtension(
641 StringToRtpExtensionType(extension), id));
642}
643
philipel022b54e2016-12-20 04:15:59 -0800644void RtpStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
645 auto codec_params_it = pt_codec_params_.find(payload_type);
646 if (codec_params_it == pt_codec_params_.end())
647 return;
648
649 LOG(LS_INFO) << "Found out of band supplied codec parameters for"
johan62d02c32017-01-24 04:38:27 -0800650 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800651
652 H264SpropParameterSets sprop_decoder;
653 auto sprop_base64_it =
654 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
655
656 if (sprop_base64_it == codec_params_it->second.end())
657 return;
658
johan62d02c32017-01-24 04:38:27 -0800659 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800660 return;
661
johand2b092f2017-01-24 02:38:17 -0800662 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
663 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800664}
665
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000666} // namespace webrtc