use std::sync::Arc;
use crate::error::Result;
use crate::util::ratio::Ratio;
pub const VIDEO_CLOCK_RATE: u32 = 90_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AudioFamily {
AirPlay,
Cast,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TrackKind {
Video,
Audio(AudioFamily),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VideoCodec {
H264,
Hevc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AudioCodec {
Alac,
AacLc,
AacEld,
Opus,
PcmS16le,
}
#[derive(Debug, Clone)]
pub struct EncodedFrame {
pub track: TrackKind,
pub pts: u64,
pub keyframe: bool,
pub data: Arc<[u8]>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VideoInfo {
pub codec: VideoCodec,
pub width: u32,
pub height: u32,
pub fps: Ratio,
pub avcc: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AudioInfo {
pub codec: AudioCodec,
pub sample_rate: u32,
pub channels: u8,
pub frame_samples: u32,
pub config: Vec<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AudioFormat {
pub codec: AudioCodec,
pub sample_rate: u32,
pub channels: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Capabilities {
pub max_width: u32,
pub max_height: u32,
pub max_fps: Ratio,
pub video_codecs: Vec<VideoCodec>,
pub audio_formats: Vec<AudioFormat>,
pub audio_family: AudioFamily,
pub max_bitrate_kbps: Option<u32>,
pub max_pixels_per_second: Option<u64>,
pub min_latency_ms: Option<u32>,
pub preferred_latency_ms: Option<u32>,
pub display_aspect: Option<(u32, u32)>,
}
pub const DEFAULT_DISPLAY_ASPECT: (u32, u32) = (16, 9);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Resolution {
#[default]
Auto,
P720,
P1080,
P2160,
}
pub const AUTO_MAX: (u32, u32) = (1920, 1080);
impl Resolution {
pub const ALL: [Resolution; 4] = [
Resolution::Auto,
Resolution::P720,
Resolution::P1080,
Resolution::P2160,
];
pub fn parse(s: &str) -> Option<Resolution> {
match s {
"auto" => Some(Resolution::Auto),
"720p" => Some(Resolution::P720),
"1080p" => Some(Resolution::P1080),
"2160p" | "4k" | "4K" => Some(Resolution::P2160),
_ => None,
}
}
pub fn as_str(self) -> &'static str {
match self {
Resolution::Auto => "auto",
Resolution::P720 => "720p",
Resolution::P1080 => "1080p",
Resolution::P2160 => "2160p",
}
}
pub fn bounds(self) -> (u32, u32) {
match self {
Resolution::Auto => AUTO_MAX,
Resolution::P720 => (1280, 720),
Resolution::P1080 => (1920, 1080),
Resolution::P2160 => (3840, 2160),
}
}
pub fn explicit(self) -> bool {
self != Resolution::Auto
}
}
pub fn aspect_of(w: u32, h: u32) -> Option<(u32, u32)> {
fn gcd(a: u32, b: u32) -> u32 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
if w == 0 || h == 0 {
return None;
}
let g = gcd(w, h);
Some((w / g, h / g))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Encoder {
#[default]
Auto,
Builtin,
X264,
VideoToolbox,
Nvenc,
Qsv,
Vaapi,
MediaFoundation,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VideoPlan {
pub codec: VideoCodec,
pub encoder: Encoder,
pub width: u32,
pub height: u32,
pub fps: Ratio,
pub bitrate_kbps: u32,
pub gop: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AudioPlan {
pub family: AudioFamily,
pub codec: AudioCodec,
pub sample_rate: u32,
pub channels: u8,
pub frame_samples: u32,
pub bitrate_kbps: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncodePlan {
pub video: VideoPlan,
pub audio: Vec<AudioPlan>,
pub latency_ms: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SinkStatus {
Accepted,
Dropped,
Finished,
}
pub trait Sink: Send {
fn name(&self) -> &str;
fn capabilities(&self) -> &Capabilities;
fn set_clock(&mut self, clock: crate::media::clock::SessionClock) {
let _ = clock;
}
fn start(&mut self, video: &VideoInfo, audio: Option<&AudioInfo>) -> Result<()>;
fn push(&mut self, frame: &EncodedFrame) -> Result<SinkStatus>;
fn wants_keyframe(&self) -> bool;
fn stop(&mut self) -> Result<()>;
}
impl Capabilities {
pub fn accepts_audio(&self, fmt: &AudioFormat) -> bool {
self.audio_formats.contains(fmt)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamInfo {
pub video: VideoInfo,
pub audio: Vec<(AudioFamily, AudioInfo)>,
}
impl StreamInfo {
pub fn audio_for(&self, family: AudioFamily) -> Option<&AudioInfo> {
self.audio
.iter()
.find(|(f, _)| *f == family)
.map(|(_, a)| a)
}
}