|
34 | 34 | import dev.onvoid.webrtc.RTCSessionDescription; |
35 | 35 | import dev.onvoid.webrtc.media.audio.AudioDevice; |
36 | 36 | import dev.onvoid.webrtc.media.video.CustomVideoSource; |
| 37 | +import dev.onvoid.webrtc.media.video.VideoDevice; |
37 | 38 |
|
38 | 39 | import org.junit.jupiter.api.Test; |
39 | 40 |
|
@@ -441,6 +442,73 @@ void switchDeviceAfterClose() throws Exception { |
441 | 442 | await(closed); |
442 | 443 | } |
443 | 444 |
|
| 445 | + @Test |
| 446 | + void closeWhileSwitchingCamera() throws Exception { |
| 447 | + // A camera switch is queued when the runtime begins to close. The |
| 448 | + // runtime waits for it before it frees the factory, and the switch, |
| 449 | + // which finds its track closed, opens no camera. |
| 450 | + ManualExecutor control = new ManualExecutor(); |
| 451 | + WebRtc runtime = runtime(control); |
| 452 | + LocalMediaImpl media = (LocalMediaImpl) runtime.media(); |
| 453 | + LocalCameraImpl camera = camera(runtime); |
| 454 | + |
| 455 | + CompletableFuture<Void> switched = camera.switchCamera(camera("Other camera")); |
| 456 | + |
| 457 | + // Named among what the runtime waits for, beside the track. |
| 458 | + assertEquals(2, media.closeAll().size()); |
| 459 | + |
| 460 | + CompletableFuture<Void> closed = runtime.closeAsync(); |
| 461 | + control.runUntil(closed::isDone, TIMEOUT_SECONDS); |
| 462 | + await(closed); |
| 463 | + |
| 464 | + // The switch ran while the runtime still had its threads, rather than |
| 465 | + // being failed by the runtime's close. |
| 466 | + assertTrackClosed(switched); |
| 467 | + } |
| 468 | + |
| 469 | + @Test |
| 470 | + void switchCameraAfterClose() throws Exception { |
| 471 | + LocalCameraImpl camera = camera(rtc); |
| 472 | + await(camera.closeAsync()); |
| 473 | + |
| 474 | + // Failed at once, without opening a camera over the factory. |
| 475 | + CompletableFuture<Void> switched = camera.switchCamera(camera("Other camera")); |
| 476 | + |
| 477 | + assertTrue(switched.isDone()); |
| 478 | + assertTrackClosed(switched); |
| 479 | + } |
| 480 | + |
| 481 | + @Test |
| 482 | + void switchCameraRejected() throws Exception { |
| 483 | + // The executor refuses the switch, which fails and lets go of its |
| 484 | + // registration, so that the runtime does not wait for it. |
| 485 | + ManualExecutor control = new ManualExecutor(); |
| 486 | + WebRtc runtime = runtime(control); |
| 487 | + LocalMediaImpl media = (LocalMediaImpl) runtime.media(); |
| 488 | + LocalCameraImpl camera = camera(runtime); |
| 489 | + |
| 490 | + try { |
| 491 | + control.rejecting(true); |
| 492 | + CompletableFuture<Void> switched = camera.switchCamera(camera("Other camera")); |
| 493 | + |
| 494 | + ExecutionException e = assertThrows(ExecutionException.class, () -> await(switched)); |
| 495 | + assertInstanceOf(PeerClosedException.class, e.getCause()); |
| 496 | + assertEquals(1, errors.size(), () -> "Errors: " + errors); |
| 497 | + assertInstanceOf(RejectedExecutionException.class, errors.get(0).getCause()); |
| 498 | + } |
| 499 | + finally { |
| 500 | + control.rejecting(false); |
| 501 | + errors.clear(); |
| 502 | + } |
| 503 | + |
| 504 | + // Only the track is left. |
| 505 | + assertEquals(1, media.closeAll().size()); |
| 506 | + |
| 507 | + CompletableFuture<Void> closed = runtime.closeAsync(); |
| 508 | + control.runUntil(closed::isDone, TIMEOUT_SECONDS); |
| 509 | + await(closed); |
| 510 | + } |
| 511 | + |
444 | 512 | @Test |
445 | 513 | void closedPeerNeedsNoTimer() throws Exception { |
446 | 514 | // An operation reaches a peer the closing runtime has closed, after it |
@@ -511,6 +579,38 @@ private WebRtc runtime(ManualExecutor control) { |
511 | 579 | .build(); |
512 | 580 | } |
513 | 581 |
|
| 582 | + /** |
| 583 | + * A camera track over a custom source, so that no camera is needed. Its |
| 584 | + * camera has another user, so that the track's release lets go of its |
| 585 | + * share only, and touches no capture device. |
| 586 | + */ |
| 587 | + private static LocalCameraImpl camera(WebRtc runtime) { |
| 588 | + LocalMediaImpl media = (LocalMediaImpl) runtime.media(); |
| 589 | + VideoDevice device = camera("Test camera"); |
| 590 | + LocalMediaImpl.SharedCamera shared = new LocalMediaImpl.SharedCamera(device.getDescriptor(), |
| 591 | + CameraOptions.builder().device(device).build(), null); |
| 592 | + shared.users = 2; |
| 593 | + |
| 594 | + try (LocalMediaImpl.Creation creation = media.begin()) { |
| 595 | + CustomVideoSource source = new CustomVideoSource(); |
| 596 | + LocalCameraImpl camera = media.register(new LocalCameraImpl(media, |
| 597 | + runtime.factory.createVideoTrack("camera", source), shared)); |
| 598 | + camera.onEnded(source::dispose); |
| 599 | + |
| 600 | + return camera; |
| 601 | + } |
| 602 | + } |
| 603 | + |
| 604 | + private static VideoDevice camera(String name) { |
| 605 | + return new VideoDevice(name, name.toLowerCase().replace(' ', '-')) { }; |
| 606 | + } |
| 607 | + |
| 608 | + private static void assertTrackClosed(CompletableFuture<Void> switched) { |
| 609 | + ExecutionException e = assertThrows(ExecutionException.class, () -> await(switched)); |
| 610 | + assertInstanceOf(MediaException.class, e.getCause()); |
| 611 | + assertEquals("The camera track is closed", e.getCause().getMessage()); |
| 612 | + } |
| 613 | + |
514 | 614 | /** |
515 | 615 | * @return what waiting for the future threw, or null. |
516 | 616 | */ |
|
0 commit comments