- mDNS 常驻浏览并与 UDP 信标按 ip+控制端口合并,信标优先 - TXT 解析 id/cport,缺失时回落 ip+41001 - 新增 ttstd_file_request(msgType=7 一问一答,5s 超时)与 base64 工具 - 新增 session/relay.rs:relay/1 信封、hs/hsAck/frame/bye、对端过滤 - demo_server 控制写端抽象为 TCP/中继两种,修复双编码器 nonce 复用 - 新增 ttstd_relay_start/stop,ABI 版本升到 4 - 单测覆盖信封往返与本地假信令服务器上的收发过滤
144 lines
5.3 KiB
Rust
144 lines
5.3 KiB
Rust
use crate::json::JsonValue;
|
|
|
|
use super::DiscoveryError;
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct DeviceInfo {
|
|
pub device_id: String,
|
|
pub name: String,
|
|
pub ip: String,
|
|
pub media_port: u16,
|
|
pub control_port: u16,
|
|
pub fingerprint: String,
|
|
pub capabilities: Vec<String>,
|
|
pub model: String,
|
|
pub resolution: String,
|
|
pub signaling_url: Option<String>,
|
|
}
|
|
|
|
impl DeviceInfo {
|
|
pub fn from_json(value: &JsonValue) -> Result<Self, DiscoveryError> {
|
|
let device_id = required_string(value, "deviceId")
|
|
.or_else(|| required_string(value, "id"))
|
|
.ok_or(DiscoveryError::InvalidRecord("缺少 deviceId"))?;
|
|
let ip = required_string(value, "ip").ok_or(DiscoveryError::InvalidRecord("缺少 ip"))?;
|
|
let fingerprint = required_string(value, "fp")
|
|
.or_else(|| required_string(value, "fingerprint"))
|
|
.ok_or(DiscoveryError::InvalidRecord("缺少证书指纹 fp"))?;
|
|
let media_port = port_from(value, "mediaPort")?;
|
|
let control_port = port_from(value, "controlPort")?;
|
|
let capabilities = value
|
|
.get("cap")
|
|
.and_then(JsonValue::as_array)
|
|
.map(|values| {
|
|
values
|
|
.iter()
|
|
.filter_map(JsonValue::as_str)
|
|
.map(str::to_string)
|
|
.collect()
|
|
})
|
|
.unwrap_or_default();
|
|
|
|
let name = required_string(value, "name").unwrap_or_else(|| device_id.clone());
|
|
Ok(Self {
|
|
device_id,
|
|
name,
|
|
ip,
|
|
media_port,
|
|
control_port,
|
|
fingerprint,
|
|
capabilities,
|
|
model: required_string(value, "model").unwrap_or_default(),
|
|
resolution: required_string(value, "res").unwrap_or_default(),
|
|
signaling_url: required_string(value, "sig"),
|
|
})
|
|
}
|
|
|
|
/// 发现列表(`/list`、`ttstd_discovery_devices`)里单个设备的 JSON 形态。
|
|
pub fn to_discovery_json(&self) -> JsonValue {
|
|
let mut value = JsonValue::object();
|
|
value.insert("deviceId", JsonValue::from(self.device_id.as_str()));
|
|
value.insert("name", JsonValue::from(self.name.as_str()));
|
|
value.insert("ip", JsonValue::from(self.ip.as_str()));
|
|
value.insert("controlPort", JsonValue::from(self.control_port as u64));
|
|
value.insert("mediaPort", JsonValue::from(self.media_port as u64));
|
|
value.insert("fp", JsonValue::from(self.fingerprint.as_str()));
|
|
value.insert(
|
|
"cap",
|
|
JsonValue::Array(self.capabilities.iter().map(JsonValue::from).collect()),
|
|
);
|
|
value.insert("model", JsonValue::from(self.model.as_str()));
|
|
value.insert("res", JsonValue::from(self.resolution.as_str()));
|
|
if let Some(url) = &self.signaling_url {
|
|
value.insert("sig", JsonValue::from(url.as_str()));
|
|
}
|
|
value
|
|
}
|
|
|
|
pub fn to_register_json(&self, ttl: u64) -> JsonValue {
|
|
let mut value = JsonValue::object();
|
|
value.insert("deviceId", JsonValue::from(self.device_id.as_str()));
|
|
value.insert("name", JsonValue::from(self.name.as_str()));
|
|
value.insert("ip", JsonValue::from(self.ip.as_str()));
|
|
value.insert("mediaPort", JsonValue::from(self.media_port as u64));
|
|
value.insert("controlPort", JsonValue::from(self.control_port as u64));
|
|
value.insert("fp", JsonValue::from(self.fingerprint.as_str()));
|
|
value.insert(
|
|
"cap",
|
|
JsonValue::Array(self.capabilities.iter().map(JsonValue::from).collect()),
|
|
);
|
|
value.insert("ttl", JsonValue::from(ttl));
|
|
value.insert("model", JsonValue::from(self.model.as_str()));
|
|
value.insert("res", JsonValue::from(self.resolution.as_str()));
|
|
if let Some(url) = &self.signaling_url {
|
|
value.insert("sig", JsonValue::from(url.as_str()));
|
|
}
|
|
value
|
|
}
|
|
|
|
pub fn supports(&self, capability: &str) -> bool {
|
|
self.capabilities.iter().any(|item| item == capability)
|
|
}
|
|
}
|
|
|
|
fn required_string(value: &JsonValue, key: &str) -> Option<String> {
|
|
value
|
|
.get(key)
|
|
.and_then(JsonValue::as_str)
|
|
.filter(|text| !text.is_empty())
|
|
.map(str::to_string)
|
|
}
|
|
|
|
fn port_from(value: &JsonValue, key: &str) -> Result<u16, DiscoveryError> {
|
|
let number = value
|
|
.get(key)
|
|
.and_then(JsonValue::as_u64)
|
|
.ok_or_else(|| DiscoveryError::InvalidRecord("端口字段无效"))?;
|
|
if number == 0 || number > u16::MAX as u64 {
|
|
return Err(DiscoveryError::InvalidRecord("端口超出范围"));
|
|
}
|
|
Ok(number as u16)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::json::parse;
|
|
|
|
#[test]
|
|
fn device_from_signaling_payload() {
|
|
let raw = r#"{"deviceId":"uuid-1","name":"客厅Pixel","ip":"192.168.1.10",
|
|
"mediaPort":41000,"controlPort":41001,"fp":"sha256:ab12","cap":["h264","opus"]}"#;
|
|
let device = DeviceInfo::from_json(&parse(raw).unwrap()).unwrap();
|
|
assert_eq!(device.name, "客厅Pixel");
|
|
assert_eq!(device.media_port, 41000);
|
|
assert!(device.supports("opus"));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_port_is_rejected() {
|
|
let raw = r#"{"deviceId":"a","ip":"1.2.3.4","fp":"f","mediaPort":0,"controlPort":1}"#;
|
|
assert!(DeviceInfo::from_json(&parse(raw).unwrap()).is_err());
|
|
}
|
|
}
|