1
//! POD types for the video-playback surface
2
//! (SUPER_PLAN_2 ยง4 Priority 6 + research).
3
//!
4
//! Same "dumb widget" architecture as camera/screencap
5
//! (`azul_layout::widgets::video::VideoWidget`): a background thread decodes
6
//! the source (vk-video - GPU decode + HTTP-range fetch) and its writeback
7
//! uploads each frame into the shared GL-texture `ImageRef` + recomposites.
8
//! Defined here in `azul-core` so the config crosses the FFI without
9
//! `azul-layout` (or vk-video) as a dependency.
10
//!
11
//! Unlike the camera/screencap configs this carries a `source` string, so
12
//! it's `Clone` but not `Copy`.
13

            
14
use azul_css::{AzString, U8Vec};
15

            
16
use crate::{resources::RawImageFormat, url::Url};
17
#[allow(variant_size_differences)]
18
// repr(C,u8) FFI enum: boxing the large variant would change the C ABI (api.json bindings); size
19
// disparity accepted
20
/// Where a video widget pulls its H.264/MP4 data from โ€” strongly typed so the
21
/// decode worker matches on it directly (no `RefAny` downcast). Mirrors
22
/// [`crate::screencap::ScreenCaptureSource`].
23
#[repr(C, u8)]
24
#[derive(Debug, Clone, PartialEq, Eq)]
25
#[allow(clippy::large_enum_variant)] // #[repr(C,u8)] FFI enum: boxing a variant changes the C
26
                                     // ABI/api.json
27
pub enum VideoSource {
28
    /// An HTTP(S) URL, fetched on the decode thread via an HTTP range request.
29
    Url(Url),
30
    /// A local filesystem path.
31
    File(AzString),
32
    /// Raw MP4 bytes already in memory.
33
    Bytes(U8Vec),
34
}
35

            
36
impl Default for VideoSource {
37
543
    fn default() -> Self {
38
543
        Self::Url(Url::default())
39
543
    }
40
}
41

            
42
/// Requested video-playback configuration.
43
#[repr(C)]
44
#[derive(Debug, Clone, PartialEq)]
45
pub struct VideoConfig {
46
    /// Where to load the video from (URL / file path / in-memory bytes).
47
    pub source: VideoSource,
48
    /// Seek / scrub position in seconds. Changing it across a relayout makes the
49
    /// widget's merge callback tell the decode worker to seek (scrubbing
50
    /// timeline) โ€” the decoder survives relayout like the map's tile cache.
51
    pub timestamp: f32,
52
    /// Start playing automatically on mount.
53
    pub autoplay: bool,
54
    /// Restart from the beginning when the stream ends.
55
    pub looping: bool,
56
    /// Texture format the decoder delivers. `BGRA8` is the portable default;
57
    /// `Nv12` (a later `RawImageFormat` addition) is the zero-copy path.
58
    pub output_format: RawImageFormat,
59
}
60

            
61
impl Default for VideoConfig {
62
536
    fn default() -> Self {
63
536
        Self {
64
536
            source: VideoSource::default(),
65
536
            timestamp: 0.0,
66
536
            autoplay: true,
67
536
            looping: false,
68
536
            output_format: RawImageFormat::BGRA8,
69
536
        }
70
536
    }
71
}
72

            
73
impl VideoConfig {
74
    /// A default config playing `source` (autoplay on, no loop, BGRA8, t=0).
75
    #[must_use]
76
18
    pub fn new(source: VideoSource) -> Self {
77
18
        Self {
78
18
            source,
79
18
            ..Self::default()
80
18
        }
81
18
    }
82
}
83

            
84
/// One captured or decoded frame - tightly-packed RGBA8 pixels
85
/// (`width * height * 4`).
86
///
87
/// The unit a capture/decode worker produces, the
88
/// `set_on_frame` hook hands to user code (effects / save / send), and (P8)
89
/// azul-meet sends over UDP. Defined here (like [`crate::audio::AudioFrame`])
90
/// so it crosses the FFI without `azul-layout` as a dependency.
91
#[repr(C)]
92
#[derive(Debug, Clone, PartialEq, Eq)]
93
pub struct VideoFrame {
94
    /// Frame width in px.
95
    pub width: u32,
96
    /// Frame height in px.
97
    pub height: u32,
98
    /// Tightly-packed RGBA8 pixel bytes (`width * height * 4`).
99
    pub bytes: U8Vec,
100
}
101

            
102
impl VideoFrame {
103
    /// A frame wrapping `bytes` (tightly-packed RGBA8, `width * height * 4`).
104
    #[must_use]
105
628
    pub const fn new(width: u32, height: u32, bytes: U8Vec) -> Self {
106
628
        Self {
107
628
            width,
108
628
            height,
109
628
            bytes,
110
628
        }
111
628
    }
112
}
113

            
114
// FFI Option wrapper for a frame-pull hook / accessor. `copy = false` (U8Vec).
115
impl_option!(VideoFrame, OptionVideoFrame, copy = false, [Clone, Debug]);
116

            
117
/// One CONSUMER of a capture source's frames: a requested output size.
118
///
119
/// A camera or a screen share has many consumers of the same captured
120
/// frame at once โ€” the on-screen preview tile (sized by layout), a remote
121
/// participant who asked for 500x200, a recorder at full size. The device
122
/// captures ONCE, at the smallest size that covers every consumer
123
/// (`azul_layout::image_scale::covering_size`), and every consumer gets its
124
/// own resample of that one frame (`azul_layout::image_scale::fan_out`): the
125
/// camera never captures more than the largest consumer needs, and nothing
126
/// is ever sent bigger than the consumer asked for. Registered on a capture
127
/// widget with `CameraWidget::with_consumer` /
128
/// `ScreenCaptureWidget::with_consumer`; each cut frame is handed to the
129
/// widget's `on_consumer_frame` hook as a [`ConsumerFrame`].
130
///
131
/// `id` is caller-chosen and handed back with every cut frame so one hook can
132
/// serve many consumers ("client Bob" = 7, "the recorder" = 8). Id 0
133
/// ([`FrameConsumer::PREVIEW_ID`]) is reserved for the widget's own on-screen
134
/// tile, whose size follows layout.
135
#[repr(C)]
136
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
137
pub struct FrameConsumer {
138
    /// Caller-chosen id, handed back with every cut frame. 0 is the preview.
139
    pub id: u32,
140
    /// Requested output width in px (0 = invalid, the consumer is skipped).
141
    pub width: u32,
142
    /// Requested output height in px (0 = invalid, the consumer is skipped).
143
    pub height: u32,
144
}
145

            
146
impl FrameConsumer {
147
    /// The id of the widget's own on-screen preview: its size follows the
148
    /// laid-out node (device pixels), never the caller.
149
    pub const PREVIEW_ID: u32 = 0;
150

            
151
    /// A consumer `id` that wants `width` x `height` frames.
152
    #[must_use]
153
25
    pub const fn new(id: u32, width: u32, height: u32) -> Self {
154
25
        Self { id, width, height }
155
25
    }
156

            
157
    /// `false` for a zero-sized request (skipped by the fan-out) or the
158
    /// reserved preview id.
159
    #[must_use]
160
59
    pub const fn is_valid(&self) -> bool {
161
59
        self.id != Self::PREVIEW_ID && self.width > 0 && self.height > 0
162
59
    }
163
}
164

            
165
impl_vec!(
166
    FrameConsumer,
167
    FrameConsumerVec,
168
    FrameConsumerVecDestructor,
169
    FrameConsumerVecDestructorType,
170
    FrameConsumerVecSlice,
171
    OptionFrameConsumer
172
);
173
impl_vec_debug!(FrameConsumer, FrameConsumerVec);
174
impl_vec_clone!(FrameConsumer, FrameConsumerVec, FrameConsumerVecDestructor);
175
impl_vec_partialeq!(FrameConsumer, FrameConsumerVec);
176
impl_vec_eq!(FrameConsumer, FrameConsumerVec);
177
impl_vec_partialord!(FrameConsumer, FrameConsumerVec);
178
impl_vec_ord!(FrameConsumer, FrameConsumerVec);
179
impl_vec_hash!(FrameConsumer, FrameConsumerVec);
180

            
181
impl_option!(
182
    FrameConsumer,
183
    OptionFrameConsumer,
184
    [Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash]
185
);
186

            
187
/// A frame cut to one consumer's requested size: the [`FrameConsumer`] it
188
/// was cut for (so one hook can route by `consumer.id`) and the resampled
189
/// RGBA8 pixels (`consumer.width * consumer.height * 4`).
190
#[repr(C)]
191
#[derive(Debug, Clone, PartialEq, Eq)]
192
pub struct ConsumerFrame {
193
    /// Which consumer this frame was cut for.
194
    pub consumer: FrameConsumer,
195
    /// The frame at the consumer's size.
196
    pub frame: VideoFrame,
197
}
198

            
199
impl ConsumerFrame {
200
    /// Pair a cut frame with the consumer it was cut for.
201
    #[must_use]
202
56
    pub const fn new(consumer: FrameConsumer, frame: VideoFrame) -> Self {
203
56
        Self { consumer, frame }
204
56
    }
205
}
206

            
207
impl_option!(
208
    ConsumerFrame,
209
    OptionConsumerFrame,
210
    copy = false,
211
    [Clone, Debug]
212
);
213

            
214
// FFI `Vec<VideoFrame>` wrapper โ€” the list a batch decode (`DecodedVideo`,
215
// `dll::desktop::extra::video_codec::pipeline`) hands back across the C ABI.
216
// `VideoFrame` derives Debug + Clone + PartialEq, so mirror exactly those Vec
217
// trait impls (no PartialOrd: `VideoFrame` isn't `PartialOrd`).
218
impl_vec!(
219
    VideoFrame,
220
    VideoFrameVec,
221
    VideoFrameVecDestructor,
222
    VideoFrameVecDestructorType,
223
    VideoFrameVecSlice,
224
    OptionVideoFrame
225
);
226
impl_vec_debug!(VideoFrame, VideoFrameVec);
227
impl_vec_clone!(VideoFrame, VideoFrameVec, VideoFrameVecDestructor);
228
impl_vec_partialeq!(VideoFrame, VideoFrameVec);
229

            
230
#[cfg(test)]
231
#[path = "video_test.rs"]
232
mod video_test;