blob: 6f478f8c22c5af390579e9ae9c8d4b408e2ebe5f [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,
Erik Språnge3a10e12019-08-19 15:45:00 +020059 RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
60 uint32_t local_ssrc) {
mflodmanc0e58a32016-04-25 01:26:26 -070061 RtpRtcp::Configuration configuration;
Sebastian Jansson8026d602019-03-04 19:39:01 +010062 configuration.clock = clock;
mflodmanc0e58a32016-04-25 01:26:26 -070063 configuration.audio = false;
64 configuration.receiver_only = true;
65 configuration.receive_statistics = receive_statistics;
66 configuration.outgoing_transport = outgoing_transport;
mflodmanc0e58a32016-04-25 01:26:26 -070067 configuration.rtt_stats = rtt_stats;
68 configuration.rtcp_packet_type_counter_observer =
69 rtcp_packet_type_counter_observer;
Erik Språng54d5d2c2019-08-20 17:22:36 +020070 configuration.local_media_ssrc = local_ssrc;
mflodmanc0e58a32016-04-25 01:26:26 -070071
Danil Chapovalovc44f6cc2019-03-06 11:31:09 +010072 std::unique_ptr<RtpRtcp> rtp_rtcp = RtpRtcp::Create(configuration);
mflodmanc0e58a32016-04-25 01:26:26 -070073 rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound);
74
75 return rtp_rtcp;
76}
77
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +000078static const int kPacketLogIntervalMs = 10000;
79
Elad Alonef09c5b2019-05-31 13:25:50 +020080RtpVideoStreamReceiver::RtcpFeedbackBuffer::RtcpFeedbackBuffer(
81 KeyFrameRequestSender* key_frame_request_sender,
82 NackSender* nack_sender,
83 LossNotificationSender* loss_notification_sender)
84 : key_frame_request_sender_(key_frame_request_sender),
85 nack_sender_(nack_sender),
86 loss_notification_sender_(loss_notification_sender),
87 request_key_frame_(false) {
88 RTC_DCHECK(key_frame_request_sender_);
89 RTC_DCHECK(nack_sender_);
90 RTC_DCHECK(loss_notification_sender_);
91}
92
93void RtpVideoStreamReceiver::RtcpFeedbackBuffer::RequestKeyFrame() {
94 rtc::CritScope lock(&cs_);
95 request_key_frame_ = true;
96}
97
98void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendNack(
Elad Alonef09c5b2019-05-31 13:25:50 +020099 const std::vector<uint16_t>& sequence_numbers,
100 bool buffering_allowed) {
101 RTC_DCHECK(!sequence_numbers.empty());
102 rtc::CritScope lock(&cs_);
103 nack_sequence_numbers_.insert(nack_sequence_numbers_.end(),
104 sequence_numbers.cbegin(),
105 sequence_numbers.cend());
106 if (!buffering_allowed) {
107 // Note that while *buffering* is not allowed, *batching* is, meaning that
108 // previously buffered messages may be sent along with the current message.
109 SendBufferedRtcpFeedback();
110 }
111}
112
113void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendLossNotification(
114 uint16_t last_decoded_seq_num,
115 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200116 bool decodability_flag,
117 bool buffering_allowed) {
118 RTC_DCHECK(buffering_allowed);
Elad Alonef09c5b2019-05-31 13:25:50 +0200119 rtc::CritScope lock(&cs_);
Elad Alon36690cd2019-06-04 22:59:54 +0200120 RTC_DCHECK(!lntf_state_)
Elad Alonef09c5b2019-05-31 13:25:50 +0200121 << "SendLossNotification() called twice in a row with no call to "
122 "SendBufferedRtcpFeedback() in between.";
123 lntf_state_ = absl::make_optional<LossNotificationState>(
124 last_decoded_seq_num, last_received_seq_num, decodability_flag);
125}
126
Elad Alonef09c5b2019-05-31 13:25:50 +0200127void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendBufferedRtcpFeedback() {
128 bool request_key_frame = false;
129 std::vector<uint16_t> nack_sequence_numbers;
130 absl::optional<LossNotificationState> lntf_state;
131
132 {
133 rtc::CritScope lock(&cs_);
134 std::swap(request_key_frame, request_key_frame_);
135 std::swap(nack_sequence_numbers, nack_sequence_numbers_);
136 std::swap(lntf_state, lntf_state_);
137 }
138
Elad Alone86af2c2019-06-03 14:37:50 +0200139 if (lntf_state) {
140 // If either a NACK or a key frame request is sent, we should buffer
141 // the LNTF and wait for them (NACK or key frame request) to trigger
142 // the compound feedback message.
143 // Otherwise, the LNTF should be sent out immediately.
144 const bool buffering_allowed =
145 request_key_frame || !nack_sequence_numbers.empty();
146
147 loss_notification_sender_->SendLossNotification(
148 lntf_state->last_decoded_seq_num, lntf_state->last_received_seq_num,
149 lntf_state->decodability_flag, buffering_allowed);
150 }
151
Elad Alonef09c5b2019-05-31 13:25:50 +0200152 if (request_key_frame) {
153 key_frame_request_sender_->RequestKeyFrame();
154 } else if (!nack_sequence_numbers.empty()) {
155 nack_sender_->SendNack(nack_sequence_numbers, true);
156 }
Elad Alonef09c5b2019-05-31 13:25:50 +0200157}
158
nisseb1f2ff92017-06-09 04:01:55 -0700159RtpVideoStreamReceiver::RtpVideoStreamReceiver(
Sebastian Jansson8026d602019-03-04 19:39:01 +0100160 Clock* clock,
mflodmanfa666592016-04-28 23:15:33 -0700161 Transport* transport,
162 RtcpRttStats* rtt_stats,
mflodmancfc8e3b2016-05-03 21:22:04 -0700163 PacketRouter* packet_router,
Tommi733b5472016-06-10 17:58:01 +0200164 const VideoReceiveStream::Config* config,
nisseca5706d2017-09-11 02:32:16 -0700165 ReceiveStatistics* rtp_receive_statistics,
mflodmandc7d0d22016-05-06 05:32:22 -0700166 ReceiveStatisticsProxy* receive_stats_proxy,
Erik Språng737336d2016-07-29 12:59:36 +0200167 ProcessThread* process_thread,
philipelfd5a20f2016-11-15 00:57:57 -0800168 NackSender* nack_sender,
Niels Möller2f5554d2019-05-29 13:35:14 +0200169 KeyFrameRequestSender* keyframe_request_sender,
Benjamin Wright192eeec2018-10-17 17:27:25 -0700170 video_coding::OnCompleteFrameCallback* complete_frame_callback,
171 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)
Sebastian Jansson8026d602019-03-04 19:39:01 +0100172 : clock_(clock),
Tommi733b5472016-06-10 17:58:01 +0200173 config_(*config),
mflodmanc0e58a32016-04-25 01:26:26 -0700174 packet_router_(packet_router),
mflodmandc7d0d22016-05-06 05:32:22 -0700175 process_thread_(process_thread),
Sebastian Jansson8026d602019-03-04 19:39:01 +0100176 ntp_estimator_(clock),
Niels Möllerb0573bc2017-09-25 10:47:00 +0200177 rtp_header_extensions_(config_.rtp.extensions),
nisseca5706d2017-09-11 02:32:16 -0700178 rtp_receive_statistics_(rtp_receive_statistics),
Ilya Nikolaevskiy2d821c32019-06-26 14:39:36 +0200179 ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc,
180 this,
181 config->rtp.extensions)),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000182 receiving_(false),
mflodmanc0e58a32016-04-25 01:26:26 -0700183 last_packet_log_ms_(-1),
Niels Möller2f5554d2019-05-29 13:35:14 +0200184 rtp_rtcp_(CreateRtpRtcpModule(clock,
185 rtp_receive_statistics_,
186 transport,
187 rtt_stats,
Erik Språnge3a10e12019-08-19 15:45:00 +0200188 receive_stats_proxy,
189 config_.rtp.local_ssrc)),
philipelfd5a20f2016-11-15 00:57:57 -0800190 complete_frame_callback_(complete_frame_callback),
Niels Möller2f5554d2019-05-29 13:35:14 +0200191 keyframe_request_sender_(keyframe_request_sender),
Elad Alonef09c5b2019-05-31 13:25:50 +0200192 // TODO(bugs.webrtc.org/10336): Let |rtcp_feedback_buffer_| communicate
193 // directly with |rtp_rtcp_|.
194 rtcp_feedback_buffer_(this, nack_sender, this),
Benjamin Wright52426ed2019-03-01 11:01:59 -0800195 has_received_frame_(false),
196 frames_decryptable_(false) {
eladalon822ff2b2017-08-01 06:30:28 -0700197 constexpr bool remb_candidate = true;
Niels Möller60f4e292019-05-20 11:06:33 +0200198 if (packet_router_)
199 packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
mflodmancfc8e3b2016-05-03 21:22:04 -0700200
Tommi733b5472016-06-10 17:58:01 +0200201 RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff)
mflodmancfc8e3b2016-05-03 21:22:04 -0700202 << "A stream should not be configured with RTCP disabled. This value is "
203 "reserved for internal usage.";
mflodmandc7d0d22016-05-06 05:32:22 -0700204 RTC_DCHECK(config_.rtp.remote_ssrc != 0);
205 // TODO(pbos): What's an appropriate local_ssrc for receive-only streams?
206 RTC_DCHECK(config_.rtp.local_ssrc != 0);
207 RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc);
208
Tommi733b5472016-06-10 17:58:01 +0200209 rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode);
stefanb4ab3812017-06-09 06:12:11 -0700210 rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc);
mflodmandc7d0d22016-05-06 05:32:22 -0700211
mflodmancfc8e3b2016-05-03 21:22:04 -0700212 static const int kMaxPacketAgeToNack = 450;
Tommi733b5472016-06-10 17:58:01 +0200213 const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0)
214 ? kMaxPacketAgeToNack
215 : kDefaultMaxReorderingThreshold;
Niels Möller87da1092019-05-24 14:04:28 +0200216 rtp_receive_statistics_->SetMaxReorderingThreshold(config_.rtp.remote_ssrc,
217 max_reordering_threshold);
218 // TODO(nisse): For historic reasons, we applied the above
219 // max_reordering_threshold also for RTX stats, which makes little sense since
220 // we don't NACK rtx packets. Consider deleting the below block, and rely on
221 // the default threshold.
222 if (config_.rtp.rtx_ssrc) {
223 rtp_receive_statistics_->SetMaxReorderingThreshold(
224 config_.rtp.rtx_ssrc, max_reordering_threshold);
225 }
Tommi733b5472016-06-10 17:58:01 +0200226 if (config_.rtp.rtcp_xr.receiver_reference_time_report)
mflodmandc7d0d22016-05-06 05:32:22 -0700227 rtp_rtcp_->SetRtcpXrRrtrStatus(true);
228
229 // Stats callback for CNAME changes.
Niels Möller4d7c4052019-08-05 12:45:19 +0200230 rtp_rtcp_->RegisterRtcpCnameCallback(receive_stats_proxy);
mflodmandc7d0d22016-05-06 05:32:22 -0700231
tommidea489f2017-03-03 03:20:24 -0800232 process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE);
philipelfd5a20f2016-11-15 00:57:57 -0800233
Elad Alonfadb1812019-05-24 13:40:02 +0200234 if (config_.rtp.lntf.enabled) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100235 loss_notification_controller_ =
Elad Alonef09c5b2019-05-31 13:25:50 +0200236 absl::make_unique<LossNotificationController>(&rtcp_feedback_buffer_,
237 &rtcp_feedback_buffer_);
Elad Alonca2c4302019-05-27 22:43:10 +0200238 }
239
240 if (config_.rtp.nack.rtp_history_ms != 0) {
Elad Alonef09c5b2019-05-31 13:25:50 +0200241 nack_module_ = absl::make_unique<NackModule>(clock_, &rtcp_feedback_buffer_,
242 &rtcp_feedback_buffer_);
tommidea489f2017-03-03 03:20:24 -0800243 process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE);
tommif284b7f2017-02-27 01:59:36 -0800244 }
philipelfd5a20f2016-11-15 00:57:57 -0800245
Johannes Kron487e6942018-10-24 00:53:39 +0200246 // The group here must be a positive power of 2, in which case that is used as
247 // size. All other values shall result in the default value being used.
Johannes Kron201596f2018-10-22 14:33:39 +0200248 const std::string group_name =
249 webrtc::field_trial::FindFullName("WebRTC-PacketBufferMaxSize");
250 int packet_buffer_max_size = kPacketBufferMaxSize;
251 if (!group_name.empty() &&
252 (sscanf(group_name.c_str(), "%d", &packet_buffer_max_size) != 1 ||
Johannes Kron487e6942018-10-24 00:53:39 +0200253 packet_buffer_max_size <= 0 ||
254 // Verify that the number is a positive power of 2.
255 (packet_buffer_max_size & (packet_buffer_max_size - 1)) != 0)) {
Johannes Kron201596f2018-10-22 14:33:39 +0200256 RTC_LOG(LS_WARNING) << "Invalid packet buffer max size: " << group_name;
257 packet_buffer_max_size = kPacketBufferMaxSize;
258 }
259
philipela45102f2017-02-22 05:30:39 -0800260 packet_buffer_ = video_coding::PacketBuffer::Create(
Johannes Kron201596f2018-10-22 14:33:39 +0200261 clock_, kPacketBufferStartSize, packet_buffer_max_size, this);
Elad Alona8f54612018-11-06 11:21:25 +0100262 reference_finder_ =
263 absl::make_unique<video_coding::RtpFrameReferenceFinder>(this);
Benjamin Wrighta5564482019-04-03 10:44:18 -0700264
Benjamin Wright00765292018-11-30 16:18:26 -0800265 // Only construct the encrypted receiver if frame encryption is enabled.
Benjamin Wrighta5564482019-04-03 10:44:18 -0700266 if (config_.crypto_options.sframe.require_frame_encryption) {
Benjamin Wright00765292018-11-30 16:18:26 -0800267 buffered_frame_decryptor_ =
Benjamin Wrighta5564482019-04-03 10:44:18 -0700268 absl::make_unique<BufferedFrameDecryptor>(this, this);
269 if (frame_decryptor != nullptr) {
270 buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor));
271 }
Benjamin Wright00765292018-11-30 16:18:26 -0800272 }
mflodmanc0e58a32016-04-25 01:26:26 -0700273}
niklase@google.com470e71d2011-07-07 08:21:25 +0000274
nisseb1f2ff92017-06-09 04:01:55 -0700275RtpVideoStreamReceiver::~RtpVideoStreamReceiver() {
eladalonc0d481a2017-08-02 07:39:07 -0700276 RTC_DCHECK(secondary_sinks_.empty());
277
tommif284b7f2017-02-27 01:59:36 -0800278 if (nack_module_) {
279 process_thread_->DeRegisterModule(nack_module_.get());
280 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
tommif284b7f2017-02-27 01:59:36 -0800282 process_thread_->DeRegisterModule(rtp_rtcp_.get());
philipelfd5a20f2016-11-15 00:57:57 -0800283
Niels Möller60f4e292019-05-20 11:06:33 +0200284 if (packet_router_)
285 packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get());
mflodmandc7d0d22016-05-06 05:32:22 -0700286 UpdateHistograms();
asapersson@webrtc.org0800db72015-01-15 07:40:20 +0000287}
288
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200289void RtpVideoStreamReceiver::AddReceiveCodec(
philipel022b54e2016-12-20 04:15:59 -0800290 const VideoCodec& video_codec,
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200291 const std::map<std::string, std::string>& codec_params,
292 bool raw_payload) {
293 absl::optional<VideoCodecType> video_type;
294 if (!raw_payload) {
295 video_type = video_codec.codecType;
296 }
297 payload_type_map_.emplace(video_codec.plType, video_type);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200298 pt_codec_params_.emplace(video_codec.plType, codec_params);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000299}
300
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200301absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200302 Syncable::Info info;
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200303 if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
304 &info.capture_time_ntp_frac, nullptr, nullptr,
305 &info.capture_time_source_clock) != 0) {
306 return absl::nullopt;
307 }
Niels Möllerb0d4b412018-08-28 13:58:15 +0200308 {
Chen Xing90f3b892019-06-25 10:16:14 +0200309 rtc::CritScope lock(&sync_info_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200310 if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) {
311 return absl::nullopt;
312 }
313 info.latest_received_capture_timestamp = *last_received_rtp_timestamp_;
314 info.latest_receive_time_ms = *last_received_rtp_system_time_ms_;
315 }
Niels Möllerdf9e9ae2018-07-31 08:29:53 +0200316
317 // Leaves info.current_delay_ms uninitialized.
318 return info;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000319}
320
nisseb1f2ff92017-06-09 04:01:55 -0700321int32_t RtpVideoStreamReceiver::OnReceivedPayloadData(
mflodmanfa666592016-04-28 23:15:33 -0700322 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +0200323 size_t payload_size,
Niels Möller125b5d62019-03-11 16:11:07 +0100324 const RTPHeader& rtp_header,
325 const RTPVideoHeader& video_header,
Ying Wangb32bb952018-10-31 10:12:27 +0100326 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
327 bool is_recovered) {
Niels Möller125b5d62019-03-11 16:11:07 +0100328 VCMPacket packet(payload_data, payload_size, rtp_header, video_header,
Chen Xingf00bf422019-06-20 10:05:55 +0200329 ntp_estimator_.Estimate(rtp_header.timestamp),
330 clock_->TimeInMilliseconds());
Elad Alon7d6a4c02019-02-25 13:00:51 +0100331 packet.generic_descriptor = generic_descriptor;
332
Elad Alonca2c4302019-05-27 22:43:10 +0200333 if (loss_notification_controller_) {
334 if (is_recovered) {
335 // TODO(bugs.webrtc.org/10336): Implement support for reordering.
336 RTC_LOG(LS_WARNING)
337 << "LossNotificationController does not support reordering.";
338 } else {
Elad Alonca2c4302019-05-27 22:43:10 +0200339 loss_notification_controller_->OnReceivedPacket(packet);
340 }
341 }
342
Niels Möller8dad9b42018-08-22 10:36:35 +0200343 if (nack_module_) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200344 const bool is_keyframe =
345 video_header.is_first_packet_in_frame &&
346 video_header.frame_type == VideoFrameType::kVideoFrameKey;
Niels Möller8dad9b42018-08-22 10:36:35 +0200347
348 packet.timesNacked = nack_module_->OnReceivedPacket(
Niels Möller125b5d62019-03-11 16:11:07 +0100349 rtp_header.sequenceNumber, is_keyframe, is_recovered);
Niels Möller8dad9b42018-08-22 10:36:35 +0200350 } else {
351 packet.timesNacked = -1;
352 }
philipelfd5a20f2016-11-15 00:57:57 -0800353
philipel54ca9192017-03-21 05:45:18 -0700354 if (packet.sizeBytes == 0) {
Niels Möllerbc010472018-03-23 13:22:29 +0100355 NotifyReceiverOfEmptyPacket(packet.seqNum);
Elad Alonef09c5b2019-05-31 13:25:50 +0200356 rtcp_feedback_buffer_.SendBufferedRtcpFeedback();
philipel54ca9192017-03-21 05:45:18 -0700357 return 0;
358 }
359
Niels Möllerd5e02f02019-02-20 13:12:21 +0100360 if (packet.codec() == kVideoCodecH264) {
philipela45102f2017-02-22 05:30:39 -0800361 // Only when we start to receive packets will we know what payload type
362 // that will be used. When we know the payload type insert the correct
363 // sps/pps into the tracker.
364 if (packet.payloadType != last_payload_type_) {
365 last_payload_type_ = packet.payloadType;
366 InsertSpsPpsIntoTracker(packet.payloadType);
philipelfd5a20f2016-11-15 00:57:57 -0800367 }
368
philipela45102f2017-02-22 05:30:39 -0800369 switch (tracker_.CopyAndFixBitstream(&packet)) {
370 case video_coding::H264SpsPpsTracker::kRequestKeyframe:
Elad Alonef09c5b2019-05-31 13:25:50 +0200371 rtcp_feedback_buffer_.RequestKeyFrame();
372 rtcp_feedback_buffer_.SendBufferedRtcpFeedback();
Karl Wiberg80ba3332018-02-05 10:33:35 +0100373 RTC_FALLTHROUGH();
philipela45102f2017-02-22 05:30:39 -0800374 case video_coding::H264SpsPpsTracker::kDrop:
375 return 0;
376 case video_coding::H264SpsPpsTracker::kInsert:
377 break;
378 }
379
philipelfd5a20f2016-11-15 00:57:57 -0800380 } else {
philipela45102f2017-02-22 05:30:39 -0800381 uint8_t* data = new uint8_t[packet.sizeBytes];
382 memcpy(data, packet.dataPtr, packet.sizeBytes);
383 packet.dataPtr = data;
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000384 }
philipela45102f2017-02-22 05:30:39 -0800385
Elad Alonef09c5b2019-05-31 13:25:50 +0200386 rtcp_feedback_buffer_.SendBufferedRtcpFeedback();
Johannes Kronbd3f3052019-08-01 15:45:54 +0200387 if (!packet_buffer_->InsertPacket(&packet)) {
388 RequestKeyFrame();
389 }
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000390 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
392
nisseb1f2ff92017-06-09 04:01:55 -0700393void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet,
394 size_t rtp_packet_length) {
Niels Möllerb0573bc2017-09-25 10:47:00 +0200395 RtpPacketReceived packet;
396 if (!packet.Parse(rtp_packet, rtp_packet_length))
nisse30e89312017-05-29 08:16:37 -0700397 return;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200398 if (packet.PayloadType() == config_.rtp.red_payload_type) {
399 RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation";
400 return;
401 }
402
Niels Möllerb0573bc2017-09-25 10:47:00 +0200403 packet.IdentifyExtensions(rtp_header_extensions_);
404 packet.set_payload_type_frequency(kVideoPayloadTypeFrequency);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200405 // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both
406 // original (decapsulated) media packets and recovered packets to
407 // this callback. We need a way to distinguish, for setting
408 // packet.recovered() correctly. Ideally, move RED decapsulation out
409 // of the Ulpfec implementation.
Niels Möllerb0573bc2017-09-25 10:47:00 +0200410
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200411 ReceivePacket(packet);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000412}
413
nissed2ef3142017-05-11 08:00:58 -0700414// This method handles both regular RTP packets and packets recovered
415// via FlexFEC.
nisseb1f2ff92017-06-09 04:01:55 -0700416void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200417 RTC_DCHECK_RUN_ON(&worker_task_checker_);
eladalonc0d481a2017-08-02 07:39:07 -0700418
eladalon8b073052017-08-25 00:49:08 -0700419 if (!receiving_) {
420 return;
421 }
solenberg@webrtc.orgfc320462014-02-11 15:27:49 +0000422
eladalon8b073052017-08-25 00:49:08 -0700423 if (!packet.recovered()) {
Jonas Oreland49ac5952018-09-26 16:04:32 +0200424 // TODO(nisse): Exclude out-of-order packets?
eladalon8b073052017-08-25 00:49:08 -0700425 int64_t now_ms = clock_->TimeInMilliseconds();
Niels Möllerb0d4b412018-08-28 13:58:15 +0200426 {
Chen Xing90f3b892019-06-25 10:16:14 +0200427 rtc::CritScope cs(&sync_info_lock_);
Niels Möllerb0d4b412018-08-28 13:58:15 +0200428 last_received_rtp_timestamp_ = packet.Timestamp();
429 last_received_rtp_system_time_ms_ = now_ms;
430 }
eladalon8b073052017-08-25 00:49:08 -0700431 // Periodically log the RTP header of incoming packets.
432 if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200433 rtc::StringBuilder ss;
eladalon8b073052017-08-25 00:49:08 -0700434 ss << "Packet received on SSRC: " << packet.Ssrc()
435 << " with payload type: " << static_cast<int>(packet.PayloadType())
436 << ", timestamp: " << packet.Timestamp()
437 << ", sequence number: " << packet.SequenceNumber()
438 << ", arrival time: " << packet.arrival_time_ms();
439 int32_t time_offset;
440 if (packet.GetExtension<TransmissionOffset>(&time_offset)) {
441 ss << ", toffset: " << time_offset;
nisse38cc1d62017-02-13 05:59:46 -0800442 }
eladalon8b073052017-08-25 00:49:08 -0700443 uint32_t send_time;
444 if (packet.GetExtension<AbsoluteSendTime>(&send_time)) {
445 ss << ", abs send time: " << send_time;
446 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100447 RTC_LOG(LS_INFO) << ss.str();
eladalon8b073052017-08-25 00:49:08 -0700448 last_packet_log_ms_ = now_ms;
stefan@webrtc.orgeb24b042014-10-14 11:40:13 +0000449 }
450 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000451
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200452 ReceivePacket(packet);
nisse38cc1d62017-02-13 05:59:46 -0800453
asapersson@webrtc.org1457b472014-05-26 13:06:04 +0000454 // Update receive statistics after ReceivePacket.
455 // Receive statistics will be reset if the payload type changes (make sure
456 // that the first packet is included in the stats).
nissed2ef3142017-05-11 08:00:58 -0700457 if (!packet.recovered()) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200458 rtp_receive_statistics_->OnRtpPacket(packet);
nissed2ef3142017-05-11 08:00:58 -0700459 }
eladalonc0d481a2017-08-02 07:39:07 -0700460
461 for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) {
462 secondary_sink->OnRtpPacket(packet);
463 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000464}
465
Niels Möller41684372019-03-25 15:51:03 +0100466void RtpVideoStreamReceiver::RequestKeyFrame() {
Elad Alonef09c5b2019-05-31 13:25:50 +0200467 // TODO(bugs.webrtc.org/10336): Allow the sender to ignore key frame requests
468 // issued by anything other than the LossNotificationController if it (the
469 // sender) is relying on LNTF alone.
Niels Möller2f5554d2019-05-29 13:35:14 +0200470 if (keyframe_request_sender_) {
471 keyframe_request_sender_->RequestKeyFrame();
472 } else {
Niels Möllerdd0094a2019-06-04 14:46:27 +0200473 rtp_rtcp_->SendPictureLossIndication();
Niels Möller2f5554d2019-05-29 13:35:14 +0200474 }
mflodmancfc8e3b2016-05-03 21:22:04 -0700475}
476
Elad Alon7d6a4c02019-02-25 13:00:51 +0100477void RtpVideoStreamReceiver::SendLossNotification(
478 uint16_t last_decoded_seq_num,
479 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200480 bool decodability_flag,
481 bool buffering_allowed) {
Elad Alonfadb1812019-05-24 13:40:02 +0200482 RTC_DCHECK(config_.rtp.lntf.enabled);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100483 rtp_rtcp_->SendLossNotification(last_decoded_seq_num, last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200484 decodability_flag, buffering_allowed);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100485}
486
nisseb1f2ff92017-06-09 04:01:55 -0700487bool RtpVideoStreamReceiver::IsUlpfecEnabled() const {
nisse3b3622f2017-09-26 02:49:21 -0700488 return config_.rtp.ulpfec_payload_type != -1;
brandtre6f98c72016-11-11 03:28:30 -0800489}
490
nisseb1f2ff92017-06-09 04:01:55 -0700491bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const {
mflodmandc7d0d22016-05-06 05:32:22 -0700492 return config_.rtp.nack.rtp_history_ms > 0;
493}
494
nisseb1f2ff92017-06-09 04:01:55 -0700495void RtpVideoStreamReceiver::RequestPacketRetransmit(
mflodmandc7d0d22016-05-06 05:32:22 -0700496 const std::vector<uint16_t>& sequence_numbers) {
497 rtp_rtcp_->SendNack(sequence_numbers);
498}
499
Benjamin Wright52426ed2019-03-01 11:01:59 -0800500bool RtpVideoStreamReceiver::IsDecryptable() const {
501 return frames_decryptable_.load();
502}
503
Elad Alonb4643ad2019-02-22 11:19:50 +0100504void RtpVideoStreamReceiver::OnAssembledFrame(
philipelfd5a20f2016-11-15 00:57:57 -0800505 std::unique_ptr<video_coding::RtpFrameObject> frame) {
Benjamin Wright00765292018-11-30 16:18:26 -0800506 RTC_DCHECK_RUN_ON(&network_tc_);
Elad Alon7d6a4c02019-02-25 13:00:51 +0100507 RTC_DCHECK(frame);
508
509 absl::optional<RtpGenericFrameDescriptor> descriptor =
510 frame->GetGenericFrameDescriptor();
511
512 if (loss_notification_controller_ && descriptor) {
513 loss_notification_controller_->OnAssembledFrame(
514 frame->first_seq_num(), descriptor->FrameId(),
515 descriptor->Discardable().value_or(false),
516 descriptor->FrameDependenciesDiffs());
Elad Alonca2c4302019-05-27 22:43:10 +0200517 }
518
Elad Alonef09c5b2019-05-31 13:25:50 +0200519 // If frames arrive before a key frame, they would not be decodable.
520 // In that case, request a key frame ASAP.
Elad Alonca2c4302019-05-27 22:43:10 +0200521 if (!has_received_frame_) {
Niels Möller8f7ce222019-03-21 15:43:58 +0100522 if (frame->FrameType() != VideoFrameType::kVideoFrameKey) {
Elad Alonef09c5b2019-05-31 13:25:50 +0200523 // |loss_notification_controller_|, if present, would have already
524 // requested a key frame when the first packet for the non-key frame
525 // had arrived, so no need to replicate the request.
526 if (!loss_notification_controller_) {
527 RequestKeyFrame();
528 }
Benjamin Wright39feabe2018-10-22 13:33:09 -0700529 }
Elad Alonef09c5b2019-05-31 13:25:50 +0200530 has_received_frame_ = true;
Benjamin Wright39feabe2018-10-22 13:33:09 -0700531 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100532
Benjamin Wright00765292018-11-30 16:18:26 -0800533 if (buffered_frame_decryptor_ == nullptr) {
534 reference_finder_->ManageFrame(std::move(frame));
535 } else {
536 buffered_frame_decryptor_->ManageEncryptedFrame(std::move(frame));
Benjamin Wright192eeec2018-10-17 17:27:25 -0700537 }
philipelfd5a20f2016-11-15 00:57:57 -0800538}
539
nisseb1f2ff92017-06-09 04:01:55 -0700540void RtpVideoStreamReceiver::OnCompleteFrame(
philipele7c891f2018-02-22 14:35:06 +0100541 std::unique_ptr<video_coding::EncodedFrame> frame) {
philipelfd5a20f2016-11-15 00:57:57 -0800542 {
543 rtc::CritScope lock(&last_seq_num_cs_);
544 video_coding::RtpFrameObject* rtp_frame =
545 static_cast<video_coding::RtpFrameObject*>(frame.get());
philipel0fa82a62018-03-19 15:34:53 +0100546 last_seq_num_for_pic_id_[rtp_frame->id.picture_id] =
547 rtp_frame->last_seq_num();
philipelfd5a20f2016-11-15 00:57:57 -0800548 }
549 complete_frame_callback_->OnCompleteFrame(std::move(frame));
550}
551
Benjamin Wright00765292018-11-30 16:18:26 -0800552void RtpVideoStreamReceiver::OnDecryptedFrame(
553 std::unique_ptr<video_coding::RtpFrameObject> frame) {
554 reference_finder_->ManageFrame(std::move(frame));
555}
556
Benjamin Wright2af5dcb2019-04-09 20:08:41 +0000557void RtpVideoStreamReceiver::OnDecryptionStatusChange(
558 FrameDecryptorInterface::Status status) {
559 frames_decryptable_.store(
560 (status == FrameDecryptorInterface::Status::kOk) ||
561 (status == FrameDecryptorInterface::Status::kRecoverable));
Benjamin Wright52426ed2019-03-01 11:01:59 -0800562}
563
Benjamin Wrighta5564482019-04-03 10:44:18 -0700564void RtpVideoStreamReceiver::SetFrameDecryptor(
565 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
566 RTC_DCHECK_RUN_ON(&network_tc_);
567 if (buffered_frame_decryptor_ == nullptr) {
568 buffered_frame_decryptor_ =
569 absl::make_unique<BufferedFrameDecryptor>(this, this);
570 }
571 buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor));
572}
573
Tommi81de14f2018-03-25 22:19:25 +0200574void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) {
tommif284b7f2017-02-27 01:59:36 -0800575 if (nack_module_)
576 nack_module_->UpdateRtt(max_rtt_ms);
philipelfd5a20f2016-11-15 00:57:57 -0800577}
578
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200579absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const {
philipel3184f8e2017-05-18 08:08:53 -0700580 return packet_buffer_->LastReceivedPacketMs();
581}
582
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200583absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs()
nisseb1f2ff92017-06-09 04:01:55 -0700584 const {
philipel3184f8e2017-05-18 08:08:53 -0700585 return packet_buffer_->LastReceivedKeyframePacketMs();
586}
587
eladalonc0d481a2017-08-02 07:39:07 -0700588void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200589 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Steve Antonbd631a02019-03-28 10:51:27 -0700590 RTC_DCHECK(!absl::c_linear_search(secondary_sinks_, sink));
eladalonc0d481a2017-08-02 07:39:07 -0700591 secondary_sinks_.push_back(sink);
592}
593
594void RtpVideoStreamReceiver::RemoveSecondarySink(
595 const RtpPacketSinkInterface* sink) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200596 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Steve Antonbd631a02019-03-28 10:51:27 -0700597 auto it = absl::c_find(secondary_sinks_, sink);
eladalonc0d481a2017-08-02 07:39:07 -0700598 if (it == secondary_sinks_.end()) {
599 // We might be rolling-back a call whose setup failed mid-way. In such a
600 // case, it's simpler to remove "everything" rather than remember what
601 // has already been added.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100602 RTC_LOG(LS_WARNING) << "Removal of unknown sink.";
eladalonc0d481a2017-08-02 07:39:07 -0700603 return;
604 }
605 secondary_sinks_.erase(it);
606}
607
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200608void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) {
609 if (packet.payload_size() == 0) {
Niels Möller0b926782018-08-21 17:49:24 +0200610 // Padding or keep-alive packet.
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200611 // TODO(nisse): Could drop empty packets earlier, but need to figure out how
612 // they should be counted in stats.
Niels Möller0b926782018-08-21 17:49:24 +0200613 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
nisse30e89312017-05-29 08:16:37 -0700614 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000615 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200616 if (packet.PayloadType() == config_.rtp.red_payload_type) {
Niels Möller1f3206c2018-09-14 08:26:32 +0200617 ParseAndHandleEncapsulatingHeader(packet);
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200618 return;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000619 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200620
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200621 const auto type_it = payload_type_map_.find(packet.PayloadType());
622 if (type_it == payload_type_map_.end()) {
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200623 return;
624 }
625 auto depacketizer =
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200626 absl::WrapUnique(RtpDepacketizer::Create(type_it->second));
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200627
628 if (!depacketizer) {
629 RTC_LOG(LS_ERROR) << "Failed to create depacketizer.";
630 return;
631 }
632 RtpDepacketizer::ParsedPayload parsed_payload;
633 if (!depacketizer->Parse(&parsed_payload, packet.payload().data(),
634 packet.payload().size())) {
635 RTC_LOG(LS_WARNING) << "Failed parsing payload.";
636 return;
637 }
638
Niels Möller125b5d62019-03-11 16:11:07 +0100639 RTPHeader rtp_header;
640 packet.GetHeader(&rtp_header);
641 RTPVideoHeader video_header = parsed_payload.video_header();
642 video_header.rotation = kVideoRotation_0;
643 video_header.content_type = VideoContentType::UNSPECIFIED;
644 video_header.video_timing.flags = VideoSendTiming::kInvalid;
645 video_header.is_last_packet_in_frame = rtp_header.markerBit;
646 video_header.frame_marking.temporal_id = kNoTemporalIdx;
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500647
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100648 if (parsed_payload.video_header().codec == kVideoCodecVP9) {
649 const RTPVideoHeaderVP9& codec_header = absl::get<RTPVideoHeaderVP9>(
650 parsed_payload.video_header().video_type_header);
Niels Möller125b5d62019-03-11 16:11:07 +0100651 video_header.is_last_packet_in_frame |= codec_header.end_of_frame;
652 video_header.is_first_packet_in_frame |= codec_header.beginning_of_frame;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100653 }
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200654
Niels Möller125b5d62019-03-11 16:11:07 +0100655 packet.GetExtension<VideoOrientation>(&video_header.rotation);
656 packet.GetExtension<VideoContentTypeExtension>(&video_header.content_type);
657 packet.GetExtension<VideoTimingExtension>(&video_header.video_timing);
658 packet.GetExtension<PlayoutDelayLimits>(&video_header.playout_delay);
659 packet.GetExtension<FrameMarkingExtension>(&video_header.frame_marking);
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500660
Johannes Krond51ec582019-04-15 13:32:41 +0200661 // Color space should only be transmitted in the last packet of a frame,
662 // therefore, neglect it otherwise so that last_color_space_ is not reset by
663 // mistake.
664 if (video_header.is_last_packet_in_frame) {
665 video_header.color_space = packet.GetExtension<ColorSpaceExtension>();
666 if (video_header.color_space ||
Niels Möllerabbc50e2019-04-24 09:41:16 +0200667 video_header.frame_type == VideoFrameType::kVideoFrameKey) {
Johannes Krond51ec582019-04-15 13:32:41 +0200668 // Store color space since it's only transmitted when changed or for key
669 // frames. Color space will be cleared if a key frame is transmitted
670 // without color space information.
671 last_color_space_ = video_header.color_space;
672 } else if (last_color_space_) {
673 video_header.color_space = last_color_space_;
674 }
Johannes Krond0b69a82018-12-03 14:18:53 +0100675 }
Johnny Lee1a1c52b2019-02-08 14:25:40 -0500676
philipel2837edc2018-10-02 13:55:47 +0200677 absl::optional<RtpGenericFrameDescriptor> generic_descriptor_wire;
678 generic_descriptor_wire.emplace();
Elad Alonccb9b752019-02-19 13:01:31 +0100679 const bool generic_descriptor_v00 =
680 packet.GetExtension<RtpGenericFrameDescriptorExtension00>(
681 &generic_descriptor_wire.value());
682 const bool generic_descriptor_v01 =
683 packet.GetExtension<RtpGenericFrameDescriptorExtension01>(
684 &generic_descriptor_wire.value());
685 if (generic_descriptor_v00 && generic_descriptor_v01) {
686 RTC_LOG(LS_WARNING) << "RTP packet had two different GFD versions.";
687 return;
688 }
689
690 if (generic_descriptor_v00 || generic_descriptor_v01) {
691 if (generic_descriptor_v00) {
692 generic_descriptor_wire->SetByteRepresentation(
693 packet.GetRawExtension<RtpGenericFrameDescriptorExtension00>());
694 } else {
695 generic_descriptor_wire->SetByteRepresentation(
696 packet.GetRawExtension<RtpGenericFrameDescriptorExtension01>());
697 }
698
Niels Möller125b5d62019-03-11 16:11:07 +0100699 video_header.is_first_packet_in_frame =
philipel2837edc2018-10-02 13:55:47 +0200700 generic_descriptor_wire->FirstPacketInSubFrame();
Niels Möller125b5d62019-03-11 16:11:07 +0100701 video_header.is_last_packet_in_frame =
702 rtp_header.markerBit || generic_descriptor_wire->LastPacketInSubFrame();
philipelfab91292018-10-17 14:36:08 +0200703
704 if (generic_descriptor_wire->FirstPacketInSubFrame()) {
Niels Möllerabbc50e2019-04-24 09:41:16 +0200705 video_header.frame_type =
philipelfab91292018-10-17 14:36:08 +0200706 generic_descriptor_wire->FrameDependenciesDiffs().empty()
Niels Möller8f7ce222019-03-21 15:43:58 +0100707 ? VideoFrameType::kVideoFrameKey
708 : VideoFrameType::kVideoFrameDelta;
philipelfab91292018-10-17 14:36:08 +0200709 }
710
Niels Möller125b5d62019-03-11 16:11:07 +0100711 video_header.width = generic_descriptor_wire->Width();
712 video_header.height = generic_descriptor_wire->Height();
philipel2837edc2018-10-02 13:55:47 +0200713 } else {
714 generic_descriptor_wire.reset();
philipelb3e42a42018-09-13 10:57:14 +0200715 }
716
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200717 OnReceivedPayloadData(parsed_payload.payload, parsed_payload.payload_length,
Niels Möllerabbc50e2019-04-24 09:41:16 +0200718 rtp_header, video_header, generic_descriptor_wire,
719 packet.recovered());
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000720}
721
nisseb1f2ff92017-06-09 04:01:55 -0700722void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader(
Niels Möller1f3206c2018-09-14 08:26:32 +0200723 const RtpPacketReceived& packet) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200724 RTC_DCHECK_RUN_ON(&worker_task_checker_);
Niels Möller1f3206c2018-09-14 08:26:32 +0200725 if (packet.PayloadType() == config_.rtp.red_payload_type &&
726 packet.payload_size() > 0) {
727 if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) {
728 rtp_receive_statistics_->FecPacketReceived(packet);
Peter Boström0b250722016-04-22 18:23:15 +0200729 // Notify video_receiver about received FEC packets to avoid NACKing these
730 // packets.
Niels Möller1f3206c2018-09-14 08:26:32 +0200731 NotifyReceiverOfEmptyPacket(packet.SequenceNumber());
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000732 }
Niels Möller1f3206c2018-09-14 08:26:32 +0200733 RTPHeader header;
734 packet.GetHeader(&header);
Niels Möller3f027b32018-03-14 08:04:58 +0100735 if (ulpfec_receiver_->AddReceivedRedPacket(
Niels Möller1f3206c2018-09-14 08:26:32 +0200736 header, packet.data(), packet.size(),
737 config_.rtp.ulpfec_payload_type) != 0) {
nisse30e89312017-05-29 08:16:37 -0700738 return;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000739 }
nisse30e89312017-05-29 08:16:37 -0700740 ulpfec_receiver_->ProcessReceivedFec();
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000741 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
Niels Möllerbc010472018-03-23 13:22:29 +0100744// In the case of a video stream without picture ids and no rtx the
745// RtpFrameReferenceFinder will need to know about padding to
746// correctly calculate frame references.
747void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
748 reference_finder_->PaddingReceived(seq_num);
749 packet_buffer_->PaddingReceived(seq_num);
Niels Möllerbc010472018-03-23 13:22:29 +0100750 if (nack_module_) {
Ying Wangb32bb952018-10-31 10:12:27 +0100751 nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false,
752 /* is _recovered = */ false);
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000753 }
Elad Alon7d6a4c02019-02-25 13:00:51 +0100754 if (loss_notification_controller_) {
Elad Alonca2c4302019-05-27 22:43:10 +0200755 // TODO(bugs.webrtc.org/10336): Handle empty packets.
Elad Alon7d6a4c02019-02-25 13:00:51 +0100756 RTC_LOG(LS_WARNING)
757 << "LossNotificationController does not expect empty packets.";
758 }
asapersson@webrtc.org37c05592015-01-28 13:58:27 +0000759}
760
nisseb1f2ff92017-06-09 04:01:55 -0700761bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet,
762 size_t rtcp_packet_length) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200763 RTC_DCHECK_RUN_ON(&worker_task_checker_);
eladalon8b073052017-08-25 00:49:08 -0700764
765 if (!receiving_) {
766 return false;
Peter Boström4fa7eca2016-03-02 15:05:53 +0100767 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000768
Per83d09102016-04-15 14:59:13 +0200769 rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length);
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000770
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000771 int64_t rtt = 0;
Niels Möller2ff1f2a2018-08-09 16:16:34 +0200772 rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr);
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000773 if (rtt == 0) {
774 // Waiting for valid rtt.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100775 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000776 }
777 uint32_t ntp_secs = 0;
778 uint32_t ntp_frac = 0;
779 uint32_t rtp_timestamp = 0;
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100780 uint32_t recieved_ntp_secs = 0;
781 uint32_t recieved_ntp_frac = 0;
782 if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs,
783 &recieved_ntp_frac, &rtp_timestamp) != 0) {
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000784 // Waiting for RTCP.
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100785 return true;
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +0000786 }
Ilya Nikolaevskiy7172ea12017-10-30 11:17:34 +0100787 NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac);
788 int64_t time_since_recieved =
789 clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs();
790 // Don't use old SRs to estimate time.
791 if (time_since_recieved <= 1) {
792 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
793 }
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000794
Peter Boströmd1d66ba2016-02-08 14:07:14 +0100795 return true;
wu@webrtc.orgcd701192014-04-24 22:10:24 +0000796}
797
philipeld4fac692017-09-04 07:03:46 -0700798void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) {
tommif284b7f2017-02-27 01:59:36 -0800799 if (!nack_module_)
800 return;
801
philipela45102f2017-02-22 05:30:39 -0800802 int seq_num = -1;
803 {
804 rtc::CritScope lock(&last_seq_num_cs_);
805 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
806 if (seq_num_it != last_seq_num_for_pic_id_.end())
807 seq_num = seq_num_it->second;
philipelfd5a20f2016-11-15 00:57:57 -0800808 }
philipela45102f2017-02-22 05:30:39 -0800809 if (seq_num != -1)
810 nack_module_->ClearUpTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800811}
812
philipeld4fac692017-09-04 07:03:46 -0700813void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) {
philipela45102f2017-02-22 05:30:39 -0800814 int seq_num = -1;
815 {
816 rtc::CritScope lock(&last_seq_num_cs_);
817 auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
818 if (seq_num_it != last_seq_num_for_pic_id_.end()) {
819 seq_num = seq_num_it->second;
820 last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(),
821 ++seq_num_it);
philipelfd5a20f2016-11-15 00:57:57 -0800822 }
philipela45102f2017-02-22 05:30:39 -0800823 }
824 if (seq_num != -1) {
825 packet_buffer_->ClearTo(seq_num);
826 reference_finder_->ClearTo(seq_num);
philipelfd5a20f2016-11-15 00:57:57 -0800827 }
828}
829
nisseb1f2ff92017-06-09 04:01:55 -0700830void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) {
mflodmandc7d0d22016-05-06 05:32:22 -0700831 rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode
832 : RtcpMode::kOff);
833}
834
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100835int RtpVideoStreamReceiver::GetUniqueFramesSeen() const {
836 return packet_buffer_->GetUniqueFramesSeen();
837}
838
nisseb1f2ff92017-06-09 04:01:55 -0700839void RtpVideoStreamReceiver::StartReceive() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200840 RTC_DCHECK_RUN_ON(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000841 receiving_ = true;
842}
843
nisseb1f2ff92017-06-09 04:01:55 -0700844void RtpVideoStreamReceiver::StopReceive() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200845 RTC_DCHECK_RUN_ON(&worker_task_checker_);
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000846 receiving_ = false;
847}
848
nisseb1f2ff92017-06-09 04:01:55 -0700849void RtpVideoStreamReceiver::UpdateHistograms() {
brandtrd55c3f62016-10-31 04:51:33 -0700850 FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter();
asapersson0c43f772016-11-30 01:42:26 -0800851 if (counter.first_packet_time_ms == -1)
852 return;
853
854 int64_t elapsed_sec =
855 (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000;
856 if (elapsed_sec < metrics::kMinRunTimeInSeconds)
857 return;
858
mflodmandc7d0d22016-05-06 05:32:22 -0700859 if (counter.num_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700860 RTC_HISTOGRAM_PERCENTAGE(
mflodmandc7d0d22016-05-06 05:32:22 -0700861 "WebRTC.Video.ReceivedFecPacketsInPercent",
862 static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets));
863 }
864 if (counter.num_fec_packets > 0) {
asapersson1d02d3e2016-09-09 22:40:25 -0700865 RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec",
866 static_cast<int>(counter.num_recovered_packets *
867 100 / counter.num_fec_packets));
mflodmandc7d0d22016-05-06 05:32:22 -0700868 }
869}
870
nisseb1f2ff92017-06-09 04:01:55 -0700871void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) {
philipel022b54e2016-12-20 04:15:59 -0800872 auto codec_params_it = pt_codec_params_.find(payload_type);
873 if (codec_params_it == pt_codec_params_.end())
874 return;
875
Mirko Bonadei675513b2017-11-09 11:09:25 +0100876 RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for"
877 << " payload type: " << static_cast<int>(payload_type);
philipel022b54e2016-12-20 04:15:59 -0800878
879 H264SpropParameterSets sprop_decoder;
880 auto sprop_base64_it =
881 codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets);
882
883 if (sprop_base64_it == codec_params_it->second.end())
884 return;
885
johan62d02c32017-01-24 04:38:27 -0800886 if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str()))
philipel022b54e2016-12-20 04:15:59 -0800887 return;
888
johand2b092f2017-01-24 02:38:17 -0800889 tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(),
890 sprop_decoder.pps_nalu());
philipel022b54e2016-12-20 04:15:59 -0800891}
892
mflodman@webrtc.orgad4ee362011-11-28 22:39:24 +0000893} // namespace webrtc