|
| 1 | +# Screen Capturer |
| 2 | + |
| 3 | +The `ScreenCapturer` enumerates and captures full desktop screens/monitors. Use it to list available screens and to drive screen-source selection for desktop capture workflows. |
| 4 | + |
| 5 | +API: `dev.onvoid.webrtc.media.video.desktop.ScreenCapturer` |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +- Purpose: Discover and capture full screens (monitors). |
| 10 | +- Pairs with: `VideoDesktopSource` to produce a capturable video track from a selected screen ID. |
| 11 | +- Common ops: list screens, select one, configure capture parameters on `DesktopCapturer`, start capture via callback, or pass the ID to `VideoDesktopSource`. |
| 12 | +- Resource management: Call `dispose()` when finished to free native resources. |
| 13 | + |
| 14 | +Key methods (inherited from `DesktopCapturer`): |
| 15 | +- `List<DesktopSource> getDesktopSources()` – enumerate available screens |
| 16 | +- `void selectSource(DesktopSource source)` – choose the screen to capture |
| 17 | +- `void setFocusSelectedSource(boolean focus)` – try to focus the selected source during capture |
| 18 | +- `void setMaxFrameRate(int maxFrameRate)` – cap the capture FPS |
| 19 | +- `void start(DesktopCaptureCallback callback)` – begin capturing; frames are delivered via callback |
| 20 | +- `void captureFrame()` – request a single frame (manual capture) |
| 21 | +- `void dispose()` – release resources |
| 22 | + |
| 23 | +## Typical usage: enumerate screens |
| 24 | + |
| 25 | +```java |
| 26 | +import dev.onvoid.webrtc.media.video.desktop.DesktopSource; |
| 27 | +import dev.onvoid.webrtc.media.video.desktop.ScreenCapturer; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +ScreenCapturer screenCapturer = new ScreenCapturer(); |
| 31 | +try { |
| 32 | + List<DesktopSource> screens = screenCapturer.getDesktopSources(); |
| 33 | + for (DesktopSource s : screens) { |
| 34 | + System.out.printf("Screen: %s (ID: %d)%n", s.title, s.id); |
| 35 | + } |
| 36 | +} |
| 37 | +finally { |
| 38 | + screenCapturer.dispose(); |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Selecting a screen and starting capture with a callback |
| 43 | + |
| 44 | +```java |
| 45 | +import dev.onvoid.webrtc.media.video.desktop.*; |
| 46 | +import dev.onvoid.webrtc.media.video.VideoFrame; |
| 47 | + |
| 48 | +ScreenCapturer capturer = new ScreenCapturer(); |
| 49 | +try { |
| 50 | + // pick the first screen |
| 51 | + DesktopSource screen = capturer.getDesktopSources().stream().findFirst() |
| 52 | + .orElseThrow(() -> new IllegalStateException("No screens found")); |
| 53 | + |
| 54 | + capturer.selectSource(screen); |
| 55 | + capturer.setMaxFrameRate(30); |
| 56 | + capturer.setFocusSelectedSource(false); |
| 57 | + |
| 58 | + DesktopCaptureCallback callback = (result, frame) -> { |
| 59 | + if (result == DesktopCapturer.Result.SUCCESS && frame != null) { |
| 60 | + // process VideoFrame |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + capturer.start(callback); |
| 65 | + |
| 66 | + // Optionally trigger ad-hoc capture |
| 67 | + capturer.captureFrame(); |
| 68 | +} |
| 69 | +finally { |
| 70 | + capturer.dispose(); |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Using with VideoDesktopSource to create a VideoTrack |
| 75 | + |
| 76 | +For most WebRTC pipelines, you will create a `VideoDesktopSource` and set the selected screen ID: |
| 77 | + |
| 78 | +```java |
| 79 | +import dev.onvoid.webrtc.media.video.VideoDesktopSource; |
| 80 | +import dev.onvoid.webrtc.media.video.desktop.*; |
| 81 | + |
| 82 | +ScreenCapturer sc = new ScreenCapturer(); |
| 83 | +DesktopSource screen = sc.getDesktopSources().get(0); |
| 84 | + |
| 85 | +VideoDesktopSource vds = new VideoDesktopSource(); |
| 86 | +vds.setFrameRate(30); |
| 87 | +vds.setMaxFrameSize(1920, 1080); |
| 88 | + |
| 89 | +// Select the screen (isWindow = false) |
| 90 | +vds.setSourceId(screen.id, false); |
| 91 | + |
| 92 | +// Start the source and use it to create a VideoTrack in your PeerConnection |
| 93 | +vds.start(); |
| 94 | + |
| 95 | +// ... |
| 96 | + |
| 97 | +// cleanup |
| 98 | +vds.stop(); |
| 99 | +vds.dispose(); |
| 100 | +sc.dispose(); |
| 101 | +``` |
| 102 | + |
| 103 | +## Integration tips |
| 104 | + |
| 105 | +- If screens can change (hot‑plug monitors), refresh `getDesktopSources()` as needed. |
| 106 | +- Use `setMaxFrameRate` to limit capture load on the system. |
| 107 | +- Prefer using `VideoDesktopSource` for WebRTC pipelines; keep `ScreenCapturer` for discovery and advanced control. |
| 108 | + |
| 109 | +## Related guides |
| 110 | + |
| 111 | +- [Desktop Capture](guide/desktop_capture.md) |
| 112 | +- [Power Management](guide/power_management.md) |
0 commit comments