Skip to main content

saorsa_webrtc_codecs/
lib.rs

1#![deny(clippy::panic)]
2#![deny(clippy::unwrap_used)]
3#![deny(clippy::expect_used)]
4#![deny(unsafe_code)]
5
6//! Video and audio codec implementations
7//!
8//! # Implementation Status
9//!
10//! The current codec implementations are **stub/simulation implementations** designed for:
11//! - Development and testing without external codec libraries
12//! - API design validation and interface stabilization
13//! - Performance testing of the transport layer
14//!
15//! ## OpenH264 (Video)
16//! - **Current**: Simulation using compression (achieves ~25% size reduction for testing)
17//! - **Production**: Will integrate with real openh264 crate when `h264` feature is enabled
18//! - **Status**: Safe for development/testing, not for production video calls
19//!
20//! ## Opus (Audio)
21//! - **Current**: Simulation with frame size validation and format conversion
22//! - **Production**: Will integrate with real opus crate when `opus` feature is enabled
23//! - **Status**: Safe for development/testing, not for production audio calls
24//!
25//! ## Migration Path
26//!
27//! To use real codecs in production:
28//! 1. Enable the respective features: `features = ["h264", "opus"]`
29//! 2. Replace stub implementations in `openh264.rs` and `opus.rs`
30//! 3. Run integration tests with actual codec libraries
31//!
32//! The stub implementations maintain the same API surface, so migration is transparent to users.
33
34pub mod openh264;
35pub mod opus;
36
37use bytes::Bytes;
38
39/// Codec error types
40#[derive(Debug, thiserror::Error)]
41pub enum CodecError {
42    #[error("Dimension mismatch: frame ({frame_width}x{frame_height}) vs config ({cfg_width}x{cfg_height})")]
43    DimensionMismatch {
44        frame_width: u32,
45        frame_height: u32,
46        cfg_width: u32,
47        cfg_height: u32,
48    },
49    #[error("Invalid codec data: {0}")]
50    InvalidData(&'static str),
51    #[error("Numeric overflow in codec operation")]
52    Overflow,
53    #[error("Codec initialization failed: {0}")]
54    InitFailed(String),
55    #[error("Feature not implemented: {0}")]
56    NotImplemented(&'static str),
57    #[error("Invalid dimensions: width={0}, height={1}")]
58    InvalidDimensions(u32, u32),
59    #[error("Data size exceeds maximum allowed: {actual} > {max}")]
60    SizeExceeded { actual: usize, max: usize },
61}
62
63/// Codec result type
64pub type Result<T> = std::result::Result<T, CodecError>;
65
66/// Maximum allowed dimensions for safety
67pub const MAX_WIDTH: u32 = 8192;
68pub const MAX_HEIGHT: u32 = 8192;
69pub const MAX_RGB_SIZE: usize = 100 * 1024 * 1024; // 100MB
70
71/// Video codec selection
72#[derive(Debug, Clone, Copy)]
73pub enum VideoCodec {
74    H264,
75}
76
77/// Audio codec selection
78#[derive(Debug, Clone, Copy)]
79pub enum AudioCodec {
80    Opus,
81}
82
83/// Video frame
84#[derive(Debug, Clone)]
85pub struct VideoFrame {
86    pub data: Vec<u8>,
87    pub width: u32,
88    pub height: u32,
89    pub timestamp: u64,
90}
91
92/// Video encoder trait
93pub trait VideoEncoder: Send + Sync {
94    fn encode(&mut self, frame: &VideoFrame) -> Result<Bytes>;
95    fn request_keyframe(&mut self);
96}
97
98/// Video decoder trait
99pub trait VideoDecoder: Send + Sync {
100    fn decode(&mut self, data: &[u8]) -> Result<VideoFrame>;
101}
102
103pub use openh264::{OpenH264Decoder, OpenH264Encoder};
104pub use opus::{AudioFrame, Channels, OpusDecoder, OpusEncoder, OpusEncoderConfig, SampleRate};