videocall_codecs/decoder/
mod.rs

1/*
2 * Copyright 2025 Security Union LLC
3 *
4 * Licensed under either of
5 *
6 * * Apache License, Version 2.0
7 *   (http://www.apache.org/licenses/LICENSE-2.0)
8 * * MIT license
9 *   (http://opensource.org/licenses/MIT)
10 *
11 * at your option.
12 *
13 * Unless you explicitly state otherwise, any contribution intentionally
14 * submitted for inclusion in the work by you, as defined in the Apache-2.0
15 * license, shall be dual licensed as above, without any additional terms or
16 * conditions.
17 */
18
19//! The common interface for platform-specific decoders.
20
21use crate::frame::FrameBuffer;
22use serde::{Deserialize, Serialize};
23
24/// An enumeration of the supported video codecs.
25#[derive(Debug, Clone, Copy)]
26pub enum VideoCodec {
27    /// VP9 codec, using libvpx.
28    VP9,
29    /// A mock decoder that does nothing, for testing and simulation.
30    Mock,
31}
32
33/// Represents a fully decoded frame, ready for rendering.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct DecodedFrame {
36    pub sequence_number: u64,
37    pub width: u32,
38    pub height: u32,
39    // In a real implementation, this would hold image planes (e.g., YUV data).
40    pub data: Vec<u8>,
41}
42
43/// A trait that abstracts over the platform-specific decoder implementation
44/// (e.g., `std::thread` on native, Web Workers + WebCodecs in WASM).
45pub trait Decodable: Send + Sync {
46    /// The type of frame passed to the callback when a frame is decoded.
47    type Frame;
48
49    /// Creates a new decoder and starts its underlying thread or worker.
50    /// The `on_decoded_frame` callback will be invoked whenever a frame is successfully decoded.
51    fn new(codec: VideoCodec, on_decoded_frame: Box<dyn Fn(Self::Frame) + Send + Sync>) -> Self
52    where
53        Self: Sized;
54
55    /// Sends a raw frame buffer to the decoder for processing.
56    fn decode(&self, frame: FrameBuffer);
57}
58
59// Conditionally compile and expose the native implementation
60#[cfg(feature = "native")]
61mod native;
62#[cfg(feature = "native")]
63pub use self::native::NativeDecoder as Decoder;
64
65// Conditionally compile and expose the WASM implementation
66#[cfg(feature = "wasm")]
67mod wasm;
68#[cfg(feature = "wasm")]
69pub use self::wasm::WasmDecoder; // Export WasmDecoder directly for VideoFrame callbacks