blob: 79373fd270d810f2662d12c10afd936441987bed [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 Bonadei92ea95e2017-09-15 06:47:31 +020017#include "call/video_config.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "media/base/mediaconstants.h"
20#include "modules/pacing/packet_router.h"
21#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
22#include "modules/rtp_rtcp/include/receive_statistics.h"
23#include "modules/rtp_rtcp/include/rtp_cvo.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/include/rtp_receiver.h"
25#include "modules/rtp_rtcp/include/rtp_rtcp.h"
26#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
27#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
28#include "modules/rtp_rtcp/source/rtp_packet_received.h"
29#include "modules/video_coding/frame_object.h"
30#include "modules/video_coding/h264_sprop_parameter_sets.h"
31#include "modules/video_coding/h264_sps_pps_tracker.h"
32#include "modules/video_coding/packet_buffer.h"
33#include "modules/video_coding/video_coding_impl.h"
34#include "rtc_base/checks.h"
35#include "rtc_base/location.h"
36#include "rtc_base/logging.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010037#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "system_wrappers/include/field_trial.h"
39#include "system_wrappers/include/metrics.h"
40#include "system_wrappers/include/timestamp_extrapolator.h"
41#include "video/receive_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000042
43namespace webrtc {
44
philipelfd5a20f2016-11-15 00:57:57 -080045namespace {
philipel3bf97cf2017-08-10 18:10:59 +020046// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
47// crbug.com/752886
48constexpr int kPacketBufferStartSize = 512;
philipelfd5a20f2016-11-15 00:57:57 -080049constexpr int kPacketBufferMaxSixe = 2048;
50}
51
mflodmanc0e58a32016-04-25 01:26:26 -070052std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
53 ReceiveStatistics* receive_statistics,
54 Transport* outgoing_transport,
55 RtcpRttStats* rtt_stats,
56 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
nisse15389c02017-01-24 02:36:58 -080057 TransportSequenceNumberAllocator* transport_sequence_number_allocator) {
mflodmanc0e58a32016-04-25 01:26:26 -070058 RtpRtcp::Configuration configuration;
59 configuration.audio = false;
60 configuration.receiver_only = true;
61 configuration.receive_statistics = receive_statistics;
62 configuration.outgoing_transport = outgoing_transport;
63 configuration.intra_frame_callback = nullptr;
64 configuration.rtt_stats = rtt_stats;
65 configuration.rtcp_packet_type_counter_observer =
66 rtcp_packet_type_counter_observer;
mflodmanc0e58a32016-04-25 01:26:26 -070067 configuration.transport_sequence_number_allocator =
68 transport_sequence_number_allocator;
69 configuration.send_bitrate_observer = nullptr;
70 configuration.send_frame_count_observer = nullptr;
71 configuration.send_side_delay_observer = nullptr;
asapersson35151f32016-05-02 23:44:01 -070072 configuration.send_packet_observer = nullptr;
mflodmanc0e58a32016-04-25 01:26:26 -070073 configuration.bandwidth_callback = nullptr;
74 configuration.transport_feedback_callback = nullptr;
75
76 std::unique_ptr<RtpRtcp> rtp_rtcp(RtpRtcp::CreateRtpRtcp(configuration));
mflodmanc0e58a32016-04-25 01:26:26 -070077 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
78
79 return rtp_rtcp;
80}
81
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000082static const int kPacketLogIntervalMs = 10000;
83
nisseb1f2ff92017-06-09 04:01:55 -070084RtpVideoStreamReceiver::RtpVideoStreamReceiver(
mflodmanfa666592016-04-28 23:15:33 -070085 Transport* transport,
86 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070087 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020088 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070089 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -070090 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020091 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080092 NackSender* nack_sender,
93 KeyFrameRequestSender* keyframe_request_sender,
philipel0a9f6de2018-02-28 11:29:47 +010094 video_coding::OnCompleteFrameCallback* complete_frame_callback)
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 process_thread_(process_thread),
Peter Boström4fa7eca2016-03-02 15:05:53 +010099 ntp_estimator_(clock_),
Niels Möllerb0573bc2017-09-25 10:47:00 +0200100 rtp_header_extensions_(config_.rtp.extensions),
Peter Boström4fa7eca2016-03-02 15:05:53 +0100101 rtp_receiver_(RtpReceiver::CreateVideoReceiver(clock_,
102 this,
mflodmanfa666592016-04-28 23:15:33 -0700103 this,
Peter Boström4fa7eca2016-03-02 15:05:53 +0100104 &rtp_payload_registry_)),
nisseca5706d2017-09-11 02:32:16 -0700105 rtp_receive_statistics_(rtp_receive_statistics),
brandtrd726a3f2017-06-29 02:45:35 -0700106 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000107 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700108 last_packet_log_ms_(-1),
nisseca5706d2017-09-11 02:32:16 -0700109 rtp_rtcp_(CreateRtpRtcpModule(rtp_receive_statistics_,
mflodmanc0e58a32016-04-25 01:26:26 -0700110 transport,
111 rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700112 receive_stats_proxy,
nisse15389c02017-01-24 02:36:58 -0800113 packet_router)),
philipelfd5a20f2016-11-15 00:57:57 -0800114 complete_frame_callback_(complete_frame_callback),
115 keyframe_request_sender_(keyframe_request_sender),
philipel2c53b132017-05-16 08:06:30 -0700116 has_received_frame_(false) {
eladalon822ff2b2017-08-01 06:30:28 -0700117 constexpr bool remb_candidate = true;
118 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700119 rtp_receive_statistics_->RegisterRtpStatisticsCallback(receive_stats_proxy);
120 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
121
Tommi733b5472016-06-10 17:58:01 +0200122 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700123 << "A stream should not be configured with RTCP disabled. This value is "
124 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700125 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
126 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
127 RTC_DCHECK(config_.rtp.local_ssrc != 0);
128 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
129
Tommi733b5472016-06-10 17:58:01 +0200130 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
131 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
stefanb4ab3812017-06-09 06:12:11 -0700132 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700133 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodmandc7d0d22016-05-06 05:32:22 -0700134
mflodmancfc8e3b2016-05-03 21:22:04 -0700135 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200136 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
137 ? kMaxPacketAgeToNack
138 : kDefaultMaxReorderingThreshold;
mflodmancfc8e3b2016-05-03 21:22:04 -0700139 rtp_receive_statistics_->SetMaxReorderingThreshold(max_reordering_threshold);
mflodmandc7d0d22016-05-06 05:32:22 -0700140
Tommi733b5472016-06-10 17:58:01 +0200141 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700142 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
143
144 // Stats callback for CNAME changes.
145 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
146
tommidea489f2017-03-03 03:20:24 -0800147 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800148
tommif284b7f2017-02-27 01:59:36 -0800149 if (config_.rtp.nack.rtp_history_ms != 0) {
150 nack_module_.reset(
151 new NackModule(clock_, nack_sender, keyframe_request_sender));
tommidea489f2017-03-03 03:20:24 -0800152 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800153 }
philipelfd5a20f2016-11-15 00:57:57 -0800154
philipela45102f2017-02-22 05:30:39 -0800155 packet_buffer_ = video_coding::PacketBuffer::Create(
156 clock_, kPacketBufferStartSize, kPacketBufferMaxSixe, this);
157 reference_finder_.reset(new video_coding::RtpFrameReferenceFinder(this));
mflodmanc0e58a32016-04-25 01:26:26 -0700158}
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
nisseb1f2ff92017-06-09 04:01:55 -0700160RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700161 RTC_DCHECK(secondary_sinks_.empty());
162
tommif284b7f2017-02-27 01:59:36 -0800163 if (nack_module_) {
164 process_thread_->DeRegisterModule(nack_module_.get());
165 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
tommif284b7f2017-02-27 01:59:36 -0800167 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800168
nissefdbfdc92017-03-31 05:44:52 -0700169 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700170 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000171}
172
nisseb1f2ff92017-06-09 04:01:55 -0700173bool RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800174 const VideoCodec& video_codec,
175 const std::map<std::string, std::string>& codec_params) {
176 pt_codec_params_.insert(make_pair(video_codec.plType, codec_params));
177 return AddReceiveCodec(video_codec);
178}
179
nisseb1f2ff92017-06-09 04:01:55 -0700180bool RtpVideoStreamReceiver::AddReceiveCodec(const VideoCodec& video_codec) {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000181 int8_t old_pltype = -1;
magjed56124bd2016-11-24 09:34:46 -0800182 if (rtp_payload_registry_.ReceivePayloadType(video_codec, &old_pltype) !=
183 -1) {
Peter Boström4fa7eca2016-03-02 15:05:53 +0100184 rtp_payload_registry_.DeRegisterReceivePayload(old_pltype);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185 }
magjed56124bd2016-11-24 09:34:46 -0800186 return rtp_payload_registry_.RegisterReceivePayload(video_codec) == 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000187}
188
nisseb1f2ff92017-06-09 04:01:55 -0700189uint32_t RtpVideoStreamReceiver::GetRemoteSsrc() const {
stefanb4ab3812017-06-09 06:12:11 -0700190 return config_.rtp.remote_ssrc;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000191}
192
nisseb1f2ff92017-06-09 04:01:55 -0700193int RtpVideoStreamReceiver::GetCsrcs(uint32_t* csrcs) const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000194 return rtp_receiver_->CSRCs(csrcs);
195}
196
nisseb1f2ff92017-06-09 04:01:55 -0700197RtpReceiver* RtpVideoStreamReceiver::GetRtpReceiver() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000198 return rtp_receiver_.get();
199}
200
nisseb1f2ff92017-06-09 04:01:55 -0700201int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmanfa666592016-04-28 23:15:33 -0700202 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200203 size_t payload_size,
mflodmanfa666592016-04-28 23:15:33 -0700204 const WebRtcRTPHeader* rtp_header) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000205 WebRtcRTPHeader rtp_header_with_ntp = *rtp_header;
wu@webrtc.org88abf112014-05-14 16:53:51 +0000206 rtp_header_with_ntp.ntp_time_ms =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100207 ntp_estimator_.Estimate(rtp_header->header.timestamp);
philipela45102f2017-02-22 05:30:39 -0800208 VCMPacket packet(payload_data, payload_size, rtp_header_with_ntp);
tommif284b7f2017-02-27 01:59:36 -0800209 packet.timesNacked =
210 nack_module_ ? nack_module_->OnReceivedPacket(packet) : -1;
ilnik04f4d122017-06-19 07:18:55 -0700211 packet.receive_time_ms = clock_->TimeInMilliseconds();
philipelfd5a20f2016-11-15 00:57:57 -0800212
philipel54ca9192017-03-21 05:45:18 -0700213 // In the case of a video stream without picture ids and no rtx the
214 // RtpFrameReferenceFinder will need to know about padding to
215 // correctly calculate frame references.
216 if (packet.sizeBytes == 0) {
217 reference_finder_->PaddingReceived(packet.seqNum);
philipel2c9f9f22017-06-13 02:47:28 -0700218 packet_buffer_->PaddingReceived(packet.seqNum);
philipel54ca9192017-03-21 05:45:18 -0700219 return 0;
220 }
221
philipela45102f2017-02-22 05:30:39 -0800222 if (packet.codec == kVideoCodecH264) {
223 // Only when we start to receive packets will we know what payload type
224 // that will be used. When we know the payload type insert the correct
225 // sps/pps into the tracker.
226 if (packet.payloadType != last_payload_type_) {
227 last_payload_type_ = packet.payloadType;
228 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800229 }
230
philipela45102f2017-02-22 05:30:39 -0800231 switch (tracker_.CopyAndFixBitstream(&packet)) {
232 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
233 keyframe_request_sender_->RequestKeyFrame();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100234 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800235 case video_coding::H264SpsPpsTracker::kDrop:
236 return 0;
237 case video_coding::H264SpsPpsTracker::kInsert:
238 break;
239 }
240
philipelfd5a20f2016-11-15 00:57:57 -0800241 } else {
philipela45102f2017-02-22 05:30:39 -0800242 uint8_t* data = new uint8_t[packet.sizeBytes];
243 memcpy(data, packet.dataPtr, packet.sizeBytes);
244 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000245 }
philipela45102f2017-02-22 05:30:39 -0800246
247 packet_buffer_->InsertPacket(&packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000248 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249}
250
nisseb1f2ff92017-06-09 04:01:55 -0700251void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
252 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200253 RtpPacketReceived packet;
254 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700255 return;
Niels Möllerb0573bc2017-09-25 10:47:00 +0200256 packet.IdentifyExtensions(rtp_header_extensions_);
257 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
258
259 RTPHeader header;
260 packet.GetHeader(&header);
Niels Möller22ec9522017-10-05 08:39:15 +0200261 ReceivePacket(rtp_packet, rtp_packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000262}
263
mflodmanfa666592016-04-28 23:15:33 -0700264// TODO(pbos): Remove as soon as audio can handle a changing payload type
265// without this callback.
nisseb1f2ff92017-06-09 04:01:55 -0700266int32_t RtpVideoStreamReceiver::OnInitializeDecoder(
Karl Wibergc62f6c72017-10-04 12:38:53 +0200267 const int payload_type,
268 const SdpAudioFormat& audio_format,
mflodmanfa666592016-04-28 23:15:33 -0700269 const uint32_t rate) {
270 RTC_NOTREACHED();
271 return 0;
272}
273
nissed2ef3142017-05-11 08:00:58 -0700274// This method handles both regular RTP packets and packets recovered
275// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700276void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
eladalon8b073052017-08-25 00:49:08 -0700277 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700278
eladalon8b073052017-08-25 00:49:08 -0700279 if (!receiving_) {
280 return;
281 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000282
eladalon8b073052017-08-25 00:49:08 -0700283 if (!packet.recovered()) {
284 int64_t now_ms = clock_->TimeInMilliseconds();
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000285
eladalon8b073052017-08-25 00:49:08 -0700286 // Periodically log the RTP header of incoming packets.
287 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
288 std::stringstream ss;
289 ss << "Packet received on SSRC: " << packet.Ssrc()
290 << " with payload type: " << static_cast<int>(packet.PayloadType())
291 << ", timestamp: " << packet.Timestamp()
292 << ", sequence number: " << packet.SequenceNumber()
293 << ", arrival time: " << packet.arrival_time_ms();
294 int32_t time_offset;
295 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
296 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800297 }
eladalon8b073052017-08-25 00:49:08 -0700298 uint32_t send_time;
299 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
300 ss << ", abs send time: " << send_time;
301 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100302 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700303 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000304 }
305 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000306
nisse38cc1d62017-02-13 05:59:46 -0800307 // TODO(nisse): Delete use of GetHeader, but needs refactoring of
308 // ReceivePacket and IncomingPacket methods below.
309 RTPHeader header;
310 packet.GetHeader(&header);
311
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000312 header.payload_type_frequency = kVideoPayloadTypeFrequency;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000313
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000314 bool in_order = IsPacketInOrder(header);
Niels Möller6fed9242018-03-09 15:31:17 +0100315
Niels Möller22ec9522017-10-05 08:39:15 +0200316 ReceivePacket(packet.data(), packet.size(), header);
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000317 // Update receive statistics after ReceivePacket.
318 // Receive statistics will be reset if the payload type changes (make sure
319 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700320 if (!packet.recovered()) {
321 // TODO(nisse): We should pass a recovered flag to stats, to aid
322 // fixing bug bugs.webrtc.org/6339.
323 rtp_receive_statistics_->IncomingPacket(
324 header, packet.size(), IsPacketRetransmitted(header, in_order));
325 }
eladalonc0d481a2017-08-02 07:39:07 -0700326
327 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
328 secondary_sink->OnRtpPacket(packet);
329 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000330}
331
nisseb1f2ff92017-06-09 04:01:55 -0700332int32_t RtpVideoStreamReceiver::RequestKeyFrame() {
mflodmancfc8e3b2016-05-03 21:22:04 -0700333 return rtp_rtcp_->RequestKeyFrame();
334}
335
nisseb1f2ff92017-06-09 04:01:55 -0700336bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700337 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800338}
339
nisseb1f2ff92017-06-09 04:01:55 -0700340bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700341 return config_.rtp.nack.rtp_history_ms > 0;
342}
343
nisseb1f2ff92017-06-09 04:01:55 -0700344void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700345 const std::vector<uint16_t>& sequence_numbers) {
346 rtp_rtcp_->SendNack(sequence_numbers);
347}
348
nisseb1f2ff92017-06-09 04:01:55 -0700349int32_t RtpVideoStreamReceiver::ResendPackets(const uint16_t* sequence_numbers,
350 uint16_t length) {
mflodmancfc8e3b2016-05-03 21:22:04 -0700351 return rtp_rtcp_->SendNACK(sequence_numbers, length);
352}
353
nisseb1f2ff92017-06-09 04:01:55 -0700354void RtpVideoStreamReceiver::OnReceivedFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800355 std::unique_ptr<video_coding::RtpFrameObject> frame) {
philipel2c53b132017-05-16 08:06:30 -0700356 if (!has_received_frame_) {
357 has_received_frame_ = true;
358 if (frame->FrameType() != kVideoFrameKey)
359 keyframe_request_sender_->RequestKeyFrame();
360 }
361
philipelfd5a20f2016-11-15 00:57:57 -0800362 reference_finder_->ManageFrame(std::move(frame));
363}
364
nisseb1f2ff92017-06-09 04:01:55 -0700365void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100366 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800367 {
368 rtc::CritScope lock(&last_seq_num_cs_);
369 video_coding::RtpFrameObject* rtp_frame =
370 static_cast<video_coding::RtpFrameObject*>(frame.get());
371 last_seq_num_for_pic_id_[rtp_frame->picture_id] = rtp_frame->last_seq_num();
372 }
373 complete_frame_callback_->OnCompleteFrame(std::move(frame));
374}
375
nisseb1f2ff92017-06-09 04:01:55 -0700376void RtpVideoStreamReceiver::OnRttUpdate(int64_t avg_rtt_ms,
377 int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800378 if (nack_module_)
379 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800380}
381
nisseb1f2ff92017-06-09 04:01:55 -0700382rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700383 return packet_buffer_->LastReceivedPacketMs();
384}
385
nisseb1f2ff92017-06-09 04:01:55 -0700386rtc::Optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
387 const {
philipel3184f8e2017-05-18 08:08:53 -0700388 return packet_buffer_->LastReceivedKeyframePacketMs();
389}
390
eladalonc0d481a2017-08-02 07:39:07 -0700391void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700392 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700393 RTC_DCHECK(std::find(secondary_sinks_.cbegin(), secondary_sinks_.cend(),
394 sink) == secondary_sinks_.cend());
395 secondary_sinks_.push_back(sink);
396}
397
398void RtpVideoStreamReceiver::RemoveSecondarySink(
399 const RtpPacketSinkInterface* sink) {
eladalon8b073052017-08-25 00:49:08 -0700400 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700401 auto it = std::find(secondary_sinks_.begin(), secondary_sinks_.end(), sink);
402 if (it == secondary_sinks_.end()) {
403 // We might be rolling-back a call whose setup failed mid-way. In such a
404 // case, it's simpler to remove "everything" rather than remember what
405 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700407 return;
408 }
409 secondary_sinks_.erase(it);
410}
411
nisseb1f2ff92017-06-09 04:01:55 -0700412void RtpVideoStreamReceiver::ReceivePacket(const uint8_t* packet,
413 size_t packet_length,
Niels Möller22ec9522017-10-05 08:39:15 +0200414 const RTPHeader& header) {
Niels Möllere10675a2018-03-14 10:13:43 +0100415 if (header.payloadType == config_.rtp.red_payload_type) {
nisse30e89312017-05-29 08:16:37 -0700416 ParseAndHandleEncapsulatingHeader(packet, packet_length, header);
417 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000418 }
419 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000420 assert(packet_length >= header.headerLength);
421 size_t payload_length = packet_length - header.headerLength;
Karl Wiberg73b60b82017-09-21 15:00:58 +0200422 const auto pl =
423 rtp_payload_registry_.PayloadTypeToPayload(header.payloadType);
424 if (pl) {
425 rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
Niels Möller22ec9522017-10-05 08:39:15 +0200426 pl->typeSpecific);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000427 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000428}
429
nisseb1f2ff92017-06-09 04:01:55 -0700430void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
mflodmanfa666592016-04-28 23:15:33 -0700431 const uint8_t* packet, size_t packet_length, const RTPHeader& header) {
eladalon8b073052017-08-25 00:49:08 -0700432 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
Niels Möllere10675a2018-03-14 10:13:43 +0100433 if (header.payloadType == config_.rtp.red_payload_type) {
Niels Möller3f027b32018-03-14 08:04:58 +0100434 if (packet[header.headerLength] == config_.rtp.ulpfec_payload_type) {
asapersson@webrtc.org273fbbb2015-01-27 12:17:29 +0000435 rtp_receive_statistics_->FecPacketReceived(header, packet_length);
Peter Boström0b250722016-04-22 18:23:15 +0200436 // Notify video_receiver about received FEC packets to avoid NACKing these
437 // packets.
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000438 NotifyReceiverOfFecPacket(header);
439 }
Niels Möller3f027b32018-03-14 08:04:58 +0100440 if (ulpfec_receiver_->AddReceivedRedPacket(
441 header, packet, packet_length,
442 config_.rtp.ulpfec_payload_type) != 0) {
nisse30e89312017-05-29 08:16:37 -0700443 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000444 }
nisse30e89312017-05-29 08:16:37 -0700445 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000446 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
nisseb1f2ff92017-06-09 04:01:55 -0700449void RtpVideoStreamReceiver::NotifyReceiverOfFecPacket(
450 const RTPHeader& header) {
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000451 int8_t last_media_payload_type =
Peter Boström4fa7eca2016-03-02 15:05:53 +0100452 rtp_payload_registry_.last_received_media_payload_type();
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000453 if (last_media_payload_type < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100454 RTC_LOG(LS_WARNING) << "Failed to get last media payload type.";
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000455 return;
456 }
457 // Fake an empty media packet.
458 WebRtcRTPHeader rtp_header = {};
459 rtp_header.header = header;
460 rtp_header.header.payloadType = last_media_payload_type;
461 rtp_header.header.paddingLength = 0;
Karl Wiberg73b60b82017-09-21 15:00:58 +0200462 const auto pl =
463 rtp_payload_registry_.PayloadTypeToPayload(last_media_payload_type);
464 if (!pl) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100465 RTC_LOG(LS_WARNING) << "Failed to get payload specifics.";
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000466 return;
467 }
Karl Wibergc856dc22017-09-28 20:13:59 +0200468 rtp_header.type.Video.codec = pl->typeSpecific.video_payload().videoCodecType;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000469 rtp_header.type.Video.rotation = kVideoRotation_0;
470 if (header.extension.hasVideoRotation) {
magjed71eb61c2016-09-08 03:24:58 -0700471 rtp_header.type.Video.rotation = header.extension.videoRotation;
guoweis@webrtc.orgfdd10572015-03-12 20:50:57 +0000472 }
ilnik00d802b2017-04-11 10:34:31 -0700473 rtp_header.type.Video.content_type = VideoContentType::UNSPECIFIED;
474 if (header.extension.hasVideoContentType) {
475 rtp_header.type.Video.content_type = header.extension.videoContentType;
476 }
ilnik04f4d122017-06-19 07:18:55 -0700477 rtp_header.type.Video.video_timing = {0u, 0u, 0u, 0u, 0u, 0u, false};
478 if (header.extension.has_video_timing) {
479 rtp_header.type.Video.video_timing = header.extension.video_timing;
ilnik04f4d122017-06-19 07:18:55 -0700480 }
isheriff6b4b5f32016-06-08 00:24:21 -0700481 rtp_header.type.Video.playout_delay = header.extension.playout_delay;
482
Peter Boström74f6e9e2016-04-04 17:56:10 +0200483 OnReceivedPayloadData(nullptr, 0, &rtp_header);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000484}
485
nisseb1f2ff92017-06-09 04:01:55 -0700486bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
487 size_t rtcp_packet_length) {
eladalon8b073052017-08-25 00:49:08 -0700488 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
489
490 if (!receiving_) {
491 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100492 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
Per83d09102016-04-15 14:59:13 +0200494 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000495
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000496 int64_t rtt = 0;
Per83d09102016-04-15 14:59:13 +0200497 rtp_rtcp_->RTT(rtp_receiver_->SSRC(), &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000498 if (rtt == 0) {
499 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100500 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000501 }
502 uint32_t ntp_secs = 0;
503 uint32_t ntp_frac = 0;
504 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100505 uint32_t recieved_ntp_secs = 0;
506 uint32_t recieved_ntp_frac = 0;
507 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
508 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000509 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100510 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000511 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100512 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
513 int64_t time_since_recieved =
514 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
515 // Don't use old SRs to estimate time.
516 if (time_since_recieved <= 1) {
517 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
518 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000519
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100520 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000521}
522
philipeld4fac692017-09-04 07:03:46 -0700523void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800524 if (!nack_module_)
525 return;
526
philipela45102f2017-02-22 05:30:39 -0800527 int seq_num = -1;
528 {
529 rtc::CritScope lock(&last_seq_num_cs_);
530 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
531 if (seq_num_it != last_seq_num_for_pic_id_.end())
532 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800533 }
philipela45102f2017-02-22 05:30:39 -0800534 if (seq_num != -1)
535 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800536}
537
philipeld4fac692017-09-04 07:03:46 -0700538void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800539 int seq_num = -1;
540 {
541 rtc::CritScope lock(&last_seq_num_cs_);
542 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
543 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
544 seq_num = seq_num_it->second;
545 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
546 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800547 }
philipela45102f2017-02-22 05:30:39 -0800548 }
549 if (seq_num != -1) {
550 packet_buffer_->ClearTo(seq_num);
551 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800552 }
553}
554
nisseb1f2ff92017-06-09 04:01:55 -0700555void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700556 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
557 : RtcpMode::kOff);
558}
559
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100560int RtpVideoStreamReceiver::GetUniqueFramesSeen() const {
561 return packet_buffer_->GetUniqueFramesSeen();
562}
563
nisseb1f2ff92017-06-09 04:01:55 -0700564void RtpVideoStreamReceiver::StartReceive() {
eladalon8b073052017-08-25 00:49:08 -0700565 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000566 receiving_ = true;
567}
568
nisseb1f2ff92017-06-09 04:01:55 -0700569void RtpVideoStreamReceiver::StopReceive() {
eladalon8b073052017-08-25 00:49:08 -0700570 RTC_DCHECK_CALLED_SEQUENTIALLY(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000571 receiving_ = false;
572}
573
nisseb1f2ff92017-06-09 04:01:55 -0700574bool RtpVideoStreamReceiver::IsPacketInOrder(const RTPHeader& header) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000575 StreamStatistician* statistician =
576 rtp_receive_statistics_->GetStatistician(header.ssrc);
577 if (!statistician)
578 return false;
579 return statistician->IsPacketInOrder(header.sequenceNumber);
580}
581
nisseb1f2ff92017-06-09 04:01:55 -0700582bool RtpVideoStreamReceiver::IsPacketRetransmitted(const RTPHeader& header,
583 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000584 // Retransmissions are handled separately if RTX is enabled.
Niels Möllere63afff2018-03-12 14:23:41 +0100585 if (config_.rtp.rtx_ssrc != 0)
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000586 return false;
587 StreamStatistician* statistician =
588 rtp_receive_statistics_->GetStatistician(header.ssrc);
589 if (!statistician)
590 return false;
591 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000592 int64_t min_rtt = 0;
stefanb4ab3812017-06-09 06:12:11 -0700593 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, nullptr, nullptr, &min_rtt, nullptr);
stefan@webrtc.org48df3812013-11-08 15:18:52 +0000594 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000595 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000596}
mflodmandc7d0d22016-05-06 05:32:22 -0700597
nisseb1f2ff92017-06-09 04:01:55 -0700598void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700599 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800600 if (counter.first_packet_time_ms == -1)
601 return;
602
603 int64_t elapsed_sec =
604 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
605 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
606 return;
607
mflodmandc7d0d22016-05-06 05:32:22 -0700608 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700609 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700610 "WebRTC.Video.ReceivedFecPacketsInPercent",
611 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
612 }
613 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700614 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
615 static_cast<int>(counter.num_recovered_packets *
616 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700617 }
618}
619
nisseb1f2ff92017-06-09 04:01:55 -0700620void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800621 auto codec_params_it = pt_codec_params_.find(payload_type);
622 if (codec_params_it == pt_codec_params_.end())
623 return;
624
Mirko Bonadei675513b2017-11-09 11:09:25 +0100625 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
626 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800627
628 H264SpropParameterSets sprop_decoder;
629 auto sprop_base64_it =
630 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
631
632 if (sprop_base64_it == codec_params_it->second.end())
633 return;
634
johan62d02c32017-01-24 04:38:27 -0800635 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800636 return;
637
johand2b092f2017-01-24 02:38:17 -0800638 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
639 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800640}
641
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000642} // namespace webrtc