Skip to main content

spoolframe_protocol/
previews.rs

1//! Frame previews for scrubbing: `Request::GetPreviews` and its
2//! reply.
3//!
4//! An item's previews are pictures of it at even intervals, packed row by
5//! row into sheets of `columns × rows` tiles. A sheet is an image like
6//! any other: read it on the artwork stream by its [`ArtworkId`],
7//! at [`crate::ArtworkSize::Original`], once for the item, and draw the
8//! part of it that [`Previews::tile`] names. A smaller size scales the
9//! whole sheet: scale the tile's rectangle by the image's width over
10//! `columns × tile_width`.
11
12use serde::{Deserialize, Serialize};
13use spoolframe_types::ArtworkId;
14
15/// An item's previews: what `GetPreviews` answers once they are made.
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
18pub struct Previews {
19    /// Tile `i` is the picture at about `i × interval_ms` (the keyframe
20    /// at or just before it).
21    pub interval_ms: u64,
22    pub tile_width: u32,
23    pub tile_height: u32,
24    /// Tiles across a sheet.
25    pub columns: u32,
26    /// Tiles down a sheet.
27    pub rows: u32,
28    /// Tiles in all. The last sheet is filled only this far.
29    pub count: u32,
30    /// The sheets, in order: tiles `0..columns × rows` are on the first.
31    pub sheets: Vec<ArtworkId>,
32}
33
34/// Where the picture for a time is.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct PreviewTile {
37    /// Which tile, from 0.
38    pub index: u32,
39    /// Which sheet: an index into [`Previews::sheets`].
40    pub sheet: usize,
41    /// The tile's rectangle in the sheet, in pixels at its original size.
42    pub x: u32,
43    pub y: u32,
44    pub width: u32,
45    pub height: u32,
46}
47
48impl Previews {
49    /// The tile to show for `position_ms`: the last one at or before it;
50    /// past the end, the last one. `None` only for previews with no
51    /// tiles, which the server does not send.
52    pub fn tile(&self, position_ms: u64) -> Option<PreviewTile> {
53        let per_sheet = self.columns.checked_mul(self.rows)?;
54        if self.count == 0 || per_sheet == 0 || self.interval_ms == 0 {
55            return None;
56        }
57        let index = (position_ms / self.interval_ms).min(u64::from(self.count - 1)) as u32;
58        let sheet = (index / per_sheet) as usize;
59        if sheet >= self.sheets.len() {
60            return None;
61        }
62        let at = index % per_sheet;
63        Some(PreviewTile {
64            index,
65            sheet,
66            x: (at % self.columns) * self.tile_width,
67            y: (at / self.columns) * self.tile_height,
68            width: self.tile_width,
69            height: self.tile_height,
70        })
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    //! Finding the tile for a time in an item's previews (P3-13).
77
78    use crate::{PreviewTile, Previews};
79    use spoolframe_types::ArtworkId;
80
81    /// A two-hour film at 10 s: 720 tiles of 320 × 134 on 8 sheets.
82    fn film() -> Previews {
83        Previews {
84            interval_ms: 10_000,
85            tile_width: 320,
86            tile_height: 134,
87            columns: 10,
88            rows: 10,
89            count: 720,
90            sheets: (0..8)
91                .map(|n| format!("preview:abc/{n}").parse::<ArtworkId>().unwrap())
92                .collect(),
93        }
94    }
95
96    #[test]
97    fn the_tile_for_1_23_40_is_on_its_sheet_by_row_and_column() {
98        // 1:23:40 is 5020 s: tile 502, the 3rd of sheet 5, in its first row.
99        assert_eq!(
100            film().tile(5_020_000),
101            Some(PreviewTile {
102                index: 502,
103                sheet: 5,
104                x: 2 * 320,
105                y: 0,
106                width: 320,
107                height: 134,
108            })
109        );
110        // 37 s past it: still tile 505's span; tile 505 is the 6th, row 0.
111        let tile = film().tile(5_057_000).unwrap();
112        assert_eq!((tile.index, tile.x, tile.y), (505, 5 * 320, 0));
113        // Tile 579: sheet 5, row 7, column 9.
114        let tile = film().tile(5_799_999).unwrap();
115        assert_eq!(
116            (tile.index, tile.sheet, tile.x, tile.y),
117            (579, 5, 9 * 320, 7 * 134)
118        );
119    }
120
121    #[test]
122    fn a_time_between_tiles_shows_the_one_before() {
123        assert_eq!(film().tile(0).unwrap().index, 0);
124        assert_eq!(film().tile(9_999).unwrap().index, 0);
125        assert_eq!(film().tile(10_000).unwrap().index, 1);
126    }
127
128    #[test]
129    fn past_the_end_is_the_last_tile() {
130        let tile = film().tile(10 * 3_600_000).unwrap();
131        assert_eq!((tile.index, tile.sheet), (719, 7));
132        assert_eq!((tile.x, tile.y), (9 * 320, 134));
133    }
134
135    #[test]
136    fn previews_without_tiles_have_none() {
137        let empty = Previews {
138            count: 0,
139            sheets: vec![],
140            ..film()
141        };
142        assert_eq!(empty.tile(0), None);
143    }
144
145    #[test]
146    fn a_grid_that_is_not_square_is_read_by_its_columns() {
147        // 4 across, 3 down: 12 a sheet.
148        let previews = Previews {
149            columns: 4,
150            rows: 3,
151            count: 20,
152            sheets: film().sheets[..2].to_vec(),
153            ..film()
154        };
155        let tile = previews.tile(70_000).unwrap();
156        assert_eq!((tile.sheet, tile.x, tile.y), (0, 3 * 320, 134));
157        let tile = previews.tile(130_000).unwrap();
158        assert_eq!((tile.index, tile.sheet, tile.x, tile.y), (13, 1, 320, 0));
159    }
160}