blob: 448559f060b3da576d892654f7000096e779df1b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "video/rtp_video_stream_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
eladalonc0d481a2017-08-02 07:39:07 -070013#include <algorithm>
philipelfd5a20f2016-11-15 00:57:57 -080014#include <utility>
ilnik04f4d122017-06-19 07:18:55 -070015#include <vector>
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000016
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "media/base/mediaconstants.h"
19#include "modules/pacing/packet_router.h"
20#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
21#include "modules/rtp_rtcp/include/receive_statistics.h"
22#include "modules/rtp_rtcp/include/rtp_cvo.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/rtp_rtcp/include/rtp_receiver.h"
24#include "modules/rtp_rtcp/include/rtp_rtcp.h"
25#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
26#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
27#include "modules/rtp_rtcp/source/rtp_packet_received.h"
28#include "modules/video_coding/frame_object.h"
29#include "modules/video_coding/h264_sprop_parameter_sets.h"
30#include "modules/video_coding/h264_sps_pps_tracker.h"
31#include "modules/video_coding/packet_buffer.h"
32#include "modules/video_coding/video_coding_impl.h"
33#include "rtc_base/checks.h"
34#include "rtc_base/location.h"
35#include "rtc_base/logging.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010036#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "system_wrappers/include/field_trial.h"
38#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "video/receive_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000040
41namespace webrtc {
42
philipelfd5a20f2016-11-15 00:57:57 -080043namespace {
philipel3bf97cf2017-08-10 18:10:59 +020044// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
45// crbug.com/752886
46constexpr int kPacketBufferStartSize = 512;
philipelfd5a20f2016-11-15 00:57:57 -080047constexpr int kPacketBufferMaxSixe = 2048;
48}
49
mflodmanc0e58a32016-04-25 01:26:26 -070050std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
51 ReceiveStatistics* receive_statistics,
52 Transport* outgoing_transport,
53 RtcpRttStats* rtt_stats,
54 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
nisse15389c02017-01-24 02:36:58 -080055 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
mflodmanc0e58a32016-04-25 01:26:26 -070056 RtpRtcp::Configuration configuration;
57 configuration.audio = false;
58 configuration.receiver_only = true;
59 configuration.receive_statistics = receive_statistics;
60 configuration.outgoing_transport = outgoing_transport;
61 configuration.intra_frame_callback = nullptr;
62 configuration.rtt_stats = rtt_stats;
63 configuration.rtcp_packet_type_counter_observer =
64 rtcp_packet_type_counter_observer;
mflodmanc0e58a32016-04-25 01:26:26 -070065 configuration.transport_sequence_number_allocator =
66 transport_sequence_number_allocator;
67 configuration.send_bitrate_observer = nullptr;
68 configuration.send_frame_count_observer = nullptr;
69 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070070 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070071 configuration.bandwidth_callback = nullptr;
72 configuration.transport_feedback_callback = nullptr;
73
74 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
mflodmanc0e58a32016-04-25 01:26:26 -070075 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
76
77 return rtp_rtcp;
78}
79
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000080static const int kPacketLogIntervalMs = 10000;
81
nisseb1f2ff92017-06-09 04:01:55 -070082RtpVideoStreamReceiver::RtpVideoStreamReceiver(
mflodmanfa666592016-04-28 23:15:33 -070083 Transport* transport,
84 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070085 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020086 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070087 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -070088 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020089 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080090 NackSender* nack_sender,
91 KeyFrameRequestSender* keyframe_request_sender,
philipel0a9f6de2018-02-28 11:29:47 +010092 video_coding::OnCompleteFrameCallback* complete_frame_callback)
Tommi97888bd2016-01-21 23:24:59 +010093 : clock_(Clock::GetRealTimeClock()),
Tommi733b5472016-06-10 17:58:01 +020094 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -070095 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -070096 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010097 ntp_estimator_(clock_),
Niels Möllerb0573bc2017-09-25 10:47:00 +020098 rtp_header_extensions_(config_.rtp.extensions),
Peter Boström4fa7eca2016-03-02 15:05:53 +010099 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
100 this,
mflodmanfa666592016-04-28 23:15:33 -0700101 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100102 &rtp_payload_registry_)),
nisseca5706d2017-09-11 02:32:16 -0700103 rtp_receive_statistics_(rtp_receive_statistics),
brandtrd726a3f2017-06-29 02:45:35 -0700104 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000105 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700106 last_packet_log_ms_(-1),
nisseca5706d2017-09-11 02:32:16 -0700107 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_,
mflodmanc0e58a32016-04-25 01:26:26 -0700108 transport,
109 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700110 receive_stats_proxy,
nisse15389c02017-01-24 02:36:58 -0800111 packet_router)),
philipelfd5a20f2016-11-15 00:57:57 -0800112 complete_frame_callback_(complete_frame_callback),
113 keyframe_request_sender_(keyframe_request_sender),
philipel2c53b132017-05-16 08:06:30 -0700114 has_received_frame_(false) {
eladalon822ff2b2017-08-01 06:30:28 -0700115 constexpr bool remb_candidate = true;
116 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700117 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
118 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
119
Tommi733b5472016-06-10 17:58:01 +0200120 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700121 << "A stream should not be configured with RTCP disabled. This value is "
122 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700123 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
124 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
125 RTC_DCHECK(config_.rtp.local_ssrc != 0);
126 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
127
Tommi733b5472016-06-10 17:58:01 +0200128 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
129 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
stefanb4ab3812017-06-09 06:12:11 -0700130 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700131 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodmandc7d0d22016-05-06 05:32:22 -0700132
mflodmancfc8e3b2016-05-03 21:22:04 -0700133 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200134 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
135 ? kMaxPacketAgeToNack
136 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700137 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700138
Tommi733b5472016-06-10 17:58:01 +0200139 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700140 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
141
142 // Stats callback for CNAME changes.
143 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
144
tommidea489f2017-03-03 03:20:24 -0800145 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800146
tommif284b7f2017-02-27 01:59:36 -0800147 if (config_.rtp.nack.rtp_history_ms != 0) {
148 nack_module_.reset(
149 new NackModule(clock_, nack_sender, keyframe_request_sender));
tommidea489f2017-03-03 03:20:24 -0800150 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800151 }
philipelfd5a20f2016-11-15 00:57:57 -0800152
philipela45102f2017-02-22 05:30:39 -0800153 packet_buffer_ = video_coding::PacketBuffer::Create(
154 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
155 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
mflodmanc0e58a32016-04-25 01:26:26 -0700156}
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
nisseb1f2ff92017-06-09 04:01:55 -0700158RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700159 RTC_DCHECK(secondary_sinks_.empty());
160
tommif284b7f2017-02-27 01:59:36 -0800161 if (nack_module_) {
162 process_thread_->DeRegisterModule(nack_module_.get());
163 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
tommif284b7f2017-02-27 01:59:36 -0800165 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800166
nissefdbfdc92017-03-31 05:44:52 -0700167 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700168 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000169}
170
nisseb1f2ff92017-06-09 04:01:55 -0700171bool RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800172 const VideoCodec& video_codec,
173 const std::map<std::string, std::string>& codec_params) {
174 pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
175 return AddReceiveCodec(video_codec);
176}
177
nisseb1f2ff92017-06-09 04:01:55 -0700178bool RtpVideoStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000179 int8_t old_pltype = -1;
magjed56124bd2016-11-24 09:34:46 -0800180 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
181 -1) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100182 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000183 }
magjed56124bd2016-11-24 09:34:46 -0800184 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185}
186
nisseb1f2ff92017-06-09 04:01:55 -0700187uint32_t RtpVideoStreamReceiver::GetRemoteSsrc() const {
stefanb4ab3812017-06-09 06:12:11 -0700188 return config_.rtp.remote_ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000189}
190
nisseb1f2ff92017-06-09 04:01:55 -0700191int RtpVideoStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000192 return rtp_receiver_->CSRCs(csrcs);
193}
194
nisseb1f2ff92017-06-09 04:01:55 -0700195RtpReceiver* RtpVideoStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000196 return rtp_receiver_.get();
197}
198
nisseb1f2ff92017-06-09 04:01:55 -0700199int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmanfa666592016-04-28 23:15:33 -0700200 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200201 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700202 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000203 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000204 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100205 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipela45102f2017-02-22 05:30:39 -0800206 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
tommif284b7f2017-02-27 01:59:36 -0800207 packet.timesNacked =
208 nack_module_ ? nack_module_->OnReceivedPacket(packet) : -1;
ilnik04f4d122017-06-19 07:18:55 -0700209 packet.receive_time_ms = clock_->TimeInMilliseconds();
philipelfd5a20f2016-11-15 00:57:57 -0800210
philipel54ca9192017-03-21 05:45:18 -0700211 if (packet.sizeBytes == 0) {
Niels Möllerbc010472018-03-23 13:22:29 +0100212 NotifyReceiverOfEmptyPacket(packet.seqNum);
philipel54ca9192017-03-21 05:45:18 -0700213 return 0;
214 }
215
philipela45102f2017-02-22 05:30:39 -0800216 if (packet.codec == kVideoCodecH264) {
217 // Only when we start to receive packets will we know what payload type
218 // that will be used. When we know the payload type insert the correct
219 // sps/pps into the tracker.
220 if (packet.payloadType != last_payload_type_) {
221 last_payload_type_ = packet.payloadType;
222 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800223 }
224
philipela45102f2017-02-22 05:30:39 -0800225 switch (tracker_.CopyAndFixBitstream(&packet)) {
226 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
227 keyframe_request_sender_->RequestKeyFrame();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100228 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800229 case video_coding::H264SpsPpsTracker::kDrop:
230 return 0;
231 case video_coding::H264SpsPpsTracker::kInsert:
232 break;
233 }
234
philipelfd5a20f2016-11-15 00:57:57 -0800235 } else {
philipela45102f2017-02-22 05:30:39 -0800236 uint8_t* data = new uint8_t[packet.sizeBytes];
237 memcpy(data, packet.dataPtr, packet.sizeBytes);
238 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000239 }
philipela45102f2017-02-22 05:30:39 -0800240
241 packet_buffer_->InsertPacket(&packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000242 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
nisseb1f2ff92017-06-09 04:01:55 -0700245void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
246 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200247 RtpPacketReceived packet;
248 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700249 return;
Niels Möllerb0573bc2017-09-25 10:47:00 +0200250 packet.IdentifyExtensions(rtp_header_extensions_);
251 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
252
253 RTPHeader header;
254 packet.GetHeader(&header);
Niels Möller22ec9522017-10-05 08:39:15 +0200255 ReceivePacket(rtp_packet, rtp_packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000256}
257
nissed2ef3142017-05-11 08:00:58 -0700258// This method handles both regular RTP packets and packets recovered
259// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700260void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
eladalon8b073052017-08-25 00:49:08 -0700261 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700262
eladalon8b073052017-08-25 00:49:08 -0700263 if (!receiving_) {
264 return;
265 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000266
eladalon8b073052017-08-25 00:49:08 -0700267 if (!packet.recovered()) {
268 int64_t now_ms = clock_->TimeInMilliseconds();
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000269
eladalon8b073052017-08-25 00:49:08 -0700270 // Periodically log the RTP header of incoming packets.
271 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
272 std::stringstream ss;
273 ss << "Packet received on SSRC: " << packet.Ssrc()
274 << " with payload type: " << static_cast<int>(packet.PayloadType())
275 << ", timestamp: " << packet.Timestamp()
276 << ", sequence number: " << packet.SequenceNumber()
277 << ", arrival time: " << packet.arrival_time_ms();
278 int32_t time_offset;
279 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
280 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800281 }
eladalon8b073052017-08-25 00:49:08 -0700282 uint32_t send_time;
283 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
284 ss << ", abs send time: " << send_time;
285 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100286 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700287 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000288 }
289 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000290
nisse38cc1d62017-02-13 05:59:46 -0800291 // TODO(nisse): Delete use of GetHeader, but needs refactoring of
292 // ReceivePacket and IncomingPacket methods below.
293 RTPHeader header;
294 packet.GetHeader(&header);
295
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000296 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000297
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000298 bool in_order = IsPacketInOrder(header);
Niels Möller6fed9242018-03-09 15:31:17 +0100299
Niels Möller22ec9522017-10-05 08:39:15 +0200300 ReceivePacket(packet.data(), packet.size(), header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000301 // Update receive statistics after ReceivePacket.
302 // Receive statistics will be reset if the payload type changes (make sure
303 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700304 if (!packet.recovered()) {
305 // TODO(nisse): We should pass a recovered flag to stats, to aid
306 // fixing bug bugs.webrtc.org/6339.
307 rtp_receive_statistics_->IncomingPacket(
308 header, packet.size(), IsPacketRetransmitted(header, in_order));
309 }
eladalonc0d481a2017-08-02 07:39:07 -0700310
311 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
312 secondary_sink->OnRtpPacket(packet);
313 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000314}
315
nisseb1f2ff92017-06-09 04:01:55 -0700316int32_t RtpVideoStreamReceiver::RequestKeyFrame() {
mflodmancfc8e3b2016-05-03 21:22:04 -0700317 return rtp_rtcp_->RequestKeyFrame();
318}
319
nisseb1f2ff92017-06-09 04:01:55 -0700320bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700321 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800322}
323
nisseb1f2ff92017-06-09 04:01:55 -0700324bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700325 return config_.rtp.nack.rtp_history_ms > 0;
326}
327
nisseb1f2ff92017-06-09 04:01:55 -0700328void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700329 const std::vector<uint16_t>& sequence_numbers) {
330 rtp_rtcp_->SendNack(sequence_numbers);
331}
332
nisseb1f2ff92017-06-09 04:01:55 -0700333int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
334 uint16_t length) {
mflodmancfc8e3b2016-05-03 21:22:04 -0700335 return rtp_rtcp_->SendNACK(sequence_numbers, length);
336}
337
nisseb1f2ff92017-06-09 04:01:55 -0700338void RtpVideoStreamReceiver::OnReceivedFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800339 std::unique_ptr<video_coding::RtpFrameObject> frame) {
philipel2c53b132017-05-16 08:06:30 -0700340 if (!has_received_frame_) {
341 has_received_frame_ = true;
342 if (frame->FrameType() != kVideoFrameKey)
343 keyframe_request_sender_->RequestKeyFrame();
344 }
345
philipelfd5a20f2016-11-15 00:57:57 -0800346 reference_finder_->ManageFrame(std::move(frame));
347}
348
nisseb1f2ff92017-06-09 04:01:55 -0700349void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100350 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800351 {
352 rtc::CritScope lock(&last_seq_num_cs_);
353 video_coding::RtpFrameObject* rtp_frame =
354 static_cast<video_coding::RtpFrameObject*>(frame.get());
philipel0fa82a62018-03-19 15:34:53 +0100355 last_seq_num_for_pic_id_[rtp_frame->id.picture_id] =
356 rtp_frame->last_seq_num();
philipelfd5a20f2016-11-15 00:57:57 -0800357 }
358 complete_frame_callback_->OnCompleteFrame(std::move(frame));
359}
360
Tommi81de14f2018-03-25 22:19:25 +0200361void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800362 if (nack_module_)
363 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800364}
365
nisseb1f2ff92017-06-09 04:01:55 -0700366rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700367 return packet_buffer_->LastReceivedPacketMs();
368}
369
nisseb1f2ff92017-06-09 04:01:55 -0700370rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
371 const {
philipel3184f8e2017-05-18 08:08:53 -0700372 return packet_buffer_->LastReceivedKeyframePacketMs();
373}
374
eladalonc0d481a2017-08-02 07:39:07 -0700375void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700376 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700377 RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(),
378 sink) == secondary_sinks_.cend());
379 secondary_sinks_.push_back(sink);
380}
381
382void RtpVideoStreamReceiver::RemoveSecondarySink(
383 const RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700384 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700385 auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink);
386 if (it == secondary_sinks_.end()) {
387 // We might be rolling-back a call whose setup failed mid-way. In such a
388 // case, it's simpler to remove "everything" rather than remember what
389 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100390 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700391 return;
392 }
393 secondary_sinks_.erase(it);
394}
395
nisseb1f2ff92017-06-09 04:01:55 -0700396void RtpVideoStreamReceiver::ReceivePacket(const uint8_t* packet,
397 size_t packet_length,
Niels Möller22ec9522017-10-05 08:39:15 +0200398 const RTPHeader& header) {
Niels Möllere10675a2018-03-14 10:13:43 +0100399 if (header.payloadType == config_.rtp.red_payload_type) {
nisse30e89312017-05-29 08:16:37 -0700400 ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
401 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000402 }
403 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000404 assert(packet_length >= header.headerLength);
405 size_t payload_length = packet_length - header.headerLength;
Karl Wiberg73b60b82017-09-21 15:00:58 +0200406 const auto pl =
407 rtp_payload_registry_.PayloadTypeToPayload(header.payloadType);
408 if (pl) {
409 rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
Niels Möller22ec9522017-10-05 08:39:15 +0200410 pl->typeSpecific);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000411 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000412}
413
nisseb1f2ff92017-06-09 04:01:55 -0700414void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
mflodmanfa666592016-04-28 23:15:33 -0700415 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
eladalon8b073052017-08-25 00:49:08 -0700416 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
Niels Möllere10675a2018-03-14 10:13:43 +0100417 if (header.payloadType == config_.rtp.red_payload_type) {
Niels Möller3f027b32018-03-14 08:04:58 +0100418 if (packet[header.headerLength] == config_.rtp.ulpfec_payload_type) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000419 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200420 // Notify video_receiver about received FEC packets to avoid NACKing these
421 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000422 NotifyReceiverOfFecPacket(header);
423 }
Niels Möller3f027b32018-03-14 08:04:58 +0100424 if (ulpfec_receiver_->AddReceivedRedPacket(
425 header, packet, packet_length,
426 config_.rtp.ulpfec_payload_type) != 0) {
nisse30e89312017-05-29 08:16:37 -0700427 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000428 }
nisse30e89312017-05-29 08:16:37 -0700429 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000430 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000431}
432
Niels Möllerbc010472018-03-23 13:22:29 +0100433// In the case of a video stream without picture ids and no rtx the
434// RtpFrameReferenceFinder will need to know about padding to
435// correctly calculate frame references.
436void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
437 reference_finder_->PaddingReceived(seq_num);
438 packet_buffer_->PaddingReceived(seq_num);
439}
440
nisseb1f2ff92017-06-09 04:01:55 -0700441void RtpVideoStreamReceiver::NotifyReceiverOfFecPacket(
442 const RTPHeader& header) {
Niels Möllerbc010472018-03-23 13:22:29 +0100443 if (nack_module_) {
444 nack_module_->OnReceivedPacket(header.sequenceNumber,
445 /* is_keyframe = */ false);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000446 }
Niels Möllerbc010472018-03-23 13:22:29 +0100447 NotifyReceiverOfEmptyPacket(header.sequenceNumber);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000448}
449
nisseb1f2ff92017-06-09 04:01:55 -0700450bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
451 size_t rtcp_packet_length) {
eladalon8b073052017-08-25 00:49:08 -0700452 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
453
454 if (!receiving_) {
455 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100456 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
Per83d09102016-04-15 14:59:13 +0200458 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000459
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000460 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200461 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000462 if (rtt == 0) {
463 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100464 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000465 }
466 uint32_t ntp_secs = 0;
467 uint32_t ntp_frac = 0;
468 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100469 uint32_t recieved_ntp_secs = 0;
470 uint32_t recieved_ntp_frac = 0;
471 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
472 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000473 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100474 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000475 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100476 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
477 int64_t time_since_recieved =
478 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
479 // Don't use old SRs to estimate time.
480 if (time_since_recieved <= 1) {
481 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
482 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000483
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100484 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000485}
486
philipeld4fac692017-09-04 07:03:46 -0700487void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800488 if (!nack_module_)
489 return;
490
philipela45102f2017-02-22 05:30:39 -0800491 int seq_num = -1;
492 {
493 rtc::CritScope lock(&last_seq_num_cs_);
494 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
495 if (seq_num_it != last_seq_num_for_pic_id_.end())
496 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800497 }
philipela45102f2017-02-22 05:30:39 -0800498 if (seq_num != -1)
499 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800500}
501
philipeld4fac692017-09-04 07:03:46 -0700502void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800503 int seq_num = -1;
504 {
505 rtc::CritScope lock(&last_seq_num_cs_);
506 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
507 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
508 seq_num = seq_num_it->second;
509 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
510 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800511 }
philipela45102f2017-02-22 05:30:39 -0800512 }
513 if (seq_num != -1) {
514 packet_buffer_->ClearTo(seq_num);
515 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800516 }
517}
518
nisseb1f2ff92017-06-09 04:01:55 -0700519void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700520 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
521 : RtcpMode::kOff);
522}
523
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100524int RtpVideoStreamReceiver::GetUniqueFramesSeen() const {
525 return packet_buffer_->GetUniqueFramesSeen();
526}
527
nisseb1f2ff92017-06-09 04:01:55 -0700528void RtpVideoStreamReceiver::StartReceive() {
eladalon8b073052017-08-25 00:49:08 -0700529 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000530 receiving_ = true;
531}
532
nisseb1f2ff92017-06-09 04:01:55 -0700533void RtpVideoStreamReceiver::StopReceive() {
eladalon8b073052017-08-25 00:49:08 -0700534 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000535 receiving_ = false;
536}
537
nisseb1f2ff92017-06-09 04:01:55 -0700538bool RtpVideoStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000539 StreamStatistician* statistician =
540 rtp_receive_statistics_->GetStatistician(header.ssrc);
541 if (!statistician)
542 return false;
543 return statistician->IsPacketInOrder(header.sequenceNumber);
544}
545
nisseb1f2ff92017-06-09 04:01:55 -0700546bool RtpVideoStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
547 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000548 // Retransmissions are handled separately if RTX is enabled.
Niels Möllere63afff2018-03-12 14:23:41 +0100549 if (config_.rtp.rtx_ssrc != 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000550 return false;
551 StreamStatistician* statistician =
552 rtp_receive_statistics_->GetStatistician(header.ssrc);
553 if (!statistician)
554 return false;
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000555 return !in_order &&
Niels Möllereda00872018-05-23 13:54:51 +0200556 statistician->IsRetransmitOfOldPacket(header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000557}
mflodmandc7d0d22016-05-06 05:32:22 -0700558
nisseb1f2ff92017-06-09 04:01:55 -0700559void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700560 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800561 if (counter.first_packet_time_ms == -1)
562 return;
563
564 int64_t elapsed_sec =
565 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
566 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
567 return;
568
mflodmandc7d0d22016-05-06 05:32:22 -0700569 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700570 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700571 "WebRTC.Video.ReceivedFecPacketsInPercent",
572 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
573 }
574 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700575 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
576 static_cast<int>(counter.num_recovered_packets *
577 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700578 }
579}
580
nisseb1f2ff92017-06-09 04:01:55 -0700581void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800582 auto codec_params_it = pt_codec_params_.find(payload_type);
583 if (codec_params_it == pt_codec_params_.end())
584 return;
585
Mirko Bonadei675513b2017-11-09 11:09:25 +0100586 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
587 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800588
589 H264SpropParameterSets sprop_decoder;
590 auto sprop_base64_it =
591 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
592
593 if (sprop_base64_it == codec_params_it->second.end())
594 return;
595
johan62d02c32017-01-24 04:38:27 -0800596 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800597 return;
598
johand2b092f2017-01-24 02:38:17 -0800599 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
600 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800601}
602
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000603} // namespace webrtc