Power Management
The PowerManagement utility allows your application to keep the display awake and prevent the operating system from idling to sleep while the user is considered "active" by your app. This is especially useful during screen sharing, presentations, or long-running desktop capture sessions where user input may be minimal.
API: dev.onvoid.webrtc.media.video.desktop.PowerManagement
Overview
- Purpose: Temporarily prevent the system from sleeping due to user idle.
- Scope: Affects display sleep/idle behavior while enabled.
- Cross‑platform: Implements native integrations for Windows, Linux, and macOS.
- Responsibility: You must explicitly disable the assertion when you are done.
Key methods:
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
Call enableUserActivity() when you start an operation that must keep the display awake (e.g., desktop capture or a presentation). Always pair it with disableUserActivity() (for example, in a finally block) to restore the normal power behavior.
java
import dev.onvoid.webrtc.media.video.desktop.PowerManagement;
PowerManagement pm = new PowerManagement();
// Example: keep display awake during a screen sharing session
pm.enableUserActivity();
try {
// Start and run your desktop capture / screen sharing pipeline
// ...
}
finally {
// Always restore normal behavior
pm.disableUserActivity();
}Integration tips
- Lifetime management: Keep the
PowerManagementinstance 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., infinallyblocks, 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 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.
Exact mechanisms are handled by the native layer; your Java code remains the same across platforms.
When to use
- While capturing or sharing the desktop to prevent the monitor from sleeping during inactivity.
- During long‑running, unattended demos, playback, or monitoring dashboards where user input is infrequent.
