Bound MPP encoder packet wait to reduce publish stalls

This commit is contained in:
tian 2026-03-15 02:42:03 +08:00
parent bd02e17837
commit f0d1d341a8
4 changed files with 21 additions and 1 deletions

View File

@ -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"}

View File

@ -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"}

View File

@ -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"}

View File

@ -1,6 +1,7 @@
#include "hw/rk3588_defaults.h"
#include <algorithm>
#include <chrono>
#include <cctype>
#include <cstring>
#include <deque>
@ -366,6 +367,8 @@ public:
bitrate_bps_ = config.ValueOr<int>("bitrate_kbps", 4000) * 1000;
codec_ = ToLower(config.ValueOr<std::string>("codec", "h264"));
std::string fmt = ToLower(config.ValueOr<std::string>("pixel_format", "nv12"));
output_timeout_ms_ = std::max(1, config.ValueOr<int>("mpp_output_timeout_ms", 50));
packet_wait_budget_ms_ = std::max(0, config.ValueOr<int>("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<RK_S32>(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::milliseconds>(
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<uint8_t> header_;
bool initialized_ = false;
bool header_sent_ = false;