Skip to content

Commit f68ee04

Browse files
committed
Let a local track's close be waited for
LocalTrack.closeAsync completes once the native track is released, after which a source it was made over may be closed.
1 parent 1da3b10 commit f68ee04

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package dev.onvoid.webrtc.client;
1818

19+
import java.util.concurrent.CompletableFuture;
20+
1921
/**
2022
* A track this side sends, made by {@link WebRtc#media()}. One track may be sent
2123
* on any number of peers.
@@ -60,6 +62,16 @@ public interface LocalTrack extends AutoCloseable {
6062
*/
6163
boolean isEnded();
6264

65+
/**
66+
* Stops sending the track on every peer, without renegotiating, and stops
67+
* its capture.
68+
*
69+
* @return a future that completes once the native track is released, after
70+
* which a source the track was made over, with {@link
71+
* LocalMedia#videoTrack} or {@link LocalMedia#audioTrack}, may be closed.
72+
*/
73+
CompletableFuture<Void> closeAsync();
74+
6375
/**
6476
* Stops sending the track on every peer, without renegotiating, and stops
6577
* its capture. Never blocks, may be called more than once.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public synchronized boolean isEnded() {
124124
return ended;
125125
}
126126

127+
@Override
128+
public CompletableFuture<Void> closeAsync() {
129+
return closeInternal();
130+
}
131+
127132
@Override
128133
public void close() {
129134
closeInternal();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ void tracksOverOwnSources() throws Exception {
329329
VideoFrame frame = new VideoFrame(NativeI420Buffer.allocate(160, 120), System.nanoTime());
330330
videoSource.pushFrame(frame);
331331
frame.release();
332+
333+
// Once released, the source may go before the runtime does.
334+
await(audio.closeAsync());
335+
assertTrue(audio.isEnded());
332336
}
333337
finally {
334338
feeder.shutdownNow();

0 commit comments

Comments
 (0)