feat(webrtc): 添加公共ICE服务器支持和优化HID延迟

- 重构ICE配置:将TURN配置改为统一的ICE配置,支持STUN和多TURN URL
- 添加公共ICE服务器:类似RustDesk,用户留空时使用编译时配置的公共服务器
- 优化DataChannel HID消息:使用tokio::spawn立即处理,避免依赖webrtc-rs轮询
- 添加WebRTCReady事件:客户端等待此事件后再建立连接
- 初始化时启动音频流,确保WebRTC可订阅
- 移除多余的trace/debug日志减少开销
- 更新前端配置界面支持公共ICE服务器显示
This commit is contained in:
mofeng-git
2026-01-04 15:06:08 +08:00
parent 0c82d1a840
commit 9ab3d052f9
24 changed files with 766 additions and 258 deletions

View File

@@ -78,7 +78,7 @@ impl PeerConnection {
for turn in &config.turn_servers {
ice_servers.push(RTCIceServer {
urls: vec![turn.url.clone()],
urls: turn.urls.clone(),
username: turn.username.clone(),
credential: turn.credential.clone(),
..Default::default()
@@ -207,10 +207,11 @@ impl PeerConnection {
*data_channel.write().await = Some(dc.clone());
// Set up message handler with HID processing
// Immediately spawn task in tokio runtime for low latency
dc.on_message(Box::new(move |msg: DataChannelMessage| {
let hid = hid.clone();
Box::pin(async move {
tokio::spawn(async move {
debug!("DataChannel HID message: {} bytes", msg.data.len());
// Parse and process HID message
@@ -233,7 +234,10 @@ impl PeerConnection {
}
}
}
})
});
// Return empty future (actual work is spawned above)
Box::pin(async {})
}));
})
}));
@@ -432,11 +436,10 @@ impl PeerConnectionManager {
// Add video track
peer.add_video_track(VideoTrackConfig::default()).await?;
// Create data channel and set HID controller
// Set HID controller if available
// Note: We DON'T create a data channel here - the frontend creates it.
// The server receives it via on_data_channel callback set in set_hid_controller().
if self.config.enable_datachannel {
peer.create_data_channel("hid").await?;
// Set HID controller if available
if let Some(ref hid) = self.hid_controller {
peer.set_hid_controller(hid.clone());
}