use base64::Engine;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MediaInfo {
pub mime: String,
pub size_bytes: usize,
pub width: Option<u32>,
pub height: Option<u32>,
}
impl MediaInfo {
pub fn kind(&self) -> &str {
self.mime.split('/').next().unwrap_or("")
}
pub fn description(&self) -> String {
let dims = match (self.width, self.height) {
(Some(w), Some(h)) => format!("{w}×{h} px"),
_ => "dimensions inconnues".to_string(),
};
format!("{} · {} · {} o", self.mime, dims, self.size_bytes)
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct StreamInfo {
pub mime: String,
pub bytes: u64,
pub chunks: u64,
}
impl StreamInfo {
pub fn kb(&self) -> f64 {
self.bytes as f64 / 1024.0
}
}
pub fn split_data_url(data_url: &str) -> Option<(String, String)> {
let rest = data_url.strip_prefix("data:")?;
let (meta, data) = rest.split_once(',')?;
let mime = meta
.split(';')
.next()
.unwrap_or("application/octet-stream")
.to_string();
Some((mime, data.to_string()))
}
pub fn inspect(data_url: &str) -> Option<MediaInfo> {
let (mime, data) = split_data_url(data_url)?;
let bytes = decode(&data)?;
let size = bytes.len();
if size == 0 {
return None;
}
let (width, height) = dimensions(&mime, &bytes);
Some(MediaInfo {
mime,
size_bytes: size,
width,
height,
})
}
pub fn encode(bytes: &[u8]) -> String {
base64::engine::general_purpose::STANDARD.encode(bytes)
}
pub fn decode(b64: &str) -> Option<Vec<u8>> {
base64::engine::general_purpose::STANDARD
.decode(b64)
.or_else(|_| base64::engine::general_purpose::URL_SAFE.decode(b64))
.ok()
}
fn dimensions(mime: &str, b: &[u8]) -> (Option<u32>, Option<u32>) {
match mime {
"image/png" if b.len() >= 24 && &b[0..8] == b"\x89PNG\r\n\x1a\n" => (
Some(u32::from_be_bytes([b[16], b[17], b[18], b[19]])),
Some(u32::from_be_bytes([b[20], b[21], b[22], b[23]])),
),
"image/gif" if b.len() >= 10 && &b[0..3] == b"GIF" => (
Some(u16::from_le_bytes([b[6], b[7]]) as u32),
Some(u16::from_le_bytes([b[8], b[9]]) as u32),
),
"image/jpeg" => jpeg_dimensions(b),
_ => (None, None),
}
}
fn jpeg_dimensions(b: &[u8]) -> (Option<u32>, Option<u32>) {
if b.len() < 4 || b[0] != 0xFF || b[1] != 0xD8 {
return (None, None);
}
let mut i = 2usize;
while i + 9 < b.len() {
if b[i] != 0xFF {
i += 1;
continue;
}
let marker = b[i + 1];
let sof = (0xC0..=0xCF).contains(&marker) && !matches!(marker, 0xC4 | 0xC8 | 0xCC);
if sof {
let h = u16::from_be_bytes([b[i + 5], b[i + 6]]);
let w = u16::from_be_bytes([b[i + 7], b[i + 8]]);
return (Some(w as u32), Some(h as u32));
}
let len = u16::from_be_bytes([b[i + 2], b[i + 3]]);
if len < 2 {
break;
}
i += 2 + len as usize;
}
(None, None)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AudioFormat {
Pcm16KHzMono,
Pcm48KHzMono,
Pcm48KHzStereo,
F32_16KHzMono,
Opus,
RawBytes,
}
impl Default for AudioFormat {
fn default() -> Self {
Self::Pcm16KHzMono
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AudioChunk {
pub format: AudioFormat,
pub sample_rate: u32,
pub channels: u16,
pub data: Vec<u8>,
}
impl AudioChunk {
pub fn new(format: AudioFormat, sample_rate: u32, channels: u16, data: Vec<u8>) -> Self {
Self {
format,
sample_rate,
channels,
data,
}
}
pub fn pcm16_mono(samples: &[i16]) -> Self {
let mut data = Vec::with_capacity(samples.len() * 2);
for &s in samples {
data.extend_from_slice(&s.to_le_bytes());
}
Self {
format: AudioFormat::Pcm16KHzMono,
sample_rate: 16000,
channels: 1,
data,
}
}
pub fn from_f32_slice(samples: &[f32], sample_rate: u32) -> Self {
let mut data = Vec::with_capacity(samples.len() * 2);
for &s in samples {
let clamped = s.clamp(-1.0, 1.0);
let sample_i16 = (clamped * 32767.0) as i16;
data.extend_from_slice(&sample_i16.to_le_bytes());
}
let format = if sample_rate == 16000 {
AudioFormat::Pcm16KHzMono
} else {
AudioFormat::Pcm48KHzMono
};
Self {
format,
sample_rate,
channels: 1,
data,
}
}
pub fn as_f32_vec(&self) -> Vec<f32> {
let mut out = Vec::with_capacity(self.data.len() / 2);
for chunk in self.data.chunks_exact(2) {
let s = i16::from_le_bytes([chunk[0], chunk[1]]);
out.push(s as f32 / 32768.0);
}
out
}
pub fn as_i16_vec(&self) -> Vec<i16> {
let mut out = Vec::with_capacity(self.data.len() / 2);
for chunk in self.data.chunks_exact(2) {
out.push(i16::from_le_bytes([chunk[0], chunk[1]]));
}
out
}
pub fn duration_sec(&self) -> f32 {
if self.sample_rate == 0 || self.channels == 0 {
return 0.0;
}
let bytes_per_sample = 2; let total_samples = self.data.len() / (bytes_per_sample * self.channels as usize);
total_samples as f32 / self.sample_rate as f32
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum VadState {
Start,
Stop,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VadEvent {
pub id: String,
pub state: VadState,
pub timestamp_ms: u64,
pub energy: Option<f32>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PolygonMask {
pub points: Vec<[f64; 2]>,
pub label: String,
pub color: String,
pub opacity: f32,
}
impl PolygonMask {
pub fn new(label: impl Into<String>, color: impl Into<String>, points: Vec<[f64; 2]>) -> Self {
Self {
points,
label: label.into(),
color: color.into(),
opacity: 0.35,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Keypoint {
pub x: f64,
pub y: f64,
pub label: Option<String>,
pub score: Option<f64>,
pub color: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PoseSkeleton {
pub keypoints: Vec<Keypoint>,
pub connections: Vec<(usize, usize)>,
pub color: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct VisionOverlay {
pub boxes: Vec<crate::components::BoundingBox>,
pub polygons: Vec<PolygonMask>,
pub skeletons: Vec<PoseSkeleton>,
pub fps: Option<f64>,
}
impl VisionOverlay {
pub fn new() -> Self {
Self::default()
}
pub fn with_box(mut self, bbox: crate::components::BoundingBox) -> Self {
self.boxes.push(bbox);
self
}
pub fn with_boxes(mut self, boxes: Vec<crate::components::BoundingBox>) -> Self {
self.boxes = boxes;
self
}
pub fn with_polygon(mut self, poly: PolygonMask) -> Self {
self.polygons.push(poly);
self
}
pub fn with_skeleton(mut self, skeleton: PoseSkeleton) -> Self {
self.skeletons.push(skeleton);
self
}
pub fn with_fps(mut self, fps: f64) -> Self {
self.fps = Some(fps);
self
}
}