Skip to content

Commit

Permalink
Video prefix (#1)
Browse files Browse the repository at this point in the history
* Add back the Video prefix.
  • Loading branch information
kixelated authored Nov 23, 2024
1 parent d2fe5d8 commit 3cd665f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Luke Curley"]
repository = "https://github.com/kixelated/web-codecs-rs"
license = "MIT OR Apache-2.0"

version = "0.1.0"
version = "0.2.0"
edition = "2021"

categories = ["wasm", "multimedia", "web-programming", "api-bindings"]
Expand Down
2 changes: 0 additions & 2 deletions src/video/frame.rs → src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
pub type DecodedFrame = web_sys::VideoFrame;

pub struct EncodedFrame {
pub payload: bytes::Bytes,
pub timestamp: f64,
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! WebCodecs API bindings for Rust.
pub mod audio;
mod audio;
mod error;
pub mod video;
mod frame;
mod video;

pub use error::*;
pub use frame::*;
pub use video::*;
22 changes: 11 additions & 11 deletions src/video/color.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub struct ColorSpaceConfig {
pub struct VideoColorSpaceConfig {
inner: web_sys::VideoColorSpaceInit,
}

impl Default for ColorSpaceConfig {
impl Default for VideoColorSpaceConfig {
fn default() -> Self {
Self::new()
}
}

impl ColorSpaceConfig {
impl VideoColorSpaceConfig {
pub fn new() -> Self {
Self {
inner: web_sys::VideoColorSpaceInit::new(),
Expand All @@ -20,28 +20,28 @@ impl ColorSpaceConfig {
self
}

pub fn matrix(self, matrix: MatrixCoefficients) -> Self {
pub fn matrix(self, matrix: VideoMatrixCoefficients) -> Self {
self.inner.set_matrix(matrix);
self
}

pub fn primaries(self, primaries: ColorPrimaries) -> Self {
pub fn primaries(self, primaries: VideoColorPrimaries) -> Self {
self.inner.set_primaries(primaries);
self
}

pub fn transfer(self, transfer: TransferCharacteristics) -> Self {
pub fn transfer(self, transfer: VideoTransferCharacteristics) -> Self {
self.inner.set_transfer(transfer);
self
}
}

impl From<&ColorSpaceConfig> for web_sys::VideoColorSpaceInit {
fn from(this: &ColorSpaceConfig) -> Self {
impl From<&VideoColorSpaceConfig> for web_sys::VideoColorSpaceInit {
fn from(this: &VideoColorSpaceConfig) -> Self {
this.inner.clone()
}
}

pub type MatrixCoefficients = web_sys::VideoMatrixCoefficients;
pub type ColorPrimaries = web_sys::VideoColorPrimaries;
pub type TransferCharacteristics = web_sys::VideoTransferCharacteristics;
pub type VideoMatrixCoefficients = web_sys::VideoMatrixCoefficients;
pub type VideoColorPrimaries = web_sys::VideoColorPrimaries;
pub type VideoTransferCharacteristics = web_sys::VideoTransferCharacteristics;
42 changes: 21 additions & 21 deletions src/video/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use bytes::Bytes;
use tokio::sync::{mpsc, watch};
use wasm_bindgen::prelude::*;

use super::{ColorSpaceConfig, DecodedFrame, EncodedFrame};
use crate::Error;
use super::VideoColorSpaceConfig;
use crate::{EncodedFrame, Error, VideoFrame};

pub fn decoder() -> (Decoder, Decoded) {
pub fn decoder() -> (VideoDecoder, VideoDecoded) {
let (frames_tx, frames_rx) = mpsc::unbounded_channel();
let (closed_tx, closed_rx) = watch::channel(Ok(()));
let closed_tx2 = closed_tx.clone();
Expand All @@ -16,7 +16,7 @@ pub fn decoder() -> (Decoder, Decoded) {

let on_frame = Closure::wrap(Box::new(move |e: JsValue| {
let frame: web_sys::VideoFrame = e.unchecked_into();
let frame = DecodedFrame::from(frame);
let frame = VideoFrame::from(frame);

if frames_tx.send(frame).is_err() {
closed_tx2.send_replace(Err(Error::Dropped)).ok();
Expand All @@ -29,21 +29,21 @@ pub fn decoder() -> (Decoder, Decoded) {
);
let inner = web_sys::VideoDecoder::new(&init).unwrap();

let decoder = Decoder {
let decoder = VideoDecoder {
inner,
on_error,
on_frame,
};

let decoded = Decoded {
let decoded = VideoDecoded {
frames: frames_rx,
closed: closed_rx,
};

(decoder, decoded)
}

pub struct Decoder {
pub struct VideoDecoder {
inner: web_sys::VideoDecoder,

// These are held to avoid dropping them.
Expand All @@ -53,7 +53,7 @@ pub struct Decoder {
on_frame: Closure<dyn FnMut(JsValue)>,
}

impl Decoder {
impl VideoDecoder {
pub fn decode(&self, frame: EncodedFrame) -> Result<(), Error> {
let chunk_type = match frame.keyframe {
true => web_sys::EncodedVideoChunkType::Key,
Expand All @@ -72,7 +72,7 @@ impl Decoder {
Ok(())
}

pub fn configure(&self, config: &DecoderConfig) -> Result<(), Error> {
pub fn configure(&self, config: &VideoDecoderConfig) -> Result<(), Error> {
self.inner.configure(&config.into())?;
Ok(())
}
Expand All @@ -82,7 +82,7 @@ impl Decoder {
Ok(())
}

pub async fn is_supported(config: &DecoderConfig) -> Result<bool, Error> {
pub async fn is_supported(config: &VideoDecoderConfig) -> Result<bool, Error> {
let res = wasm_bindgen_futures::JsFuture::from(web_sys::VideoDecoder::is_config_supported(
&config.into(),
))
Expand All @@ -106,19 +106,19 @@ impl Decoder {
}
}

impl Drop for Decoder {
impl Drop for VideoDecoder {
fn drop(&mut self) {
let _ = self.inner.close();
}
}

pub struct Decoded {
frames: mpsc::UnboundedReceiver<DecodedFrame>,
pub struct VideoDecoded {
frames: mpsc::UnboundedReceiver<VideoFrame>,
closed: watch::Receiver<Result<(), Error>>,
}

impl Decoded {
pub async fn next(&mut self) -> Result<Option<DecodedFrame>, Error> {
impl VideoDecoded {
pub async fn next(&mut self) -> Result<Option<VideoFrame>, Error> {
tokio::select! {
biased;
frame = self.frames.recv() => Ok(frame),
Expand All @@ -127,18 +127,18 @@ impl Decoded {
}
}

pub struct DecoderConfig {
pub struct VideoDecoderConfig {
codec: String,

coded_dimensions: Option<(u32, u32)>,
color_space: Option<ColorSpaceConfig>,
color_space: Option<VideoColorSpaceConfig>,
display_dimensions: Option<(u32, u32)>,
description: Option<Bytes>,
hardware_acceleration: Option<bool>,
latency_optimized: bool,
}

impl DecoderConfig {
impl VideoDecoderConfig {
pub fn new<T: Into<String>>(codec: T) -> Self {
Self {
codec: codec.into(),
Expand Down Expand Up @@ -166,7 +166,7 @@ impl DecoderConfig {
self
}

pub fn color_space(mut self, color_space: ColorSpaceConfig) -> Self {
pub fn color_space(mut self, color_space: VideoColorSpaceConfig) -> Self {
self.color_space = Some(color_space);
self
}
Expand All @@ -182,8 +182,8 @@ impl DecoderConfig {
}
}

impl From<&DecoderConfig> for web_sys::VideoDecoderConfig {
fn from(this: &DecoderConfig) -> Self {
impl From<&VideoDecoderConfig> for web_sys::VideoDecoderConfig {
fn from(this: &VideoDecoderConfig) -> Self {
let config = web_sys::VideoDecoderConfig::new(&this.codec);

if let Some((width, height)) = this.coded_dimensions {
Expand Down
4 changes: 2 additions & 2 deletions src/video/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod color;
mod decoder;
mod encoder;
mod frame;

pub use color::*;
pub use decoder::*;
pub use frame::*;

pub type VideoFrame = web_sys::VideoFrame;

0 comments on commit 3cd665f

Please sign in to comment.