use std::borrow::Cow;
use thiserror::Error;
use wasm_bindgen::{JsCast, JsValue};
#[derive(Debug, Clone, Error)]
#[error("WebCodecs error: {message}")]
pub struct Error {
message: Cow<'static, str>,
}
impl Error {
pub fn from_js(value: JsValue) -> Self {
let message: String = if let Some(exc) = value.dyn_ref::<web_sys::DomException>() {
let mut s = exc.name();
let m = exc.message();
if !m.is_empty() {
s.push_str(": ");
s.push_str(&m);
}
s
} else if let Some(s) = value.as_string() {
s
} else {
format!("{value:?}")
};
Self {
message: Cow::Owned(message),
}
}
pub const fn from_static(msg: &'static str) -> Self {
Self {
message: Cow::Borrowed(msg),
}
}
pub fn message(&self) -> &str {
&self.message
}
}
#[derive(Debug, Clone, Error)]
pub enum VideoDecodeError {
#[error("output queue full; drain via receive_frame and retry")]
OutputFull,
#[error("no video frame available; submit packets via send_packet")]
NoFrameReady,
#[error("decoder is at EOF; flush() before sending new packets")]
AtEof,
#[error("decoder exhausted; call flush to reuse")]
Eof,
#[error("unsupported codec: {0}")]
UnsupportedCodec(String),
#[error("WebCodecs VideoDecoder is not available in this browser")]
Unavailable,
#[error("decoder is closed: {0}")]
Closed(Error),
#[error("unsupported pixel format: {0}")]
UnsupportedPixelFormat(String),
#[error(transparent)]
Js(#[from] Error),
}
#[derive(Debug, Clone, Error)]
pub enum AudioDecodeError {
#[error("output queue full; drain via receive_frame and retry")]
OutputFull,
#[error("no audio frame available; submit packets via send_packet")]
NoFrameReady,
#[error("decoder is at EOF; flush() before sending new packets")]
AtEof,
#[error("decoder exhausted; call flush to reuse")]
Eof,
#[error("unsupported codec: {0}")]
UnsupportedCodec(String),
#[error("WebCodecs AudioDecoder is not available in this browser")]
Unavailable,
#[error("decoder is closed: {0}")]
Closed(Error),
#[error("unsupported sample format: {0}")]
UnsupportedSampleFormat(String),
#[error(transparent)]
Js(#[from] Error),
}