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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace jni
class DesktopCaptureCallback : public webrtc::DesktopCapturer::Callback
{
public:
DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback);
DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback, bool windowSource);
~DesktopCaptureCallback() override = default;

// DesktopCapturer::Callback implementation.
Expand All @@ -49,6 +49,8 @@ namespace jni
private:
JavaGlobalRef<jobject> callback;

const bool windowSource;

const std::shared_ptr<JavaDesktopCaptureCallbackClass> javaClass;
const std::shared_ptr<JavaVideoFrameClass> javaFrameClass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -67,6 +68,8 @@ namespace jni

bool focusSelectedSource;

const bool windowCapturer;

#if defined(WEBRTC_WIN)
private:
ComInitializer comInitializer;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <algorithm>

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
2 changes: 1 addition & 1 deletion webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_DesktopCapture
jni::NativeLease<jni::DesktopCapturer> capturer(env, caller);
CHECK_HANDLE(capturer);

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

if (!capturer->Start(std::move(callback))) {
ThrowJavaException(env, "java/lang/IllegalStateException", "Capture has been started already");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "media/video/VideoTrackDesktopSource.h"
#include "media/video/desktop/WindowFrameCrop.h"
#include "Exception.h"

#include "api/video/i420_buffer.h"
Expand Down Expand Up @@ -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

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,8 +34,9 @@

namespace jni
{
DesktopCaptureCallback::DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback) :
DesktopCaptureCallback::DesktopCaptureCallback(JNIEnv * env, const JavaGlobalRef<jobject> & callback, bool windowSource) :
callback(callback),
windowSource(windowSource),
javaClass(JavaClasses::get<JavaDesktopCaptureCallbackClass>(env)),
javaFrameClass(JavaClasses::get<JavaVideoFrameClass>(env))
{
Expand Down Expand Up @@ -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

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
namespace jni
{
DesktopCapturer::DesktopCapturer(bool screenCapturer) :
focusSelectedSource(false)
focusSelectedSource(false),
windowCapturer(!screenCapturer)
{
auto options = webrtc::DesktopCaptureOptions::CreateDefault();
// Enable desktop effects.
Expand Down Expand Up @@ -167,4 +168,9 @@ namespace jni
if (!capturer) return false;
return capturer->IsOccluded(pos);
}

bool DesktopCapturer::isWindowCapturer() const
{
return windowCapturer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,36 @@

#include <cstdio>
#include <dbus/dbus.h>
#include <memory>

#include "rtc_base/logging.h"

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<DBusConnection, ConnectionUnref>;
using ScopedMessage = std::unique_ptr<DBusMessage, MessageUnref>;
}

LinuxPowerManagement::LinuxPowerManagement()
{
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
Loading