diff options
author | metamuffin <metamuffin@disroot.org> | 2022-10-07 19:59:43 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-10-07 19:59:43 +0200 |
commit | 439428f5c3967f5dd76db5540e085cdd91e7b747 (patch) | |
tree | 35be38807c121c833439025721f8bf386b45a003 /client-native-lib/src/peer.rs | |
parent | 8f07699fbe594e1ec007fbc908adced89fc41f7e (diff) | |
download | keks-meet-439428f5c3967f5dd76db5540e085cdd91e7b747.tar keks-meet-439428f5c3967f5dd76db5540e085cdd91e7b747.tar.bz2 keks-meet-439428f5c3967f5dd76db5540e085cdd91e7b747.tar.zst |
some work on native lib
Diffstat (limited to 'client-native-lib/src/peer.rs')
-rw-r--r-- | client-native-lib/src/peer.rs | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/client-native-lib/src/peer.rs b/client-native-lib/src/peer.rs index d45d0e5..5e672e6 100644 --- a/client-native-lib/src/peer.rs +++ b/client-native-lib/src/peer.rs @@ -4,13 +4,17 @@ Copyright (C) 2022 metamuffin <metamuffin@disroot.org> */ use crate::{ - protocol::{self, RelayMessage, Sdp}, + protocol::{self, ProvideInfo, RelayMessage, Sdp}, state::State, }; use log::info; -use std::sync::Arc; +use std::{collections::HashMap, sync::Arc}; +use tokio::sync::RwLock; use webrtc::{ - ice_transport::{ice_candidate::RTCIceCandidate, ice_server::RTCIceServer}, + ice_transport::{ + ice_candidate::{RTCIceCandidate, RTCIceCandidateInit}, + ice_server::RTCIceServer, + }, peer_connection::{ configuration::RTCConfiguration, peer_connection_state::RTCPeerConnectionState, sdp::session_description::RTCSessionDescription, RTCPeerConnection, @@ -20,6 +24,7 @@ use webrtc::{ pub struct Peer { pub state: Arc<State>, pub peer_connection: RTCPeerConnection, + pub resources_provided: RwLock<HashMap<String, ProvideInfo>>, pub id: usize, } @@ -36,6 +41,7 @@ impl Peer { let peer_connection = state.api.new_peer_connection(config).await.unwrap(); let peer = Arc::new(Self { + resources_provided: Default::default(), state: state.clone(), peer_connection, id, @@ -92,22 +98,53 @@ impl Peer { pub async fn on_relay(&self, p: RelayMessage) { match p { - protocol::RelayMessage::Offer(o) => self.on_offer(o).await, - protocol::RelayMessage::Answer(a) => self.on_answer(a).await, - protocol::RelayMessage::IceCandidate(c) => { - info!("received ICE candidate"); - self.peer_connection.add_ice_candidate(c).await.unwrap(); + RelayMessage::Offer(o) => self.on_offer(o).await, + RelayMessage::Answer(a) => self.on_answer(a).await, + RelayMessage::IceCandidate(c) => self.on_remote_ice_candidate(c).await, + RelayMessage::Provide(info) => { + info!( + "remote resource provided: ({:?}) {:?} {:?}", + info.id, info.kind, info.label + ); + self.resources_provided + .write() + .await + .insert(info.id.clone(), info.clone()); + self.state + .event_handler + .remote_resource_added(&self, info) + .await; + } + RelayMessage::ProvideStop { id } => { + info!("remote resource removed: ({:?}) ", id); + self.resources_provided.write().await.remove(&id); + self.state + .event_handler + .remote_resource_removed(&self, id) + .await; } _ => (), } } + pub async fn on_leave(&self) { + info!("({}) peer left", self.id); + } + pub async fn on_ice_candidate(&self, candidate: RTCIceCandidate) { + info!("publishing local ICE candidate"); self.send_relay(RelayMessage::IceCandidate( candidate.to_json().await.unwrap(), )) .await; } + pub async fn on_remote_ice_candidate(&self, candidate: RTCIceCandidateInit) { + info!("adding remote ICE candidate"); + self.peer_connection + .add_ice_candidate(candidate) + .await + .unwrap(); + } pub async fn on_negotiation_needed(self: Arc<Self>) { info!("({}) negotiation needed", self.id); |