sew_call_client/video/types.rs
1//! The engine's video types: remote video tracks as the host sees them, the receive side's
2//! items ([`Call::video_frames`](crate::Call::video_frames)) and the sinks a host opens to ask
3//! for a track ([`Call::video_sink`](crate::Call::video_sink)).
4//!
5//! The engine hands out **encoded plaintext** frames: decrypted and verified, never decoded. A
6//! host decodes them itself (the webview's WebCodecs) or with kessel-video's decoders.
7
8use bytes::Bytes;
9use serde::Serialize;
10
11/// A video codec a remote track can carry.
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
13#[serde(rename_all = "lowercase")]
14#[non_exhaustive]
15pub enum VideoCodec {
16 /// VP8 (RFC 6386 frames).
17 Vp8,
18 /// H.264: one Annex B access unit per frame, with 4-byte start codes.
19 H264,
20}
21
22impl VideoCodec {
23 /// The codec's name in Sew's media policy and in `TrackInfo` mime types (`vp8`, `h264`).
24 pub fn as_str(self) -> &'static str {
25 match self {
26 VideoCodec::Vp8 => "vp8",
27 VideoCodec::H264 => "h264",
28 }
29 }
30
31 /// The codec of a mime type such as `video/VP8` or `video/H264` (any case).
32 pub fn from_mime(mime: &str) -> Option<VideoCodec> {
33 let m = mime.to_ascii_lowercase();
34 let name = m.strip_prefix("video/").unwrap_or(&m);
35 match name {
36 "vp8" => Some(VideoCodec::Vp8),
37 "h264" => Some(VideoCodec::H264),
38 _ => None,
39 }
40 }
41}
42
43/// What a remote video track shows.
44#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
45#[serde(rename_all = "snake_case")]
46#[non_exhaustive]
47pub enum VideoSource {
48 /// A camera.
49 Camera,
50 /// A screen share.
51 ScreenShare,
52}
53
54/// Why the media server paused a track's video for this member.
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
56#[serde(rename_all = "snake_case")]
57#[non_exhaustive]
58pub enum VideoPause {
59 /// This member's downlink is congested: the server pauses video (never audio) until the
60 /// bandwidth estimate allows it again.
61 Congestion,
62}
63
64/// One simulcast layer a publisher sends, as its `TrackInfo` lists it.
65#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
66#[non_exhaustive]
67pub struct VideoLayerInfo {
68 /// Width in pixels.
69 pub width: u32,
70 /// Height in pixels.
71 pub height: u32,
72 /// Bitrate in bits per second (0 when the publisher did not say).
73 pub bitrate: u32,
74}
75
76/// A remote video track (camera or screen) of a participant.
77#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
78#[non_exhaustive]
79pub struct VideoTrack {
80 /// The engine's stable id for the track in this call (the `track` of its
81 /// [`VideoRecvItem`]s).
82 pub id: u64,
83 /// The publisher's engine identity (`p_…`).
84 pub identity: String,
85 /// The publisher's Sew member, once the keys' roster or the call socket named it.
86 pub member_id: Option<String>,
87 /// Camera or screen.
88 pub source: VideoSource,
89 /// The engine's track sid (`TR_…`).
90 pub track_sid: String,
91 /// The codec: the negotiated payload type of the track's packets once they arrive, else
92 /// the first codec of the publisher's `TrackInfo` (its primary, then its backups) this
93 /// device's media connection receives. `None` when neither says yet (a publisher that does
94 /// not name its codec in `TrackInfo` until its first packet).
95 pub codec: Option<VideoCodec>,
96 /// The publisher's size, from its `TrackInfo` (0 when it did not say).
97 pub width: u32,
98 /// The publisher's size, from its `TrackInfo` (0 when it did not say).
99 pub height: u32,
100 /// The publisher muted the track (camera off).
101 pub muted: bool,
102 /// The server paused the track for this member (`None`: not paused).
103 pub paused: Option<VideoPause>,
104 /// The simulcast layers the publisher sends, lowest first.
105 pub layers: Vec<VideoLayerInfo>,
106 /// This device can't receive any of the codecs the publisher sends (e.g. an H.264-only
107 /// sender, and this device decodes VP8 only): no frames will come. `codec` is then the
108 /// publisher's primary codec. Cleared by the track's first packet.
109 pub unsupported: bool,
110}
111
112/// One participant's camera or screen in the snapshot ([`crate::Participant::camera`],
113/// [`crate::Participant::screen`]).
114#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
115#[non_exhaustive]
116pub struct VideoTrackView {
117 /// The engine's id for the track ([`VideoTrack::id`]).
118 pub id: u64,
119 /// The track sid.
120 pub track_sid: String,
121 /// The codec, when known.
122 pub codec: Option<VideoCodec>,
123 /// The publisher's size (0 when unknown).
124 pub width: u32,
125 /// The publisher's size (0 when unknown).
126 pub height: u32,
127 /// The publisher muted it.
128 pub muted: bool,
129 /// The server paused it for this member.
130 pub paused: Option<VideoPause>,
131 /// This device can't receive any of the track's codecs ([`VideoTrack::unsupported`]).
132 pub unsupported: bool,
133}
134
135impl VideoTrack {
136 /// The snapshot's view of the track.
137 pub fn view(&self) -> VideoTrackView {
138 VideoTrackView {
139 id: self.id,
140 track_sid: self.track_sid.clone(),
141 codec: self.codec,
142 width: self.width,
143 height: self.height,
144 muted: self.muted,
145 paused: self.paused,
146 unsupported: self.unsupported,
147 }
148 }
149}
150
151/// One decrypted, verified encoded frame of a remote video track.
152#[derive(Clone, Debug, PartialEq)]
153#[non_exhaustive]
154pub struct VideoFrame {
155 /// The track ([`VideoTrack::id`]).
156 pub track: u64,
157 /// The codec of this frame.
158 pub codec: VideoCodec,
159 /// The plaintext frame: a VP8 frame, or an H.264 access unit with 4-byte start codes.
160 pub data: Bytes,
161 /// A keyframe (VP8's P bit clear; H.264 with an IDR slice).
162 pub keyframe: bool,
163 /// The RTP timestamp (90 kHz).
164 pub rtp_ts: u32,
165 /// When its last packet arrived, in microseconds since the call started.
166 pub arrival_us: u64,
167 /// The coded width, from a VP8 keyframe's header or the H.264 SPS (`None` on other frames).
168 pub width: Option<u32>,
169 /// The coded height, likewise.
170 pub height: Option<u32>,
171}
172
173/// What the receive side hands a video host, in order ([`crate::Call::video_frames`]).
174///
175/// Frames of a track flow only while at least one of its sinks is open
176/// ([`crate::Call::video_sink`]); with none, the engine asks the server to disable the track
177/// and nothing is forwarded.
178#[derive(Clone, Debug, PartialEq)]
179#[non_exhaustive]
180pub enum VideoRecvItem {
181 /// A track's frames start (its first sink opened): frames follow, from a keyframe.
182 Added {
183 /// The track.
184 track: u64,
185 /// Its codec.
186 codec: VideoCodec,
187 },
188 /// A track's frames stop (its last sink closed, the publisher unpublished or left).
189 Removed {
190 /// The track.
191 track: u64,
192 },
193 /// A decrypted frame.
194 Frame(VideoFrame),
195 /// Frames were lost or failed to decrypt: the decoder must reset and wait for a keyframe
196 /// (the engine already asked the publisher for one).
197 Broken {
198 /// The track.
199 track: u64,
200 },
201 /// The engine asked the publisher for a keyframe (a sink opened, a loss, a decrypt failure
202 /// or the host's [`VideoSink::request_keyframe`](crate::VideoSink::request_keyframe)).
203 KeyframeNeeded {
204 /// The track.
205 track: u64,
206 },
207}
208
209/// What a sink shows of a track; the engine asks the server for the union of a track's sinks.
210#[derive(Clone, Copy, Debug, PartialEq, Eq)]
211#[non_exhaustive]
212pub struct VideoSinkSpec {
213 /// The tile's size in device pixels (the layer whose size fits it is asked for).
214 pub width: u32,
215 /// The tile's size in device pixels.
216 pub height: u32,
217 /// On screen. A track whose sinks are all hidden is disabled at the server.
218 pub visible: bool,
219 /// A large tile (30 fps) rather than a grid tile (15 fps).
220 pub large: bool,
221}
222
223impl VideoSinkSpec {
224 /// A visible sink of `width` × `height` device pixels; `large` for a large tile.
225 pub fn new(width: u32, height: u32, large: bool) -> VideoSinkSpec {
226 VideoSinkSpec {
227 width,
228 height,
229 visible: true,
230 large,
231 }
232 }
233
234 /// The same sink, on or off screen.
235 pub fn visible(mut self, visible: bool) -> VideoSinkSpec {
236 self.visible = visible;
237 self
238 }
239}