Skip to content

Commit c99481a

Browse files
committed
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.
1 parent 64cf799 commit c99481a

9 files changed

Lines changed: 130 additions & 52 deletions

File tree

‎webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCaptureCallback.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace jni
3131
class DesktopCaptureCallback : public webrtc::DesktopCapturer::Callback
3232
{
3333
public:
34-
DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback);
34+
DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback, bool windowSource);
3535
~DesktopCaptureCallback() override = default;
3636

3737
// DesktopCapturer::Callback implementation.
@@ -49,6 +49,8 @@ namespace jni
4949
private:
5050
JavaGlobalRef<jobject> callback;
5151

52+
const bool windowSource;
53+
5254
const std::shared_ptr<JavaDesktopCaptureCallbackClass> javaClass;
5355
const std::shared_ptr<JavaVideoFrameClass> javaFrameClass;
5456

‎webrtc-jni/src/main/cpp/include/media/video/desktop/DesktopCapturer.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ namespace jni
5656
bool FocusOnSelectedSource();
5757
void setFocusSelectedSource(bool focus);
5858
bool IsOccluded(const webrtc::DesktopVector & pos);
59+
bool isWindowCapturer() const;
5960

6061
protected:
6162
std::mutex startMutex;
@@ -67,6 +68,8 @@ namespace jni
6768

6869
bool focusSelectedSource;
6970

71+
const bool windowCapturer;
72+
7073
#if defined(WEBRTC_WIN)
7174
private:
7275
ComInitializer comInitializer;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#ifndef JNI_WEBRTC_MEDIA_DESKTOP_WINDOW_FRAME_CROP_H_
18+
#define JNI_WEBRTC_MEDIA_DESKTOP_WINDOW_FRAME_CROP_H_
19+
20+
#include "modules/desktop_capture/desktop_frame.h"
21+
#include "modules/desktop_capture/desktop_geometry.h"
22+
23+
#include <algorithm>
24+
25+
namespace jni
26+
{
27+
// Crops the black border of a frame captured from a window on Windows, for
28+
// window sources only. The window capturer's bitmaps have no row padding, so
29+
// a stride wider than a row means it cut the frame out of a larger bitmap to
30+
// drop a resize border. Windows without such a border come whole and keep
31+
// all their pixels. The crop values stay as they are when there is nothing
32+
// to crop, or when a frame too small for the border would be left empty.
33+
inline void CropWindowBorder(const webrtc::DesktopFrame & frame, int border, int * crop_x, int * crop_y, int * crop_w, int * crop_h)
34+
{
35+
const int width = frame.size().width();
36+
const int height = frame.size().height();
37+
38+
if (frame.stride() == width * webrtc::DesktopFrame::kBytesPerPixel) {
39+
return;
40+
}
41+
42+
const int top = std::max(0, -frame.top_left().y());
43+
const auto rect = webrtc::DesktopRect::MakeLTRB(border, top, width - border, height - border);
44+
45+
if (rect.is_empty()) {
46+
return;
47+
}
48+
49+
*crop_x = rect.left();
50+
*crop_y = rect.top();
51+
*crop_w = rect.width();
52+
*crop_h = rect.height();
53+
}
54+
}
55+
56+
#endif

‎webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_DesktopCapture
124124
jni::NativeLease<jni::DesktopCapturer> capturer(env, caller);
125125
CHECK_HANDLE(capturer);
126126

127-
auto callback = std::make_unique<jni::DesktopCaptureCallback>(env, jni::JavaGlobalRef<jobject>(env, jcallback));
127+
auto callback = std::make_unique<jni::DesktopCaptureCallback>(env, jni::JavaGlobalRef<jobject>(env, jcallback), capturer->isWindowCapturer());
128128

129129
if (!capturer->Start(std::move(callback))) {
130130
ThrowJavaException(env, "java/lang/IllegalStateException", "Capture has been started already");

‎webrtc-jni/src/main/cpp/src/media/video/VideoTrackDesktopSource.cpp‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "media/video/VideoTrackDesktopSource.h"
18+
#include "media/video/desktop/WindowFrameCrop.h"
1819
#include "Exception.h"
1920

2021
#include "api/video/i420_buffer.h"
@@ -170,17 +171,8 @@ namespace jni
170171
}
171172

172173
#if defined(WEBRTC_WIN)
173-
// Crop black window borders.
174-
bool fullscreen = frame->stride() == (frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel);
175-
176-
if (!fullscreen) {
177-
const webrtc::DesktopVector& top_left = frame->top_left();
178-
const int32_t border = GetSystemMetrics(SM_CXPADDEDBORDER);
179-
180-
crop_x = border;
181-
crop_y = top_left.y() < 0 ? -top_left.y() : 0;
182-
crop_w = width - crop_x * 2;
183-
crop_h = height - (crop_y + border);
174+
if (sourceIsWindow) {
175+
CropWindowBorder(*frame, GetSystemMetrics(SM_CXPADDEDBORDER), &crop_x, &crop_y, &crop_w, &crop_h);
184176
}
185177
#endif
186178

@@ -199,8 +191,8 @@ namespace jni
199191
// output. Passing buffer->height() (== crop_h) here made libyuv's internal
200192
// bounds check (crop_y + crop_height <= src_height) fail with -1 whenever
201193
// crop_y > 0 — i.e. for every maximized window, whose frame sits at
202-
// top_left().y() == -border. Screen frames never hit this branch (exact stride,
203-
// fullscreen == true), which is why only window capture appeared broken.
194+
// top_left().y() == -border. Screen frames are not cropped, which is why only
195+
// window capture appeared broken.
204196
frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, height, crop_w, crop_h,
205197
libyuv::kRotate0,
206198
libyuv::FOURCC_ARGB);

‎webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCaptureCallback.cpp‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "media/video/desktop/DesktopCaptureCallback.h"
18+
#include "media/video/desktop/WindowFrameCrop.h"
1819
#include "api/WebRTCUtils.h"
1920
#include "JavaClasses.h"
2021
#include "JavaEnums.h"
@@ -33,8 +34,9 @@
3334

3435
namespace jni
3536
{
36-
DesktopCaptureCallback::DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback) :
37+
DesktopCaptureCallback::DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback, bool windowSource) :
3738
callback(callback),
39+
windowSource(windowSource),
3840
javaClass(JavaClasses::get<JavaDesktopCaptureCallbackClass>(env)),
3941
javaFrameClass(JavaClasses::get<JavaVideoFrameClass>(env))
4042
{
@@ -72,17 +74,8 @@ namespace jni
7274
int crop_h = height;
7375

7476
#if defined(WEBRTC_WIN)
75-
// Crop black window borders.
76-
bool fullscreen = frame->stride() == (frame->size().width() * webrtc::DesktopFrame::kBytesPerPixel);
77-
78-
if (!fullscreen) {
79-
const webrtc::DesktopVector & top_left = frame->top_left();
80-
const int32_t border = GetSystemMetrics(SM_CXPADDEDBORDER);
81-
82-
crop_x = border;
83-
crop_y = top_left.y() < 0 ? -top_left.y() : 0;
84-
crop_w = width - crop_x * 2;
85-
crop_h = height - (crop_y + border);
77+
if (windowSource) {
78+
CropWindowBorder(*frame, GetSystemMetrics(SM_CXPADDEDBORDER), &crop_x, &crop_y, &crop_w, &crop_h);
8679
}
8780
#endif
8881

@@ -101,8 +94,8 @@ namespace jni
10194
// output. Passing i420Buffer->height() (== crop_h) here made libyuv's internal
10295
// bounds check (crop_y + crop_height <= src_height) fail with -1 whenever
10396
// crop_y > 0 — i.e. for every maximized window, whose frame sits at
104-
// top_left().y() == -border. Screen frames never hit this branch (exact stride,
105-
// fullscreen == true), which is why only window capture appeared broken.
97+
// top_left().y() == -border. Screen frames are not cropped, which is why only
98+
// window capture appeared broken.
10699
frame->stride() / webrtc::DesktopFrame::kBytesPerPixel, height, crop_w, crop_h,
107100
libyuv::kRotate0,
108101
libyuv::FOURCC_ARGB);

‎webrtc-jni/src/main/cpp/src/media/video/desktop/DesktopCapturer.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
namespace jni
2626
{
2727
DesktopCapturer::DesktopCapturer(bool screenCapturer) :
28-
focusSelectedSource(false)
28+
focusSelectedSource(false),
29+
windowCapturer(!screenCapturer)
2930
{
3031
auto options = webrtc::DesktopCaptureOptions::CreateDefault();
3132
// Enable desktop effects.
@@ -167,4 +168,9 @@ namespace jni
167168
if (!capturer) return false;
168169
return capturer->IsOccluded(pos);
169170
}
171+
172+
bool DesktopCapturer::isWindowCapturer() const
173+
{
174+
return windowCapturer;
175+
}
170176
}

‎webrtc-jni/src/main/cpp/src/media/video/desktop/linux/LinuxPowerManagement.cpp‎

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,36 @@
1919

2020
#include <cstdio>
2121
#include <dbus/dbus.h>
22+
#include <memory>
2223

2324
#include "rtc_base/logging.h"
2425

2526
namespace jni
2627
{
2728
namespace avdev
2829
{
30+
namespace
31+
{
32+
struct ConnectionUnref
33+
{
34+
void operator()(DBusConnection * connection) const
35+
{
36+
DBusLoader::instance().dbus_connection_unref(connection);
37+
}
38+
};
39+
40+
struct MessageUnref
41+
{
42+
void operator()(DBusMessage * message) const
43+
{
44+
DBusLoader::instance().dbus_message_unref(message);
45+
}
46+
};
47+
48+
using ScopedConnection = std::unique_ptr<DBusConnection, ConnectionUnref>;
49+
using ScopedMessage = std::unique_ptr<DBusMessage, MessageUnref>;
50+
}
51+
2952
LinuxPowerManagement::LinuxPowerManagement()
3053
{
3154
}
@@ -46,7 +69,7 @@ namespace jni
4669
DBusError error;
4770
dbus.dbus_error_init(&error);
4871

49-
DBusConnection * dbusConnection = dbus.dbus_bus_get(DBUS_BUS_SESSION, &error);
72+
ScopedConnection dbusConnection(dbus.dbus_bus_get(DBUS_BUS_SESSION, &error));
5073

5174
if (dbus.dbus_error_is_set(&error)) {
5275
// throw
@@ -55,7 +78,7 @@ namespace jni
5578
return;
5679
}
5780

58-
if (dbus.dbus_bus_name_has_owner(dbusConnection, BUS_SERVICE_NAME, NULL)) {
81+
if (dbus.dbus_bus_name_has_owner(dbusConnection.get(), BUS_SERVICE_NAME, NULL)) {
5982
RTC_LOG(LS_INFO) << "[PowerManagement] Found service: " << BUS_SERVICE_NAME;
6083
}
6184
else {
@@ -64,36 +87,31 @@ namespace jni
6487
return;
6588
}
6689

67-
DBusMessage * message = dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE,
68-
"Inhibit");
90+
ScopedMessage message(dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE,
91+
"Inhibit"));
6992

7093
if (message == nullptr) {
7194
// throw
7295
return;
7396
}
7497

75-
dbus.dbus_message_append_args(message, DBUS_TYPE_STRING, &appName, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
98+
dbus.dbus_message_append_args(message.get(), DBUS_TYPE_STRING, &appName, DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
7699

77-
DBusMessage * reply = dbus.dbus_connection_send_with_reply_and_block(dbusConnection, message, 50, &error);
78-
dbus.dbus_message_unref(message);
100+
ScopedMessage reply(dbus.dbus_connection_send_with_reply_and_block(dbusConnection.get(), message.get(), 50, &error));
79101

80102
if (dbus.dbus_error_is_set(&error)) {
81103
dbus.dbus_error_free(&error);
82-
dbus.dbus_connection_unref(dbusConnection);
83104
// throw
84105
RTC_LOG(LS_ERROR) << "[PowerManagement] Cannot retrieve cookie";
85106
return;
86107
}
87108

88109
DBusMessageIter reply_iter;
89-
dbus.dbus_message_iter_init(reply, &reply_iter);
110+
dbus.dbus_message_iter_init(reply.get(), &reply_iter);
90111
dbus.dbus_message_iter_get_basic(&reply_iter, &dbusCookie);
91112
inhibited = true;
92113

93114
RTC_LOG(LS_INFO) << "[PowerManagement] Acquired screensaver inhibition cookie";
94-
95-
dbus.dbus_message_unref(reply);
96-
dbus.dbus_connection_unref(dbusConnection);
97115
}
98116

99117
void LinuxPowerManagement::disableUserActivity()
@@ -112,7 +130,7 @@ namespace jni
112130
DBusError error;
113131
dbus.dbus_error_init(&error);
114132

115-
DBusConnection * dbusConnection = dbus.dbus_bus_get(DBUS_BUS_SESSION, &error);
133+
ScopedConnection dbusConnection(dbus.dbus_bus_get(DBUS_BUS_SESSION, &error));
116134

117135
if (dbus.dbus_error_is_set(&error)) {
118136
// throw
@@ -121,34 +139,29 @@ namespace jni
121139
return;
122140
}
123141

124-
DBusMessage * message = dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE,
125-
"UnInhibit");
142+
ScopedMessage message(dbus.dbus_message_new_method_call(BUS_SERVICE_NAME, BUS_SERVICE_PATH, BUS_INTERFACE,
143+
"UnInhibit"));
126144

127145
if (message == nullptr) {
128146
// throw
129147
return;
130148
}
131149

132-
dbus.dbus_message_append_args(message, DBUS_TYPE_UINT32, &dbusCookie, DBUS_TYPE_INVALID);
150+
dbus.dbus_message_append_args(message.get(), DBUS_TYPE_UINT32, &dbusCookie, DBUS_TYPE_INVALID);
133151

134-
DBusMessage * reply = dbus.dbus_connection_send_with_reply_and_block(dbusConnection, message, 50, &error);
135-
dbus.dbus_message_unref(message);
152+
ScopedMessage reply(dbus.dbus_connection_send_with_reply_and_block(dbusConnection.get(), message.get(), 50, &error));
136153

137154
if (dbus.dbus_error_is_set(&error)) {
138155
// throw
139156
RTC_LOG(LS_ERROR) << "[PowerManagement] Cannot release cookie";
140157

141158
dbus.dbus_error_free(&error);
142-
dbus.dbus_connection_unref(dbusConnection);
143159
return;
144160
}
145161

146162
inhibited = false;
147163

148164
RTC_LOG(LS_INFO) << "[PowerManagement] Released screensaver inhibition cookie";
149-
150-
dbus.dbus_message_unref(reply);
151-
dbus.dbus_connection_unref(dbusConnection);
152165
}
153166
}
154167
}

‎webrtc/src/test/java/dev/onvoid/webrtc/media/video/desktop/PowerManagementTests.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
import dev.onvoid.webrtc.TestBase;
2323

24+
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.TimeUnit;
26+
2427
import org.junit.jupiter.api.BeforeEach;
2528
import org.junit.jupiter.api.Test;
2629

@@ -47,6 +50,16 @@ void nativeCallsSucceed() {
4750
});
4851
}
4952

53+
@Test
54+
void disableOnOtherThread() throws Exception {
55+
PowerManagement powerManagement = new PowerManagement();
56+
57+
// Neither call may depend on the thread of the other one.
58+
CompletableFuture.runAsync(powerManagement::enableUserActivity)
59+
.thenRunAsync(powerManagement::disableUserActivity)
60+
.get(10, TimeUnit.SECONDS);
61+
}
62+
5063
@Test
5164
void repeatedEnableNeedsOneDisable() {
5265
PowerManagement powerManagement = new RecordingPowerManagement();

0 commit comments

Comments
 (0)