Skip to main content

oximedia_proxy/resolution/
switcher.rs

1//! Resolution switching for dynamic proxy selection.
2
3use super::manager::{ProxyResolution, ResolutionManager};
4use crate::Result;
5use std::path::{Path, PathBuf};
6
7/// Resolution switcher for changing between proxy resolutions.
8pub struct ResolutionSwitcher {
9    manager: ResolutionManager,
10    current_resolution: ProxyResolution,
11}
12
13impl ResolutionSwitcher {
14    /// Create a new resolution switcher.
15    #[must_use]
16    pub fn new(manager: ResolutionManager) -> Self {
17        Self {
18            manager,
19            current_resolution: ProxyResolution::Quarter,
20        }
21    }
22
23    /// Switch to a different resolution.
24    pub fn switch_to(&mut self, resolution: ProxyResolution) {
25        self.current_resolution = resolution;
26        tracing::info!("Switched to {} resolution", resolution.name());
27    }
28
29    /// Get the current proxy for an original file at the current resolution.
30    pub fn get_current_proxy(&self, original: &Path) -> Result<PathBuf> {
31        let original_buf = original.to_path_buf();
32        self.manager
33            .get_best_variant(&original_buf, self.current_resolution)
34            .map(|v| v.path.clone())
35            .ok_or_else(|| crate::ProxyError::LinkNotFound(original.display().to_string()))
36    }
37
38    /// Get the current resolution.
39    #[must_use]
40    pub const fn current_resolution(&self) -> ProxyResolution {
41        self.current_resolution
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use crate::resolution::manager::ProxyVariant;
49
50    #[test]
51    fn test_resolution_switcher() {
52        let mut manager = ResolutionManager::new();
53
54        let original = PathBuf::from("original.mov");
55        let variant = ProxyVariant {
56            resolution: ProxyResolution::Quarter,
57            path: PathBuf::from("proxy_quarter.mp4"),
58            file_size: 1000,
59            codec: "h264".to_string(),
60        };
61
62        manager.add_variant(original.clone(), variant);
63
64        let mut switcher = ResolutionSwitcher::new(manager);
65        assert_eq!(switcher.current_resolution(), ProxyResolution::Quarter);
66
67        switcher.switch_to(ProxyResolution::Half);
68        assert_eq!(switcher.current_resolution(), ProxyResolution::Half);
69    }
70}