spoolframe-protocol 0.17.1

The spoolframe/client/1 wire protocol.
Documentation
//! Playback plans: how the server would play an item
//! for this client, given what it can decode and which tracks it picked.

use serde::{Deserialize, Serialize};
use spoolframe_types::{AudioCodec, Container, Delivery, SubtitleFormat, VideoCodec};

/// Of a link's measured rate, the share a playback is planned for:
/// the rest is room for the link's swings and for the requests,
/// artwork and progress reports that share it. The server plans for it,
/// and the SDK asks for a new plan when a file needs more than it.
pub const LINK_SHARE: f64 = 0.75;

/// One variant of an HLS ladder: its picture's height and its
/// video's bitrate, bits per second (an encode's cap; a copy's average).
/// Its codec, range and burned-in subtitle are the plan's `video`'s, only
/// the size and rate differing, unless `video` says otherwise.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct Rung {
    pub height: u32,
    pub max_bitrate: u64,
    /// This variant's own video, where it is not the plan's resized: the
    /// encodes under a copied original (a remux ladder), whose plan
    /// `video` is the copy, the top variant. `None` for a ladder of
    /// encodes, and the copy's own rung.
    #[serde(deserialize_with = "spoolframe_types::required::option")]
    pub video: Option<ConvertedVideo>,
}

/// What quality a playback is planned for: the viewer's choice
/// for this playback. The capabilities' `max_bitrate`, the app's own
/// limit, stays a ceiling over each.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Quality {
    /// For the link: [`LINK_SHARE`] of what the device measured it
    /// carrying lately, bits per second; `None`: it has measured nothing.
    /// A playback that stalls is planned again for the link.
    Auto {
        #[serde(deserialize_with = "spoolframe_types::required::option")]
        link_bitrate: Option<u64>,
    },
    /// At most `max_bitrate` bits per second, whatever the link: encoded
    /// under it when the file is over it.
    Bitrate { max_bitrate: u64 },
    /// The file as it is, whatever the link: converted only for what the
    /// device cannot play, and never for the link.
    Original,
    /// A choice this build does not know: treated as `Auto` with nothing
    /// measured.
    #[serde(other)]
    Unknown,
}

impl Default for Quality {
    fn default() -> Self {
        Self::Auto { link_bitrate: None }
    }
}

/// The tracks a client wants, as positions in the item's `MediaTracks`
/// lists. Only these count: a track the user did not pick never blocks
/// playback.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct Selection {
    /// `None`: the file's default audio track.
    #[serde(deserialize_with = "spoolframe_types::required::option")]
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    pub audio: Option<u32>,
    /// `None`: no subtitles.
    #[serde(deserialize_with = "spoolframe_types::required::option")]
    #[cfg_attr(feature = "uniffi", uniffi(default = None))]
    pub subtitle: Option<u32>,
}

/// How a picked bitmap subtitle (PGS) is shown: drawn by the
/// client, read as text by the server, or burned into the video. Text
/// subtitles are not affected; they are never burned in. Pictures are
/// burned in only when the client cannot draw them.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BitmapSubtitles {
    /// As the viewer's preference says (`TrackPreferences`'
    /// `subtitles.bitmap`): read as text if the server can, else drawn by
    /// the client if it can, else burned in; or, for pictures, drawn by
    /// the client if it can, else burned in. Apps send this unless the
    /// user overrides it for one playback. The plan says which was used.
    #[default]
    Auto,
    /// Read as text and sent as a text subtitle, even to a client that
    /// could draw the pictures. The plan is `Unsupported` if the server
    /// cannot read this track.
    Text,
    /// Pictures: drawn by the client if it can, else burned into the
    /// video. The server plans it as the `pictures` preference; it no
    /// longer forces a burn-in on a client that can draw the pictures.
    /// Kept so the wire does not change.
    BurnIn,
    /// A mode this build does not know: treated as `Auto`.
    #[serde(other)]
    Unknown,
}

/// The tracks the server chose when the client sent no [`Selection`],
/// and why, so the client can show it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct ChosenTracks {
    pub selection: Selection,
    pub audio: TrackReason,
    pub subtitle: TrackReason,
}

/// Why a track was chosen, or why none was.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(rename_all = "snake_case")]
pub enum TrackReason {
    /// What the viewer picked for this series or movie.
    Override,
    /// The viewer's preferences.
    Preferences,
    /// No preferred language is in the file: its default track.
    FileDefault,
    /// The preferences want none here (subtitles off, or the audio is
    /// already in a preferred subtitle language).
    NotWanted,
    /// Wanted, but no track fits (e.g. only SDH, and SDH is avoided).
    NoneMatched,
    /// The file has no tracks of this kind.
    NoTracks,
    /// A reason this build does not know.
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum PlaybackPlan {
    /// The client plays the file as it is.
    Direct {
        container: Container,
        video: Option<VideoCodec>,
        audio: Option<PlannedAudio>,
        subtitle: Option<PlannedSubtitle>,
        /// The source video's RFC 6381 codec string as the plan judged
        /// it against `ClientCapabilities::video_codecs`
        /// (`hvc1.2.4.L153.B0`; `dvh1.08.06` for Dolby Vision shown as
        /// such): for a client's log. `None` where the server did not read
        /// it, or there is no video. It may be left out, and is `None`
        /// then.
        #[serde(default)]
        source_codec: Option<String>,
    },
    /// The server converts the file for this client: copies the
    /// streams that fit, encodes the ones that do not, and delivers the
    /// result as `delivery` says. A remux is a plan whose streams are all
    /// `Copy`.
    Converted {
        delivery: Delivery,
        /// The container of the output.
        container: Container,
        video: Option<ConvertedVideo>,
        audio: Option<ConvertedAudio>,
        subtitle: Option<ConvertedSubtitle>,
        /// The encodes an HLS player switches between as its link changes,
        /// highest first: `video` is the first of them. Empty for
        /// one encode, as every plan but `Auto` quality over HLS is.
        ladder: Vec<Rung>,
        /// Why the file is not played as it is, one per stream that is
        /// converted, e.g. "subtitle 0: pgs cannot be shown; burned into
        /// the video". A client can offer to avoid them (subtitles off).
        reasons: Vec<String>,
        /// As in `Direct`.
        #[serde(default)]
        source_codec: Option<String>,
    },
    /// The client cannot play this selection. Each reason names a stream
    /// and why, e.g. "audio 0: dts not supported".
    Unsupported { reasons: Vec<String> },
    /// A plan kind this build does not know.
    #[serde(other)]
    Unknown,
}

impl PlaybackPlan {
    /// Its `source_codec`: `None` where it has none, as an unsupported
    /// plan has not.
    pub fn source_codec(&self) -> Option<&str> {
        match self {
            Self::Direct { source_codec, .. } | Self::Converted { source_codec, .. } => {
                source_codec.as_deref()
            }
            Self::Unsupported { .. } | Self::Unknown => None,
        }
    }
}

/// What happens to the video in a converted plan.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ConvertedVideo {
    /// Into the output as it is.
    Copy {
        codec: VideoCodec,
        /// Labelled as Dolby Vision (PGPU-06): the client shows it, and
        /// the stream is Dolby Vision profile 5 or 8, so the output says
        /// so (`dvcC`/`dvvC`, `dvh1`, HLS's `SUPPLEMENTAL-CODECS`). `false`: a
        /// Dolby Vision stream copied as its base layer, HDR10 or HLG, or
        /// any other stream.
        dolby_vision: bool,
    },
    Encode {
        codec: VideoCodec,
        /// Lines in the output.
        height: u32,
        /// The most it may use, bits per second; `None` for the encoder's
        /// own choice.
        max_bitrate: Option<u64>,
        /// HDR mapped to SDR.
        tone_map: bool,
    },
    /// A kind this build does not know.
    #[serde(other)]
    Unknown,
}

/// What happens to the selected audio track in a converted plan.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct ConvertedAudio {
    pub index: u32,
    pub action: AudioAction,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AudioAction {
    Copy {
        codec: AudioCodec,
    },
    Encode {
        codec: AudioCodec,
        channels: u32,
        /// Bits per second.
        bitrate: u64,
    },
    #[serde(other)]
    Unknown,
}

/// What happens to the selected subtitle in a converted plan.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct ConvertedSubtitle {
    pub index: u32,
    pub action: SubtitleAction,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SubtitleAction {
    /// Into the output as it is.
    Copy { format: SubtitleFormat },
    /// Converted to another text format, e.g. WebVTT for HLS.
    Convert {
        from: SubtitleFormat,
        to: SubtitleFormat,
    },
    /// Picture subtitles drawn into the video: the client cannot draw them
    /// in this delivery, and they are not read as text. Forces a video
    /// encode. The server never burns in a text subtitle: one the client
    /// cannot show is left out of the plan, with a reason.
    BurnIn { format: SubtitleFormat },
    /// A bitmap subtitle read as text by the server. It is not in
    /// the output: the client fetches it whole with `ReadSubtitle`, which
    /// answers `SubtitleNotReady` until the reading is done.
    Ocr {
        from: SubtitleFormat,
        to: SubtitleFormat,
    },
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct PlannedAudio {
    pub index: u32,
    pub codec: AudioCodec,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct PlannedSubtitle {
    pub index: u32,
    pub format: SubtitleFormat,
    pub delivery: SubtitleDelivery,
}

/// Where the client gets a subtitle from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[serde(rename_all = "snake_case")]
pub enum SubtitleDelivery {
    /// A stream inside the media file it plays.
    Embedded,
    /// A separate file the client fetches whole. A bitmap track
    /// planned as a text format is one the server reads as text:
    /// `ReadSubtitle` answers `SubtitleNotReady` until it is done.
    External,
    #[serde(other)]
    Unknown,
}