Initial commit
This commit is contained in:
222
src-tauri/src/lib.rs
Normal file
222
src-tauri/src/lib.rs
Normal file
@@ -0,0 +1,222 @@
|
||||
use std::collections::HashMap;
|
||||
use std::net::IpAddr;
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
use mdns_sd::{ServiceDaemon, ServiceEvent};
|
||||
use rust_cast::channels::media::{Media, StreamType};
|
||||
use rust_cast::channels::receiver::CastDeviceApp;
|
||||
use rust_cast::CastDevice;
|
||||
use tauri::State;
|
||||
|
||||
struct AppState {
|
||||
known_devices: Mutex<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn list_cast_devices(state: State<'_, Arc<AppState>>) -> Result<Vec<String>, String> {
|
||||
let devices = state.known_devices.lock().unwrap();
|
||||
let mut list: Vec<String> = devices.keys().cloned().collect();
|
||||
list.sort();
|
||||
Ok(list)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cast_play(
|
||||
state: State<'_, Arc<AppState>>,
|
||||
device_name: String,
|
||||
url: String,
|
||||
) -> Result<(), String> {
|
||||
let ip = {
|
||||
let devices = state.known_devices.lock().unwrap();
|
||||
devices
|
||||
.get(&device_name)
|
||||
.cloned()
|
||||
.ok_or("Device not found")?
|
||||
};
|
||||
|
||||
println!("Connecting to {} ({})", device_name, ip);
|
||||
|
||||
// Run connection logic
|
||||
let ip_addr = IpAddr::from_str(&ip).map_err(|e| e.to_string())?;
|
||||
|
||||
// Connect to port 8009
|
||||
let device = CastDevice::connect_without_host_verification(ip_addr.to_string(), 8009)
|
||||
.map_err(|e| format!("Failed to connect: {:?}", e))?;
|
||||
|
||||
device
|
||||
.connection
|
||||
.connect("receiver-0")
|
||||
.map_err(|e| format!("Failed to connect receiver: {:?}", e))?;
|
||||
|
||||
// Check if Default Media Receiver is already running
|
||||
let app = CastDeviceApp::DefaultMediaReceiver;
|
||||
let status = device
|
||||
.receiver
|
||||
.get_status()
|
||||
.map_err(|e| format!("Failed to get status: {:?}", e))?;
|
||||
|
||||
// Determine if we need to launch or if we can use existing
|
||||
let application = status.applications.iter().find(|a| a.app_id == "CC1AD845"); // Default Media Receiver ID
|
||||
|
||||
let (transport_id, session_id) = if let Some(app_instance) = application {
|
||||
println!(
|
||||
"App already running, joining session {}",
|
||||
app_instance.session_id
|
||||
);
|
||||
(
|
||||
app_instance.transport_id.clone(),
|
||||
app_instance.session_id.clone(),
|
||||
)
|
||||
} else {
|
||||
println!("Launching app...");
|
||||
let app_instance = device
|
||||
.receiver
|
||||
.launch_app(&app)
|
||||
.map_err(|e| format!("Failed to launch app: {:?}", e))?;
|
||||
(app_instance.transport_id, app_instance.session_id)
|
||||
};
|
||||
|
||||
device
|
||||
.connection
|
||||
.connect(&transport_id)
|
||||
.map_err(|e| format!("Failed to connect transport: {:?}", e))?;
|
||||
|
||||
// Load Media
|
||||
let media = Media {
|
||||
content_id: url,
|
||||
stream_type: StreamType::Live, // Live stream
|
||||
content_type: "audio/mp3".to_string(),
|
||||
metadata: None,
|
||||
duration: None,
|
||||
};
|
||||
|
||||
device
|
||||
.media
|
||||
.load(&transport_id, &session_id, &media)
|
||||
.map_err(|e| format!("Failed to load media: {:?}", e))?;
|
||||
|
||||
println!("Playing on {}", device_name);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cast_stop(state: State<'_, Arc<AppState>>, device_name: String) -> Result<(), String> {
|
||||
let ip = {
|
||||
let devices = state.known_devices.lock().unwrap();
|
||||
devices
|
||||
.get(&device_name)
|
||||
.cloned()
|
||||
.ok_or("Device not found")?
|
||||
};
|
||||
|
||||
let ip_addr = IpAddr::from_str(&ip).map_err(|e| e.to_string())?;
|
||||
let device = CastDevice::connect_without_host_verification(ip_addr.to_string(), 8009)
|
||||
.map_err(|e| format!("Failed to connect: {:?}", e))?;
|
||||
|
||||
device.connection.connect("receiver-0").unwrap();
|
||||
let status = device
|
||||
.receiver
|
||||
.get_status()
|
||||
.map_err(|e| format!("{:?}", e))?;
|
||||
|
||||
if let Some(app) = status.applications.first() {
|
||||
let _transport_id = &app.transport_id;
|
||||
// device.connection.connect(transport_id).unwrap();
|
||||
|
||||
device
|
||||
.receiver
|
||||
.stop_app(app.session_id.as_str())
|
||||
.map_err(|e| format!("{:?}", e))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cast_set_volume(
|
||||
state: State<'_, Arc<AppState>>,
|
||||
device_name: String,
|
||||
volume: f32,
|
||||
) -> Result<(), String> {
|
||||
let ip = {
|
||||
let devices = state.known_devices.lock().unwrap();
|
||||
devices
|
||||
.get(&device_name)
|
||||
.cloned()
|
||||
.ok_or("Device not found")?
|
||||
};
|
||||
|
||||
let ip_addr = IpAddr::from_str(&ip).map_err(|e| e.to_string())?;
|
||||
let device = CastDevice::connect_without_host_verification(ip_addr.to_string(), 8009)
|
||||
.map_err(|e| format!("Failed to connect: {:?}", e))?;
|
||||
|
||||
device.connection.connect("receiver-0").unwrap();
|
||||
|
||||
// Volume is on the receiver struct
|
||||
let vol = rust_cast::channels::receiver::Volume {
|
||||
level: Some(volume),
|
||||
muted: None,
|
||||
};
|
||||
|
||||
device
|
||||
.receiver
|
||||
.set_volume(vol)
|
||||
.map_err(|e| format!("{:?}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let app_state = Arc::new(AppState {
|
||||
known_devices: Mutex::new(HashMap::new()),
|
||||
});
|
||||
|
||||
let state_clone = app_state.clone();
|
||||
|
||||
// Start Discovery Thread
|
||||
thread::spawn(move || {
|
||||
let mdns = ServiceDaemon::new().expect("Failed to create daemon");
|
||||
// Google Cast service
|
||||
let receiver = mdns
|
||||
.browse("_googlecast._tcp.local.")
|
||||
.expect("Failed to browse");
|
||||
|
||||
while let Ok(event) = receiver.recv() {
|
||||
match event {
|
||||
ServiceEvent::ServiceResolved(info) => {
|
||||
// Try to get "fn" property for Friendly Name
|
||||
let name = info
|
||||
.get_property_val_str("fn")
|
||||
.or_else(|| Some(info.get_fullname()))
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
if let Some(ip) = info.get_addresses().iter().next() {
|
||||
let ip_str = ip.to_string();
|
||||
let mut devices = state_clone.known_devices.lock().unwrap();
|
||||
if !devices.contains_key(&name) {
|
||||
println!("Discovered Cast Device: {} at {}", name, ip_str);
|
||||
devices.insert(name, ip_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.manage(app_state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
list_cast_devices,
|
||||
cast_play,
|
||||
cast_stop,
|
||||
cast_set_volume
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
6
src-tauri/src/main.rs
Normal file
6
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
radio_tauri_lib::run()
|
||||
}
|
||||
Reference in New Issue
Block a user