From 2f8197210e2f2c17ade301eb152ae6306ff8453d Mon Sep 17 00:00:00 2001 From: dailz Date: Mon, 22 Jun 2026 17:51:40 +0800 Subject: [PATCH] =?UTF-8?q?docs(webrtc):=20=E4=B8=AD=E6=96=87=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=20str0m=20WebRTC=20=E4=BF=A1=E4=BB=A4=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webrtc.rs | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) diff --git a/src/webrtc.rs b/src/webrtc.rs index bcf5e93..fa2280f 100644 --- a/src/webrtc.rs +++ b/src/webrtc.rs @@ -1,3 +1,47 @@ +//! # WebRTC 传输模块 — str0m Sans-IO 信令服务器与媒体出口 +//! +//! ## 模块定位 +//! 将 H.264 编码帧通过 WebRTC 推送到浏览器(替代文件输出)。仅在 `--port > 0` 时启用; +//! `--port 0`(默认)走纯文件输出路径,本模块不会被实例化(见 `main.rs` 入口判断)。 +//! +//! ## str0m 是 Sans-IO WebRTC 库 +//! 类比 Go 的 `net/http`,但 Sans-IO 哲学不同: +//! - **没有 background goroutine**:str0m 不创建任何线程,所有进度都靠外部 poll 推动 +//! - **手动驱动 3 步循环**(见 `poll_and_feed`/`feed_network`/`poll_rtc`): +//! 1. 读 UDP 包 → `Rtc::handle_input(Input::Receive(...))` 喂给 str0m +//! 2. 调 `Rtc::poll_output()` 拿 `Output::Transmit` 包 → 写回 UDP socket +//! 3. 定时喂 `Input::Timeout(Instant::now())` 推动内部时钟 +//! - **同步而非 async**:str0m 不是 async/await 库(与 `tokio::net::TcpListener` 等 +//! 异步运行时无关);本文件用 `std::net::TcpListener` + `UdpSocket`(手动 +//! `set_nonblocking(true)`),完全同步代码;上层 `main.rs` 在 mio 事件循环里 +//! 周期性调 `poll_and_feed()` 推动 RTC 状态机 +//! - **Go 等价物**:`github.com/pion/webrtc`(Go 主流 WebRTC 库)也是同步 + 手动驱动, +//! 但 str0m 把 Sans-IO 推得更彻底——连 UDP socket 都不持有,所有 I/O 都由调用方管理 +//! +//! ## 内嵌 HTTP 信令服务器 +//! 本模块自带一个极简 HTTP 服务器(`std::net::TcpListener`,非 tokio/axum),3 个端点: +//! - `GET /` → 返回 `HTML_PAGE`(自带 SDP 协商 + `