videocall_codecs/frame.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//! Contains the fundamental data structures for video frames.
20
21use serde::{Deserialize, Serialize};
22
23/// The type of a video frame, indicating its dependency on other frames.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25pub enum FrameType {
26 /// A KeyFrame (or I-frame) can be decoded independently of any other frame.
27 KeyFrame,
28 /// A DeltaFrame (or P-frame) can only be decoded if the preceding frame has been decoded.
29 DeltaFrame,
30}
31
32/// Represents a raw, encoded video frame as it arrives from the network.
33/// In our simulation, this is the unit that comes from a QUIC stream.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct VideoFrame {
36 /// The sequence number of the frame. Must be contiguous.
37 pub sequence_number: u64,
38 /// The type of the frame (KeyFrame or DeltaFrame).
39 pub frame_type: FrameType,
40 /// The encoded video data.
41 pub data: Vec<u8>,
42 /// The timestamp of the frame.
43 pub timestamp: f64,
44}
45
46/// A wrapper for a `VideoFrame` that includes metadata used by the jitter buffer.
47/// This is the object that is stored and managed within the buffer itself.
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct FrameBuffer {
50 /// The underlying video frame data and properties.
51 pub frame: VideoFrame,
52 /// The system time when this frame was received by the jitter buffer.
53 pub arrival_time_ms: u128,
54}
55
56impl FrameBuffer {
57 /// Creates a new, empty `FrameBuffer` ready to be populated.
58 /// In a real system with object pooling, this would be reused.
59 pub fn new(frame: VideoFrame, arrival_time_ms: u128) -> Self {
60 Self {
61 frame,
62 arrival_time_ms,
63 }
64 }
65
66 pub fn sequence_number(&self) -> u64 {
67 self.frame.sequence_number
68 }
69
70 pub fn is_keyframe(&self) -> bool {
71 self.frame.frame_type == FrameType::KeyFrame
72 }
73}