Skip to main content

sonos_cli/tui/
image_loader.rs

1//! Background image fetcher with in-memory cache.
2//!
3//! Fetches album art from Sonos speakers over HTTP in a background thread,
4//! decodes images via the `image` crate, and caches decoded `DynamicImage`s
5//! by URI. The main thread polls for completed loads each event loop tick.
6//!
7//! Design: single-threaded access from the TUI event loop. `request()` uses
8//! `RefCell` for the pending set so it can be called from render functions
9//! that only have `&self` access.
10
11use std::cell::RefCell;
12use std::collections::{HashMap, HashSet, VecDeque};
13use std::net::IpAddr;
14use std::sync::mpsc;
15use std::time::Duration;
16
17use image::DynamicImage;
18
19/// Maximum number of cached images before evicting oldest entries.
20const MAX_CACHE_SIZE: usize = 20;
21
22/// HTTP fetch timeout for album art requests.
23const FETCH_TIMEOUT: Duration = Duration::from_secs(3);
24
25struct LoadRequest {
26    uri: String,
27    full_url: String,
28}
29
30struct LoadResult {
31    uri: String,
32    image: Option<DynamicImage>,
33}
34
35/// Background image loader with request/poll/get API.
36///
37/// - `request()` — enqueue a fetch (callable from render with `&self`)
38/// - `poll()` — drain completed fetches into cache (callable from event loop with `&mut self`)
39/// - `get()` — read from cache (callable from render with `&self`)
40pub struct ImageLoader {
41    cache: HashMap<String, DynamicImage>,
42    /// Insertion order for LRU eviction.
43    insertion_order: VecDeque<String>,
44    /// URIs currently being fetched (RefCell for &self access from render).
45    pending: RefCell<HashSet<String>>,
46    result_rx: mpsc::Receiver<LoadResult>,
47    request_tx: mpsc::Sender<LoadRequest>,
48}
49
50impl Default for ImageLoader {
51    fn default() -> Self {
52        Self::new()
53    }
54}
55
56impl ImageLoader {
57    pub fn new() -> Self {
58        let (request_tx, request_rx) = mpsc::channel::<LoadRequest>();
59        let (result_tx, result_rx) = mpsc::channel::<LoadResult>();
60
61        // Spawn a single worker thread for fetching images
62        std::thread::Builder::new()
63            .name("album-art-loader".into())
64            .spawn(move || {
65                worker_loop(request_rx, result_tx);
66            })
67            .expect("failed to spawn album art loader thread");
68
69        Self {
70            cache: HashMap::new(),
71            insertion_order: VecDeque::new(),
72            pending: RefCell::new(HashSet::new()),
73            result_rx,
74            request_tx,
75        }
76    }
77
78    /// Request an image fetch if not already cached or pending.
79    ///
80    /// Callable from render functions with `&self`. The fetch happens in a
81    /// background thread; call `poll()` from the event loop to collect results.
82    pub fn request(&self, uri: &str, speaker_ip: IpAddr) {
83        if self.cache.contains_key(uri) {
84            return;
85        }
86
87        let mut pending = self.pending.borrow_mut();
88        if pending.contains(uri) {
89            return;
90        }
91
92        let full_url = build_url(uri, speaker_ip);
93        let req = LoadRequest {
94            uri: uri.to_string(),
95            full_url,
96        };
97
98        if self.request_tx.send(req).is_ok() {
99            pending.insert(uri.to_string());
100        }
101    }
102
103    /// Drain completed fetches into the cache. Call from event loop each tick.
104    ///
105    /// Returns `true` if any new images were loaded (should mark app dirty).
106    pub fn poll(&mut self) -> bool {
107        let mut loaded = false;
108        while let Ok(result) = self.result_rx.try_recv() {
109            self.pending.borrow_mut().remove(&result.uri);
110            if let Some(img) = result.image {
111                self.evict_if_full();
112                self.insertion_order.push_back(result.uri.clone());
113                self.cache.insert(result.uri, img);
114                loaded = true;
115            }
116        }
117        loaded
118    }
119
120    pub fn get(&self, uri: &str) -> Option<&DynamicImage> {
121        self.cache.get(uri)
122    }
123
124    fn evict_if_full(&mut self) {
125        while self.cache.len() >= MAX_CACHE_SIZE {
126            if let Some(oldest) = self.insertion_order.pop_front() {
127                self.cache.remove(&oldest);
128            } else {
129                break;
130            }
131        }
132    }
133}
134
135/// Build a full URL from an album art URI and speaker IP.
136///
137/// Sonos speakers return `album_art_uri` as either:
138/// - A relative path: `/getaa?s=1&u=...` → prepend `http://{ip}:1400`
139/// - An absolute URL: `http://...` → use as-is
140fn build_url(uri: &str, speaker_ip: IpAddr) -> String {
141    if uri.starts_with("http://") || uri.starts_with("https://") {
142        uri.to_string()
143    } else {
144        format!("http://{speaker_ip}:1400{uri}")
145    }
146}
147
148/// Worker thread: receives fetch requests, downloads + decodes images, sends results back.
149fn worker_loop(rx: mpsc::Receiver<LoadRequest>, tx: mpsc::Sender<LoadResult>) {
150    let agent = ureq::Agent::config_builder()
151        .timeout_global(Some(FETCH_TIMEOUT))
152        .build()
153        .new_agent();
154
155    for req in rx {
156        let image = fetch_and_decode(&agent, &req.full_url);
157        let result = LoadResult {
158            uri: req.uri,
159            image,
160        };
161        if tx.send(result).is_err() {
162            break; // main thread dropped, exit
163        }
164    }
165}
166
167/// Fetch an image over HTTP and decode it.
168fn fetch_and_decode(agent: &ureq::Agent, url: &str) -> Option<DynamicImage> {
169    let response = agent.get(url).call().ok()?;
170
171    let status = response.status();
172    if status != 200 {
173        tracing::debug!("Album art fetch returned {status} for {url}");
174        return None;
175    }
176
177    // Read body into memory (limit to 5MB to prevent OOM)
178    let body = response
179        .into_body()
180        .with_config()
181        .limit(5 * 1024 * 1024)
182        .read_to_vec()
183        .ok()?;
184
185    let img = image::load_from_memory(&body)
186        .map_err(|e| {
187            tracing::debug!("Album art decode failed for {url}: {e}");
188            e
189        })
190        .ok()?;
191
192    Some(crop_to_square(img))
193}
194
195/// Center-crop an image to a square using the shorter dimension.
196fn crop_to_square(img: DynamicImage) -> DynamicImage {
197    let (w, h) = (img.width(), img.height());
198    if w == h {
199        return img;
200    }
201    let side = w.min(h);
202    let x = (w - side) / 2;
203    let y = (h - side) / 2;
204    img.crop_imm(x, y, side, side)
205}
206
207#[cfg(test)]
208mod tests {
209    use super::*;
210
211    #[test]
212    fn build_url_relative_path() {
213        let ip: IpAddr = "192.168.1.100".parse().unwrap();
214        assert_eq!(
215            build_url("/getaa?s=1&u=test", ip),
216            "http://192.168.1.100:1400/getaa?s=1&u=test"
217        );
218    }
219
220    #[test]
221    fn build_url_absolute_http() {
222        let ip: IpAddr = "192.168.1.100".parse().unwrap();
223        let url = "http://example.com/art.jpg";
224        assert_eq!(build_url(url, ip), url);
225    }
226
227    #[test]
228    fn build_url_absolute_https() {
229        let ip: IpAddr = "192.168.1.100".parse().unwrap();
230        let url = "https://example.com/art.jpg";
231        assert_eq!(build_url(url, ip), url);
232    }
233
234    #[test]
235    fn crop_to_square_landscape() {
236        let img = DynamicImage::new_rgb8(160, 90);
237        let cropped = crop_to_square(img);
238        assert_eq!(cropped.width(), 90);
239        assert_eq!(cropped.height(), 90);
240    }
241
242    #[test]
243    fn crop_to_square_portrait() {
244        let img = DynamicImage::new_rgb8(90, 160);
245        let cropped = crop_to_square(img);
246        assert_eq!(cropped.width(), 90);
247        assert_eq!(cropped.height(), 90);
248    }
249
250    #[test]
251    fn crop_to_square_already_square() {
252        let img = DynamicImage::new_rgb8(100, 100);
253        let cropped = crop_to_square(img);
254        assert_eq!(cropped.width(), 100);
255        assert_eq!(cropped.height(), 100);
256    }
257}