From 123bac1fdfc72745de8244579d10f77b42dd8ff5 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 5 Nov 2025 19:49:59 +0000 Subject: [PATCH] Add register_audio_cache/register_cover_cache functions to pmoupnp Fix "PlaylistManager not initialized" error in play_and_cache example. The error occurred because pmoplaylist's WriteHandle calls pmoupnp::get_audio_cache() to validate cache pks, but the global cache registry wasn't initialized. Changes: - Add register_audio_cache() and register_cover_cache() functions - Export them from pmoupnp lib.rs - Call them in play_and_cache example after creating caches This mirrors how UpnpServer initializes the cache registry. --- pmoparadise/examples/play_and_cache.rs | 6 ++++ pmoupnp/src/cache_registry.rs | 38 ++++++++++++++++++++++++++ pmoupnp/src/lib.rs | 4 ++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pmoparadise/examples/play_and_cache.rs b/pmoparadise/examples/play_and_cache.rs index 38b8f17c..6a8b77f8 100644 --- a/pmoparadise/examples/play_and_cache.rs +++ b/pmoparadise/examples/play_and_cache.rs @@ -100,6 +100,12 @@ async fn main() -> Result<(), Box> { )?); tracing::debug!("Cover cache initialized at: {}", cover_cache_dir); + // Enregistrer les caches dans le registre global pmoupnp + // (requis par pmoplaylist pour valider les pks) + pmoupnp::register_audio_cache(audio_cache.clone()); + pmoupnp::register_cover_cache(cover_cache.clone()); + tracing::debug!("Caches registered in pmoupnp global registry"); + // Utiliser le gestionnaire de playlist singleton tracing::info!("Getting playlist manager..."); let playlist_manager = pmoplaylist::PlaylistManager(); diff --git a/pmoupnp/src/cache_registry.rs b/pmoupnp/src/cache_registry.rs index bd265296..8c4d87da 100644 --- a/pmoupnp/src/cache_registry.rs +++ b/pmoupnp/src/cache_registry.rs @@ -157,6 +157,44 @@ pub fn get_audio_cache() -> Option> { CACHE_REGISTRY.read().unwrap().audio_cache() } +/// Enregistre le cache audio global +/// +/// Cette fonction doit être appelée au démarrage de l'application +/// pour rendre le cache audio disponible globalement. +/// +/// # Examples +/// +/// ```rust,ignore +/// use pmoupnp::cache_registry::register_audio_cache; +/// use pmoaudiocache::Cache as AudioCache; +/// use std::sync::Arc; +/// +/// let audio_cache = Arc::new(AudioCache::new("./cache", 1000)?); +/// register_audio_cache(audio_cache); +/// ``` +pub fn register_audio_cache(cache: Arc) { + CACHE_REGISTRY.write().unwrap().set_audio_cache(cache); +} + +/// Enregistre le cache de couvertures global +/// +/// Cette fonction doit être appelée au démarrage de l'application +/// pour rendre le cache de couvertures disponible globalement. +/// +/// # Examples +/// +/// ```rust,ignore +/// use pmoupnp::cache_registry::register_cover_cache; +/// use pmocovers::Cache as CoverCache; +/// use std::sync::Arc; +/// +/// let cover_cache = Arc::new(CoverCache::new("./covers", 100)?); +/// register_cover_cache(cover_cache); +/// ``` +pub fn register_cover_cache(cache: Arc) { + CACHE_REGISTRY.write().unwrap().set_cover_cache(cache); +} + /// Construit l'URL complète pour une couverture /// /// Fonction globale qui utilise le registre de caches pour construire l'URL. diff --git a/pmoupnp/src/lib.rs b/pmoupnp/src/lib.rs index 904829d7..4442472a 100644 --- a/pmoupnp/src/lib.rs +++ b/pmoupnp/src/lib.rs @@ -16,7 +16,9 @@ pub mod variable_types; use std::sync::RwLock; use std::{collections::HashMap, sync::Arc}; -pub use crate::cache_registry::{get_audio_cache, get_cover_cache}; +pub use crate::cache_registry::{ + get_audio_cache, get_cover_cache, register_audio_cache, register_cover_cache, +}; pub use crate::object_trait::*; pub use crate::upnp_server::UpnpServerExt;