diff --git a/docs/guide/examples.md b/docs/guide/examples.md index c304b5f0a..beefa694c 100644 --- a/docs/guide/examples.md +++ b/docs/guide/examples.md @@ -148,6 +148,29 @@ Each audio sender gets the audio of its own track, so the microphone keeps its e mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.ScreenShareAudioExample" ``` +## Desktop Picker + +The [`DesktopPickerExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopPickerExample.java) lets the user choose a screen or window in the operating system's own picker, on macOS 14 and newer and under Wayland. + +**Key features demonstrated:** +- Checking whether the system offers a picker +- Showing the picker by starting a `VideoDesktopSource` +- Reacting to the end of the track when the picker is cancelled or sharing stops + +## Desktop Capturer Picker + +The [`DesktopCapturerPickerExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopCapturerPickerExample.java) is a small Swing application that takes frames from a `ScreenCapturer` without a video track, after the user has chosen a screen or window in the operating system's picker. + +**Key features demonstrated:** +- Setting `setUseSystemPicker(true)` before `start()`, which shows the picker +- Calling `captureFrame()` at the frame rate, on the one thread that uses the capturer +- Showing the frames in a Swing `VideoView` +- Telling a pending choice (`ERROR_TEMPORARY`) from a cancelled picker or stopped sharing (`ERROR_PERMANENT`) + +```bash +mvn exec:java -D"exec.mainClass=dev.onvoid.webrtc.examples.DesktopCapturerPickerExample" +``` + ## Media File The [`MediaFileExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/MediaFileExample.java) demonstrates how to send a media file over a peer connection, in place of a camera and a microphone. See the [Media Files](/guide/media/media-files) guide for the API it uses. diff --git a/docs/guide/video/desktop-capture.md b/docs/guide/video/desktop-capture.md index 9712ed7be..d2d68ff7b 100644 --- a/docs/guide/video/desktop-capture.md +++ b/docs/guide/video/desktop-capture.md @@ -70,6 +70,27 @@ videoSource.setSourceId(windowId, true); videoSource.start(); ``` +### Letting the User Choose in the System Picker + +Instead of listing sources yourself, you can let the user choose a screen or window in the operating system's own picker. It is available on macOS 14 and newer, where it is ScreenCaptureKit's content sharing picker, and on Linux under Wayland, where the desktop portal's picker is always used: + +```java +VideoDesktopSource videoSource = new VideoDesktopSource(); +videoSource.setFrameRate(30); + +if (VideoDesktopSource.isSystemPickerAvailable()) { + // The picker chooses the source; no setSourceId() is needed. + videoSource.setUseSystemPicker(true); +} +else { + videoSource.setSourceId(screenId, false); +} +``` + +`start()` shows the picker and returns at once. No frames arrive until the user has made a choice. The user can change it later from the macOS menu bar, and the frames follow. If the user cancels the picker, or stops sharing from the menu bar, the source ends (see [Handling the End of Capture](#handling-the-end-of-capture)). Where `isSystemPickerAvailable()` is false, a source set to use the picker ends right away instead of capturing. + +On macOS the picker offers a whole screen or one or more windows. Show it for one source at a time: sources waiting on it at the same time all receive the user's choice. To take frames yourself rather than through a track, `ScreenCapturer` and `WindowCapturer` offer the same picker (see [Screen Capturer](/tools/desktop/screen-capturer#letting-the-user-choose-in-the-system-picker)). The [`DesktopPickerExample`](https://github.com/devopvoid/webrtc-java/blob/master/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopPickerExample.java) shows the picker and reports the frames it receives. + ### Creating a Video Track with the Desktop Source Once you have configured your desktop video source, you can create a video track: @@ -153,16 +174,20 @@ videoSource.stop(); videoSource.dispose(); ``` -### Handling Capture Failures +### Handling the End of Capture -`start()` returns before capturing begins. If capturing cannot begin, because no capture backend is available or the selected source does not exist, the source ends and its tracks with it. Create the track and add the listener before calling `start()`, as a listener added later misses this: +`start()` returns before capturing begins. The source ends, and its tracks with it, when capturing cannot begin or cannot go on: no capture backend is available, the selected source does not exist, the captured window was closed, or the user cancelled the system picker or stopped sharing. Create the track and add the listener before calling `start()`, as a listener added later misses an end that comes at once: ```java videoTrack.addTrackEndedListener(track -> { - // No frames will arrive from this source. + // No more frames will arrive from this source. The listener runs on the + // capture thread; stopping or disposing the source here is safe. + videoSource.stop(); }); ``` +`dispose()` ends the capture as well, even while a track still holds the source. + ### Processing Frames A `VideoDesktopSource` can pass its frames through a `VideoProcessor` before they reach its tracks, for example to blank out a part of the screen. It works as for a camera; see [Changing Frames Before They Are Sent](/guide/video/camera-capture#changing-frames-before-they-are-sent). @@ -188,7 +213,7 @@ windowCapturer.dispose(); On macOS 14 and newer, screens and windows are captured with ScreenCaptureKit; older releases use the earlier Core Graphics capture. Both need the app to have Screen Recording permission (System Settings → Privacy & Security → Screen & System Audio Recording). Without it, the windows of other apps are listed without their titles and their content cannot be captured. Windows of the app's own process need no permission, and are captured with Core Graphics. -While a captured window is minimized or hidden, its last image repeats. When it is closed, capture stops. +While a captured window is minimized or hidden, its last image repeats. When it is closed, the source ends. ## Conclusion diff --git a/docs/tools/desktop/screen-capturer.md b/docs/tools/desktop/screen-capturer.md index 67b9219b1..52b318409 100644 --- a/docs/tools/desktop/screen-capturer.md +++ b/docs/tools/desktop/screen-capturer.md @@ -16,6 +16,8 @@ Key methods (inherited from `DesktopCapturer`): - `void selectSource(DesktopSource source)` – choose the screen to capture - `void setFocusSelectedSource(boolean focus)` – try to focus the selected source during capture - `void setMaxFrameRate(int maxFrameRate)` – cap the capture FPS +- `void setUseSystemPicker(boolean usePicker)` – let the user choose a screen or windows in the operating system's picker, shown by `start`, instead of `selectSource`; set it before `start`, a later call throws `IllegalStateException` +- `static boolean isSystemPickerAvailable()` – whether the system has such a picker: macOS 14 and newer, and Linux under Wayland - `void start(DesktopCaptureCallback callback)` – begin capturing; frames are delivered via callback. A capturer can be started once, a second call throws `IllegalStateException` - `void captureFrame()` – request a single frame (manual capture) after `start`; without a capture backend, as on Linux without a display, the callback gets `ERROR_PERMANENT` - `void dispose()` – release resources @@ -71,6 +73,36 @@ finally { } ``` +## Letting the user choose in the system picker + +Instead of selecting a source, a capturer can show the operating system's own picker when it starts. On macOS 14 and newer that is ScreenCaptureKit's content sharing picker, which offers a whole screen or one or more windows, for a `ScreenCapturer` and a `WindowCapturer` alike. Under Wayland it is the desktop portal's picker: + +```java +ScreenCapturer capturer = new ScreenCapturer(); +try { + if (DesktopCapturer.isSystemPickerAvailable()) { + // The picker chooses the source; no selectSource() is needed. + capturer.setUseSystemPicker(true); + } + + capturer.start((result, frame) -> { + if (result == DesktopCapturer.Result.SUCCESS && frame != null) { + // process VideoFrame + } + else if (result == DesktopCapturer.Result.ERROR_PERMANENT) { + // the picker was cancelled, or sharing stopped + } + }); + + // Call captureFrame() periodically +} +finally { + capturer.dispose(); +} +``` + +Set it before `start`, which shows the picker and returns at once. Until the user has chosen, `captureFrame()` reports `ERROR_TEMPORARY`. If the user cancels the picker, or stops sharing from the macOS menu bar, it reports `ERROR_PERMANENT`. Where `isSystemPickerAvailable()` is false, it reports `ERROR_PERMANENT` from the first frame on, instead of capturing something the user did not choose. Show the picker for one capturer at a time: capturers waiting on it at the same time all receive the user's choice. + ## Using with VideoDesktopSource to create a VideoTrack For most WebRTC pipelines, you will create a `VideoDesktopSource` and set the selected screen ID: diff --git a/docs/tools/desktop/window-capturer.md b/docs/tools/desktop/window-capturer.md index 6f533b5c6..bf383a372 100644 --- a/docs/tools/desktop/window-capturer.md +++ b/docs/tools/desktop/window-capturer.md @@ -16,6 +16,8 @@ Key methods (inherited from `DesktopCapturer`): - `void selectSource(DesktopSource source)` – choose which window to capture - `void setFocusSelectedSource(boolean focus)` – attempt to focus/raise the selected window - `void setMaxFrameRate(int maxFrameRate)` – limit the capture frame rate +- `void setUseSystemPicker(boolean usePicker)` – let the user choose a screen or windows in the operating system's picker, shown by `start`, instead of `selectSource`; set it before `start`, a later call throws `IllegalStateException` +- `static boolean isSystemPickerAvailable()` – whether the system has such a picker: macOS 14 and newer, and Linux under Wayland - `void start(DesktopCaptureCallback callback)` – start capture, frames via callback. A capturer can be started once, a second call throws `IllegalStateException` - `void captureFrame()` – request a single capture after `start`; without a capture backend, as on Linux without a display, the callback gets `ERROR_PERMANENT` - `void dispose()` – release resources @@ -68,6 +70,10 @@ finally { } ``` +## Letting the user choose in the system picker + +With `setUseSystemPicker(true)` before `start`, the capturer shows the operating system's picker instead of capturing a selected window. It works the same way for a `WindowCapturer` as for a `ScreenCapturer`; see [Letting the user choose in the system picker](/tools/desktop/screen-capturer#letting-the-user-choose-in-the-system-picker). + ## Using with VideoDesktopSource to create a VideoTrack ```java diff --git a/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopCapturerPickerExample.java b/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopCapturerPickerExample.java new file mode 100644 index 000000000..9007493a9 --- /dev/null +++ b/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopCapturerPickerExample.java @@ -0,0 +1,242 @@ +/* + * Copyright 2026 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. + */ + +package dev.onvoid.webrtc.examples; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.SwingUtilities; + +import dev.onvoid.webrtc.client.swing.VideoView; +import dev.onvoid.webrtc.media.video.VideoFrame; +import dev.onvoid.webrtc.media.video.desktop.DesktopCapturer; +import dev.onvoid.webrtc.media.video.desktop.ScreenCapturer; + +/** + * Swing example taking frames from a {@link DesktopCapturer} whose source the + * user chooses in the operating system's own picker, without a video track. + *
+ * "Choose" starts a {@link ScreenCapturer} set to use the system picker: on + * macOS 14 and newer, ScreenCaptureKit's content sharing picker, which offers + * a screen or one or more windows; under Wayland, the desktop portal's + * picker. The window shows the captured frames and the last capture result. + * Cancelling the picker, or stopping the sharing from the macOS menu bar, ends + * the capture with {@link DesktopCapturer.Result#ERROR_PERMANENT}. + *
+ * A capturer is used from one thread only, here a single scheduled thread + * that starts it, calls {@link DesktopCapturer#captureFrame()} at the frame + * rate and disposes of it. + *
+ * Run it with: + *
+ * mvn -pl webrtc-examples exec:java -Dexec.mainClass=dev.onvoid.webrtc.examples.DesktopCapturerPickerExample + *+ * + * @author Alex Andres + */ +public class DesktopCapturerPickerExample { + + private static final int FRAME_RATE = 30; + + private final ScheduledExecutorService captureThread = Executors.newSingleThreadScheduledExecutor(r -> { + Thread thread = new Thread(r, "desktop-capture"); + thread.setDaemon(true); + return thread; + }); + + private final VideoView view = new VideoView(); + private final JLabel status = new JLabel(); + private final JButton chooseButton = new JButton("Choose…"); + private final JButton stopButton = new JButton("Stop"); + private final JFrame window = new JFrame("Desktop Capturer Picker"); + + // Used on the capture thread only. + private DesktopCapturer capturer; + private DesktopCapturer.Result lastResult; + private boolean ended; + private long frames; + + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new DesktopCapturerPickerExample().show()); + } + + private void show() { + view.setBackground(Color.DARK_GRAY); + view.setPreferredSize(new Dimension(960, 540)); + + status.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8)); + + chooseButton.addActionListener(e -> startCapture()); + stopButton.addActionListener(e -> stopCapture("Stopped.")); + stopButton.setEnabled(false); + + JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT)); + controls.add(chooseButton); + controls.add(stopButton); + + JPanel bottom = new JPanel(new BorderLayout()); + bottom.add(controls, BorderLayout.WEST); + bottom.add(status, BorderLayout.CENTER); + + window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + window.addWindowListener(new WindowAdapter() { + + @Override + public void windowClosed(WindowEvent e) { + captureThread.execute(() -> disposeCapturer()); + captureThread.shutdown(); + } + }); + window.getContentPane().add(view, BorderLayout.CENTER); + window.getContentPane().add(bottom, BorderLayout.SOUTH); + window.pack(); + window.setLocationRelativeTo(null); + window.setVisible(true); + + if (DesktopCapturer.isSystemPickerAvailable()) { + setStatus("Press \"Choose…\" to pick a screen or window."); + } + else { + chooseButton.setEnabled(false); + setStatus("This system offers no picker for screens and windows."); + } + + // Captures while a capturer is running. + captureThread.scheduleAtFixedRate(this::captureFrame, 0, 1000 / FRAME_RATE, TimeUnit.MILLISECONDS); + } + + private void startCapture() { + chooseButton.setEnabled(false); + stopButton.setEnabled(true); + view.clear(); + + captureThread.execute(() -> { + disposeCapturer(); + + lastResult = null; + ended = false; + frames = 0; + + capturer = new ScreenCapturer(); + capturer.setMaxFrameRate(FRAME_RATE); + // Set before start(). No selectSource() is needed: the user + // chooses in the picker, which start() shows. + capturer.setUseSystemPicker(true); + capturer.start(this::onCaptureResult); + + setStatus("Choose a screen or window in the picker."); + }); + } + + private void stopCapture(String message) { + captureThread.execute(() -> { + disposeCapturer(); + setStatus(message); + }); + + chooseButton.setEnabled(true); + stopButton.setEnabled(false); + } + + private void captureFrame() { + if (capturer == null) { + return; + } + + try { + capturer.captureFrame(); + } + catch (RuntimeException e) { + ended = true; + setStatus("Capture failed: " + e.getMessage()); + } + + if (ended) { + // Disposed of here, not in the callback that reported the end. + disposeCapturer(); + + SwingUtilities.invokeLater(() -> { + chooseButton.setEnabled(true); + stopButton.setEnabled(false); + }); + } + } + + /** + * Called on the capture thread from within {@link DesktopCapturer#captureFrame()}. + */ + private void onCaptureResult(DesktopCapturer.Result result, VideoFrame frame) { + try { + switch (result) { + case SUCCESS: + if (frame != null) { + view.accept(frame); + frames++; + + setStatus(String.format("Capturing: %d frames, %dx%d", frames, + frame.buffer.getWidth(), frame.buffer.getHeight())); + } + break; + + case ERROR_TEMPORARY: + // No frame yet, while the picker waits for the user. + if (lastResult != DesktopCapturer.Result.ERROR_TEMPORARY && frames == 0) { + setStatus("Waiting for a choice in the picker…"); + } + break; + + default: + ended = true; + setStatus(frames == 0 + ? "The picker was cancelled." + : "Sharing stopped after " + frames + " frames."); + break; + } + + lastResult = result; + } + finally { + if (frame != null) { + frame.release(); + } + } + } + + private void disposeCapturer() { + if (capturer != null) { + capturer.dispose(); + capturer = null; + } + } + + private void setStatus(String text) { + SwingUtilities.invokeLater(() -> status.setText(text)); + } +} diff --git a/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopPickerExample.java b/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopPickerExample.java new file mode 100644 index 000000000..a2aa59a90 --- /dev/null +++ b/webrtc-examples/src/main/java/dev/onvoid/webrtc/examples/DesktopPickerExample.java @@ -0,0 +1,94 @@ +/* + * Copyright 2026 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. + */ + +package dev.onvoid.webrtc.examples; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import dev.onvoid.webrtc.PeerConnectionFactory; +import dev.onvoid.webrtc.media.video.VideoDesktopSource; +import dev.onvoid.webrtc.media.video.VideoTrack; +import dev.onvoid.webrtc.media.video.VideoTrackSink; + +/** + * Example letting the user choose a screen or window in the operating + * system's own picker, instead of listing sources in the application. + *
+ * On macOS 14 and newer, {@link VideoDesktopSource#start()} shows + * ScreenCaptureKit's content sharing picker. The example prints the number and + * size of the frames it receives, and reports when the track ends: when the + * picker is cancelled, or the user stops sharing from the menu bar. + *
+ * Run it with: + *
+ * mvn -pl webrtc-examples exec:java -Dexec.mainClass=dev.onvoid.webrtc.examples.DesktopPickerExample + *+ * + * @author Alex Andres + */ +public class DesktopPickerExample { + + public static void main(String[] args) throws Exception { + if (!VideoDesktopSource.isSystemPickerAvailable()) { + System.out.println("This system offers no picker for screens and windows."); + return; + } + + PeerConnectionFactory factory = new PeerConnectionFactory(); + VideoDesktopSource videoSource = new VideoDesktopSource(); + videoSource.setFrameRate(15); + videoSource.setUseSystemPicker(true); + + VideoTrack videoTrack = factory.createVideoTrack("video0", videoSource); + + AtomicInteger frames = new AtomicInteger(); + AtomicReference
+ * On macOS 14 and newer this is ScreenCaptureKit's content sharing + * picker, which offers a screen or one or more windows. Under Wayland the + * desktop portal's picker is used with or without this setting. Where + * {@link #isSystemPickerAvailable()} is false, the source ends instead of + * capturing. + *
+ * {@link #start()} shows the picker and returns at once; no frames arrive + * until the user has chosen. If the user cancels the picker, or stops + * sharing from the menu bar, the source ends and its tracks call their + * {@link dev.onvoid.webrtc.media.MediaStreamTrackEndedListener}s. Show the + * picker for one source at a time: sources waiting on it at the same time + * all receive the user's choice. + * + * @param usePicker True to show the system picker when the source starts. + */ + public native void setUseSystemPicker(boolean usePicker); + + /** + * Whether this system offers a picker for + * {@link #setUseSystemPicker(boolean)}: macOS 14 and newer, and Linux + * under Wayland. + * + * @return True if a system picker is available. + */ + public static native boolean isSystemPickerAvailable(); + public native void start(); + /** + * Stops capturing and waits for the capture to finish. Called from a + * listener that the capture invokes, such as an ended listener, it does + * not wait. + */ public native void stop(); + /** + * Stops capturing, without waiting for the capture to finish, and + * releases the source. A track created from the source keeps it, but + * receives no more frames. + */ public void dispose() { detachVideoProcessor(); disposeInternal(); diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/desktop/DesktopCapturer.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/desktop/DesktopCapturer.java index 10ecbd66d..9ca0cb763 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/desktop/DesktopCapturer.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/desktop/DesktopCapturer.java @@ -101,6 +101,42 @@ public enum Result { */ public native void setMaxFrameRate(int maxFrameRate); + /** + * Lets the user choose what to capture in the operating system's own + * picker, instead of the source set with {@link #selectSource}. Off by + * default. Set it before {@link #start}; changing it replaces the native + * capturer, which forgets a selected source. + *
+ * On macOS 14 and newer this is ScreenCaptureKit's content sharing + * picker, which offers a screen or one or more windows, for a + * {@link ScreenCapturer} and a {@link WindowCapturer} alike. Under + * Wayland it is the desktop portal's picker for screens and windows. + * Where {@link #isSystemPickerAvailable()} is false, every + * {@link #captureFrame()} reports {@link Result#ERROR_PERMANENT} instead + * of capturing something the user did not choose. + *
+ * {@link #start} shows the picker and returns at once. Until the user
+ * has chosen, {@link #captureFrame()} reports {@link Result#ERROR_TEMPORARY}.
+ * If the user cancels the picker, or stops sharing from the menu bar, it
+ * reports {@link Result#ERROR_PERMANENT}. Show the picker for one capturer
+ * at a time: capturers waiting on it at the same time all receive the
+ * user's choice.
+ *
+ * @param usePicker True to show the system picker when capture starts.
+ *
+ * @throws IllegalStateException If the capturer has been started.
+ */
+ public native void setUseSystemPicker(boolean usePicker);
+
+ /**
+ * Whether this system offers a picker for
+ * {@link #setUseSystemPicker(boolean)}: macOS 14 and newer, and Linux
+ * under Wayland.
+ *
+ * @return True if a system picker is available.
+ */
+ public static native boolean isSystemPickerAvailable();
+
/**
* Starts the desktop capture process with the provided callback. A capturer
* can be started once and keeps its callback until it is disposed.
diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/VideoDesktopSourceTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/VideoDesktopSourceTests.java
index 583a9a75d..c3a94cdf6 100644
--- a/webrtc/src/test/java/dev/onvoid/webrtc/media/video/VideoDesktopSourceTests.java
+++ b/webrtc/src/test/java/dev/onvoid/webrtc/media/video/VideoDesktopSourceTests.java
@@ -251,7 +251,7 @@ void windowFramesFollowWindow() throws Exception {
}
@Test
- void closedWindowStopsCapture() throws Exception {
+ void closedWindowEndsTrack() throws Exception {
assumeTrue(OS.contains("mac"), "covers the macOS window capturers");
assumeFalse(GraphicsEnvironment.isHeadless(), "No display");
@@ -261,22 +261,153 @@ void closedWindowStopsCapture() throws Exception {
assumeTrue(source != null, "The window is not listed under its title");
try (WindowCapture capture = new WindowCapture(source)) {
+ CountDownLatch ended = new CountDownLatch(1);
+
+ capture.track.addTrackEndedListener(track -> ended.countDown());
+
assumeTrue(capture.await(frame -> true, 5000) != null, "The window delivered no frames");
window.closeWindow();
- // Capture stops within a frame or two of the window closing.
- Thread.sleep(1000);
+ assertTrue(ended.await(5, TimeUnit.SECONDS), "The track did not end");
+ assertEquals(MediaStreamTrackState.ENDED, capture.track.getState());
int frames = capture.frames();
- Thread.sleep(1000);
+ Thread.sleep(500);
assertEquals(frames, capture.frames(), "Frames kept coming after the window closed");
}
}
}
+ @Test
+ void endedListenerMayStopAndDispose() throws Exception {
+ assumeTrue(OS.contains("mac"), "covers the macOS window capturers");
+ assumeFalse(GraphicsEnvironment.isHeadless(), "No display");
+
+ try (TestWindowProcess window = TestWindowProcess.open(400, 300)) {
+ DesktopSource source = window.find();
+
+ assumeTrue(source != null, "The window is not listed under its title");
+
+ try (WindowCapture capture = new WindowCapture(source)) {
+ CountDownLatch ended = new CountDownLatch(1);
+
+ // The listener runs on the capture thread, which stop() must
+ // not wait for there (issue #186).
+ capture.track.addTrackEndedListener(track -> {
+ capture.stopAndDisposeSource();
+ ended.countDown();
+ });
+
+ assumeTrue(capture.await(frame -> true, 5000) != null, "The window delivered no frames");
+
+ window.closeWindow();
+
+ assertTrue(ended.await(5, TimeUnit.SECONDS), "The listener did not finish");
+ assertEquals(MediaStreamTrackState.ENDED, capture.track.getState());
+ }
+ }
+ }
+
+ @Test
+ void disposeEndsCapture() throws Exception {
+ assumeTrue(OS.contains("mac"), "covers the macOS window capturers");
+ assumeFalse(GraphicsEnvironment.isHeadless(), "No display");
+
+ try (TestWindowProcess window = TestWindowProcess.open(400, 300)) {
+ DesktopSource desktopSource = window.find();
+
+ assumeTrue(desktopSource != null, "The window is not listed under its title");
+
+ VideoDesktopSource source = new VideoDesktopSource();
+ source.setSourceId(desktopSource.id, true);
+ source.setFrameRate(10);
+
+ VideoTrack track = factory.createVideoTrack("window", source);
+ AtomicInteger frames = new AtomicInteger();
+
+ VideoTrackSink sink = frame -> {
+ frame.release();
+ frames.incrementAndGet();
+ };
+
+ try {
+ track.addSink(sink);
+ source.start();
+
+ for (int i = 0; i < 50 && frames.get() == 0; i++) {
+ Thread.sleep(100);
+ }
+
+ assumeTrue(frames.get() > 0, "The window delivered no frames");
+
+ // The track keeps the source, but disposing it ends its capture.
+ source.dispose();
+
+ Thread.sleep(500);
+
+ int count = frames.get();
+
+ Thread.sleep(1000);
+
+ assertEquals(count, frames.get(), "Frames kept coming after the source was disposed");
+ }
+ finally {
+ track.removeSink(sink);
+ track.dispose();
+ source.dispose();
+ }
+ }
+ }
+
+ @Test
+ void systemPickerAvailability() {
+ boolean expected;
+
+ if (OS.contains("mac")) {
+ expected = Integer.parseInt(System.getProperty("os.version").split("\\.")[0]) >= 14;
+ }
+ else if (OS.contains("linux")) {
+ String session = System.getenv("XDG_SESSION_TYPE");
+
+ expected = session != null && session.startsWith("wayland") && System.getenv("WAYLAND_DISPLAY") != null;
+ }
+ else {
+ expected = false;
+ }
+
+ assertEquals(expected, VideoDesktopSource.isSystemPickerAvailable());
+ }
+
+ @Test
+ void endsWithoutPicker() throws Exception {
+ // With a picker, starting would wait for the user.
+ assumeFalse(VideoDesktopSource.isSystemPickerAvailable(), "This system has a picker");
+
+ VideoDesktopSource source = new VideoDesktopSource();
+ source.setUseSystemPicker(true);
+
+ VideoTrack track = factory.createVideoTrack("picker", source);
+ AtomicInteger ended = new AtomicInteger();
+
+ track.addTrackEndedListener(t -> ended.incrementAndGet());
+
+ try {
+ source.start();
+ // Waits for the capture thread, which has given up by then.
+ source.stop();
+
+ assertEquals(MediaStreamTrackState.ENDED, track.getState());
+ assertEquals(1, ended.get());
+ }
+ finally {
+ track.dispose();
+ source.dispose();
+ }
+ }
+
@Test
void ownWindowCapturedUntilClosed() throws Exception {
assumeTrue(OS.contains("mac"), "covers the macOS window capturers");
@@ -514,6 +645,8 @@ private final class WindowCapture implements AutoCloseable {
private final AtomicReference