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
4 changes: 2 additions & 2 deletions docs/guide/audio/dtmf-sender.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
2 changes: 2 additions & 0 deletions docs/guide/data/data-channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions docs/guide/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions docs/tools/video/video-capturer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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).
Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCDataChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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
Expand Down
11 changes: 7 additions & 4 deletions webrtc/src/main/java/dev/onvoid/webrtc/RTCDtmfSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ public class RTCDtmfSender extends DisposableNativeObject {
* <p>
* 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.
* <p>
* 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.
* <p>
* 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).
Expand Down Expand Up @@ -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.
* <p>
* 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.
*/
Expand All @@ -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.
* <p>
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
* <p>
* {@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();
}

}
5 changes: 3 additions & 2 deletions webrtc/src/main/java/dev/onvoid/webrtc/media/SyncClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

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
* audio and video synchronization.
*
* @author Alex Andres
*/
public class SyncClock extends NativeObject {
public class SyncClock extends DisposableNativeObject {

static {
try {
Expand Down Expand Up @@ -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();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <pre>
* VideoCapture capture = new VideoCapture();
* capture.setVideoCaptureDevice(device);
Expand All @@ -32,12 +32,12 @@
* capture.dispose();
* </pre>
* Notes:
* <li>Always call dispose() to release native resources.</li>
* <li>Always call dispose(), or use try-with-resources, to release native resources.</li>
* <li>Methods are not guaranteed to be thread-safe unless externally synchronized.</li>
*
* @author Alex Andres
*/
public class VideoCapture extends NativeObject {
public class VideoCapture extends DisposableNativeObject {

/**
* Constructs a new VideoCapture and initializes underlying native resources.
Expand Down Expand Up @@ -125,13 +125,15 @@ public VideoCapture() {
* <p>
* Lifecycle:
* <li>Implicitly stops capture if currently running.</li>
* <li>After disposal, any further method call, including another call to
* {@code dispose()} itself, throws {@link NullPointerException}.</li>
* <li>After disposal, any further method call throws an
* {@link IllegalStateException}; another call to {@code dispose()}
* itself does nothing.</li>
* <p>
* 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();

/**
Expand Down
60 changes: 60 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/NativeDisposeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
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;
import dev.onvoid.webrtc.media.audio.AudioTrackSource;
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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/RTCDtmfSenderTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading