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