Skip to content

Commit

Permalink
Update protocol (#350)
Browse files Browse the repository at this point in the history
* Update protocol.

* generated protobuf

* Handle attribute updates.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
dennwc and github-actions[bot] authored Jun 21, 2024
1 parent 9c2a467 commit 1e47c5c
Show file tree
Hide file tree
Showing 16 changed files with 4,341 additions and 2,174 deletions.
22 changes: 22 additions & 0 deletions livekit-api/src/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ impl Default for VideoGrants {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SIPGrants {
// manage sip resources
pub admin: bool,
// make outbound calls
pub call: bool,
}

impl Default for SIPGrants {
fn default() -> Self {
Self { admin: false, call: false }
}
}

#[derive(Debug, Clone, Serialize, Default, Deserialize)]
#[serde(default)]
#[serde(rename_all = "camelCase")]
Expand All @@ -106,6 +121,7 @@ pub struct Claims {

pub name: String,
pub video: VideoGrants,
pub sip: SIPGrants,
pub sha256: String, // Used to verify the integrity of the message body
pub metadata: String,
}
Expand Down Expand Up @@ -140,6 +156,7 @@ impl AccessToken {
sub: Default::default(),
name: Default::default(),
video: VideoGrants::default(),
sip: SIPGrants::default(),
sha256: Default::default(),
metadata: Default::default(),
},
Expand All @@ -162,6 +179,11 @@ impl AccessToken {
self
}

pub fn with_sip_grants(mut self, grants: SIPGrants) -> Self {
self.claims.sip = grants;
self
}

pub fn with_identity(mut self, identity: &str) -> Self {
self.claims.sub = identity.to_owned();
self
Expand Down
27 changes: 18 additions & 9 deletions livekit-api/src/services/egress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ impl EgressClient {
image_outputs,
output: None, // Deprecated
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -152,7 +153,8 @@ impl EgressClient {
output: None, // Deprecated
await_start_signal: options.await_start_signal,
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -182,7 +184,8 @@ impl EgressClient {
segment_outputs,
image_outputs,
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -212,7 +215,8 @@ impl EgressClient {
image_outputs,
output: None, // Deprecated
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand Down Expand Up @@ -240,7 +244,8 @@ impl EgressClient {
},
track_id: track_id.to_string(),
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand All @@ -259,7 +264,8 @@ impl EgressClient {
egress_id: egress_id.to_owned(),
layout: layout.to_owned(),
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand All @@ -280,7 +286,8 @@ impl EgressClient {
add_output_urls,
remove_output_urls,
},
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand All @@ -305,7 +312,8 @@ impl EgressClient {
SVC,
"ListEgress",
proto::ListEgressRequest { room_name, egress_id, active: options.active },
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await?;

Expand All @@ -318,7 +326,8 @@ impl EgressClient {
SVC,
"StopEgress",
proto::StopEgressRequest { egress_id: egress_id.to_owned() },
self.base.auth_header(VideoGrants { room_record: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { room_record: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand Down
16 changes: 12 additions & 4 deletions livekit-api/src/services/ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct CreateIngressOptions {
pub audio: proto::IngressAudioOptions,
pub video: proto::IngressVideoOptions,
pub bypass_transcoding: bool,
pub enable_transcoding: Option<bool>,
pub url: String,
}

Expand All @@ -40,6 +41,7 @@ pub struct UpdateIngressOptions {
pub audio: proto::IngressAudioOptions,
pub video: proto::IngressVideoOptions,
pub bypass_transcoding: Option<bool>,
pub enable_transcoding: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -89,9 +91,11 @@ impl IngressClient {
audio: Some(options.audio),
video: Some(options.video),
bypass_transcoding: options.bypass_transcoding,
enable_transcoding: options.enable_transcoding,
url: options.url,
},
self.base.auth_header(VideoGrants { ingress_admin: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { ingress_admin: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand All @@ -116,8 +120,10 @@ impl IngressClient {
audio: Some(options.audio),
video: Some(options.video),
bypass_transcoding: options.bypass_transcoding,
enable_transcoding: options.enable_transcoding,
},
self.base.auth_header(VideoGrants { ingress_admin: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { ingress_admin: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand All @@ -142,7 +148,8 @@ impl IngressClient {
_ => Default::default(),
},
},
self.base.auth_header(VideoGrants { ingress_admin: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { ingress_admin: true, ..Default::default() }, None)?,
)
.await?;

Expand All @@ -155,7 +162,8 @@ impl IngressClient {
SVC,
"DeleteIngress",
proto::DeleteIngressRequest { ingress_id: ingress_id.to_owned() },
self.base.auth_header(VideoGrants { ingress_admin: true, ..Default::default() })?,
self.base
.auth_header(VideoGrants { ingress_admin: true, ..Default::default() }, None)?,
)
.await
.map_err(Into::into)
Expand Down
17 changes: 12 additions & 5 deletions livekit-api/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::fmt::Debug;
use http::header::{HeaderMap, HeaderValue, AUTHORIZATION};
use thiserror::Error;

use crate::access_token::{AccessToken, AccessTokenError, VideoGrants};
use crate::access_token::{AccessToken, AccessTokenError, SIPGrants, VideoGrants};

pub mod egress;
pub mod ingress;
Expand Down Expand Up @@ -56,10 +56,17 @@ impl ServiceBase {
Self { api_key: api_key.to_owned(), api_secret: api_secret.to_owned() }
}

pub fn auth_header(&self, grants: VideoGrants) -> Result<HeaderMap, AccessTokenError> {
let token = AccessToken::with_api_key(&self.api_key, &self.api_secret)
.with_grants(grants)
.to_jwt()?;
pub fn auth_header(
&self,
grants: VideoGrants,
sip: Option<SIPGrants>,
) -> Result<HeaderMap, AccessTokenError> {
let mut tok =
AccessToken::with_api_key(&self.api_key, &self.api_secret).with_grants(grants);
if sip.is_some() {
tok = tok.with_sip_grants(sip.unwrap())
}
let token = tok.to_jwt()?;

let mut headers = HeaderMap::new();
headers.insert(AUTHORIZATION, HeaderValue::from_str(&format!("Bearer {}", token)).unwrap());
Expand Down
Loading

0 comments on commit 1e47c5c

Please sign in to comment.