Skip to main content

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/// Names include profile/level details for scalability.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum VideoCodec {
28    /// Unspecified codec - skip decoding.
29    Unspecified,
30    /// VP8 codec - no profile variants.
31    Vp8,
32    /// VP9 Profile 0, Level 1.0, 8-bit (vp09.00.10.08).
33    Vp9Profile0Level10Bit8,
34    /// A mock decoder that does nothing, for testing and simulation.
35    Mock,
36}
37
38impl VideoCodec {
39    /// Returns the WebCodecs codec string for this codec, or None for Unspecified.
40    pub fn as_webcodecs_str(&self) -> Option<&'static str> {
41        match self {
42            VideoCodec::Unspecified => None,
43            VideoCodec::Vp8 => Some("vp8"),
44            VideoCodec::Vp9Profile0Level10Bit8 => Some("vp09.00.10.08"),
45            VideoCodec::Mock => Some("vp8"), // Fallback for testing
46        }
47    }
48}
49
50/// Represents a fully decoded frame, ready for rendering.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct DecodedFrame {
53    pub sequence_number: u64,
54    pub width: u32,
55    pub height: u32,
56    // In a real implementation, this would hold image planes (e.g., YUV data).
57    pub data: Vec<u8>,
58}
59
60/// A trait that abstracts over the platform-specific decoder implementation
61/// (e.g., `std::thread` on native, Web Workers + WebCodecs in WASM).
62pub trait Decodable: Send + Sync {
63    /// The type of frame passed to the callback when a frame is decoded.
64    type Frame;
65
66    /// Creates a new decoder and starts its underlying thread or worker.
67    /// The `on_decoded_frame` callback will be invoked whenever a frame is successfully decoded.
68    fn new(codec: VideoCodec, on_decoded_frame: Box<dyn Fn(Self::Frame) + Send + Sync>) -> Self
69    where
70        Self: Sized;
71
72    /// Sends a raw frame buffer to the decoder for processing.
73    fn decode(&self, frame: FrameBuffer);
74}
75
76// Conditionally compile and expose the native implementation
77#[cfg(feature = "native")]
78mod native;
79#[cfg(feature = "native")]
80pub use self::native::NativeDecoder as Decoder;
81
82// Conditionally compile and expose the WASM implementation
83#[cfg(feature = "wasm")]
84mod wasm;
85#[cfg(feature = "wasm")]
86pub use self::wasm::WasmDecoder; // Export WasmDecoder directly for VideoFrame callbacks