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