blob: 641af8ae2c0b9c79eeb8af665c958fce57771361 [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
philipelfd5a20f2016-11-15 00:57:57 -080013#include <utility>
ilnik04f4d122017-06-19 07:18:55 -070014#include <vector>
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000015
Steve Antonbd631a02019-03-28 10:51:27 -070016#include "absl/algorithm/container.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020017#include "absl/memory/memory.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "media/base/media_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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_rtcp.h"
24#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020025#include "modules/rtp_rtcp/source/rtp_format.h"
philipelb3e42a42018-09-13 10:57:14 +020026#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
28#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020029#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/video_coding/frame_object.h"
31#include "modules/video_coding/h264_sprop_parameter_sets.h"
32#include "modules/video_coding/h264_sps_pps_tracker.h"
Ilya Nikolaevskiy8643b782018-06-07 16:15:40 +020033#include "modules/video_coding/nack_module.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "modules/video_coding/packet_buffer.h"
35#include "modules/video_coding/video_coding_impl.h"
36#include "rtc_base/checks.h"
37#include "rtc_base/location.h"
38#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020039#include "rtc_base/strings/string_builder.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010040#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "system_wrappers/include/field_trial.h"
42#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "video/receive_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000044
45namespace webrtc {
46
philipelfd5a20f2016-11-15 00:57:57 -080047namespace {
philipel3bf97cf2017-08-10 18:10:59 +020048// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
49// crbug.com/752886
50constexpr int kPacketBufferStartSize = 512;
Johannes Kron201596f2018-10-22 14:33:39 +020051constexpr int kPacketBufferMaxSize = 2048;
Yves Gerey665174f2018-06-19 15:03:05 +020052} // namespace
philipelfd5a20f2016-11-15 00:57:57 -080053
mflodmanc0e58a32016-04-25 01:26:26 -070054std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
Sebastian Jansson8026d602019-03-04 19:39:01 +010055 Clock* clock,
mflodmanc0e58a32016-04-25 01:26:26 -070056 ReceiveStatistics* receive_statistics,
57 Transport* outgoing_transport,
58 RtcpRttStats* rtt_stats,
Niels Möller60f4e292019-05-20 11:06:33 +020059 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer) {
mflodmanc0e58a32016-04-25 01:26:26 -070060 RtpRtcp::Configuration configuration;
Sebastian Jansson8026d602019-03-04 19:39:01 +010061 configuration.clock = clock;
mflodmanc0e58a32016-04-25 01:26:26 -070062 configuration.audio = false;
63 configuration.receiver_only = true;
64 configuration.receive_statistics = receive_statistics;
65 configuration.outgoing_transport = outgoing_transport;
mflodmanc0e58a32016-04-25 01:26:26 -070066 configuration.rtt_stats = rtt_stats;
67 configuration.rtcp_packet_type_counter_observer =
68 rtcp_packet_type_counter_observer;
mflodmanc0e58a32016-04-25 01:26:26 -070069
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +010070 std::unique_ptr<RtpRtcp> rtp_rtcp = RtpRtcp::Create(configuration);
mflodmanc0e58a32016-04-25 01:26:26 -070071 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
72
73 return rtp_rtcp;
74}
75
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000076static const int kPacketLogIntervalMs = 10000;
77
nisseb1f2ff92017-06-09 04:01:55 -070078RtpVideoStreamReceiver::RtpVideoStreamReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +010079 Clock* clock,
mflodmanfa666592016-04-28 23:15:33 -070080 Transport* transport,
81 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -070082 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +020083 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -070084 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -070085 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +020086 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -080087 NackSender* nack_sender,
Niels Möller479c0552019-05-23 16:57:01 +020088 video_coding::OnCompleteFrameCallback* complete_frame_callback,
89 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
90 : RtpVideoStreamReceiver(clock,
91 CreateRtpRtcpModule(clock,
92 rtp_receive_statistics,
93 transport,
94 rtt_stats,
95 receive_stats_proxy),
96 packet_router,
97 config,
98 rtp_receive_statistics,
99 receive_stats_proxy,
100 process_thread,
101 nack_sender,
102 complete_frame_callback,
103 frame_decryptor) {}
104
105RtpVideoStreamReceiver::RtpVideoStreamReceiver(
106 Clock* clock,
107 std::unique_ptr<RtpRtcp> rtp_rtcp,
108 PacketRouter* packet_router,
109 const VideoReceiveStream::Config* config,
110 ReceiveStatistics* rtp_receive_statistics,
111 ReceiveStatisticsProxy* receive_stats_proxy,
112 ProcessThread* process_thread,
113 NackSender* nack_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700114 video_coding::OnCompleteFrameCallback* complete_frame_callback,
115 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
Sebastian Jansson8026d602019-03-04 19:39:01 +0100116 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +0200117 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -0700118 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700119 process_thread_(process_thread),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100120 ntp_estimator_(clock),
Niels Möllerb0573bc2017-09-25 10:47:00 +0200121 rtp_header_extensions_(config_.rtp.extensions),
nisseca5706d2017-09-11 02:32:16 -0700122 rtp_receive_statistics_(rtp_receive_statistics),
brandtrd726a3f2017-06-29 02:45:35 -0700123 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, this)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000124 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700125 last_packet_log_ms_(-1),
Niels Möller479c0552019-05-23 16:57:01 +0200126 rtp_rtcp_(std::move(rtp_rtcp)),
philipelfd5a20f2016-11-15 00:57:57 -0800127 complete_frame_callback_(complete_frame_callback),
Benjamin Wright52426ed2019-03-01 11:01:59 -0800128 has_received_frame_(false),
129 frames_decryptable_(false) {
eladalon822ff2b2017-08-01 06:30:28 -0700130 constexpr bool remb_candidate = true;
Niels Möller60f4e292019-05-20 11:06:33 +0200131 if (packet_router_)
132 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700133
Tommi733b5472016-06-10 17:58:01 +0200134 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700135 << "A stream should not be configured with RTCP disabled. This value is "
136 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700137 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
138 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
139 RTC_DCHECK(config_.rtp.local_ssrc != 0);
140 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
141
Tommi733b5472016-06-10 17:58:01 +0200142 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
143 rtp_rtcp_->SetSSRC(config_.rtp.local_ssrc);
stefanb4ab3812017-06-09 06:12:11 -0700144 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmanc0e58a32016-04-25 01:26:26 -0700145 rtp_rtcp_->SetKeyFrameRequestMethod(kKeyFrameReqPliRtcp);
mflodmandc7d0d22016-05-06 05:32:22 -0700146
mflodmancfc8e3b2016-05-03 21:22:04 -0700147 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200148 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
149 ? kMaxPacketAgeToNack
150 : kDefaultMaxReorderingThreshold;
Niels Möller87da1092019-05-24 14:04:28 +0200151 rtp_receive_statistics_->SetMaxReorderingThreshold(config_.rtp.remote_ssrc,
152 max_reordering_threshold);
153 // TODO(nisse): For historic reasons, we applied the above
154 // max_reordering_threshold also for RTX stats, which makes little sense since
155 // we don't NACK rtx packets. Consider deleting the below block, and rely on
156 // the default threshold.
157 if (config_.rtp.rtx_ssrc) {
158 rtp_receive_statistics_->SetMaxReorderingThreshold(
159 config_.rtp.rtx_ssrc, max_reordering_threshold);
160 }
Tommi733b5472016-06-10 17:58:01 +0200161 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700162 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
163
164 // Stats callback for CNAME changes.
165 rtp_rtcp_->RegisterRtcpStatisticsCallback(receive_stats_proxy);
166
tommidea489f2017-03-03 03:20:24 -0800167 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800168
Elad Alonfadb1812019-05-24 13:40:02 +0200169 // TODO(bugs.webrtc.org/10662): NACK and LNTF shouldn't be mutually exclusive.
170 if (config_.rtp.lntf.enabled) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100171 loss_notification_controller_ =
Niels Möller479c0552019-05-23 16:57:01 +0200172 absl::make_unique<LossNotificationController>(this, this);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100173 } else if (config_.rtp.nack.rtp_history_ms != 0) {
Niels Möller479c0552019-05-23 16:57:01 +0200174 nack_module_ = absl::make_unique<NackModule>(clock_, nack_sender, this);
tommidea489f2017-03-03 03:20:24 -0800175 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800176 }
philipelfd5a20f2016-11-15 00:57:57 -0800177
Johannes Kron487e6942018-10-24 00:53:39 +0200178 // The group here must be a positive power of 2, in which case that is used as
179 // size. All other values shall result in the default value being used.
Johannes Kron201596f2018-10-22 14:33:39 +0200180 const std::string group_name =
181 webrtc::field_trial::FindFullName("WebRTC-PacketBufferMaxSize");
182 int packet_buffer_max_size = kPacketBufferMaxSize;
183 if (!group_name.empty() &&
184 (sscanf(group_name.c_str(), "%d", &packet_buffer_max_size) != 1 ||
Johannes Kron487e6942018-10-24 00:53:39 +0200185 packet_buffer_max_size <= 0 ||
186 // Verify that the number is a positive power of 2.
187 (packet_buffer_max_size & (packet_buffer_max_size - 1)) != 0)) {
Johannes Kron201596f2018-10-22 14:33:39 +0200188 RTC_LOG(LS_WARNING) << "Invalid packet buffer max size: " << group_name;
189 packet_buffer_max_size = kPacketBufferMaxSize;
190 }
191
philipela45102f2017-02-22 05:30:39 -0800192 packet_buffer_ = video_coding::PacketBuffer::Create(
Johannes Kron201596f2018-10-22 14:33:39 +0200193 clock_, kPacketBufferStartSize, packet_buffer_max_size, this);
Elad Alona8f54612018-11-06 11:21:25 +0100194 reference_finder_ =
195 absl::make_unique<video_coding::RtpFrameReferenceFinder>(this);
Benjamin Wrighta5564482019-04-03 10:44:18 -0700196
Benjamin Wright00765292018-11-30 16:18:26 -0800197 // Only construct the encrypted receiver if frame encryption is enabled.
Benjamin Wrighta5564482019-04-03 10:44:18 -0700198 if (config_.crypto_options.sframe.require_frame_encryption) {
Benjamin Wright00765292018-11-30 16:18:26 -0800199 buffered_frame_decryptor_ =
Benjamin Wrighta5564482019-04-03 10:44:18 -0700200 absl::make_unique<BufferedFrameDecryptor>(this, this);
201 if (frame_decryptor != nullptr) {
202 buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor));
203 }
Benjamin Wright00765292018-11-30 16:18:26 -0800204 }
mflodmanc0e58a32016-04-25 01:26:26 -0700205}
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
nisseb1f2ff92017-06-09 04:01:55 -0700207RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700208 RTC_DCHECK(secondary_sinks_.empty());
209
tommif284b7f2017-02-27 01:59:36 -0800210 if (nack_module_) {
211 process_thread_->DeRegisterModule(nack_module_.get());
212 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
tommif284b7f2017-02-27 01:59:36 -0800214 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800215
Niels Möller60f4e292019-05-20 11:06:33 +0200216 if (packet_router_)
217 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700218 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000219}
220
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200221void RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800222 const VideoCodec& video_codec,
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200223 const std::map<std::string, std::string>& codec_params,
224 bool raw_payload) {
225 absl::optional<VideoCodecType> video_type;
226 if (!raw_payload) {
227 video_type = video_codec.codecType;
228 }
229 payload_type_map_.emplace(video_codec.plType, video_type);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200230 pt_codec_params_.emplace(video_codec.plType, codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000231}
232
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200233absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200234 Syncable::Info info;
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200235 if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
236 &info.capture_time_ntp_frac, nullptr, nullptr,
237 &info.capture_time_source_clock) != 0) {
238 return absl::nullopt;
239 }
Niels Möllerb0d4b412018-08-28 13:58:15 +0200240 {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200241 rtc::CritScope lock(&rtp_sources_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200242 if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
243 return absl::nullopt;
244 }
245 info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
246 info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
247 }
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200248
249 // Leaves info.current_delay_ms uninitialized.
250 return info;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000251}
252
nisseb1f2ff92017-06-09 04:01:55 -0700253int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmanfa666592016-04-28 23:15:33 -0700254 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200255 size_t payload_size,
Niels Möller125b5d62019-03-11 16:11:07 +0100256 const RTPHeader& rtp_header,
257 const RTPVideoHeader& video_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100258 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
259 bool is_recovered) {
Niels Möller125b5d62019-03-11 16:11:07 +0100260 VCMPacket packet(payload_data, payload_size, rtp_header, video_header,
Niels Möllerabbc50e2019-04-24 09:41:16 +0200261 ntp_estimator_.Estimate(rtp_header.timestamp));
Elad Alon7d6a4c02019-02-25 13:00:51 +0100262 packet.generic_descriptor = generic_descriptor;
263
Niels Möller8dad9b42018-08-22 10:36:35 +0200264 if (nack_module_) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200265 const bool is_keyframe =
266 video_header.is_first_packet_in_frame &&
267 video_header.frame_type == VideoFrameType::kVideoFrameKey;
Niels Möller8dad9b42018-08-22 10:36:35 +0200268
269 packet.timesNacked = nack_module_->OnReceivedPacket(
Niels Möller125b5d62019-03-11 16:11:07 +0100270 rtp_header.sequenceNumber, is_keyframe, is_recovered);
Ying Wangb32bb952018-10-31 10:12:27 +0100271
Niels Möller8dad9b42018-08-22 10:36:35 +0200272 } else {
273 packet.timesNacked = -1;
274 }
ilnik04f4d122017-06-19 07:18:55 -0700275 packet.receive_time_ms = clock_->TimeInMilliseconds();
philipelfd5a20f2016-11-15 00:57:57 -0800276
Elad Alon7d6a4c02019-02-25 13:00:51 +0100277 if (loss_notification_controller_) {
278 if (is_recovered) {
279 // TODO(bugs.webrtc.org/10336): Implement support for reordering.
280 RTC_LOG(LS_WARNING)
281 << "LossNotificationController does not support reordering.";
282 } else {
283 loss_notification_controller_->OnReceivedPacket(packet);
284 }
285 }
286
philipel54ca9192017-03-21 05:45:18 -0700287 if (packet.sizeBytes == 0) {
Niels Möllerbc010472018-03-23 13:22:29 +0100288 NotifyReceiverOfEmptyPacket(packet.seqNum);
philipel54ca9192017-03-21 05:45:18 -0700289 return 0;
290 }
291
Niels Möllerd5e02f02019-02-20 13:12:21 +0100292 if (packet.codec() == kVideoCodecH264) {
philipela45102f2017-02-22 05:30:39 -0800293 // Only when we start to receive packets will we know what payload type
294 // that will be used. When we know the payload type insert the correct
295 // sps/pps into the tracker.
296 if (packet.payloadType != last_payload_type_) {
297 last_payload_type_ = packet.payloadType;
298 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800299 }
300
philipela45102f2017-02-22 05:30:39 -0800301 switch (tracker_.CopyAndFixBitstream(&packet)) {
302 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
Niels Möller479c0552019-05-23 16:57:01 +0200303 RequestKeyFrame();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100304 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800305 case video_coding::H264SpsPpsTracker::kDrop:
306 return 0;
307 case video_coding::H264SpsPpsTracker::kInsert:
308 break;
309 }
310
philipelfd5a20f2016-11-15 00:57:57 -0800311 } else {
philipela45102f2017-02-22 05:30:39 -0800312 uint8_t* data = new uint8_t[packet.sizeBytes];
313 memcpy(data, packet.dataPtr, packet.sizeBytes);
314 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000315 }
philipela45102f2017-02-22 05:30:39 -0800316
317 packet_buffer_->InsertPacket(&packet);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000318 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
nisseb1f2ff92017-06-09 04:01:55 -0700321void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
322 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200323 RtpPacketReceived packet;
324 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700325 return;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200326 if (packet.PayloadType() == config_.rtp.red_payload_type) {
327 RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation";
328 return;
329 }
330
Niels Möllerb0573bc2017-09-25 10:47:00 +0200331 packet.IdentifyExtensions(rtp_header_extensions_);
332 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200333 // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both
334 // original (decapsulated) media packets and recovered packets to
335 // this callback. We need a way to distinguish, for setting
336 // packet.recovered() correctly. Ideally, move RED decapsulation out
337 // of the Ulpfec implementation.
Niels Möllerb0573bc2017-09-25 10:47:00 +0200338
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200339 ReceivePacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000340}
341
nissed2ef3142017-05-11 08:00:58 -0700342// This method handles both regular RTP packets and packets recovered
343// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700344void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200345 RTC_DCHECK_RUN_ON(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700346
eladalon8b073052017-08-25 00:49:08 -0700347 if (!receiving_) {
348 return;
349 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000350
eladalon8b073052017-08-25 00:49:08 -0700351 if (!packet.recovered()) {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200352 // TODO(nisse): Exclude out-of-order packets?
eladalon8b073052017-08-25 00:49:08 -0700353 int64_t now_ms = clock_->TimeInMilliseconds();
Niels Möllerb0d4b412018-08-28 13:58:15 +0200354 {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200355 rtc::CritScope cs(&rtp_sources_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200356 last_received_rtp_timestamp_ = packet.Timestamp();
357 last_received_rtp_system_time_ms_ = now_ms;
Jonas Oreland49ac5952018-09-26 16:04:32 +0200358
359 std::vector<uint32_t> csrcs = packet.Csrcs();
Jonas Oreland967f7d52018-11-06 07:35:06 +0100360 contributing_sources_.Update(now_ms, csrcs,
Johannes Kronb5d91832019-05-21 13:19:22 +0200361 /* audio level */ absl::nullopt,
362 packet.Timestamp());
Niels Möllerb0d4b412018-08-28 13:58:15 +0200363 }
eladalon8b073052017-08-25 00:49:08 -0700364 // Periodically log the RTP header of incoming packets.
365 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200366 rtc::StringBuilder ss;
eladalon8b073052017-08-25 00:49:08 -0700367 ss << "Packet received on SSRC: " << packet.Ssrc()
368 << " with payload type: " << static_cast<int>(packet.PayloadType())
369 << ", timestamp: " << packet.Timestamp()
370 << ", sequence number: " << packet.SequenceNumber()
371 << ", arrival time: " << packet.arrival_time_ms();
372 int32_t time_offset;
373 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
374 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800375 }
eladalon8b073052017-08-25 00:49:08 -0700376 uint32_t send_time;
377 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
378 ss << ", abs send time: " << send_time;
379 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100380 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700381 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000382 }
383 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000384
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200385 ReceivePacket(packet);
nisse38cc1d62017-02-13 05:59:46 -0800386
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000387 // Update receive statistics after ReceivePacket.
388 // Receive statistics will be reset if the payload type changes (make sure
389 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700390 if (!packet.recovered()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200391 rtp_receive_statistics_->OnRtpPacket(packet);
nissed2ef3142017-05-11 08:00:58 -0700392 }
eladalonc0d481a2017-08-02 07:39:07 -0700393
394 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
395 secondary_sink->OnRtpPacket(packet);
396 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000397}
398
Niels Möller41684372019-03-25 15:51:03 +0100399void RtpVideoStreamReceiver::RequestKeyFrame() {
400 rtp_rtcp_->RequestKeyFrame();
mflodmancfc8e3b2016-05-03 21:22:04 -0700401}
402
Elad Alon7d6a4c02019-02-25 13:00:51 +0100403void RtpVideoStreamReceiver::SendLossNotification(
404 uint16_t last_decoded_seq_num,
405 uint16_t last_received_seq_num,
406 bool decodability_flag) {
Elad Alonfadb1812019-05-24 13:40:02 +0200407 RTC_DCHECK(config_.rtp.lntf.enabled);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100408 rtp_rtcp_->SendLossNotification(last_decoded_seq_num, last_received_seq_num,
409 decodability_flag);
410}
411
nisseb1f2ff92017-06-09 04:01:55 -0700412bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700413 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800414}
415
nisseb1f2ff92017-06-09 04:01:55 -0700416bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700417 return config_.rtp.nack.rtp_history_ms > 0;
418}
419
nisseb1f2ff92017-06-09 04:01:55 -0700420void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700421 const std::vector<uint16_t>& sequence_numbers) {
422 rtp_rtcp_->SendNack(sequence_numbers);
423}
424
Benjamin Wright52426ed2019-03-01 11:01:59 -0800425bool RtpVideoStreamReceiver::IsDecryptable() const {
426 return frames_decryptable_.load();
427}
428
Elad Alonb4643ad2019-02-22 11:19:50 +0100429void RtpVideoStreamReceiver::OnAssembledFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800430 std::unique_ptr<video_coding::RtpFrameObject> frame) {
Benjamin Wright00765292018-11-30 16:18:26 -0800431 RTC_DCHECK_RUN_ON(&network_tc_);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100432 RTC_DCHECK(frame);
433
434 absl::optional<RtpGenericFrameDescriptor> descriptor =
435 frame->GetGenericFrameDescriptor();
436
437 if (loss_notification_controller_ && descriptor) {
438 loss_notification_controller_->OnAssembledFrame(
439 frame->first_seq_num(), descriptor->FrameId(),
440 descriptor->Discardable().value_or(false),
441 descriptor->FrameDependenciesDiffs());
442 } else if (!has_received_frame_) {
443 // Request a key frame as soon as possible.
Niels Möller8f7ce222019-03-21 15:43:58 +0100444 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Niels Möller479c0552019-05-23 16:57:01 +0200445 RequestKeyFrame();
Benjamin Wright39feabe2018-10-22 13:33:09 -0700446 }
447 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100448
449 has_received_frame_ = true;
450
Benjamin Wright00765292018-11-30 16:18:26 -0800451 if (buffered_frame_decryptor_ == nullptr) {
452 reference_finder_->ManageFrame(std::move(frame));
453 } else {
454 buffered_frame_decryptor_->ManageEncryptedFrame(std::move(frame));
Benjamin Wright192eeec2018-10-17 17:27:25 -0700455 }
philipelfd5a20f2016-11-15 00:57:57 -0800456}
457
nisseb1f2ff92017-06-09 04:01:55 -0700458void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100459 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800460 {
461 rtc::CritScope lock(&last_seq_num_cs_);
462 video_coding::RtpFrameObject* rtp_frame =
463 static_cast<video_coding::RtpFrameObject*>(frame.get());
philipel0fa82a62018-03-19 15:34:53 +0100464 last_seq_num_for_pic_id_[rtp_frame->id.picture_id] =
465 rtp_frame->last_seq_num();
philipelfd5a20f2016-11-15 00:57:57 -0800466 }
467 complete_frame_callback_->OnCompleteFrame(std::move(frame));
468}
469
Benjamin Wright00765292018-11-30 16:18:26 -0800470void RtpVideoStreamReceiver::OnDecryptedFrame(
471 std::unique_ptr<video_coding::RtpFrameObject> frame) {
472 reference_finder_->ManageFrame(std::move(frame));
473}
474
Benjamin Wright2af5dcb2019-04-09 20:08:41 +0000475void RtpVideoStreamReceiver::OnDecryptionStatusChange(
476 FrameDecryptorInterface::Status status) {
477 frames_decryptable_.store(
478 (status == FrameDecryptorInterface::Status::kOk) ||
479 (status == FrameDecryptorInterface::Status::kRecoverable));
Benjamin Wright52426ed2019-03-01 11:01:59 -0800480}
481
Benjamin Wrighta5564482019-04-03 10:44:18 -0700482void RtpVideoStreamReceiver::SetFrameDecryptor(
483 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
484 RTC_DCHECK_RUN_ON(&network_tc_);
485 if (buffered_frame_decryptor_ == nullptr) {
486 buffered_frame_decryptor_ =
487 absl::make_unique<BufferedFrameDecryptor>(this, this);
488 }
489 buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor));
490}
491
Tommi81de14f2018-03-25 22:19:25 +0200492void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800493 if (nack_module_)
494 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800495}
496
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200497absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700498 return packet_buffer_->LastReceivedPacketMs();
499}
500
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200501absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
nisseb1f2ff92017-06-09 04:01:55 -0700502 const {
philipel3184f8e2017-05-18 08:08:53 -0700503 return packet_buffer_->LastReceivedKeyframePacketMs();
504}
505
eladalonc0d481a2017-08-02 07:39:07 -0700506void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200507 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Steve Antonbd631a02019-03-28 10:51:27 -0700508 RTC_DCHECK(!absl::c_linear_search(secondary_sinks_, sink));
eladalonc0d481a2017-08-02 07:39:07 -0700509 secondary_sinks_.push_back(sink);
510}
511
512void RtpVideoStreamReceiver::RemoveSecondarySink(
513 const RtpPacketSinkInterface* sink) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200514 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Steve Antonbd631a02019-03-28 10:51:27 -0700515 auto it = absl::c_find(secondary_sinks_, sink);
eladalonc0d481a2017-08-02 07:39:07 -0700516 if (it == secondary_sinks_.end()) {
517 // We might be rolling-back a call whose setup failed mid-way. In such a
518 // case, it's simpler to remove "everything" rather than remember what
519 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100520 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700521 return;
522 }
523 secondary_sinks_.erase(it);
524}
525
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200526void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) {
527 if (packet.payload_size() == 0) {
Niels Möller0b926782018-08-21 17:49:24 +0200528 // Padding or keep-alive packet.
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200529 // TODO(nisse): Could drop empty packets earlier, but need to figure out how
530 // they should be counted in stats.
Niels Möller0b926782018-08-21 17:49:24 +0200531 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
nisse30e89312017-05-29 08:16:37 -0700532 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000533 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200534 if (packet.PayloadType() == config_.rtp.red_payload_type) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200535 ParseAndHandleEncapsulatingHeader(packet);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200536 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000537 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200538
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200539 const auto type_it = payload_type_map_.find(packet.PayloadType());
540 if (type_it == payload_type_map_.end()) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200541 return;
542 }
543 auto depacketizer =
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200544 absl::WrapUnique(RtpDepacketizer::Create(type_it->second));
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200545
546 if (!depacketizer) {
547 RTC_LOG(LS_ERROR) << "Failed to create depacketizer.";
548 return;
549 }
550 RtpDepacketizer::ParsedPayload parsed_payload;
551 if (!depacketizer->Parse(&parsed_payload, packet.payload().data(),
552 packet.payload().size())) {
553 RTC_LOG(LS_WARNING) << "Failed parsing payload.";
554 return;
555 }
556
Niels Möller125b5d62019-03-11 16:11:07 +0100557 RTPHeader rtp_header;
558 packet.GetHeader(&rtp_header);
559 RTPVideoHeader video_header = parsed_payload.video_header();
560 video_header.rotation = kVideoRotation_0;
561 video_header.content_type = VideoContentType::UNSPECIFIED;
562 video_header.video_timing.flags = VideoSendTiming::kInvalid;
563 video_header.is_last_packet_in_frame = rtp_header.markerBit;
564 video_header.frame_marking.temporal_id = kNoTemporalIdx;
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500565
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100566 if (parsed_payload.video_header().codec == kVideoCodecVP9) {
567 const RTPVideoHeaderVP9& codec_header = absl::get<RTPVideoHeaderVP9>(
568 parsed_payload.video_header().video_type_header);
Niels Möller125b5d62019-03-11 16:11:07 +0100569 video_header.is_last_packet_in_frame |= codec_header.end_of_frame;
570 video_header.is_first_packet_in_frame |= codec_header.beginning_of_frame;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100571 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200572
Niels Möller125b5d62019-03-11 16:11:07 +0100573 packet.GetExtension<VideoOrientation>(&video_header.rotation);
574 packet.GetExtension<VideoContentTypeExtension>(&video_header.content_type);
575 packet.GetExtension<VideoTimingExtension>(&video_header.video_timing);
576 packet.GetExtension<PlayoutDelayLimits>(&video_header.playout_delay);
577 packet.GetExtension<FrameMarkingExtension>(&video_header.frame_marking);
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500578
Johannes Krond51ec582019-04-15 13:32:41 +0200579 // Color space should only be transmitted in the last packet of a frame,
580 // therefore, neglect it otherwise so that last_color_space_ is not reset by
581 // mistake.
582 if (video_header.is_last_packet_in_frame) {
583 video_header.color_space = packet.GetExtension<ColorSpaceExtension>();
584 if (video_header.color_space ||
Niels Möllerabbc50e2019-04-24 09:41:16 +0200585 video_header.frame_type == VideoFrameType::kVideoFrameKey) {
Johannes Krond51ec582019-04-15 13:32:41 +0200586 // Store color space since it's only transmitted when changed or for key
587 // frames. Color space will be cleared if a key frame is transmitted
588 // without color space information.
589 last_color_space_ = video_header.color_space;
590 } else if (last_color_space_) {
591 video_header.color_space = last_color_space_;
592 }
Johannes Krond0b69a82018-12-03 14:18:53 +0100593 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500594
philipel2837edc2018-10-02 13:55:47 +0200595 absl::optional<RtpGenericFrameDescriptor> generic_descriptor_wire;
596 generic_descriptor_wire.emplace();
Elad Alonccb9b752019-02-19 13:01:31 +0100597 const bool generic_descriptor_v00 =
598 packet.GetExtension<RtpGenericFrameDescriptorExtension00>(
599 &generic_descriptor_wire.value());
600 const bool generic_descriptor_v01 =
601 packet.GetExtension<RtpGenericFrameDescriptorExtension01>(
602 &generic_descriptor_wire.value());
603 if (generic_descriptor_v00 && generic_descriptor_v01) {
604 RTC_LOG(LS_WARNING) << "RTP packet had two different GFD versions.";
605 return;
606 }
607
608 if (generic_descriptor_v00 || generic_descriptor_v01) {
609 if (generic_descriptor_v00) {
610 generic_descriptor_wire->SetByteRepresentation(
611 packet.GetRawExtension<RtpGenericFrameDescriptorExtension00>());
612 } else {
613 generic_descriptor_wire->SetByteRepresentation(
614 packet.GetRawExtension<RtpGenericFrameDescriptorExtension01>());
615 }
616
Niels Möller125b5d62019-03-11 16:11:07 +0100617 video_header.is_first_packet_in_frame =
philipel2837edc2018-10-02 13:55:47 +0200618 generic_descriptor_wire->FirstPacketInSubFrame();
Niels Möller125b5d62019-03-11 16:11:07 +0100619 video_header.is_last_packet_in_frame =
620 rtp_header.markerBit || generic_descriptor_wire->LastPacketInSubFrame();
philipelfab91292018-10-17 14:36:08 +0200621
622 if (generic_descriptor_wire->FirstPacketInSubFrame()) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200623 video_header.frame_type =
philipelfab91292018-10-17 14:36:08 +0200624 generic_descriptor_wire->FrameDependenciesDiffs().empty()
Niels Möller8f7ce222019-03-21 15:43:58 +0100625 ? VideoFrameType::kVideoFrameKey
626 : VideoFrameType::kVideoFrameDelta;
philipelfab91292018-10-17 14:36:08 +0200627 }
628
Niels Möller125b5d62019-03-11 16:11:07 +0100629 video_header.width = generic_descriptor_wire->Width();
630 video_header.height = generic_descriptor_wire->Height();
philipel2837edc2018-10-02 13:55:47 +0200631 } else {
632 generic_descriptor_wire.reset();
philipelb3e42a42018-09-13 10:57:14 +0200633 }
634
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200635 OnReceivedPayloadData(parsed_payload.payload, parsed_payload.payload_length,
Niels Möllerabbc50e2019-04-24 09:41:16 +0200636 rtp_header, video_header, generic_descriptor_wire,
637 packet.recovered());
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000638}
639
nisseb1f2ff92017-06-09 04:01:55 -0700640void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
Niels Möller1f3206c2018-09-14 08:26:32 +0200641 const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200642 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200643 if (packet.PayloadType() == config_.rtp.red_payload_type &&
644 packet.payload_size() > 0) {
645 if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) {
646 rtp_receive_statistics_->FecPacketReceived(packet);
Peter Boström0b250722016-04-22 18:23:15 +0200647 // Notify video_receiver about received FEC packets to avoid NACKing these
648 // packets.
Niels Möller1f3206c2018-09-14 08:26:32 +0200649 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000650 }
Niels Möller1f3206c2018-09-14 08:26:32 +0200651 RTPHeader header;
652 packet.GetHeader(&header);
Niels Möller3f027b32018-03-14 08:04:58 +0100653 if (ulpfec_receiver_->AddReceivedRedPacket(
Niels Möller1f3206c2018-09-14 08:26:32 +0200654 header, packet.data(), packet.size(),
655 config_.rtp.ulpfec_payload_type) != 0) {
nisse30e89312017-05-29 08:16:37 -0700656 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000657 }
nisse30e89312017-05-29 08:16:37 -0700658 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000659 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000660}
661
Niels Möllerbc010472018-03-23 13:22:29 +0100662// In the case of a video stream without picture ids and no rtx the
663// RtpFrameReferenceFinder will need to know about padding to
664// correctly calculate frame references.
665void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
666 reference_finder_->PaddingReceived(seq_num);
667 packet_buffer_->PaddingReceived(seq_num);
Niels Möllerbc010472018-03-23 13:22:29 +0100668 if (nack_module_) {
Ying Wangb32bb952018-10-31 10:12:27 +0100669 nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false,
670 /* is _recovered = */ false);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000671 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100672 if (loss_notification_controller_) {
673 RTC_LOG(LS_WARNING)
674 << "LossNotificationController does not expect empty packets.";
675 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000676}
677
nisseb1f2ff92017-06-09 04:01:55 -0700678bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
679 size_t rtcp_packet_length) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200680 RTC_DCHECK_RUN_ON(&worker_task_checker_);
eladalon8b073052017-08-25 00:49:08 -0700681
682 if (!receiving_) {
683 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100684 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000685
Per83d09102016-04-15 14:59:13 +0200686 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000687
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000688 int64_t rtt = 0;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200689 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000690 if (rtt == 0) {
691 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100692 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000693 }
694 uint32_t ntp_secs = 0;
695 uint32_t ntp_frac = 0;
696 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100697 uint32_t recieved_ntp_secs = 0;
698 uint32_t recieved_ntp_frac = 0;
699 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
700 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000701 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100702 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000703 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100704 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
705 int64_t time_since_recieved =
706 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
707 // Don't use old SRs to estimate time.
708 if (time_since_recieved <= 1) {
709 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
710 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000711
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100712 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000713}
714
philipeld4fac692017-09-04 07:03:46 -0700715void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800716 if (!nack_module_)
717 return;
718
philipela45102f2017-02-22 05:30:39 -0800719 int seq_num = -1;
720 {
721 rtc::CritScope lock(&last_seq_num_cs_);
722 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
723 if (seq_num_it != last_seq_num_for_pic_id_.end())
724 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800725 }
philipela45102f2017-02-22 05:30:39 -0800726 if (seq_num != -1)
727 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800728}
729
philipeld4fac692017-09-04 07:03:46 -0700730void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800731 int seq_num = -1;
732 {
733 rtc::CritScope lock(&last_seq_num_cs_);
734 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
735 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
736 seq_num = seq_num_it->second;
737 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
738 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800739 }
philipela45102f2017-02-22 05:30:39 -0800740 }
741 if (seq_num != -1) {
742 packet_buffer_->ClearTo(seq_num);
743 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800744 }
745}
746
nisseb1f2ff92017-06-09 04:01:55 -0700747void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700748 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
749 : RtcpMode::kOff);
750}
751
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100752int RtpVideoStreamReceiver::GetUniqueFramesSeen() const {
753 return packet_buffer_->GetUniqueFramesSeen();
754}
755
nisseb1f2ff92017-06-09 04:01:55 -0700756void RtpVideoStreamReceiver::StartReceive() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200757 RTC_DCHECK_RUN_ON(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000758 receiving_ = true;
759}
760
nisseb1f2ff92017-06-09 04:01:55 -0700761void RtpVideoStreamReceiver::StopReceive() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200762 RTC_DCHECK_RUN_ON(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000763 receiving_ = false;
764}
765
nisseb1f2ff92017-06-09 04:01:55 -0700766void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700767 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800768 if (counter.first_packet_time_ms == -1)
769 return;
770
771 int64_t elapsed_sec =
772 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
773 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
774 return;
775
mflodmandc7d0d22016-05-06 05:32:22 -0700776 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700777 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700778 "WebRTC.Video.ReceivedFecPacketsInPercent",
779 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
780 }
781 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700782 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
783 static_cast<int>(counter.num_recovered_packets *
784 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700785 }
786}
787
nisseb1f2ff92017-06-09 04:01:55 -0700788void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800789 auto codec_params_it = pt_codec_params_.find(payload_type);
790 if (codec_params_it == pt_codec_params_.end())
791 return;
792
Mirko Bonadei675513b2017-11-09 11:09:25 +0100793 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
794 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800795
796 H264SpropParameterSets sprop_decoder;
797 auto sprop_base64_it =
798 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
799
800 if (sprop_base64_it == codec_params_it->second.end())
801 return;
802
johan62d02c32017-01-24 04:38:27 -0800803 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800804 return;
805
johand2b092f2017-01-24 02:38:17 -0800806 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
807 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800808}
809
Jonas Oreland49ac5952018-09-26 16:04:32 +0200810std::vector<webrtc::RtpSource> RtpVideoStreamReceiver::GetSources() const {
811 int64_t now_ms = rtc::TimeMillis();
812 std::vector<RtpSource> sources;
813 {
814 rtc::CritScope cs(&rtp_sources_lock_);
815 sources = contributing_sources_.GetSources(now_ms);
816 if (last_received_rtp_system_time_ms_ >=
817 now_ms - ContributingSources::kHistoryMs) {
Johannes Kronb5d91832019-05-21 13:19:22 +0200818 RTC_DCHECK(last_received_rtp_timestamp_.has_value());
Jonas Oreland49ac5952018-09-26 16:04:32 +0200819 sources.emplace_back(*last_received_rtp_system_time_ms_,
Johannes Kronb5d91832019-05-21 13:19:22 +0200820 config_.rtp.remote_ssrc, RtpSourceType::SSRC,
821 /* audio_level */ absl::nullopt,
822 *last_received_rtp_timestamp_);
Jonas Oreland49ac5952018-09-26 16:04:32 +0200823 }
824 }
825 return sources;
826}
827
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000828} // namespace webrtc