From 1e1cafac12dc4967585b36d18e50f8ac8f7a8ecf Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 5 Nov 2026 14:11:33 +0100 Subject: [PATCH 1/2] docs: state the DTMF limits WebRTC enforces --- docs/guide/audio/dtmf-sender.md | 4 ++-- .../java/dev/onvoid/webrtc/RTCDtmfSender.java | 11 +++++++---- .../dev/onvoid/webrtc/RTCDtmfSenderTests.java | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/docs/guide/audio/dtmf-sender.md b/docs/guide/audio/dtmf-sender.md index a8ab3883a..9bcc91711 100644 --- a/docs/guide/audio/dtmf-sender.md +++ b/docs/guide/audio/dtmf-sender.md @@ -86,8 +86,8 @@ Unrecognized characters are ignored. ### Duration and Inter-Tone Gap Constraints The duration and inter-tone gap parameters have the following constraints: -- Duration must be between 70ms and 6000ms (default: 100ms) -- Inter-tone gap must be at least 50ms (default: 50ms) +- Duration must be between 40ms and 6000ms (default: 100ms) +- Inter-tone gap must be at least 30ms (default: 50ms) If these constraints are not met, the `insertDtmf` method will return `false`. diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCDtmfSender.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCDtmfSender.java index 39bd49393..e18a759d8 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/RTCDtmfSender.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCDtmfSender.java @@ -83,10 +83,13 @@ public class RTCDtmfSender extends DisposableNativeObject { *

* The duration parameter indicates the duration in milliseconds to use for each * character passed in the tones parameter. The duration cannot be more than 6000 - * milliseconds or less than 70 milliseconds. The default duration is 100 milliseconds. + * milliseconds or less than 40 milliseconds. The default duration is 100 milliseconds. *

* The interToneGap parameter indicates the gap between tones in milliseconds. - * The interToneGap cannot be less than 50 milliseconds. The default value is 50 milliseconds. + * The interToneGap cannot be less than 30 milliseconds. The default value is 50 milliseconds. + *

+ * If the duration or the interToneGap is outside these limits, this method has + * no effect and returns false. * * @param tones String containing the DTMF tones to be transmitted. * Valid characters are 0-9, A-D, #, *, and , (comma). @@ -117,7 +120,7 @@ public class RTCDtmfSender extends DisposableNativeObject { * future calls to {@link #insertDtmf}. The duration is the length of time, in milliseconds, * that each DTMF tone is played. *

- * The default value is 100 milliseconds. Valid values are between 70 and 6000 milliseconds. + * The default value is 100 milliseconds. Valid values are between 40 and 6000 milliseconds. * * @return The duration in milliseconds for each tone. */ @@ -130,7 +133,7 @@ public class RTCDtmfSender extends DisposableNativeObject { * future calls to {@link #insertDtmf}. The interToneGap is the length of time, in milliseconds, * between the end of one DTMF tone and the start of the next. *

- * The default value is 50 milliseconds. Valid values must be at least 50 milliseconds but should + * The default value is 50 milliseconds. Valid values must be at least 30 milliseconds but should * be as short as possible. * * @return The gap between tones in milliseconds. diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/RTCDtmfSenderTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/RTCDtmfSenderTests.java index 054c4cc8a..016776afa 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/RTCDtmfSenderTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/RTCDtmfSenderTests.java @@ -214,6 +214,24 @@ void insertDtmf() { assertFalse(dtmfSender.insertDtmf("123", 100, 20)); // InterToneGap too short } + @Test + void insertDtmfLimits() { + assertTrue(dtmfSender.insertDtmf("1", 40, 30)); + assertEquals(40, dtmfSender.duration()); + assertEquals(30, dtmfSender.interToneGap()); + + assertTrue(dtmfSender.insertDtmf("1", 6000, 30)); + assertEquals(6000, dtmfSender.duration()); + + assertFalse(dtmfSender.insertDtmf("1", 39, 30)); + assertFalse(dtmfSender.insertDtmf("1", 6001, 30)); + assertFalse(dtmfSender.insertDtmf("1", 100, 29)); + + // A rejected call leaves the values of the last accepted one. + assertEquals(6000, dtmfSender.duration()); + assertEquals(30, dtmfSender.interToneGap()); + } + @Test void getTones() throws InterruptedException { insertTones(Arrays.asList("1", "2", "3"), 100, 70); From 1d23b479d237341bf40c513b18dde46368258312 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 5 Nov 2026 14:11:33 +0100 Subject: [PATCH 2/2] feat: release native objects with try-with-resources --- docs/guide/data/data-channels.md | 2 + docs/guide/get-started.md | 10 ++++ docs/tools/video/video-capturer.md | 8 +-- .../dev/onvoid/webrtc/RTCDataChannel.java | 6 ++ .../internal/DisposableNativeObject.java | 17 +++++- .../dev/onvoid/webrtc/media/SyncClock.java | 5 +- .../webrtc/media/video/VideoCapture.java | 18 +++--- .../dev/onvoid/webrtc/NativeDisposeTests.java | 60 +++++++++++++++++++ 8 files changed, 110 insertions(+), 16 deletions(-) diff --git a/docs/guide/data/data-channels.md b/docs/guide/data/data-channels.md index 614c5972f..ca01903c6 100644 --- a/docs/guide/data/data-channels.md +++ b/docs/guide/data/data-channels.md @@ -296,6 +296,8 @@ dataChannel.close(); dataChannel.dispose(); ``` +Unlike other disposable objects, a data channel is not disposed by `close()`, which only closes the channel. A try-with-resources statement therefore does not release it: call `dispose()` yourself. + ## Best Practices 1. **Error Handling**: Always wrap `send` calls in try-catch blocks as they can throw exceptions if the buffer is full or the channel is not in the `OPEN` state. diff --git a/docs/guide/get-started.md b/docs/guide/get-started.md index 45d3afb0c..026e7f7cd 100644 --- a/docs/guide/get-started.md +++ b/docs/guide/get-started.md @@ -483,6 +483,16 @@ A track may be disposed, and a listener removed, from within one of its listener `dispose()` and `close()` may be called from any thread and more than once: only the first call frees anything, later ones do nothing. Once an object is disposed, its methods throw an `IllegalStateException` (a closed connection still reports its state). A method that another thread is running at that moment keeps the native object until it returns, and what the dispose frees is freed then. +Apart from the media sources, which only have `dispose()`, objects with a `dispose()` method also have a `close()` that does the same. A try-with-resources statement therefore releases them, also when an exception leaves the block: + +```java +try (AudioConverter converter = new AudioConverter(48000, 1, 16000, 1)) { + converter.convert(input, output); +} +``` + +`RTCDataChannel` is the exception: its `close()` closes the channel, which then still has to be disposed. + #### Finding Objects That Were Never Disposed An object that becomes unreachable without being disposed or closed leaks its native object. To find such objects, start the JVM with `-Dwebrtc.java.leak.detection=true`: each one is then reported, with its class, once the garbage collector finds it unreachable. Add `-Dwebrtc.java.leak.trace=true` to have the report tell where the object was created, at the cost of a stack trace per object. diff --git a/docs/tools/video/video-capturer.md b/docs/tools/video/video-capturer.md index 5c721af24..86474798f 100644 --- a/docs/tools/video/video-capturer.md +++ b/docs/tools/video/video-capturer.md @@ -8,7 +8,7 @@ API: `dev.onvoid.webrtc.media.video.VideoCapture` - Purpose: Control a physical or virtual video input device and deliver frames to a sink. - Typical workflow: set device ➜ set capability ➜ set sink ➜ start ➜ stop ➜ dispose. -- Resource management: Always call `dispose()` to release native resources. +- Resource management: Always call `dispose()`, or use try-with-resources, to release native resources. - Threading: Methods are not guaranteed to be thread‑safe; synchronize externally if accessed from multiple threads. Key methods: @@ -17,7 +17,7 @@ Key methods: - `void setVideoSink(VideoTrackSink sink)` – register/replace the sink that will receive frames. - `void start()` – begin asynchronous capture and frame delivery. - `void stop()` – stop capture (idempotent). -- `void dispose()` – release native resources; implicitly stops if running. +- `void dispose()` – release native resources; implicitly stops if running. `close()` does the same. Related types: - `VideoDevice` – describes a capturable device (enumerate via your platform/device utilities). @@ -92,11 +92,11 @@ If you need pixel format conversion, see `VideoBufferConverter`. - Constructor: Initializes native resources; failures may surface as runtime exceptions from native code. - `IllegalStateException`: Thrown by `start()` if no device was selected with `setVideoCaptureDevice`. -- `NullPointerException`: Thrown if methods are invoked after disposal. +- `IllegalStateException`: Thrown if methods are invoked after disposal; another `dispose()` does nothing. - Other native failures (e.g. the selected device is not available) surface as an `Error` carrying the native message. - Restart: after `stop()`, `start()` can be called again; the sink set earlier stays registered. - Idempotency: `start()` and `stop()` are intended to be safe to call multiple times (implementation‑dependent no‑op if already in that state). -- Always call `dispose()` in a `finally` block to avoid leaking native resources. +- Always call `dispose()` in a `finally` block, or use try-with-resources, to avoid leaking native resources. ## Tips diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/RTCDataChannel.java b/webrtc/src/main/java/dev/onvoid/webrtc/RTCDataChannel.java index e3a39fea7..143f18cd3 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/RTCDataChannel.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/RTCDataChannel.java @@ -165,7 +165,13 @@ private RTCDataChannel() { /** * Closes this RTCDataChannel. It may be called regardless of whether the * RTCDataChannel was created by this peer or the remote peer. + *

+ * This does not dispose the channel, unlike the close method of other + * disposable objects: its observer still hears of the state change, and + * its state can still be read. Neither does a try-with-resources statement + * then, so {@link #dispose()} remains to be called. */ + @Override public native void close(); @Override diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/internal/DisposableNativeObject.java b/webrtc/src/main/java/dev/onvoid/webrtc/internal/DisposableNativeObject.java index 335608b5e..28b179134 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/internal/DisposableNativeObject.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/internal/DisposableNativeObject.java @@ -18,11 +18,11 @@ /** * Allows disposing the native object bound to the Java object and free native - * resources. + * resources, also through a try-with-resources statement. * * @author Alex Andres */ -public abstract class DisposableNativeObject extends NativeObject { +public abstract class DisposableNativeObject extends NativeObject implements AutoCloseable { /** * Invoke native object destructors and free native resources. @@ -35,4 +35,17 @@ public abstract class DisposableNativeObject extends NativeObject { */ public abstract void dispose(); + /** + * Disposes this object, see {@link #dispose()}, which lets a + * try-with-resources statement release it, also when an exception leaves + * the block. + *

+ * {@link dev.onvoid.webrtc.RTCDataChannel} is the one exception: closing it + * closes the channel, which then still has to be disposed. + */ + @Override + public void close() { + dispose(); + } + } diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java index c0167644f..23bc8ef43 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java @@ -16,8 +16,8 @@ package dev.onvoid.webrtc.media; +import dev.onvoid.webrtc.internal.DisposableNativeObject; import dev.onvoid.webrtc.internal.NativeLoader; -import dev.onvoid.webrtc.internal.NativeObject; /** * Synchronized Clock for A/V timing. Provides timing functionality for @@ -25,7 +25,7 @@ * * @author Alex Andres */ -public class SyncClock extends NativeObject { +public class SyncClock extends DisposableNativeObject { static { try { @@ -97,6 +97,7 @@ public SyncClock() { * so the clock may be disposed while such a source is still in use: the * source keeps the native clock until it is disposed itself. */ + @Override public native void dispose(); /** diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoCapture.java b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoCapture.java index c373ebb9b..47adc0c21 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoCapture.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoCapture.java @@ -16,11 +16,11 @@ package dev.onvoid.webrtc.media.video; -import dev.onvoid.webrtc.internal.NativeObject; +import dev.onvoid.webrtc.internal.DisposableNativeObject; /** * A class representing a video capture device that can be controlled to capture video. - * Extends NativeObject to interact with native code implementations. + * Extends DisposableNativeObject to interact with native code implementations. *

  * VideoCapture capture = new VideoCapture();
  * capture.setVideoCaptureDevice(device);
@@ -32,12 +32,12 @@
  * capture.dispose();
  * 
* Notes: - *
  • Always call dispose() to release native resources.
  • + *
  • Always call dispose(), or use try-with-resources, to release native resources.
  • *
  • Methods are not guaranteed to be thread-safe unless externally synchronized.
  • * * @author Alex Andres */ -public class VideoCapture extends NativeObject { +public class VideoCapture extends DisposableNativeObject { /** * Constructs a new VideoCapture and initializes underlying native resources. @@ -125,13 +125,15 @@ public VideoCapture() { *

    * Lifecycle: *

  • Implicitly stops capture if currently running.
  • - *
  • After disposal, any further method call, including another call to - * {@code dispose()} itself, throws {@link NullPointerException}.
  • + *
  • After disposal, any further method call throws an + * {@link IllegalStateException}; another call to {@code dispose()} + * itself does nothing.
  • *

    * Best Practice: - * Always invoke in a finally block or use a higher-level resource management - * construct to avoid native leaks. + * Always invoke in a finally block or use try-with-resources to avoid + * native leaks. */ + @Override public native void dispose(); /** diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/NativeDisposeTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/NativeDisposeTests.java index de81d6ac6..09bd6dd9b 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/NativeDisposeTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/NativeDisposeTests.java @@ -19,10 +19,12 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import dev.onvoid.webrtc.media.MediaStreamTrackMuteListener; +import dev.onvoid.webrtc.media.SyncClock; import dev.onvoid.webrtc.media.audio.AudioConverter; import dev.onvoid.webrtc.media.audio.AudioOptions; import dev.onvoid.webrtc.media.audio.AudioTrack; @@ -30,6 +32,7 @@ import dev.onvoid.webrtc.media.audio.AudioDeviceModule; import dev.onvoid.webrtc.media.audio.AudioLayer; import dev.onvoid.webrtc.media.video.CustomVideoSource; +import dev.onvoid.webrtc.media.video.VideoCapture; import dev.onvoid.webrtc.media.video.VideoTrack; import java.util.AbstractList; @@ -122,6 +125,63 @@ void disposeTwiceDoesNothing() { assertDisposed("AudioConverter is disposed", () -> converter.convert(new byte[960], new byte[960])); } + @Test + void tryWithResourcesDisposes() { + AudioConverter converter = new AudioConverter(48000, 1, 48000, 1); + SyncClock clock = new SyncClock(); + VideoCapture capture = new VideoCapture(); + + try (converter; clock; capture) { + assertNotNull(converter); + } + + assertDisposed("AudioConverter is disposed", () -> converter.convert(new byte[960], new byte[960])); + assertDisposed("SyncClock is disposed", clock::getTimestampUs); + assertDisposed("VideoCapture is disposed", capture::start); + } + + @Test + void tryWithResourcesDisposesOnException() { + SyncClock clock = new SyncClock(); + RuntimeException failure = new RuntimeException(); + + RuntimeException thrown = assertThrows(RuntimeException.class, () -> { + try (clock) { + throw failure; + } + }); + + assertSame(failure, thrown); + assertEquals(0, thrown.getSuppressed().length); + assertDisposed("SyncClock is disposed", clock::getTimestampUs); + } + + @Test + void closeAfterDisposeDoesNothing() { + SyncClock clock = new SyncClock(); + + clock.dispose(); + + assertDoesNotThrow(clock::close); + } + + @Test + void closeKeepsDataChannel() { + RTCPeerConnection peerConnection = factory.createPeerConnection(new RTCConfiguration(), observer); + RTCDataChannel channel = peerConnection.createDataChannel("data", new RTCDataChannelInit()); + + // Closes the channel only, the native object stays until it is disposed. + channel.close(); + + assertEquals("data", channel.getLabel()); + + channel.dispose(); + + assertDisposed("RTCDataChannel is disposed", channel::getLabel); + + peerConnection.close(); + } + @Test void disposeFactoryTwice() { AudioDeviceModule audioModule = new AudioDeviceModule(AudioLayer.kDummyAudio);