diff --git a/src-tauri/src/cast/commands.rs b/src-tauri/src/cast/commands.rs index 30d11a7c7..72449db30 100644 --- a/src-tauri/src/cast/commands.rs +++ b/src-tauri/src/cast/commands.rs @@ -1,49 +1,17 @@ //! Tauri commands for Chromecast casting -use std::sync::Arc; use tauri::State; -use tokio::sync::Mutex; use crate::api::models::Quality; -use crate::cast::chromecast_thread::ChromecastHandle; use crate::cast::{ - CastError, CastPositionInfo, CastStatus, DeviceDiscovery, DiscoveredDevice, MediaMetadata, - MediaServer, + CastError, CastPositionInfo, CastStatus, DiscoveredDevice, MediaMetadata, }; use crate::library::{AudioFormat, LibraryState}; use crate::AppState; -/// Cast state shared across commands -/// Uses a dedicated thread for Chromecast operations since rust_cast is not thread-safe -pub struct CastState { - pub discovery: Arc>, - pub chromecast: ChromecastHandle, - /// Media server is lazily initialized on first cast operation to save CPU when not casting - pub media_server: Arc>>, - pub connected_device_ip: Arc>>, -} - -impl CastState { - pub fn new() -> Result { - Ok(Self { - discovery: Arc::new(Mutex::new(DeviceDiscovery::new())), - chromecast: ChromecastHandle::new(), - // Don't start media server until needed - saves CPU when not casting - media_server: Arc::new(Mutex::new(None)), - connected_device_ip: Arc::new(Mutex::new(None)), - }) - } - - /// Get or create the media server (lazy initialization) - pub async fn get_or_create_media_server(&self) -> Result<(), CastError> { - let mut server_guard = self.media_server.lock().await; - if server_guard.is_none() { - log::info!("Starting media server on demand (lazy init)"); - *server_guard = Some(MediaServer::start()?); - } - Ok(()) - } -} +// CastState lives in `super::state`; re-export from commands so any +// historical `crate::cast::commands::CastState` paths continue to compile. +pub use super::state::CastState; // === Discovery === diff --git a/src-tauri/src/cast/dlna/commands.rs b/src-tauri/src/cast/dlna/commands.rs index 384d26448..ecb48699e 100644 --- a/src-tauri/src/cast/dlna/commands.rs +++ b/src-tauri/src/cast/dlna/commands.rs @@ -1,46 +1,17 @@ //! Tauri commands for DLNA/UPnP casting -use std::sync::Arc; use tauri::State; -use tokio::sync::Mutex; use crate::api::models::Quality; use crate::cast::dlna::{ - DiscoveredDlnaDevice, DlnaConnection, DlnaDiscovery, DlnaError, DlnaMetadata, DlnaPositionInfo, - DlnaStatus, + DiscoveredDlnaDevice, DlnaConnection, DlnaError, DlnaMetadata, DlnaPositionInfo, DlnaStatus, }; -use crate::cast::MediaServer; use crate::AppState; -/// DLNA state shared across commands -pub struct DlnaState { - pub discovery: Arc>, - pub connection: Arc>>, - /// Shared media server (lazily initialized) - pub media_server: Arc>>, -} - -impl DlnaState { - pub fn new(media_server: Arc>>) -> Result { - Ok(Self { - discovery: Arc::new(Mutex::new(DlnaDiscovery::new())), - connection: Arc::new(Mutex::new(None)), - media_server, - }) - } - - /// Ensure media server is started (lazy initialization) - pub async fn ensure_media_server(&self) -> Result<(), DlnaError> { - let mut server_guard = self.media_server.lock().await; - if server_guard.is_none() { - log::info!("Starting media server on demand for DLNA"); - *server_guard = Some(MediaServer::start().map_err(|e| { - DlnaError::Connection(format!("Failed to start media server: {}", e)) - })?); - } - Ok(()) - } -} +// DlnaState lives in `super::state`; re-export from commands so any +// historical `crate::cast::dlna::commands::DlnaState` paths continue +// to compile. +pub use super::state::DlnaState; // === Discovery === diff --git a/src-tauri/src/cast/dlna/mod.rs b/src-tauri/src/cast/dlna/mod.rs index 98160b47b..ad6cafc58 100644 --- a/src-tauri/src/cast/dlna/mod.rs +++ b/src-tauri/src/cast/dlna/mod.rs @@ -3,6 +3,7 @@ //! Core functionality is in qbz-cast crate, this module provides Tauri commands. pub mod commands; +pub mod state; // Re-export from qbz-cast pub use qbz_cast::{ @@ -11,4 +12,4 @@ pub use qbz_cast::{ }; // Re-export Tauri command state -pub use commands::DlnaState; +pub use state::DlnaState; diff --git a/src-tauri/src/cast/dlna/state.rs b/src-tauri/src/cast/dlna/state.rs new file mode 100644 index 000000000..517ac5216 --- /dev/null +++ b/src-tauri/src/cast/dlna/state.rs @@ -0,0 +1,42 @@ +//! Shared state types for the DLNA/UPnP cast module. +//! +//! Originally co-located with the Tauri commands in +//! `cast/dlna/commands.rs`. Relocated here so the type surface +//! remains available after the legacy `#[tauri::command]` definitions +//! in `commands.rs` are eventually removed. + +use std::sync::Arc; +use tokio::sync::Mutex; + +use crate::cast::dlna::{DlnaConnection, DlnaDiscovery, DlnaError}; +use crate::cast::MediaServer; + +/// DLNA state shared across commands +pub struct DlnaState { + pub discovery: Arc>, + pub connection: Arc>>, + /// Shared media server (lazily initialized) + pub media_server: Arc>>, +} + +impl DlnaState { + pub fn new(media_server: Arc>>) -> Result { + Ok(Self { + discovery: Arc::new(Mutex::new(DlnaDiscovery::new())), + connection: Arc::new(Mutex::new(None)), + media_server, + }) + } + + /// Ensure media server is started (lazy initialization) + pub async fn ensure_media_server(&self) -> Result<(), DlnaError> { + let mut server_guard = self.media_server.lock().await; + if server_guard.is_none() { + log::info!("Starting media server on demand for DLNA"); + *server_guard = Some(MediaServer::start().map_err(|e| { + DlnaError::Connection(format!("Failed to start media server: {}", e)) + })?); + } + Ok(()) + } +} diff --git a/src-tauri/src/cast/mod.rs b/src-tauri/src/cast/mod.rs index 12f6021cf..86812b641 100644 --- a/src-tauri/src/cast/mod.rs +++ b/src-tauri/src/cast/mod.rs @@ -10,6 +10,7 @@ pub mod discovery; pub mod dlna; pub mod errors; pub mod media_server; +pub mod state; // Re-export from qbz-cast for internal use pub use qbz_cast::{ @@ -20,5 +21,5 @@ pub use qbz_cast::{ }; // Re-export Tauri command states -pub use commands::CastState; -pub use dlna::commands::DlnaState; +pub use state::CastState; +pub use dlna::state::DlnaState; diff --git a/src-tauri/src/cast/state.rs b/src-tauri/src/cast/state.rs new file mode 100644 index 000000000..09dd33ea0 --- /dev/null +++ b/src-tauri/src/cast/state.rs @@ -0,0 +1,46 @@ +//! Shared state types for the cast (Chromecast) module. +//! +//! Originally co-located with the Tauri commands in `cast/commands.rs`. +//! Relocated here so the type surface remains available after the +//! legacy `#[tauri::command]` definitions in `commands.rs` are +//! eventually removed. + +use std::sync::Arc; +use tokio::sync::Mutex; + +use crate::cast::chromecast_thread::ChromecastHandle; +use crate::cast::{CastError, DeviceDiscovery, MediaServer}; + +/// Cast state shared across commands. +/// +/// Uses a dedicated thread for Chromecast operations since rust_cast +/// is not thread-safe. +pub struct CastState { + pub discovery: Arc>, + pub chromecast: ChromecastHandle, + /// Media server is lazily initialized on first cast operation to save CPU when not casting + pub media_server: Arc>>, + pub connected_device_ip: Arc>>, +} + +impl CastState { + pub fn new() -> Result { + Ok(Self { + discovery: Arc::new(Mutex::new(DeviceDiscovery::new())), + chromecast: ChromecastHandle::new(), + // Don't start media server until needed - saves CPU when not casting + media_server: Arc::new(Mutex::new(None)), + connected_device_ip: Arc::new(Mutex::new(None)), + }) + } + + /// Get or create the media server (lazy initialization) + pub async fn get_or_create_media_server(&self) -> Result<(), CastError> { + let mut server_guard = self.media_server.lock().await; + if server_guard.is_none() { + log::info!("Starting media server on demand (lazy init)"); + *server_guard = Some(MediaServer::start()?); + } + Ok(()) + } +}