blob: ae3475b7d0f8d549edb4e156717f377cfa3157a7 [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
philipel7acc4a42019-09-26 11:25:52 +020013#include <algorithm>
14#include <limits>
philipelfd5a20f2016-11-15 00:57:57 -080015#include <utility>
ilnik04f4d122017-06-19 07:18:55 -070016#include <vector>
mflodman@webrtc.org4fd55272013-02-06 17:46:39 +000017
Steve Antonbd631a02019-03-28 10:51:27 -070018#include "absl/algorithm/container.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020019#include "absl/memory/memory.h"
Danil Chapovalovcebdbf62019-12-13 16:08:18 +010020#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "media/base/media_constants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/pacing/packet_router.h"
23#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
24#include "modules/rtp_rtcp/include/receive_statistics.h"
25#include "modules/rtp_rtcp/include/rtp_cvo.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/rtp_rtcp/include/rtp_rtcp.h"
27#include "modules/rtp_rtcp/include/ulpfec_receiver.h"
Danil Chapovalovcebdbf62019-12-13 16:08:18 +010028#include "modules/rtp_rtcp/source/create_video_rtp_depacketizer.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020029#include "modules/rtp_rtcp/source/rtp_format.h"
philipelb3e42a42018-09-13 10:57:14 +020030#include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
32#include "modules/rtp_rtcp/source/rtp_packet_received.h"
Niels Möller2ff1f2a2018-08-09 16:16:34 +020033#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
Danil Chapovalovcebdbf62019-12-13 16:08:18 +010034#include "modules/rtp_rtcp/source/video_rtp_depacketizer.h"
35#include "modules/rtp_rtcp/source/video_rtp_depacketizer_raw.h"
Niels Möllerfe407b72019-09-10 10:48:48 +020036#include "modules/utility/include/process_thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "modules/video_coding/frame_object.h"
38#include "modules/video_coding/h264_sprop_parameter_sets.h"
39#include "modules/video_coding/h264_sps_pps_tracker.h"
Ilya Nikolaevskiy8643b782018-06-07 16:15:40 +020040#include "modules/video_coding/nack_module.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "modules/video_coding/packet_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/checks.h"
43#include "rtc_base/location.h"
44#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020045#include "rtc_base/strings/string_builder.h"
Karl Wiberg80ba3332018-02-05 10:33:35 +010046#include "rtc_base/system/fallthrough.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "system_wrappers/include/field_trial.h"
48#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "video/receive_statistics_proxy.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000050
51namespace webrtc {
52
philipelfd5a20f2016-11-15 00:57:57 -080053namespace {
philipel3bf97cf2017-08-10 18:10:59 +020054// TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see:
55// crbug.com/752886
56constexpr int kPacketBufferStartSize = 512;
Johannes Kron201596f2018-10-22 14:33:39 +020057constexpr int kPacketBufferMaxSize = 2048;
Danil Chapovalovf7457e52019-09-20 17:57:15 +020058
59int PacketBufferMaxSize() {
60 // The group here must be a positive power of 2, in which case that is used as
61 // size. All other values shall result in the default value being used.
62 const std::string group_name =
63 webrtc::field_trial::FindFullName("WebRTC-PacketBufferMaxSize");
64 int packet_buffer_max_size = kPacketBufferMaxSize;
65 if (!group_name.empty() &&
66 (sscanf(group_name.c_str(), "%d", &packet_buffer_max_size) != 1 ||
67 packet_buffer_max_size <= 0 ||
68 // Verify that the number is a positive power of 2.
69 (packet_buffer_max_size & (packet_buffer_max_size - 1)) != 0)) {
70 RTC_LOG(LS_WARNING) << "Invalid packet buffer max size: " << group_name;
71 packet_buffer_max_size = kPacketBufferMaxSize;
72 }
73 return packet_buffer_max_size;
74}
75
Yves Gerey665174f2018-06-19 15:03:05 +020076} // namespace
philipelfd5a20f2016-11-15 00:57:57 -080077
mflodmanc0e58a32016-04-25 01:26:26 -070078std::unique_ptr<RtpRtcp> CreateRtpRtcpModule(
Sebastian Jansson8026d602019-03-04 19:39:01 +010079 Clock* clock,
mflodmanc0e58a32016-04-25 01:26:26 -070080 ReceiveStatistics* receive_statistics,
81 Transport* outgoing_transport,
82 RtcpRttStats* rtt_stats,
Erik Språnge3a10e12019-08-19 15:45:00 +020083 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
84 uint32_t local_ssrc) {
mflodmanc0e58a32016-04-25 01:26:26 -070085 RtpRtcp::Configuration configuration;
Sebastian Jansson8026d602019-03-04 19:39:01 +010086 configuration.clock = clock;
mflodmanc0e58a32016-04-25 01:26:26 -070087 configuration.audio = false;
88 configuration.receiver_only = true;
89 configuration.receive_statistics = receive_statistics;
90 configuration.outgoing_transport = outgoing_transport;
mflodmanc0e58a32016-04-25 01:26:26 -070091 configuration.rtt_stats = rtt_stats;
92 configuration.rtcp_packet_type_counter_observer =
93 rtcp_packet_type_counter_observer;
Erik Språng54d5d2c2019-08-20 17:22:36 +020094 configuration.local_media_ssrc = local_ssrc;
mflodmanc0e58a32016-04-25 01:26:26 -070095
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +010096 std::unique_ptr<RtpRtcp> rtp_rtcp = RtpRtcp::Create(configuration);
mflodmanc0e58a32016-04-25 01:26:26 -070097 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
98
99 return rtp_rtcp;
100}
101
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000102static const int kPacketLogIntervalMs = 10000;
103
Elad Alonef09c5b2019-05-31 13:25:50 +0200104RtpVideoStreamReceiver::RtcpFeedbackBuffer::RtcpFeedbackBuffer(
105 KeyFrameRequestSender* key_frame_request_sender,
106 NackSender* nack_sender,
107 LossNotificationSender* loss_notification_sender)
108 : key_frame_request_sender_(key_frame_request_sender),
109 nack_sender_(nack_sender),
110 loss_notification_sender_(loss_notification_sender),
111 request_key_frame_(false) {
112 RTC_DCHECK(key_frame_request_sender_);
113 RTC_DCHECK(nack_sender_);
114 RTC_DCHECK(loss_notification_sender_);
115}
116
117void RtpVideoStreamReceiver::RtcpFeedbackBuffer::RequestKeyFrame() {
118 rtc::CritScope lock(&cs_);
119 request_key_frame_ = true;
120}
121
122void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendNack(
Elad Alonef09c5b2019-05-31 13:25:50 +0200123 const std::vector<uint16_t>& sequence_numbers,
124 bool buffering_allowed) {
125 RTC_DCHECK(!sequence_numbers.empty());
126 rtc::CritScope lock(&cs_);
127 nack_sequence_numbers_.insert(nack_sequence_numbers_.end(),
128 sequence_numbers.cbegin(),
129 sequence_numbers.cend());
130 if (!buffering_allowed) {
131 // Note that while *buffering* is not allowed, *batching* is, meaning that
132 // previously buffered messages may be sent along with the current message.
133 SendBufferedRtcpFeedback();
134 }
135}
136
137void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendLossNotification(
138 uint16_t last_decoded_seq_num,
139 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200140 bool decodability_flag,
141 bool buffering_allowed) {
142 RTC_DCHECK(buffering_allowed);
Elad Alonef09c5b2019-05-31 13:25:50 +0200143 rtc::CritScope lock(&cs_);
Elad Alon36690cd2019-06-04 22:59:54 +0200144 RTC_DCHECK(!lntf_state_)
Elad Alonef09c5b2019-05-31 13:25:50 +0200145 << "SendLossNotification() called twice in a row with no call to "
146 "SendBufferedRtcpFeedback() in between.";
147 lntf_state_ = absl::make_optional<LossNotificationState>(
148 last_decoded_seq_num, last_received_seq_num, decodability_flag);
149}
150
Elad Alonef09c5b2019-05-31 13:25:50 +0200151void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendBufferedRtcpFeedback() {
152 bool request_key_frame = false;
153 std::vector<uint16_t> nack_sequence_numbers;
154 absl::optional<LossNotificationState> lntf_state;
155
156 {
157 rtc::CritScope lock(&cs_);
158 std::swap(request_key_frame, request_key_frame_);
159 std::swap(nack_sequence_numbers, nack_sequence_numbers_);
160 std::swap(lntf_state, lntf_state_);
161 }
162
Elad Alone86af2c2019-06-03 14:37:50 +0200163 if (lntf_state) {
164 // If either a NACK or a key frame request is sent, we should buffer
165 // the LNTF and wait for them (NACK or key frame request) to trigger
166 // the compound feedback message.
167 // Otherwise, the LNTF should be sent out immediately.
168 const bool buffering_allowed =
169 request_key_frame || !nack_sequence_numbers.empty();
170
171 loss_notification_sender_->SendLossNotification(
172 lntf_state->last_decoded_seq_num, lntf_state->last_received_seq_num,
173 lntf_state->decodability_flag, buffering_allowed);
174 }
175
Elad Alonef09c5b2019-05-31 13:25:50 +0200176 if (request_key_frame) {
177 key_frame_request_sender_->RequestKeyFrame();
178 } else if (!nack_sequence_numbers.empty()) {
179 nack_sender_->SendNack(nack_sequence_numbers, true);
180 }
Elad Alonef09c5b2019-05-31 13:25:50 +0200181}
182
nisseb1f2ff92017-06-09 04:01:55 -0700183RtpVideoStreamReceiver::RtpVideoStreamReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100184 Clock* clock,
mflodmanfa666592016-04-28 23:15:33 -0700185 Transport* transport,
186 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700187 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +0200188 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -0700189 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -0700190 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +0200191 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -0800192 NackSender* nack_sender,
Niels Möller2f5554d2019-05-29 13:35:14 +0200193 KeyFrameRequestSender* keyframe_request_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700194 video_coding::OnCompleteFrameCallback* complete_frame_callback,
195 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
Sebastian Jansson8026d602019-03-04 19:39:01 +0100196 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +0200197 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -0700198 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700199 process_thread_(process_thread),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100200 ntp_estimator_(clock),
Niels Möllerb0573bc2017-09-25 10:47:00 +0200201 rtp_header_extensions_(config_.rtp.extensions),
nisseca5706d2017-09-11 02:32:16 -0700202 rtp_receive_statistics_(rtp_receive_statistics),
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200203 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc,
204 this,
205 config->rtp.extensions)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000206 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700207 last_packet_log_ms_(-1),
Niels Möller2f5554d2019-05-29 13:35:14 +0200208 rtp_rtcp_(CreateRtpRtcpModule(clock,
209 rtp_receive_statistics_,
210 transport,
211 rtt_stats,
Erik Språnge3a10e12019-08-19 15:45:00 +0200212 receive_stats_proxy,
213 config_.rtp.local_ssrc)),
philipelfd5a20f2016-11-15 00:57:57 -0800214 complete_frame_callback_(complete_frame_callback),
Niels Möller2f5554d2019-05-29 13:35:14 +0200215 keyframe_request_sender_(keyframe_request_sender),
Elad Alonef09c5b2019-05-31 13:25:50 +0200216 // TODO(bugs.webrtc.org/10336): Let |rtcp_feedback_buffer_| communicate
217 // directly with |rtp_rtcp_|.
218 rtcp_feedback_buffer_(this, nack_sender, this),
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200219 packet_buffer_(clock_, kPacketBufferStartSize, PacketBufferMaxSize()),
Benjamin Wright52426ed2019-03-01 11:01:59 -0800220 has_received_frame_(false),
Ruslan Burakovd08bb1e2019-11-27 16:49:10 +0100221 frames_decryptable_(false),
222 absolute_capture_time_receiver_(clock) {
eladalon822ff2b2017-08-01 06:30:28 -0700223 constexpr bool remb_candidate = true;
Niels Möller60f4e292019-05-20 11:06:33 +0200224 if (packet_router_)
225 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700226
Tommi733b5472016-06-10 17:58:01 +0200227 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700228 << "A stream should not be configured with RTCP disabled. This value is "
229 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700230 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
231 RTC_DCHECK(config_.rtp.local_ssrc != 0);
232 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
233
Tommi733b5472016-06-10 17:58:01 +0200234 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
stefanb4ab3812017-06-09 06:12:11 -0700235 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmandc7d0d22016-05-06 05:32:22 -0700236
mflodmancfc8e3b2016-05-03 21:22:04 -0700237 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200238 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
239 ? kMaxPacketAgeToNack
240 : kDefaultMaxReorderingThreshold;
Niels Möller87da1092019-05-24 14:04:28 +0200241 rtp_receive_statistics_->SetMaxReorderingThreshold(config_.rtp.remote_ssrc,
242 max_reordering_threshold);
243 // TODO(nisse): For historic reasons, we applied the above
244 // max_reordering_threshold also for RTX stats, which makes little sense since
245 // we don't NACK rtx packets. Consider deleting the below block, and rely on
246 // the default threshold.
247 if (config_.rtp.rtx_ssrc) {
248 rtp_receive_statistics_->SetMaxReorderingThreshold(
249 config_.rtp.rtx_ssrc, max_reordering_threshold);
250 }
Tommi733b5472016-06-10 17:58:01 +0200251 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700252 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
253
254 // Stats callback for CNAME changes.
Niels Möller4d7c4052019-08-05 12:45:19 +0200255 rtp_rtcp_->RegisterRtcpCnameCallback(receive_stats_proxy);
mflodmandc7d0d22016-05-06 05:32:22 -0700256
tommidea489f2017-03-03 03:20:24 -0800257 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800258
Elad Alonfadb1812019-05-24 13:40:02 +0200259 if (config_.rtp.lntf.enabled) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100260 loss_notification_controller_ =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200261 std::make_unique<LossNotificationController>(&rtcp_feedback_buffer_,
262 &rtcp_feedback_buffer_);
Elad Alonca2c4302019-05-27 22:43:10 +0200263 }
264
265 if (config_.rtp.nack.rtp_history_ms != 0) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200266 nack_module_ = std::make_unique<NackModule>(clock_, &rtcp_feedback_buffer_,
267 &rtcp_feedback_buffer_);
tommidea489f2017-03-03 03:20:24 -0800268 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800269 }
philipelfd5a20f2016-11-15 00:57:57 -0800270
Elad Alona8f54612018-11-06 11:21:25 +0100271 reference_finder_ =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200272 std::make_unique<video_coding::RtpFrameReferenceFinder>(this);
Benjamin Wrighta5564482019-04-03 10:44:18 -0700273
Benjamin Wright00765292018-11-30 16:18:26 -0800274 // Only construct the encrypted receiver if frame encryption is enabled.
Benjamin Wrighta5564482019-04-03 10:44:18 -0700275 if (config_.crypto_options.sframe.require_frame_encryption) {
Benjamin Wright00765292018-11-30 16:18:26 -0800276 buffered_frame_decryptor_ =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200277 std::make_unique<BufferedFrameDecryptor>(this, this);
Benjamin Wrighta5564482019-04-03 10:44:18 -0700278 if (frame_decryptor != nullptr) {
279 buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor));
280 }
Benjamin Wright00765292018-11-30 16:18:26 -0800281 }
mflodmanc0e58a32016-04-25 01:26:26 -0700282}
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
nisseb1f2ff92017-06-09 04:01:55 -0700284RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700285 RTC_DCHECK(secondary_sinks_.empty());
286
tommif284b7f2017-02-27 01:59:36 -0800287 if (nack_module_) {
288 process_thread_->DeRegisterModule(nack_module_.get());
289 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
tommif284b7f2017-02-27 01:59:36 -0800291 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800292
Niels Möller60f4e292019-05-20 11:06:33 +0200293 if (packet_router_)
294 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700295 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000296}
297
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200298void RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800299 const VideoCodec& video_codec,
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200300 const std::map<std::string, std::string>& codec_params,
301 bool raw_payload) {
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100302 payload_type_map_.emplace(
303 video_codec.plType,
304 raw_payload ? std::make_unique<VideoRtpDepacketizerRaw>()
305 : CreateVideoRtpDepacketizer(video_codec.codecType));
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200306 pt_codec_params_.emplace(video_codec.plType, codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000307}
308
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200309absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200310 Syncable::Info info;
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200311 if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
312 &info.capture_time_ntp_frac, nullptr, nullptr,
313 &info.capture_time_source_clock) != 0) {
314 return absl::nullopt;
315 }
Niels Möllerb0d4b412018-08-28 13:58:15 +0200316 {
Chen Xing90f3b892019-06-25 10:16:14 +0200317 rtc::CritScope lock(&sync_info_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200318 if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
319 return absl::nullopt;
320 }
321 info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
322 info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
323 }
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200324
325 // Leaves info.current_delay_ms uninitialized.
326 return info;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000327}
328
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200329void RtpVideoStreamReceiver::OnReceivedPayloadData(
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100330 rtc::CopyOnWriteBuffer codec_payload,
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200331 const RtpPacketReceived& rtp_packet,
332 const RTPVideoHeader& video) {
Danil Chapovalov09860e02019-10-30 14:12:24 +0100333 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100334 video_coding::PacketBuffer::Packet packet(
335 rtp_packet, video, ntp_estimator_.Estimate(rtp_packet.Timestamp()),
336 clock_->TimeInMilliseconds());
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200337
Ruslan Burakovd08bb1e2019-11-27 16:49:10 +0100338 // Try to extrapolate absolute capture time if it is missing.
339 // TODO(bugs.webrtc.org/10739): Add support for estimated capture clock
340 // offset.
341 packet.packet_info.set_absolute_capture_time(
342 absolute_capture_time_receiver_.OnReceivePacket(
343 AbsoluteCaptureTimeReceiver::GetSource(packet.packet_info.ssrc(),
344 packet.packet_info.csrcs()),
345 packet.packet_info.rtp_timestamp(),
346 // Assume frequency is the same one for all video frames.
347 kVideoPayloadTypeFrequency,
348 packet.packet_info.absolute_capture_time()));
349
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200350 RTPVideoHeader& video_header = packet.video_header;
351 video_header.rotation = kVideoRotation_0;
352 video_header.content_type = VideoContentType::UNSPECIFIED;
353 video_header.video_timing.flags = VideoSendTiming::kInvalid;
354 video_header.is_last_packet_in_frame |= rtp_packet.Marker();
355 video_header.frame_marking.temporal_id = kNoTemporalIdx;
356
357 if (const auto* vp9_header =
358 absl::get_if<RTPVideoHeaderVP9>(&video_header.video_type_header)) {
359 video_header.is_last_packet_in_frame |= vp9_header->end_of_frame;
360 video_header.is_first_packet_in_frame |= vp9_header->beginning_of_frame;
361 }
362
363 rtp_packet.GetExtension<VideoOrientation>(&video_header.rotation);
364 rtp_packet.GetExtension<VideoContentTypeExtension>(
365 &video_header.content_type);
366 rtp_packet.GetExtension<VideoTimingExtension>(&video_header.video_timing);
367 rtp_packet.GetExtension<PlayoutDelayLimits>(&video_header.playout_delay);
368 rtp_packet.GetExtension<FrameMarkingExtension>(&video_header.frame_marking);
369
370 RtpGenericFrameDescriptor& generic_descriptor =
371 packet.generic_descriptor.emplace();
372 if (rtp_packet.GetExtension<RtpGenericFrameDescriptorExtension01>(
373 &generic_descriptor)) {
374 if (rtp_packet.HasExtension<RtpGenericFrameDescriptorExtension00>()) {
375 RTC_LOG(LS_WARNING) << "RTP packet had two different GFD versions.";
376 return;
377 }
378 generic_descriptor.SetByteRepresentation(
379 rtp_packet.GetRawExtension<RtpGenericFrameDescriptorExtension01>());
380 } else if ((rtp_packet.GetExtension<RtpGenericFrameDescriptorExtension00>(
381 &generic_descriptor))) {
382 generic_descriptor.SetByteRepresentation(
383 rtp_packet.GetRawExtension<RtpGenericFrameDescriptorExtension00>());
384 } else {
385 packet.generic_descriptor = absl::nullopt;
386 }
387 if (packet.generic_descriptor != absl::nullopt) {
388 video_header.is_first_packet_in_frame =
389 packet.generic_descriptor->FirstPacketInSubFrame();
390 video_header.is_last_packet_in_frame =
391 rtp_packet.Marker() ||
392 packet.generic_descriptor->LastPacketInSubFrame();
393
394 if (packet.generic_descriptor->FirstPacketInSubFrame()) {
395 video_header.frame_type =
396 packet.generic_descriptor->FrameDependenciesDiffs().empty()
397 ? VideoFrameType::kVideoFrameKey
398 : VideoFrameType::kVideoFrameDelta;
399 }
400
401 video_header.width = packet.generic_descriptor->Width();
402 video_header.height = packet.generic_descriptor->Height();
403 }
404
405 // Color space should only be transmitted in the last packet of a frame,
406 // therefore, neglect it otherwise so that last_color_space_ is not reset by
407 // mistake.
408 if (video_header.is_last_packet_in_frame) {
409 video_header.color_space = rtp_packet.GetExtension<ColorSpaceExtension>();
410 if (video_header.color_space ||
411 video_header.frame_type == VideoFrameType::kVideoFrameKey) {
412 // Store color space since it's only transmitted when changed or for key
413 // frames. Color space will be cleared if a key frame is transmitted
414 // without color space information.
415 last_color_space_ = video_header.color_space;
416 } else if (last_color_space_) {
417 video_header.color_space = last_color_space_;
418 }
419 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100420
Elad Alonca2c4302019-05-27 22:43:10 +0200421 if (loss_notification_controller_) {
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200422 if (rtp_packet.recovered()) {
Elad Alonca2c4302019-05-27 22:43:10 +0200423 // TODO(bugs.webrtc.org/10336): Implement support for reordering.
Niels Möllera7401422019-09-13 14:18:58 +0200424 RTC_LOG(LS_INFO)
Elad Alonca2c4302019-05-27 22:43:10 +0200425 << "LossNotificationController does not support reordering.";
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200426 } else if (!packet.generic_descriptor) {
Niels Möllera7401422019-09-13 14:18:58 +0200427 RTC_LOG(LS_WARNING) << "LossNotificationController requires generic "
428 "frame descriptor, but it is missing.";
Elad Alonca2c4302019-05-27 22:43:10 +0200429 } else {
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200430 loss_notification_controller_->OnReceivedPacket(
431 rtp_packet.SequenceNumber(), *packet.generic_descriptor);
Elad Alonca2c4302019-05-27 22:43:10 +0200432 }
433 }
434
Niels Möller8dad9b42018-08-22 10:36:35 +0200435 if (nack_module_) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200436 const bool is_keyframe =
437 video_header.is_first_packet_in_frame &&
438 video_header.frame_type == VideoFrameType::kVideoFrameKey;
Niels Möller8dad9b42018-08-22 10:36:35 +0200439
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100440 packet.times_nacked = nack_module_->OnReceivedPacket(
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200441 rtp_packet.SequenceNumber(), is_keyframe, rtp_packet.recovered());
Niels Möller8dad9b42018-08-22 10:36:35 +0200442 } else {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100443 packet.times_nacked = -1;
Niels Möller8dad9b42018-08-22 10:36:35 +0200444 }
philipelfd5a20f2016-11-15 00:57:57 -0800445
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100446 if (codec_payload.size() == 0) {
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100447 NotifyReceiverOfEmptyPacket(packet.seq_num);
Elad Alonef09c5b2019-05-31 13:25:50 +0200448 rtcp_feedback_buffer_.SendBufferedRtcpFeedback();
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200449 return;
philipel54ca9192017-03-21 05:45:18 -0700450 }
451
Niels Möllerd5e02f02019-02-20 13:12:21 +0100452 if (packet.codec() == kVideoCodecH264) {
philipela45102f2017-02-22 05:30:39 -0800453 // Only when we start to receive packets will we know what payload type
454 // that will be used. When we know the payload type insert the correct
455 // sps/pps into the tracker.
Danil Chapovalovaa3f5da2019-11-14 14:57:33 +0100456 if (packet.payload_type != last_payload_type_) {
457 last_payload_type_ = packet.payload_type;
458 InsertSpsPpsIntoTracker(packet.payload_type);
philipelfd5a20f2016-11-15 00:57:57 -0800459 }
460
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100461 video_coding::H264SpsPpsTracker::FixedBitstream fixed =
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100462 tracker_.CopyAndFixBitstream(
463 rtc::MakeArrayView(codec_payload.cdata(), codec_payload.size()),
464 &packet.video_header);
Danil Chapovalovfbec2ec2019-10-28 13:27:05 +0100465
466 switch (fixed.action) {
philipela45102f2017-02-22 05:30:39 -0800467 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
Elad Alonef09c5b2019-05-31 13:25:50 +0200468 rtcp_feedback_buffer_.RequestKeyFrame();
469 rtcp_feedback_buffer_.SendBufferedRtcpFeedback();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100470 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800471 case video_coding::H264SpsPpsTracker::kDrop:
Danil Chapovalovc71d85b2019-10-16 19:18:21 +0200472 return;
philipela45102f2017-02-22 05:30:39 -0800473 case video_coding::H264SpsPpsTracker::kInsert:
Danil Chapovalove3c48842019-12-02 15:54:27 +0100474 packet.video_payload = std::move(fixed.bitstream);
philipela45102f2017-02-22 05:30:39 -0800475 break;
476 }
477
philipelfd5a20f2016-11-15 00:57:57 -0800478 } else {
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100479 packet.video_payload = std::move(codec_payload);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000480 }
philipela45102f2017-02-22 05:30:39 -0800481
Elad Alonef09c5b2019-05-31 13:25:50 +0200482 rtcp_feedback_buffer_.SendBufferedRtcpFeedback();
Danil Chapovalov09860e02019-10-30 14:12:24 +0100483 frame_counter_.Add(packet.timestamp);
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200484 OnInsertedPacket(packet_buffer_.InsertPacket(&packet));
niklase@google.com470e71d2011-07-07 08:21:25 +0000485}
486
nisseb1f2ff92017-06-09 04:01:55 -0700487void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
488 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200489 RtpPacketReceived packet;
490 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700491 return;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200492 if (packet.PayloadType() == config_.rtp.red_payload_type) {
493 RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation";
494 return;
495 }
496
Niels Möllerb0573bc2017-09-25 10:47:00 +0200497 packet.IdentifyExtensions(rtp_header_extensions_);
498 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200499 // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both
500 // original (decapsulated) media packets and recovered packets to
501 // this callback. We need a way to distinguish, for setting
502 // packet.recovered() correctly. Ideally, move RED decapsulation out
503 // of the Ulpfec implementation.
Niels Möllerb0573bc2017-09-25 10:47:00 +0200504
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200505 ReceivePacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000506}
507
nissed2ef3142017-05-11 08:00:58 -0700508// This method handles both regular RTP packets and packets recovered
509// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700510void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200511 RTC_DCHECK_RUN_ON(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700512
eladalon8b073052017-08-25 00:49:08 -0700513 if (!receiving_) {
514 return;
515 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000516
eladalon8b073052017-08-25 00:49:08 -0700517 if (!packet.recovered()) {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200518 // TODO(nisse): Exclude out-of-order packets?
eladalon8b073052017-08-25 00:49:08 -0700519 int64_t now_ms = clock_->TimeInMilliseconds();
Niels Möllerb0d4b412018-08-28 13:58:15 +0200520 {
Chen Xing90f3b892019-06-25 10:16:14 +0200521 rtc::CritScope cs(&sync_info_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200522 last_received_rtp_timestamp_ = packet.Timestamp();
523 last_received_rtp_system_time_ms_ = now_ms;
524 }
eladalon8b073052017-08-25 00:49:08 -0700525 // Periodically log the RTP header of incoming packets.
526 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200527 rtc::StringBuilder ss;
eladalon8b073052017-08-25 00:49:08 -0700528 ss << "Packet received on SSRC: " << packet.Ssrc()
529 << " with payload type: " << static_cast<int>(packet.PayloadType())
530 << ", timestamp: " << packet.Timestamp()
531 << ", sequence number: " << packet.SequenceNumber()
532 << ", arrival time: " << packet.arrival_time_ms();
533 int32_t time_offset;
534 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
535 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800536 }
eladalon8b073052017-08-25 00:49:08 -0700537 uint32_t send_time;
538 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
539 ss << ", abs send time: " << send_time;
540 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100541 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700542 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000543 }
544 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000545
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200546 ReceivePacket(packet);
nisse38cc1d62017-02-13 05:59:46 -0800547
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000548 // Update receive statistics after ReceivePacket.
549 // Receive statistics will be reset if the payload type changes (make sure
550 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700551 if (!packet.recovered()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200552 rtp_receive_statistics_->OnRtpPacket(packet);
nissed2ef3142017-05-11 08:00:58 -0700553 }
eladalonc0d481a2017-08-02 07:39:07 -0700554
555 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
556 secondary_sink->OnRtpPacket(packet);
557 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000558}
559
Niels Möller41684372019-03-25 15:51:03 +0100560void RtpVideoStreamReceiver::RequestKeyFrame() {
Elad Alonef09c5b2019-05-31 13:25:50 +0200561 // TODO(bugs.webrtc.org/10336): Allow the sender to ignore key frame requests
562 // issued by anything other than the LossNotificationController if it (the
563 // sender) is relying on LNTF alone.
Niels Möller2f5554d2019-05-29 13:35:14 +0200564 if (keyframe_request_sender_) {
565 keyframe_request_sender_->RequestKeyFrame();
566 } else {
Niels Möllerdd0094a2019-06-04 14:46:27 +0200567 rtp_rtcp_->SendPictureLossIndication();
Niels Möller2f5554d2019-05-29 13:35:14 +0200568 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700569}
570
Elad Alon7d6a4c02019-02-25 13:00:51 +0100571void RtpVideoStreamReceiver::SendLossNotification(
572 uint16_t last_decoded_seq_num,
573 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200574 bool decodability_flag,
575 bool buffering_allowed) {
Elad Alonfadb1812019-05-24 13:40:02 +0200576 RTC_DCHECK(config_.rtp.lntf.enabled);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100577 rtp_rtcp_->SendLossNotification(last_decoded_seq_num, last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200578 decodability_flag, buffering_allowed);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100579}
580
nisseb1f2ff92017-06-09 04:01:55 -0700581bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700582 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800583}
584
nisseb1f2ff92017-06-09 04:01:55 -0700585bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700586 return config_.rtp.nack.rtp_history_ms > 0;
587}
588
nisseb1f2ff92017-06-09 04:01:55 -0700589void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700590 const std::vector<uint16_t>& sequence_numbers) {
591 rtp_rtcp_->SendNack(sequence_numbers);
592}
593
Benjamin Wright52426ed2019-03-01 11:01:59 -0800594bool RtpVideoStreamReceiver::IsDecryptable() const {
595 return frames_decryptable_.load();
596}
597
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200598void RtpVideoStreamReceiver::OnInsertedPacket(
599 video_coding::PacketBuffer::InsertResult result) {
600 for (std::unique_ptr<video_coding::RtpFrameObject>& frame : result.frames) {
601 OnAssembledFrame(std::move(frame));
602 }
603 if (result.buffer_cleared) {
604 RequestKeyFrame();
605 }
606}
607
Elad Alonb4643ad2019-02-22 11:19:50 +0100608void RtpVideoStreamReceiver::OnAssembledFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800609 std::unique_ptr<video_coding::RtpFrameObject> frame) {
Benjamin Wright00765292018-11-30 16:18:26 -0800610 RTC_DCHECK_RUN_ON(&network_tc_);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100611 RTC_DCHECK(frame);
612
613 absl::optional<RtpGenericFrameDescriptor> descriptor =
614 frame->GetGenericFrameDescriptor();
615
616 if (loss_notification_controller_ && descriptor) {
617 loss_notification_controller_->OnAssembledFrame(
618 frame->first_seq_num(), descriptor->FrameId(),
619 descriptor->Discardable().value_or(false),
620 descriptor->FrameDependenciesDiffs());
Elad Alonca2c4302019-05-27 22:43:10 +0200621 }
622
Elad Alonef09c5b2019-05-31 13:25:50 +0200623 // If frames arrive before a key frame, they would not be decodable.
624 // In that case, request a key frame ASAP.
Elad Alonca2c4302019-05-27 22:43:10 +0200625 if (!has_received_frame_) {
Niels Möller8f7ce222019-03-21 15:43:58 +0100626 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Elad Alonef09c5b2019-05-31 13:25:50 +0200627 // |loss_notification_controller_|, if present, would have already
628 // requested a key frame when the first packet for the non-key frame
629 // had arrived, so no need to replicate the request.
630 if (!loss_notification_controller_) {
631 RequestKeyFrame();
632 }
Benjamin Wright39feabe2018-10-22 13:33:09 -0700633 }
Elad Alonef09c5b2019-05-31 13:25:50 +0200634 has_received_frame_ = true;
Benjamin Wright39feabe2018-10-22 13:33:09 -0700635 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100636
philipel7acc4a42019-09-26 11:25:52 +0200637 rtc::CritScope lock(&reference_finder_lock_);
638 // Reset |reference_finder_| if |frame| is new and the codec have changed.
639 if (current_codec_) {
640 bool frame_is_newer =
641 AheadOf(frame->Timestamp(), last_assembled_frame_rtp_timestamp_);
642
643 if (frame->codec_type() != current_codec_) {
644 if (frame_is_newer) {
Ilya Nikolaevskiy815e00c2019-11-12 15:20:21 +0000645 // When we reset the |reference_finder_| we don't want new picture ids
646 // to overlap with old picture ids. To ensure that doesn't happen we
647 // start from the |last_completed_picture_id_| and add an offset in case
648 // of reordering.
649 reference_finder_ =
650 std::make_unique<video_coding::RtpFrameReferenceFinder>(
651 this, last_completed_picture_id_ +
652 std::numeric_limits<uint16_t>::max());
philipel7acc4a42019-09-26 11:25:52 +0200653 current_codec_ = frame->codec_type();
654 } else {
655 // Old frame from before the codec switch, discard it.
656 return;
657 }
658 }
659
660 if (frame_is_newer) {
661 last_assembled_frame_rtp_timestamp_ = frame->Timestamp();
662 }
663 } else {
664 current_codec_ = frame->codec_type();
665 last_assembled_frame_rtp_timestamp_ = frame->Timestamp();
666 }
667
Benjamin Wright00765292018-11-30 16:18:26 -0800668 if (buffered_frame_decryptor_ == nullptr) {
669 reference_finder_->ManageFrame(std::move(frame));
670 } else {
671 buffered_frame_decryptor_->ManageEncryptedFrame(std::move(frame));
Benjamin Wright192eeec2018-10-17 17:27:25 -0700672 }
philipelfd5a20f2016-11-15 00:57:57 -0800673}
674
nisseb1f2ff92017-06-09 04:01:55 -0700675void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100676 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800677 {
678 rtc::CritScope lock(&last_seq_num_cs_);
679 video_coding::RtpFrameObject* rtp_frame =
680 static_cast<video_coding::RtpFrameObject*>(frame.get());
philipel0fa82a62018-03-19 15:34:53 +0100681 last_seq_num_for_pic_id_[rtp_frame->id.picture_id] =
682 rtp_frame->last_seq_num();
philipelfd5a20f2016-11-15 00:57:57 -0800683 }
philipel7acc4a42019-09-26 11:25:52 +0200684 last_completed_picture_id_ =
685 std::max(last_completed_picture_id_, frame->id.picture_id);
philipelfd5a20f2016-11-15 00:57:57 -0800686 complete_frame_callback_->OnCompleteFrame(std::move(frame));
687}
688
Benjamin Wright00765292018-11-30 16:18:26 -0800689void RtpVideoStreamReceiver::OnDecryptedFrame(
690 std::unique_ptr<video_coding::RtpFrameObject> frame) {
philipel7acc4a42019-09-26 11:25:52 +0200691 rtc::CritScope lock(&reference_finder_lock_);
Benjamin Wright00765292018-11-30 16:18:26 -0800692 reference_finder_->ManageFrame(std::move(frame));
693}
694
Benjamin Wright2af5dcb2019-04-09 20:08:41 +0000695void RtpVideoStreamReceiver::OnDecryptionStatusChange(
696 FrameDecryptorInterface::Status status) {
697 frames_decryptable_.store(
698 (status == FrameDecryptorInterface::Status::kOk) ||
699 (status == FrameDecryptorInterface::Status::kRecoverable));
Benjamin Wright52426ed2019-03-01 11:01:59 -0800700}
701
Benjamin Wrighta5564482019-04-03 10:44:18 -0700702void RtpVideoStreamReceiver::SetFrameDecryptor(
703 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
704 RTC_DCHECK_RUN_ON(&network_tc_);
705 if (buffered_frame_decryptor_ == nullptr) {
706 buffered_frame_decryptor_ =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200707 std::make_unique<BufferedFrameDecryptor>(this, this);
Benjamin Wrighta5564482019-04-03 10:44:18 -0700708 }
709 buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor));
710}
711
Tommi81de14f2018-03-25 22:19:25 +0200712void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800713 if (nack_module_)
714 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800715}
716
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200717absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200718 return packet_buffer_.LastReceivedPacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700719}
720
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200721absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
nisseb1f2ff92017-06-09 04:01:55 -0700722 const {
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200723 return packet_buffer_.LastReceivedKeyframePacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700724}
725
eladalonc0d481a2017-08-02 07:39:07 -0700726void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200727 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Steve Antonbd631a02019-03-28 10:51:27 -0700728 RTC_DCHECK(!absl::c_linear_search(secondary_sinks_, sink));
eladalonc0d481a2017-08-02 07:39:07 -0700729 secondary_sinks_.push_back(sink);
730}
731
732void RtpVideoStreamReceiver::RemoveSecondarySink(
733 const RtpPacketSinkInterface* sink) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200734 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Steve Antonbd631a02019-03-28 10:51:27 -0700735 auto it = absl::c_find(secondary_sinks_, sink);
eladalonc0d481a2017-08-02 07:39:07 -0700736 if (it == secondary_sinks_.end()) {
737 // We might be rolling-back a call whose setup failed mid-way. In such a
738 // case, it's simpler to remove "everything" rather than remember what
739 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100740 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700741 return;
742 }
743 secondary_sinks_.erase(it);
744}
745
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200746void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) {
747 if (packet.payload_size() == 0) {
Niels Möller0b926782018-08-21 17:49:24 +0200748 // Padding or keep-alive packet.
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200749 // TODO(nisse): Could drop empty packets earlier, but need to figure out how
750 // they should be counted in stats.
Niels Möller0b926782018-08-21 17:49:24 +0200751 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
nisse30e89312017-05-29 08:16:37 -0700752 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000753 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200754 if (packet.PayloadType() == config_.rtp.red_payload_type) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200755 ParseAndHandleEncapsulatingHeader(packet);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200756 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000757 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200758
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200759 const auto type_it = payload_type_map_.find(packet.PayloadType());
760 if (type_it == payload_type_map_.end()) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200761 return;
762 }
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100763 absl::optional<VideoRtpDepacketizer::ParsedRtpPayload> parsed_payload =
764 type_it->second->Parse(packet.PayloadBuffer());
765 if (parsed_payload == absl::nullopt) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200766 RTC_LOG(LS_WARNING) << "Failed parsing payload.";
767 return;
768 }
769
Danil Chapovalovcebdbf62019-12-13 16:08:18 +0100770 OnReceivedPayloadData(std::move(parsed_payload->video_payload), packet,
771 parsed_payload->video_header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000772}
773
nisseb1f2ff92017-06-09 04:01:55 -0700774void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
Niels Möller1f3206c2018-09-14 08:26:32 +0200775 const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200776 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200777 if (packet.PayloadType() == config_.rtp.red_payload_type &&
778 packet.payload_size() > 0) {
779 if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) {
Peter Boström0b250722016-04-22 18:23:15 +0200780 // Notify video_receiver about received FEC packets to avoid NACKing these
781 // packets.
Niels Möller1f3206c2018-09-14 08:26:32 +0200782 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000783 }
Danil Chapovalov04fd2152019-09-20 11:40:12 +0200784 if (!ulpfec_receiver_->AddReceivedRedPacket(
785 packet, config_.rtp.ulpfec_payload_type)) {
nisse30e89312017-05-29 08:16:37 -0700786 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000787 }
nisse30e89312017-05-29 08:16:37 -0700788 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000789 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000790}
791
Niels Möllerbc010472018-03-23 13:22:29 +0100792// In the case of a video stream without picture ids and no rtx the
793// RtpFrameReferenceFinder will need to know about padding to
794// correctly calculate frame references.
795void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
philipel7acc4a42019-09-26 11:25:52 +0200796 {
797 rtc::CritScope lock(&reference_finder_lock_);
798 reference_finder_->PaddingReceived(seq_num);
799 }
Danil Chapovalovce1ffcd2019-10-22 17:12:42 +0200800 OnInsertedPacket(packet_buffer_.InsertPadding(seq_num));
Niels Möllerbc010472018-03-23 13:22:29 +0100801 if (nack_module_) {
Ying Wangb32bb952018-10-31 10:12:27 +0100802 nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false,
803 /* is _recovered = */ false);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000804 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100805 if (loss_notification_controller_) {
Elad Alonca2c4302019-05-27 22:43:10 +0200806 // TODO(bugs.webrtc.org/10336): Handle empty packets.
Elad Alon7d6a4c02019-02-25 13:00:51 +0100807 RTC_LOG(LS_WARNING)
808 << "LossNotificationController does not expect empty packets.";
809 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000810}
811
nisseb1f2ff92017-06-09 04:01:55 -0700812bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
813 size_t rtcp_packet_length) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200814 RTC_DCHECK_RUN_ON(&worker_task_checker_);
eladalon8b073052017-08-25 00:49:08 -0700815
816 if (!receiving_) {
817 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100818 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000819
Per83d09102016-04-15 14:59:13 +0200820 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000821
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000822 int64_t rtt = 0;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200823 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000824 if (rtt == 0) {
825 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100826 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000827 }
828 uint32_t ntp_secs = 0;
829 uint32_t ntp_frac = 0;
830 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100831 uint32_t recieved_ntp_secs = 0;
832 uint32_t recieved_ntp_frac = 0;
833 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
834 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000835 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100836 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000837 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100838 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
839 int64_t time_since_recieved =
840 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
841 // Don't use old SRs to estimate time.
842 if (time_since_recieved <= 1) {
843 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
844 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000845
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100846 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000847}
848
philipeld4fac692017-09-04 07:03:46 -0700849void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800850 if (!nack_module_)
851 return;
852
philipela45102f2017-02-22 05:30:39 -0800853 int seq_num = -1;
854 {
855 rtc::CritScope lock(&last_seq_num_cs_);
856 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
857 if (seq_num_it != last_seq_num_for_pic_id_.end())
858 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800859 }
philipela45102f2017-02-22 05:30:39 -0800860 if (seq_num != -1)
861 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800862}
863
philipeld4fac692017-09-04 07:03:46 -0700864void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800865 int seq_num = -1;
866 {
867 rtc::CritScope lock(&last_seq_num_cs_);
868 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
869 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
870 seq_num = seq_num_it->second;
871 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
872 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800873 }
philipela45102f2017-02-22 05:30:39 -0800874 }
875 if (seq_num != -1) {
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200876 packet_buffer_.ClearTo(seq_num);
philipel7acc4a42019-09-26 11:25:52 +0200877 rtc::CritScope lock(&reference_finder_lock_);
philipela45102f2017-02-22 05:30:39 -0800878 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800879 }
880}
881
nisseb1f2ff92017-06-09 04:01:55 -0700882void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700883 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
884 : RtcpMode::kOff);
885}
886
nisseb1f2ff92017-06-09 04:01:55 -0700887void RtpVideoStreamReceiver::StartReceive() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200888 RTC_DCHECK_RUN_ON(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000889 receiving_ = true;
890}
891
nisseb1f2ff92017-06-09 04:01:55 -0700892void RtpVideoStreamReceiver::StopReceive() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200893 RTC_DCHECK_RUN_ON(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000894 receiving_ = false;
895}
896
nisseb1f2ff92017-06-09 04:01:55 -0700897void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700898 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800899 if (counter.first_packet_time_ms == -1)
900 return;
901
902 int64_t elapsed_sec =
903 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
904 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
905 return;
906
mflodmandc7d0d22016-05-06 05:32:22 -0700907 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700908 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700909 "WebRTC.Video.ReceivedFecPacketsInPercent",
910 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
911 }
912 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700913 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
914 static_cast<int>(counter.num_recovered_packets *
915 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700916 }
Niels Möllercaef51e2019-08-27 09:19:49 +0200917 if (config_.rtp.ulpfec_payload_type != -1) {
918 RTC_HISTOGRAM_COUNTS_10000(
919 "WebRTC.Video.FecBitrateReceivedInKbps",
920 static_cast<int>(counter.num_bytes * 8 / elapsed_sec / 1000));
921 }
mflodmandc7d0d22016-05-06 05:32:22 -0700922}
923
nisseb1f2ff92017-06-09 04:01:55 -0700924void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800925 auto codec_params_it = pt_codec_params_.find(payload_type);
926 if (codec_params_it == pt_codec_params_.end())
927 return;
928
Mirko Bonadei675513b2017-11-09 11:09:25 +0100929 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
930 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800931
932 H264SpropParameterSets sprop_decoder;
933 auto sprop_base64_it =
934 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
935
936 if (sprop_base64_it == codec_params_it->second.end())
937 return;
938
johan62d02c32017-01-24 04:38:27 -0800939 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800940 return;
941
johand2b092f2017-01-24 02:38:17 -0800942 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
943 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800944}
945
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000946} // namespace webrtc