Extract constants and add config loader + example appsettings for niom-webrtc; use STUN constant in media manager
This commit is contained in:
parent
676a7fae24
commit
2250859ff9
5
appsettings.example.json
Normal file
5
appsettings.example.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"server": {
|
||||||
|
"stun_server": "stun:stun.l.google.com:19302"
|
||||||
|
}
|
||||||
|
}
|
||||||
24
src/config.rs
Normal file
24
src/config.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub struct ServerOptions {
|
||||||
|
pub stun_server: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub struct Config {
|
||||||
|
pub server: ServerOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn from_file<P: AsRef<Path>>(p: P) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
let s = std::fs::read_to_string(p)?;
|
||||||
|
let cfg: Config = serde_json::from_str(&s)?;
|
||||||
|
Ok(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_default() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
Self::from_file("appsettings.json")
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/constants.rs
Normal file
4
src/constants.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// Central constants for niom-webrtc
|
||||||
|
pub const DEFAULT_STUN_SERVER: &str = "stun:stun.l.google.com:19302";
|
||||||
|
pub const ASSET_FAVICON: &str = "/assets/favicon.ico";
|
||||||
|
pub const ASSET_MAIN_CSS: &str = "/assets/main.css";
|
||||||
10
src/main.rs
10
src/main.rs
@ -3,6 +3,8 @@
|
|||||||
mod components;
|
mod components;
|
||||||
mod models;
|
mod models;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
mod constants;
|
||||||
|
mod config;
|
||||||
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use log::Level;
|
use log::Level;
|
||||||
@ -30,10 +32,10 @@ fn App() -> Element {
|
|||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Content() -> Element {
|
pub fn Content() -> Element {
|
||||||
let mut peer_id = use_signal(|| "peer-loading...".to_string());
|
let peer_id = use_signal(|| "peer-loading...".to_string());
|
||||||
let mut remote_id = use_signal(|| String::new());
|
let remote_id = use_signal(|| String::new());
|
||||||
let mut connected = use_signal(|| false);
|
let connected = use_signal(|| false);
|
||||||
let mut websocket = use_signal(|| None::<BrowserWebSocket>);
|
let websocket = use_signal(|| None::<BrowserWebSocket>);
|
||||||
let initiator_connection = use_signal(|| None::<RtcPeerConnection>);
|
let initiator_connection = use_signal(|| None::<RtcPeerConnection>);
|
||||||
let responder_connection = use_signal(|| None::<RtcPeerConnection>);
|
let responder_connection = use_signal(|| None::<RtcPeerConnection>);
|
||||||
// globaler Signal für den lokal freigegebenen MediaStream (Mikrofon)
|
// globaler Signal für den lokal freigegebenen MediaStream (Mikrofon)
|
||||||
|
|||||||
@ -25,7 +25,8 @@ impl MediaManager {
|
|||||||
pub fn create_peer_connection() -> Result<RtcPeerConnection, String> {
|
pub fn create_peer_connection() -> Result<RtcPeerConnection, String> {
|
||||||
let ice_server = RtcIceServer::new();
|
let ice_server = RtcIceServer::new();
|
||||||
let urls = js_sys::Array::new();
|
let urls = js_sys::Array::new();
|
||||||
urls.push(&JsValue::from_str("stun:stun.l.google.com:19302"));
|
// Use centralized default STUN server constant
|
||||||
|
urls.push(&JsValue::from_str(crate::constants::DEFAULT_STUN_SERVER));
|
||||||
ice_server.set_urls(&urls.into());
|
ice_server.set_urls(&urls.into());
|
||||||
let config = RtcConfiguration::new();
|
let config = RtcConfiguration::new();
|
||||||
let servers = js_sys::Array::new();
|
let servers = js_sys::Array::new();
|
||||||
@ -51,7 +52,7 @@ impl MediaManager {
|
|||||||
.ok_or_else(|| "SDP field was not a string".to_string())?;
|
.ok_or_else(|| "SDP field was not a string".to_string())?;
|
||||||
|
|
||||||
// 3. Init-Objekt bauen und SDP setzen
|
// 3. Init-Objekt bauen und SDP setzen
|
||||||
let mut init = RtcSessionDescriptionInit::new(RtcSdpType::Offer);
|
let init = RtcSessionDescriptionInit::new(RtcSdpType::Offer);
|
||||||
init.set_sdp(&sdp);
|
init.set_sdp(&sdp);
|
||||||
|
|
||||||
// 4. Local Description setzen
|
// 4. Local Description setzen
|
||||||
@ -112,7 +113,7 @@ impl MediaManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut init = RtcSessionDescriptionInit::new(RtcSdpType::Answer);
|
let init = RtcSessionDescriptionInit::new(RtcSdpType::Answer);
|
||||||
init.set_sdp(answer_sdp);
|
init.set_sdp(answer_sdp);
|
||||||
|
|
||||||
JsFuture::from(pc.set_remote_description(&init))
|
JsFuture::from(pc.set_remote_description(&init))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user