├── .rustfmt.toml ├── .cargo └── config.toml ├── Cargo.toml ├── rust-toolchain.toml ├── web-async ├── src │ ├── lib.rs │ ├── spawn.rs │ ├── futures.rs │ └── lock.rs ├── CHANGELOG.md └── Cargo.toml ├── web-codecs ├── src │ ├── audio │ │ ├── mod.rs │ │ ├── decoder.rs │ │ ├── encoder.rs │ │ └── data.rs │ ├── video │ │ ├── mod.rs │ │ ├── dimensions.rs │ │ ├── frame.rs │ │ ├── color.rs │ │ ├── decoder.rs │ │ └── encoder.rs │ ├── lib.rs │ ├── error.rs │ └── frame.rs ├── Cargo.toml └── CHANGELOG.md ├── web-streams ├── src │ ├── lib.rs │ ├── error.rs │ ├── promise.rs │ ├── writer.rs │ └── reader.rs ├── Cargo.toml └── CHANGELOG.md ├── .editorconfig ├── web-message ├── src │ ├── error.rs │ ├── derive.rs │ ├── lib.rs │ └── message.rs ├── CHANGELOG.md └── Cargo.toml ├── web-message-derive ├── Cargo.toml ├── CHANGELOG.md └── src │ └── lib.rs ├── .github └── workflows │ ├── pr.yml │ └── release.yml ├── .gitignore ├── LICENSE-MIT ├── README.md ├── justfile └── LICENSE-APACHE /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | # i die on this hill 2 | hard_tabs = true 3 | max_width = 120 4 | -------------------------------------------------------------------------------- /.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "wasm32-unknown-unknown" 3 | rustflags = ["--cfg=web_sys_unstable_apis"] 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | resolver = "2" 3 | members = ["web-async", "web-codecs", "web-message", "web-streams"] 4 | -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | components = ["rustfmt", "clippy"] 3 | targets = ["wasm32-unknown-unknown"] 4 | -------------------------------------------------------------------------------- /web-async/src/lib.rs: -------------------------------------------------------------------------------- 1 | mod futures; 2 | mod lock; 3 | mod spawn; 4 | 5 | pub use futures::*; 6 | pub use lock::*; 7 | pub use spawn::*; 8 | -------------------------------------------------------------------------------- /web-codecs/src/audio/mod.rs: -------------------------------------------------------------------------------- 1 | mod data; 2 | mod decoder; 3 | mod encoder; 4 | 5 | pub use data::*; 6 | pub use decoder::*; 7 | pub use encoder::*; 8 | -------------------------------------------------------------------------------- /web-streams/src/lib.rs: -------------------------------------------------------------------------------- 1 | mod error; 2 | mod promise; 3 | mod reader; 4 | mod writer; 5 | 6 | pub use error::*; 7 | pub(crate) use promise::*; 8 | pub use reader::*; 9 | pub use writer::*; 10 | -------------------------------------------------------------------------------- /web-codecs/src/video/mod.rs: -------------------------------------------------------------------------------- 1 | mod color; 2 | mod decoder; 3 | mod dimensions; 4 | mod encoder; 5 | mod frame; 6 | 7 | pub use color::*; 8 | pub use decoder::*; 9 | pub use dimensions::*; 10 | pub use encoder::*; 11 | pub use frame::*; 12 | -------------------------------------------------------------------------------- /web-codecs/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! WebCodecs API bindings for Rust. 2 | mod audio; 3 | mod error; 4 | mod frame; 5 | mod video; 6 | 7 | pub use audio::*; 8 | pub use error::*; 9 | pub use frame::*; 10 | pub use video::*; 11 | 12 | pub type Timestamp = std::time::Duration; 13 | -------------------------------------------------------------------------------- /web-codecs/src/video/dimensions.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)] 2 | pub struct Dimensions { 3 | pub width: u32, 4 | pub height: u32, 5 | } 6 | 7 | impl Dimensions { 8 | pub fn new(width: u32, height: u32) -> Self { 9 | Self { width, height } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /web-streams/src/error.rs: -------------------------------------------------------------------------------- 1 | use wasm_bindgen::prelude::*; 2 | 3 | #[derive(Debug, thiserror::Error, Clone)] 4 | pub enum Error { 5 | #[error("unknown error: {0:?}")] 6 | Unknown(JsValue), 7 | } 8 | 9 | impl From for Error { 10 | fn from(e: JsValue) -> Self { 11 | Self::Unknown(e) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | indent_style = tab 9 | indent_size = 4 10 | max_line_length = 120 11 | 12 | # YAML doesn't support hard tabs 🙃 13 | [**.yml] 14 | indent_style = space 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /web-streams/src/promise.rs: -------------------------------------------------------------------------------- 1 | use wasm_bindgen::prelude::*; 2 | use web_sys::js_sys; 3 | 4 | /// A helper to ignore the result of a promise. 5 | pub(crate) trait PromiseExt { 6 | fn ignore(self); 7 | } 8 | 9 | impl PromiseExt for js_sys::Promise { 10 | // Ignore the result of the promise by using an empty catch. 11 | fn ignore(self) { 12 | let closure = Closure::wrap(Box::new(|_: JsValue| {}) as Box); 13 | let _ = self.catch(&closure); 14 | closure.forget(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /web-message/src/error.rs: -------------------------------------------------------------------------------- 1 | #[derive(thiserror::Error, Debug)] 2 | pub enum Error { 3 | #[error("missing '{0}' field")] 4 | MissingField(&'static str), 5 | 6 | #[error("invalid '{0}' field")] 7 | InvalidField(&'static str), 8 | 9 | #[error("unexpected length")] 10 | UnexpectedLength, 11 | 12 | #[error("unexpected type")] 13 | UnexpectedType, 14 | 15 | #[error("unknown tag")] 16 | UnknownTag, 17 | 18 | #[cfg(feature = "Url")] 19 | #[error("invalid URL: {0}")] 20 | InvalidUrl(url::ParseError), 21 | } 22 | -------------------------------------------------------------------------------- /web-codecs/src/error.rs: -------------------------------------------------------------------------------- 1 | use wasm_bindgen::prelude::*; 2 | 3 | #[derive(Debug, thiserror::Error, Clone)] 4 | pub enum Error { 5 | #[error("dropped")] 6 | Dropped, 7 | 8 | #[error("invalid dimensions")] 9 | InvalidDimensions, 10 | 11 | #[error("no channels")] 12 | NoChannels, 13 | 14 | #[error("unknown error: {0:?}")] 15 | Unknown(JsValue), 16 | } 17 | 18 | impl From for Error { 19 | fn from(e: JsValue) -> Self { 20 | Self::Unknown(e) 21 | } 22 | } 23 | 24 | pub type Result = std::result::Result; 25 | -------------------------------------------------------------------------------- /web-async/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.1.1](https://github.com/kixelated/web-rs/compare/web-async-v0.1.0...web-async-v0.1.1) - 2025-05-09 11 | 12 | ### Other 13 | 14 | - Add a tracing feature. ([#22](https://github.com/kixelated/web-rs/pull/22)) 15 | -------------------------------------------------------------------------------- /web-message-derive/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "web-message-derive" 3 | version = "0.0.2" 4 | edition = "2024" 5 | 6 | description = "A macro that converts Rust structs to/from JavaScript objects via postMessage." 7 | authors = ["Luke Curley"] 8 | repository = "https://github.com/kixelated/web-rs" 9 | license = "MIT OR Apache-2.0" 10 | 11 | categories = ["wasm", "web-programming"] 12 | 13 | [lib] 14 | proc-macro = true 15 | 16 | [dependencies] 17 | proc-macro2 = "1" 18 | quote = "1" 19 | syn = { version = "2", features = ["full"] } 20 | -------------------------------------------------------------------------------- /web-streams/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "web-streams" 3 | description = "WASM bindings for the Streams API" 4 | authors = ["Luke Curley"] 5 | repository = "https://github.com/kixelated/web-rs" 6 | license = "MIT OR Apache-2.0" 7 | 8 | version = "0.1.2" 9 | edition = "2021" 10 | 11 | categories = ["wasm", "web-programming", "api-bindings"] 12 | 13 | [dependencies] 14 | thiserror = "2.0" 15 | wasm-bindgen = "0.2" 16 | wasm-bindgen-futures = "0.4" 17 | 18 | [dependencies.web-sys] 19 | version = "0.3.77" 20 | features = [ 21 | "ReadableStream", 22 | "ReadableStreamDefaultReader", 23 | "ReadableStreamReadResult", 24 | "WritableStream", 25 | "WritableStreamDefaultWriter", 26 | ] 27 | -------------------------------------------------------------------------------- /web-async/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "web-async" 3 | description = "Async helpers and utilities for WASM" 4 | authors = ["Luke Curley"] 5 | repository = "https://github.com/kixelated/web-rs" 6 | license = "MIT OR Apache-2.0" 7 | 8 | version = "0.1.1" 9 | edition = "2021" 10 | 11 | keywords = ["wasm", "async", "futures"] 12 | categories = ["wasm"] 13 | 14 | [dependencies] 15 | tracing = { version = "0.1", optional = true } 16 | 17 | [features] 18 | tracing = ["dep:tracing"] 19 | 20 | [target.'cfg(target_arch = "wasm32")'.dependencies] 21 | wasm-bindgen-futures = "0.4" 22 | 23 | [target.'cfg(not(target_arch = "wasm32"))'.dependencies] 24 | tokio = { version = "1", features = ["rt"] } 25 | -------------------------------------------------------------------------------- /web-message-derive/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.0.2](https://github.com/kixelated/web-rs/compare/web-message-derive-v0.0.1...web-message-derive-v0.0.2) - 2025-05-09 11 | 12 | ### Other 13 | 14 | - Add support for unnamed tuples. ([#23](https://github.com/kixelated/web-rs/pull/23)) 15 | - Make unit enums strings, not null objects. ([#21](https://github.com/kixelated/web-rs/pull/21)) 16 | - Add the web-message crate ([#17](https://github.com/kixelated/web-rs/pull/17)) 17 | -------------------------------------------------------------------------------- /web-async/src/spawn.rs: -------------------------------------------------------------------------------- 1 | use std::future::Future; 2 | 3 | // It's not pretty, but we have per-platform implementations of spawn. 4 | // The main problem is Send; it's an annoying trait that colors everything. 5 | // The rest of this crate is Send agnostic so it will work on WASM. 6 | // TODO: use a send feature and make this runtime agnostic? 7 | 8 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 9 | pub fn spawn + Send + 'static>(f: F) { 10 | #[cfg(feature = "tracing")] 11 | let f = tracing::Instrument::in_current_span(f); 12 | tokio::task::spawn(f); 13 | } 14 | 15 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 16 | pub fn spawn + 'static>(f: F) { 17 | #[cfg(feature = "tracing")] 18 | let f = tracing::Instrument::in_current_span(f); 19 | wasm_bindgen_futures::spawn_local(f); 20 | } 21 | -------------------------------------------------------------------------------- /web-message/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.0.2](https://github.com/kixelated/web-rs/compare/web-message-v0.0.1...web-message-v0.0.2) - 2025-05-09 11 | 12 | ### Other 13 | 14 | - Add a bunch of stuff ([#24](https://github.com/kixelated/web-rs/pull/24)) 15 | - Add support for unnamed tuples. ([#23](https://github.com/kixelated/web-rs/pull/23)) 16 | - Add Just ([#18](https://github.com/kixelated/web-rs/pull/18)) 17 | - Make unit enums strings, not null objects. ([#21](https://github.com/kixelated/web-rs/pull/21)) 18 | - Add the web-message crate ([#17](https://github.com/kixelated/web-rs/pull/17)) 19 | -------------------------------------------------------------------------------- /web-streams/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [0.1.2](https://github.com/kixelated/web-rs/compare/web-streams-v0.1.1...web-streams-v0.1.2) - 2025-05-09 11 | 12 | ### Other 13 | 14 | - Add a bunch of stuff ([#24](https://github.com/kixelated/web-rs/pull/24)) 15 | - Add Just ([#18](https://github.com/kixelated/web-rs/pull/18)) 16 | 17 | ## [0.1.1](https://github.com/kixelated/web-rs/compare/web-streams-v0.1.0...web-streams-v0.1.1) - 2025-01-10 18 | 19 | ### Other 20 | 21 | - Fix encoding and some bugs. ([#9](https://github.com/kixelated/web-rs/pull/9)) 22 | - Tabs r cool ([#8](https://github.com/kixelated/web-rs/pull/8)) 23 | -------------------------------------------------------------------------------- /.github/workflows/pr.yml: -------------------------------------------------------------------------------- 1 | name: pr 2 | 3 | on: 4 | pull_request: 5 | branches: ["main"] 6 | 7 | env: 8 | CARGO_TERM_COLOR: always 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | 17 | # Install Rust with clippy/rustfmt 18 | - uses: actions-rust-lang/setup-rust-toolchain@v1 19 | with: 20 | target: wasm32-unknown-unknown 21 | components: clippy, rustfmt 22 | 23 | # Install Just to run CI scripts 24 | - uses: extractions/setup-just@v3 25 | 26 | # Cargo binstall is used to install tools faster than compiling them from source. 27 | - uses: cargo-bins/cargo-binstall@main 28 | - run: just setup-tools 29 | 30 | # Set RUSTFLAGS 31 | - run: echo "RUSTFLAGS=--cfg=web_sys_unstable_apis" >> $GITHUB_ENV 32 | 33 | # Make sure u guys don't write bad code 34 | - run: just check -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | # RustRover 17 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 18 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 19 | # and can be added to the global gitignore or merged into this file. For a more nuclear 20 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 21 | #.idea/ 22 | 23 | # Added by cargo 24 | 25 | /target 26 | 27 | .DS_Store 28 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | permissions: 4 | pull-requests: write 5 | contents: write 6 | 7 | on: 8 | push: 9 | branches: 10 | - main 11 | 12 | jobs: 13 | release: 14 | runs-on: ubuntu-latest 15 | steps: 16 | # Checkout the repository 17 | - name: Checkout repository 18 | uses: actions/checkout@v4 19 | with: 20 | fetch-depth: 0 21 | 22 | # Install Rust 23 | - uses: actions-rust-lang/setup-rust-toolchain@v1 24 | with: 25 | target: wasm32-unknown-unknown 26 | 27 | # Set RUSTFLAGS 28 | - run: echo "RUSTFLAGS=--cfg=web_sys_unstable_apis" >> $GITHUB_ENV 29 | 30 | # Run release-plz to create PRs and releases 31 | - name: Run release-plz 32 | uses: MarcoIeni/release-plz-action@v0.5 33 | env: 34 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 35 | CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} 36 | -------------------------------------------------------------------------------- /web-streams/src/writer.rs: -------------------------------------------------------------------------------- 1 | use wasm_bindgen::prelude::*; 2 | use wasm_bindgen_futures::JsFuture; 3 | use web_sys::{WritableStream, WritableStreamDefaultWriter}; 4 | 5 | use crate::{Error, PromiseExt}; 6 | 7 | // Wrapper around WritableStream 8 | pub struct Writer { 9 | inner: WritableStreamDefaultWriter, 10 | } 11 | 12 | impl Writer { 13 | pub fn new(stream: &WritableStream) -> Result { 14 | let inner = stream.get_writer()?.unchecked_into(); 15 | Ok(Self { inner }) 16 | } 17 | 18 | pub async fn write(&mut self, v: &JsValue) -> Result<(), Error> { 19 | JsFuture::from(self.inner.write_with_chunk(v)).await?; 20 | Ok(()) 21 | } 22 | 23 | pub fn close(&mut self) { 24 | self.inner.close().ignore(); 25 | } 26 | 27 | pub fn abort(&mut self, reason: &str) { 28 | let str = JsValue::from_str(reason); 29 | self.inner.abort_with_reason(&str).ignore(); 30 | } 31 | 32 | pub async fn closed(&self) -> Result<(), Error> { 33 | JsFuture::from(self.inner.closed()).await?; 34 | Ok(()) 35 | } 36 | } 37 | 38 | impl Drop for Writer { 39 | fn drop(&mut self) { 40 | self.inner.release_lock(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Luke Curley 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # web-rs 2 | Rust bindings to make WASM more tolerable. 3 | 4 | ## Unstable API 5 | Some crates use unstable `web_sys` APIs and you may need to set `--cfg=web_sys_unstable_apis` when compiling. 6 | For more information, see the [web-sys docs](https://rustwasm.github.io/wasm-bindgen/web-sys/unstable-apis.html). 7 | 8 | There's a few ways to set this depending on the environment: 9 | - [Cargo Config](./cargo/config.toml) via `rustflags` 10 | - [Github Action](.github/workflows/pr.yml) via `GITHUB_ENV` 11 | - [docs.rs](./web-codecs/Cargo.toml) via `package.metadata.docs.rs` 12 | 13 | ## web-codecs 14 | [web-codecs](./web-codecs) provides a wrapper around the [WebCodecs API](https://developer.mozilla.org/en-US/docs/Web/API/WebCodecs_API). 15 | 16 | The callbacks have been replaced with a channel-like API. 17 | For example, the `VideoEncoder` is split into a `VideoEncoder` for input and a `VideoEncoded` for output. 18 | 19 | ## web-streams 20 | [web-streams](./web-streams) provides a wrapper around the [Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API). 21 | 22 | This API is annoyingly untyped when using web_sys. 23 | This library handles the casting for you as well as providing guard-rails around the API (ex. closing on Drop). -------------------------------------------------------------------------------- /web-codecs/src/video/frame.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | ops::{Deref, DerefMut}, 3 | time::Duration, 4 | }; 5 | 6 | use derive_more::From; 7 | 8 | use crate::Timestamp; 9 | 10 | use super::Dimensions; 11 | 12 | #[derive(Debug, From)] 13 | pub struct VideoFrame(web_sys::VideoFrame); 14 | 15 | impl VideoFrame { 16 | pub fn timestamp(&self) -> Timestamp { 17 | Timestamp::from_micros(self.0.timestamp().unwrap() as _) 18 | } 19 | 20 | pub fn duration(&self) -> Option { 21 | Some(Duration::from_micros(self.0.duration()? as _)) 22 | } 23 | 24 | pub fn dimensions(&self) -> Dimensions { 25 | Dimensions { 26 | width: self.0.coded_width(), 27 | height: self.0.coded_height(), 28 | } 29 | } 30 | } 31 | 32 | // Avoid closing the video frame on transfer by cloning it. 33 | impl From for web_sys::VideoFrame { 34 | fn from(this: VideoFrame) -> Self { 35 | this.0.clone().expect("detached") 36 | } 37 | } 38 | 39 | impl Clone for VideoFrame { 40 | fn clone(&self) -> Self { 41 | Self(self.0.clone().expect("detached")) 42 | } 43 | } 44 | 45 | impl Deref for VideoFrame { 46 | type Target = web_sys::VideoFrame; 47 | 48 | fn deref(&self) -> &Self::Target { 49 | &self.0 50 | } 51 | } 52 | 53 | impl DerefMut for VideoFrame { 54 | fn deref_mut(&mut self) -> &mut Self::Target { 55 | &mut self.0 56 | } 57 | } 58 | 59 | // Make sure we close the frame on drop. 60 | impl Drop for VideoFrame { 61 | fn drop(&mut self) { 62 | self.0.close(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /web-codecs/src/video/color.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug, Clone)] 2 | pub struct VideoColorSpaceConfig { 3 | inner: web_sys::VideoColorSpaceInit, 4 | } 5 | 6 | impl Default for VideoColorSpaceConfig { 7 | fn default() -> Self { 8 | Self::new() 9 | } 10 | } 11 | 12 | impl VideoColorSpaceConfig { 13 | pub fn new() -> Self { 14 | Self { 15 | inner: web_sys::VideoColorSpaceInit::new(), 16 | } 17 | } 18 | 19 | pub fn full_range(self, enabled: bool) -> Self { 20 | self.inner.set_full_range(enabled); 21 | self 22 | } 23 | 24 | pub fn matrix(self, matrix: VideoMatrixCoefficients) -> Self { 25 | self.inner.set_matrix(matrix); 26 | self 27 | } 28 | 29 | pub fn primaries(self, primaries: VideoColorPrimaries) -> Self { 30 | self.inner.set_primaries(primaries); 31 | self 32 | } 33 | 34 | pub fn transfer(self, transfer: VideoTransferCharacteristics) -> Self { 35 | self.inner.set_transfer(transfer); 36 | self 37 | } 38 | } 39 | 40 | impl From<&VideoColorSpaceConfig> for web_sys::VideoColorSpaceInit { 41 | fn from(this: &VideoColorSpaceConfig) -> Self { 42 | this.inner.clone() 43 | } 44 | } 45 | 46 | impl From for VideoColorSpaceConfig { 47 | fn from(inner: web_sys::VideoColorSpaceInit) -> Self { 48 | Self { inner } 49 | } 50 | } 51 | 52 | pub type VideoMatrixCoefficients = web_sys::VideoMatrixCoefficients; 53 | pub type VideoColorPrimaries = web_sys::VideoColorPrimaries; 54 | pub type VideoTransferCharacteristics = web_sys::VideoTransferCharacteristics; 55 | -------------------------------------------------------------------------------- /justfile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env just --justfile 2 | 3 | # Using Just: https://github.com/casey/just?tab=readme-ov-file#installation 4 | 5 | export RUST_BACKTRACE := "1" 6 | export RUST_LOG := "debug" 7 | 8 | # List all of the available commands. 9 | default: 10 | just --list 11 | 12 | # Install any required dependencies. 13 | setup: 14 | # Install cargo-binstall for faster tool installation. 15 | cargo install cargo-binstall 16 | just setup-tools 17 | 18 | # A separate entrypoint for CI. 19 | setup-tools: 20 | cargo binstall -y cargo-shear cargo-sort cargo-upgrades cargo-edit cargo-audit 21 | 22 | # Run the CI checks 23 | check: 24 | cargo check --all-targets --all-features 25 | cargo clippy --all-targets --all-features -- -D warnings 26 | cargo fmt -- --check 27 | 28 | # requires: cargo install cargo-shear 29 | cargo shear 30 | 31 | # requires: cargo install cargo-sort 32 | cargo sort --workspace --check 33 | 34 | # requires: cargo install cargo-audit 35 | cargo audit 36 | 37 | # Run any CI tests 38 | test: 39 | cargo test 40 | 41 | # Automatically fix some issues. 42 | fix: 43 | cargo fix --allow-staged --all-targets --all-features 44 | cargo clippy --fix --allow-staged --all-targets --all-features 45 | 46 | # requires: cargo install cargo-shear 47 | cargo shear --fix 48 | 49 | # requires: cargo install cargo-sort 50 | cargo sort --workspace 51 | 52 | cargo fmt --all 53 | 54 | # Upgrade any tooling 55 | upgrade: 56 | rustup upgrade 57 | 58 | # Requires: cargo install cargo-upgrades cargo-edit 59 | cargo upgrade -------------------------------------------------------------------------------- /web-message/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "web-message" 3 | version = "0.0.2" 4 | edition = "2024" 5 | description = "A macro that converts Rust structs to/from JavaScript objects via postMessage." 6 | authors = ["Luke Curley"] 7 | repository = "https://github.com/kixelated/web-rs" 8 | license = "MIT OR Apache-2.0" 9 | 10 | categories = ["wasm", "web-programming"] 11 | 12 | [features] 13 | default = ["derive"] 14 | derive = ["dep:web-message-derive"] 15 | 16 | # These features implement the Message interface for popular crates: 17 | Url = ["dep:url"] 18 | 19 | # These feature names copy web_sys for all (currently) transferable types. 20 | # https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects 21 | MessagePort = ["web-sys/MessagePort"] 22 | ReadableStream = ["web-sys/ReadableStream"] 23 | WritableStream = ["web-sys/WritableStream"] 24 | TransformStream = ["web-sys/TransformStream"] 25 | WebTransportReceiveStream = ["web-sys/WebTransportReceiveStream"] 26 | WebTransportSendStream = ["web-sys/WebTransportSendStream"] 27 | AudioData = ["web-sys/AudioData"] 28 | ImageBitmap = ["web-sys/ImageBitmap"] 29 | VideoFrame = ["web-sys/VideoFrame"] 30 | OffscreenCanvas = ["web-sys/OffscreenCanvas"] 31 | RtcDataChannel = ["web-sys/RtcDataChannel"] 32 | # MediaSourceHandle = ["web-sys/MediaSourceHandle"] 33 | MidiAccess = ["web-sys/MidiAccess"] 34 | 35 | [dependencies] 36 | thiserror = "2" 37 | 38 | url = { version = "2", optional = true } 39 | web-message-derive = { path = "../web-message-derive", version = "0.0.2", optional = true } 40 | web-sys = "0.3" 41 | -------------------------------------------------------------------------------- /web-message/src/derive.rs: -------------------------------------------------------------------------------- 1 | pub use web_message_derive::Message; 2 | 3 | #[cfg(test)] 4 | mod test { 5 | use web_sys::js_sys::{Array, ArrayBuffer}; 6 | 7 | use crate::Message; 8 | 9 | #[test] 10 | fn enum_msg() { 11 | #[derive(Message, Clone, Debug, PartialEq, Eq)] 12 | struct Config { 13 | width: u32, 14 | height: u32, 15 | } 16 | 17 | #[derive(Message, Clone, Debug, PartialEq, Eq)] 18 | enum Command { 19 | Connect { url: String }, 20 | Frame { name: Option, payload: ArrayBuffer }, 21 | Config(Config), 22 | Close, 23 | Dimensions(u32, u64), 24 | } 25 | 26 | let command = Command::Frame { 27 | name: Some("test".to_string()), 28 | payload: ArrayBuffer::new(100), 29 | }; 30 | 31 | let mut transferable = Array::new(); 32 | let obj = command.clone().into_message(&mut transferable); 33 | let out = Command::from_message(obj).unwrap(); 34 | 35 | assert_eq!(command, out); 36 | assert_eq!(transferable.length(), 1); 37 | } 38 | 39 | #[test] 40 | fn struct_msg() { 41 | #[derive(Message, Clone, Debug, PartialEq, Eq)] 42 | struct Event { 43 | payload: ArrayBuffer, 44 | width: u64, 45 | name: String, 46 | } 47 | 48 | let event = Event { 49 | payload: ArrayBuffer::new(100), 50 | width: 100, 51 | name: "test".to_string(), 52 | }; 53 | 54 | let mut transferable = Array::new(); 55 | let obj = event.clone().into_message(&mut transferable); 56 | let out = Event::from_message(obj).unwrap(); 57 | 58 | assert_eq!(event, out); 59 | assert_eq!(transferable, [event.payload].iter().collect()); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /web-codecs/src/frame.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | 3 | use bytes::{Bytes, BytesMut}; 4 | 5 | use crate::Timestamp; 6 | 7 | pub struct EncodedFrame { 8 | pub payload: Bytes, 9 | pub timestamp: Timestamp, 10 | pub keyframe: bool, 11 | } 12 | 13 | impl fmt::Debug for EncodedFrame { 14 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 15 | f.debug_struct("EncodedFrame") 16 | .field("payload", &self.payload.len()) 17 | .field("timestamp", &self.timestamp) 18 | .field("keyframe", &self.keyframe) 19 | .finish() 20 | } 21 | } 22 | 23 | impl From for EncodedFrame { 24 | fn from(chunk: web_sys::EncodedVideoChunk) -> Self { 25 | let size = chunk.byte_length() as usize; 26 | 27 | let mut payload = BytesMut::with_capacity(size); 28 | payload.resize(size, 0); 29 | chunk.copy_to_with_u8_slice(&mut payload).unwrap(); 30 | 31 | Self { 32 | payload: payload.freeze(), 33 | timestamp: Timestamp::from_micros(chunk.timestamp() as _), 34 | keyframe: chunk.type_() == web_sys::EncodedVideoChunkType::Key, 35 | } 36 | } 37 | } 38 | 39 | impl From for EncodedFrame { 40 | fn from(chunk: web_sys::EncodedAudioChunk) -> Self { 41 | let size = chunk.byte_length() as usize; 42 | 43 | let mut payload = BytesMut::with_capacity(size); 44 | payload.resize(size, 0); 45 | chunk.copy_to_with_u8_slice(&mut payload).unwrap(); 46 | 47 | Self { 48 | payload: payload.freeze(), 49 | timestamp: Timestamp::from_micros(chunk.timestamp() as _), 50 | keyframe: chunk.type_() == web_sys::EncodedAudioChunkType::Key, 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /web-async/src/futures.rs: -------------------------------------------------------------------------------- 1 | use std::future::Future; 2 | use std::pin::Pin; 3 | use std::task::{Context, Poll}; 4 | 5 | pub trait FuturesExt: Future { 6 | fn transpose(self) -> Transpose 7 | where 8 | Self: Sized, 9 | { 10 | Transpose { future: self } 11 | } 12 | 13 | fn cloned(self) -> Cloned 14 | where 15 | Self: Sized, 16 | { 17 | Cloned { future: self } 18 | } 19 | } 20 | 21 | impl FuturesExt for F {} 22 | 23 | pub struct Transpose { 24 | future: F, 25 | } 26 | 27 | impl Future for Transpose 28 | where 29 | F: Future, E>>, 30 | { 31 | type Output = Option>; 32 | 33 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 34 | // Frankly I have no idea if this is correct; I hate Pin 35 | let future = unsafe { self.map_unchecked_mut(|s| &mut s.future) }; 36 | 37 | match future.poll(cx) { 38 | Poll::Ready(Ok(Some(val))) => Poll::Ready(Some(Ok(val))), 39 | Poll::Ready(Ok(None)) => Poll::Ready(None), 40 | Poll::Ready(Err(err)) => Poll::Ready(Some(Err(err))), 41 | Poll::Pending => Poll::Pending, 42 | } 43 | } 44 | } 45 | 46 | pub struct Cloned { 47 | future: F, 48 | } 49 | 50 | impl Future for Cloned 51 | where 52 | F: Future, 53 | T: Clone, 54 | { 55 | type Output = T; 56 | 57 | fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { 58 | // Frankly I have no idea if this is correct; I hate Pin 59 | let future = unsafe { self.map_unchecked_mut(|s| &mut s.future) }; 60 | 61 | match future.poll(cx) { 62 | Poll::Ready(val) => Poll::Ready(val.clone()), 63 | Poll::Pending => Poll::Pending, 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /web-codecs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "web-codecs" 3 | description = "WASM bindings for Web Codecs" 4 | authors = ["Luke Curley"] 5 | repository = "https://github.com/kixelated/web-rs" 6 | license = "MIT OR Apache-2.0" 7 | 8 | version = "0.3.7" 9 | edition = "2021" 10 | 11 | categories = ["wasm", "multimedia", "web-programming", "api-bindings"] 12 | rust-version = "1.85" 13 | 14 | [dependencies] 15 | bytemuck = "1.22" 16 | bytes = "1" 17 | derive_more = { version = "2", features = ["from", "display"] } 18 | js-sys = "0.3.77" 19 | thiserror = "2" 20 | tokio = { version = "1", features = ["sync", "macros"] } 21 | wasm-bindgen = "0.2" 22 | wasm-bindgen-futures = "0.4" 23 | 24 | [package.metadata.docs.rs] 25 | rustdoc-args = ["--cfg", "web_sys_unstable_apis"] 26 | rustc-args = ["--cfg", "web_sys_unstable_apis"] 27 | 28 | [dependencies.web-sys] 29 | version = "0.3.77" 30 | features = [ 31 | "VideoDecoder", 32 | "VideoDecoderInit", 33 | "VideoDecoderConfig", 34 | "VideoFrame", 35 | "VideoColorSpace", 36 | "VideoColorSpaceInit", 37 | "EncodedVideoChunk", 38 | "EncodedVideoChunkInit", 39 | "EncodedVideoChunkType", 40 | "HardwareAcceleration", 41 | "VideoMatrixCoefficients", 42 | "VideoColorPrimaries", 43 | "VideoTransferCharacteristics", 44 | "VideoEncoder", 45 | "VideoEncoderInit", 46 | "VideoEncoderConfig", 47 | "VideoEncoderEncodeOptions", 48 | "LatencyMode", 49 | "AlphaOption", 50 | "EncodedAudioChunk", 51 | "EncodedAudioChunkInit", 52 | "EncodedAudioChunkType", 53 | "AudioData", 54 | "AudioDecoder", 55 | "AudioDecoderInit", 56 | "AudioDecoderConfig", 57 | "AudioEncoder", 58 | "AudioEncoderInit", 59 | "AudioEncoderConfig", 60 | "AudioSampleFormat", 61 | "AudioDataCopyToOptions", 62 | "AudioDataInit", 63 | "console", 64 | ] 65 | -------------------------------------------------------------------------------- /web-streams/src/reader.rs: -------------------------------------------------------------------------------- 1 | use std::marker::PhantomData; 2 | 3 | use js_sys::Reflect; 4 | use wasm_bindgen::prelude::*; 5 | use wasm_bindgen_futures::JsFuture; 6 | use web_sys::{js_sys, ReadableStream, ReadableStreamDefaultReader, ReadableStreamReadResult}; 7 | 8 | use crate::{Error, PromiseExt}; 9 | 10 | /// A wrapper around ReadableStream 11 | pub struct Reader { 12 | inner: ReadableStreamDefaultReader, 13 | 14 | // Keep the most recent promise to make `read` cancelable 15 | read: Option, 16 | 17 | _phantom: PhantomData, 18 | } 19 | 20 | impl Reader { 21 | /// Grab a lock on the given readable stream until dropped. 22 | pub fn new(stream: &ReadableStream) -> Result { 23 | let inner = stream.get_reader().unchecked_into(); 24 | Ok(Self { 25 | inner, 26 | read: None, 27 | _phantom: PhantomData, 28 | }) 29 | } 30 | 31 | /// Read the next element from the stream, returning None if the stream is done. 32 | pub async fn read(&mut self) -> Result, Error> { 33 | if self.read.is_none() { 34 | self.read = Some(self.inner.read()); 35 | } 36 | 37 | let result: ReadableStreamReadResult = JsFuture::from(self.read.as_ref().unwrap().clone()).await?.into(); 38 | self.read.take(); // Clear the promise on success 39 | 40 | if Reflect::get(&result, &"done".into())?.is_truthy() { 41 | return Ok(None); 42 | } 43 | 44 | let res = Reflect::get(&result, &"value".into())?.unchecked_into(); 45 | 46 | Ok(Some(res)) 47 | } 48 | 49 | /// Abort the stream early with the given reason. 50 | pub fn abort(&mut self, reason: &str) { 51 | let str = JsValue::from_str(reason); 52 | self.inner.cancel_with_reason(&str).ignore(); 53 | } 54 | 55 | pub async fn closed(&self) -> Result<(), Error> { 56 | JsFuture::from(self.inner.closed()).await?; 57 | Ok(()) 58 | } 59 | } 60 | 61 | impl Drop for Reader { 62 | /// Release the lock 63 | fn drop(&mut self) { 64 | self.inner.release_lock(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /web-message/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A crate for sending and receiving messages via `postMessage`. 2 | //! 3 | //! Any type that implements [Message] can be serialized and unserialized. 4 | //! Unlike using Serde for JSON encoding, this approach preserves [Transferable Objects](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects) and can avoid expensive allocations and copying. 5 | //! Unlike using #[wasm-bindgen], this approach works outside of the `wasm-bindgen` ABI, supporting more types (ex. named enum variants). 6 | //! 7 | //! For example, the main thread can send a [js_sys::ArrayBuffer] or a Web Worker without copying the data. 8 | //! If the WASM worker only needs to process a few header bytes, it can use the [js_sys::ArrayBuffer] instead of copying into a [Vec]. 9 | //! The resulting bytes can then be passed to [VideoDecoder](https://developer.mozilla.org/en-US/docs/Web/API/VideoDecoder) and the resulting [VideoFrame](https://developer.mozilla.org/en-US/docs/Web/API/VideoFrame) (transferable) can be posted back to the main thread. 10 | //! You can even pass around a [web_sys::MessagePort]! 11 | //! 12 | //! This crate is designed to be used in conjunction with the `web-message-derive` crate. 13 | //! We currently attempt parity with [ts-rs](https://docs.rs/ts-rs/latest/ts_rs/) so the resulting types can use `postMessage` directly from Typescript. 14 | //! 15 | //! ```rs 16 | //! // NOTE: This is not possible with `wasm-bindgen` or `wasm-bindgen-serde` 17 | //! #[derive(Message)] 18 | //! #[msg(tag = "command")] 19 | //! enum Command { 20 | //! Connect { 21 | //! url: String, 22 | //! }, 23 | //! Frame { 24 | //! keyframe: bool, 25 | //! payload: js_sys::ArrayBuffer, 26 | //! }, 27 | //! Close, 28 | //! } 29 | //! ``` 30 | //! 31 | //! Some transferable types are gated behind feature flags: 32 | //! 33 | 34 | // Required for derive to work. 35 | extern crate self as web_message; 36 | 37 | #[cfg(feature = "derive")] 38 | mod derive; 39 | #[cfg(feature = "derive")] 40 | pub use derive::*; 41 | 42 | mod error; 43 | mod message; 44 | 45 | pub use error::*; 46 | pub use message::*; 47 | -------------------------------------------------------------------------------- /web-codecs/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Unreleased] 8 | 9 | ## [0.3.7](https://github.com/kixelated/web-rs/compare/web-codecs-v0.3.6...web-codecs-v0.3.7) - 2025-05-19 10 | 11 | ### Other 12 | 13 | - And add dimensions to VideoFrame. ([#28](https://github.com/kixelated/web-rs/pull/28)) 14 | - Add PartialEq/Eq to Dimensions ([#26](https://github.com/kixelated/web-rs/pull/26)) 15 | 16 | ## [0.3.6](https://github.com/kixelated/web-rs/compare/web-codecs-v0.3.5...web-codecs-v0.3.6) - 2025-05-09 17 | 18 | ### Other 19 | 20 | - Add a bunch of stuff ([#24](https://github.com/kixelated/web-rs/pull/24)) 21 | - Add Just ([#18](https://github.com/kixelated/web-rs/pull/18)) 22 | - Make a few nits for the WebCodecs API. ([#20](https://github.com/kixelated/web-rs/pull/20)) 23 | - Initial audio support. ([#19](https://github.com/kixelated/web-rs/pull/19)) 24 | - Move moq-async to web-async. ([#14](https://github.com/kixelated/web-rs/pull/14)) 25 | 26 | ## [0.3.5](https://github.com/kixelated/web-rs/compare/web-codecs-v0.3.4...web-codecs-v0.3.5) - 2025-02-13 27 | 28 | ### Other 29 | 30 | - Remove the Timestamp wrapper. ([#12](https://github.com/kixelated/web-rs/pull/12)) 31 | 32 | ## [0.3.4](https://github.com/kixelated/web-rs/compare/web-codecs-v0.3.3...web-codecs-v0.3.4) - 2025-01-26 33 | 34 | ### Other 35 | 36 | - Remove our custom Duration time. ([#10](https://github.com/kixelated/web-rs/pull/10)) 37 | 38 | ## [0.3.3](https://github.com/kixelated/web-rs/compare/web-codecs-v0.3.2...web-codecs-v0.3.3) - 2025-01-10 39 | 40 | ### Other 41 | 42 | - Fix encoding and some bugs. ([#9](https://github.com/kixelated/web-rs/pull/9)) 43 | - Tabs r cool ([#8](https://github.com/kixelated/web-rs/pull/8)) 44 | - Try to fix cargo docs. 45 | - Merge in web-streams and basic encoder support ([#6](https://github.com/kixelated/web-rs/pull/6)) 46 | 47 | ## [0.3.2](https://github.com/kixelated/web-codecs-rs/compare/v0.3.1...v0.3.2) - 2024-11-26 48 | 49 | ### Other 50 | 51 | - Actually use DerefMut wrapper. ([#4](https://github.com/kixelated/web-codecs-rs/pull/4)) 52 | - Drop frames when done. ([#3](https://github.com/kixelated/web-codecs-rs/pull/3)) 53 | 54 | ## [0.3.0](https://github.com/kixelated/web-codecs-rs/compare/v0.2.0...v0.3.0) - 2024-11-23 55 | 56 | ### Other 57 | - Add missing GITHUB_TOKEN 58 | - Video prefix ([#1](https://github.com/kixelated/web-codecs-rs/pull/1)) 59 | - cargo fmt 60 | - Unstable flags? 61 | - Install WASM 62 | - Fix build 63 | - release-plz 64 | -------------------------------------------------------------------------------- /web-async/src/lock.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | use std::ops::{Deref, DerefMut}; 3 | 4 | // It's a cosmetic wrapper around Arc> on native platforms. 5 | // On WASM, it uses Rc> instead. 6 | pub struct Lock { 7 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 8 | inner: std::sync::Arc>, 9 | 10 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 11 | inner: std::rc::Rc>, 12 | } 13 | 14 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 15 | impl Lock { 16 | pub fn new(value: T) -> Self { 17 | Self { 18 | inner: std::sync::Arc::new(std::sync::Mutex::new(value)), 19 | } 20 | } 21 | } 22 | 23 | impl Lock { 24 | pub fn lock(&self) -> LockGuard { 25 | LockGuard::new(&self.inner) 26 | } 27 | 28 | pub fn downgrade(&self) -> LockWeak { 29 | LockWeak::new(&self.inner) 30 | } 31 | } 32 | 33 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 34 | impl Lock { 35 | pub fn new(value: T) -> Self { 36 | Self { 37 | inner: std::rc::Rc::new(std::cell::RefCell::new(value)), 38 | } 39 | } 40 | } 41 | 42 | impl Default for Lock { 43 | fn default() -> Self { 44 | Self::new(T::default()) 45 | } 46 | } 47 | 48 | impl Clone for Lock { 49 | fn clone(&self) -> Self { 50 | Self { 51 | inner: self.inner.clone(), 52 | } 53 | } 54 | } 55 | 56 | pub struct LockGuard<'a, T> { 57 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 58 | inner: std::sync::MutexGuard<'a, T>, 59 | 60 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 61 | inner: std::cell::RefMut<'a, T>, 62 | } 63 | 64 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 65 | impl<'a, T> LockGuard<'a, T> { 66 | fn new(inner: &'a std::sync::Arc>) -> Self { 67 | Self { 68 | inner: inner.lock().unwrap(), 69 | } 70 | } 71 | } 72 | 73 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 74 | impl<'a, T> LockGuard<'a, T> { 75 | fn new(inner: &'a std::rc::Rc>) -> Self { 76 | Self { 77 | inner: inner.borrow_mut(), 78 | } 79 | } 80 | } 81 | 82 | impl Deref for LockGuard<'_, T> { 83 | type Target = T; 84 | 85 | fn deref(&self) -> &Self::Target { 86 | &self.inner 87 | } 88 | } 89 | 90 | impl DerefMut for LockGuard<'_, T> { 91 | fn deref_mut(&mut self) -> &mut Self::Target { 92 | &mut self.inner 93 | } 94 | } 95 | 96 | impl fmt::Debug for LockGuard<'_, T> { 97 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 98 | self.inner.fmt(f) 99 | } 100 | } 101 | 102 | pub struct LockWeak { 103 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 104 | inner: std::sync::Weak>, 105 | 106 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 107 | inner: std::rc::Weak>, 108 | } 109 | 110 | #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] 111 | impl LockWeak { 112 | fn new(inner: &std::sync::Arc>) -> Self { 113 | Self { 114 | inner: std::sync::Arc::downgrade(inner), 115 | } 116 | } 117 | } 118 | 119 | #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] 120 | impl LockWeak { 121 | fn new(inner: &std::rc::Rc>) -> Self { 122 | Self { 123 | inner: std::rc::Rc::downgrade(inner), 124 | } 125 | } 126 | } 127 | 128 | impl LockWeak { 129 | pub fn upgrade(&self) -> Option> { 130 | Some(Lock { 131 | inner: self.inner.upgrade()?, 132 | }) 133 | } 134 | } 135 | 136 | impl Clone for LockWeak { 137 | fn clone(&self) -> Self { 138 | Self { 139 | inner: self.inner.clone(), 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /web-codecs/src/audio/decoder.rs: -------------------------------------------------------------------------------- 1 | use bytes::{Bytes, BytesMut}; 2 | use tokio::sync::{mpsc, watch}; 3 | use wasm_bindgen::prelude::*; 4 | 5 | use super::AudioData; 6 | use crate::{EncodedFrame, Error}; 7 | 8 | #[derive(Debug, Default, Clone)] 9 | pub struct AudioDecoderConfig { 10 | /// The codec mimetype string. 11 | pub codec: String, 12 | 13 | /// Some codec formats use a description to configure the decoder. 14 | pub description: Option, 15 | 16 | /// The number of channels in the audio. 17 | pub channel_count: u32, 18 | 19 | /// The sample rate of the audio. 20 | pub sample_rate: u32, 21 | } 22 | 23 | impl AudioDecoderConfig { 24 | pub fn new>(codec: T, channel_count: u32, sample_rate: u32) -> Self { 25 | Self { 26 | codec: codec.into(), 27 | channel_count, 28 | sample_rate, 29 | ..Default::default() 30 | } 31 | } 32 | 33 | /// Check if the configuration is supported by this browser. 34 | /// Returns an error if the configuration is invalid, and false if just unsupported. 35 | pub async fn is_supported(&self) -> Result { 36 | let res = 37 | wasm_bindgen_futures::JsFuture::from(web_sys::AudioDecoder::is_config_supported(&self.into())).await?; 38 | 39 | let supported = js_sys::Reflect::get(&res, &JsValue::from_str("supported")) 40 | .unwrap() 41 | .as_bool() 42 | .unwrap(); 43 | 44 | Ok(supported) 45 | } 46 | 47 | pub fn build(self) -> Result<(AudioDecoder, AudioDecoded), Error> { 48 | let (frames_tx, frames_rx) = mpsc::unbounded_channel(); 49 | let (closed_tx, closed_rx) = watch::channel(Ok(())); 50 | let closed_tx2 = closed_tx.clone(); 51 | 52 | let on_error = Closure::wrap(Box::new(move |e: JsValue| { 53 | closed_tx.send_replace(Err(Error::from(e))).ok(); 54 | }) as Box); 55 | 56 | let on_frame = Closure::wrap(Box::new(move |e: JsValue| { 57 | let frame: web_sys::AudioData = e.unchecked_into(); 58 | let frame = AudioData::from(frame); 59 | 60 | if frames_tx.send(frame).is_err() { 61 | closed_tx2.send_replace(Err(Error::Dropped)).ok(); 62 | } 63 | }) as Box); 64 | 65 | let init = web_sys::AudioDecoderInit::new(on_error.as_ref().unchecked_ref(), on_frame.as_ref().unchecked_ref()); 66 | let inner: web_sys::AudioDecoder = web_sys::AudioDecoder::new(&init).unwrap(); 67 | inner.configure(&(&self).into())?; 68 | 69 | let decoder = AudioDecoder { 70 | inner, 71 | on_error, 72 | on_frame, 73 | }; 74 | 75 | let decoded = AudioDecoded { 76 | frames: frames_rx, 77 | closed: closed_rx, 78 | }; 79 | 80 | Ok((decoder, decoded)) 81 | } 82 | } 83 | 84 | impl From<&AudioDecoderConfig> for web_sys::AudioDecoderConfig { 85 | fn from(this: &AudioDecoderConfig) -> Self { 86 | let config = web_sys::AudioDecoderConfig::new(&this.codec, this.channel_count, this.sample_rate); 87 | 88 | if let Some(description) = &this.description { 89 | config.set_description(&js_sys::Uint8Array::from(description.as_ref())); 90 | } 91 | 92 | config 93 | } 94 | } 95 | 96 | impl From for AudioDecoderConfig { 97 | fn from(this: web_sys::AudioDecoderConfig) -> Self { 98 | let description = this.get_description().map(|d| { 99 | // TODO: An ArrayBuffer, a TypedArray, or a DataView containing a sequence of codec-specific bytes, commonly known as "extradata". 100 | let buffer = js_sys::Uint8Array::new(&d); 101 | let size = buffer.byte_length() as usize; 102 | 103 | let mut payload = BytesMut::with_capacity(size); 104 | payload.resize(size, 0); 105 | buffer.copy_to(&mut payload); 106 | 107 | payload.freeze() 108 | }); 109 | 110 | let channels = this.get_number_of_channels(); 111 | let sample_rate = this.get_sample_rate(); 112 | 113 | Self { 114 | codec: this.get_codec(), 115 | description, 116 | channel_count: channels, 117 | sample_rate, 118 | } 119 | } 120 | } 121 | 122 | pub struct AudioDecoder { 123 | inner: web_sys::AudioDecoder, 124 | 125 | // These are held to avoid dropping them. 126 | #[allow(dead_code)] 127 | on_error: Closure, 128 | #[allow(dead_code)] 129 | on_frame: Closure, 130 | } 131 | 132 | impl AudioDecoder { 133 | pub fn decode(&self, frame: EncodedFrame) -> Result<(), Error> { 134 | let chunk_type = match frame.keyframe { 135 | true => web_sys::EncodedAudioChunkType::Key, 136 | false => web_sys::EncodedAudioChunkType::Delta, 137 | }; 138 | 139 | let chunk = web_sys::EncodedAudioChunkInit::new( 140 | &js_sys::Uint8Array::from(frame.payload.as_ref()), 141 | frame.timestamp.as_micros() as _, 142 | chunk_type, 143 | ); 144 | 145 | let chunk = web_sys::EncodedAudioChunk::new(&chunk)?; 146 | self.inner.decode(&chunk)?; 147 | 148 | Ok(()) 149 | } 150 | 151 | pub async fn flush(&self) -> Result<(), Error> { 152 | wasm_bindgen_futures::JsFuture::from(self.inner.flush()).await?; 153 | Ok(()) 154 | } 155 | 156 | pub fn queue_size(&self) -> u32 { 157 | self.inner.decode_queue_size() 158 | } 159 | } 160 | 161 | impl Drop for AudioDecoder { 162 | fn drop(&mut self) { 163 | let _ = self.inner.close(); 164 | } 165 | } 166 | 167 | pub struct AudioDecoded { 168 | frames: mpsc::UnboundedReceiver, 169 | closed: watch::Receiver>, 170 | } 171 | 172 | impl AudioDecoded { 173 | pub async fn next(&mut self) -> Result, Error> { 174 | tokio::select! { 175 | biased; 176 | frame = self.frames.recv() => Ok(frame), 177 | Ok(()) = self.closed.changed() => Err(self.closed.borrow().clone().err().unwrap()), 178 | } 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /web-codecs/src/audio/encoder.rs: -------------------------------------------------------------------------------- 1 | use std::{cell::RefCell, rc::Rc}; 2 | 3 | use tokio::sync::{mpsc, watch}; 4 | use wasm_bindgen::prelude::*; 5 | 6 | use crate::{EncodedFrame, Error}; 7 | 8 | use super::{AudioData, AudioDecoderConfig}; 9 | 10 | // TODO support the full specification: https://developer.mozilla.org/en-US/docs/Web/API/AudioEncoder/configure 11 | #[derive(Debug, Default, Clone)] 12 | pub struct AudioEncoderConfig { 13 | pub codec: String, 14 | pub channel_count: Option, 15 | pub sample_rate: Option, 16 | pub bitrate: Option, // bits per second 17 | } 18 | 19 | impl AudioEncoderConfig { 20 | pub fn new>(codec: T) -> Self { 21 | Self { 22 | codec: codec.into(), 23 | channel_count: None, 24 | sample_rate: None, 25 | bitrate: None, 26 | } 27 | } 28 | 29 | pub async fn is_supported(&self) -> Result { 30 | let res = 31 | wasm_bindgen_futures::JsFuture::from(web_sys::AudioEncoder::is_config_supported(&self.into())).await?; 32 | 33 | let supported = js_sys::Reflect::get(&res, &JsValue::from_str("supported")) 34 | .unwrap() 35 | .as_bool() 36 | .unwrap(); 37 | 38 | Ok(supported) 39 | } 40 | 41 | pub fn init(self) -> Result<(AudioEncoder, AudioEncoded), Error> { 42 | let (frames_tx, frames_rx) = mpsc::unbounded_channel(); 43 | let (closed_tx, closed_rx) = watch::channel(Ok(())); 44 | let config = Rc::new(RefCell::new(None)); 45 | 46 | let decoder = AudioEncoder::new(self, config.clone(), frames_tx, closed_tx)?; 47 | let decoded = AudioEncoded::new(config, frames_rx, closed_rx); 48 | 49 | Ok((decoder, decoded)) 50 | } 51 | } 52 | 53 | impl From<&AudioEncoderConfig> for web_sys::AudioEncoderConfig { 54 | fn from(this: &AudioEncoderConfig) -> Self { 55 | let config = web_sys::AudioEncoderConfig::new(&this.codec); 56 | 57 | if let Some(channels) = this.channel_count { 58 | config.set_number_of_channels(channels); 59 | } 60 | 61 | if let Some(sample_rate) = this.sample_rate { 62 | config.set_sample_rate(sample_rate); 63 | } 64 | 65 | if let Some(bit_rate) = this.bitrate { 66 | config.set_bitrate(bit_rate as f64); 67 | } 68 | 69 | config 70 | } 71 | } 72 | 73 | pub struct AudioEncoder { 74 | inner: web_sys::AudioEncoder, 75 | config: AudioEncoderConfig, 76 | 77 | // These are held to avoid dropping them. 78 | #[allow(dead_code)] 79 | on_error: Closure, 80 | #[allow(dead_code)] 81 | on_frame: Closure, 82 | } 83 | 84 | impl AudioEncoder { 85 | fn new( 86 | config: AudioEncoderConfig, 87 | on_config: Rc>>, 88 | on_frame: mpsc::UnboundedSender, 89 | on_error: watch::Sender>, 90 | ) -> Result { 91 | let on_error2 = on_error.clone(); 92 | let on_error = Closure::wrap(Box::new(move |e: JsValue| { 93 | on_error.send_replace(Err(Error::from(e))).ok(); 94 | }) as Box); 95 | 96 | let on_frame = Closure::wrap(Box::new(move |frame: JsValue, meta: JsValue| { 97 | // First parameter is the frame, second optional parameter is metadata. 98 | let frame: web_sys::EncodedAudioChunk = frame.unchecked_into(); 99 | let frame = EncodedFrame::from(frame); 100 | 101 | if let Ok(metadata) = meta.dyn_into::() { 102 | // TODO handle metadata 103 | if let Ok(config) = js_sys::Reflect::get(&metadata, &"decoderConfig".into()) { 104 | if !config.is_falsy() { 105 | let config: web_sys::AudioDecoderConfig = config.unchecked_into(); 106 | let config = AudioDecoderConfig::from(config); 107 | on_config.borrow_mut().replace(config); 108 | } 109 | } 110 | } 111 | 112 | if on_frame.send(frame).is_err() { 113 | on_error2.send_replace(Err(Error::Dropped)).ok(); 114 | } 115 | }) as Box); 116 | 117 | let init = web_sys::AudioEncoderInit::new(on_error.as_ref().unchecked_ref(), on_frame.as_ref().unchecked_ref()); 118 | let inner: web_sys::AudioEncoder = web_sys::AudioEncoder::new(&init).unwrap(); 119 | inner.configure(&(&config).into())?; 120 | 121 | Ok(Self { 122 | config, 123 | inner, 124 | on_error, 125 | on_frame, 126 | }) 127 | } 128 | 129 | pub fn encode(&mut self, frame: &AudioData) -> Result<(), Error> { 130 | self.inner.encode(frame)?; 131 | Ok(()) 132 | } 133 | 134 | pub fn queue_size(&self) -> u32 { 135 | self.inner.encode_queue_size() 136 | } 137 | 138 | pub fn config(&self) -> &AudioEncoderConfig { 139 | &self.config 140 | } 141 | 142 | pub async fn flush(&mut self) -> Result<(), Error> { 143 | wasm_bindgen_futures::JsFuture::from(self.inner.flush()).await?; 144 | Ok(()) 145 | } 146 | } 147 | 148 | impl Drop for AudioEncoder { 149 | fn drop(&mut self) { 150 | let _ = self.inner.close(); 151 | } 152 | } 153 | 154 | pub struct AudioEncoded { 155 | config: Rc>>, 156 | frames: mpsc::UnboundedReceiver, 157 | closed: watch::Receiver>, 158 | } 159 | 160 | impl AudioEncoded { 161 | fn new( 162 | config: Rc>>, 163 | frames: mpsc::UnboundedReceiver, 164 | closed: watch::Receiver>, 165 | ) -> Self { 166 | Self { config, frames, closed } 167 | } 168 | 169 | pub async fn frame(&mut self) -> Result, Error> { 170 | tokio::select! { 171 | biased; 172 | frame = self.frames.recv() => Ok(frame), 173 | Ok(()) = self.closed.changed() => Err(self.closed.borrow().clone().err().unwrap()), 174 | } 175 | } 176 | 177 | pub fn config(&self) -> Option { 178 | self.config.borrow().clone() 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /web-message/src/message.rs: -------------------------------------------------------------------------------- 1 | use js_sys::Array; 2 | use js_sys::wasm_bindgen; 3 | use wasm_bindgen::{JsCast, JsValue}; 4 | use web_sys::js_sys; 5 | 6 | use crate::Error; 7 | pub trait Message: Sized { 8 | // Serializes the message into a JsValue. 9 | // Any transferable fields are appended to the given array. 10 | fn into_message(self, transferable: &mut Array) -> JsValue; 11 | 12 | // Deserializes the message from a JsValue. 13 | fn from_message(message: JsValue) -> Result; 14 | } 15 | 16 | macro_rules! upstream { 17 | ($($t:ty),*) => { 18 | $( 19 | impl Message for $t { 20 | fn into_message(self, _transferable: &mut Array) -> JsValue { 21 | self.into() 22 | } 23 | 24 | fn from_message(message: JsValue) -> Result { 25 | Self::try_from(message).map_err(|_| Error::UnexpectedType) 26 | } 27 | } 28 | )* 29 | }; 30 | } 31 | 32 | // Macro for implementing Message for primitive casts supported in wasm-bindgen 33 | upstream!(String, f64, i128, i64, u128, u64); 34 | 35 | macro_rules! integer { 36 | ($($t:ty),*) => { 37 | $( 38 | impl Message for $t { 39 | fn into_message(self, _transferable: &mut Array) -> JsValue { 40 | self.into() 41 | } 42 | 43 | fn from_message(message: JsValue) -> Result { 44 | Ok(message.as_f64().ok_or(Error::UnexpectedType)? as $t) 45 | } 46 | } 47 | )* 48 | }; 49 | } 50 | 51 | // Macro for implementing Message for floating point types, less than Number.MAX_SAFE_INTEGER 52 | integer!(u32, i32, u16, i16, u8, i8); 53 | 54 | impl Message for bool { 55 | fn into_message(self, _transferable: &mut Array) -> JsValue { 56 | self.into() 57 | } 58 | 59 | fn from_message(message: JsValue) -> Result { 60 | message.as_bool().ok_or(Error::UnexpectedType) 61 | } 62 | } 63 | 64 | impl Message for Option { 65 | fn into_message(self, transferable: &mut Array) -> JsValue { 66 | match self { 67 | Some(value) => value.into_message(transferable), 68 | None => JsValue::NULL, 69 | } 70 | } 71 | 72 | fn from_message(message: JsValue) -> Result { 73 | Ok(match message.is_null() { 74 | true => None, 75 | false => Some(T::from_message(message)?), 76 | }) 77 | } 78 | } 79 | 80 | impl Message for Vec { 81 | fn into_message(self, transferable: &mut Array) -> JsValue { 82 | let array = Array::new(); 83 | for value in self { 84 | array.push(&value.into_message(transferable)); 85 | } 86 | array.into() 87 | } 88 | 89 | fn from_message(message: JsValue) -> Result { 90 | if !message.is_array() { 91 | return Err(Error::UnexpectedType); 92 | } 93 | 94 | let array = Array::from(&message); 95 | let mut values = Vec::with_capacity(array.length() as usize); 96 | for i in 0..array.length() { 97 | values.push(T::from_message(array.get(i))?); 98 | } 99 | Ok(values) 100 | } 101 | } 102 | 103 | impl Message for js_sys::ArrayBuffer { 104 | fn into_message(self, transferable: &mut Array) -> JsValue { 105 | transferable.push(&self); 106 | self.into() 107 | } 108 | 109 | fn from_message(message: JsValue) -> Result { 110 | message 111 | .dyn_into::() 112 | .map_err(|_| Error::UnexpectedType) 113 | } 114 | } 115 | 116 | macro_rules! typed_array { 117 | ($($t:ident),*,) => { 118 | $( 119 | impl Message for js_sys::$t { 120 | fn into_message(self, transferable: &mut Array) -> JsValue { 121 | transferable.push(&self.buffer()); 122 | self.into() 123 | } 124 | 125 | fn from_message(message: JsValue) -> Result { 126 | message 127 | .dyn_into::() 128 | .map_err(|_| Error::UnexpectedType) 129 | } 130 | } 131 | )* 132 | }; 133 | } 134 | 135 | // These are all the types that wrap an ArrayBuffer and can be transferred. 136 | typed_array!( 137 | Float32Array, 138 | Float64Array, 139 | Int8Array, 140 | Int16Array, 141 | Int32Array, 142 | Uint8Array, 143 | Uint8ClampedArray, 144 | Uint16Array, 145 | Uint32Array, 146 | BigInt64Array, 147 | BigUint64Array, 148 | ); 149 | 150 | macro_rules! transferable_feature { 151 | ($($feature:literal = $t:ident),* $(,)?) => { 152 | $( 153 | #[cfg(feature = $feature)] 154 | impl Message for web_sys::$t { 155 | fn into_message(self, transferable: &mut Array) -> JsValue { 156 | transferable.push(&self); 157 | self.into() 158 | } 159 | 160 | fn from_message(message: JsValue) -> Result { 161 | message 162 | .dyn_into::() 163 | .map_err(|_| Error::UnexpectedType) 164 | } 165 | } 166 | )* 167 | }; 168 | } 169 | 170 | // These feature names copy web_sys for all (currently) transferable types. 171 | // https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects 172 | transferable_feature!( 173 | "MessagePort" = MessagePort, 174 | "ReadableStream" = ReadableStream, 175 | "WritableStream" = WritableStream, 176 | "TransformStream" = TransformStream, 177 | "WebTransportReceiveStream" = WebTransportReceiveStream, 178 | "WebTransportSendStream" = WebTransportSendStream, 179 | "AudioData" = AudioData, 180 | "ImageBitmap" = ImageBitmap, 181 | "VideoFrame" = VideoFrame, 182 | "OffscreenCanvas" = OffscreenCanvas, 183 | "RtcDataChannel" = RtcDataChannel, 184 | //"MediaSourceHandle" = MediaSourceHandle, 185 | "MidiAccess" = MidiAccess, 186 | ); 187 | 188 | #[cfg(feature = "Url")] 189 | impl Message for url::Url { 190 | fn into_message(self, _transferable: &mut Array) -> JsValue { 191 | self.to_string().into() 192 | } 193 | 194 | fn from_message(message: JsValue) -> Result { 195 | let str = message.as_string().ok_or(Error::UnexpectedType)?; 196 | url::Url::parse(&str).map_err(Error::InvalidUrl) 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /web-codecs/src/audio/data.rs: -------------------------------------------------------------------------------- 1 | use std::ops::{Deref, DerefMut}; 2 | use std::time::Duration; 3 | 4 | use crate::{Error, Result, Timestamp}; 5 | 6 | pub use web_sys::AudioSampleFormat as AudioDataFormat; 7 | 8 | /// A wrapper around [web_sys::AudioData] that closes on Drop. 9 | // It's an option so `leak` can return the inner AudioData if needed. 10 | #[derive(Debug)] 11 | pub struct AudioData(Option); 12 | 13 | impl AudioData { 14 | /// A helper to construct AudioData in a more type-safe way. 15 | /// This currently only supports F32. 16 | pub fn new<'a>( 17 | channels: impl ExactSizeIterator, 18 | sample_rate: u32, 19 | timestamp: Timestamp, 20 | ) -> Result { 21 | let mut channels = channels.enumerate(); 22 | let channel_count = channels.size_hint().0; 23 | let (_, channel) = channels.next().ok_or(Error::NoChannels)?; 24 | 25 | let frame_count = channel.len(); 26 | let total_samples = channel_count * frame_count; 27 | 28 | // Annoyingly, we need to create a contiguous buffer for the data. 29 | let data = js_sys::Float32Array::new_with_length(total_samples as _); 30 | 31 | // Copy the first channel using a Float32Array as a view into the buffer. 32 | let slice = js_sys::Float32Array::new_with_byte_offset_and_length(&data.buffer(), 0, frame_count as _); 33 | slice.copy_from(channel); 34 | 35 | for (i, channel) in channels { 36 | // Copy the other channels using a Float32Array as a view into the buffer. 37 | let slice = js_sys::Float32Array::new_with_byte_offset_and_length( 38 | &data.buffer(), 39 | (i * frame_count) as u32, 40 | frame_count as _, 41 | ); 42 | slice.copy_from(channel); 43 | } 44 | 45 | let init = web_sys::AudioDataInit::new( 46 | &data, 47 | AudioDataFormat::F32Planar, 48 | channel_count as _, 49 | frame_count as _, 50 | sample_rate as _, 51 | timestamp.as_micros() as _, 52 | ); 53 | 54 | // Manually add `transfer` to the init options. 55 | // TODO Update web_sys to support this natively. 56 | // I'm not even sure if this works. 57 | let transfer = js_sys::Array::new(); 58 | transfer.push(&data.buffer()); 59 | js_sys::Reflect::set(&init, &js_sys::JsString::from("transfer"), &transfer)?; 60 | 61 | let audio_data = web_sys::AudioData::new(&init)?; 62 | Ok(Self(Some(audio_data))) 63 | } 64 | 65 | pub fn timestamp(&self) -> Timestamp { 66 | Timestamp::from_micros(self.0.as_ref().unwrap().timestamp() as _) 67 | } 68 | 69 | pub fn duration(&self) -> Duration { 70 | Duration::from_micros(self.0.as_ref().unwrap().duration() as _) 71 | } 72 | 73 | pub fn sample_rate(&self) -> u32 { 74 | self.0.as_ref().unwrap().sample_rate() as u32 75 | } 76 | 77 | pub fn append_to(&self, dst: &mut T, channel: usize, options: AudioCopyOptions) -> Result<()> { 78 | dst.append_to(self, channel, options) 79 | } 80 | 81 | pub fn copy_to(&self, dst: &mut T, channel: usize, options: AudioCopyOptions) -> Result<()> { 82 | dst.copy_to(self, channel, options) 83 | } 84 | 85 | pub fn leak(mut self) -> web_sys::AudioData { 86 | self.0.take().unwrap() 87 | } 88 | } 89 | 90 | impl Clone for AudioData { 91 | fn clone(&self) -> Self { 92 | Self(self.0.clone()) 93 | } 94 | } 95 | 96 | impl Deref for AudioData { 97 | type Target = web_sys::AudioData; 98 | 99 | fn deref(&self) -> &Self::Target { 100 | self.0.as_ref().unwrap() 101 | } 102 | } 103 | 104 | impl DerefMut for AudioData { 105 | fn deref_mut(&mut self) -> &mut Self::Target { 106 | self.0.as_mut().unwrap() 107 | } 108 | } 109 | 110 | // Make sure we close the frame on drop. 111 | impl Drop for AudioData { 112 | fn drop(&mut self) { 113 | if let Some(audio_data) = self.0.take() { 114 | audio_data.close(); 115 | } 116 | } 117 | } 118 | 119 | impl From for AudioData { 120 | fn from(this: web_sys::AudioData) -> Self { 121 | Self(Some(this)) 122 | } 123 | } 124 | 125 | pub trait AudioCopy { 126 | fn copy_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()>; 127 | } 128 | 129 | impl AudioCopy for [u8] { 130 | fn copy_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()> { 131 | let options = options.into_web_sys(channel); 132 | // NOTE: The format is unuset so it will default to the AudioData format. 133 | // This means you couldn't export as U8Planar for whatever that's worth... 134 | data.0.as_ref().unwrap().copy_to_with_u8_slice(self, &options)?; 135 | Ok(()) 136 | } 137 | } 138 | 139 | impl AudioCopy for [f32] { 140 | fn copy_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()> { 141 | let options = options.into_web_sys(channel); 142 | options.set_format(AudioDataFormat::F32Planar); 143 | 144 | // Cast from a f32 to a u8 slice. 145 | let bytes = bytemuck::cast_slice_mut(self); 146 | data.0.as_ref().unwrap().copy_to_with_u8_slice(bytes, &options)?; 147 | Ok(()) 148 | } 149 | } 150 | 151 | impl AudioCopy for js_sys::Uint8Array { 152 | fn copy_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()> { 153 | let options = options.into_web_sys(channel); 154 | data.0.as_ref().unwrap().copy_to_with_u8_array(self, &options)?; 155 | Ok(()) 156 | } 157 | } 158 | 159 | impl AudioCopy for js_sys::Float32Array { 160 | fn copy_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()> { 161 | let options = options.into_web_sys(channel); 162 | data.0.as_ref().unwrap().copy_to_with_buffer_source(self, &options)?; 163 | Ok(()) 164 | } 165 | } 166 | 167 | pub trait AudioAppend { 168 | fn append_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()>; 169 | } 170 | 171 | impl AudioAppend for Vec { 172 | fn append_to(&mut self, data: &AudioData, channel: usize, options: AudioCopyOptions) -> Result<()> { 173 | // TODO do unsafe stuff to avoid zeroing the buffer. 174 | let grow = options.count.unwrap_or(data.number_of_frames() as _) - options.offset; 175 | let offset = self.len(); 176 | self.resize(offset + grow, 0.0); 177 | 178 | let options = options.into_web_sys(channel); 179 | let bytes = bytemuck::cast_slice_mut(&mut self[offset..]); 180 | data.0.as_ref().unwrap().copy_to_with_u8_slice(bytes, &options)?; 181 | 182 | Ok(()) 183 | } 184 | } 185 | 186 | #[derive(Debug, Default)] 187 | pub struct AudioCopyOptions { 188 | pub offset: usize, // defaults to 0 189 | pub count: Option, // defaults to remainder 190 | } 191 | 192 | impl AudioCopyOptions { 193 | fn into_web_sys(self, channel: usize) -> web_sys::AudioDataCopyToOptions { 194 | let options = web_sys::AudioDataCopyToOptions::new(channel as _); 195 | options.set_frame_offset(self.offset as _); 196 | if let Some(count) = self.count { 197 | options.set_frame_count(count as _); 198 | } 199 | options 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /web-codecs/src/video/decoder.rs: -------------------------------------------------------------------------------- 1 | use bytes::{Bytes, BytesMut}; 2 | use tokio::sync::{mpsc, watch}; 3 | use wasm_bindgen::prelude::*; 4 | 5 | use super::{Dimensions, VideoColorSpaceConfig, VideoFrame}; 6 | use crate::{EncodedFrame, Error}; 7 | 8 | #[derive(Debug, Default, Clone)] 9 | pub struct VideoDecoderConfig { 10 | /// The codec mimetype string. 11 | pub codec: String, 12 | 13 | /// The resolution of the media. 14 | /// Neither width nor height can be zero. 15 | pub resolution: Option, 16 | 17 | /// The resolution that the media should be displayed at. 18 | /// Neither width nor height can be zero. 19 | pub display: Option, 20 | 21 | /// Color stuff. 22 | pub color_space: Option, 23 | 24 | /// Some codec formats use a description to configure the decoder. 25 | /// ex. For h264: 26 | /// - If present: AVC format, with the SPS/PPS in this description. 27 | /// - If absent: Annex-B format, with the SPS/PPS before each keyframe. 28 | pub description: Option, 29 | 30 | /// Optionally require or disable hardware acceleration. 31 | pub hardware_acceleration: Option, 32 | 33 | /// Optionally optimize for latency. 34 | pub latency_optimized: Option, 35 | } 36 | 37 | impl VideoDecoderConfig { 38 | pub fn new>(codec: T) -> Self { 39 | Self { 40 | codec: codec.into(), 41 | ..Default::default() 42 | } 43 | } 44 | 45 | /// Check if the configuration is supported by this browser. 46 | /// Returns an error if the configuration is invalid, and false if just unsupported. 47 | pub async fn is_supported(&self) -> Result { 48 | if self.resolution.is_none_or(|d| d.width == 0 || d.height == 0) { 49 | return Err(Error::InvalidDimensions); 50 | } 51 | 52 | if self.display.is_none_or(|d| d.width == 0 || d.height == 0) { 53 | return Err(Error::InvalidDimensions); 54 | } 55 | 56 | let res = 57 | wasm_bindgen_futures::JsFuture::from(web_sys::VideoDecoder::is_config_supported(&self.into())).await?; 58 | 59 | let supported = js_sys::Reflect::get(&res, &JsValue::from_str("supported")) 60 | .unwrap() 61 | .as_bool() 62 | .unwrap(); 63 | 64 | Ok(supported) 65 | } 66 | 67 | pub fn build(self) -> Result<(VideoDecoder, VideoDecoded), Error> { 68 | let (frames_tx, frames_rx) = mpsc::unbounded_channel(); 69 | let (closed_tx, closed_rx) = watch::channel(Ok(())); 70 | let closed_tx2 = closed_tx.clone(); 71 | 72 | let on_error = Closure::wrap(Box::new(move |e: JsValue| { 73 | closed_tx.send_replace(Err(Error::from(e))).ok(); 74 | }) as Box); 75 | 76 | let on_frame = Closure::wrap(Box::new(move |e: JsValue| { 77 | let frame: web_sys::VideoFrame = e.unchecked_into(); 78 | let frame = VideoFrame::from(frame); 79 | 80 | if frames_tx.send(frame).is_err() { 81 | closed_tx2.send_replace(Err(Error::Dropped)).ok(); 82 | } 83 | }) as Box); 84 | 85 | let init = web_sys::VideoDecoderInit::new(on_error.as_ref().unchecked_ref(), on_frame.as_ref().unchecked_ref()); 86 | let inner: web_sys::VideoDecoder = web_sys::VideoDecoder::new(&init).unwrap(); 87 | inner.configure(&(&self).into())?; 88 | 89 | let decoder = VideoDecoder { 90 | inner, 91 | on_error, 92 | on_frame, 93 | }; 94 | 95 | let decoded = VideoDecoded { 96 | frames: frames_rx, 97 | closed: closed_rx, 98 | }; 99 | 100 | Ok((decoder, decoded)) 101 | } 102 | } 103 | 104 | impl From<&VideoDecoderConfig> for web_sys::VideoDecoderConfig { 105 | fn from(this: &VideoDecoderConfig) -> Self { 106 | let config = web_sys::VideoDecoderConfig::new(&this.codec); 107 | 108 | if let Some(Dimensions { width, height }) = this.resolution { 109 | config.set_coded_width(width); 110 | config.set_coded_height(height); 111 | } 112 | 113 | if let Some(Dimensions { width, height }) = this.display { 114 | config.set_display_aspect_width(width); 115 | config.set_display_aspect_height(height); 116 | } 117 | 118 | if let Some(description) = &this.description { 119 | config.set_description(&js_sys::Uint8Array::from(description.as_ref())); 120 | } 121 | 122 | if let Some(color_space) = &this.color_space { 123 | config.set_color_space(&color_space.into()); 124 | } 125 | 126 | if let Some(preferred) = this.hardware_acceleration { 127 | config.set_hardware_acceleration(match preferred { 128 | true => web_sys::HardwareAcceleration::PreferHardware, 129 | false => web_sys::HardwareAcceleration::PreferSoftware, 130 | }); 131 | } 132 | 133 | if let Some(value) = this.latency_optimized { 134 | config.set_optimize_for_latency(value); 135 | } 136 | 137 | config 138 | } 139 | } 140 | 141 | impl From for VideoDecoderConfig { 142 | fn from(this: web_sys::VideoDecoderConfig) -> Self { 143 | let resolution = match (this.get_coded_width(), this.get_coded_height()) { 144 | (Some(width), Some(height)) if width != 0 && height != 0 => Some(Dimensions { width, height }), 145 | _ => None, 146 | }; 147 | 148 | let display = match (this.get_display_aspect_width(), this.get_display_aspect_height()) { 149 | (Some(width), Some(height)) if width != 0 && height != 0 => Some(Dimensions { width, height }), 150 | _ => None, 151 | }; 152 | 153 | let color_space = this.get_color_space().map(VideoColorSpaceConfig::from); 154 | 155 | let description = this.get_description().map(|d| { 156 | // TODO: An ArrayBuffer, a TypedArray, or a DataView containing a sequence of codec-specific bytes, commonly known as "extradata". 157 | let buffer = js_sys::Uint8Array::new(&d); 158 | let size = buffer.byte_length() as usize; 159 | 160 | let mut payload = BytesMut::with_capacity(size); 161 | payload.resize(size, 0); 162 | buffer.copy_to(&mut payload); 163 | 164 | payload.freeze() 165 | }); 166 | 167 | let hardware_acceleration = match this.get_hardware_acceleration() { 168 | Some(web_sys::HardwareAcceleration::PreferHardware) => Some(true), 169 | Some(web_sys::HardwareAcceleration::PreferSoftware) => Some(false), 170 | _ => None, 171 | }; 172 | 173 | let latency_optimized = this.get_optimize_for_latency(); 174 | 175 | Self { 176 | codec: this.get_codec(), 177 | resolution, 178 | display, 179 | color_space, 180 | description, 181 | hardware_acceleration, 182 | latency_optimized, 183 | } 184 | } 185 | } 186 | 187 | pub struct VideoDecoder { 188 | inner: web_sys::VideoDecoder, 189 | 190 | // These are held to avoid dropping them. 191 | #[allow(dead_code)] 192 | on_error: Closure, 193 | #[allow(dead_code)] 194 | on_frame: Closure, 195 | } 196 | 197 | impl VideoDecoder { 198 | pub fn decode(&self, frame: EncodedFrame) -> Result<(), Error> { 199 | let chunk_type = match frame.keyframe { 200 | true => web_sys::EncodedVideoChunkType::Key, 201 | false => web_sys::EncodedVideoChunkType::Delta, 202 | }; 203 | 204 | let chunk = web_sys::EncodedVideoChunkInit::new( 205 | &js_sys::Uint8Array::from(frame.payload.as_ref()), 206 | frame.timestamp.as_micros() as _, 207 | chunk_type, 208 | ); 209 | 210 | let chunk = web_sys::EncodedVideoChunk::new(&chunk)?; 211 | self.inner.decode(&chunk)?; 212 | 213 | Ok(()) 214 | } 215 | 216 | pub async fn flush(&self) -> Result<(), Error> { 217 | wasm_bindgen_futures::JsFuture::from(self.inner.flush()).await?; 218 | Ok(()) 219 | } 220 | 221 | pub fn queue_size(&self) -> u32 { 222 | self.inner.decode_queue_size() 223 | } 224 | } 225 | 226 | impl Drop for VideoDecoder { 227 | fn drop(&mut self) { 228 | let _ = self.inner.close(); 229 | } 230 | } 231 | 232 | pub struct VideoDecoded { 233 | frames: mpsc::UnboundedReceiver, 234 | closed: watch::Receiver>, 235 | } 236 | 237 | impl VideoDecoded { 238 | pub async fn next(&mut self) -> Result, Error> { 239 | tokio::select! { 240 | biased; 241 | frame = self.frames.recv() => Ok(frame), 242 | Ok(()) = self.closed.changed() => Err(self.closed.borrow().clone().err().unwrap()), 243 | } 244 | } 245 | } 246 | -------------------------------------------------------------------------------- /web-message-derive/src/lib.rs: -------------------------------------------------------------------------------- 1 | use proc_macro::TokenStream; 2 | use quote::quote; 3 | use syn::{DeriveInput, Fields, parse_macro_input}; 4 | 5 | #[proc_macro_derive(Message, attributes(msg))] 6 | pub fn derive_message(input: TokenStream) -> TokenStream { 7 | let input = parse_macro_input!(input as DeriveInput); 8 | let ident = &input.ident; 9 | 10 | let output = match &input.data { 11 | syn::Data::Struct(data) => expand_struct(ident, &data.fields), 12 | syn::Data::Enum(data_enum) => expand_enum(ident, &data_enum.variants), 13 | _ => panic!("Message only supports structs and enums (for now)"), 14 | }; 15 | 16 | output.into() 17 | } 18 | 19 | fn expand_struct(ident: &syn::Ident, fields: &Fields) -> proc_macro2::TokenStream { 20 | let field_inits = fields.iter().map(|f| { 21 | let name = f.ident.as_ref().unwrap(); 22 | let name_str = name.to_string(); 23 | 24 | quote! { 25 | #name: ::web_message::Message::from_message(::web_sys::js_sys::Reflect::get(&obj, &#name_str.into()).map_err(|_| ::web_message::Error::MissingField(#name_str))?) 26 | .map_err(|_| ::web_message::Error::InvalidField(#name_str))? 27 | } 28 | }); 29 | 30 | let field_assignments = fields.iter().map(|f| { 31 | let name = f.ident.as_ref().unwrap(); 32 | let name_str = name.to_string(); 33 | 34 | quote! { 35 | ::web_sys::js_sys::Reflect::set(&obj, &#name_str.into(), &self.#name.into()).unwrap(); 36 | } 37 | }); 38 | 39 | quote! { 40 | impl ::web_message::Message for #ident { 41 | fn from_message(message: ::web_sys::js_sys::wasm_bindgen::JsValue) -> Result { 42 | let obj = web_sys::js_sys::Object::try_from(&message).ok_or(::web_message::Error::UnexpectedType)?; 43 | Ok(Self { 44 | #(#field_inits),* 45 | }) 46 | } 47 | 48 | fn into_message(self, _transferable: &mut ::web_sys::js_sys::Array) -> ::web_sys::js_sys::wasm_bindgen::JsValue { 49 | let obj = ::web_sys::js_sys::Object::new(); 50 | #(#field_assignments)* 51 | obj.into() 52 | } 53 | } 54 | } 55 | } 56 | 57 | fn expand_enum( 58 | enum_ident: &syn::Ident, 59 | variants: &syn::punctuated::Punctuated, 60 | ) -> proc_macro2::TokenStream { 61 | let from_obj= variants.iter().filter_map(|variant| { 62 | let variant_ident = &variant.ident; 63 | let variant_str = variant_ident.to_string(); 64 | 65 | match &variant.fields { 66 | Fields::Named(fields_named) => { 67 | let field_assignments = fields_named.named.iter().map(|f| { 68 | let name = f.ident.as_ref().unwrap(); 69 | let name_str = name.to_string(); 70 | 71 | quote! { 72 | #name: ::web_message::Message::from_message(::web_sys::js_sys::Reflect::get(&val, &#name_str.into()).map_err(|_| ::web_message::Error::MissingField(#name_str))?) 73 | .map_err(|_| ::web_message::Error::InvalidField(#name_str))? 74 | } 75 | }); 76 | 77 | Some(quote! { 78 | #variant_str if val.is_object() => { 79 | Ok(#enum_ident::#variant_ident { 80 | #(#field_assignments),* 81 | }) 82 | } 83 | #variant_str => Err(::web_message::Error::UnexpectedType), 84 | }) 85 | } 86 | 87 | Fields::Unit => None, 88 | 89 | Fields::Unnamed(fields_unnamed) if fields_unnamed.unnamed.len() == 1 => { 90 | Some(quote! { 91 | #variant_str => Ok(#enum_ident::#variant_ident(::web_message::Message::from_message(val)?)), 92 | }) 93 | }, 94 | 95 | Fields::Unnamed(fields_unnamed) => { 96 | let fields_count = fields_unnamed.unnamed.len() as u32; 97 | let field_assignments = (0..fields_count).map(|i| { 98 | quote! { 99 | ::web_message::Message::from_message(arr.get(#i)) 100 | .map_err(|_| ::web_message::Error::InvalidField(stringify!(#i)))? 101 | } 102 | }); 103 | 104 | Some(quote! { 105 | #variant_str if val.is_array() => { 106 | let arr = ::web_sys::js_sys::Array::from(&val); 107 | if arr.length() != #fields_count { 108 | return Err(::web_message::Error::UnexpectedLength); 109 | } 110 | 111 | Ok(#enum_ident::#variant_ident ( 112 | #(#field_assignments),* 113 | )) 114 | } 115 | #variant_str => Err(::web_message::Error::UnexpectedType), 116 | }) 117 | } 118 | } 119 | }); 120 | 121 | let from_string = variants.iter().filter_map(|variant| { 122 | let variant_ident = &variant.ident; 123 | let variant_str = variant_ident.to_string(); 124 | 125 | if let Fields::Unit = &variant.fields { 126 | Some(quote! { 127 | #variant_str => Ok(#enum_ident::#variant_ident), 128 | }) 129 | } else { 130 | None 131 | } 132 | }); 133 | 134 | let into_matches = variants.iter().map(|variant| { 135 | let variant_ident = &variant.ident; 136 | let variant_str = variant_ident.to_string(); 137 | 138 | match &variant.fields { 139 | Fields::Named(fields_named) => { 140 | let field_names = fields_named.named.iter().map(|f| f.ident.as_ref().unwrap()); 141 | 142 | let set_fields = fields_named.named.iter().map(|f| { 143 | let name = f.ident.as_ref().unwrap(); 144 | let name_str = name.to_string(); 145 | 146 | quote! { 147 | ::web_sys::js_sys::Reflect::set(&inner, &#name_str.into(), &#name.into_message(_transferable)).unwrap(); 148 | } 149 | }); 150 | 151 | quote! { 152 | #enum_ident::#variant_ident { #(#field_names),* } => { 153 | let obj = ::web_sys::js_sys::Object::new(); 154 | let inner = ::web_sys::js_sys::Object::new(); 155 | #(#set_fields)* 156 | ::web_sys::js_sys::Reflect::set(&obj, &#variant_str.into(), &inner.into()).unwrap(); 157 | obj.into() 158 | } 159 | } 160 | } 161 | Fields::Unit => { 162 | quote! { 163 | #enum_ident::#variant_ident => #variant_str.into() 164 | } 165 | } 166 | Fields::Unnamed(fields_unnamed) if fields_unnamed.unnamed.len() == 1 => { 167 | quote! { 168 | #enum_ident::#variant_ident(v) => { 169 | let obj = ::web_sys::js_sys::Object::new(); 170 | ::web_sys::js_sys::Reflect::set(&obj, &#variant_str.into(), &v.into_message(_transferable)).unwrap(); 171 | obj.into() 172 | } 173 | } 174 | } 175 | Fields::Unnamed(fields_unnamed) => { 176 | let fields_count = fields_unnamed.unnamed.len(); 177 | let field_idents: Vec<_> = (0..fields_count) 178 | .map(|i| syn::Ident::new(&format!("field_{}", i), proc_macro2::Span::call_site())) 179 | .collect(); 180 | 181 | let set_fields = field_idents.iter().map(|f| { 182 | quote! { 183 | inner.push(&#f.into_message(_transferable)); 184 | } 185 | }); 186 | 187 | quote! { 188 | #enum_ident::#variant_ident(#(#field_idents),*) => { 189 | let obj = ::web_sys::js_sys::Object::new(); 190 | let inner = ::web_sys::js_sys::Array::new(); 191 | #(#set_fields)* 192 | ::web_sys::js_sys::Reflect::set(&obj, &#variant_str.into(), &inner.into()).unwrap(); 193 | obj.into() 194 | } 195 | } 196 | } 197 | } 198 | }); 199 | 200 | quote! { 201 | impl ::web_message::Message for #enum_ident { 202 | fn from_message(message: ::web_sys::js_sys::wasm_bindgen::JsValue) -> ::std::result::Result { 203 | if let Some(s) = message.as_string() { 204 | match s.as_str() { 205 | #(#from_string)* 206 | _ => Err(::web_message::Error::UnknownTag), 207 | } 208 | } else if let Some(obj) = web_sys::js_sys::Object::try_from(&message) { 209 | let keys = web_sys::js_sys::Object::keys(&obj); 210 | if keys.length() != 1 { 211 | return Err(::web_message::Error::UnexpectedType); 212 | } 213 | 214 | let tag = keys.get(0); 215 | let tag_str = tag.as_string().ok_or(::web_message::Error::UnexpectedType)?; 216 | 217 | let val = ::web_sys::js_sys::Reflect::get(&obj, &tag).unwrap(); 218 | 219 | match tag_str.as_str() { 220 | #(#from_obj)* 221 | _ => Err(::web_message::Error::UnknownTag), 222 | } 223 | } else { 224 | Err(::web_message::Error::UnexpectedType) 225 | } 226 | } 227 | 228 | fn into_message(self, _transferable: &mut ::web_sys::js_sys::Array) -> ::web_sys::js_sys::wasm_bindgen::JsValue { 229 | match self { 230 | #(#into_matches),* 231 | } 232 | } 233 | } 234 | } 235 | } 236 | -------------------------------------------------------------------------------- /web-codecs/src/video/encoder.rs: -------------------------------------------------------------------------------- 1 | use std::{cell::RefCell, rc::Rc, time::Duration}; 2 | 3 | use tokio::sync::{mpsc, watch}; 4 | use wasm_bindgen::prelude::*; 5 | 6 | use crate::{EncodedFrame, Error, Timestamp}; 7 | 8 | use super::{Dimensions, VideoDecoderConfig, VideoFrame}; 9 | 10 | use derive_more::Display; 11 | 12 | #[derive(Debug, Display, Clone, Copy)] 13 | pub enum VideoBitrateMode { 14 | #[display("constant")] 15 | Constant, 16 | 17 | #[display("variable")] 18 | Variable, 19 | 20 | #[display("quantizer")] 21 | Quantizer, 22 | } 23 | 24 | #[derive(Debug, Default, Clone)] 25 | pub struct VideoEncoderConfig { 26 | pub codec: String, 27 | pub resolution: Dimensions, 28 | pub display: Option, 29 | pub hardware_acceleration: Option, 30 | pub latency_optimized: Option, 31 | pub bitrate: Option, // bits per second 32 | pub framerate: Option, // frames per second 33 | pub alpha_preserved: Option, // keep alpha channel 34 | pub scalability_mode: Option, 35 | pub bitrate_mode: Option, 36 | 37 | // NOTE: This is a custom configuration 38 | /// The maximum duration of a Group of Pictures (GOP) before forcing a new keyframe. 39 | pub max_gop_duration: Option, // seconds 40 | } 41 | 42 | impl VideoEncoderConfig { 43 | pub fn new>(codec: T, resolution: Dimensions) -> Self { 44 | Self { 45 | codec: codec.into(), 46 | resolution, 47 | display: None, 48 | hardware_acceleration: None, 49 | latency_optimized: None, 50 | bitrate: None, 51 | framerate: None, 52 | alpha_preserved: None, 53 | scalability_mode: None, 54 | bitrate_mode: None, 55 | max_gop_duration: None, 56 | } 57 | } 58 | 59 | pub async fn is_supported(&self) -> Result { 60 | let res = 61 | wasm_bindgen_futures::JsFuture::from(web_sys::VideoEncoder::is_config_supported(&self.into())).await?; 62 | 63 | let supported = js_sys::Reflect::get(&res, &JsValue::from_str("supported")) 64 | .unwrap() 65 | .as_bool() 66 | .unwrap(); 67 | 68 | Ok(supported) 69 | } 70 | 71 | pub fn is_valid(&self) -> Result<(), Error> { 72 | if self.resolution.width == 0 || self.resolution.height == 0 { 73 | return Err(Error::InvalidDimensions); 74 | } 75 | 76 | if let Some(display) = self.display { 77 | if display.width == 0 || display.height == 0 { 78 | return Err(Error::InvalidDimensions); 79 | } 80 | } 81 | 82 | Ok(()) 83 | } 84 | 85 | pub fn init(self) -> Result<(VideoEncoder, VideoEncoded), Error> { 86 | let (frames_tx, frames_rx) = mpsc::unbounded_channel(); 87 | let (closed_tx, closed_rx) = watch::channel(Ok(())); 88 | let config = Rc::new(RefCell::new(None)); 89 | 90 | let decoder = VideoEncoder::new(self, config.clone(), frames_tx, closed_tx)?; 91 | let decoded = VideoEncoded::new(config, frames_rx, closed_rx); 92 | 93 | Ok((decoder, decoded)) 94 | } 95 | } 96 | 97 | impl From<&VideoEncoderConfig> for web_sys::VideoEncoderConfig { 98 | fn from(this: &VideoEncoderConfig) -> Self { 99 | let config = web_sys::VideoEncoderConfig::new(&this.codec, this.resolution.height, this.resolution.width); 100 | 101 | if let Some(Dimensions { width, height }) = this.display { 102 | config.set_display_height(height); 103 | config.set_display_width(width); 104 | } 105 | 106 | if let Some(preferred) = this.hardware_acceleration { 107 | config.set_hardware_acceleration(match preferred { 108 | true => web_sys::HardwareAcceleration::PreferHardware, 109 | false => web_sys::HardwareAcceleration::PreferSoftware, 110 | }); 111 | } 112 | 113 | if let Some(value) = this.latency_optimized { 114 | config.set_latency_mode(match value { 115 | true => web_sys::LatencyMode::Realtime, 116 | false => web_sys::LatencyMode::Quality, 117 | }); 118 | } 119 | 120 | if let Some(value) = this.bitrate { 121 | config.set_bitrate(value as f64); 122 | } 123 | 124 | if let Some(value) = this.framerate { 125 | config.set_framerate(value); 126 | } 127 | 128 | if let Some(value) = this.alpha_preserved { 129 | config.set_alpha(match value { 130 | true => web_sys::AlphaOption::Keep, 131 | false => web_sys::AlphaOption::Discard, 132 | }); 133 | } 134 | 135 | if let Some(value) = &this.scalability_mode { 136 | config.set_scalability_mode(value); 137 | } 138 | 139 | if let Some(_value) = &this.bitrate_mode { 140 | // TODO not supported yet 141 | } 142 | 143 | config 144 | } 145 | } 146 | 147 | #[derive(Debug, Default)] 148 | pub struct VideoEncodeOptions { 149 | // Force or deny a key frame. 150 | pub key_frame: Option, 151 | // TODO 152 | // pub quantizer: Option, 153 | } 154 | 155 | pub struct VideoEncoder { 156 | inner: web_sys::VideoEncoder, 157 | config: VideoEncoderConfig, 158 | 159 | last_keyframe: Rc>>, 160 | 161 | // These are held to avoid dropping them. 162 | #[allow(dead_code)] 163 | on_error: Closure, 164 | #[allow(dead_code)] 165 | on_frame: Closure, 166 | } 167 | 168 | impl VideoEncoder { 169 | fn new( 170 | config: VideoEncoderConfig, 171 | on_config: Rc>>, 172 | on_frame: mpsc::UnboundedSender, 173 | on_error: watch::Sender>, 174 | ) -> Result { 175 | let last_keyframe = Rc::new(RefCell::new(None)); 176 | let last_keyframe2 = last_keyframe.clone(); 177 | 178 | let on_error2 = on_error.clone(); 179 | let on_error = Closure::wrap(Box::new(move |e: JsValue| { 180 | on_error.send_replace(Err(Error::from(e))).ok(); 181 | }) as Box); 182 | 183 | let on_frame = Closure::wrap(Box::new(move |frame: JsValue, meta: JsValue| { 184 | // First parameter is the frame, second optional parameter is metadata. 185 | let frame: web_sys::EncodedVideoChunk = frame.unchecked_into(); 186 | let frame = EncodedFrame::from(frame); 187 | 188 | if let Ok(metadata) = meta.dyn_into::() { 189 | // TODO handle metadata 190 | if let Ok(config) = js_sys::Reflect::get(&metadata, &"decoderConfig".into()) { 191 | if !config.is_falsy() { 192 | let config: web_sys::VideoDecoderConfig = config.unchecked_into(); 193 | let config = VideoDecoderConfig::from(config); 194 | on_config.borrow_mut().replace(config); 195 | } 196 | } 197 | } 198 | 199 | if frame.keyframe { 200 | let mut last_keyframe = last_keyframe2.borrow_mut(); 201 | if frame.timestamp > last_keyframe.unwrap_or_default() { 202 | *last_keyframe = Some(frame.timestamp); 203 | } 204 | } 205 | 206 | if on_frame.send(frame).is_err() { 207 | on_error2.send_replace(Err(Error::Dropped)).ok(); 208 | } 209 | }) as Box); 210 | 211 | let init = web_sys::VideoEncoderInit::new(on_error.as_ref().unchecked_ref(), on_frame.as_ref().unchecked_ref()); 212 | let inner: web_sys::VideoEncoder = web_sys::VideoEncoder::new(&init).unwrap(); 213 | inner.configure(&(&config).into())?; 214 | 215 | Ok(Self { 216 | config, 217 | inner, 218 | last_keyframe, 219 | on_error, 220 | on_frame, 221 | }) 222 | } 223 | 224 | pub fn encode(&mut self, frame: &VideoFrame, options: VideoEncodeOptions) -> Result<(), Error> { 225 | let o = web_sys::VideoEncoderEncodeOptions::new(); 226 | 227 | if let Some(key_frame) = options.key_frame { 228 | o.set_key_frame(key_frame); 229 | } else if let Some(max_gop_duration) = self.config.max_gop_duration { 230 | let timestamp = frame.timestamp(); 231 | let mut last_keyframe = self.last_keyframe.borrow_mut(); 232 | 233 | let duration = timestamp - last_keyframe.unwrap_or_default(); 234 | if duration >= max_gop_duration { 235 | o.set_key_frame(true); 236 | } 237 | 238 | *last_keyframe = Some(timestamp); 239 | } 240 | 241 | self.inner.encode_with_options(frame, &o)?; 242 | 243 | Ok(()) 244 | } 245 | 246 | pub fn queue_size(&self) -> u32 { 247 | self.inner.encode_queue_size() 248 | } 249 | 250 | pub fn config(&self) -> &VideoEncoderConfig { 251 | &self.config 252 | } 253 | 254 | pub async fn flush(&mut self) -> Result<(), Error> { 255 | wasm_bindgen_futures::JsFuture::from(self.inner.flush()).await?; 256 | Ok(()) 257 | } 258 | } 259 | 260 | impl Drop for VideoEncoder { 261 | fn drop(&mut self) { 262 | let _ = self.inner.close(); 263 | } 264 | } 265 | 266 | pub struct VideoEncoded { 267 | config: Rc>>, 268 | frames: mpsc::UnboundedReceiver, 269 | closed: watch::Receiver>, 270 | } 271 | 272 | impl VideoEncoded { 273 | fn new( 274 | config: Rc>>, 275 | frames: mpsc::UnboundedReceiver, 276 | closed: watch::Receiver>, 277 | ) -> Self { 278 | Self { config, frames, closed } 279 | } 280 | 281 | pub async fn frame(&mut self) -> Result, Error> { 282 | tokio::select! { 283 | biased; 284 | frame = self.frames.recv() => Ok(frame), 285 | Ok(()) = self.closed.changed() => Err(self.closed.borrow().clone().err().unwrap()), 286 | } 287 | } 288 | 289 | /// Returns the decoder config, after the first frame has been encoded. 290 | pub fn config(&self) -> Option { 291 | self.config.borrow().clone() 292 | } 293 | } 294 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------