|
| 1 | +# Power Management |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +API: `dev.onvoid.webrtc.media.video.desktop.PowerManagement` |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +- Purpose: Temporarily prevent the system from sleeping due to user idle. |
| 10 | +- Scope: Affects display sleep/idle behavior while enabled. |
| 11 | +- Cross‑platform: Implements native integrations for Windows, Linux, and macOS. |
| 12 | +- Responsibility: You must explicitly disable the assertion when you are done. |
| 13 | + |
| 14 | +Key methods: |
| 15 | +- `void enableUserActivity()` – Declare the user as active; prevents idle sleep. |
| 16 | +- `void disableUserActivity()` – Revoke the assertion; system idle behavior resumes normally. |
| 17 | + |
| 18 | +## Typical usage |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +```java |
| 23 | +import dev.onvoid.webrtc.media.video.desktop.PowerManagement; |
| 24 | + |
| 25 | +PowerManagement pm = new PowerManagement(); |
| 26 | + |
| 27 | +// Example: keep display awake during a screen sharing session |
| 28 | +pm.enableUserActivity(); |
| 29 | +try { |
| 30 | + // Start and run your desktop capture / screen sharing pipeline |
| 31 | + // ... |
| 32 | +} |
| 33 | +finally { |
| 34 | + // Always restore normal behavior |
| 35 | + pm.disableUserActivity(); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Integration tips |
| 40 | + |
| 41 | +- 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. |
| 42 | +- Fail‑safe: If your workflow can terminate unexpectedly, ensure `disableUserActivity()` is called (e.g., in `finally` blocks, shutdown hooks, or close handlers). |
| 43 | +- Minimal footprint: Only enable while strictly necessary. Do not keep the assertion enabled longer than needed. |
| 44 | + |
| 45 | +## Platform notes |
| 46 | + |
| 47 | +- Windows: Uses native Windows power APIs to request that the display remain on while enabled. |
| 48 | +- Linux (Freedesktop environments): Uses DBus screensaver inhibition (e.g., org.freedesktop.ScreenSaver) where available. |
| 49 | +- macOS: Uses macOS power management assertions to prevent display sleep while enabled. |
| 50 | + |
| 51 | +Exact mechanisms are handled by the native layer; your Java code remains the same across platforms. |
| 52 | + |
| 53 | +## When to use |
| 54 | + |
| 55 | +- While capturing or sharing the desktop to prevent the monitor from sleeping during inactivity. |
| 56 | +- During long‑running, unattended demos, playback, or monitoring dashboards where user input is infrequent. |
| 57 | + |
| 58 | +## Related guides |
| 59 | + |
| 60 | +- [Desktop Capture](guide/desktop_capture.md) |
| 61 | +- [Logging](guide/logging.md) |
0 commit comments