Skip to content

Commit 18c058c

Browse files
committed
move starting of GLib event loop into libwebrtc crate
Downstream applications should not have to be concerned with this implementation detail. If they want to disable the automatic start of the GLib main loop, they can opt out by disabling the glib-main-loop feature.
1 parent 80eb255 commit 18c058c

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

examples/screensharing/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,3 @@ livekit-api = { path = "../../livekit-api"}
1111
log = "0.4"
1212
clap = { version = "4.0", features = ["derive"] }
1313
inquire = "0.9"
14-
15-
[target.'cfg(target_os = "linux")'.dependencies]
16-
glib = "0.21.1"

examples/screensharing/src/main.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ async fn main() {
4242
.init();
4343
let args = Args::parse();
4444

45-
// On Wayland, libwebrtc communicates with the XDG Desktop Portal over dbus using GDBus
46-
// from the gio library. This requires a running glib event loop to work.
47-
// If your application already has a glib event loop running for another reason, for
48-
// example using the GTK or GStreamer Rust bindings, you do not need to setup another
49-
// glib event loop for this.
50-
if cfg!(target_os = "linux") && std::env::var("WAYLAND_DISPLAY").is_ok() {
51-
let main_loop = glib::MainLoop::new(None, false);
52-
let _handle = std::thread::spawn(move || {
53-
main_loop.run();
54-
});
55-
}
56-
5745
let url = env::var("LIVEKIT_URL").expect("LIVEKIT_URL is not set");
5846
let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
5947
let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");

libwebrtc/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ license = "Apache-2.0"
77
description = "Livekit safe bindings to libwebrtc"
88
repository = "https://github.com/livekit/rust-sdks"
99

10+
[features]
11+
default = [ "glib-main-loop" ]
12+
# On Wayland, libwebrtc communicates with the XDG Desktop Portal over dbus using GDBus
13+
# from the gio library. This requires a running glib event loop to work.
14+
# If your application already has a glib event loop running for another reason, for
15+
# example using the GTK or GStreamer Rust bindings, it does not need to setup another
16+
# glib event loop for this and can opt out by disabling this default glib-main-loop feature.
17+
glib-main-loop = [ "dep:glib" ]
18+
1019
[dependencies]
1120
livekit-protocol = { workspace = true }
1221
log = "0.4"
1322
serde = { version = "1.0", features = ["derive"] }
1423
serde_json = "1.0"
1524
thiserror = "1.0"
1625

26+
[target.'cfg(target_os = "linux")'.dependencies]
27+
glib = { version = "0.21.3", optional = true }
28+
1729
[target.'cfg(target_os = "android")'.dependencies]
1830
jni = "0.21"
1931

libwebrtc/src/native/desktop_capturer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ impl DesktopCapturer {
145145
}
146146

147147
pub fn start(&mut self) {
148+
#[cfg(all(target_os = "linux", feature = "glib-main-loop"))]
149+
if std::env::var("WAYLAND_DISPLAY").is_ok() {
150+
let main_loop = glib::MainLoop::new(None, false);
151+
let _handle = std::thread::spawn(move || {
152+
main_loop.run();
153+
});
154+
}
148155
let pin_handle = self.sys_handle.pin_mut();
149156
pin_handle.start();
150157
}

0 commit comments

Comments
 (0)