use std::{ collections::VecDeque, path::PathBuf, sync::{ Arc, Mutex, MutexGuard, PoisonError, atomic::{AtomicBool, Ordering}, }, time::{Duration, Instant}, }; use common::{constant::VOICE_SAMPLE_RATE, share::Codec}; use ec_core::frame::{Frame as CodecFrame, PixelFormat, VideoFrame}; use ec_core::packet::Packet; use ec_core::registry::{CodecId, CodecParameters, Decoder as CodecDecoder, SeekMode}; use ec_core::timebase::{TimeBase, Timestamp}; use ec_h264::H264Decoder; use ec_probe::{Reader, decoder::AudioDecoder}; use pixel::yuv_to_bgra; use rtrb::Producer; use tracing::warn; use super::{ ATTACHMENT_WIDTH, Frame, SHARE_WIDTH, Tracks, resample::{self, Resampler, Stereo}, }; use crate::voice::{PLAYBACK_CHANNELS, Playback}; pub const RING_FRAMES: usize = VOICE_SAMPLE_RATE as usize / 4; pub const RING_SAMPLES: usize = RING_FRAMES * PLAYBACK_CHANNELS; const QUEUE_LIMIT: usize = 8; const BACKLOG_LIMIT: usize = 600; const LATE: f64 = 0.1; const TICK: Duration = Duration::from_millis(4); const MAX_PAD: usize = VOICE_SAMPLE_RATE as usize * 5; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum State { Playing, Paused, Finished, } #[derive(Debug)] pub struct Clock { pub state: State, pub base: f64, pub written: u64, pub started: Option, pub audio: bool, } impl Clock { pub fn new(audio: bool) -> Clock { Clock { state: State::Paused, base: 0.0, written: 0, started: None, audio, } } pub fn position(&self, queued: usize) -> f64 { let played = if self.audio { self.written.saturating_sub(queued as u64) as f64 / f64::from(VOICE_SAMPLE_RATE) } else { self.started .map_or(0.0, |started| started.elapsed().as_secs_f64()) }; self.base + played } pub fn set(&mut self, state: State) { if !self.audio { self.base = self.position(0); } self.state = state; self.started = self.wall_start(); } pub fn rebase(&mut self, to: f64) { self.base = to; self.written = 0; self.started = self.wall_start(); } fn wall_start(&self) -> Option { (self.state == State::Playing && !self.audio).then(Instant::now) } } #[derive(Debug, Default)] pub struct Video { pub queue: VecDeque<(f64, Frame)>, pub current: Option, pub generation: u64, } #[derive(Debug)] pub struct Shared { pub stop: AtomicBool, pub seek: Mutex>, pub clock: Mutex, pub video: Mutex