From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- .../libwebrtc/video/video_stream_encoder.cc | 115 +++++++++------------ 1 file changed, 47 insertions(+), 68 deletions(-) (limited to 'third_party/libwebrtc/video/video_stream_encoder.cc') diff --git a/third_party/libwebrtc/video/video_stream_encoder.cc b/third_party/libwebrtc/video/video_stream_encoder.cc index c5f086e155..669f165635 100644 --- a/third_party/libwebrtc/video/video_stream_encoder.cc +++ b/third_party/libwebrtc/video/video_stream_encoder.cc @@ -94,6 +94,18 @@ int GetNumSpatialLayers(const VideoCodec& codec) { } } +absl::optional MaybeConvertDropReason( + VideoStreamEncoderObserver::DropReason reason) { + switch (reason) { + case VideoStreamEncoderObserver::DropReason::kMediaOptimization: + return EncodedImageCallback::DropReason::kDroppedByMediaOptimizations; + case VideoStreamEncoderObserver::DropReason::kEncoder: + return EncodedImageCallback::DropReason::kDroppedByEncoder; + default: + return absl::nullopt; + } +} + bool RequiresEncoderReset(const VideoCodec& prev_send_codec, const VideoCodec& new_send_codec, bool was_encode_called_since_last_initialization) { @@ -647,7 +659,6 @@ VideoStreamEncoder::VideoStreamEncoder( : field_trials_(field_trials), worker_queue_(TaskQueueBase::Current()), number_of_cores_(number_of_cores), - sink_(nullptr), settings_(settings), allocation_cb_type_(allocation_cb_type), rate_control_settings_( @@ -661,39 +672,12 @@ VideoStreamEncoder::VideoStreamEncoder( ? encoder_selector_from_constructor_ : encoder_selector_from_factory_.get()), encoder_stats_observer_(encoder_stats_observer), - cadence_callback_(*this), frame_cadence_adapter_(std::move(frame_cadence_adapter)), - encoder_initialized_(false), - max_framerate_(-1), - pending_encoder_reconfiguration_(false), - pending_encoder_creation_(false), - crop_width_(0), - crop_height_(0), - encoder_target_bitrate_bps_(absl::nullopt), - max_data_payload_length_(0), - encoder_paused_and_dropped_frame_(false), - was_encode_called_since_last_initialization_(false), - encoder_failed_(false), clock_(clock), - last_captured_timestamp_(0), delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - clock_->TimeInMilliseconds()), last_frame_log_ms_(clock_->TimeInMilliseconds()), - captured_frame_count_(0), - dropped_frame_cwnd_pushback_count_(0), - dropped_frame_encoder_block_count_(0), - pending_frame_post_time_us_(0), - accumulated_update_rect_{0, 0, 0, 0}, - accumulated_update_rect_is_valid_(true), - animation_start_time_(Timestamp::PlusInfinity()), - cap_resolution_due_to_video_content_(false), - expect_resize_state_(ExpectResizeState::kNoResize), - fec_controller_override_(nullptr), - force_disable_frame_dropper_(false), - pending_frame_drops_(0), - cwnd_frame_counter_(0), next_frame_types_(1, VideoFrameType::kVideoFrameDelta), - frame_encode_metadata_writer_(this), automatic_animation_detection_experiment_( ParseAutomatincAnimationDetectionFieldTrial()), input_state_provider_(encoder_stats_observer), @@ -704,7 +688,6 @@ VideoStreamEncoder::VideoStreamEncoder( degradation_preference_manager_( std::make_unique( video_stream_adapter_.get())), - adaptation_constraints_(), stream_resource_manager_(&input_state_provider_, encoder_stats_observer, clock_, @@ -1499,7 +1482,7 @@ void VideoStreamEncoder::OnEncoderSettingsChanged() { } void VideoStreamEncoder::OnFrame(Timestamp post_time, - int frames_scheduled_for_processing, + bool queue_overload, const VideoFrame& video_frame) { RTC_DCHECK_RUN_ON(&encoder_queue_); VideoFrame incoming_frame = video_frame; @@ -1538,11 +1521,8 @@ void VideoStreamEncoder::OnFrame(Timestamp post_time, << incoming_frame.ntp_time_ms() << " <= " << last_captured_timestamp_ << ") for incoming frame. Dropping."; - encoder_queue_.PostTask([this, incoming_frame]() { - RTC_DCHECK_RUN_ON(&encoder_queue_); - accumulated_update_rect_.Union(incoming_frame.update_rect()); - accumulated_update_rect_is_valid_ &= incoming_frame.has_update_rect(); - }); + ProcessDroppedFrame(incoming_frame, + VideoStreamEncoderObserver::DropReason::kBadTimestamp); return; } @@ -1561,25 +1541,24 @@ void VideoStreamEncoder::OnFrame(Timestamp post_time, bool cwnd_frame_drop = cwnd_frame_drop_interval_ && (cwnd_frame_counter_++ % cwnd_frame_drop_interval_.value() == 0); - if (frames_scheduled_for_processing == 1 && !cwnd_frame_drop) { + if (!queue_overload && !cwnd_frame_drop) { MaybeEncodeVideoFrame(incoming_frame, post_time.us()); } else { if (cwnd_frame_drop) { // Frame drop by congestion window pushback. Do not encode this // frame. ++dropped_frame_cwnd_pushback_count_; - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kCongestionWindow); } else { // There is a newer frame in flight. Do not encode this frame. RTC_LOG(LS_VERBOSE) << "Incoming frame dropped due to that the encoder is blocked."; ++dropped_frame_encoder_block_count_; - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kEncoderQueue); } - accumulated_update_rect_.Union(incoming_frame.update_rect()); - accumulated_update_rect_is_valid_ &= incoming_frame.has_update_rect(); + ProcessDroppedFrame( + incoming_frame, + cwnd_frame_drop + ? VideoStreamEncoderObserver::DropReason::kCongestionWindow + : VideoStreamEncoderObserver::DropReason::kEncoderQueue); } if (log_stats) { RTC_LOG(LS_INFO) << "Number of frames: captured " << captured_frame_count_ @@ -1816,10 +1795,8 @@ void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame, // Because pending frame will be dropped in any case, we need to // remember its updated region. if (pending_frame_) { - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kEncoderQueue); - accumulated_update_rect_.Union(pending_frame_->update_rect()); - accumulated_update_rect_is_valid_ &= pending_frame_->has_update_rect(); + ProcessDroppedFrame(*pending_frame_, + VideoStreamEncoderObserver::DropReason::kEncoderQueue); } if (DropDueToSize(video_frame.size())) { @@ -1833,10 +1810,8 @@ void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame, } else { // Ensure that any previously stored frame is dropped. pending_frame_.reset(); - accumulated_update_rect_.Union(video_frame.update_rect()); - accumulated_update_rect_is_valid_ &= video_frame.has_update_rect(); - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kEncoderQueue); + ProcessDroppedFrame( + video_frame, VideoStreamEncoderObserver::DropReason::kEncoderQueue); } return; } @@ -1854,10 +1829,8 @@ void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame, // Ensure that any previously stored frame is dropped. pending_frame_.reset(); TraceFrameDropStart(); - accumulated_update_rect_.Union(video_frame.update_rect()); - accumulated_update_rect_is_valid_ &= video_frame.has_update_rect(); - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kEncoderQueue); + ProcessDroppedFrame( + video_frame, VideoStreamEncoderObserver::DropReason::kEncoderQueue); } return; } @@ -1879,10 +1852,9 @@ void VideoStreamEncoder::MaybeEncodeVideoFrame(const VideoFrame& video_frame, ? last_encoder_rate_settings_->encoder_target.bps() : 0) << ", input frame rate " << framerate_fps; - OnDroppedFrame( - EncodedImageCallback::DropReason::kDroppedByMediaOptimizations); - accumulated_update_rect_.Union(video_frame.update_rect()); - accumulated_update_rect_is_valid_ &= video_frame.has_update_rect(); + ProcessDroppedFrame( + video_frame, + VideoStreamEncoderObserver::DropReason::kMediaOptimization); return; } @@ -2228,16 +2200,6 @@ EncodedImageCallback::Result VideoStreamEncoder::OnEncodedImage( } void VideoStreamEncoder::OnDroppedFrame(DropReason reason) { - switch (reason) { - case DropReason::kDroppedByMediaOptimizations: - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kMediaOptimization); - break; - case DropReason::kDroppedByEncoder: - encoder_stats_observer_->OnFrameDropped( - VideoStreamEncoderObserver::DropReason::kEncoder); - break; - } sink_->OnDroppedFrame(reason); encoder_queue_.PostTask([this, reason] { RTC_DCHECK_RUN_ON(&encoder_queue_); @@ -2396,6 +2358,11 @@ void VideoStreamEncoder::OnVideoSourceRestrictionsUpdated( << (reason ? reason->Name() : std::string("")) << " to " << restrictions.ToString(); + if (frame_cadence_adapter_) { + frame_cadence_adapter_->UpdateVideoSourceRestrictions( + restrictions.max_frame_rate()); + } + // TODO(webrtc:14451) Split video_source_sink_controller_ // so that ownership on restrictions/wants is kept on &encoder_queue_ latest_restrictions_ = restrictions; @@ -2620,4 +2587,16 @@ void VideoStreamEncoder::RemoveRestrictionsListenerForTesting( event.Wait(rtc::Event::kForever); } +// RTC_RUN_ON(&encoder_queue_) +void VideoStreamEncoder::ProcessDroppedFrame( + const VideoFrame& frame, + VideoStreamEncoderObserver::DropReason reason) { + accumulated_update_rect_.Union(frame.update_rect()); + accumulated_update_rect_is_valid_ &= frame.has_update_rect(); + if (auto converted_reason = MaybeConvertDropReason(reason)) { + OnDroppedFrame(*converted_reason); + } + encoder_stats_observer_->OnFrameDropped(reason); +} + } // namespace webrtc -- cgit v1.2.3