Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 The WebRTC project authors. All Rights Reserved. |
| 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 | |
| 11 | #include "video/video_stream_buffer_controller.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "absl/base/attributes.h" |
| 18 | #include "absl/functional/bind_front.h" |
| 19 | #include "api/sequence_checker.h" |
| 20 | #include "api/task_queue/task_queue_base.h" |
| 21 | #include "api/units/data_size.h" |
| 22 | #include "api/video/encoded_frame.h" |
| 23 | #include "api/video/frame_buffer.h" |
| 24 | #include "api/video/video_content_type.h" |
| 25 | #include "modules/video_coding/frame_helpers.h" |
| 26 | #include "modules/video_coding/timing/inter_frame_delay.h" |
| 27 | #include "modules/video_coding/timing/jitter_estimator.h" |
| 28 | #include "rtc_base/checks.h" |
| 29 | #include "rtc_base/logging.h" |
| 30 | #include "rtc_base/thread_annotations.h" |
Evan Shrubsole | a006ba1 | 2022-09-05 10:09:08 +0000 | [diff] [blame] | 31 | #include "video/frame_decode_scheduler.h" |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 32 | #include "video/frame_decode_timing.h" |
| 33 | #include "video/task_queue_frame_decode_scheduler.h" |
| 34 | #include "video/video_receive_stream_timeout_tracker.h" |
| 35 | |
| 36 | namespace webrtc { |
| 37 | |
| 38 | namespace { |
| 39 | |
| 40 | // Max number of frames the buffer will hold. |
| 41 | static constexpr size_t kMaxFramesBuffered = 800; |
| 42 | // Max number of decoded frame info that will be saved. |
| 43 | static constexpr int kMaxFramesHistory = 1 << 13; |
| 44 | |
| 45 | // Default value for the maximum decode queue size that is used when the |
| 46 | // low-latency renderer is used. |
| 47 | static constexpr size_t kZeroPlayoutDelayDefaultMaxDecodeQueueSize = 8; |
| 48 | |
| 49 | struct FrameMetadata { |
| 50 | explicit FrameMetadata(const EncodedFrame& frame) |
| 51 | : is_last_spatial_layer(frame.is_last_spatial_layer), |
| 52 | is_keyframe(frame.is_keyframe()), |
| 53 | size(frame.size()), |
| 54 | contentType(frame.contentType()), |
| 55 | delayed_by_retransmission(frame.delayed_by_retransmission()), |
| 56 | rtp_timestamp(frame.Timestamp()), |
| 57 | receive_time(frame.ReceivedTimestamp()) {} |
| 58 | |
| 59 | const bool is_last_spatial_layer; |
| 60 | const bool is_keyframe; |
| 61 | const size_t size; |
| 62 | const VideoContentType contentType; |
| 63 | const bool delayed_by_retransmission; |
| 64 | const uint32_t rtp_timestamp; |
| 65 | const absl::optional<Timestamp> receive_time; |
| 66 | }; |
| 67 | |
| 68 | Timestamp ReceiveTime(const EncodedFrame& frame) { |
| 69 | absl::optional<Timestamp> ts = frame.ReceivedTimestamp(); |
| 70 | RTC_DCHECK(ts.has_value()) << "Received frame must have a timestamp set!"; |
| 71 | return *ts; |
| 72 | } |
| 73 | |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 74 | } // namespace |
| 75 | |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 76 | VideoStreamBufferController::VideoStreamBufferController( |
| 77 | Clock* clock, |
| 78 | TaskQueueBase* worker_queue, |
| 79 | VCMTiming* timing, |
| 80 | VCMReceiveStatisticsCallback* stats_proxy, |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 81 | FrameSchedulingReceiver* receiver, |
| 82 | TimeDelta max_wait_for_keyframe, |
| 83 | TimeDelta max_wait_for_frame, |
| 84 | std::unique_ptr<FrameDecodeScheduler> frame_decode_scheduler, |
| 85 | const FieldTrialsView& field_trials) |
| 86 | : field_trials_(field_trials), |
| 87 | clock_(clock), |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 88 | stats_proxy_(stats_proxy), |
| 89 | receiver_(receiver), |
| 90 | timing_(timing), |
| 91 | frame_decode_scheduler_(std::move(frame_decode_scheduler)), |
| 92 | jitter_estimator_(clock_, field_trials), |
| 93 | buffer_(std::make_unique<FrameBuffer>(kMaxFramesBuffered, |
| 94 | kMaxFramesHistory, |
| 95 | field_trials)), |
| 96 | decode_timing_(clock_, timing_), |
| 97 | timeout_tracker_( |
| 98 | clock_, |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 99 | worker_queue, |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 100 | VideoReceiveStreamTimeoutTracker::Timeouts{ |
| 101 | .max_wait_for_keyframe = max_wait_for_keyframe, |
| 102 | .max_wait_for_frame = max_wait_for_frame}, |
| 103 | absl::bind_front(&VideoStreamBufferController::OnTimeout, this)), |
| 104 | zero_playout_delay_max_decode_queue_size_( |
| 105 | "max_decode_queue_size", |
| 106 | kZeroPlayoutDelayDefaultMaxDecodeQueueSize) { |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 107 | RTC_DCHECK(stats_proxy_); |
| 108 | RTC_DCHECK(receiver_); |
| 109 | RTC_DCHECK(timing_); |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 110 | RTC_DCHECK(clock_); |
| 111 | RTC_DCHECK(frame_decode_scheduler_); |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 112 | |
| 113 | ParseFieldTrial({&zero_playout_delay_max_decode_queue_size_}, |
| 114 | field_trials.Lookup("WebRTC-ZeroPlayoutDelay")); |
| 115 | } |
| 116 | |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 117 | void VideoStreamBufferController::Stop() { |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 118 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 119 | frame_decode_scheduler_->Stop(); |
| 120 | timeout_tracker_.Stop(); |
| 121 | decoder_ready_for_new_frame_ = false; |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void VideoStreamBufferController::SetProtectionMode( |
| 125 | VCMVideoProtection protection_mode) { |
| 126 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 127 | protection_mode_ = protection_mode; |
| 128 | } |
| 129 | |
| 130 | void VideoStreamBufferController::Clear() { |
| 131 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 132 | stats_proxy_->OnDroppedFrames(buffer_->CurrentSize()); |
| 133 | buffer_ = std::make_unique<FrameBuffer>(kMaxFramesBuffered, kMaxFramesHistory, |
| 134 | field_trials_); |
| 135 | frame_decode_scheduler_->CancelOutstanding(); |
| 136 | } |
| 137 | |
| 138 | absl::optional<int64_t> VideoStreamBufferController::InsertFrame( |
| 139 | std::unique_ptr<EncodedFrame> frame) { |
| 140 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 141 | FrameMetadata metadata(*frame); |
| 142 | int complete_units = buffer_->GetTotalNumberOfContinuousTemporalUnits(); |
| 143 | if (buffer_->InsertFrame(std::move(frame))) { |
| 144 | RTC_DCHECK(metadata.receive_time) << "Frame receive time must be set!"; |
philipel | 7446b60 | 2022-10-06 15:49:17 +0200 | [diff] [blame] | 145 | if (!metadata.delayed_by_retransmission && metadata.receive_time && |
| 146 | (field_trials_.IsDisabled("WebRTC-IncomingTimestampOnMarkerBitOnly") || |
| 147 | metadata.is_last_spatial_layer)) { |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 148 | timing_->IncomingTimestamp(metadata.rtp_timestamp, |
| 149 | *metadata.receive_time); |
philipel | 7446b60 | 2022-10-06 15:49:17 +0200 | [diff] [blame] | 150 | } |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 151 | if (complete_units < buffer_->GetTotalNumberOfContinuousTemporalUnits()) { |
| 152 | stats_proxy_->OnCompleteFrame(metadata.is_keyframe, metadata.size, |
| 153 | metadata.contentType); |
| 154 | MaybeScheduleFrameForRelease(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return buffer_->LastContinuousFrameId(); |
| 159 | } |
| 160 | |
| 161 | void VideoStreamBufferController::UpdateRtt(int64_t max_rtt_ms) { |
| 162 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 163 | jitter_estimator_.UpdateRtt(TimeDelta::Millis(max_rtt_ms)); |
| 164 | } |
| 165 | |
| 166 | void VideoStreamBufferController::SetMaxWaits(TimeDelta max_wait_for_keyframe, |
| 167 | TimeDelta max_wait_for_frame) { |
| 168 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 169 | timeout_tracker_.SetTimeouts({.max_wait_for_keyframe = max_wait_for_keyframe, |
| 170 | .max_wait_for_frame = max_wait_for_frame}); |
| 171 | } |
| 172 | |
| 173 | void VideoStreamBufferController::StartNextDecode(bool keyframe_required) { |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 174 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 175 | if (!timeout_tracker_.Running()) |
| 176 | timeout_tracker_.Start(keyframe_required); |
| 177 | keyframe_required_ = keyframe_required; |
| 178 | if (keyframe_required_) { |
| 179 | timeout_tracker_.SetWaitingForKeyframe(); |
| 180 | } |
| 181 | decoder_ready_for_new_frame_ = true; |
| 182 | MaybeScheduleFrameForRelease(); |
| 183 | } |
| 184 | |
| 185 | int VideoStreamBufferController::Size() { |
| 186 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 187 | return buffer_->CurrentSize(); |
| 188 | } |
| 189 | |
| 190 | void VideoStreamBufferController::OnFrameReady( |
| 191 | absl::InlinedVector<std::unique_ptr<EncodedFrame>, 4> frames, |
| 192 | Timestamp render_time) { |
| 193 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 194 | RTC_DCHECK(!frames.empty()); |
| 195 | |
| 196 | timeout_tracker_.OnEncodedFrameReleased(); |
| 197 | |
| 198 | Timestamp now = clock_->CurrentTime(); |
| 199 | bool superframe_delayed_by_retransmission = false; |
| 200 | DataSize superframe_size = DataSize::Zero(); |
| 201 | const EncodedFrame& first_frame = *frames.front(); |
| 202 | Timestamp receive_time = ReceiveTime(first_frame); |
| 203 | |
| 204 | if (first_frame.is_keyframe()) |
| 205 | keyframe_required_ = false; |
| 206 | |
| 207 | // Gracefully handle bad RTP timestamps and render time issues. |
Rasmus Brandt | fb3bd4a | 2022-10-13 13:43:27 +0200 | [diff] [blame] | 208 | if (FrameHasBadRenderTiming(render_time, now) || |
| 209 | TargetVideoDelayIsTooLarge(timing_->TargetVideoDelay())) { |
| 210 | RTC_LOG(LS_WARNING) << "Resetting jitter estimator and timing module due " |
| 211 | "to bad render timing for rtp_timestamp=" |
| 212 | << first_frame.Timestamp(); |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 213 | jitter_estimator_.Reset(); |
| 214 | timing_->Reset(); |
| 215 | render_time = timing_->RenderTime(first_frame.Timestamp(), now); |
| 216 | } |
| 217 | |
| 218 | for (std::unique_ptr<EncodedFrame>& frame : frames) { |
| 219 | frame->SetRenderTime(render_time.ms()); |
| 220 | |
| 221 | superframe_delayed_by_retransmission |= frame->delayed_by_retransmission(); |
| 222 | receive_time = std::max(receive_time, ReceiveTime(*frame)); |
| 223 | superframe_size += DataSize::Bytes(frame->size()); |
| 224 | } |
| 225 | |
| 226 | if (!superframe_delayed_by_retransmission) { |
| 227 | auto frame_delay = inter_frame_delay_.CalculateDelay( |
| 228 | first_frame.Timestamp(), receive_time); |
| 229 | if (frame_delay) { |
| 230 | jitter_estimator_.UpdateEstimate(*frame_delay, superframe_size); |
| 231 | } |
| 232 | |
| 233 | float rtt_mult = protection_mode_ == kProtectionNackFEC ? 0.0 : 1.0; |
| 234 | absl::optional<TimeDelta> rtt_mult_add_cap_ms = absl::nullopt; |
| 235 | if (rtt_mult_settings_.has_value()) { |
| 236 | rtt_mult = rtt_mult_settings_->rtt_mult_setting; |
| 237 | rtt_mult_add_cap_ms = |
| 238 | TimeDelta::Millis(rtt_mult_settings_->rtt_mult_add_cap_ms); |
| 239 | } |
| 240 | timing_->SetJitterDelay( |
| 241 | jitter_estimator_.GetJitterEstimate(rtt_mult, rtt_mult_add_cap_ms)); |
| 242 | timing_->UpdateCurrentDelay(render_time, now); |
| 243 | } else if (RttMultExperiment::RttMultEnabled()) { |
| 244 | jitter_estimator_.FrameNacked(); |
| 245 | } |
| 246 | |
| 247 | // Update stats. |
| 248 | UpdateDroppedFrames(); |
| 249 | UpdateJitterDelay(); |
| 250 | UpdateTimingFrameInfo(); |
| 251 | |
| 252 | std::unique_ptr<EncodedFrame> frame = |
| 253 | CombineAndDeleteFrames(std::move(frames)); |
| 254 | |
| 255 | timing_->SetLastDecodeScheduledTimestamp(now); |
| 256 | |
| 257 | decoder_ready_for_new_frame_ = false; |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 258 | receiver_->OnEncodedFrame(std::move(frame)); |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void VideoStreamBufferController::OnTimeout(TimeDelta delay) { |
| 262 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 263 | |
| 264 | // Stop sending timeouts until receiver starts waiting for a new frame. |
| 265 | timeout_tracker_.Stop(); |
| 266 | |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 267 | // If the stream is paused then ignore the timeout. |
| 268 | if (!decoder_ready_for_new_frame_) { |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 271 | decoder_ready_for_new_frame_ = false; |
Evan Shrubsole | 214cab5 | 2022-08-16 09:48:23 +0000 | [diff] [blame] | 272 | receiver_->OnDecodableFrameTimeout(delay); |
Evan Shrubsole | 476f18d | 2022-08-15 15:21:16 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void VideoStreamBufferController::FrameReadyForDecode(uint32_t rtp_timestamp, |
| 276 | Timestamp render_time) { |
| 277 | RTC_DCHECK_RUN_ON(&worker_sequence_checker_); |
| 278 | auto frames = buffer_->ExtractNextDecodableTemporalUnit(); |
| 279 | RTC_DCHECK(frames[0]->Timestamp() == rtp_timestamp) |
| 280 | << "Frame buffer's next decodable frame was not the one sent for " |
| 281 | "extraction rtp=" |
| 282 | << rtp_timestamp << " extracted rtp=" << frames[0]->Timestamp(); |
| 283 | OnFrameReady(std::move(frames), render_time); |
| 284 | } |
| 285 | |
| 286 | void VideoStreamBufferController::UpdateDroppedFrames() |
| 287 | RTC_RUN_ON(&worker_sequence_checker_) { |
| 288 | const int dropped_frames = buffer_->GetTotalNumberOfDroppedFrames() - |
| 289 | frames_dropped_before_last_new_frame_; |
| 290 | if (dropped_frames > 0) |
| 291 | stats_proxy_->OnDroppedFrames(dropped_frames); |
| 292 | frames_dropped_before_last_new_frame_ = |
| 293 | buffer_->GetTotalNumberOfDroppedFrames(); |
| 294 | } |
| 295 | |
| 296 | void VideoStreamBufferController::UpdateJitterDelay() { |
| 297 | auto timings = timing_->GetTimings(); |
| 298 | if (timings.num_decoded_frames) { |
| 299 | stats_proxy_->OnFrameBufferTimingsUpdated( |
| 300 | timings.max_decode_duration.ms(), timings.current_delay.ms(), |
| 301 | timings.target_delay.ms(), timings.jitter_buffer_delay.ms(), |
| 302 | timings.min_playout_delay.ms(), timings.render_delay.ms()); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void VideoStreamBufferController::UpdateTimingFrameInfo() { |
| 307 | absl::optional<TimingFrameInfo> info = timing_->GetTimingFrameInfo(); |
| 308 | if (info) |
| 309 | stats_proxy_->OnTimingFrameInfoUpdated(*info); |
| 310 | } |
| 311 | |
| 312 | bool VideoStreamBufferController::IsTooManyFramesQueued() const |
| 313 | RTC_RUN_ON(&worker_sequence_checker_) { |
| 314 | return buffer_->CurrentSize() > zero_playout_delay_max_decode_queue_size_; |
| 315 | } |
| 316 | |
| 317 | void VideoStreamBufferController::ForceKeyFrameReleaseImmediately() |
| 318 | RTC_RUN_ON(&worker_sequence_checker_) { |
| 319 | RTC_DCHECK(keyframe_required_); |
| 320 | // Iterate through the frame buffer until there is a complete keyframe and |
| 321 | // release this right away. |
| 322 | while (buffer_->DecodableTemporalUnitsInfo()) { |
| 323 | auto next_frame = buffer_->ExtractNextDecodableTemporalUnit(); |
| 324 | if (next_frame.empty()) { |
| 325 | RTC_DCHECK_NOTREACHED() |
| 326 | << "Frame buffer should always return at least 1 frame."; |
| 327 | continue; |
| 328 | } |
| 329 | // Found keyframe - decode right away. |
| 330 | if (next_frame.front()->is_keyframe()) { |
| 331 | auto render_time = timing_->RenderTime(next_frame.front()->Timestamp(), |
| 332 | clock_->CurrentTime()); |
| 333 | OnFrameReady(std::move(next_frame), render_time); |
| 334 | return; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void VideoStreamBufferController::MaybeScheduleFrameForRelease() |
| 340 | RTC_RUN_ON(&worker_sequence_checker_) { |
| 341 | auto decodable_tu_info = buffer_->DecodableTemporalUnitsInfo(); |
| 342 | if (!decoder_ready_for_new_frame_ || !decodable_tu_info) { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | if (keyframe_required_) { |
| 347 | return ForceKeyFrameReleaseImmediately(); |
| 348 | } |
| 349 | |
| 350 | // If already scheduled then abort. |
| 351 | if (frame_decode_scheduler_->ScheduledRtpTimestamp() == |
| 352 | decodable_tu_info->next_rtp_timestamp) { |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | TimeDelta max_wait = timeout_tracker_.TimeUntilTimeout(); |
| 357 | // Ensures the frame is scheduled for decode before the stream times out. |
| 358 | // This is otherwise a race condition. |
| 359 | max_wait = std::max(max_wait - TimeDelta::Millis(1), TimeDelta::Zero()); |
| 360 | absl::optional<FrameDecodeTiming::FrameSchedule> schedule; |
| 361 | while (decodable_tu_info) { |
| 362 | schedule = decode_timing_.OnFrameBufferUpdated( |
| 363 | decodable_tu_info->next_rtp_timestamp, |
| 364 | decodable_tu_info->last_rtp_timestamp, max_wait, |
| 365 | IsTooManyFramesQueued()); |
| 366 | if (schedule) { |
| 367 | // Don't schedule if already waiting for the same frame. |
| 368 | if (frame_decode_scheduler_->ScheduledRtpTimestamp() != |
| 369 | decodable_tu_info->next_rtp_timestamp) { |
| 370 | frame_decode_scheduler_->CancelOutstanding(); |
| 371 | frame_decode_scheduler_->ScheduleFrame( |
| 372 | decodable_tu_info->next_rtp_timestamp, *schedule, |
| 373 | absl::bind_front(&VideoStreamBufferController::FrameReadyForDecode, |
| 374 | this)); |
| 375 | } |
| 376 | return; |
| 377 | } |
| 378 | // If no schedule for current rtp, drop and try again. |
| 379 | buffer_->DropNextDecodableTemporalUnit(); |
| 380 | decodable_tu_info = buffer_->DecodableTemporalUnitsInfo(); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | } // namespace webrtc |