Skip to content
Merged
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.

3 changes: 0 additions & 3 deletions examples/screensharing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ livekit = { workspace = true, features = ["rustls-tls-native-roots"] }
livekit-api = { workspace = true }
log = "0.4"
clap = { version = "4.0", features = ["derive"] }

[target.'cfg(target_os = "linux")'.dependencies]
glib = "0.21.1"
10 changes: 0 additions & 10 deletions examples/screensharing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ mod test {
env_logger::init();
let args = Args::parse();

#[cfg(target_os = "linux")]
{
/* This is needed for getting the system picker for screen sharing. */
use glib::MainLoop;
let main_loop = MainLoop::new(None, false);
let _handle = std::thread::spawn(move || {
main_loop.run();
});
}

let url = env::var("LIVEKIT_URL").expect("LIVEKIT_URL is not set");
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");
Expand Down
11 changes: 11 additions & 0 deletions libwebrtc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ license = "Apache-2.0"
description = "Livekit safe bindings to libwebrtc"
repository = "https://github.com/livekit/rust-sdks"

[features]
default = [ "glib-main-loop" ]
# On Wayland, libwebrtc uses GDBus to communicate with the XDG Desktop Portal.
# GDBus requires a GLib event loop to be running. If you already have a GLib
# event loop running in your application, for example if you are using the
# GTK or GStreamer Rust bindings, disable this feature.
glib-main-loop = [ "dep:glib" ]

[dependencies]
livekit-protocol = { workspace = true }
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"

[target.'cfg(any(target_os = "linux", target_os = "freebsd"))'.dependencies]
glib = { version = "0.21.3", optional = true }

[target.'cfg(target_os = "android")'.dependencies]
jni = "0.21"

Expand Down
28 changes: 27 additions & 1 deletion libwebrtc/src/native/desktop_capturer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ impl DesktopCapturerOptions {

pub(crate) struct DesktopCapturer {
sys_handle: UniquePtr<sys_dc::ffi::DesktopCapturer>,
#[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))]
glib_loop: Option<glib::MainLoop>,
}

impl DesktopCapturer {
Expand All @@ -86,7 +88,14 @@ impl DesktopCapturer {
if sys_handle.is_null() {
None
} else {
Some(Self { sys_handle })
Some(Self {
sys_handle,
#[cfg(all(
any(target_os = "linux", target_os = "freebsd"),
feature = "glib-main-loop"
))]
glib_loop: None,
})
}
}

Expand All @@ -98,6 +107,14 @@ impl DesktopCapturer {
where
T: FnMut(Result<DesktopFrame, CaptureError>) + Send + 'static,
{
#[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))]
if std::env::var("WAYLAND_DISPLAY").is_ok() {
let main_loop = glib::MainLoop::new(None, false);
self.glib_loop = Some(main_loop.clone());
let _handle = std::thread::spawn(move || {
main_loop.run();
});
}
let pin_handle = self.sys_handle.pin_mut();
let callback = DesktopCallback::new(callback);
let callback_wrapper = sys_dc::DesktopCapturerCallbackWrapper::new(Box::new(callback));
Expand All @@ -118,6 +135,15 @@ impl DesktopCapturer {
}
}

#[cfg(all(any(target_os = "linux", target_os = "freebsd"), feature = "glib-main-loop"))]
impl Drop for DesktopCapturer {
fn drop(&mut self) {
if let Some(glib_loop) = &self.glib_loop {
glib_loop.quit();
}
}
}

pub(crate) struct DesktopFrame {
sys_handle: UniquePtr<sys_dc::ffi::DesktopFrame>,
}
Expand Down