feat: ✨ webrtc answer
This commit is contained in:
parent
5a84d8fda7
commit
cde03367c3
1 changed files with 58 additions and 1 deletions
|
@ -8,7 +8,10 @@ use web_sys::{
|
|||
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 {
|
||||
audio_stream: Option<MediaStream>,
|
||||
|
@ -160,4 +163,58 @@ impl WebRTC {
|
|||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue