#![forbid(unsafe_code)]
#[cfg(not(feature = "video"))]
compile_error!("enable the `video` feature on mediaway-decoder-linux");
use crate::DecodeError;
#[cfg(feature = "video")]
use crate::{VideoDecoder, VideoDecoderConfig};
#[cfg(feature = "video")]
use mediaway_common::VideoFrame;
use mediaway_common::{Bytes, Packet, StreamInfo};
#[cfg(target_os = "linux")]
mod vaapi;
#[cfg(feature = "video")]
pub struct LinuxVideoDecoder {
#[cfg(target_os = "linux")]
inner: Option<vaapi::VaapiH264Decoder>,
#[cfg(not(target_os = "linux"))]
_priv: (),
}
#[cfg(feature = "video")]
impl LinuxVideoDecoder {
#[cfg(target_os = "linux")]
pub fn open(config: &VideoDecoderConfig) -> Result<Self, DecodeError> {
let inner = vaapi::VaapiH264Decoder::open(config)?;
Ok(Self { inner: Some(inner) })
}
#[cfg(not(target_os = "linux"))]
pub const fn open(_config: &VideoDecoderConfig) -> Result<Self, DecodeError> {
Err(DecodeError::Unsupported)
}
}
#[cfg(feature = "video")]
#[cfg(target_os = "linux")]
impl VideoDecoder for LinuxVideoDecoder {
fn stream_info(&self) -> &StreamInfo {
#[allow(
clippy::option_if_let_else,
reason = "map_or_else forces 'static vs 'self lifetime clash"
)]
if let Some(d) = self.inner.as_ref() {
d.stream_info()
} else {
closed_stream_info()
}
}
fn push_packet(&mut self, packet: &Packet) -> Result<(), DecodeError> {
self.inner
.as_mut()
.ok_or(DecodeError::Closed)?
.push_packet(packet)
}
fn poll_frame(&mut self) -> Result<Option<VideoFrame>, DecodeError> {
self.inner.as_mut().ok_or(DecodeError::Closed)?.poll_frame()
}
fn flush(&mut self) -> Result<(), DecodeError> {
self.inner.as_mut().ok_or(DecodeError::Closed)?.flush()
}
}
#[cfg(feature = "video")]
#[cfg(not(target_os = "linux"))]
impl VideoDecoder for LinuxVideoDecoder {
fn stream_info(&self) -> &StreamInfo {
closed_stream_info()
}
fn push_packet(&mut self, _packet: &Packet) -> Result<(), DecodeError> {
Err(DecodeError::Unsupported)
}
fn poll_frame(&mut self) -> Result<Option<VideoFrame>, DecodeError> {
Ok(None)
}
fn flush(&mut self) -> Result<(), DecodeError> {
Err(DecodeError::Unsupported)
}
}
#[cfg(feature = "video")]
fn closed_stream_info() -> &'static StreamInfo {
use std::sync::OnceLock;
static INFO: OnceLock<StreamInfo> = OnceLock::new();
INFO.get_or_init(|| StreamInfo::Video {
id: 0,
codec: mediaway_common::CodecKind::H264,
time_base: mediaway_common::Rational::new(1, 30),
geometry: mediaway_common::VideoGeometry {
width: 0,
height: 0,
},
extra_data: Bytes::new(),
})
}
#[cfg(all(test, target_os = "linux", feature = "video"))]
#[path = "lib_tests.rs"]
mod tests;