From f0d1d341a8581dab03d38952dc1eb937cf418d41 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Sun, 15 Mar 2026 02:42:03 +0800 Subject: [PATCH] Bound MPP encoder packet wait to reduce publish stalls --- .../sample_person_shoe_two_stage_balanced.json | 2 ++ configs/sample_rtsp_preprocess_publish.json | 2 ++ configs/sample_rtsp_publish_only.json | 2 ++ src/hw_codec.cpp | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/configs/sample_person_shoe_two_stage_balanced.json b/configs/sample_person_shoe_two_stage_balanced.json index a8cc3ce..78e843c 100644 --- a/configs/sample_person_shoe_two_stage_balanced.json +++ b/configs/sample_person_shoe_two_stage_balanced.json @@ -147,6 +147,8 @@ "codec": "h264", "fps": 30, "bitrate_kbps": 2000, + "mpp_output_timeout_ms": 50, + "mpp_packet_wait_ms": 10, "use_mpp": true, "outputs": [ {"proto": "rtsp_server", "port": 8555, "path": "/live/cam1"} diff --git a/configs/sample_rtsp_preprocess_publish.json b/configs/sample_rtsp_preprocess_publish.json index 16f52a6..e0abec0 100644 --- a/configs/sample_rtsp_preprocess_publish.json +++ b/configs/sample_rtsp_preprocess_publish.json @@ -59,6 +59,8 @@ "codec": "h264", "fps": 30, "bitrate_kbps": 2000, + "mpp_output_timeout_ms": 50, + "mpp_packet_wait_ms": 10, "use_mpp": true, "outputs": [ {"proto": "rtsp_server", "port": 8555, "path": "/live/cam1"} diff --git a/configs/sample_rtsp_publish_only.json b/configs/sample_rtsp_publish_only.json index 7f7b3e8..223c542 100644 --- a/configs/sample_rtsp_publish_only.json +++ b/configs/sample_rtsp_publish_only.json @@ -32,6 +32,8 @@ "codec": "h264", "fps": 30, "bitrate_kbps": 2000, + "mpp_output_timeout_ms": 50, + "mpp_packet_wait_ms": 10, "use_mpp": true, "outputs": [ {"proto": "rtsp_server", "port": 8555, "path": "/live/cam1"} diff --git a/src/hw_codec.cpp b/src/hw_codec.cpp index 4b85017..9cb7d3d 100644 --- a/src/hw_codec.cpp +++ b/src/hw_codec.cpp @@ -1,6 +1,7 @@ #include "hw/rk3588_defaults.h" #include +#include #include #include #include @@ -366,6 +367,8 @@ public: bitrate_bps_ = config.ValueOr("bitrate_kbps", 4000) * 1000; codec_ = ToLower(config.ValueOr("codec", "h264")); std::string fmt = ToLower(config.ValueOr("pixel_format", "nv12")); + output_timeout_ms_ = std::max(1, config.ValueOr("mpp_output_timeout_ms", 50)); + packet_wait_budget_ms_ = std::max(0, config.ValueOr("mpp_packet_wait_ms", 10)); if (width_ <= 0 || height_ <= 0) return FailStatus("invalid size"); if (fmt == "nv12") { @@ -415,7 +418,7 @@ public: mpi_->control(ctx_, MPP_ENC_SET_HEADER_MODE, &header_mode); } - RK_S32 timeout = 2000; + RK_S32 timeout = static_cast(output_timeout_ms_); mpi_->control(ctx_, MPP_SET_OUTPUT_TIMEOUT, &timeout); MppPacket hdr = nullptr; @@ -480,10 +483,19 @@ public: } mpp_frame_deinit(&mpp_frame); + const auto wait_begin = std::chrono::steady_clock::now(); while (true) { MppPacket packet = nullptr; MPP_RET ret = mpi_->encode_get_packet(ctx_, &packet); if (ret == MPP_ERR_TIMEOUT) { + if (packet_wait_budget_ms_ <= 0) { + break; + } + const auto waited_ms = std::chrono::duration_cast( + std::chrono::steady_clock::now() - wait_begin).count(); + if (waited_ms >= packet_wait_budget_ms_) { + break; + } usleep(2000); continue; } @@ -617,6 +629,8 @@ private: int gop_ = 50; int bitrate_bps_ = 4000000; std::string codec_; + int output_timeout_ms_ = 50; + int packet_wait_budget_ms_ = 10; std::vector header_; bool initialized_ = false; bool header_sent_ = false;