|
24 | 24 | import static org.junit.jupiter.api.Assumptions.assumeTrue; |
25 | 25 |
|
26 | 26 | import dev.onvoid.webrtc.TestBase; |
| 27 | +import dev.onvoid.webrtc.media.MediaStreamTrackState; |
27 | 28 | import dev.onvoid.webrtc.media.video.desktop.DesktopSource; |
28 | 29 | import dev.onvoid.webrtc.media.video.desktop.ScreenCapturer; |
29 | 30 |
|
|
37 | 38 | import java.util.List; |
38 | 39 | import java.util.Locale; |
39 | 40 | import java.util.concurrent.CompletableFuture; |
| 41 | +import java.util.concurrent.CountDownLatch; |
40 | 42 | import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.concurrent.atomic.AtomicInteger; |
| 44 | +import java.util.concurrent.atomic.AtomicReference; |
41 | 45 |
|
42 | 46 | import org.junit.jupiter.api.Test; |
43 | 47 | import org.junit.jupiter.api.parallel.Execution; |
@@ -199,6 +203,154 @@ void screenFramesFollowScreen() throws Exception { |
199 | 203 | + String.format(Locale.ROOT, "%.1f to %.1f", min, max)); |
200 | 204 | } |
201 | 205 |
|
| 206 | + @Test |
| 207 | + void retainedFrameKeepsPixels() throws Exception { |
| 208 | + assumeFalse(isWaylandSession(), "Starting a capture asks the user what to share"); |
| 209 | + assumeFalse(GraphicsEnvironment.isHeadless(), "No display"); |
| 210 | + |
| 211 | + DesktopSource screen = firstScreen(); |
| 212 | + |
| 213 | + // A window of our own, to change the screen while a frame is retained. |
| 214 | + Frame window = new Frame(); |
| 215 | + Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment() |
| 216 | + .getDefaultScreenDevice().getDefaultConfiguration().getBounds(); |
| 217 | + |
| 218 | + EventQueue.invokeAndWait(() -> { |
| 219 | + window.setUndecorated(true); |
| 220 | + window.setAlwaysOnTop(true); |
| 221 | + window.setBackground(Color.BLACK); |
| 222 | + window.setBounds(bounds.x + bounds.width / 10, bounds.y + bounds.height / 10, |
| 223 | + bounds.width * 8 / 10, bounds.height * 8 / 10); |
| 224 | + window.setVisible(true); |
| 225 | + window.toFront(); |
| 226 | + }); |
| 227 | + |
| 228 | + // No maximum size: only frames that are not scaled were shared. |
| 229 | + VideoDesktopSource source = new VideoDesktopSource(); |
| 230 | + source.setSourceId(screen.id, false); |
| 231 | + source.setFrameRate(10); |
| 232 | + |
| 233 | + VideoTrack track = factory.createVideoTrack("screen", source); |
| 234 | + |
| 235 | + AtomicReference<VideoFrame> retained = new AtomicReference<>(); |
| 236 | + AtomicReference<ByteBuffer> original = new AtomicReference<>(); |
| 237 | + CountDownLatch firstFrame = new CountDownLatch(1); |
| 238 | + CountDownLatch changedFrame = new CountDownLatch(1); |
| 239 | + |
| 240 | + VideoProcessor processor = new VideoProcessor() { |
| 241 | + @Override |
| 242 | + public void setSink(VideoTrackSink sink) { |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public void onFrameCaptured(VideoFrame frame) { |
| 247 | + ByteBuffer pixels = ((NativeI420Buffer) frame.buffer).getDataY(); |
| 248 | + |
| 249 | + if (retained.get() == null) { |
| 250 | + frame.retain(); |
| 251 | + |
| 252 | + original.set(copyOf(pixels)); |
| 253 | + retained.set(frame); |
| 254 | + firstFrame.countDown(); |
| 255 | + } |
| 256 | + else if (!original.get().equals(pixels)) { |
| 257 | + changedFrame.countDown(); |
| 258 | + } |
| 259 | + } |
| 260 | + }; |
| 261 | + |
| 262 | + // Without a sink the source drops its frames. |
| 263 | + VideoTrackSink sink = VideoFrame::release; |
| 264 | + |
| 265 | + try { |
| 266 | + source.setVideoProcessor(processor); |
| 267 | + track.addSink(sink); |
| 268 | + source.start(); |
| 269 | + |
| 270 | + assumeTrue(firstFrame.await(10, TimeUnit.SECONDS), "The screen delivered no frames"); |
| 271 | + |
| 272 | + EventQueue.invokeAndWait(() -> { |
| 273 | + window.setBackground(Color.WHITE); |
| 274 | + window.repaint(); |
| 275 | + }); |
| 276 | + |
| 277 | + assumeTrue(changedFrame.await(10, TimeUnit.SECONDS), "The screen did not change"); |
| 278 | + |
| 279 | + source.stop(); |
| 280 | + |
| 281 | + assertEquals(original.get(), ((NativeI420Buffer) retained.get().buffer).getDataY(), |
| 282 | + "The next capture changed the pixels of a retained frame"); |
| 283 | + } |
| 284 | + finally { |
| 285 | + source.stop(); |
| 286 | + track.removeSink(sink); |
| 287 | + track.dispose(); |
| 288 | + source.dispose(); |
| 289 | + |
| 290 | + if (retained.get() != null) { |
| 291 | + retained.get().release(); |
| 292 | + } |
| 293 | + |
| 294 | + EventQueue.invokeAndWait(window::dispose); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + void missingBackendEndsTrack() { |
| 300 | + assumeFalse(isWaylandSession(), "Starting a capture asks the user what to share"); |
| 301 | + assumeTrue(hasNoBackend(), "A capture backend is available"); |
| 302 | + |
| 303 | + VideoDesktopSource source = new VideoDesktopSource(); |
| 304 | + VideoTrack track = factory.createVideoTrack("screen", source); |
| 305 | + |
| 306 | + AtomicInteger ended = new AtomicInteger(); |
| 307 | + |
| 308 | + track.addTrackEndedListener(t -> ended.incrementAndGet()); |
| 309 | + |
| 310 | + try { |
| 311 | + source.start(); |
| 312 | + // Waits for the capture thread, which has given up by then. |
| 313 | + source.stop(); |
| 314 | + |
| 315 | + assertEquals(MediaStreamTrackState.ENDED, track.getState()); |
| 316 | + assertEquals(1, ended.get()); |
| 317 | + } |
| 318 | + finally { |
| 319 | + track.dispose(); |
| 320 | + source.dispose(); |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + private static ByteBuffer copyOf(ByteBuffer buffer) { |
| 325 | + ByteBuffer copy = ByteBuffer.allocate(buffer.remaining()); |
| 326 | + |
| 327 | + copy.put(buffer.duplicate()); |
| 328 | + copy.flip(); |
| 329 | + |
| 330 | + return copy; |
| 331 | + } |
| 332 | + |
| 333 | + private static boolean isWaylandSession() { |
| 334 | + return "wayland".equals(System.getenv("XDG_SESSION_TYPE")); |
| 335 | + } |
| 336 | + |
| 337 | + private static boolean hasNoBackend() { |
| 338 | + ScreenCapturer capturer = new ScreenCapturer(); |
| 339 | + |
| 340 | + try { |
| 341 | + capturer.getDesktopSources(); |
| 342 | + |
| 343 | + return false; |
| 344 | + } |
| 345 | + catch (Error | RuntimeException e) { |
| 346 | + // Without a display, as on headless Linux, listing fails. |
| 347 | + return true; |
| 348 | + } |
| 349 | + finally { |
| 350 | + capturer.dispose(); |
| 351 | + } |
| 352 | + } |
| 353 | + |
202 | 354 | private static double meanLuma(NativeI420Buffer buffer) { |
203 | 355 | ByteBuffer data = buffer.getDataY(); |
204 | 356 | int stride = buffer.getStrideY(); |
|
0 commit comments