-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathpublisher.rs
More file actions
39 lines (30 loc) · 1.04 KB
/
publisher.rs
File metadata and controls
39 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use anyhow::Result;
use livekit::prelude::*;
use std::{env, time::Duration};
use tokio::{signal, time};
#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let url = env::var("LIVEKIT_URL").expect("LIVEKIT_URL is not set");
let token = env::var("LIVEKIT_TOKEN").expect("LIVEKIT_TOKEN is not set");
let (room, _) = Room::connect(&url, &token, RoomOptions::default()).await?;
let track = room.local_participant().publish_data_track("my_sensor_data").await?;
tokio::select! {
_ = push_frames(track) => {}
_ = signal::ctrl_c() => {}
}
Ok(())
}
async fn read_sensor() -> Vec<u8> {
// Dynamically read some sensor data...
vec![0xFA; 256]
}
async fn push_frames(track: LocalDataTrack) {
loop {
log::info!("Pushing frame");
let frame = DataTrackFrame::new(read_sensor().await)
.with_user_timestamp_now();
track.try_push(frame).inspect_err(|err| println!("Failed to push frame: {}", err)).ok();
time::sleep(Duration::from_millis(500)).await
}
}