|
| 1 | +/* |
| 2 | + * Copyright 2026 Alex Andres |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package dev.onvoid.webrtc.examples; |
| 18 | + |
| 19 | +import java.awt.BorderLayout; |
| 20 | +import java.awt.Color; |
| 21 | +import java.awt.Dimension; |
| 22 | +import java.awt.FlowLayout; |
| 23 | +import java.awt.event.WindowAdapter; |
| 24 | +import java.awt.event.WindowEvent; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.concurrent.ScheduledExecutorService; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | + |
| 29 | +import javax.swing.BorderFactory; |
| 30 | +import javax.swing.JButton; |
| 31 | +import javax.swing.JFrame; |
| 32 | +import javax.swing.JLabel; |
| 33 | +import javax.swing.JPanel; |
| 34 | +import javax.swing.SwingUtilities; |
| 35 | + |
| 36 | +import dev.onvoid.webrtc.client.swing.VideoView; |
| 37 | +import dev.onvoid.webrtc.media.video.VideoFrame; |
| 38 | +import dev.onvoid.webrtc.media.video.desktop.DesktopCapturer; |
| 39 | +import dev.onvoid.webrtc.media.video.desktop.ScreenCapturer; |
| 40 | + |
| 41 | +/** |
| 42 | + * Swing example taking frames from a {@link DesktopCapturer} whose source the |
| 43 | + * user chooses in the operating system's own picker, without a video track. |
| 44 | + * <p> |
| 45 | + * "Choose" starts a {@link ScreenCapturer} set to use the system picker: on |
| 46 | + * macOS 14 and newer, ScreenCaptureKit's content sharing picker, which offers |
| 47 | + * a screen or one or more windows; under Wayland, the desktop portal's |
| 48 | + * picker. The window shows the captured frames and the last capture result. |
| 49 | + * Cancelling the picker, or stopping the sharing from the macOS menu bar, ends |
| 50 | + * the capture with {@link DesktopCapturer.Result#ERROR_PERMANENT}. |
| 51 | + * <p> |
| 52 | + * A capturer is used from one thread only, here a single scheduled thread |
| 53 | + * that starts it, calls {@link DesktopCapturer#captureFrame()} at the frame |
| 54 | + * rate and disposes of it. |
| 55 | + * <p> |
| 56 | + * Run it with: |
| 57 | + * <pre> |
| 58 | + * mvn -pl webrtc-examples exec:java -Dexec.mainClass=dev.onvoid.webrtc.examples.DesktopCapturerPickerExample |
| 59 | + * </pre> |
| 60 | + * |
| 61 | + * @author Alex Andres |
| 62 | + */ |
| 63 | +public class DesktopCapturerPickerExample { |
| 64 | + |
| 65 | + private static final int FRAME_RATE = 30; |
| 66 | + |
| 67 | + private final ScheduledExecutorService captureThread = Executors.newSingleThreadScheduledExecutor(r -> { |
| 68 | + Thread thread = new Thread(r, "desktop-capture"); |
| 69 | + thread.setDaemon(true); |
| 70 | + return thread; |
| 71 | + }); |
| 72 | + |
| 73 | + private final VideoView view = new VideoView(); |
| 74 | + private final JLabel status = new JLabel(); |
| 75 | + private final JButton chooseButton = new JButton("Choose…"); |
| 76 | + private final JButton stopButton = new JButton("Stop"); |
| 77 | + private final JFrame window = new JFrame("Desktop Capturer Picker"); |
| 78 | + |
| 79 | + // Used on the capture thread only. |
| 80 | + private DesktopCapturer capturer; |
| 81 | + private DesktopCapturer.Result lastResult; |
| 82 | + private boolean ended; |
| 83 | + private long frames; |
| 84 | + |
| 85 | + |
| 86 | + public static void main(String[] args) { |
| 87 | + SwingUtilities.invokeLater(() -> new DesktopCapturerPickerExample().show()); |
| 88 | + } |
| 89 | + |
| 90 | + private void show() { |
| 91 | + view.setBackground(Color.DARK_GRAY); |
| 92 | + view.setPreferredSize(new Dimension(960, 540)); |
| 93 | + |
| 94 | + status.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8)); |
| 95 | + |
| 96 | + chooseButton.addActionListener(e -> startCapture()); |
| 97 | + stopButton.addActionListener(e -> stopCapture("Stopped.")); |
| 98 | + stopButton.setEnabled(false); |
| 99 | + |
| 100 | + JPanel controls = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
| 101 | + controls.add(chooseButton); |
| 102 | + controls.add(stopButton); |
| 103 | + |
| 104 | + JPanel bottom = new JPanel(new BorderLayout()); |
| 105 | + bottom.add(controls, BorderLayout.WEST); |
| 106 | + bottom.add(status, BorderLayout.CENTER); |
| 107 | + |
| 108 | + window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 109 | + window.addWindowListener(new WindowAdapter() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public void windowClosed(WindowEvent e) { |
| 113 | + captureThread.execute(() -> disposeCapturer()); |
| 114 | + captureThread.shutdown(); |
| 115 | + } |
| 116 | + }); |
| 117 | + window.getContentPane().add(view, BorderLayout.CENTER); |
| 118 | + window.getContentPane().add(bottom, BorderLayout.SOUTH); |
| 119 | + window.pack(); |
| 120 | + window.setLocationRelativeTo(null); |
| 121 | + window.setVisible(true); |
| 122 | + |
| 123 | + if (DesktopCapturer.isSystemPickerAvailable()) { |
| 124 | + setStatus("Press \"Choose…\" to pick a screen or window."); |
| 125 | + } |
| 126 | + else { |
| 127 | + chooseButton.setEnabled(false); |
| 128 | + setStatus("This system offers no picker for screens and windows."); |
| 129 | + } |
| 130 | + |
| 131 | + // Captures while a capturer is running. |
| 132 | + captureThread.scheduleAtFixedRate(this::captureFrame, 0, 1000 / FRAME_RATE, TimeUnit.MILLISECONDS); |
| 133 | + } |
| 134 | + |
| 135 | + private void startCapture() { |
| 136 | + chooseButton.setEnabled(false); |
| 137 | + stopButton.setEnabled(true); |
| 138 | + view.clear(); |
| 139 | + |
| 140 | + captureThread.execute(() -> { |
| 141 | + disposeCapturer(); |
| 142 | + |
| 143 | + lastResult = null; |
| 144 | + ended = false; |
| 145 | + frames = 0; |
| 146 | + |
| 147 | + capturer = new ScreenCapturer(); |
| 148 | + capturer.setMaxFrameRate(FRAME_RATE); |
| 149 | + // Set before start(). No selectSource() is needed: the user |
| 150 | + // chooses in the picker, which start() shows. |
| 151 | + capturer.setUseSystemPicker(true); |
| 152 | + capturer.start(this::onCaptureResult); |
| 153 | + |
| 154 | + setStatus("Choose a screen or window in the picker."); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + private void stopCapture(String message) { |
| 159 | + captureThread.execute(() -> { |
| 160 | + disposeCapturer(); |
| 161 | + setStatus(message); |
| 162 | + }); |
| 163 | + |
| 164 | + chooseButton.setEnabled(true); |
| 165 | + stopButton.setEnabled(false); |
| 166 | + } |
| 167 | + |
| 168 | + private void captureFrame() { |
| 169 | + if (capturer == null) { |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + try { |
| 174 | + capturer.captureFrame(); |
| 175 | + } |
| 176 | + catch (RuntimeException e) { |
| 177 | + ended = true; |
| 178 | + setStatus("Capture failed: " + e.getMessage()); |
| 179 | + } |
| 180 | + |
| 181 | + if (ended) { |
| 182 | + // Disposed of here, not in the callback that reported the end. |
| 183 | + disposeCapturer(); |
| 184 | + |
| 185 | + SwingUtilities.invokeLater(() -> { |
| 186 | + chooseButton.setEnabled(true); |
| 187 | + stopButton.setEnabled(false); |
| 188 | + }); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Called on the capture thread from within {@link DesktopCapturer#captureFrame()}. |
| 194 | + */ |
| 195 | + private void onCaptureResult(DesktopCapturer.Result result, VideoFrame frame) { |
| 196 | + try { |
| 197 | + switch (result) { |
| 198 | + case SUCCESS: |
| 199 | + if (frame != null) { |
| 200 | + view.accept(frame); |
| 201 | + frames++; |
| 202 | + |
| 203 | + setStatus(String.format("Capturing: %d frames, %dx%d", frames, |
| 204 | + frame.buffer.getWidth(), frame.buffer.getHeight())); |
| 205 | + } |
| 206 | + break; |
| 207 | + |
| 208 | + case ERROR_TEMPORARY: |
| 209 | + // No frame yet, while the picker waits for the user. |
| 210 | + if (lastResult != DesktopCapturer.Result.ERROR_TEMPORARY && frames == 0) { |
| 211 | + setStatus("Waiting for a choice in the picker…"); |
| 212 | + } |
| 213 | + break; |
| 214 | + |
| 215 | + default: |
| 216 | + ended = true; |
| 217 | + setStatus(frames == 0 |
| 218 | + ? "The picker was cancelled." |
| 219 | + : "Sharing stopped after " + frames + " frames."); |
| 220 | + break; |
| 221 | + } |
| 222 | + |
| 223 | + lastResult = result; |
| 224 | + } |
| 225 | + finally { |
| 226 | + if (frame != null) { |
| 227 | + frame.release(); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private void disposeCapturer() { |
| 233 | + if (capturer != null) { |
| 234 | + capturer.dispose(); |
| 235 | + capturer = null; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + private void setStatus(String text) { |
| 240 | + SwingUtilities.invokeLater(() -> status.setText(text)); |
| 241 | + } |
| 242 | +} |
0 commit comments