Skip to main content

spoolframe_protocol/
media.rs

1//! Playing media: playback sessions and range reads.
2//!
3//! `StartPlayback` (an RPC) plans the item and returns a
4//! [`PlaybackHandle`]. The client then reads the file in ranges, each on its
5//! own [`crate::streams::bi::RESOURCE`] stream, wrapped with the playback
6//! revision in [`crate::resource::ResourceRequest`]. These media structs
7//! describe the request and SDK result; the resource header announces the
8//! raw body length. Several reads may be in flight at once. End of file is
9//! `offset + sent == size`; a read at or past the end has `sent = 0`.
10
11use std::fmt;
12
13use serde::{Deserialize, Serialize};
14
15use crate::messages::{Error, SessionId};
16
17/// The largest range one read returns. A reply shorter than asked is
18/// normal: the client asks again from where it stopped.
19pub const MAX_READ: u64 = 16 << 20;
20
21/// A playback session on the server: one planned file, bound to the
22/// session and endpoint that started it. Opaque to the client.
23#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
24#[serde(transparent)]
25pub struct PlaybackHandle(pub String);
26
27impl fmt::Debug for PlaybackHandle {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        // Not a credential on its own (it is bound to a session and an
30        // endpoint), but there is no reason to log it.
31        f.write_str("PlaybackHandle(<redacted>)")
32    }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
36pub struct MediaRead {
37    pub session: SessionId,
38    pub handle: PlaybackHandle,
39    pub offset: u64,
40    /// Capped at [`MAX_READ`].
41    pub length: u64,
42    /// The encode this read is for: byte offsets mean nothing
43    /// across a restart. 0 for direct play and a playback's first encode.
44    pub epoch: u32,
45    /// An HLS segment to read in, 0 being the init segment; `None`
46    /// reads the file itself (direct play) or the progressive output.
47    #[serde(deserialize_with = "spoolframe_types::required::option")]
48    pub segment: Option<u32>,
49    /// For HLS, the variant the segment is of: a ladder's rung,
50    /// as `v{n}/` in its playlist. 0 for one encode, and for anything
51    /// that is not a segment.
52    pub variant: u32,
53}
54
55/// What follows on the stream. Every field is required.
56#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
57pub struct MediaReadReply {
58    /// How many bytes follow this frame.
59    pub sent: u64,
60    /// The size of the whole file, or of what an encode has written so
61    /// far (see `complete`).
62    pub size: u64,
63    /// `size` is final: always for direct play, once the encode has ended
64    /// for a converted playback. The end is `complete` and `offset + sent
65    /// == size`.
66    pub complete: bool,
67    /// The encode the bytes are from.
68    pub epoch: u32,
69}
70
71#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
72#[serde(rename_all = "snake_case")]
73pub enum MediaReadResponse {
74    Ok(MediaReadReply),
75    Err(Error),
76}