Skip to main content

spoolframe_protocol/
playback.rs

1//! Playback plans: how the server would play an item
2//! for this client, given what it can decode and which tracks it picked.
3
4use serde::{Deserialize, Serialize};
5use spoolframe_types::{AudioCodec, Container, Delivery, SubtitleFormat, VideoCodec};
6
7/// Of a link's measured rate, the share a playback is planned for:
8/// the rest is room for the link's swings and for the requests,
9/// artwork and progress reports that share it. The server plans for it,
10/// and the SDK asks for a new plan when a file needs more than it.
11pub const LINK_SHARE: f64 = 0.75;
12
13/// One variant of an HLS ladder: its picture's height and its
14/// video's bitrate, bits per second (an encode's cap; a copy's average).
15/// Its codec, range and burned-in subtitle are the plan's `video`'s, only
16/// the size and rate differing, unless `video` says otherwise.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
18#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
19pub struct Rung {
20    pub height: u32,
21    pub max_bitrate: u64,
22    /// This variant's own video, where it is not the plan's resized: the
23    /// encodes under a copied original (a remux ladder), whose plan
24    /// `video` is the copy, the top variant. `None` for a ladder of
25    /// encodes, and the copy's own rung.
26    #[serde(deserialize_with = "spoolframe_types::required::option")]
27    pub video: Option<ConvertedVideo>,
28}
29
30/// What quality a playback is planned for: the viewer's choice
31/// for this playback. The capabilities' `max_bitrate`, the app's own
32/// limit, stays a ceiling over each.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
35#[serde(tag = "type", rename_all = "snake_case")]
36pub enum Quality {
37    /// For the link: [`LINK_SHARE`] of what the device measured it
38    /// carrying lately, bits per second; `None`: it has measured nothing.
39    /// A playback that stalls is planned again for the link.
40    Auto {
41        #[serde(deserialize_with = "spoolframe_types::required::option")]
42        link_bitrate: Option<u64>,
43    },
44    /// At most `max_bitrate` bits per second, whatever the link: encoded
45    /// under it when the file is over it.
46    Bitrate { max_bitrate: u64 },
47    /// The file as it is, whatever the link: converted only for what the
48    /// device cannot play, and never for the link.
49    Original,
50    /// A choice this build does not know: treated as `Auto` with nothing
51    /// measured.
52    #[serde(other)]
53    Unknown,
54}
55
56impl Default for Quality {
57    fn default() -> Self {
58        Self::Auto { link_bitrate: None }
59    }
60}
61
62/// The tracks a client wants, as positions in the item's `MediaTracks`
63/// lists. Only these count: a track the user did not pick never blocks
64/// playback.
65#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
66#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
67pub struct Selection {
68    /// `None`: the file's default audio track.
69    #[serde(deserialize_with = "spoolframe_types::required::option")]
70    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
71    pub audio: Option<u32>,
72    /// `None`: no subtitles.
73    #[serde(deserialize_with = "spoolframe_types::required::option")]
74    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
75    pub subtitle: Option<u32>,
76}
77
78/// How a picked bitmap subtitle (PGS) is shown: drawn by the
79/// client, read as text by the server, or burned into the video. Text
80/// subtitles are not affected; they are never burned in. Pictures are
81/// burned in only when the client cannot draw them.
82#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
83#[serde(rename_all = "snake_case")]
84pub enum BitmapSubtitles {
85    /// As the viewer's preference says (`TrackPreferences`'
86    /// `subtitles.bitmap`): read as text if the server can, else drawn by
87    /// the client if it can, else burned in; or, for pictures, drawn by
88    /// the client if it can, else burned in. Apps send this unless the
89    /// user overrides it for one playback. The plan says which was used.
90    #[default]
91    Auto,
92    /// Read as text and sent as a text subtitle, even to a client that
93    /// could draw the pictures. The plan is `Unsupported` if the server
94    /// cannot read this track.
95    Text,
96    /// Pictures: drawn by the client if it can, else burned into the
97    /// video. The server plans it as the `pictures` preference; it no
98    /// longer forces a burn-in on a client that can draw the pictures.
99    /// Kept so the wire does not change.
100    BurnIn,
101    /// A mode this build does not know: treated as `Auto`.
102    #[serde(other)]
103    Unknown,
104}
105
106/// The tracks the server chose when the client sent no [`Selection`],
107/// and why, so the client can show it.
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
109#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
110pub struct ChosenTracks {
111    pub selection: Selection,
112    pub audio: TrackReason,
113    pub subtitle: TrackReason,
114}
115
116/// Why a track was chosen, or why none was.
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
118#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
119#[serde(rename_all = "snake_case")]
120pub enum TrackReason {
121    /// What the viewer picked for this series or movie.
122    Override,
123    /// The viewer's preferences.
124    Preferences,
125    /// No preferred language is in the file: its default track.
126    FileDefault,
127    /// The preferences want none here (subtitles off, or the audio is
128    /// already in a preferred subtitle language).
129    NotWanted,
130    /// Wanted, but no track fits (e.g. only SDH, and SDH is avoided).
131    NoneMatched,
132    /// The file has no tracks of this kind.
133    NoTracks,
134    /// A reason this build does not know.
135    #[serde(other)]
136    Unknown,
137}
138
139#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
140#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
141#[serde(tag = "type", rename_all = "snake_case")]
142pub enum PlaybackPlan {
143    /// The client plays the file as it is.
144    Direct {
145        container: Container,
146        video: Option<VideoCodec>,
147        audio: Option<PlannedAudio>,
148        subtitle: Option<PlannedSubtitle>,
149        /// The source video's RFC 6381 codec string as the plan judged
150        /// it against `ClientCapabilities::video_codecs`
151        /// (`hvc1.2.4.L153.B0`; `dvh1.08.06` for Dolby Vision shown as
152        /// such): for a client's log. `None` where the server did not read
153        /// it, or there is no video. It may be left out, and is `None`
154        /// then.
155        #[serde(default)]
156        source_codec: Option<String>,
157    },
158    /// The server converts the file for this client: copies the
159    /// streams that fit, encodes the ones that do not, and delivers the
160    /// result as `delivery` says. A remux is a plan whose streams are all
161    /// `Copy`.
162    Converted {
163        delivery: Delivery,
164        /// The container of the output.
165        container: Container,
166        video: Option<ConvertedVideo>,
167        audio: Option<ConvertedAudio>,
168        subtitle: Option<ConvertedSubtitle>,
169        /// The encodes an HLS player switches between as its link changes,
170        /// highest first: `video` is the first of them. Empty for
171        /// one encode, as every plan but `Auto` quality over HLS is.
172        ladder: Vec<Rung>,
173        /// Why the file is not played as it is, one per stream that is
174        /// converted, e.g. "subtitle 0: pgs cannot be shown; burned into
175        /// the video". A client can offer to avoid them (subtitles off).
176        reasons: Vec<String>,
177        /// As in `Direct`.
178        #[serde(default)]
179        source_codec: Option<String>,
180    },
181    /// The client cannot play this selection. Each reason names a stream
182    /// and why, e.g. "audio 0: dts not supported".
183    Unsupported { reasons: Vec<String> },
184    /// A plan kind this build does not know.
185    #[serde(other)]
186    Unknown,
187}
188
189impl PlaybackPlan {
190    /// Its `source_codec`: `None` where it has none, as an unsupported
191    /// plan has not.
192    pub fn source_codec(&self) -> Option<&str> {
193        match self {
194            Self::Direct { source_codec, .. } | Self::Converted { source_codec, .. } => {
195                source_codec.as_deref()
196            }
197            Self::Unsupported { .. } | Self::Unknown => None,
198        }
199    }
200}
201
202/// What happens to the video in a converted plan.
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
204#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
205#[serde(tag = "type", rename_all = "snake_case")]
206pub enum ConvertedVideo {
207    /// Into the output as it is.
208    Copy {
209        codec: VideoCodec,
210        /// Labelled as Dolby Vision (PGPU-06): the client shows it, and
211        /// the stream is Dolby Vision profile 5 or 8, so the output says
212        /// so (`dvcC`/`dvvC`, `dvh1`, HLS's `SUPPLEMENTAL-CODECS`). `false`: a
213        /// Dolby Vision stream copied as its base layer, HDR10 or HLG, or
214        /// any other stream.
215        dolby_vision: bool,
216    },
217    Encode {
218        codec: VideoCodec,
219        /// Lines in the output.
220        height: u32,
221        /// The most it may use, bits per second; `None` for the encoder's
222        /// own choice.
223        max_bitrate: Option<u64>,
224        /// HDR mapped to SDR.
225        tone_map: bool,
226    },
227    /// A kind this build does not know.
228    #[serde(other)]
229    Unknown,
230}
231
232/// What happens to the selected audio track in a converted plan.
233#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
234#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
235pub struct ConvertedAudio {
236    pub index: u32,
237    pub action: AudioAction,
238}
239
240#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
241#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
242#[serde(tag = "type", rename_all = "snake_case")]
243pub enum AudioAction {
244    Copy {
245        codec: AudioCodec,
246    },
247    Encode {
248        codec: AudioCodec,
249        channels: u32,
250        /// Bits per second.
251        bitrate: u64,
252    },
253    #[serde(other)]
254    Unknown,
255}
256
257/// What happens to the selected subtitle in a converted plan.
258#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
259#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
260pub struct ConvertedSubtitle {
261    pub index: u32,
262    pub action: SubtitleAction,
263}
264
265#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
266#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
267#[serde(tag = "type", rename_all = "snake_case")]
268pub enum SubtitleAction {
269    /// Into the output as it is.
270    Copy { format: SubtitleFormat },
271    /// Converted to another text format, e.g. WebVTT for HLS.
272    Convert {
273        from: SubtitleFormat,
274        to: SubtitleFormat,
275    },
276    /// Picture subtitles drawn into the video: the client cannot draw them
277    /// in this delivery, and they are not read as text. Forces a video
278    /// encode. The server never burns in a text subtitle: one the client
279    /// cannot show is left out of the plan, with a reason.
280    BurnIn { format: SubtitleFormat },
281    /// A bitmap subtitle read as text by the server. It is not in
282    /// the output: the client fetches it whole with `ReadSubtitle`, which
283    /// answers `SubtitleNotReady` until the reading is done.
284    Ocr {
285        from: SubtitleFormat,
286        to: SubtitleFormat,
287    },
288    #[serde(other)]
289    Unknown,
290}
291
292#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
293#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
294pub struct PlannedAudio {
295    pub index: u32,
296    pub codec: AudioCodec,
297}
298
299#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
300#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
301pub struct PlannedSubtitle {
302    pub index: u32,
303    pub format: SubtitleFormat,
304    pub delivery: SubtitleDelivery,
305}
306
307/// Where the client gets a subtitle from.
308#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
309#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
310#[serde(rename_all = "snake_case")]
311pub enum SubtitleDelivery {
312    /// A stream inside the media file it plays.
313    Embedded,
314    /// A separate file the client fetches whole. A bitmap track
315    /// planned as a text format is one the server reads as text:
316    /// `ReadSubtitle` answers `SubtitleNotReady` until it is done.
317    External,
318    #[serde(other)]
319    Unknown,
320}