Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public class VideoView extends Region implements Consumer<VideoFrame> {

private boolean scheduled;

/** Counts the calls of clear(), to tell the frames that came before one. */
private long generation;

// Confined to the JavaFX Application Thread.

private Pixels shown;
Expand Down Expand Up @@ -99,6 +102,13 @@ public void accept(VideoFrame frame) {
VideoFrameBuffer buffer = frame.buffer;
int width = buffer.getWidth();
int height = buffer.getHeight();
long started;

// Before the wait for convertLock, as the frame came before any clear()
// that happens during it.
synchronized (lock) {
started = generation;
}

synchronized (convertLock) {
Pixels target = back;
Expand All @@ -120,6 +130,10 @@ public void accept(VideoFrame frame) {
target.rotation = frame.rotation;

synchronized (lock) {
if (started != generation) {
return;
}

back = ready;
ready = target;

Expand All @@ -135,11 +149,12 @@ public void accept(VideoFrame frame) {
}

/**
* Removes the picture, leaving the background. Called on the JavaFX
* Application Thread.
* Removes the picture, leaving the background, and drops the frames still
* being converted. Called on the JavaFX Application Thread.
*/
public void clear() {
synchronized (lock) {
generation++;
ready = null;
back = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.ByteBuffer;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -41,8 +42,10 @@
import dev.onvoid.webrtc.client.Signaling;
import dev.onvoid.webrtc.client.SignalingPair;
import dev.onvoid.webrtc.client.WebRtc;
import dev.onvoid.webrtc.media.video.I420Buffer;
import dev.onvoid.webrtc.media.video.NativeI420Buffer;
import dev.onvoid.webrtc.media.video.VideoFrame;
import dev.onvoid.webrtc.media.video.VideoFrameBuffer;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -114,6 +117,72 @@ void showsFrame() throws Exception {
assertNull(onFx(view::snapshot));
}

@Test
void clearDropsFrameInConversion() throws Exception {
VideoView view = onFx(VideoView::new);
CountDownLatch converting = new CountDownLatch(1);
CountDownLatch cleared = new CountDownLatch(1);
VideoFrame held = frame(64, 48, 128, 128);
VideoFrameBuffer buffer = new VideoFrameBuffer() {

@Override
public int getWidth() {
return held.buffer.getWidth();
}

@Override
public int getHeight() {
return held.buffer.getHeight();
}

@Override
public I420Buffer toI420() {
converting.countDown();

try {
cleared.await();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

return held.buffer.toI420();
}

@Override
public VideoFrameBuffer cropAndScale(int cropX, int cropY, int cropWidth, int cropHeight,
int scaleWidth, int scaleHeight) {
throw new UnsupportedOperationException();
}

@Override
public void retain() {
}

@Override
public void release() {
}
};
Thread delivery = new Thread(() -> view.accept(new VideoFrame(buffer, 0)));

delivery.start();
assertTrue(converting.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));

onFx(() -> {
view.clear();
return null;
});
cleared.countDown();
delivery.join(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS));

// Runs after whatever the conversion handed to the JavaFX thread.
assertNull(onFx(view::snapshot));

// The next frame shows as usual.
view.accept(frame(32, 24, 128, 128));
assertEquals(32, (int) presented(view).getWidth());
}

@Test
void keepsAspectRatio() throws Exception {
VideoView view = sized(200, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ void beforeRelease() {
ticker = null;
}
}

super.beforeRelease();
}

/** Queues what is left of the previous format, filled with silence. */
Expand Down
10 changes: 9 additions & 1 deletion webrtc/src/main/java/dev/onvoid/webrtc/client/StatsSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ else if (!(PeerImpl.unwrap(error) instanceof PeerClosedException)) {
}));
};

task.set(peer.rtc.timer.scheduleAtFixedRate(sample, 0, millis, TimeUnit.MILLISECONDS));
ScheduledFuture<?> scheduled = peer.rtc.timer.scheduleAtFixedRate(sample, 0, millis, TimeUnit.MILLISECONDS);

task.set(scheduled);

// The first sample is due at once, and may have found the peer closing
// before it could see the task.
if (!active.get()) {
scheduled.cancel(false);
}

return () -> {
active.set(false);
Expand Down
32 changes: 32 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/client/LocalAudioTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,38 @@ void closingTrackEndsSubscriptions() throws Exception {
assertTrue(changes.isEmpty());
}

@Test
void closingTrackDropsQueuedChange() throws Exception {
// A change still waiting for its event thread when the track closes
// stays unheard, as after closing the subscription.
ManualExecutor control = new ManualExecutor();
ManualExecutor events = new ManualExecutor();
WebRtc runtime = WebRtc.builder()
.audio(AudioMode.HEADLESS)
.errorHandler(errors::add)
.controlPool(control)
.eventExecutor(events)
.build();
BlockingQueue<Boolean> changes = new LinkedBlockingQueue<>();

try {
CustomAudio audio = runtime.media().customAudio("audio");

audio.onVoiceActivity(ONE_SEGMENT, changes::add);
audio.push(speech(48000, 1), PcmFormat.of(48000, 1));
events.awaitQueued(10);

audio.close();
control.runAll();
events.runAll();

assertTrue(changes.isEmpty());
}
finally {
HttpSessionTests.closeStepping(runtime, control);
}
}

@Test
void rejectsNull() {
CustomAudio audio = rtc.media().customAudio("audio");
Expand Down
23 changes: 22 additions & 1 deletion webrtc/src/test/java/dev/onvoid/webrtc/client/ManualTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ final class ManualTimer extends AbstractExecutorService implements ScheduledExec

private boolean shutdown;

private boolean eagerPeriodic;


/**
* Has every periodic task scheduled from now on run once before its
* schedule call returns, as a timer thread may, when its first run is due
* at once.
*/
synchronized void eagerPeriodic() {
eagerPeriodic = true;
}

/**
* Fires every task scheduled with the delay that has not been cancelled.
Expand Down Expand Up @@ -101,7 +112,17 @@ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUni

@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return add(new Task<>(Executors.callable(command, null), unit.toMillis(period), true));
Task<?> task = add(new Task<>(Executors.callable(command, null), unit.toMillis(period), true));

synchronized (this) {
if (!eagerPeriodic) {
return task;
}
}

task.fire();

return task;
}

@Override
Expand Down
25 changes: 25 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/client/PeerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,31 @@ void statsStopOnClose() throws Exception {
}
}

@Test
void statsStopBeforeTaskKnown() throws Exception {
// The first sample is due at once, so it may find the peer closed
// before the task it has to cancel is known.
ManualTimer timer = new ManualTimer();
Duration interval = Duration.ofSeconds(7);

try (WebRtc runtime = WebRtc.builder()
.audio(AudioMode.HEADLESS)
.errorHandler(errors::add)
.timer(timer)
.build()) {
Peer peer = runtime.createPeer(manualOptions());
AtomicInteger calls = new AtomicInteger();

await(peer.closeAsync());

timer.eagerPeriodic();
peer.onStats(interval, stats -> calls.incrementAndGet());

assertEquals(0, timer.scheduled(interval));
assertEquals(0, calls.get());
}
}

@Test
void statsOfClosedPeerFail() throws Exception {
Peer peer = rtc.createPeer(manualOptions());
Expand Down
Loading