feat: 立体声 client 构建

This commit is contained in:
Del Wang
2025-12-31 22:53:33 +08:00
committed by Del
parent 61ecb07a99
commit 06645dc563
9 changed files with 1303 additions and 36 deletions
+34
View File
@@ -0,0 +1,34 @@
use std::time::{SystemTime, UNIX_EPOCH};
pub fn now_us() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_micros()
}
pub struct ClockSync {
pub offset: i128, // server_time - client_time
}
impl ClockSync {
pub fn new() -> Self {
Self { offset: 0 }
}
pub fn update(&mut self, client_send_ts: u128, server_ts: u128, client_recv_ts: u128) {
let rtt = (client_recv_ts - client_send_ts) as i128;
// Estimated server time when client_recv_ts occurred: server_ts + rtt/2
let estimated_server_now = server_ts as i128 + rtt / 2;
self.offset = estimated_server_now - client_recv_ts as i128;
}
pub fn to_server_time(&self, client_time: u128) -> u128 {
(client_time as i128 + self.offset) as u128
}
pub fn to_client_time(&self, server_time: u128) -> u128 {
(server_time as i128 - self.offset) as u128
}
}