Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build of examples #493

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ match event {
match track {
RemoteTrack::Audio(audio_track) => {
let rtc_track = audio_track.rtc_track();
let mut audio_stream = NativeAudioStream::new(rtc_track);
let mut audio_stream = NativeAudioStream::new(rtc_track, 48000, 1);
tokio::spawn(async move {
// Receive the audio frames in a new task
while let Some(audio_frame) = audio_stream.next().await {
Expand Down
8 changes: 4 additions & 4 deletions examples/Cargo.lock

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

5 changes: 1 addition & 4 deletions examples/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use livekit_api::services::room::{CreateRoomOptions, RoomClient};
async fn main() {
let room_service = RoomClient::new("http://localhost:7880").unwrap();

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

println!("Created room: {:?}", room);
}
4 changes: 1 addition & 3 deletions examples/mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ pub mod android {
#[no_mangle]
pub extern "C" fn JNI_OnLoad(vm: JavaVM, _: *mut c_void) -> jint {
android_logger::init_once(
Config::default()
.with_max_level(LevelFilter::Debug)
.with_tag("livekit-rustexample"),
Config::default().with_max_level(LevelFilter::Debug).with_tag("livekit-rustexample"),
);

log::info!("JNI_OnLoad, initializing LiveKit");
Expand Down
38 changes: 7 additions & 31 deletions examples/save_to_disk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use bytes::{BufMut, BytesMut};
use futures::StreamExt;
use livekit::prelude::*;
use livekit::webrtc::audio_stream::native::NativeAudioStream;
use livekit::webrtc::native::audio_resampler;
use std::env;
use tokio::fs::File;
use tokio::io::{AsyncWriteExt, BufWriter};
Expand Down Expand Up @@ -31,11 +30,7 @@ impl WavWriter {
let file = File::create(path).await?;
let writer = BufWriter::new(file);

let mut wav_writer = WavWriter {
header,
data: BytesMut::new(),
writer,
};
let mut wav_writer = WavWriter { header, data: BytesMut::new(), writer };

wav_writer.write_header()?;
Ok(wav_writer)
Expand Down Expand Up @@ -89,19 +84,13 @@ async fn main() {
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, mut rx) = Room::connect(&url, &token, RoomOptions::default())
.await
.unwrap();
let (room, mut rx) = Room::connect(&url, &token, RoomOptions::default()).await.unwrap();
println!("Connected to room: {} - {}", room.name(), String::from(room.sid().await));

while let Some(msg) = rx.recv().await {
#[allow(clippy::single_match)]
match msg {
RoomEvent::TrackSubscribed {
track,
publication: _,
participant: _,
} => {
RoomEvent::TrackSubscribed { track, publication: _, participant: _ } => {
if let RemoteTrack::Audio(audio_track) = track {
record_track(audio_track).await.unwrap();
break;
Expand All @@ -118,29 +107,16 @@ async fn record_track(audio_track: RemoteAudioTrack) -> Result<(), std::io::Erro
println!("Recording track {:?}", audio_track.sid());
let rtc_track = audio_track.rtc_track();

let header = WavHeader {
sample_rate: 48000,
bit_depth: 16,
num_channels: 2,
};
let header = WavHeader { sample_rate: 48000, bit_depth: 16, num_channels: 2 };

let mut resampler = audio_resampler::AudioResampler::default();
let mut wav_writer = WavWriter::create(FILE_PATH, header).await?;
let mut audio_stream = NativeAudioStream::new(rtc_track);
let mut audio_stream =
NativeAudioStream::new(rtc_track, header.sample_rate as i32, header.num_channels as i32);

let max_record = 5 * header.sample_rate * header.num_channels;
let mut sample_count = 0;
'recv_loop: while let Some(frame) = audio_stream.next().await {
let data = resampler.remix_and_resample(
&frame.data,
frame.samples_per_channel,
frame.num_channels,
frame.sample_rate,
header.num_channels,
header.sample_rate,
);

for sample in data {
for sample in frame.data.into_iter() {
wav_writer.write_sample(*sample).await.unwrap();
sample_count += 1;

Expand Down
158 changes: 64 additions & 94 deletions examples/wgpu_room/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ impl LkApp {
.and_then(|storage| eframe::get_value(storage, eframe::APP_KEY))
.unwrap_or_default();

let async_runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
let async_runtime =
tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();

Self {
service: LkService::new(async_runtime.handle()),
Expand All @@ -74,11 +72,7 @@ impl LkApp {
UiCmd::RoomEvent { event } => {
log::info!("{:?}", event);
match event {
RoomEvent::TrackSubscribed {
track,
publication: _,
participant,
} => {
RoomEvent::TrackSubscribed { track, publication: _, participant } => {
if let RemoteTrack::Video(ref video_track) = track {
// Create a new VideoRenderer
let video_renderer = VideoRenderer::new(
Expand All @@ -92,19 +86,10 @@ impl LkApp {
// TODO(theomonnom): Once we support media devices, we can play audio tracks here
}
}
RoomEvent::TrackUnsubscribed {
track,
publication: _,
participant,
} => {
self.video_renderers
.remove(&(participant.identity(), track.sid()));
RoomEvent::TrackUnsubscribed { track, publication: _, participant } => {
self.video_renderers.remove(&(participant.identity(), track.sid()));
}
RoomEvent::LocalTrackPublished {
track,
publication: _,
participant,
} => {
RoomEvent::LocalTrackPublished { track, publication: _, participant } => {
if let LocalTrack::Video(ref video_track) = track {
// Also create a new VideoRenderer for local tracks
let video_renderer = VideoRenderer::new(
Expand All @@ -116,12 +101,8 @@ impl LkApp {
.insert((participant.identity(), track.sid()), video_renderer);
}
}
RoomEvent::LocalTrackUnpublished {
publication,
participant,
} => {
self.video_renderers
.remove(&(participant.identity(), publication.sid()));
RoomEvent::LocalTrackUnpublished { publication, participant } => {
self.video_renderers.remove(&(participant.identity(), publication.sid()));
}
RoomEvent::Disconnected { reason: _ } => {
self.video_renderers.clear();
Expand Down Expand Up @@ -240,10 +221,7 @@ impl LkApp {
ui.label(format!("Name: {}", room.name()));
//ui.label(format!("Sid: {}", String::from(room.sid().await)));
ui.label(format!("ConnectionState: {:?}", room.connection_state()));
ui.label(format!(
"ParticipantCount: {:?}",
room.remote_participants().len() + 1
));
ui.label(format!("ParticipantCount: {:?}", room.remote_participants().len() + 1));
}

egui::warn_if_debug_build(ui);
Expand All @@ -262,10 +240,8 @@ impl LkApp {
egui::ScrollArea::vertical().show(ui, |ui| {
// Iterate with sorted keys to avoid flickers (Because this is a immediate mode UI)
let participants = room.remote_participants();
let mut sorted_participants = participants
.keys()
.cloned()
.collect::<Vec<ParticipantIdentity>>();
let mut sorted_participants =
participants.keys().cloned().collect::<Vec<ParticipantIdentity>>();
sorted_participants.sort_by(|a, b| a.as_str().cmp(b.as_str()));

for psid in sorted_participants {
Expand All @@ -288,11 +264,7 @@ impl LkApp {
}
});

ui.label(format!(
"{} - {:?}",
publication.name(),
publication.source()
));
ui.label(format!("{} - {:?}", publication.name(), publication.source()));

ui.horizontal(|ui| {
if publication.is_muted() {
Expand All @@ -307,9 +279,8 @@ impl LkApp {

if publication.is_subscribed() {
if ui.button("Unsubscribe").clicked() {
let _ = self
.service
.send(AsyncCmd::UnsubscribeTrack { publication });
let _ =
self.service.send(AsyncCmd::UnsubscribeTrack { publication });
}
} else if ui.button("Subscribe").clicked() {
let _ = self.service.send(AsyncCmd::SubscribeTrack { publication });
Expand All @@ -334,46 +305,46 @@ impl LkApp {
}

egui::ScrollArea::vertical().show(ui, |ui| {
VideoGrid::new("default_grid")
.max_columns(6)
.show(ui, |ui| {
if show_videos {
// Draw participant videos
for ((participant_sid, _), video_renderer) in &self.video_renderers {
ui.video_frame(|ui| {
let room = room.as_ref().unwrap().clone();

if let Some(participant) = room.remote_participants().get(participant_sid)
{
draw_video(
participant.name().as_str(),
participant.is_speaking(),
video_renderer,
ui,
);
} else {
draw_video(
room.local_participant().name().as_str(),
room.local_participant().is_speaking(),
video_renderer,
ui,
);
}
});
}
} else {
// Draw video skeletons when we're not connected
for _ in 0..5 {
ui.video_frame(|ui| {
egui::Frame::none()
.fill(ui.style().visuals.code_bg_color)
.show(ui, |ui| {
ui.allocate_space(ui.available_size());
});
});
}
VideoGrid::new("default_grid").max_columns(6).show(ui, |ui| {
if show_videos {
// Draw participant videos
for ((participant_sid, _), video_renderer) in &self.video_renderers {
ui.video_frame(|ui| {
let room = room.as_ref().unwrap().clone();

if let Some(participant) =
room.remote_participants().get(participant_sid)
{
draw_video(
participant.name().as_str(),
participant.is_speaking(),
video_renderer,
ui,
);
} else {
draw_video(
room.local_participant().name().as_str(),
room.local_participant().is_speaking(),
video_renderer,
ui,
);
}
});
}
} else {
// Draw video skeletons when we're not connected
for _ in 0..5 {
ui.video_frame(|ui| {
egui::Frame::none().fill(ui.style().visuals.code_bg_color).show(
ui,
|ui| {
ui.allocate_space(ui.available_size());
},
);
});
}
})
}
})
});
}
}
Expand All @@ -392,12 +363,12 @@ impl eframe::App for LkApp {
self.top_panel(ui);
});

egui::SidePanel::left("left_panel")
.resizable(true)
.width_range(20.0..=360.0)
.show(ctx, |ui| {
egui::SidePanel::left("left_panel").resizable(true).width_range(20.0..=360.0).show(
ctx,
|ui| {
self.left_panel(ui);
});
},
);

/*egui::TopBottomPanel::bottom("bottom_panel")
.resizable(true)
Expand All @@ -406,12 +377,12 @@ impl eframe::App for LkApp {
self.bottom_panel(ui);
});*/

egui::SidePanel::right("right_panel")
.resizable(true)
.width_range(20.0..=360.0)
.show(ctx, |ui| {
egui::SidePanel::right("right_panel").resizable(true).width_range(20.0..=360.0).show(
ctx,
|ui| {
self.right_panel(ui);
});
},
);

egui::CentralPanel::default().show(ctx, |ui| {
self.central_panel(ui);
Expand All @@ -427,8 +398,7 @@ fn draw_video(name: &str, speaking: bool, video_renderer: &VideoRenderer, ui: &m
let inner_rect = rect.shrink(1.0);

if speaking {
ui.painter()
.rect(rect, Rounding::none(), egui::Color32::GREEN, Stroke::NONE);
ui.painter().rect(rect, Rounding::none(), egui::Color32::GREEN, Stroke::NONE);
}

// Always draw a background in case we still didn't receive a frame
Expand Down
Loading
Loading