feat: 接入端到端 demo 与签名变体
- Flutter 桌面端通过 dart:ffi 调用 Rust Core cdylib - 实现 ECDH P-256 密钥协商、HKDF 子密钥派生、AES-256-GCM 控制帧 - Android 被控端用派生密钥做控制帧加密与媒体包 MAC - 新增 libopus JNI 骨架,默认回退 MediaCodec - 新增 aosp/zhanxun/neutral 签名变体与系统 sharedUserId - 本机独立信令服务与设置页开关 - 新增 .gitignore 排除所有编译产物与打包目录
This commit is contained in:
commit
31b9a52eb4
154 files changed
+13287
No files matched your search
@@ -0,0 +1,5 @@
|
||||
pub mod packet;
|
||||
pub mod renderer;
|
||||
|
||||
pub use packet::{decode_media_packet, encode_media_packet, MediaError, MediaPacket, FLAG_AUDIO, FLAG_KEYFRAME};
|
||||
pub use renderer::{AudioSink, DecodedFrame, FrameQueue, PixelFormat, VideoRenderer};
|
||||
@@ -0,0 +1,109 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::crypto::hmac_sha256;
|
||||
|
||||
pub const FLAG_KEYFRAME: u8 = 0x01;
|
||||
pub const FLAG_AUDIO: u8 = 0x02;
|
||||
pub const FLAG_END: u8 = 0x04;
|
||||
pub const HEADER_LEN: usize = 13;
|
||||
pub const MAC_LEN: usize = 4;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct MediaPacket {
|
||||
pub pts: u64,
|
||||
pub flags: u8,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum MediaError {
|
||||
InvalidMac,
|
||||
LengthMismatch,
|
||||
}
|
||||
|
||||
impl fmt::Display for MediaError {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::InvalidMac => write!(formatter, "媒体包 MAC 校验失败"),
|
||||
Self::LengthMismatch => write!(formatter, "媒体包长度字段与实际负载不一致"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for MediaError {}
|
||||
|
||||
pub fn encode_media_packet(packet: &MediaPacket, session_key: &[u8; 32]) -> Vec<u8> {
|
||||
let mut output = Vec::with_capacity(HEADER_LEN + packet.payload.len() + MAC_LEN);
|
||||
output.extend_from_slice(&packet.pts.to_le_bytes());
|
||||
output.push(packet.flags);
|
||||
output.extend_from_slice(&(packet.payload.len() as u32).to_le_bytes());
|
||||
output.extend_from_slice(&packet.payload);
|
||||
let mac = hmac_sha256(session_key, &output[..HEADER_LEN + packet.payload.len()]);
|
||||
output.extend_from_slice(&mac[..MAC_LEN]);
|
||||
output
|
||||
}
|
||||
|
||||
pub fn decode_media_packet(
|
||||
buffer: &[u8],
|
||||
session_key: &[u8; 32],
|
||||
) -> Result<Option<(MediaPacket, usize)>, MediaError> {
|
||||
if buffer.len() < HEADER_LEN {
|
||||
return Ok(None);
|
||||
}
|
||||
let pts = u64::from_le_bytes([
|
||||
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],
|
||||
]);
|
||||
let flags = buffer[8];
|
||||
let declared = u32::from_le_bytes([buffer[9], buffer[10], buffer[11], buffer[12]]) as usize;
|
||||
let total = HEADER_LEN + declared + MAC_LEN;
|
||||
if buffer.len() < total {
|
||||
return Ok(None);
|
||||
}
|
||||
let signed_part = &buffer[..HEADER_LEN + declared];
|
||||
let expected = hmac_sha256(session_key, signed_part);
|
||||
if expected[..MAC_LEN] != buffer[HEADER_LEN + declared..total] {
|
||||
return Err(MediaError::InvalidMac);
|
||||
}
|
||||
Ok(Some((
|
||||
MediaPacket {
|
||||
pts,
|
||||
flags,
|
||||
payload: signed_part[HEADER_LEN..].to_vec(),
|
||||
},
|
||||
total,
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn packet_roundtrip_and_mac() {
|
||||
let key = [7u8; 32];
|
||||
let packet = MediaPacket {
|
||||
pts: 1234,
|
||||
flags: FLAG_KEYFRAME,
|
||||
payload: vec![1, 2, 3, 4],
|
||||
};
|
||||
let bytes = encode_media_packet(&packet, &key);
|
||||
let (decoded, consumed) = decode_media_packet(&bytes, &key).unwrap().unwrap();
|
||||
assert_eq!(decoded, packet);
|
||||
assert_eq!(consumed, bytes.len());
|
||||
assert_eq!(decode_media_packet(&bytes, &[8u8; 32]), Err(MediaError::InvalidMac));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete_packet_waits() {
|
||||
let key = [1u8; 32];
|
||||
let bytes = encode_media_packet(
|
||||
&MediaPacket {
|
||||
pts: 0,
|
||||
flags: 0,
|
||||
payload: vec![9; 32],
|
||||
},
|
||||
&key,
|
||||
);
|
||||
assert_eq!(decode_media_packet(&bytes[..20], &key).unwrap(), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum PixelFormat {
|
||||
Nv12,
|
||||
Bgra8888,
|
||||
Yuv420p,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DecodedFrame {
|
||||
pub pts: u64,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub format: PixelFormat,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
pub trait VideoRenderer: Send {
|
||||
fn submit(&mut self, frame: DecodedFrame) -> Result<(), String>;
|
||||
fn clear(&mut self);
|
||||
}
|
||||
|
||||
pub trait AudioSink: Send {
|
||||
fn submit(&mut self, pcm: &[i16]) -> Result<(), String>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct FrameQueue {
|
||||
frames: VecDeque<DecodedFrame>,
|
||||
capacity: usize,
|
||||
}
|
||||
|
||||
impl FrameQueue {
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
Self {
|
||||
frames: VecDeque::new(),
|
||||
capacity,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.frames.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.frames.is_empty()
|
||||
}
|
||||
|
||||
pub fn pop_frame(&mut self) -> Option<DecodedFrame> {
|
||||
self.frames.pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
impl VideoRenderer for FrameQueue {
|
||||
fn submit(&mut self, frame: DecodedFrame) -> Result<(), String> {
|
||||
if self.capacity > 0 && self.frames.len() >= self.capacity {
|
||||
self.frames.pop_front();
|
||||
}
|
||||
self.frames.push_back(frame);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.frames.clear();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn frame_queue_drops_oldest_frame() {
|
||||
let mut queue = FrameQueue::new(2);
|
||||
for pts in 0..3 {
|
||||
queue
|
||||
.submit(DecodedFrame {
|
||||
pts,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
format: PixelFormat::Nv12,
|
||||
data: Vec::new(),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
assert_eq!(queue.len(), 2);
|
||||
assert_eq!(queue.pop_frame().unwrap().pts, 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user