Skip to content

Commit 06a2b13

Browse files
committed
fix: 动态码率更新也计入FEC开销
1 parent f3976e9 commit 06a2b13

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/video.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,21 @@ namespace video {
385385
void
386386
set_bitrate(int bitrate_kbps) override {
387387
if (avcodec_ctx) {
388-
auto bitrate = bitrate_kbps * 1000; // 转换为bps
388+
// 考虑FEC影响,调整编码码率
389+
// 当FEC百分比为X%时,实际编码码率需要调整为原始码率的(100-X)%
390+
auto adjusted_bitrate_kbps = bitrate_kbps;
391+
if (config::stream.fec_percentage <= 80) {
392+
adjusted_bitrate_kbps = (int)(bitrate_kbps * (100 - config::stream.fec_percentage) / 100.0f);
393+
}
394+
395+
auto bitrate = adjusted_bitrate_kbps * 1000; // 转换为bps
389396
avcodec_ctx->rc_max_rate = bitrate;
390397
avcodec_ctx->bit_rate = bitrate;
391398
avcodec_ctx->rc_min_rate = bitrate; // Set min rate for CBR mode
392399

393-
BOOST_LOG(info) << "AVCodec encoder bitrate changed to: " << bitrate_kbps << " Kbps";
400+
BOOST_LOG(info) << "AVCodec encoder bitrate changed to: " << adjusted_bitrate_kbps
401+
<< " Kbps (requested: " << bitrate_kbps << " Kbps, FEC: "
402+
<< config::stream.fec_percentage << "%)";
394403
}
395404
}
396405

@@ -440,8 +449,17 @@ namespace video {
440449
void
441450
set_bitrate(int bitrate_kbps) override {
442451
if (device && device->nvenc) {
443-
device->nvenc->set_bitrate(bitrate_kbps);
444-
BOOST_LOG(info) << "NVENC encoder bitrate changed to: " << bitrate_kbps << " Kbps";
452+
// 考虑FEC影响,调整编码码率
453+
// 当FEC百分比为X%时,实际编码码率需要调整为原始码率的(100-X)%
454+
auto adjusted_bitrate_kbps = bitrate_kbps;
455+
if (config::stream.fec_percentage <= 80) {
456+
adjusted_bitrate_kbps = (int)(bitrate_kbps * (100 - config::stream.fec_percentage) / 100.0f);
457+
}
458+
459+
device->nvenc->set_bitrate(adjusted_bitrate_kbps);
460+
BOOST_LOG(info) << "NVENC encoder bitrate changed to: " << adjusted_bitrate_kbps
461+
<< " Kbps (requested: " << bitrate_kbps << " Kbps, FEC: "
462+
<< config::stream.fec_percentage << "%)";
445463
}
446464
}
447465

0 commit comments

Comments
 (0)