added support for running on non-webrtc OSs
This commit is contained in:
parent
cf5e0f9386
commit
1777b5e39f
5 changed files with 89 additions and 15 deletions
|
@ -18,6 +18,7 @@ use joystick_source::joystick_loop;
|
|||
use satellite_connection::SatelliteConnection;
|
||||
|
||||
pub enum ApplicationEvent {
|
||||
SupportsWebRTC(bool),
|
||||
WebRTCMessage((String, vcs_common::ApplicationMessage)),
|
||||
JoystickMove(Point),
|
||||
RetryDisconnectedSatellites,
|
||||
|
@ -40,6 +41,7 @@ pub struct AppState {
|
|||
to_mec: Sender<ApplicationEvent>,
|
||||
mec: Receiver<ApplicationEvent>,
|
||||
pub runtime: Handle,
|
||||
pub has_webrtc_support: Option<bool>,
|
||||
|
||||
_config: Arc<tokio::sync::RwLock<AppConfig>>,
|
||||
|
||||
|
@ -70,6 +72,7 @@ impl AppState {
|
|||
to_mec,
|
||||
mec,
|
||||
runtime: rt,
|
||||
has_webrtc_support: None,
|
||||
|
||||
_config: config,
|
||||
|
||||
|
@ -119,18 +122,28 @@ pub async fn run_main_event_loop(
|
|||
Err(TryRecvError::Closed) => break,
|
||||
Ok(msg) => {
|
||||
match msg {
|
||||
ApplicationEvent::SupportsWebRTC(has_support) => {
|
||||
state.has_webrtc_support = Some(has_support);
|
||||
state
|
||||
.camera_satellites
|
||||
.iter_mut()
|
||||
.filter(|x| x.is_connected())
|
||||
.for_each(|x| {
|
||||
x.send_blocking(
|
||||
ApplicationMessage::ConnectionSupportsWebRTC(has_support),
|
||||
state.runtime.clone(),
|
||||
);
|
||||
});
|
||||
}
|
||||
ApplicationEvent::Close => {
|
||||
state.mec.close(); // cleanup is handled on reading from a closed mec
|
||||
}
|
||||
ApplicationEvent::RetryDisconnectedSatellites => {
|
||||
state
|
||||
.camera_satellites
|
||||
.iter_mut()
|
||||
.for_each(|x| {
|
||||
info!("Resetting connections");
|
||||
x.try_connecting = true;
|
||||
x.retry_attempts = 0;
|
||||
});
|
||||
state.camera_satellites.iter_mut().for_each(|x| {
|
||||
info!("Resetting connections");
|
||||
x.try_connecting = true;
|
||||
x.retry_attempts = 0;
|
||||
});
|
||||
}
|
||||
ApplicationEvent::WebRTCMessage((name, msg)) => {
|
||||
info!(
|
||||
|
@ -202,14 +215,22 @@ pub async fn run_main_event_loop(
|
|||
ApplicationMessage::ConnectionSupportsWebRTCRequest => {
|
||||
let does_support_webrtc = true;
|
||||
if let Err(e) = connection
|
||||
.send(ApplicationMessage::ConnectionSupportsWebRTC(does_support_webrtc))
|
||||
.send(ApplicationMessage::ConnectionSupportsWebRTC(
|
||||
does_support_webrtc,
|
||||
))
|
||||
.await
|
||||
{
|
||||
info!("Was not able to send webrtc support status to remote: {:?}", e);
|
||||
info!(
|
||||
"Was not able to send webrtc support status to remote: {:?}",
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
ApplicationMessage::ConnectionSupportsWebRTC(does_support) => {
|
||||
info!("Cool, the camera satellite supports webrtc: {}", does_support);
|
||||
info!(
|
||||
"Cool, the camera satellite supports webrtc: {}",
|
||||
does_support
|
||||
);
|
||||
}
|
||||
ApplicationMessage::WebRTCIceCandidateInit(pkt) => {
|
||||
send_frontend_message(serde_json::to_string(&pkt).unwrap())
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::sync::{atomic::AtomicBool, Arc};
|
|||
|
||||
use async_channel::TryRecvError;
|
||||
use tokio::runtime::Handle;
|
||||
use tracing::{error, instrument, warn, info};
|
||||
use tracing::{error, info, instrument, warn};
|
||||
|
||||
use crate::config::ConnectionString;
|
||||
|
||||
|
@ -47,7 +47,13 @@ impl SatelliteConnection {
|
|||
#[instrument(skip(self))]
|
||||
pub async fn close(&mut self) {
|
||||
if self.to_socket.is_some() {
|
||||
if let Err(_) = self.to_socket.as_ref().unwrap().send(ApplicationMessage::CloseConnection).await {
|
||||
if let Err(_) = self
|
||||
.to_socket
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.send(ApplicationMessage::CloseConnection)
|
||||
.await
|
||||
{
|
||||
info!("Could not send close connection to active satellite");
|
||||
}
|
||||
}
|
||||
|
@ -106,6 +112,20 @@ impl SatelliteConnection {
|
|||
}
|
||||
}
|
||||
|
||||
/// This is not the recommneded method, as it ignores error, but sometimes
|
||||
/// you just really don't want to be async
|
||||
#[instrument(skip(self))]
|
||||
pub fn send_blocking(&mut self, msg: ApplicationMessage, rt: tokio::runtime::Handle) {
|
||||
if self.to_socket.is_some() {
|
||||
let sender = self.to_socket.as_ref().unwrap().clone();
|
||||
rt.spawn(async move {
|
||||
if let Err(e) = sender.send(msg).await {
|
||||
error!("Unhandled send error while in send_blocking! {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self))]
|
||||
pub async fn send(&mut self, msg: ApplicationMessage) -> Result<(), SatelliteConnectionError> {
|
||||
if self.to_socket.is_some() {
|
||||
|
|
|
@ -74,7 +74,10 @@ fn main() {
|
|||
to_mec: to_mec.clone(),
|
||||
rt: handle2.clone(),
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![tauri_functions::connect_to_camera])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
tauri_functions::connect_to_camera,
|
||||
tauri_functions::supports_webrtc
|
||||
])
|
||||
.setup(|app| {
|
||||
*APP_HANDLE.lock().unwrap() = Some(app.handle());
|
||||
|
||||
|
|
|
@ -17,3 +17,11 @@ pub fn connect_to_camera(state: State<'_, TauriState>) {
|
|||
let _ = mec.send_blocking(ApplicationEvent::RetryDisconnectedSatellites);
|
||||
});
|
||||
}
|
||||
|
||||
#[tauri::command(rename_all="snake_case")]
|
||||
pub fn supports_webrtc(has_support: bool, state: State<'_, TauriState>) {
|
||||
let mec = state.to_mec.clone();
|
||||
state.rt.spawn(async move {
|
||||
let _ = mec.send_blocking(ApplicationEvent::SupportsWebRTC(has_support));
|
||||
});
|
||||
}
|
||||
|
|
|
@ -16,7 +16,29 @@ function call_camera_connect() {
|
|||
async function init() {
|
||||
console.log("Setting up");
|
||||
document.getElementById("camera_connect_button").addEventListener("click", call_camera_connect);
|
||||
await rtc_init();
|
||||
|
||||
let webrtc_support = supports_webrtc();
|
||||
|
||||
invoke("supports_webrtc", { has_support: webrtc_support });
|
||||
if (webrtc_support) {
|
||||
await rtc_init();
|
||||
}
|
||||
}
|
||||
|
||||
function supports_webrtc() {
|
||||
var isWebRTCSupported = false;
|
||||
|
||||
['RTCPeerConnection', 'webkitRTCPeerConnection', 'mozRTCPeerConnection', 'RTCIceGatherer'].forEach(function(item) {
|
||||
if (isWebRTCSupported) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (item in window) {
|
||||
isWebRTCSupported = true;
|
||||
}
|
||||
});
|
||||
|
||||
return isWebRTCSupported;
|
||||
}
|
||||
|
||||
export { init };
|
||||
|
|
Loading…
Reference in a new issue