Skip to content

Commit

Permalink
fmt + api
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Nov 13, 2023
1 parent 76a0e92 commit 98ff77b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 56 deletions.
117 changes: 61 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,37 @@ use livekit_api::access_token;
use std::env;

fn create_token() -> Result<String, access_token::AccessTokenError> {
let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");

let token = access_token::AccessToken::with_api_key(&api_key, &api_secret)
.with_identity("rust-bot")
.with_name("Rust Bot")
.with_grants(access_token::VideoGrants {
room_join: true,
room: "my-room".to_string(),
..Default::default()
})
.to_jwt();
return token
let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");

let token = access_token::AccessToken::with_api_key(&api_key, &api_secret)
.with_identity("rust-bot")
.with_name("Rust Bot")
.with_grants(access_token::VideoGrants {
room_join: true,
room: "my-room".to_string(),
..Default::default()
})
.to_jwt();
return token
}
```

### Creating a room with RoomService API

```rust
use livekit_api::services::room;
use livekit_api::services::room::{CreateRoomOptions, RoomClient};

async fn create_room() -> Result<()> {
// by default, it'll load API Key and Secret from env vars
// LIVEKIT_API_KEY and LIVEKIT_API_SECRET
let client = room::RoomClient::new("https://my.livekit.server");
// TODO: fill out
#[tokio::main]
async fn main() {
let room_service = RoomClient::new("http://localhost:7880").unwrap();

let room = room_service
.create_room("my_room", CreateRoomOptions::default())
.await
.unwrap();

println!("Created room: {:?}", room);
}
```

Expand All @@ -90,18 +95,18 @@ use livekit::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
let (room, mut room_events) = Room::connect(&url, &token).await?;

while let Some(event) = room_events.recv().await {
match event {
RoomEvent::TrackSubscribed { track, publication, participant } => {
// ...
}
_ => {}
}
}

Ok(())
let (room, mut room_events) = Room::connect(&url, &token).await?;

while let Some(event) = room_events.recv().await {
match event {
RoomEvent::TrackSubscribed { track, publication, participant } => {
// ...
}
_ => {}
}
}

Ok(())
}
```

Expand All @@ -113,31 +118,31 @@ use futures::StreamExt; // this trait is required for iterating on audio & video
use livekit::prelude::*;

match event {
RoomEvent::TrackSubscribed { track, publication, participant } => {
match track {
RemoteTrack::Audio(audio_track) => {
let rtc_track = audio_track.rtc_track();
let audio_stream = NativeAudioStream::new(rtc_track);
tokio::spawn(async move {
// Receive the audio frames in a new task
while let Some(audio_frame) = audio_stream.next().await {
log::info!("received audio frame - {audio_frame:#?}");
}
});
},
RemoteTrack::Video(video_track) => {
let rtc_track = video_track.rtc_track();
let video_stream = NativeVideoStream::new(rtc_track);
tokio::spawn(async move {
// Receive the video frames in a new task
while let Some(video_frame) = video_stream.next().await {
log::info!("received video frame - {video_frame:#?}");
}
});
},
}
},
_ => {}
RoomEvent::TrackSubscribed { track, publication, participant } => {
match track {
RemoteTrack::Audio(audio_track) => {
let rtc_track = audio_track.rtc_track();
let audio_stream = NativeAudioStream::new(rtc_track);
tokio::spawn(async move {
// Receive the audio frames in a new task
while let Some(audio_frame) = audio_stream.next().await {
log::info!("received audio frame - {audio_frame:#?}");
}
});
},
RemoteTrack::Video(video_track) => {
let rtc_track = video_track.rtc_track();
let video_stream = NativeVideoStream::new(rtc_track);
tokio::spawn(async move {
// Receive the video frames in a new task
while let Some(video_frame) = video_stream.next().await {
log::info!("received video frame - {video_frame:#?}");
}
});
},
}
},
_ => {}
}
```

Expand Down
9 changes: 9 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ members = [
"save_to_disk",
"wgpu_room",
"webhooks",
"api",
]
10 changes: 10 additions & 0 deletions examples/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "api"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1", features = ["full", "parking_lot"] }
livekit-api = { path = "../../livekit-api", version = "0.2.0", features = ["native-tls"] }
futures = "0.3"

13 changes: 13 additions & 0 deletions examples/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use livekit_api::services::room::{CreateRoomOptions, RoomClient};

#[tokio::main]
async fn main() {
let room_service = RoomClient::new("http://localhost:7880").unwrap();

let room = room_service
.create_room("my_room", CreateRoomOptions::default())
.await
.unwrap();

println!("Created room: {:?}", room);
}

0 comments on commit 98ff77b

Please sign in to comment.