Skip to content

fix(video): track latency timestamps by encoded frame#705

Open
qiin2333 wants to merge 1 commit into
masterfrom
codex/accurate-host-latency
Open

fix(video): track latency timestamps by encoded frame#705
qiin2333 wants to merge 1 commit into
masterfrom
codex/accurate-host-latency

Conversation

@qiin2333
Copy link
Copy Markdown
Collaborator

@qiin2333 qiin2333 commented Jun 3, 2026

改了啥呀,杂鱼

  • 新增一个固定 256 槽的 frame timestamp ring,用提交帧号保存 capture timestamp。
  • AVCodec 按返回 packet 的 PTS 回查 timestamp。
  • NVENC/AMF 按编码器实际返回的 encoded_frame.frame_index 回查 timestamp。
  • AMF pipeline lag / 分片输出时,不再把“当前提交帧”的 timestamp 误绑到“上一帧输出”上。

为啥这样改

之前 host processing latency 的 packet timestamp 直接来自当前 encode 调用的 frame_timestamp。当编码器输出落后于提交帧时,尤其 AMF 每帧分片/流水线输出场景,会低估完整帧从 capture 到 packet 出队的时间。

这个实现只做 O(1) 数组读写,没有 map、堆分配、新计时点或额外日志,默认路径上的性能开销保持很低。

验证

  • git diff --check -- src/video.cpp
  • 本机 ninja -C build CMakeFiles/sunshine.dir/src/video.cpp.obj 被 MSYS GCC 前端崩溃阻塞:cc1plus.exe --version 直接退出 -1073741511,最小 C++ 文件编译也同样静默失败,无法作为该 diff 的编译诊断。

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4efb0808-a630-4e4e-95e7-7bb8592b88e8

📥 Commits

Reviewing files that changed from the base of the PR and between c71a4a3 and 3197214.

📒 Files selected for processing (1)
  • src/video.cpp
📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (1)
src/**/*.{cpp,c,h}

⚙️ CodeRabbit configuration file

src/**/*.{cpp,c,h}: Sunshine 核心 C++ 源码,自托管游戏串流服务器。审查要点:内存安全、 线程安全、RAII 资源管理、安全漏洞。注意预处理宏控制的平台相关代码。

Files:

  • src/video.cpp
🔇 Additional comments (7)
src/video.cpp (7)

7-13: LGTM!


354-380: LGTM!


407-407: LGTM!


1851-1852: LGTM!

Also applies to: 1934-1936


638-650: LGTM!

Also applies to: 1948-1949, 1970-1970


722-734: LGTM!

Also applies to: 1978-1979, 2010-2010


521-521: LGTM!


Summary by CodeRabbit

Bug Fixes

  • 修复视频编码过程中的时间戳错配问题,确保编码器输出的视频帧时间戳准确性和同步一致性。

Walkthrough

采用固定大小环形缓冲区(256 槽)存储帧号与采集时间戳的映射,在 AVCodec、NVENC、AMF 三种编码器会话中集成该缓冲,于帧提交时记录时间戳、输出时根据编码器返回的帧索引回查对应时间戳,替代之前的直接时间戳传递或基于 pts 匹配的逻辑。

Changes

编码器时间戳追踪与环形缓冲集成

Layer / File(s) Summary
Ring buffer data structure and dependencies
src/video.cpp
定义 frame_timestamp_ring_t 结构,以 256 槽数组存储帧号与可选时间戳,并提供 store()lookup() 方法处理模运算包裹。新增相关 C++ 标准库依赖头文件。
AVCodec session timestamp tracking and lookup
src/video.cpp
avcodec_encode_session_t 中新增 frame_timestamps 成员,encode_avcodec() 在帧提交时记录时间戳,输出包生成时按 pts 回查对应时间戳。同时处理移动赋值时的缓冲转移。
NVENC session with track and resolve methods
src/video.cpp
nvenc_encode_session_t 中新增 track_frame_timestamp()resolve_frame_timestamp() 包装方法及 frame_timestamps 成员,encode_nvenc() 帧提交时调用 track_frame_timestamp(),输出时按编码器返回的帧号回查时间戳。
AMF session with track and resolve methods
src/video.cpp
amf_encode_session_t 中新增与 NVENC 同构的 track_frame_timestamp()resolve_frame_timestamp() 方法及 frame_timestamps 成员,encode_amf() 帧提交时记录,输出时按帧索引回查。

🎯 4 (Complex) | ⏱️ ~45 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed PR标题"fix(video): track latency timestamps by encoded frame"清晰、准确地概括了主要改动——通过编码帧跟踪延迟时间戳,与文件改动内容高度相关。
Description check ✅ Passed PR描述详细说明了改动内容(256槽frame timestamp ring、AVCodec/NVENC/AMF回查逻辑)、改动原因(解决编码器输出滞后导致的时间戳错配)及性能考量,与变更集紧密相关。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/accurate-host-latency

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
src/video.cpp

src/video.cpp:16:10: fatal error: 'boost/pointer_cast.hpp' file not found
16 | #include <boost/pointer_cast.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
src/video.cpp:100:7-14: ERROR translating statement 'ReturnStmt'
Aborting translation of method 'video::anonymous_namespace_src_video.cpp::allow_encoder_probing' in file 'src/video.cpp': "Assert_failure src/clang/cAst_utils.ml:249:53"
Uncaught Internal Error: "Assert_failure src/clang/cAst_utils.ml:249:53"
Error backtrace:
Raised at ClangFrontend__CAst_utils.get_decl_from_typ_ptr in file "src/clang/cAst_utils.ml", line 249, characters 53-65
Called from ClangFrontend__CTrans.CTrans_funct.get_destructor_decl_ref in file "src/clang/cTrans.ml", line 658, characters 12-59
Called from ClangFrontend__CTrans.CTrans_funct.destructor_calls.(fun) in file "src/clang/cTrans.ml", line 2048, characters 12-69
Called from Base__List.rev_filter_map.loop in file "src/list.ml", line 944, characters 13-17
Called from Base__List.filt

... [truncated 2200 characters] ...

clang/cTrans.ml", line 4784, characters 10-1023
Called from ClangFrontend__CTrans.CTrans_funct.instruction in file "src/clang/cTrans.ml" (inlined), line 4765, characters 38-71
Called from ClangFrontend__CTrans.CTrans_funct.exec_with_node_creation in file "src/clang/cTrans.ml" (inlined), line 104, characters 20-38
Called from ClangFrontend__CTrans.CTrans_funct.get_clang_stmt_trans in file "src/clang/cTrans.ml" (inlined), line 5395, characters 4-69
Called from ClangFrontend__CTrans.CTrans_funct.get_custom_stmt_trans in file "src/clang/cTrans.ml", line 5401, characters 8-55
Called from ClangFrontend__CTrans.CTrans_funct.exec_trans_instrs.exec_trans_instrs_rev in file "src/clang/cTrans.ml" (inlined), line 5365, characters 28-54
Called from ClangFrontend__CTrans.CTrans_funct.exec_trans_instrs i


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@qiin2333 qiin2333 marked this pull request as ready for review June 3, 2026 13:46
@qiin2333 qiin2333 force-pushed the master branch 2 times, most recently from 4db4aa3 to e5579b9 Compare June 4, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant