Skip to main content

spoolframe_protocol/
media.rs

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