feat: webrtc answer

This commit is contained in:
Ahmet Kaan Gümüş 2025-04-25 02:32:40 +03:00
parent 5a84d8fda7
commit cde03367c3

View file

@ -8,7 +8,10 @@ use web_sys::{
wasm_bindgen::{JsCast, JsValue, prelude::Closure}, wasm_bindgen::{JsCast, JsValue, prelude::Closure},
}; };
use crate::signal::{receive_answer, receive_ice_candidate, send_ice_candidate, send_offer}; use crate::signal::{
receive_answer, receive_ice_candidate, receive_offer, send_answer, send_ice_candidate,
send_offer,
};
pub struct WebRTC { pub struct WebRTC {
audio_stream: Option<MediaStream>, audio_stream: Option<MediaStream>,
@ -160,4 +163,58 @@ impl WebRTC {
} }
return Err(Error::WebRTCOffer); return Err(Error::WebRTCOffer);
} }
async fn answer(&mut self) -> Result<(), Error> {
if let Ok(received_offer) = receive_offer() {
let offer = RtcSessionDescriptionInit::new(RtcSdpType::Offer);
offer.set_sdp(&received_offer.get_data());
let set_remote_description_promise =
self.peer_connection.set_remote_description(&offer);
JsFuture::from(set_remote_description_promise)
.await
.map_err(|_| Error::WebRTCSetRemoteDescription)?;
let answer_promise = self.peer_connection.create_answer();
match JsFuture::from(answer_promise)
.await
.map_err(|_| Error::WebRTCAnswer)
{
Ok(answer) => {
let answer_session_description_protocol =
Reflect::get(&answer, &JsValue::from_str("sdp"))
.map_err(|_| Error::WebRTCSessionDescriptionProtocol)?;
match answer_session_description_protocol
.as_string()
.ok_or_else(|| Error::WebRTCAnswer)
{
Ok(answer_session_description_protocol) => {
let answer = RtcSessionDescriptionInit::new(RtcSdpType::Answer);
answer.set_sdp(&answer_session_description_protocol);
let set_local_description_promise =
self.peer_connection.set_local_description(&answer);
JsFuture::from(set_local_description_promise)
.await
.map_err(|_| Error::WebRTCSetLocalDescription)?;
send_answer(&answer_session_description_protocol)?;
return Ok(());
}
Err(err_val) => {
log!(
"Error: WebRTC Answer | Session Description Protocol String Transformation | {}",
err_val
);
}
}
}
Err(err_val) => {
log!("Error: WebRTC Answer | Promise | {}", err_val);
}
}
}
Err(Error::WebRTCAnswer)
}
} }