fix(cast): keep Chromecast session alive

This commit is contained in:
vicrodh committed 2026-01-12 14:26:12 -06:00
1 parent 7287da5b27
commit 0acfb5343b
2 files changed
+22 -3

No files matched your search

+14 -3
View File
@@ -3,8 +3,9 @@
//! Since rust_cast uses Rc (not Arc), it cannot be shared across threads.
//! This module provides a thread-safe wrapper using channels.
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::thread::{self, JoinHandle};
use std::time::Duration;
use crate::cast::device::CastDeviceConnection;
use crate::cast::errors::CastError;
@@ -195,14 +196,24 @@ impl Drop for ChromecastHandle {
}
}
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(25);
/// Main loop for the Chromecast thread
fn chromecast_thread_main(receiver: Receiver<CastCommand>) {
let mut connection: Option<CastDeviceConnection> = None;
loop {
let command = match receiver.recv() {
let command = match receiver.recv_timeout(HEARTBEAT_INTERVAL) {
Ok(cmd) => cmd,
Err(_) => break, // Channel closed
Err(RecvTimeoutError::Timeout) => {
if let Some(conn) = connection.as_ref() {
if let Err(err) = conn.heartbeat() {
log::warn!("Chromecast heartbeat failed: {}", err);
}
}
continue;
}
Err(RecvTimeoutError::Disconnected) => break, // Channel closed
};
match command {
+8
View File
@@ -69,6 +69,14 @@ impl CastDeviceConnection {
})
}
/// Keep the connection alive with a heartbeat ping
pub fn heartbeat(&self) -> Result<(), CastError> {
self.device
.heartbeat
.ping()
.map_err(|e| CastError::Connection(e.to_string()))
}
/// Disconnect from device
pub fn disconnect(&mut self) -> Result<(), CastError> {
if let Some(session) = &self.session {