1919import static java .util .Objects .requireNonNull ;
2020
2121import java .util .concurrent .CompletableFuture ;
22+ import java .util .concurrent .atomic .AtomicLong ;
2223
2324import dev .onvoid .webrtc .client .RemoteTrack ;
2425import dev .onvoid .webrtc .client .Sender ;
26+ import dev .onvoid .webrtc .internal .NativeApi ;
2527
2628/**
2729 * Lets a native extension, such as the media module's recorder, observe the
3234 * them on the control executor of their peer. What this hands out keeps to
3335 * that: the handle is taken there, and key frames are asked for there, once
3436 * the sender or receiver is gone not at all.
37+ * <p>
38+ * The futures of {@link #of(Sender)} and {@link #of(RemoteTrack)} complete
39+ * in any case, failed once the peer or its runtime is closed. A caller that
40+ * stops waiting cancels the future and closes a source that completes it all
41+ * the same, see {@link Source}.
3542 */
3643public final class EncodedFrames {
3744
@@ -55,31 +62,54 @@ public interface Access {
5562 }
5663
5764 /**
58- * Where a sender's or receiver's encoded frames come from.
65+ * Where a sender's or receiver's encoded frames come from. It owns the
66+ * reference of its handle until {@link #take()} hands it over; closing it
67+ * before drops the reference, closing it after does nothing. Whoever gets
68+ * a source does one or the other, so that no reference is left behind.
5969 */
60- public static final class Source {
70+ public static final class Source implements AutoCloseable {
6171
62- private final long frames ;
72+ /** The handle, or 0 once taken or released. */
73+ private final AtomicLong frames ;
6374
6475 private final Runnable keyFrameRequest ;
6576
6677
6778 /**
6879 * @param frames the handle of {@code NativeApi.encodedFramesOf},
69- * whose reference the receiver of this source
70- * drops.
80+ * whose reference this source takes over.
7181 * @param keyFrameRequest asks for a key frame, from any thread.
7282 */
7383 public Source (long frames , Runnable keyFrameRequest ) {
74- this .frames = frames ;
84+ this .frames = new AtomicLong ( frames ) ;
7585 this .keyFrameRequest = requireNonNull (keyFrameRequest , "keyFrameRequest" );
7686 }
7787
7888 /**
79- * @return the handle of the encoded frames, with a reference to drop.
89+ * Hands over the handle together with its reference, which the caller
90+ * drops from then on.
91+ *
92+ * @return the handle of the encoded frames.
93+ *
94+ * @throws IllegalStateException if it was taken or released before.
95+ */
96+ public long take () {
97+ long handle = frames .getAndSet (0 );
98+
99+ if (handle == 0 ) {
100+ throw new IllegalStateException ("The encoded frames were taken or released" );
101+ }
102+
103+ return handle ;
104+ }
105+
106+ /**
107+ * Drops the reference of the handle, unless {@link #take()} handed it
108+ * over. Closing it again does nothing.
80109 */
81- public long frames () {
82- return frames ;
110+ @ Override
111+ public void close () {
112+ NativeApi .releaseEncodedFrames (frames .getAndSet (0 ));
83113 }
84114
85115 /**
0 commit comments