Skip to content

Commit 67bee6f

Browse files
authored
fix: register a camera switch with the runtime and reject it on a closed track (#447)
1 parent 60dc11b commit 67bee6f

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

‎webrtc/src/main/java/dev/onvoid/webrtc/client/LocalCameraImpl.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ public CompletableFuture<Void> switchCamera(VideoDevice device) {
6161
requireNonNull(device, "device");
6262

6363
CompletableFuture<Void> switched = new CompletableFuture<>();
64+
65+
if (isEnded()) {
66+
switched.completeExceptionally(new MediaException("The camera track is closed"));
67+
return switched;
68+
}
69+
70+
// Registered like the creation of a track, as the switch opens a
71+
// camera and uses the factory: a runtime that begins to close either
72+
// waits for it before it frees the factory, or is seen closing here.
73+
LocalMediaImpl.Creation operation;
74+
75+
try {
76+
operation = media.begin();
77+
}
78+
catch (PeerClosedException e) {
79+
switched.completeExceptionally(e);
80+
return switched;
81+
}
82+
6483
media.rtc.track(switched, null);
6584

6685
// Opening a camera can take a moment, so it happens off the caller's thread.
@@ -73,6 +92,12 @@ public CompletableFuture<Void> switchCamera(VideoDevice device) {
7392
switched.completeExceptionally(e instanceof WebRtcException
7493
? e : new MediaException("Switching to the camera " + device.getName() + " failed", e));
7594
}
95+
finally {
96+
operation.close();
97+
}
98+
}, () -> {
99+
operation.close();
100+
switched.completeExceptionally(new PeerClosedException("The runtime is closed"));
76101
});
77102

78103
// Completed on the event executor, so that the application's
@@ -84,6 +109,11 @@ private void switchTo(VideoDevice device) {
84109
LocalMediaImpl.SharedCamera current;
85110

86111
synchronized (this) {
112+
// Closed while the switch was queued, so no camera is opened.
113+
if (!isLive()) {
114+
throw new MediaException("The camera track is closed");
115+
}
116+
87117
current = camera;
88118
}
89119

‎webrtc/src/test/java/dev/onvoid/webrtc/client/RuntimeCloseTests.java‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import dev.onvoid.webrtc.RTCSessionDescription;
3535
import dev.onvoid.webrtc.media.audio.AudioDevice;
3636
import dev.onvoid.webrtc.media.video.CustomVideoSource;
37+
import dev.onvoid.webrtc.media.video.VideoDevice;
3738

3839
import org.junit.jupiter.api.Test;
3940

@@ -441,6 +442,73 @@ void switchDeviceAfterClose() throws Exception {
441442
await(closed);
442443
}
443444

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+
444512
@Test
445513
void closedPeerNeedsNoTimer() throws Exception {
446514
// An operation reaches a peer the closing runtime has closed, after it
@@ -511,6 +579,38 @@ private WebRtc runtime(ManualExecutor control) {
511579
.build();
512580
}
513581

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+
514614
/**
515615
* @return what waiting for the future threw, or null.
516616
*/

0 commit comments

Comments
 (0)