Skip to content

Commit

Permalink
whep return link
Browse files Browse the repository at this point in the history
  • Loading branch information
hongcha98 committed Dec 16, 2023
1 parent 268d45b commit aa5c4f7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/forward/forward_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl PeerForwardInternal {
video_track_remotes
}

async fn publish_svc_rids(&self) -> Result<Vec<String>> {
pub async fn publish_svc_rids(&self) -> Result<Vec<String>> {
let anchor = self.anchor.read().await.as_ref().cloned();
if let Some(pc) = anchor {
if let Some(rd) = pc.remote_description().await {
Expand Down
18 changes: 16 additions & 2 deletions src/forward/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Cursor;
use std::sync::Arc;

use anyhow::Result;
use anyhow::{Ok, Result};
use log::info;
use tokio::sync::Mutex;
use webrtc::ice_transport::ice_candidate::RTCIceCandidateInit;
Expand All @@ -13,6 +13,7 @@ use webrtc::rtp_transceiver::rtp_codec::RTPCodecType;
use webrtc::sdp::{MediaDescription, SessionDescription};

use crate::forward::forward_internal::{get_peer_key, PeerForwardInternal};
use crate::layer::Layer;
use crate::AppError;
use crate::{media, metrics};

Expand Down Expand Up @@ -155,8 +156,21 @@ impl PeerForward {
pub async fn remove_peer(&self, key: String) -> Result<bool> {
self.internal.remove_peer(key).await
}
}

pub async fn layers(&self) -> Result<Vec<Layer>> {
if self.internal.publish_is_svc().await {
let mut layers = vec![];
for rid in self.internal.publish_svc_rids().await? {
layers.push(Layer {
encoding_id: rid.to_owned(),
});
}
Ok(layers)
} else {
Err(anyhow::anyhow!("not layers"))
}
}
}
async fn peer_complete(
offer: RTCSessionDescription,
peer: Arc<RTCPeerConnection>,
Expand Down
8 changes: 8 additions & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone)]
pub struct Layer {
#[serde(rename = "encodingId")]
pub encoding_id: String,
// TODO Other fields
}
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::config::Config;
mod auth;
mod config;
mod forward;
mod layer;
mod media;
mod metrics;
mod path;
Expand Down Expand Up @@ -186,14 +187,18 @@ async fn whep(
return Err(anyhow::anyhow!("Content-Type must be application/sdp").into());
}
let offer = RTCSessionDescription::offer(body)?;
let (answer, key) = state.paths.subscribe(id, offer).await?;
Ok(Response::builder()
let (answer, key) = state.paths.subscribe(id.clone(), offer).await?;
let mut builder = Response::builder()
.status(StatusCode::CREATED)
.header("Content-Type", "application/sdp")
.header("Accept-Patch", "application/trickle-ice-sdpfrag")
.header("E-Tag", key)
.header("Location", uri.to_string())
.body(answer.sdp)?)
.header("Location", uri.to_string());
if state.paths.layers(id).await.is_ok() {
builder = builder.header("Link", format!("<{}/layer>; rel=\"urn:ietf:params:whep:ext:core:layer\"", uri.to_string()))
.header("Link", format!("<{}/sse_info>; rel=\"urn:ietf:params:whep:ext:core:server-sent-events\"; events=\"layers\"", uri.to_string()))
}
Ok(builder.body(answer.sdp)?)
}

async fn add_ice_candidate(
Expand Down Expand Up @@ -279,6 +284,7 @@ fn string_encoder(s: &impl ToString) -> String {
}

pub type AppResult<T> = Result<T, AppError>;

#[derive(Debug, Error)]
pub enum AppError {
#[error("resource not found:{0}")]
Expand All @@ -304,11 +310,13 @@ impl IntoResponse for AppError {
}
}
}

impl From<http::Error> for AppError {
fn from(err: http::Error) -> Self {
AppError::InternalServerError(err.into())
}
}

impl From<ToStrError> for AppError {
fn from(err: ToStrError) -> Self {
AppError::InternalServerError(err.into())
Expand Down
12 changes: 12 additions & 0 deletions src/path/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use webrtc::{
};

use crate::forward::PeerForward;
use crate::layer::Layer;
use crate::AppError;

#[derive(Clone)]
Expand Down Expand Up @@ -91,4 +92,15 @@ impl Manager {
}
Ok(())
}

pub async fn layers(&self, path: String) -> Result<Vec<Layer>> {
let paths = self.paths.read().await;
let forward = paths.get(&path).cloned();
drop(paths);
if let Some(forward) = forward {
forward.layers().await
} else {
Err(anyhow::anyhow!("resource not exists"))
}
}
}

0 comments on commit aa5c4f7

Please sign in to comment.