niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 39e9659 | 2012-03-01 18:22:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/rtp_video_stream_receiver.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <limits> |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 15 | #include <utility> |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 16 | #include <vector> |
mflodman@webrtc.org | 4fd5527 | 2013-02-06 17:46:39 +0000 | [diff] [blame] | 17 | |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 18 | #include "absl/algorithm/container.h" |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 19 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "media/base/media_constants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/pacing/packet_router.h" |
| 22 | #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" |
| 23 | #include "modules/rtp_rtcp/include/receive_statistics.h" |
| 24 | #include "modules/rtp_rtcp/include/rtp_cvo.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/rtp_rtcp/include/rtp_rtcp.h" |
| 26 | #include "modules/rtp_rtcp/include/ulpfec_receiver.h" |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 27 | #include "modules/rtp_rtcp/source/rtp_format.h" |
philipel | b3e42a4 | 2018-09-13 10:57:14 +0200 | [diff] [blame] | 28 | #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "modules/rtp_rtcp/source/rtp_header_extensions.h" |
| 30 | #include "modules/rtp_rtcp/source/rtp_packet_received.h" |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 31 | #include "modules/rtp_rtcp/source/rtp_rtcp_config.h" |
Niels Möller | fe407b7 | 2019-09-10 10:48:48 +0200 | [diff] [blame] | 32 | #include "modules/utility/include/process_thread.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "modules/video_coding/frame_object.h" |
| 34 | #include "modules/video_coding/h264_sprop_parameter_sets.h" |
| 35 | #include "modules/video_coding/h264_sps_pps_tracker.h" |
Ilya Nikolaevskiy | 8643b78 | 2018-06-07 16:15:40 +0200 | [diff] [blame] | 36 | #include "modules/video_coding/nack_module.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "modules/video_coding/packet_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "rtc_base/checks.h" |
| 39 | #include "rtc_base/location.h" |
| 40 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 41 | #include "rtc_base/strings/string_builder.h" |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 42 | #include "rtc_base/system/fallthrough.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "system_wrappers/include/field_trial.h" |
| 44 | #include "system_wrappers/include/metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 45 | #include "video/receive_statistics_proxy.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
| 47 | namespace webrtc { |
| 48 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 49 | namespace { |
philipel | 3bf97cf | 2017-08-10 18:10:59 +0200 | [diff] [blame] | 50 | // TODO(philipel): Change kPacketBufferStartSize back to 32 in M63 see: |
| 51 | // crbug.com/752886 |
| 52 | constexpr int kPacketBufferStartSize = 512; |
Johannes Kron | 201596f | 2018-10-22 14:33:39 +0200 | [diff] [blame] | 53 | constexpr int kPacketBufferMaxSize = 2048; |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 54 | |
| 55 | int PacketBufferMaxSize() { |
| 56 | // The group here must be a positive power of 2, in which case that is used as |
| 57 | // size. All other values shall result in the default value being used. |
| 58 | const std::string group_name = |
| 59 | webrtc::field_trial::FindFullName("WebRTC-PacketBufferMaxSize"); |
| 60 | int packet_buffer_max_size = kPacketBufferMaxSize; |
| 61 | if (!group_name.empty() && |
| 62 | (sscanf(group_name.c_str(), "%d", &packet_buffer_max_size) != 1 || |
| 63 | packet_buffer_max_size <= 0 || |
| 64 | // Verify that the number is a positive power of 2. |
| 65 | (packet_buffer_max_size & (packet_buffer_max_size - 1)) != 0)) { |
| 66 | RTC_LOG(LS_WARNING) << "Invalid packet buffer max size: " << group_name; |
| 67 | packet_buffer_max_size = kPacketBufferMaxSize; |
| 68 | } |
| 69 | return packet_buffer_max_size; |
| 70 | } |
| 71 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | } // namespace |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 73 | |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 74 | std::unique_ptr<RtpRtcp> CreateRtpRtcpModule( |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 75 | Clock* clock, |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 76 | ReceiveStatistics* receive_statistics, |
| 77 | Transport* outgoing_transport, |
| 78 | RtcpRttStats* rtt_stats, |
Erik Språng | e3a10e1 | 2019-08-19 15:45:00 +0200 | [diff] [blame] | 79 | RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer, |
| 80 | uint32_t local_ssrc) { |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 81 | RtpRtcp::Configuration configuration; |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 82 | configuration.clock = clock; |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 83 | configuration.audio = false; |
| 84 | configuration.receiver_only = true; |
| 85 | configuration.receive_statistics = receive_statistics; |
| 86 | configuration.outgoing_transport = outgoing_transport; |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 87 | configuration.rtt_stats = rtt_stats; |
| 88 | configuration.rtcp_packet_type_counter_observer = |
| 89 | rtcp_packet_type_counter_observer; |
Erik Språng | 54d5d2c | 2019-08-20 17:22:36 +0200 | [diff] [blame] | 90 | configuration.local_media_ssrc = local_ssrc; |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 91 | |
Danil Chapovalov | c44f6cc | 2019-03-06 11:31:09 +0100 | [diff] [blame] | 92 | std::unique_ptr<RtpRtcp> rtp_rtcp = RtpRtcp::Create(configuration); |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 93 | rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound); |
| 94 | |
| 95 | return rtp_rtcp; |
| 96 | } |
| 97 | |
stefan@webrtc.org | eb24b04 | 2014-10-14 11:40:13 +0000 | [diff] [blame] | 98 | static const int kPacketLogIntervalMs = 10000; |
| 99 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 100 | RtpVideoStreamReceiver::RtcpFeedbackBuffer::RtcpFeedbackBuffer( |
| 101 | KeyFrameRequestSender* key_frame_request_sender, |
| 102 | NackSender* nack_sender, |
| 103 | LossNotificationSender* loss_notification_sender) |
| 104 | : key_frame_request_sender_(key_frame_request_sender), |
| 105 | nack_sender_(nack_sender), |
| 106 | loss_notification_sender_(loss_notification_sender), |
| 107 | request_key_frame_(false) { |
| 108 | RTC_DCHECK(key_frame_request_sender_); |
| 109 | RTC_DCHECK(nack_sender_); |
| 110 | RTC_DCHECK(loss_notification_sender_); |
| 111 | } |
| 112 | |
| 113 | void RtpVideoStreamReceiver::RtcpFeedbackBuffer::RequestKeyFrame() { |
| 114 | rtc::CritScope lock(&cs_); |
| 115 | request_key_frame_ = true; |
| 116 | } |
| 117 | |
| 118 | void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendNack( |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 119 | const std::vector<uint16_t>& sequence_numbers, |
| 120 | bool buffering_allowed) { |
| 121 | RTC_DCHECK(!sequence_numbers.empty()); |
| 122 | rtc::CritScope lock(&cs_); |
| 123 | nack_sequence_numbers_.insert(nack_sequence_numbers_.end(), |
| 124 | sequence_numbers.cbegin(), |
| 125 | sequence_numbers.cend()); |
| 126 | if (!buffering_allowed) { |
| 127 | // Note that while *buffering* is not allowed, *batching* is, meaning that |
| 128 | // previously buffered messages may be sent along with the current message. |
| 129 | SendBufferedRtcpFeedback(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendLossNotification( |
| 134 | uint16_t last_decoded_seq_num, |
| 135 | uint16_t last_received_seq_num, |
Elad Alon | e86af2c | 2019-06-03 14:37:50 +0200 | [diff] [blame] | 136 | bool decodability_flag, |
| 137 | bool buffering_allowed) { |
| 138 | RTC_DCHECK(buffering_allowed); |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 139 | rtc::CritScope lock(&cs_); |
Elad Alon | 36690cd | 2019-06-04 22:59:54 +0200 | [diff] [blame] | 140 | RTC_DCHECK(!lntf_state_) |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 141 | << "SendLossNotification() called twice in a row with no call to " |
| 142 | "SendBufferedRtcpFeedback() in between."; |
| 143 | lntf_state_ = absl::make_optional<LossNotificationState>( |
| 144 | last_decoded_seq_num, last_received_seq_num, decodability_flag); |
| 145 | } |
| 146 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 147 | void RtpVideoStreamReceiver::RtcpFeedbackBuffer::SendBufferedRtcpFeedback() { |
| 148 | bool request_key_frame = false; |
| 149 | std::vector<uint16_t> nack_sequence_numbers; |
| 150 | absl::optional<LossNotificationState> lntf_state; |
| 151 | |
| 152 | { |
| 153 | rtc::CritScope lock(&cs_); |
| 154 | std::swap(request_key_frame, request_key_frame_); |
| 155 | std::swap(nack_sequence_numbers, nack_sequence_numbers_); |
| 156 | std::swap(lntf_state, lntf_state_); |
| 157 | } |
| 158 | |
Elad Alon | e86af2c | 2019-06-03 14:37:50 +0200 | [diff] [blame] | 159 | if (lntf_state) { |
| 160 | // If either a NACK or a key frame request is sent, we should buffer |
| 161 | // the LNTF and wait for them (NACK or key frame request) to trigger |
| 162 | // the compound feedback message. |
| 163 | // Otherwise, the LNTF should be sent out immediately. |
| 164 | const bool buffering_allowed = |
| 165 | request_key_frame || !nack_sequence_numbers.empty(); |
| 166 | |
| 167 | loss_notification_sender_->SendLossNotification( |
| 168 | lntf_state->last_decoded_seq_num, lntf_state->last_received_seq_num, |
| 169 | lntf_state->decodability_flag, buffering_allowed); |
| 170 | } |
| 171 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 172 | if (request_key_frame) { |
| 173 | key_frame_request_sender_->RequestKeyFrame(); |
| 174 | } else if (!nack_sequence_numbers.empty()) { |
| 175 | nack_sender_->SendNack(nack_sequence_numbers, true); |
| 176 | } |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 177 | } |
| 178 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 179 | RtpVideoStreamReceiver::RtpVideoStreamReceiver( |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 180 | Clock* clock, |
mflodman | fa66659 | 2016-04-28 23:15:33 -0700 | [diff] [blame] | 181 | Transport* transport, |
| 182 | RtcpRttStats* rtt_stats, |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 183 | PacketRouter* packet_router, |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 184 | const VideoReceiveStream::Config* config, |
nisse | ca5706d | 2017-09-11 02:32:16 -0700 | [diff] [blame] | 185 | ReceiveStatistics* rtp_receive_statistics, |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 186 | ReceiveStatisticsProxy* receive_stats_proxy, |
Erik Språng | 737336d | 2016-07-29 12:59:36 +0200 | [diff] [blame] | 187 | ProcessThread* process_thread, |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 188 | NackSender* nack_sender, |
Niels Möller | 2f5554d | 2019-05-29 13:35:14 +0200 | [diff] [blame] | 189 | KeyFrameRequestSender* keyframe_request_sender, |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 190 | video_coding::OnCompleteFrameCallback* complete_frame_callback, |
| 191 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 192 | : clock_(clock), |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 193 | config_(*config), |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 194 | packet_router_(packet_router), |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 195 | process_thread_(process_thread), |
Sebastian Jansson | 8026d60 | 2019-03-04 19:39:01 +0100 | [diff] [blame] | 196 | ntp_estimator_(clock), |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 197 | rtp_header_extensions_(config_.rtp.extensions), |
nisse | ca5706d | 2017-09-11 02:32:16 -0700 | [diff] [blame] | 198 | rtp_receive_statistics_(rtp_receive_statistics), |
Ilya Nikolaevskiy | 2d821c3 | 2019-06-26 14:39:36 +0200 | [diff] [blame] | 199 | ulpfec_receiver_(UlpfecReceiver::Create(config->rtp.remote_ssrc, |
| 200 | this, |
| 201 | config->rtp.extensions)), |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 202 | receiving_(false), |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 203 | last_packet_log_ms_(-1), |
Niels Möller | 2f5554d | 2019-05-29 13:35:14 +0200 | [diff] [blame] | 204 | rtp_rtcp_(CreateRtpRtcpModule(clock, |
| 205 | rtp_receive_statistics_, |
| 206 | transport, |
| 207 | rtt_stats, |
Erik Språng | e3a10e1 | 2019-08-19 15:45:00 +0200 | [diff] [blame] | 208 | receive_stats_proxy, |
| 209 | config_.rtp.local_ssrc)), |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 210 | complete_frame_callback_(complete_frame_callback), |
Niels Möller | 2f5554d | 2019-05-29 13:35:14 +0200 | [diff] [blame] | 211 | keyframe_request_sender_(keyframe_request_sender), |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 212 | // TODO(bugs.webrtc.org/10336): Let |rtcp_feedback_buffer_| communicate |
| 213 | // directly with |rtp_rtcp_|. |
| 214 | rtcp_feedback_buffer_(this, nack_sender, this), |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 215 | packet_buffer_(clock_, |
| 216 | kPacketBufferStartSize, |
| 217 | PacketBufferMaxSize(), |
| 218 | this), |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 219 | has_received_frame_(false), |
| 220 | frames_decryptable_(false) { |
eladalon | 822ff2b | 2017-08-01 06:30:28 -0700 | [diff] [blame] | 221 | constexpr bool remb_candidate = true; |
Niels Möller | 60f4e29 | 2019-05-20 11:06:33 +0200 | [diff] [blame] | 222 | if (packet_router_) |
| 223 | packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate); |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 224 | |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 225 | RTC_DCHECK(config_.rtp.rtcp_mode != RtcpMode::kOff) |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 226 | << "A stream should not be configured with RTCP disabled. This value is " |
| 227 | "reserved for internal usage."; |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 228 | // TODO(pbos): What's an appropriate local_ssrc for receive-only streams? |
| 229 | RTC_DCHECK(config_.rtp.local_ssrc != 0); |
| 230 | RTC_DCHECK(config_.rtp.remote_ssrc != config_.rtp.local_ssrc); |
| 231 | |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 232 | rtp_rtcp_->SetRTCPStatus(config_.rtp.rtcp_mode); |
stefan | b4ab381 | 2017-06-09 06:12:11 -0700 | [diff] [blame] | 233 | rtp_rtcp_->SetRemoteSSRC(config_.rtp.remote_ssrc); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 234 | |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 235 | static const int kMaxPacketAgeToNack = 450; |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 236 | const int max_reordering_threshold = (config_.rtp.nack.rtp_history_ms > 0) |
| 237 | ? kMaxPacketAgeToNack |
| 238 | : kDefaultMaxReorderingThreshold; |
Niels Möller | 87da109 | 2019-05-24 14:04:28 +0200 | [diff] [blame] | 239 | rtp_receive_statistics_->SetMaxReorderingThreshold(config_.rtp.remote_ssrc, |
| 240 | max_reordering_threshold); |
| 241 | // TODO(nisse): For historic reasons, we applied the above |
| 242 | // max_reordering_threshold also for RTX stats, which makes little sense since |
| 243 | // we don't NACK rtx packets. Consider deleting the below block, and rely on |
| 244 | // the default threshold. |
| 245 | if (config_.rtp.rtx_ssrc) { |
| 246 | rtp_receive_statistics_->SetMaxReorderingThreshold( |
| 247 | config_.rtp.rtx_ssrc, max_reordering_threshold); |
| 248 | } |
Tommi | 733b547 | 2016-06-10 17:58:01 +0200 | [diff] [blame] | 249 | if (config_.rtp.rtcp_xr.receiver_reference_time_report) |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 250 | rtp_rtcp_->SetRtcpXrRrtrStatus(true); |
| 251 | |
| 252 | // Stats callback for CNAME changes. |
Niels Möller | 4d7c405 | 2019-08-05 12:45:19 +0200 | [diff] [blame] | 253 | rtp_rtcp_->RegisterRtcpCnameCallback(receive_stats_proxy); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 254 | |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 255 | process_thread_->RegisterModule(rtp_rtcp_.get(), RTC_FROM_HERE); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 256 | |
Elad Alon | fadb181 | 2019-05-24 13:40:02 +0200 | [diff] [blame] | 257 | if (config_.rtp.lntf.enabled) { |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 258 | loss_notification_controller_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 259 | std::make_unique<LossNotificationController>(&rtcp_feedback_buffer_, |
| 260 | &rtcp_feedback_buffer_); |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (config_.rtp.nack.rtp_history_ms != 0) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 264 | nack_module_ = std::make_unique<NackModule>(clock_, &rtcp_feedback_buffer_, |
| 265 | &rtcp_feedback_buffer_); |
tommi | dea489f | 2017-03-03 03:20:24 -0800 | [diff] [blame] | 266 | process_thread_->RegisterModule(nack_module_.get(), RTC_FROM_HERE); |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 267 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 268 | |
Elad Alon | a8f5461 | 2018-11-06 11:21:25 +0100 | [diff] [blame] | 269 | reference_finder_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 270 | std::make_unique<video_coding::RtpFrameReferenceFinder>(this); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 271 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 272 | // Only construct the encrypted receiver if frame encryption is enabled. |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 273 | if (config_.crypto_options.sframe.require_frame_encryption) { |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 274 | buffered_frame_decryptor_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 275 | std::make_unique<BufferedFrameDecryptor>(this, this); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 276 | if (frame_decryptor != nullptr) { |
| 277 | buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor)); |
| 278 | } |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 279 | } |
mflodman | c0e58a3 | 2016-04-25 01:26:26 -0700 | [diff] [blame] | 280 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 281 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 282 | RtpVideoStreamReceiver::~RtpVideoStreamReceiver() { |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 283 | RTC_DCHECK(secondary_sinks_.empty()); |
| 284 | |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 285 | if (nack_module_) { |
| 286 | process_thread_->DeRegisterModule(nack_module_.get()); |
| 287 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 289 | process_thread_->DeRegisterModule(rtp_rtcp_.get()); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 290 | |
Niels Möller | 60f4e29 | 2019-05-20 11:06:33 +0200 | [diff] [blame] | 291 | if (packet_router_) |
| 292 | packet_router_->RemoveReceiveRtpModule(rtp_rtcp_.get()); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 293 | UpdateHistograms(); |
asapersson@webrtc.org | 0800db7 | 2015-01-15 07:40:20 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 296 | void RtpVideoStreamReceiver::AddReceiveCodec( |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 297 | const VideoCodec& video_codec, |
Mirta Dvornicic | fe68daa | 2019-05-23 13:21:12 +0200 | [diff] [blame] | 298 | const std::map<std::string, std::string>& codec_params, |
| 299 | bool raw_payload) { |
| 300 | absl::optional<VideoCodecType> video_type; |
| 301 | if (!raw_payload) { |
| 302 | video_type = video_codec.codecType; |
| 303 | } |
| 304 | payload_type_map_.emplace(video_codec.plType, video_type); |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 305 | pt_codec_params_.emplace(video_codec.plType, codec_params); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 308 | absl::optional<Syncable::Info> RtpVideoStreamReceiver::GetSyncInfo() const { |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 309 | Syncable::Info info; |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 310 | if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs, |
| 311 | &info.capture_time_ntp_frac, nullptr, nullptr, |
| 312 | &info.capture_time_source_clock) != 0) { |
| 313 | return absl::nullopt; |
| 314 | } |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame] | 315 | { |
Chen Xing | 90f3b89 | 2019-06-25 10:16:14 +0200 | [diff] [blame] | 316 | rtc::CritScope lock(&sync_info_lock_); |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame] | 317 | if (!last_received_rtp_timestamp_ || !last_received_rtp_system_time_ms_) { |
| 318 | return absl::nullopt; |
| 319 | } |
| 320 | info.latest_received_capture_timestamp = *last_received_rtp_timestamp_; |
| 321 | info.latest_receive_time_ms = *last_received_rtp_system_time_ms_; |
| 322 | } |
Niels Möller | df9e9ae | 2018-07-31 08:29:53 +0200 | [diff] [blame] | 323 | |
| 324 | // Leaves info.current_delay_ms uninitialized. |
| 325 | return info; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 328 | void RtpVideoStreamReceiver::OnReceivedPayloadData( |
| 329 | rtc::ArrayView<const uint8_t> codec_payload, |
| 330 | const RtpPacketReceived& rtp_packet, |
| 331 | const RTPVideoHeader& video) { |
| 332 | RTPHeader rtp_header; |
| 333 | rtp_packet.GetHeader(&rtp_header); |
| 334 | VCMPacket packet(codec_payload.data(), codec_payload.size(), rtp_header, |
| 335 | video, ntp_estimator_.Estimate(rtp_packet.Timestamp()), |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 336 | clock_->TimeInMilliseconds()); |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 337 | |
| 338 | RTPVideoHeader& video_header = packet.video_header; |
| 339 | video_header.rotation = kVideoRotation_0; |
| 340 | video_header.content_type = VideoContentType::UNSPECIFIED; |
| 341 | video_header.video_timing.flags = VideoSendTiming::kInvalid; |
| 342 | video_header.is_last_packet_in_frame |= rtp_packet.Marker(); |
| 343 | video_header.frame_marking.temporal_id = kNoTemporalIdx; |
| 344 | |
| 345 | if (const auto* vp9_header = |
| 346 | absl::get_if<RTPVideoHeaderVP9>(&video_header.video_type_header)) { |
| 347 | video_header.is_last_packet_in_frame |= vp9_header->end_of_frame; |
| 348 | video_header.is_first_packet_in_frame |= vp9_header->beginning_of_frame; |
| 349 | } |
| 350 | |
| 351 | rtp_packet.GetExtension<VideoOrientation>(&video_header.rotation); |
| 352 | rtp_packet.GetExtension<VideoContentTypeExtension>( |
| 353 | &video_header.content_type); |
| 354 | rtp_packet.GetExtension<VideoTimingExtension>(&video_header.video_timing); |
| 355 | rtp_packet.GetExtension<PlayoutDelayLimits>(&video_header.playout_delay); |
| 356 | rtp_packet.GetExtension<FrameMarkingExtension>(&video_header.frame_marking); |
| 357 | |
| 358 | RtpGenericFrameDescriptor& generic_descriptor = |
| 359 | packet.generic_descriptor.emplace(); |
| 360 | if (rtp_packet.GetExtension<RtpGenericFrameDescriptorExtension01>( |
| 361 | &generic_descriptor)) { |
| 362 | if (rtp_packet.HasExtension<RtpGenericFrameDescriptorExtension00>()) { |
| 363 | RTC_LOG(LS_WARNING) << "RTP packet had two different GFD versions."; |
| 364 | return; |
| 365 | } |
| 366 | generic_descriptor.SetByteRepresentation( |
| 367 | rtp_packet.GetRawExtension<RtpGenericFrameDescriptorExtension01>()); |
| 368 | } else if ((rtp_packet.GetExtension<RtpGenericFrameDescriptorExtension00>( |
| 369 | &generic_descriptor))) { |
| 370 | generic_descriptor.SetByteRepresentation( |
| 371 | rtp_packet.GetRawExtension<RtpGenericFrameDescriptorExtension00>()); |
| 372 | } else { |
| 373 | packet.generic_descriptor = absl::nullopt; |
| 374 | } |
| 375 | if (packet.generic_descriptor != absl::nullopt) { |
| 376 | video_header.is_first_packet_in_frame = |
| 377 | packet.generic_descriptor->FirstPacketInSubFrame(); |
| 378 | video_header.is_last_packet_in_frame = |
| 379 | rtp_packet.Marker() || |
| 380 | packet.generic_descriptor->LastPacketInSubFrame(); |
| 381 | |
| 382 | if (packet.generic_descriptor->FirstPacketInSubFrame()) { |
| 383 | video_header.frame_type = |
| 384 | packet.generic_descriptor->FrameDependenciesDiffs().empty() |
| 385 | ? VideoFrameType::kVideoFrameKey |
| 386 | : VideoFrameType::kVideoFrameDelta; |
| 387 | } |
| 388 | |
| 389 | video_header.width = packet.generic_descriptor->Width(); |
| 390 | video_header.height = packet.generic_descriptor->Height(); |
| 391 | } |
| 392 | |
| 393 | // Color space should only be transmitted in the last packet of a frame, |
| 394 | // therefore, neglect it otherwise so that last_color_space_ is not reset by |
| 395 | // mistake. |
| 396 | if (video_header.is_last_packet_in_frame) { |
| 397 | video_header.color_space = rtp_packet.GetExtension<ColorSpaceExtension>(); |
| 398 | if (video_header.color_space || |
| 399 | video_header.frame_type == VideoFrameType::kVideoFrameKey) { |
| 400 | // Store color space since it's only transmitted when changed or for key |
| 401 | // frames. Color space will be cleared if a key frame is transmitted |
| 402 | // without color space information. |
| 403 | last_color_space_ = video_header.color_space; |
| 404 | } else if (last_color_space_) { |
| 405 | video_header.color_space = last_color_space_; |
| 406 | } |
| 407 | } |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 408 | |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 409 | if (loss_notification_controller_) { |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 410 | if (rtp_packet.recovered()) { |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 411 | // TODO(bugs.webrtc.org/10336): Implement support for reordering. |
Niels Möller | a740142 | 2019-09-13 14:18:58 +0200 | [diff] [blame] | 412 | RTC_LOG(LS_INFO) |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 413 | << "LossNotificationController does not support reordering."; |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 414 | } else if (!packet.generic_descriptor) { |
Niels Möller | a740142 | 2019-09-13 14:18:58 +0200 | [diff] [blame] | 415 | RTC_LOG(LS_WARNING) << "LossNotificationController requires generic " |
| 416 | "frame descriptor, but it is missing."; |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 417 | } else { |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 418 | loss_notification_controller_->OnReceivedPacket( |
| 419 | rtp_packet.SequenceNumber(), *packet.generic_descriptor); |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Niels Möller | 8dad9b4 | 2018-08-22 10:36:35 +0200 | [diff] [blame] | 423 | if (nack_module_) { |
Niels Möller | abbc50e | 2019-04-24 09:41:16 +0200 | [diff] [blame] | 424 | const bool is_keyframe = |
| 425 | video_header.is_first_packet_in_frame && |
| 426 | video_header.frame_type == VideoFrameType::kVideoFrameKey; |
Niels Möller | 8dad9b4 | 2018-08-22 10:36:35 +0200 | [diff] [blame] | 427 | |
| 428 | packet.timesNacked = nack_module_->OnReceivedPacket( |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 429 | rtp_packet.SequenceNumber(), is_keyframe, rtp_packet.recovered()); |
Niels Möller | 8dad9b4 | 2018-08-22 10:36:35 +0200 | [diff] [blame] | 430 | } else { |
| 431 | packet.timesNacked = -1; |
| 432 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 433 | |
philipel | 54ca919 | 2017-03-21 05:45:18 -0700 | [diff] [blame] | 434 | if (packet.sizeBytes == 0) { |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 435 | NotifyReceiverOfEmptyPacket(packet.seqNum); |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 436 | rtcp_feedback_buffer_.SendBufferedRtcpFeedback(); |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 437 | return; |
philipel | 54ca919 | 2017-03-21 05:45:18 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Niels Möller | d5e02f0 | 2019-02-20 13:12:21 +0100 | [diff] [blame] | 440 | if (packet.codec() == kVideoCodecH264) { |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 441 | // Only when we start to receive packets will we know what payload type |
| 442 | // that will be used. When we know the payload type insert the correct |
| 443 | // sps/pps into the tracker. |
| 444 | if (packet.payloadType != last_payload_type_) { |
| 445 | last_payload_type_ = packet.payloadType; |
| 446 | InsertSpsPpsIntoTracker(packet.payloadType); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 447 | } |
| 448 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 449 | switch (tracker_.CopyAndFixBitstream(&packet)) { |
| 450 | case video_coding::H264SpsPpsTracker::kRequestKeyframe: |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 451 | rtcp_feedback_buffer_.RequestKeyFrame(); |
| 452 | rtcp_feedback_buffer_.SendBufferedRtcpFeedback(); |
Karl Wiberg | 80ba333 | 2018-02-05 10:33:35 +0100 | [diff] [blame] | 453 | RTC_FALLTHROUGH(); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 454 | case video_coding::H264SpsPpsTracker::kDrop: |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 455 | return; |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 456 | case video_coding::H264SpsPpsTracker::kInsert: |
| 457 | break; |
| 458 | } |
| 459 | |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 460 | } else { |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 461 | uint8_t* data = new uint8_t[packet.sizeBytes]; |
| 462 | memcpy(data, packet.dataPtr, packet.sizeBytes); |
| 463 | packet.dataPtr = data; |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 464 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 465 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 466 | rtcp_feedback_buffer_.SendBufferedRtcpFeedback(); |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 467 | if (!packet_buffer_.InsertPacket(&packet)) { |
Johannes Kron | bd3f305 | 2019-08-01 15:45:54 +0200 | [diff] [blame] | 468 | RequestKeyFrame(); |
| 469 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 470 | } |
| 471 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 472 | void RtpVideoStreamReceiver::OnRecoveredPacket(const uint8_t* rtp_packet, |
| 473 | size_t rtp_packet_length) { |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 474 | RtpPacketReceived packet; |
| 475 | if (!packet.Parse(rtp_packet, rtp_packet_length)) |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 476 | return; |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 477 | if (packet.PayloadType() == config_.rtp.red_payload_type) { |
| 478 | RTC_LOG(LS_WARNING) << "Discarding recovered packet with RED encapsulation"; |
| 479 | return; |
| 480 | } |
| 481 | |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 482 | packet.IdentifyExtensions(rtp_header_extensions_); |
| 483 | packet.set_payload_type_frequency(kVideoPayloadTypeFrequency); |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 484 | // TODO(nisse): UlpfecReceiverImpl::ProcessReceivedFec passes both |
| 485 | // original (decapsulated) media packets and recovered packets to |
| 486 | // this callback. We need a way to distinguish, for setting |
| 487 | // packet.recovered() correctly. Ideally, move RED decapsulation out |
| 488 | // of the Ulpfec implementation. |
Niels Möller | b0573bc | 2017-09-25 10:47:00 +0200 | [diff] [blame] | 489 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 490 | ReceivePacket(packet); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 491 | } |
| 492 | |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 493 | // This method handles both regular RTP packets and packets recovered |
| 494 | // via FlexFEC. |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 495 | void RtpVideoStreamReceiver::OnRtpPacket(const RtpPacketReceived& packet) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 496 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 497 | |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 498 | if (!receiving_) { |
| 499 | return; |
| 500 | } |
solenberg@webrtc.org | fc32046 | 2014-02-11 15:27:49 +0000 | [diff] [blame] | 501 | |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 502 | if (!packet.recovered()) { |
Jonas Oreland | 49ac595 | 2018-09-26 16:04:32 +0200 | [diff] [blame] | 503 | // TODO(nisse): Exclude out-of-order packets? |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 504 | int64_t now_ms = clock_->TimeInMilliseconds(); |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame] | 505 | { |
Chen Xing | 90f3b89 | 2019-06-25 10:16:14 +0200 | [diff] [blame] | 506 | rtc::CritScope cs(&sync_info_lock_); |
Niels Möller | b0d4b41 | 2018-08-28 13:58:15 +0200 | [diff] [blame] | 507 | last_received_rtp_timestamp_ = packet.Timestamp(); |
| 508 | last_received_rtp_system_time_ms_ = now_ms; |
| 509 | } |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 510 | // Periodically log the RTP header of incoming packets. |
| 511 | if (now_ms - last_packet_log_ms_ > kPacketLogIntervalMs) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 512 | rtc::StringBuilder ss; |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 513 | ss << "Packet received on SSRC: " << packet.Ssrc() |
| 514 | << " with payload type: " << static_cast<int>(packet.PayloadType()) |
| 515 | << ", timestamp: " << packet.Timestamp() |
| 516 | << ", sequence number: " << packet.SequenceNumber() |
| 517 | << ", arrival time: " << packet.arrival_time_ms(); |
| 518 | int32_t time_offset; |
| 519 | if (packet.GetExtension<TransmissionOffset>(&time_offset)) { |
| 520 | ss << ", toffset: " << time_offset; |
nisse | 38cc1d6 | 2017-02-13 05:59:46 -0800 | [diff] [blame] | 521 | } |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 522 | uint32_t send_time; |
| 523 | if (packet.GetExtension<AbsoluteSendTime>(&send_time)) { |
| 524 | ss << ", abs send time: " << send_time; |
| 525 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 526 | RTC_LOG(LS_INFO) << ss.str(); |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 527 | last_packet_log_ms_ = now_ms; |
stefan@webrtc.org | eb24b04 | 2014-10-14 11:40:13 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 530 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 531 | ReceivePacket(packet); |
nisse | 38cc1d6 | 2017-02-13 05:59:46 -0800 | [diff] [blame] | 532 | |
asapersson@webrtc.org | 1457b47 | 2014-05-26 13:06:04 +0000 | [diff] [blame] | 533 | // Update receive statistics after ReceivePacket. |
| 534 | // Receive statistics will be reset if the payload type changes (make sure |
| 535 | // that the first packet is included in the stats). |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 536 | if (!packet.recovered()) { |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 537 | rtp_receive_statistics_->OnRtpPacket(packet); |
nisse | d2ef314 | 2017-05-11 08:00:58 -0700 | [diff] [blame] | 538 | } |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 539 | |
| 540 | for (RtpPacketSinkInterface* secondary_sink : secondary_sinks_) { |
| 541 | secondary_sink->OnRtpPacket(packet); |
| 542 | } |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Niels Möller | 4168437 | 2019-03-25 15:51:03 +0100 | [diff] [blame] | 545 | void RtpVideoStreamReceiver::RequestKeyFrame() { |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 546 | // TODO(bugs.webrtc.org/10336): Allow the sender to ignore key frame requests |
| 547 | // issued by anything other than the LossNotificationController if it (the |
| 548 | // sender) is relying on LNTF alone. |
Niels Möller | 2f5554d | 2019-05-29 13:35:14 +0200 | [diff] [blame] | 549 | if (keyframe_request_sender_) { |
| 550 | keyframe_request_sender_->RequestKeyFrame(); |
| 551 | } else { |
Niels Möller | dd0094a | 2019-06-04 14:46:27 +0200 | [diff] [blame] | 552 | rtp_rtcp_->SendPictureLossIndication(); |
Niels Möller | 2f5554d | 2019-05-29 13:35:14 +0200 | [diff] [blame] | 553 | } |
mflodman | cfc8e3b | 2016-05-03 21:22:04 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 556 | void RtpVideoStreamReceiver::SendLossNotification( |
| 557 | uint16_t last_decoded_seq_num, |
| 558 | uint16_t last_received_seq_num, |
Elad Alon | e86af2c | 2019-06-03 14:37:50 +0200 | [diff] [blame] | 559 | bool decodability_flag, |
| 560 | bool buffering_allowed) { |
Elad Alon | fadb181 | 2019-05-24 13:40:02 +0200 | [diff] [blame] | 561 | RTC_DCHECK(config_.rtp.lntf.enabled); |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 562 | rtp_rtcp_->SendLossNotification(last_decoded_seq_num, last_received_seq_num, |
Elad Alon | e86af2c | 2019-06-03 14:37:50 +0200 | [diff] [blame] | 563 | decodability_flag, buffering_allowed); |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 564 | } |
| 565 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 566 | bool RtpVideoStreamReceiver::IsUlpfecEnabled() const { |
nisse | 3b3622f | 2017-09-26 02:49:21 -0700 | [diff] [blame] | 567 | return config_.rtp.ulpfec_payload_type != -1; |
brandtr | e6f98c7 | 2016-11-11 03:28:30 -0800 | [diff] [blame] | 568 | } |
| 569 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 570 | bool RtpVideoStreamReceiver::IsRetransmissionsEnabled() const { |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 571 | return config_.rtp.nack.rtp_history_ms > 0; |
| 572 | } |
| 573 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 574 | void RtpVideoStreamReceiver::RequestPacketRetransmit( |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 575 | const std::vector<uint16_t>& sequence_numbers) { |
| 576 | rtp_rtcp_->SendNack(sequence_numbers); |
| 577 | } |
| 578 | |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 579 | bool RtpVideoStreamReceiver::IsDecryptable() const { |
| 580 | return frames_decryptable_.load(); |
| 581 | } |
| 582 | |
Elad Alon | b4643ad | 2019-02-22 11:19:50 +0100 | [diff] [blame] | 583 | void RtpVideoStreamReceiver::OnAssembledFrame( |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 584 | std::unique_ptr<video_coding::RtpFrameObject> frame) { |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 585 | RTC_DCHECK_RUN_ON(&network_tc_); |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 586 | RTC_DCHECK(frame); |
| 587 | |
| 588 | absl::optional<RtpGenericFrameDescriptor> descriptor = |
| 589 | frame->GetGenericFrameDescriptor(); |
| 590 | |
| 591 | if (loss_notification_controller_ && descriptor) { |
| 592 | loss_notification_controller_->OnAssembledFrame( |
| 593 | frame->first_seq_num(), descriptor->FrameId(), |
| 594 | descriptor->Discardable().value_or(false), |
| 595 | descriptor->FrameDependenciesDiffs()); |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 598 | // If frames arrive before a key frame, they would not be decodable. |
| 599 | // In that case, request a key frame ASAP. |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 600 | if (!has_received_frame_) { |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 601 | if (frame->FrameType() != VideoFrameType::kVideoFrameKey) { |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 602 | // |loss_notification_controller_|, if present, would have already |
| 603 | // requested a key frame when the first packet for the non-key frame |
| 604 | // had arrived, so no need to replicate the request. |
| 605 | if (!loss_notification_controller_) { |
| 606 | RequestKeyFrame(); |
| 607 | } |
Benjamin Wright | 39feabe | 2018-10-22 13:33:09 -0700 | [diff] [blame] | 608 | } |
Elad Alon | ef09c5b | 2019-05-31 13:25:50 +0200 | [diff] [blame] | 609 | has_received_frame_ = true; |
Benjamin Wright | 39feabe | 2018-10-22 13:33:09 -0700 | [diff] [blame] | 610 | } |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 611 | |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 612 | rtc::CritScope lock(&reference_finder_lock_); |
| 613 | // Reset |reference_finder_| if |frame| is new and the codec have changed. |
| 614 | if (current_codec_) { |
| 615 | bool frame_is_newer = |
| 616 | AheadOf(frame->Timestamp(), last_assembled_frame_rtp_timestamp_); |
| 617 | |
| 618 | if (frame->codec_type() != current_codec_) { |
| 619 | if (frame_is_newer) { |
| 620 | // When we reset the |reference_finder_| we don't want new picture ids |
| 621 | // to overlap with old picture ids. To ensure that doesn't happen we |
| 622 | // start from the |last_completed_picture_id_| and add an offset in case |
| 623 | // of reordering. |
| 624 | reference_finder_ = |
| 625 | std::make_unique<video_coding::RtpFrameReferenceFinder>( |
| 626 | this, last_completed_picture_id_ + |
| 627 | std::numeric_limits<uint16_t>::max()); |
| 628 | current_codec_ = frame->codec_type(); |
| 629 | } else { |
| 630 | // Old frame from before the codec switch, discard it. |
| 631 | return; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (frame_is_newer) { |
| 636 | last_assembled_frame_rtp_timestamp_ = frame->Timestamp(); |
| 637 | } |
| 638 | } else { |
| 639 | current_codec_ = frame->codec_type(); |
| 640 | last_assembled_frame_rtp_timestamp_ = frame->Timestamp(); |
| 641 | } |
| 642 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 643 | if (buffered_frame_decryptor_ == nullptr) { |
| 644 | reference_finder_->ManageFrame(std::move(frame)); |
| 645 | } else { |
| 646 | buffered_frame_decryptor_->ManageEncryptedFrame(std::move(frame)); |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 647 | } |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 648 | } |
| 649 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 650 | void RtpVideoStreamReceiver::OnCompleteFrame( |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 651 | std::unique_ptr<video_coding::EncodedFrame> frame) { |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 652 | { |
| 653 | rtc::CritScope lock(&last_seq_num_cs_); |
| 654 | video_coding::RtpFrameObject* rtp_frame = |
| 655 | static_cast<video_coding::RtpFrameObject*>(frame.get()); |
philipel | 0fa82a6 | 2018-03-19 15:34:53 +0100 | [diff] [blame] | 656 | last_seq_num_for_pic_id_[rtp_frame->id.picture_id] = |
| 657 | rtp_frame->last_seq_num(); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 658 | } |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 659 | last_completed_picture_id_ = |
| 660 | std::max(last_completed_picture_id_, frame->id.picture_id); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 661 | complete_frame_callback_->OnCompleteFrame(std::move(frame)); |
| 662 | } |
| 663 | |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 664 | void RtpVideoStreamReceiver::OnDecryptedFrame( |
| 665 | std::unique_ptr<video_coding::RtpFrameObject> frame) { |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 666 | rtc::CritScope lock(&reference_finder_lock_); |
Benjamin Wright | 0076529 | 2018-11-30 16:18:26 -0800 | [diff] [blame] | 667 | reference_finder_->ManageFrame(std::move(frame)); |
| 668 | } |
| 669 | |
Benjamin Wright | 2af5dcb | 2019-04-09 20:08:41 +0000 | [diff] [blame] | 670 | void RtpVideoStreamReceiver::OnDecryptionStatusChange( |
| 671 | FrameDecryptorInterface::Status status) { |
| 672 | frames_decryptable_.store( |
| 673 | (status == FrameDecryptorInterface::Status::kOk) || |
| 674 | (status == FrameDecryptorInterface::Status::kRecoverable)); |
Benjamin Wright | 52426ed | 2019-03-01 11:01:59 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 677 | void RtpVideoStreamReceiver::SetFrameDecryptor( |
| 678 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) { |
| 679 | RTC_DCHECK_RUN_ON(&network_tc_); |
| 680 | if (buffered_frame_decryptor_ == nullptr) { |
| 681 | buffered_frame_decryptor_ = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 682 | std::make_unique<BufferedFrameDecryptor>(this, this); |
Benjamin Wright | a556448 | 2019-04-03 10:44:18 -0700 | [diff] [blame] | 683 | } |
| 684 | buffered_frame_decryptor_->SetFrameDecryptor(std::move(frame_decryptor)); |
| 685 | } |
| 686 | |
Tommi | 81de14f | 2018-03-25 22:19:25 +0200 | [diff] [blame] | 687 | void RtpVideoStreamReceiver::UpdateRtt(int64_t max_rtt_ms) { |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 688 | if (nack_module_) |
| 689 | nack_module_->UpdateRtt(max_rtt_ms); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 692 | absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedPacketMs() const { |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 693 | return packet_buffer_.LastReceivedPacketMs(); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 696 | absl::optional<int64_t> RtpVideoStreamReceiver::LastReceivedKeyframePacketMs() |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 697 | const { |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 698 | return packet_buffer_.LastReceivedKeyframePacketMs(); |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 699 | } |
| 700 | |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 701 | void RtpVideoStreamReceiver::AddSecondarySink(RtpPacketSinkInterface* sink) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 702 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 703 | RTC_DCHECK(!absl::c_linear_search(secondary_sinks_, sink)); |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 704 | secondary_sinks_.push_back(sink); |
| 705 | } |
| 706 | |
| 707 | void RtpVideoStreamReceiver::RemoveSecondarySink( |
| 708 | const RtpPacketSinkInterface* sink) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 709 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 710 | auto it = absl::c_find(secondary_sinks_, sink); |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 711 | if (it == secondary_sinks_.end()) { |
| 712 | // We might be rolling-back a call whose setup failed mid-way. In such a |
| 713 | // case, it's simpler to remove "everything" rather than remember what |
| 714 | // has already been added. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 715 | RTC_LOG(LS_WARNING) << "Removal of unknown sink."; |
eladalon | c0d481a | 2017-08-02 07:39:07 -0700 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | secondary_sinks_.erase(it); |
| 719 | } |
| 720 | |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 721 | void RtpVideoStreamReceiver::ReceivePacket(const RtpPacketReceived& packet) { |
| 722 | if (packet.payload_size() == 0) { |
Niels Möller | 0b92678 | 2018-08-21 17:49:24 +0200 | [diff] [blame] | 723 | // Padding or keep-alive packet. |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 724 | // TODO(nisse): Could drop empty packets earlier, but need to figure out how |
| 725 | // they should be counted in stats. |
Niels Möller | 0b92678 | 2018-08-21 17:49:24 +0200 | [diff] [blame] | 726 | NotifyReceiverOfEmptyPacket(packet.SequenceNumber()); |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 727 | return; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 728 | } |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 729 | if (packet.PayloadType() == config_.rtp.red_payload_type) { |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 730 | ParseAndHandleEncapsulatingHeader(packet); |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 731 | return; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 732 | } |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 733 | |
Mirta Dvornicic | fe68daa | 2019-05-23 13:21:12 +0200 | [diff] [blame] | 734 | const auto type_it = payload_type_map_.find(packet.PayloadType()); |
| 735 | if (type_it == payload_type_map_.end()) { |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | auto depacketizer = |
Mirta Dvornicic | fe68daa | 2019-05-23 13:21:12 +0200 | [diff] [blame] | 739 | absl::WrapUnique(RtpDepacketizer::Create(type_it->second)); |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 740 | |
| 741 | if (!depacketizer) { |
| 742 | RTC_LOG(LS_ERROR) << "Failed to create depacketizer."; |
| 743 | return; |
| 744 | } |
| 745 | RtpDepacketizer::ParsedPayload parsed_payload; |
| 746 | if (!depacketizer->Parse(&parsed_payload, packet.payload().data(), |
| 747 | packet.payload().size())) { |
| 748 | RTC_LOG(LS_WARNING) << "Failed parsing payload."; |
| 749 | return; |
| 750 | } |
| 751 | |
Danil Chapovalov | c71d85b | 2019-10-16 19:18:21 +0200 | [diff] [blame^] | 752 | OnReceivedPayloadData( |
| 753 | rtc::MakeArrayView(parsed_payload.payload, parsed_payload.payload_length), |
| 754 | packet, parsed_payload.video); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 755 | } |
| 756 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 757 | void RtpVideoStreamReceiver::ParseAndHandleEncapsulatingHeader( |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 758 | const RtpPacketReceived& packet) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 759 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 760 | if (packet.PayloadType() == config_.rtp.red_payload_type && |
| 761 | packet.payload_size() > 0) { |
| 762 | if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) { |
Peter Boström | 0b25072 | 2016-04-22 18:23:15 +0200 | [diff] [blame] | 763 | // Notify video_receiver about received FEC packets to avoid NACKing these |
| 764 | // packets. |
Niels Möller | 1f3206c | 2018-09-14 08:26:32 +0200 | [diff] [blame] | 765 | NotifyReceiverOfEmptyPacket(packet.SequenceNumber()); |
asapersson@webrtc.org | 37c0559 | 2015-01-28 13:58:27 +0000 | [diff] [blame] | 766 | } |
Danil Chapovalov | 04fd215 | 2019-09-20 11:40:12 +0200 | [diff] [blame] | 767 | if (!ulpfec_receiver_->AddReceivedRedPacket( |
| 768 | packet, config_.rtp.ulpfec_payload_type)) { |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 769 | return; |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 770 | } |
nisse | 30e8931 | 2017-05-29 08:16:37 -0700 | [diff] [blame] | 771 | ulpfec_receiver_->ProcessReceivedFec(); |
stefan@webrtc.org | 7bb8f02 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 772 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 775 | // In the case of a video stream without picture ids and no rtx the |
| 776 | // RtpFrameReferenceFinder will need to know about padding to |
| 777 | // correctly calculate frame references. |
| 778 | void RtpVideoStreamReceiver::NotifyReceiverOfEmptyPacket(uint16_t seq_num) { |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 779 | { |
| 780 | rtc::CritScope lock(&reference_finder_lock_); |
| 781 | reference_finder_->PaddingReceived(seq_num); |
| 782 | } |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 783 | packet_buffer_.PaddingReceived(seq_num); |
Niels Möller | bc01047 | 2018-03-23 13:22:29 +0100 | [diff] [blame] | 784 | if (nack_module_) { |
Ying Wang | b32bb95 | 2018-10-31 10:12:27 +0100 | [diff] [blame] | 785 | nack_module_->OnReceivedPacket(seq_num, /* is_keyframe = */ false, |
| 786 | /* is _recovered = */ false); |
asapersson@webrtc.org | 37c0559 | 2015-01-28 13:58:27 +0000 | [diff] [blame] | 787 | } |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 788 | if (loss_notification_controller_) { |
Elad Alon | ca2c430 | 2019-05-27 22:43:10 +0200 | [diff] [blame] | 789 | // TODO(bugs.webrtc.org/10336): Handle empty packets. |
Elad Alon | 7d6a4c0 | 2019-02-25 13:00:51 +0100 | [diff] [blame] | 790 | RTC_LOG(LS_WARNING) |
| 791 | << "LossNotificationController does not expect empty packets."; |
| 792 | } |
asapersson@webrtc.org | 37c0559 | 2015-01-28 13:58:27 +0000 | [diff] [blame] | 793 | } |
| 794 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 795 | bool RtpVideoStreamReceiver::DeliverRtcp(const uint8_t* rtcp_packet, |
| 796 | size_t rtcp_packet_length) { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 797 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
eladalon | 8b07305 | 2017-08-25 00:49:08 -0700 | [diff] [blame] | 798 | |
| 799 | if (!receiving_) { |
| 800 | return false; |
Peter Boström | 4fa7eca | 2016-03-02 15:05:53 +0100 | [diff] [blame] | 801 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 802 | |
Per | 83d0910 | 2016-04-15 14:59:13 +0200 | [diff] [blame] | 803 | rtp_rtcp_->IncomingRtcpPacket(rtcp_packet, rtcp_packet_length); |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 804 | |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 805 | int64_t rtt = 0; |
Niels Möller | 2ff1f2a | 2018-08-09 16:16:34 +0200 | [diff] [blame] | 806 | rtp_rtcp_->RTT(config_.rtp.remote_ssrc, &rtt, nullptr, nullptr, nullptr); |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 807 | if (rtt == 0) { |
| 808 | // Waiting for valid rtt. |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 809 | return true; |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 810 | } |
| 811 | uint32_t ntp_secs = 0; |
| 812 | uint32_t ntp_frac = 0; |
| 813 | uint32_t rtp_timestamp = 0; |
Ilya Nikolaevskiy | 7172ea1 | 2017-10-30 11:17:34 +0100 | [diff] [blame] | 814 | uint32_t recieved_ntp_secs = 0; |
| 815 | uint32_t recieved_ntp_frac = 0; |
| 816 | if (rtp_rtcp_->RemoteNTP(&ntp_secs, &ntp_frac, &recieved_ntp_secs, |
| 817 | &recieved_ntp_frac, &rtp_timestamp) != 0) { |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 818 | // Waiting for RTCP. |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 819 | return true; |
minyue@webrtc.org | 2c0cdbc | 2014-10-09 10:52:43 +0000 | [diff] [blame] | 820 | } |
Ilya Nikolaevskiy | 7172ea1 | 2017-10-30 11:17:34 +0100 | [diff] [blame] | 821 | NtpTime recieved_ntp(recieved_ntp_secs, recieved_ntp_frac); |
| 822 | int64_t time_since_recieved = |
| 823 | clock_->CurrentNtpInMilliseconds() - recieved_ntp.ToMs(); |
| 824 | // Don't use old SRs to estimate time. |
| 825 | if (time_since_recieved <= 1) { |
| 826 | ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp); |
| 827 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 828 | |
Peter Boström | d1d66ba | 2016-02-08 14:07:14 +0100 | [diff] [blame] | 829 | return true; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 830 | } |
| 831 | |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 832 | void RtpVideoStreamReceiver::FrameContinuous(int64_t picture_id) { |
tommi | f284b7f | 2017-02-27 01:59:36 -0800 | [diff] [blame] | 833 | if (!nack_module_) |
| 834 | return; |
| 835 | |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 836 | int seq_num = -1; |
| 837 | { |
| 838 | rtc::CritScope lock(&last_seq_num_cs_); |
| 839 | auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id); |
| 840 | if (seq_num_it != last_seq_num_for_pic_id_.end()) |
| 841 | seq_num = seq_num_it->second; |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 842 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 843 | if (seq_num != -1) |
| 844 | nack_module_->ClearUpTo(seq_num); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 845 | } |
| 846 | |
philipel | d4fac69 | 2017-09-04 07:03:46 -0700 | [diff] [blame] | 847 | void RtpVideoStreamReceiver::FrameDecoded(int64_t picture_id) { |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 848 | int seq_num = -1; |
| 849 | { |
| 850 | rtc::CritScope lock(&last_seq_num_cs_); |
| 851 | auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id); |
| 852 | if (seq_num_it != last_seq_num_for_pic_id_.end()) { |
| 853 | seq_num = seq_num_it->second; |
| 854 | last_seq_num_for_pic_id_.erase(last_seq_num_for_pic_id_.begin(), |
| 855 | ++seq_num_it); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 856 | } |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 857 | } |
| 858 | if (seq_num != -1) { |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 859 | packet_buffer_.ClearTo(seq_num); |
philipel | 7acc4a4 | 2019-09-26 11:25:52 +0200 | [diff] [blame] | 860 | rtc::CritScope lock(&reference_finder_lock_); |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 861 | reference_finder_->ClearTo(seq_num); |
philipel | fd5a20f | 2016-11-15 00:57:57 -0800 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 865 | void RtpVideoStreamReceiver::SignalNetworkState(NetworkState state) { |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 866 | rtp_rtcp_->SetRTCPStatus(state == kNetworkUp ? config_.rtp.rtcp_mode |
| 867 | : RtcpMode::kOff); |
| 868 | } |
| 869 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 870 | int RtpVideoStreamReceiver::GetUniqueFramesSeen() const { |
Danil Chapovalov | f7457e5 | 2019-09-20 17:57:15 +0200 | [diff] [blame] | 871 | return packet_buffer_.GetUniqueFramesSeen(); |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 872 | } |
| 873 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 874 | void RtpVideoStreamReceiver::StartReceive() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 875 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 876 | receiving_ = true; |
| 877 | } |
| 878 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 879 | void RtpVideoStreamReceiver::StopReceive() { |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 880 | RTC_DCHECK_RUN_ON(&worker_task_checker_); |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 881 | receiving_ = false; |
| 882 | } |
| 883 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 884 | void RtpVideoStreamReceiver::UpdateHistograms() { |
brandtr | d55c3f6 | 2016-10-31 04:51:33 -0700 | [diff] [blame] | 885 | FecPacketCounter counter = ulpfec_receiver_->GetPacketCounter(); |
asapersson | 0c43f77 | 2016-11-30 01:42:26 -0800 | [diff] [blame] | 886 | if (counter.first_packet_time_ms == -1) |
| 887 | return; |
| 888 | |
| 889 | int64_t elapsed_sec = |
| 890 | (clock_->TimeInMilliseconds() - counter.first_packet_time_ms) / 1000; |
| 891 | if (elapsed_sec < metrics::kMinRunTimeInSeconds) |
| 892 | return; |
| 893 | |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 894 | if (counter.num_packets > 0) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 895 | RTC_HISTOGRAM_PERCENTAGE( |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 896 | "WebRTC.Video.ReceivedFecPacketsInPercent", |
| 897 | static_cast<int>(counter.num_fec_packets * 100 / counter.num_packets)); |
| 898 | } |
| 899 | if (counter.num_fec_packets > 0) { |
asapersson | 1d02d3e | 2016-09-09 22:40:25 -0700 | [diff] [blame] | 900 | RTC_HISTOGRAM_PERCENTAGE("WebRTC.Video.RecoveredMediaPacketsInPercentOfFec", |
| 901 | static_cast<int>(counter.num_recovered_packets * |
| 902 | 100 / counter.num_fec_packets)); |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 903 | } |
Niels Möller | caef51e | 2019-08-27 09:19:49 +0200 | [diff] [blame] | 904 | if (config_.rtp.ulpfec_payload_type != -1) { |
| 905 | RTC_HISTOGRAM_COUNTS_10000( |
| 906 | "WebRTC.Video.FecBitrateReceivedInKbps", |
| 907 | static_cast<int>(counter.num_bytes * 8 / elapsed_sec / 1000)); |
| 908 | } |
mflodman | dc7d0d2 | 2016-05-06 05:32:22 -0700 | [diff] [blame] | 909 | } |
| 910 | |
nisse | b1f2ff9 | 2017-06-09 04:01:55 -0700 | [diff] [blame] | 911 | void RtpVideoStreamReceiver::InsertSpsPpsIntoTracker(uint8_t payload_type) { |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 912 | auto codec_params_it = pt_codec_params_.find(payload_type); |
| 913 | if (codec_params_it == pt_codec_params_.end()) |
| 914 | return; |
| 915 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 916 | RTC_LOG(LS_INFO) << "Found out of band supplied codec parameters for" |
| 917 | << " payload type: " << static_cast<int>(payload_type); |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 918 | |
| 919 | H264SpropParameterSets sprop_decoder; |
| 920 | auto sprop_base64_it = |
| 921 | codec_params_it->second.find(cricket::kH264FmtpSpropParameterSets); |
| 922 | |
| 923 | if (sprop_base64_it == codec_params_it->second.end()) |
| 924 | return; |
| 925 | |
johan | 62d02c3 | 2017-01-24 04:38:27 -0800 | [diff] [blame] | 926 | if (!sprop_decoder.DecodeSprop(sprop_base64_it->second.c_str())) |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 927 | return; |
| 928 | |
johan | d2b092f | 2017-01-24 02:38:17 -0800 | [diff] [blame] | 929 | tracker_.InsertSpsPpsNalus(sprop_decoder.sps_nalu(), |
| 930 | sprop_decoder.pps_nalu()); |
philipel | 022b54e | 2016-12-20 04:15:59 -0800 | [diff] [blame] | 931 | } |
| 932 | |
mflodman@webrtc.org | ad4ee36 | 2011-11-28 22:39:24 +0000 | [diff] [blame] | 933 | } // namespace webrtc |