2021-10-24 17:51:26 -07:00
|
|
|
use once_cell::sync::Lazy;
|
2021-10-24 15:48:10 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-10-24 17:51:26 -07:00
|
|
|
use std::{collections::HashMap, sync::Mutex, time::Instant};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::events::WatchEvent;
|
2021-10-24 15:48:10 -07:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
pub struct SubtitleTrack {
|
|
|
|
pub url: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WatchSession {
|
|
|
|
pub video_url: String,
|
|
|
|
pub subtitle_tracks: Vec<SubtitleTrack>,
|
|
|
|
|
|
|
|
is_playing: bool,
|
|
|
|
playing_from_timestamp: u64,
|
|
|
|
playing_from_instant: Instant,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
pub struct WatchSessionView {
|
|
|
|
pub video_url: String,
|
|
|
|
pub subtitle_tracks: Vec<SubtitleTrack>,
|
|
|
|
pub current_time_ms: u64,
|
|
|
|
pub is_playing: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WatchSession {
|
|
|
|
pub fn new(video_url: String, subtitle_tracks: Vec<SubtitleTrack>) -> Self {
|
|
|
|
WatchSession {
|
|
|
|
video_url,
|
|
|
|
subtitle_tracks,
|
|
|
|
is_playing: false,
|
|
|
|
playing_from_timestamp: 0,
|
|
|
|
playing_from_instant: Instant::now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn view(&self) -> WatchSessionView {
|
|
|
|
WatchSessionView {
|
|
|
|
video_url: self.video_url.clone(),
|
|
|
|
subtitle_tracks: self.subtitle_tracks.clone(),
|
|
|
|
current_time_ms: self.get_time_ms() as u64,
|
|
|
|
is_playing: self.is_playing,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_time_ms(&self) -> u64 {
|
|
|
|
if !self.is_playing {
|
|
|
|
return self.playing_from_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.playing_from_timestamp + self.playing_from_instant.elapsed().as_millis() as u64
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_time_ms(&mut self, time_ms: u64) {
|
|
|
|
self.playing_from_timestamp = time_ms;
|
|
|
|
self.playing_from_instant = Instant::now();
|
|
|
|
}
|
|
|
|
|
2021-10-24 17:51:26 -07:00
|
|
|
pub fn set_playing(&mut self, playing: bool, time_ms: u64) {
|
|
|
|
self.set_time_ms(time_ms);
|
2021-10-24 15:48:10 -07:00
|
|
|
self.is_playing = playing;
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 17:51:26 -07:00
|
|
|
|
|
|
|
pub static SESSIONS: Lazy<Mutex<HashMap<Uuid, WatchSession>>> =
|
|
|
|
Lazy::new(|| Mutex::new(HashMap::new()));
|
|
|
|
|
|
|
|
pub fn get_session(uuid: Uuid) -> Option<WatchSession> {
|
|
|
|
SESSIONS.lock().unwrap().get(&uuid).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_watch_event(uuid: Uuid, watch_session: &mut WatchSession, event: WatchEvent) {
|
|
|
|
match event {
|
|
|
|
WatchEvent::SetPlaying { playing, time } => {
|
|
|
|
watch_session.set_playing(playing, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
WatchEvent::SetTime(time) => {
|
|
|
|
watch_session.set_time_ms(time);
|
|
|
|
}
|
2021-10-24 18:59:52 -07:00
|
|
|
|
|
|
|
_ => {}
|
2021-10-24 17:51:26 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let _ = SESSIONS.lock().unwrap().insert(uuid, watch_session.clone());
|
|
|
|
}
|