Skip to content

Commit

Permalink
api return peer key
Browse files Browse the repository at this point in the history
  • Loading branch information
hongcha98 committed Aug 18, 2023
1 parent 55b55af commit 5d3c014
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
16 changes: 13 additions & 3 deletions src/forward/forward_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ type SenderForwardData = UnboundedSender<ForwardData>;

struct PeerWrap(Arc<RTCPeerConnection>);

pub(crate) fn get_peer_key(peer: Arc<RTCPeerConnection>) -> String {
PeerWrap(peer).get_key().to_string()
}

impl PeerWrap {
fn get_key(&self) -> &str {
self.0.get_stats_id()
}
}

impl Clone for PeerWrap {
fn clone(&self) -> Self {
PeerWrap(self.0.clone())
Expand All @@ -34,17 +44,17 @@ impl Eq for PeerWrap {}

impl PartialEq for PeerWrap {
fn eq(&self, other: &Self) -> bool {
self.0.get_stats_id() == other.0.get_stats_id()
self.get_key() == other.get_key()
}

fn ne(&self, other: &Self) -> bool {
self.0.get_stats_id() != other.0.get_stats_id()
self.get_key() != other.get_key()
}
}

impl Hash for PeerWrap {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.get_stats_id().hash(state);
self.get_key().hash(state);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/forward/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use webrtc::sdp::{MediaDescription, SessionDescription};

use constant::*;

use crate::forward::forward_internal::PeerForwardInternal;
use crate::forward::forward_internal::{get_peer_key, PeerForwardInternal};

mod forward_internal;
pub(crate) mod constant;
Expand All @@ -40,7 +40,7 @@ impl PeerForward {
self.internal.id.clone()
}

pub async fn set_anchor(&self, offer: RTCSessionDescription) -> Result<RTCSessionDescription> {
pub async fn set_anchor(&self, offer: RTCSessionDescription) -> Result<(RTCSessionDescription, String)> {
if self.internal.anchor_is_some().await {
return Err(anyhow::anyhow!("anchor is set"));
}
Expand Down Expand Up @@ -103,14 +103,14 @@ impl PeerForward {
.local_description()
.await
.ok_or(anyhow::anyhow!("failed to get local description"))?;
self.internal.set_anchor(peer).await?;
Ok(description)
self.internal.set_anchor(peer.clone()).await?;
Ok((description, get_peer_key(peer)))
}

pub async fn add_subscribe(
&self,
offer: RTCSessionDescription,
) -> Result<RTCSessionDescription> {
) -> Result<(RTCSessionDescription, String)> {
let peer = new_peer().await?;
let internal = self.internal.clone();
let pc = peer.clone();
Expand Down Expand Up @@ -154,7 +154,7 @@ impl PeerForward {
.local_description()
.await
.ok_or(anyhow::anyhow!("failed to get local description"))?;
Ok(description)
Ok((description, get_peer_key(peer)))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn whip(
original_forward.unwrap().clone()
};
drop(map);
let answer = forward.set_anchor(offer).await?;
let (answer, key) = forward.set_anchor(offer).await?;
if is_none {
let mut map = state.write().await;
if map.contains_key(&id) {
Expand All @@ -71,7 +71,7 @@ async fn whip(
.status(StatusCode::CREATED)
.header("Content-Type", "application/sdp")
.header("Accept-Patch", "application/trickle-ice-sdpfrag")
.header("E-Tag", id)
.header("E-Tag", key)
.header("Location", uri.to_string())
.body(answer.sdp)?)
}
Expand All @@ -90,12 +90,12 @@ async fn whep(
}
let forward = forward.unwrap().clone();
drop(map);
let answer = forward.add_subscribe(offer).await?;
let (answer, key) = forward.add_subscribe(offer).await?;
Ok(Response::builder()
.status(StatusCode::CREATED)
.header("Content-Type", "application/sdp")
.header("Accept-Patch", "application/trickle-ice-sdpfrag")
.header("E-Tag", forward.get_id())
.header("E-Tag", key)
.header("Location", uri.to_string())
.body(answer.sdp)?)
}
Expand Down

0 comments on commit 5d3c014

Please sign in to comment.