Skip to content

Commit 45d417e

Browse files
fix: load libudev and libdbus at runtime instead of linking them (#271)
* fix: load libudev at runtime instead of linking it The Linux natives linked libudev, so they failed to load on any host without it, although only the V4L2 device manager uses it. The manager now opens libudev.so.1 on construction and binds the symbols it needs, the same way the PulseAudio device manager handles libpulse. A host without libudev gets an exception when it enumerates cameras instead of a load failure. * fix: load libdbus at runtime instead of linking it Only the Linux power management uses libdbus, to ask the screen saver not to start during desktop capture. It now opens libdbus-1.so.3 on first use and binds the symbols it needs. A host without libdbus logs a warning and skips the inhibition instead of failing to load the natives. The dbus headers are still required at build time. * ci: run the native tests without libudev and libdbus The test runners no longer install the two libraries, so the lanes verify that the natives load without them.
1 parent 6f9e71f commit 45d417e

7 files changed

Lines changed: 362 additions & 60 deletions

File tree

‎.github/actions/test-natives/action.yml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ runs:
3939
name: natives-${{ inputs.platform-name }}
4040
path: natives
4141

42-
# PulseAudio is deliberately absent on the test runners: the natives open
43-
# it at runtime, and the tests prove that they load without it.
42+
# PulseAudio, libudev and libdbus are deliberately not installed here: the
43+
# natives open them at runtime, and the armhf lane, whose image has none of
44+
# them, proves that they load without them.
4445
- name: Install 32-bit runtime libraries
4546
if: runner.os == 'Linux' && inputs.java-architecture == 'armv7'
4647
run: |
4748
sudo dpkg --add-architecture armhf
4849
sudo apt-get update
49-
sudo apt-get install -y libc6:armhf libstdc++6:armhf zlib1g:armhf libx11-6:armhf libxext6:armhf libxfixes3:armhf libxdamage1:armhf libxtst6:armhf libxrandr2:armhf libxcomposite1:armhf libglib2.0-0:armhf libgbm1:armhf libdrm2:armhf libdbus-1-3:armhf libudev1:armhf
50+
sudo apt-get install -y libc6:armhf libstdc++6:armhf zlib1g:armhf libx11-6:armhf libxext6:armhf libxfixes3:armhf libxdamage1:armhf libxtst6:armhf libxrandr2:armhf libxcomposite1:armhf libglib2.0-0:armhf libgbm1:armhf libdrm2:armhf
5051
shell: bash
5152

5253
- name: Install runtime libraries
5354
if: runner.os == 'Linux' && inputs.java-architecture != 'armv7'
5455
run: |
5556
sudo apt-get update
56-
sudo apt-get install -y libx11-6 libxext6 libxfixes3 libxdamage1 libxtst6 libxrandr2 libxcomposite1 libglib2.0-0 libgbm1 libdrm2 libdbus-1-3 libudev1
57+
sudo apt-get install -y libx11-6 libxext6 libxfixes3 libxdamage1 libxtst6 libxrandr2 libxcomposite1 libglib2.0-0 libgbm1 libdrm2
5758
shell: bash
5859

5960
- name: Set up JDK ${{ inputs.java-version }}

‎webrtc-jni/src/main/cpp/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ if(APPLE)
113113
elseif(LINUX)
114114
set(CXX_LIBS "-static-libgcc -stdlib=libc++ -lc++ -lc++abi")
115115

116-
target_link_libraries(${PROJECT_NAME} ${CXX_LIBS} udev)
116+
target_link_libraries(${PROJECT_NAME} ${CXX_LIBS})
117117
target_link_libraries(${PROJECT_NAME} dl)
118118
elseif(WIN32)
119119
target_link_libraries(${PROJECT_NAME} dwmapi.lib mf.lib mfreadwrite.lib mfplat.lib mfuuid.lib shcore.lib)

‎webrtc-jni/src/main/cpp/dependencies/webrtc/CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ if(APPLE)
206206
target_compile_definitions(${PROJECT_NAME} PUBLIC WEBRTC_MAC WEBRTC_POSIX WEBRTC_USE_H264)
207207
target_link_libraries(${PROJECT_NAME} "-framework Foundation" "-framework AVFoundation" "-framework CoreGraphics" "-framework CoreAudio" "-framework CoreVideo" "-framework ScreenCaptureKit" "-framework AudioToolbox" "-framework IOSurface" "-framework ApplicationServices" "-framework AppKit")
208208
elseif(LINUX)
209-
# Find DBus
209+
# Find DBus (headers only: the JNI code opens libdbus at runtime)
210210
find_package(PkgConfig QUIET REQUIRED) # Include functions provided by PkgConfig module.
211211
pkg_check_modules(DBUS REQUIRED dbus-1)
212212
# Wayland/PipeWire desktop capture: libwebrtc.a references gio, gbm and libdrm
@@ -226,7 +226,7 @@ elseif(LINUX)
226226
# libpipewire dev package.)
227227
target_include_directories(${PROJECT_NAME} PUBLIC ${DBUS_INCLUDE_DIRS} ${GIO_INCLUDE_DIRS})
228228
target_compile_definitions(${PROJECT_NAME} PUBLIC WEBRTC_LINUX WEBRTC_POSIX WEBRTC_USE_H264 WEBRTC_USE_X11 WEBRTC_USE_PIPEWIRE WEBRTC_USE_GIO)
229-
target_link_libraries(${PROJECT_NAME} X11 Xext Xfixes Xdamage Xtst Xrandr Xcomposite dbus-1 ${GIO_LIBRARIES} ${GBM_LIBRARIES} ${DRM_LIBRARIES})
229+
target_link_libraries(${PROJECT_NAME} X11 Xext Xfixes Xdamage Xtst Xrandr Xcomposite ${GIO_LIBRARIES} ${GBM_LIBRARIES} ${DRM_LIBRARIES})
230230
elseif(WIN32)
231231
# RTC_ENABLE_WIN_WGC must match the gn build: webrtc.lib is compiled with gn's
232232
# default rtc_enable_win_wgc=true (webrtc.gni: "rtc_enable_win_wgc = is_win"),
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2019 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
#ifndef JNI_WEBRTC_MEDIA_DESKTOP_LINUX_DBUS_LOADER_H_
19+
#define JNI_WEBRTC_MEDIA_DESKTOP_LINUX_DBUS_LOADER_H_
20+
21+
#include <dbus/dbus.h>
22+
23+
#include <dlfcn.h>
24+
25+
namespace jni
26+
{
27+
namespace avdev
28+
{
29+
typedef void (*dbus_error_init_t)(DBusError * error);
30+
typedef void (*dbus_error_free_t)(DBusError * error);
31+
typedef dbus_bool_t (*dbus_error_is_set_t)(const DBusError * error);
32+
typedef DBusConnection * (*dbus_bus_get_t)(DBusBusType type, DBusError * error);
33+
typedef dbus_bool_t (*dbus_bus_name_has_owner_t)(DBusConnection * connection, const char * name, DBusError * error);
34+
typedef void (*dbus_connection_unref_t)(DBusConnection * connection);
35+
typedef DBusMessage * (*dbus_connection_send_with_reply_and_block_t)(DBusConnection * connection, DBusMessage * message, int timeout, DBusError * error);
36+
typedef DBusMessage * (*dbus_message_new_method_call_t)(const char * destination, const char * path, const char * iface, const char * method);
37+
typedef dbus_bool_t (*dbus_message_append_args_t)(DBusMessage * message, int firstArgType, ...);
38+
typedef void (*dbus_message_unref_t)(DBusMessage * message);
39+
typedef dbus_bool_t (*dbus_message_iter_init_t)(DBusMessage * message, DBusMessageIter * iter);
40+
typedef void (*dbus_message_iter_get_basic_t)(DBusMessageIter * iter, void * value);
41+
42+
class DBusLoader
43+
{
44+
public:
45+
static DBusLoader & instance()
46+
{
47+
static DBusLoader instance;
48+
return instance;
49+
}
50+
51+
bool load()
52+
{
53+
if (loaded) {
54+
return true;
55+
}
56+
57+
handle = dlopen("libdbus-1.so.3", RTLD_NOW);
58+
59+
if (!handle) {
60+
return false;
61+
}
62+
63+
#define DBUS_LOAD_SYM(name) \
64+
name = (name##_t) dlsym(handle, #name); \
65+
if (!name) { close(); return false; }
66+
67+
DBUS_LOAD_SYM(dbus_error_init);
68+
DBUS_LOAD_SYM(dbus_error_free);
69+
DBUS_LOAD_SYM(dbus_error_is_set);
70+
DBUS_LOAD_SYM(dbus_bus_get);
71+
DBUS_LOAD_SYM(dbus_bus_name_has_owner);
72+
DBUS_LOAD_SYM(dbus_connection_unref);
73+
DBUS_LOAD_SYM(dbus_connection_send_with_reply_and_block);
74+
DBUS_LOAD_SYM(dbus_message_new_method_call);
75+
DBUS_LOAD_SYM(dbus_message_append_args);
76+
DBUS_LOAD_SYM(dbus_message_unref);
77+
DBUS_LOAD_SYM(dbus_message_iter_init);
78+
DBUS_LOAD_SYM(dbus_message_iter_get_basic);
79+
80+
#undef DBUS_LOAD_SYM
81+
82+
loaded = true;
83+
84+
return true;
85+
}
86+
87+
void close()
88+
{
89+
if (handle) {
90+
dlclose(handle);
91+
handle = nullptr;
92+
}
93+
94+
loaded = false;
95+
}
96+
97+
bool isLoaded() const
98+
{
99+
return loaded;
100+
}
101+
102+
dbus_error_init_t dbus_error_init;
103+
dbus_error_free_t dbus_error_free;
104+
dbus_error_is_set_t dbus_error_is_set;
105+
dbus_bus_get_t dbus_bus_get;
106+
dbus_bus_name_has_owner_t dbus_bus_name_has_owner;
107+
dbus_connection_unref_t dbus_connection_unref;
108+
dbus_connection_send_with_reply_and_block_t dbus_connection_send_with_reply_and_block;
109+
dbus_message_new_method_call_t dbus_message_new_method_call;
110+
dbus_message_append_args_t dbus_message_append_args;
111+
dbus_message_unref_t dbus_message_unref;
112+
dbus_message_iter_init_t dbus_message_iter_init;
113+
dbus_message_iter_get_basic_t dbus_message_iter_get_basic;
114+
115+
private:
116+
DBusLoader() : loaded(false), handle(nullptr) {}
117+
118+
bool loaded;
119+
void * handle;
120+
};
121+
}
122+
}
123+
124+
#endif
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2019 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
#ifndef JNI_WEBRTC_MEDIA_V4L2_UDEV_LOADER_H_
19+
#define JNI_WEBRTC_MEDIA_V4L2_UDEV_LOADER_H_
20+
21+
#include <libudev.h>
22+
23+
#include <dlfcn.h>
24+
25+
namespace jni
26+
{
27+
namespace avdev
28+
{
29+
typedef struct udev * (*udev_new_t)();
30+
typedef struct udev * (*udev_unref_t)(struct udev * udev);
31+
typedef struct udev_enumerate * (*udev_enumerate_new_t)(struct udev * udev);
32+
typedef struct udev_enumerate * (*udev_enumerate_unref_t)(struct udev_enumerate * enumerate);
33+
typedef int (*udev_enumerate_add_match_subsystem_t)(struct udev_enumerate * enumerate, const char * subsystem);
34+
typedef int (*udev_enumerate_scan_devices_t)(struct udev_enumerate * enumerate);
35+
typedef struct udev_list_entry * (*udev_enumerate_get_list_entry_t)(struct udev_enumerate * enumerate);
36+
typedef struct udev_list_entry * (*udev_list_entry_get_next_t)(struct udev_list_entry * entry);
37+
typedef const char * (*udev_list_entry_get_name_t)(struct udev_list_entry * entry);
38+
typedef struct udev_device * (*udev_device_new_from_syspath_t)(struct udev * udev, const char * syspath);
39+
typedef struct udev_device * (*udev_device_unref_t)(struct udev_device * device);
40+
typedef const char * (*udev_device_get_devnode_t)(struct udev_device * device);
41+
typedef const char * (*udev_device_get_subsystem_t)(struct udev_device * device);
42+
typedef const char * (*udev_device_get_action_t)(struct udev_device * device);
43+
typedef const char * (*udev_device_get_property_value_t)(struct udev_device * device, const char * key);
44+
typedef struct udev_monitor * (*udev_monitor_new_from_netlink_t)(struct udev * udev, const char * name);
45+
typedef struct udev_monitor * (*udev_monitor_unref_t)(struct udev_monitor * monitor);
46+
typedef int (*udev_monitor_filter_add_match_subsystem_devtype_t)(struct udev_monitor * monitor, const char * subsystem, const char * devtype);
47+
typedef int (*udev_monitor_enable_receiving_t)(struct udev_monitor * monitor);
48+
typedef int (*udev_monitor_get_fd_t)(struct udev_monitor * monitor);
49+
typedef struct udev_device * (*udev_monitor_receive_device_t)(struct udev_monitor * monitor);
50+
51+
class UdevLoader
52+
{
53+
public:
54+
static UdevLoader & instance()
55+
{
56+
static UdevLoader instance;
57+
return instance;
58+
}
59+
60+
bool load()
61+
{
62+
if (loaded) {
63+
return true;
64+
}
65+
66+
handle = dlopen("libudev.so.1", RTLD_NOW);
67+
68+
if (!handle) {
69+
return false;
70+
}
71+
72+
#define UDEV_LOAD_SYM(name) \
73+
name = (name##_t) dlsym(handle, #name); \
74+
if (!name) { close(); return false; }
75+
76+
UDEV_LOAD_SYM(udev_new);
77+
UDEV_LOAD_SYM(udev_unref);
78+
UDEV_LOAD_SYM(udev_enumerate_new);
79+
UDEV_LOAD_SYM(udev_enumerate_unref);
80+
UDEV_LOAD_SYM(udev_enumerate_add_match_subsystem);
81+
UDEV_LOAD_SYM(udev_enumerate_scan_devices);
82+
UDEV_LOAD_SYM(udev_enumerate_get_list_entry);
83+
UDEV_LOAD_SYM(udev_list_entry_get_next);
84+
UDEV_LOAD_SYM(udev_list_entry_get_name);
85+
UDEV_LOAD_SYM(udev_device_new_from_syspath);
86+
UDEV_LOAD_SYM(udev_device_unref);
87+
UDEV_LOAD_SYM(udev_device_get_devnode);
88+
UDEV_LOAD_SYM(udev_device_get_subsystem);
89+
UDEV_LOAD_SYM(udev_device_get_action);
90+
UDEV_LOAD_SYM(udev_device_get_property_value);
91+
UDEV_LOAD_SYM(udev_monitor_new_from_netlink);
92+
UDEV_LOAD_SYM(udev_monitor_unref);
93+
UDEV_LOAD_SYM(udev_monitor_filter_add_match_subsystem_devtype);
94+
UDEV_LOAD_SYM(udev_monitor_enable_receiving);
95+
UDEV_LOAD_SYM(udev_monitor_get_fd);
96+
UDEV_LOAD_SYM(udev_monitor_receive_device);
97+
98+
#undef UDEV_LOAD_SYM
99+
100+
loaded = true;
101+
102+
return true;
103+
}
104+
105+
void close()
106+
{
107+
if (handle) {
108+
dlclose(handle);
109+
handle = nullptr;
110+
}
111+
112+
loaded = false;
113+
}
114+
115+
bool isLoaded() const
116+
{
117+
return loaded;
118+
}
119+
120+
udev_new_t udev_new;
121+
udev_unref_t udev_unref;
122+
udev_enumerate_new_t udev_enumerate_new;
123+
udev_enumerate_unref_t udev_enumerate_unref;
124+
udev_enumerate_add_match_subsystem_t udev_enumerate_add_match_subsystem;
125+
udev_enumerate_scan_devices_t udev_enumerate_scan_devices;
126+
udev_enumerate_get_list_entry_t udev_enumerate_get_list_entry;
127+
udev_list_entry_get_next_t udev_list_entry_get_next;
128+
udev_list_entry_get_name_t udev_list_entry_get_name;
129+
udev_device_new_from_syspath_t udev_device_new_from_syspath;
130+
udev_device_unref_t udev_device_unref;
131+
udev_device_get_devnode_t udev_device_get_devnode;
132+
udev_device_get_subsystem_t udev_device_get_subsystem;
133+
udev_device_get_action_t udev_device_get_action;
134+
udev_device_get_property_value_t udev_device_get_property_value;
135+
udev_monitor_new_from_netlink_t udev_monitor_new_from_netlink;
136+
udev_monitor_unref_t udev_monitor_unref;
137+
udev_monitor_filter_add_match_subsystem_devtype_t udev_monitor_filter_add_match_subsystem_devtype;
138+
udev_monitor_enable_receiving_t udev_monitor_enable_receiving;
139+
udev_monitor_get_fd_t udev_monitor_get_fd;
140+
udev_monitor_receive_device_t udev_monitor_receive_device;
141+
142+
private:
143+
UdevLoader() : loaded(false), handle(nullptr) {}
144+
145+
bool loaded;
146+
void * handle;
147+
};
148+
}
149+
}
150+
151+
#endif

0 commit comments

Comments
 (0)