From c99481a7942bcc58c2fa52b5e90268a9c39ee596 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 5 Nov 2026 13:37:12 +0100 Subject: [PATCH] fix: release D-Bus references and bound the window frame crop Linux releases its D-Bus references on every exit, which matters now that each enable retries a failed inhibition. The window border crop applies to window sources only and keeps frames too small for it whole. --- .../video/desktop/DesktopCaptureCallback.h | 4 +- .../media/video/desktop/DesktopCapturer.h | 3 + .../media/video/desktop/WindowFrameCrop.h | 56 ++++++++++++++++++ .../src/main/cpp/src/JNI_DesktopCapturer.cpp | 2 +- .../media/video/VideoTrackDesktopSource.cpp | 18 ++---- .../video/desktop/DesktopCaptureCallback.cpp | 21 +++---- .../media/video/desktop/DesktopCapturer.cpp | 8 ++- .../desktop/linux/LinuxPowerManagement.cpp | 57 ++++++++++++------- .../video/desktop/PowerManagementTests.java | 13 +++++ 9 files changed, 130 insertions(+), 52 deletions(-) create mode 100644 webrtc-jni/src/main/cpp/include/media/video/desktop/WindowFrameCrop.h diff --git a/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCaptureCallback.h b/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCaptureCallback.h index e8171140c..158a60189 100644 --- a/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCaptureCallback.h +++ b/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCaptureCallback.h @@ -31,7 +31,7 @@ namespace jni class DesktopCaptureCallback : public webrtc::DesktopCapturer::Callback { public: - DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef & callback); + DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef & callback, bool windowSource); ~DesktopCaptureCallback() override = default; // DesktopCapturer::Callback implementation. @@ -49,6 +49,8 @@ namespace jni private: JavaGlobalRef callback; + const bool windowSource; + const std::shared_ptr javaClass; const std::shared_ptr javaFrameClass; diff --git a/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCapturer.h b/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCapturer.h index 0d95aeec3..d45cf9fe4 100644 --- a/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCapturer.h +++ b/webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCapturer.h @@ -56,6 +56,7 @@ namespace jni bool FocusOnSelectedSource(); void setFocusSelectedSource(bool focus); bool IsOccluded(const webrtc::DesktopVector & pos); + bool isWindowCapturer() const; protected: std::mutex startMutex; @@ -67,6 +68,8 @@ namespace jni bool focusSelectedSource; + const bool windowCapturer; + #if defined(WEBRTC_WIN) private: ComInitializer comInitializer; diff --git a/webrtc-jni/src/main/cpp/include/media/video/desktop/WindowFrameCrop.h b/webrtc-jni/src/main/cpp/include/media/video/desktop/WindowFrameCrop.h new file mode 100644 index 000000000..8a58814f8 --- /dev/null +++ b/webrtc-jni/src/main/cpp/include/media/video/desktop/WindowFrameCrop.h @@ -0,0 +1,56 @@ +/* + * Copyright 2019 Alex Andres + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JNI_WEBRTC_MEDIA_DESKTOP_WINDOW_FRAME_CROP_H_ +#define JNI_WEBRTC_MEDIA_DESKTOP_WINDOW_FRAME_CROP_H_ + +#include "modules/desktop_capture/desktop_frame.h" +#include "modules/desktop_capture/desktop_geometry.h" + +#include + +namespace jni +{ + // Crops the black border of a frame captured from a window on Windows, for + // window sources only. The window capturer's bitmaps have no row padding, so + // a stride wider than a row means it cut the frame out of a larger bitmap to + // drop a resize border. Windows without such a border come whole and keep + // all their pixels. The crop values stay as they are when there is nothing + // to crop, or when a frame too small for the border would be left empty. + inline void CropWindowBorder(const webrtc::DesktopFrame & frame, int border, int * crop_x, int * crop_y, int * crop_w, int * crop_h) + { + const int width = frame.size().width(); + const int height = frame.size().height(); + + if (frame.stride() == width * webrtc::DesktopFrame::kBytesPerPixel) { + return; + } + + const int top = std::max(0, -frame.top_left().y()); + const auto rect = webrtc::DesktopRect::MakeLTRB(border, top, width - border, height - border); + + if (rect.is_empty()) { + return; + } + + *crop_x = rect.left(); + *crop_y = rect.top(); + *crop_w = rect.width(); + *crop_h = rect.height(); + } +} + +#endif \ No newline at end of file diff --git a/webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp b/webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp index 4339efaaa..3660e1b0f 100644 --- a/webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp +++ b/webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp @@ -124,7 +124,7 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_DesktopCapture jni::NativeLease capturer(env, caller); CHECK_HANDLE(capturer); - auto callback = std::make_unique(env, jni::JavaGlobalRef(env, jcallback)); + auto callback = std::make_unique(env, jni::JavaGlobalRef(env, jcallback), capturer->isWindowCapturer()); if (!capturer->Start(std::move(callback))) { ThrowJavaException(env, "java/lang/IllegalStateException", "Capture has been started already"); diff --git a/webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp b/webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp index 996d8a517..deaad2183 100644 --- a/webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp +++ b/webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp @@ -15,6 +15,7 @@ */ #include "media/video/VideoTrackDesktopSource.h" +#include "media/video/desktop/WindowFrameCrop.h" #include "Exception.h" #include "api/video/i420_buffer.h" @@ -170,17 +171,8 @@ namespace jni } #if defined(WEBRTC_WIN) - // Crop black window borders. - bool fullscreen = frame->stride() == (frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel); - - if (!fullscreen) { - const webrtc::DesktopVector& top_left = frame->top_left(); - const int32_t border = GetSystemMetrics(SM_CXPADDEDBORDER); - - crop_x = border; - crop_y = top_left.y() < 0 ? -top_left.y() : 0; - crop_w = width - crop_x * 2; - crop_h = height - (crop_y + border); + if (sourceIsWindow) { + CropWindowBorder(*frame, GetSystemMetrics(SM_CXPADDEDBORDER), &crop_x, &crop_y, &crop_w, &crop_h); } #endif @@ -199,8 +191,8 @@ namespace jni // output. Passing buffer->height() (== crop_h) here made libyuv's internal // bounds check (crop_y + crop_height <= src_height) fail with -1 whenever // crop_y > 0 — i.e. for every maximized window, whose frame sits at - // top_left().y() == -border. Screen frames never hit this branch (exact stride, - // fullscreen == true), which is why only window capture appeared broken. + // top_left().y() == -border. Screen frames are not cropped, which is why only + // window capture appeared broken. frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, height, crop_w, crop_h, libyuv::kRotate0, libyuv::FOURCC_ARGB); diff --git a/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCaptureCallback.cpp b/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCaptureCallback.cpp index e7009406d..1fa492867 100644 --- a/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCaptureCallback.cpp +++ b/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCaptureCallback.cpp @@ -15,6 +15,7 @@ */ #include "media/video/desktop/DesktopCaptureCallback.h" +#include "media/video/desktop/WindowFrameCrop.h" #include "api/WebRTCUtils.h" #include "JavaClasses.h" #include "JavaEnums.h" @@ -33,8 +34,9 @@ namespace jni { - DesktopCaptureCallback::DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef & callback) : + DesktopCaptureCallback::DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef & callback, bool windowSource) : callback(callback), + windowSource(windowSource), javaClass(JavaClasses::get(env)), javaFrameClass(JavaClasses::get(env)) { @@ -72,17 +74,8 @@ namespace jni int crop_h = height; #if defined(WEBRTC_WIN) - // Crop black window borders. - bool fullscreen = frame->stride() == (frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel); - - if (!fullscreen) { - const webrtc::DesktopVector & top_left = frame->top_left(); - const int32_t border = GetSystemMetrics(SM_CXPADDEDBORDER); - - crop_x = border; - crop_y = top_left.y() < 0 ? -top_left.y() : 0; - crop_w = width - crop_x * 2; - crop_h = height - (crop_y + border); + if (windowSource) { + CropWindowBorder(*frame, GetSystemMetrics(SM_CXPADDEDBORDER), &crop_x, &crop_y, &crop_w, &crop_h); } #endif @@ -101,8 +94,8 @@ namespace jni // output. Passing i420Buffer->height() (== crop_h) here made libyuv's internal // bounds check (crop_y + crop_height <= src_height) fail with -1 whenever // crop_y > 0 — i.e. for every maximized window, whose frame sits at - // top_left().y() == -border. Screen frames never hit this branch (exact stride, - // fullscreen == true), which is why only window capture appeared broken. + // top_left().y() == -border. Screen frames are not cropped, which is why only + // window capture appeared broken. frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, height, crop_w, crop_h, libyuv::kRotate0, libyuv::FOURCC_ARGB); diff --git a/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCapturer.cpp b/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCapturer.cpp index 9b8a49369..0492624b3 100644 --- a/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCapturer.cpp +++ b/webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCapturer.cpp @@ -25,7 +25,8 @@ namespace jni { DesktopCapturer::DesktopCapturer(bool screenCapturer) : - focusSelectedSource(false) + focusSelectedSource(false), + windowCapturer(!screenCapturer) { auto options = webrtc::DesktopCaptureOptions::CreateDefault(); // Enable desktop effects. @@ -167,4 +168,9 @@ namespace jni if (!capturer) return false; return capturer->IsOccluded(pos); } + + bool DesktopCapturer::isWindowCapturer() const + { + return windowCapturer; + } } diff --git a/webrtc-jni/src/main/cpp/src/media/video/desktop/linux/LinuxPowerManagement.cpp b/webrtc-jni/src/main/cpp/src/media/video/desktop/linux/LinuxPowerManagement.cpp index c75201dff..b9707769b 100644 --- a/webrtc-jni/src/main/cpp/src/media/video/desktop/linux/LinuxPowerManagement.cpp +++ b/webrtc-jni/src/main/cpp/src/media/video/desktop/linux/LinuxPowerManagement.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "rtc_base/logging.h" @@ -26,6 +27,28 @@ namespace jni { namespace avdev { + namespace + { + struct ConnectionUnref + { + void operator()(DBusConnection * connection) const + { + DBusLoader::instance().dbus_connection_unref(connection); + } + }; + + struct MessageUnref + { + void operator()(DBusMessage * message) const + { + DBusLoader::instance().dbus_message_unref(message); + } + }; + + using ScopedConnection = std::unique_ptr; + using ScopedMessage = std::unique_ptr; + } + LinuxPowerManagement::LinuxPowerManagement() { } @@ -46,7 +69,7 @@ namespace jni DBusError error; dbus.dbus_error_init(&error); - DBusConnection * dbusConnection = dbus.dbus_bus_get(DBUS_BUS_SESSION, &error); + ScopedConnection dbusConnection(dbus.dbus_bus_get(DBUS_BUS_SESSION, &error)); if (dbus.dbus_error_is_set(&error)) { // throw @@ -55,7 +78,7 @@ namespace jni return; } - if (dbus.dbus_bus_name_has_owner(dbusConnection, BUS_SERVICE_NAME, NULL)) { + if (dbus.dbus_bus_name_has_owner(dbusConnection.get(), BUS_SERVICE_NAME, NULL)) { RTC_LOG(LS_INFO) << "[PowerManagement] Found service: " << BUS_SERVICE_NAME; } else { @@ -64,36 +87,31 @@ namespace jni return; } - DBusMessage * message = dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE, - "Inhibit"); + ScopedMessage message(dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE, + "Inhibit")); if (message == nullptr) { // throw return; } - dbus.dbus_message_append_args(message, DBUS_TYPE_STRING, &appName, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); + dbus.dbus_message_append_args(message.get(), DBUS_TYPE_STRING, &appName, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID); - DBusMessage * reply = dbus.dbus_connection_send_with_reply_and_block(dbusConnection, message, 50, &error); - dbus.dbus_message_unref(message); + ScopedMessage reply(dbus.dbus_connection_send_with_reply_and_block(dbusConnection.get(), message.get(), 50, &error)); if (dbus.dbus_error_is_set(&error)) { dbus.dbus_error_free(&error); - dbus.dbus_connection_unref(dbusConnection); // throw RTC_LOG(LS_ERROR) << "[PowerManagement] Cannot retrieve cookie"; return; } DBusMessageIter reply_iter; - dbus.dbus_message_iter_init(reply, &reply_iter); + dbus.dbus_message_iter_init(reply.get(), &reply_iter); dbus.dbus_message_iter_get_basic(&reply_iter, &dbusCookie); inhibited = true; RTC_LOG(LS_INFO) << "[PowerManagement] Acquired screensaver inhibition cookie"; - - dbus.dbus_message_unref(reply); - dbus.dbus_connection_unref(dbusConnection); } void LinuxPowerManagement::disableUserActivity() @@ -112,7 +130,7 @@ namespace jni DBusError error; dbus.dbus_error_init(&error); - DBusConnection * dbusConnection = dbus.dbus_bus_get(DBUS_BUS_SESSION, &error); + ScopedConnection dbusConnection(dbus.dbus_bus_get(DBUS_BUS_SESSION, &error)); if (dbus.dbus_error_is_set(&error)) { // throw @@ -121,34 +139,29 @@ namespace jni return; } - DBusMessage * message = dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE, - "UnInhibit"); + ScopedMessage message(dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE, + "UnInhibit")); if (message == nullptr) { // throw return; } - dbus.dbus_message_append_args(message, DBUS_TYPE_UINT32, &dbusCookie, DBUS_TYPE_INVALID); + dbus.dbus_message_append_args(message.get(), DBUS_TYPE_UINT32, &dbusCookie, DBUS_TYPE_INVALID); - DBusMessage * reply = dbus.dbus_connection_send_with_reply_and_block(dbusConnection, message, 50, &error); - dbus.dbus_message_unref(message); + ScopedMessage reply(dbus.dbus_connection_send_with_reply_and_block(dbusConnection.get(), message.get(), 50, &error)); if (dbus.dbus_error_is_set(&error)) { // throw RTC_LOG(LS_ERROR) << "[PowerManagement] Cannot release cookie"; dbus.dbus_error_free(&error); - dbus.dbus_connection_unref(dbusConnection); return; } inhibited = false; RTC_LOG(LS_INFO) << "[PowerManagement] Released screensaver inhibition cookie"; - - dbus.dbus_message_unref(reply); - dbus.dbus_connection_unref(dbusConnection); } } } \ No newline at end of file diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/desktop/PowerManagementTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/desktop/PowerManagementTests.java index d1b590fdc..388baac06 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/desktop/PowerManagementTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/desktop/PowerManagementTests.java @@ -21,6 +21,9 @@ import dev.onvoid.webrtc.TestBase; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -47,6 +50,16 @@ void nativeCallsSucceed() { }); } + @Test + void disableOnOtherThread() throws Exception { + PowerManagement powerManagement = new PowerManagement(); + + // Neither call may depend on the thread of the other one. + CompletableFuture.runAsync(powerManagement::enableUserActivity) + .thenRunAsync(powerManagement::disableUserActivity) + .get(10, TimeUnit.SECONDS); + } + @Test void repeatedEnableNeedsOneDisable() { PowerManagement powerManagement = new RecordingPowerManagement();