Add player state module

This commit is contained in:
2026-03-19 01:07:46 +08:00
parent 8ada36e962
commit 31bf6109d6

83
src/player/state.rs Normal file
View File

@@ -0,0 +1,83 @@
//! Player state management
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PlaybackState {
Stopped,
Playing,
Paused,
}
impl Default for PlaybackState {
fn default() -> Self {
Self::Stopped
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerState {
pub playback: PlaybackState,
pub current_frame: u64,
pub total_frames: u64,
pub current_time_ms: u64,
pub duration_ms: u64,
pub fps: f64,
pub volume: f32,
pub muted: bool,
pub speed: f32,
pub show_subtitle: bool,
pub show_yolo: bool,
pub show_chunks: bool,
pub zoom: f32,
pub pan_x: f32,
pub pan_y: f32,
}
impl Default for PlayerState {
fn default() -> Self {
Self {
playback: PlaybackState::Stopped,
current_frame: 0,
total_frames: 0,
current_time_ms: 0,
duration_ms: 0,
fps: 0.0,
volume: 1.0,
muted: false,
speed: 1.0,
show_subtitle: false,
show_yolo: false,
show_chunks: false,
zoom: 1.0,
pan_x: 0.0,
pan_y: 0.0,
}
}
}
impl PlayerState {
pub fn frame_to_time(frame: u64, fps: f64) -> u64 {
if fps > 0.0 {
((frame as f64 / fps) * 1000.0) as u64
} else {
0
}
}
pub fn time_to_frame(time_ms: u64, fps: f64) -> u64 {
if fps > 0.0 {
((time_ms as f64 / 1000.0) * fps) as u64
} else {
0
}
}
pub fn format_time(ms: u64) -> String {
let total_secs = ms / 1000;
let hours = total_secs / 3600;
let minutes = (total_secs % 3600) / 60;
let seconds = total_secs % 60;
format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
}
}