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
9 changes: 5 additions & 4 deletions docs/tools/desktop/power-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ API: `dev.onvoid.webrtc.media.video.desktop.PowerManagement`
- **Responsibility**: You must explicitly disable the assertion when you are done.

Key methods:
- `void enableUserActivity()` – Declare the user as active; prevents idle sleep.
- `void disableUserActivity()` – Revoke the assertion; system idle behavior resumes normally.
- `void enableUserActivity()` – Declare the user as active; prevents idle sleep. Enabling an instance repeatedly counts once.
- `void disableUserActivity()` – End the request of this instance; system idle behavior resumes normally once no other instance has an active request.

## Typical usage

Expand All @@ -38,13 +38,14 @@ finally {

## Integration tips

- Lifetime management: Keep the `PowerManagement` instance for as long as you need the assertion. It's safe to call `enableUserActivity()` once at the start and `disableUserActivity()` once at the end.
- Lifetime management: Keep the `PowerManagement` instance for as long as you need the assertion. Each instance is one independent request, so disabling another instance does not end it.
- Multiple users: Give each component its own instance. The system may idle to sleep again only after the last enabled instance of the process is disabled, on whichever thread.
- Fail‑safe: If your workflow can terminate unexpectedly, ensure `disableUserActivity()` is called (e.g., in `finally` blocks, shutdown hooks, or close handlers).
- Minimal footprint: Only enable while strictly necessary. Do not keep the assertion enabled longer than needed.

## Platform notes

- **Windows**: Uses native Windows power APIs to request that the display remain on while enabled.
- **Windows**: Uses Windows power requests to keep the display on while enabled.
- **Linux** (Freedesktop environments): Uses DBus screensaver inhibition (e.g., org.freedesktop.ScreenSaver) where available.
- **macOS**: Uses macOS power management assertions to prevent display sleep while enabled.

Expand Down
8 changes: 4 additions & 4 deletions webrtc-jni/src/main/cpp/include/JNI_PowerManagement.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace jni
PowerManagement() = default;
virtual ~PowerManagement() = default;

// Both are idempotent: the assertion is taken once and released once. Callers
// must serialize them; the Java class counts the requests of its instances.
virtual void enableUserActivity() = 0;
virtual void disableUserActivity() = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ namespace jni
void disableUserActivity();

private:
// Any value is a valid cookie, so it cannot tell whether one is held.
uint32_t dbusCookie = 0;
bool inhibited = false;

// KDE >= 4 and GNOME >= 3.10
const char * BUS_SERVICE_NAME = "org.freedesktop.ScreenSaver";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "media/video/desktop/PowerManagement.h"

#include <windows.h>

namespace jni
{
namespace avdev
Expand All @@ -31,6 +33,11 @@ namespace jni

void enableUserActivity();
void disableUserActivity();

private:
// Unlike SetThreadExecutionState, not tied to the calling thread: the last
// release may come from another thread and the enabling thread may exit.
HANDLE powerRequest = nullptr;
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions webrtc-jni/src/main/cpp/src/JNI_PowerManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "JNI_PowerManagement.h"
#include "WebRTCContext.h"

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_PowerManagement_enableUserActivity
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_PowerManagement_enableUserActivityInternal
(JNIEnv * env, jobject caller)
{
jni::WebRTCContext * context = static_cast<jni::WebRTCContext *>(javaContext);
Expand All @@ -32,7 +32,7 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_PowerManagemen
fflush(NULL);
}

JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_PowerManagement_disableUserActivity
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_PowerManagement_disableUserActivityInternal
(JNIEnv * env, jobject caller)
{
jni::WebRTCContext * context = static_cast<jni::WebRTCContext *>(javaContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace jni

void LinuxPowerManagement::enableUserActivity()
{
if (inhibited) {
return;
}

if (!DBusLoader::instance().load()) {
RTC_LOG(LS_WARNING) << "[PowerManagement] libdbus not found on system";
return;
Expand Down Expand Up @@ -84,6 +88,7 @@ namespace jni
DBusMessageIter reply_iter;
dbus.dbus_message_iter_init(reply, &reply_iter);
dbus.dbus_message_iter_get_basic(&reply_iter, &dbusCookie);
inhibited = true;

RTC_LOG(LS_INFO) << "[PowerManagement] Acquired screensaver inhibition cookie";

Expand All @@ -93,6 +98,10 @@ namespace jni

void LinuxPowerManagement::disableUserActivity()
{
if (!inhibited) {
return;
}

if (!DBusLoader::instance().load()) {
RTC_LOG(LS_WARNING) << "[PowerManagement] libdbus not found on system";
return;
Expand Down Expand Up @@ -134,6 +143,8 @@ namespace jni
return;
}

inhibited = false;

RTC_LOG(LS_INFO) << "[PowerManagement] Released screensaver inhibition cookie";

dbus.dbus_message_unref(reply);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ namespace jni

void MacOSPowerManagement::enableUserActivity()
{
if (powerAssertion != kIOPMNullAssertionID) {
return;
}

CFStringRef name = CFSTR("WebRTC-Java processing");

if (IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, name, &powerAssertion) != kIOReturnSuccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,66 @@

#include "media/video/desktop/windows/WindowsPowerManagement.h"

#include <windows.h>
#include "rtc_base/logging.h"

namespace jni
{
namespace avdev
{
namespace
{
constexpr POWER_REQUEST_TYPE kPowerRequestTypes[] = {
PowerRequestDisplayRequired,
PowerRequestSystemRequired,
PowerRequestAwayModeRequired
};
}

WindowsPowerManagement::WindowsPowerManagement()
{
}

void WindowsPowerManagement::enableUserActivity()
{
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED | ES_DISPLAY_REQUIRED);
if (powerRequest != nullptr) {
return;
}

wchar_t reason[] = L"WebRTC-Java processing";

REASON_CONTEXT context = {};
context.Version = POWER_REQUEST_CONTEXT_VERSION;
context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
context.Reason.SimpleReasonString = reason;

HANDLE request = PowerCreateRequest(&context);

if (request == INVALID_HANDLE_VALUE) {
RTC_LOG(LS_ERROR) << "[PowerManagement] Cannot create power request: " << GetLastError();
return;
}

for (POWER_REQUEST_TYPE type : kPowerRequestTypes) {
if (!PowerSetRequest(request, type)) {
RTC_LOG(LS_WARNING) << "[PowerManagement] Cannot set power request " << static_cast<int>(type) << ": " << GetLastError();
}
}

powerRequest = request;
}

void WindowsPowerManagement::disableUserActivity()
{
SetThreadExecutionState(ES_CONTINUOUS);
if (powerRequest == nullptr) {
return;
}

for (POWER_REQUEST_TYPE type : kPowerRequestTypes) {
PowerClearRequest(powerRequest, type);
}

CloseHandle(powerRequest);
powerRequest = nullptr;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@

/**
* Power management assertions to wake the display and prevent it from going to sleep on user idle.
* <p>
* Each instance is one independent request. Enabling an instance repeatedly counts once, so a single
* {@link #disableUserActivity()} ends its request. The system may idle to sleep again once the last
* enabled instance of this process is disabled. Keep the instance that enabled the request, since
* disabling another instance does not end it. All methods are thread-safe.
*
* @author Alex Andres
*/
public class PowerManagement {

/** Guards the request state and serializes access to the process-wide native assertion. */
private static final Object LOCK = new Object();

/** The number of instances with an active request. */
private static int activeRequests;

/** Whether this instance has an active request. */
private boolean active;


static {
try {
NativeLoader.loadLibrary("webrtc-java");
Expand All @@ -35,13 +50,41 @@ public class PowerManagement {
}

/**
* Declares that the user is active on the system and prevents the sleep idle time-out.
* Declares that the user is active on the system and prevents the sleep idle time-out. Has no further
* effect if this instance is already enabled.
*/
native public void enableUserActivity();
public void enableUserActivity() {
synchronized (LOCK) {
if (!active) {
active = true;
activeRequests++;
}

// No-op while the native assertion is held; otherwise retries one that could not be taken.
enableUserActivityInternal();
}
}

/**
* Declares that the user is no longer active on the system and allow the system to idle to sleep normally.
* Ends the request of this instance. The system may idle to sleep normally once no other instance has
* an active request. Has no further effect if this instance is not enabled.
*/
native public void disableUserActivity();
public void disableUserActivity() {
synchronized (LOCK) {
if (active) {
active = false;
activeRequests--;
}

// No-op without a native assertion; otherwise also retries a release that failed.
if (activeRequests == 0) {
disableUserActivityInternal();
}
}
}

native void enableUserActivityInternal();

native void disableUserActivityInternal();

}
Loading
Loading