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