feat: ✨ ice local and remote changes
This commit is contained in:
parent
4f874d8789
commit
3b122dc4f7
6 changed files with 88 additions and 27 deletions
|
@ -23,8 +23,10 @@ web-sys = { version = "0.3.77", features = [
|
|||
"RtcConfiguration",
|
||||
"RtcIceCandidate",
|
||||
"RtcIceCandidateInit",
|
||||
"RtcIceConnectionState",
|
||||
"RtcIceServer",
|
||||
"RtcPeerConnection",
|
||||
"RtcPeerConnectionIceEvent",
|
||||
"RtcPeerConnectionState",
|
||||
"RtcSdpType",
|
||||
"RtcSessionDescription",
|
||||
|
|
|
@ -81,7 +81,9 @@ fn signalling(username: (ReadSignal<String>, WriteSignal<String>)) -> impl IntoV
|
|||
|
||||
fn rtc_offer(username: ReadSignal<String>) -> impl IntoView {
|
||||
let offer_trigger = move || {
|
||||
spawn_local(offer(username.get()));
|
||||
spawn_local(async move {
|
||||
let peer_connection = offer(username.get()).await;
|
||||
});
|
||||
};
|
||||
|
||||
let offer_button = button()
|
||||
|
@ -95,7 +97,9 @@ fn rtc_offer(username: ReadSignal<String>) -> impl IntoView {
|
|||
|
||||
fn rtc_answer(username: ReadSignal<String>) -> impl IntoView {
|
||||
let answer_trigger = move || {
|
||||
spawn_local(answer(username.get()));
|
||||
spawn_local(async move {
|
||||
let peer_connection = answer(username.get()).await;
|
||||
});
|
||||
};
|
||||
|
||||
let answer_button = button()
|
||||
|
|
|
@ -1,16 +1,67 @@
|
|||
use leptos::logging::log;
|
||||
use leptos::{
|
||||
logging::log,
|
||||
prelude::{Set, Signal, Update, Write, WriteSignal},
|
||||
task::spawn_local,
|
||||
};
|
||||
use wasm_bindgen_futures::JsFuture;
|
||||
use web_sys::{
|
||||
RtcConfiguration, RtcIceServer, RtcPeerConnection, RtcSdpType, RtcSessionDescriptionInit,
|
||||
RtcConfiguration, RtcIceCandidate, RtcIceCandidateInit, RtcIceServer, RtcPeerConnection,
|
||||
RtcPeerConnectionIceEvent, RtcSdpType, RtcSessionDescriptionInit,
|
||||
js_sys::{Array, Reflect},
|
||||
wasm_bindgen::{JsCast, JsValue},
|
||||
wasm_bindgen::{JsCast, JsValue, prelude::Closure},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
signal::{receive_answer, receive_offer, send_answer, send_offer},
|
||||
signal::{
|
||||
receive_answer, receive_ice_candidate, receive_offer, send_answer, send_ice_candidate,
|
||||
send_offer,
|
||||
},
|
||||
sleep,
|
||||
};
|
||||
|
||||
async fn local_ice_candidate_handler(username: &String, peer_connection: &RtcPeerConnection) {
|
||||
let local_ice_candidate_handler: Box<dyn Fn(String, RtcPeerConnectionIceEvent)> =
|
||||
Box::new(move |username, peer_connection_ice_event| {
|
||||
spawn_local(async move {
|
||||
{
|
||||
let username: &String = &username;
|
||||
async move {
|
||||
sleep(1000).await;
|
||||
|
||||
if let Some(peer_connection_ice_candidate) =
|
||||
peer_connection_ice_event.candidate()
|
||||
{
|
||||
let peer_connection_ice_candidate =
|
||||
peer_connection_ice_candidate.as_string().unwrap();
|
||||
send_ice_candidate(username, &peer_connection_ice_candidate)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
.await
|
||||
});
|
||||
});
|
||||
|
||||
let local_ice_candidate_handler = Closure::wrap(local_ice_candidate_handler);
|
||||
|
||||
peer_connection.set_onicecandidate(Some(local_ice_candidate_handler.as_ref().unchecked_ref()));
|
||||
}
|
||||
|
||||
async fn remote_ice_candidate_handler(username: &String, peer_connection: &RtcPeerConnection) {
|
||||
if let Ok(user_and_signal) = receive_ice_candidate(username).await {
|
||||
let peer_connection_ice_candidate_init =
|
||||
RtcIceCandidateInit::new(&user_and_signal.signal.get_data());
|
||||
let peer_connection_ice_candidate =
|
||||
RtcIceCandidate::new(&peer_connection_ice_candidate_init).unwrap();
|
||||
let peer_connection_add_ice_candidate_promise = peer_connection
|
||||
.add_ice_candidate_with_opt_rtc_ice_candidate(Some(&peer_connection_ice_candidate));
|
||||
JsFuture::from(peer_connection_add_ice_candidate_promise)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_peer_connection_with_configuration() -> RtcPeerConnection {
|
||||
let ice_server_addresses = vec![JsValue::from("stun:stun.l.google.com:19302")]
|
||||
.into_iter()
|
||||
|
@ -23,7 +74,7 @@ async fn create_peer_connection_with_configuration() -> RtcPeerConnection {
|
|||
RtcPeerConnection::new_with_configuration(&rtc_configuration).unwrap()
|
||||
}
|
||||
|
||||
pub async fn offer(username: String) {
|
||||
pub async fn offer(username: String) -> RtcPeerConnection {
|
||||
let peer_connection = create_peer_connection_with_configuration().await;
|
||||
let peer_connection_create_offer_promise = peer_connection.create_offer();
|
||||
let peer_connection_session_offer = JsFuture::from(peer_connection_create_offer_promise)
|
||||
|
@ -49,7 +100,7 @@ pub async fn offer(username: String) {
|
|||
log!("Error: Send Offer | {}", err_val)
|
||||
}
|
||||
|
||||
for _ in 0..10 {
|
||||
loop {
|
||||
match receive_answer(&username).await {
|
||||
Ok(received_user_and_signal_answer) => {
|
||||
log!("{:#?}", received_user_and_signal_answer);
|
||||
|
@ -57,17 +108,18 @@ pub async fn offer(username: String) {
|
|||
RtcSessionDescriptionInit::new(RtcSdpType::Answer);
|
||||
peer_connection_session_answer
|
||||
.set_sdp(received_user_and_signal_answer.signal.get_data().as_str());
|
||||
sleep(1000).await;
|
||||
JsFuture::from(
|
||||
peer_connection.set_remote_description(&peer_connection_session_answer),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
for _ in 0..100 {
|
||||
log!("{:#?}", peer_connection.connection_state());
|
||||
sleep(1000).await;
|
||||
}
|
||||
break;
|
||||
log!("{:#?}", peer_connection.connection_state());
|
||||
log!("{:#?}", peer_connection.ice_connection_state());
|
||||
|
||||
local_ice_candidate_handler(&username, &peer_connection).await;
|
||||
return peer_connection;
|
||||
}
|
||||
Err(err_val) => log!("Error: Receive Answer | {}", err_val),
|
||||
}
|
||||
|
@ -75,10 +127,11 @@ pub async fn offer(username: String) {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn answer(username: String) {
|
||||
for _ in 0..10 {
|
||||
pub async fn answer(username: String) -> RtcPeerConnection {
|
||||
loop {
|
||||
match receive_offer(&username).await {
|
||||
Ok(received_user_and_signal_offer) => {
|
||||
log!("{:#?}", received_user_and_signal_offer);
|
||||
let peer_connection = create_peer_connection_with_configuration().await;
|
||||
let peer_connection_session_offer =
|
||||
RtcSessionDescriptionInit::new(RtcSdpType::Offer);
|
||||
|
@ -107,12 +160,12 @@ pub async fn answer(username: String) {
|
|||
let data = session_answer;
|
||||
|
||||
send_answer(&username, &data).await.unwrap();
|
||||
log!("{:#?}", peer_connection.connection_state());
|
||||
log!("{:#?}", peer_connection.ice_connection_state());
|
||||
|
||||
for _ in 0..100 {
|
||||
log!("{:#?}", peer_connection.connection_state());
|
||||
sleep(1000).await;
|
||||
}
|
||||
break;
|
||||
local_ice_candidate_handler(&username, &peer_connection).await;
|
||||
|
||||
return peer_connection;
|
||||
}
|
||||
Err(err_val) => log!("Error: Receive Offer | {}", err_val),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue