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
19 changes: 19 additions & 0 deletions docs/guide/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ frame.add(remoteView, BorderLayout.CENTER);

`snapshot()` returns a copy of the picture shown, for saving a still or for tests.

Both views support resolutions such as 8K without a fixed pixel limit. They reject
nonpositive dimensions and frames whose four-byte pixel data cannot fit in a Java
buffer, using overflow-safe arithmetic. Conversion buffers allocate on demand,
grow geometrically when necessary, and retain their capacity across smaller frames
and `clear()`. Swing reuses one direct buffer; JavaFX reuses up to two.
JavaFX reserves a buffer only while copying it into the displayed image; a waiting
frame may be replaced by a newer frame to avoid building a queue. Buffer
memory is released when the view becomes unreachable. Memory usage follows the
largest frames received and includes displayed images, conversion buffers, and
replacement allocations awaiting garbage collection. Snapshots, input frames and
toolkit rendering resources use additional memory.

For JavaFX, the `webrtc-java-javafx` module has a `VideoView` node in `dev.onvoid.webrtc.client.javafx` that works the same way. It needs Java 11 and the JavaFX your application already uses:

```xml
Expand Down Expand Up @@ -639,6 +651,13 @@ peer.onChannel(channel -> { // channels the remote peer opens
});
```

`Message.toString()` reports only the message type and byte length. Access content
explicitly with `text()` or `bytes()`. For diagnostics, `message.preview(64)` inspects
at most 64 payload bytes and escapes control characters. Binary previews use hex.
Use `message.preview(64, prefix -> redact(prefix))` to apply your own masking before
escaping; the masking function must handle secrets truncated at the prefix boundary.
The preview marks truncated content and also limits the masking function's output.

- A channel may be used right away: messages sent before it opens are held until it does.
- Messages that arrive while a channel has no `onMessage` handler are kept for the next one, so none are lost while a handler is being set up.
- `send` takes a `String`, a `byte[]` or a `ByteBuffer`, and copies the data before it returns.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
* Application Thread only paints, at most once per pulse however many frames
* arrive. The picture keeps its aspect ratio and its rotation, on a black
* background, and can be mirrored for a local camera preview.
* Conversion buffers grow on demand and are reused across resolution changes
* and clear(). Their retained capacity follows the largest frames received;
* buffer memory is released when the view becomes unreachable.
*/
public class VideoView extends Region implements Consumer<VideoFrame> {

Expand All @@ -58,11 +61,11 @@ public class VideoView extends Region implements Consumer<VideoFrame> {
/** Guards the hand-over between the converting thread and the JavaFX thread. */
private final Object lock = new Object();

// Three buffers: the converting thread fills back, hands it over as
// ready, and the JavaFX thread paints from shown. None is written while
// another thread reads it.
// Two reusable buffers: only the one JavaFX is copying is reserved. A waiting
// frame may be reclaimed for conversion, so the newest frame wins without
// blocking either thread during conversion or copying.

private Pixels back;
private final Pixels[] buffers = new Pixels[2];

private Pixels ready;

Expand All @@ -71,9 +74,10 @@ public class VideoView extends Region implements Consumer<VideoFrame> {
/** Counts the calls of clear(), to tell the frames that came before one. */
private long generation;

// Confined to the JavaFX Application Thread.
// Reserved under lock only while JavaFX copies pixels into the image.
private Pixels reading;

private Pixels shown;
// Confined to the JavaFX Application Thread.

private WritableImage image;

Expand All @@ -96,12 +100,15 @@ public VideoView() {
* frame is not released, which its subscription does.
*
* @param frame the frame to show.
* @throws IllegalArgumentException if dimensions are nonpositive or the
* frame cannot be represented by a Java pixel buffer.
*/
@Override
public void accept(VideoFrame frame) {
VideoFrameBuffer buffer = frame.buffer;
int width = buffer.getWidth();
int height = buffer.getHeight();
int byteCount = frameBytes(width, height);
long started;

// Before the wait for convertLock, as the frame came before any clear()
Expand All @@ -111,15 +118,31 @@ public void accept(VideoFrame frame) {
}

synchronized (convertLock) {
Pixels target = back;

if (target == null || !target.fits(width, height)) {
target = new Pixels(width, height);
// Only this thread chooses/writes a slot. Never overwrite a buffer
// JavaFX is copying; withdraw a waiting frame before reusing it.
int slot = 0;
synchronized (lock) {
while (buffers[slot] != null && buffers[slot] == reading) {
slot++;
}
if (buffers[slot] == ready) {
ready = null;
}
}
Pixels target = buffers[slot];
if (target == null || target.data.capacity() < byteCount) {
long previous = target == null ? 0 : target.data.capacity();
int capacity = (int) Math.min(Integer.MAX_VALUE, Math.max(byteCount, previous * 2L));
target = new Pixels(capacity);
buffers[slot] = target;
}
target.width = width;
target.height = height;

try {
// libyuv's ARGB is B, G, R, A in memory, which is JavaFX's BGRA.
target.data.clear();
target.data.limit(byteCount);
VideoBufferConverter.convertFromI420(buffer, target.data, FourCC.ARGB);
target.data.rewind();
}
Expand All @@ -134,7 +157,6 @@ public void accept(VideoFrame frame) {
return;
}

back = ready;
ready = target;

if (scheduled) {
Expand All @@ -148,6 +170,15 @@ public void accept(VideoFrame frame) {
Platform.runLater(this::present);
}

/** ByteBuffer capacities and image array lengths must fit in an int. */
static int frameBytes(int width, int height) {
long pixels = (long) width * height;
if (width <= 0 || height <= 0 || pixels > Integer.MAX_VALUE / 4) {
throw new IllegalArgumentException("Invalid or unrepresentable frame dimensions: " + width + "x" + height);
}
return (int) (pixels * 4L);
}

/**
* Removes the picture, leaving the background, and drops the frames still
* being converted. Called on the JavaFX Application Thread.
Expand All @@ -156,10 +187,8 @@ public void clear() {
synchronized (lock) {
generation++;
ready = null;
back = null;
}

shown = null;
image = null;
draw();
}
Expand Down Expand Up @@ -224,17 +253,24 @@ private void present() {
return;
}

ready = shown;
shown = next;
ready = null;
reading = next;
}

if (image == null || image.getWidth() != next.width || image.getHeight() != next.height) {
image = new WritableImage(next.width, next.height);
}
try {
if (image == null || image.getWidth() != next.width || image.getHeight() != next.height) {
image = new WritableImage(next.width, next.height);
}

image.getPixelWriter().setPixels(0, 0, next.width, next.height,
PixelFormat.getByteBgraPreInstance(), next.data, next.width * 4);
rotation = next.rotation;
image.getPixelWriter().setPixels(0, 0, next.width, next.height,
PixelFormat.getByteBgraPreInstance(), next.data, next.width * 4);
rotation = next.rotation;
}
finally {
synchronized (lock) {
reading = null;
}
}

draw();
}
Expand Down Expand Up @@ -270,23 +306,18 @@ private void draw() {

private static final class Pixels {

final int width;
int width;

final int height;
int height;

final ByteBuffer data;

int rotation;


Pixels(int width, int height) {
this.width = width;
this.height = height;
this.data = ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.LITTLE_ENDIAN);
Pixels(int capacity) {
this.data = ByteBuffer.allocateDirect(capacity).order(ByteOrder.LITTLE_ENDIAN);
}

boolean fits(int otherWidth, int otherHeight) {
return width == otherWidth && height == otherHeight;
}
}
}
Loading
Loading