layers = layers();
+ if (layers.isEmpty()) {
+ return null;
+ }
+
+ Simulcast.Builder builder = Simulcast.builder();
+ for (Layer layer : layers) {
+ builder.layer(layer.quality.rid(), Encoding.builder()
+ .scaleDownBy(layer.scale)
+ .maxBitrate(layer.bitrate)
+ .build());
+ }
+ return builder.build();
+ }
+
+ /** The bitrate of LiveKit's video preset for this height. */
+ private static int bitrate(int height) {
+ if (height >= 1080) {
+ return 3_000_000;
+ }
+ if (height >= 720) {
+ return 1_700_000;
+ }
+ if (height >= 540) {
+ return 800_000;
+ }
+ if (height >= 360) {
+ return 450_000;
+ }
+ return 160_000;
+ }
+
+ private static int ceilDiv(int value, int divisor) {
+ return (value + divisor - 1) / divisor;
+ }
+
+
+
+ static final class Layer {
+
+ final VideoQuality quality;
+
+ final int scale;
+
+ final int width;
+
+ final int height;
+
+ final int bitrate;
+
+
+ Layer(VideoQuality quality, int scale, int width, int height, int bitrate) {
+ this.quality = quality;
+ this.scale = scale;
+ this.width = width;
+ this.height = height;
+ this.bitrate = bitrate;
+ }
+ }
+
+
+
+ /** Builds {@link PublishOptions}. */
+ public static final class Builder {
+
+ private TrackSource source = TrackSource.UNKNOWN;
+
+ private String name;
+
+ private int width;
+
+ private int height;
+
+ private boolean simulcast;
+
+
+ private Builder() {
+ }
+
+ /**
+ * @param source where the track comes from.
+ *
+ * @return this builder.
+ */
+ public Builder source(TrackSource source) {
+ this.source = requireNonNull(source, "source");
+ return this;
+ }
+
+ /**
+ * @param name the name other participants see, the track's id by
+ * default.
+ *
+ * @return this builder.
+ */
+ public Builder name(String name) {
+ this.name = requireNonNull(name, "name");
+ return this;
+ }
+
+ /**
+ * @param width the width the video is captured in.
+ * @param height the height the video is captured in.
+ *
+ * @return this builder.
+ */
+ public Builder videoSize(int width, int height) {
+ if (width <= 0 || height <= 0) {
+ throw new IllegalArgumentException("The size must be positive, got " + width + "x" + height);
+ }
+ this.width = width;
+ this.height = height;
+ return this;
+ }
+
+ /**
+ * Sends a video track in simulcast layers, so that the server can
+ * forward each subscriber the one its connection and view take: three
+ * from 720 lines up, two from 360 lines up, none below. Needs the
+ * {@link #videoSize video size}.
+ *
+ * @param simulcast true to send the layers.
+ *
+ * @return this builder.
+ */
+ public Builder simulcast(boolean simulcast) {
+ this.simulcast = simulcast;
+ return this;
+ }
+
+ /**
+ * @return the options.
+ *
+ * @throws IllegalStateException if simulcast lacks the video size.
+ */
+ public PublishOptions build() {
+ if (simulcast && height == 0) {
+ throw new IllegalStateException("Simulcast needs the video size");
+ }
+ return new PublishOptions(this);
+ }
+ }
+
+}
diff --git a/webrtc-livekit/src/main/java/dev/onvoid/webrtc/livekit/Room.java b/webrtc-livekit/src/main/java/dev/onvoid/webrtc/livekit/Room.java
new file mode 100644
index 00000000..22d551d1
--- /dev/null
+++ b/webrtc-livekit/src/main/java/dev/onvoid/webrtc/livekit/Room.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2026 Alex Andres
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dev.onvoid.webrtc.livekit;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+
+import dev.onvoid.webrtc.client.LocalTrack;
+import dev.onvoid.webrtc.client.RemoteTrack;
+import dev.onvoid.webrtc.client.Subscription;
+
+/**
+ * A joined LiveKit room. It holds two peers of the runtime, one that publishes
+ * and one that subscribes, and the signaling connection that negotiates them.
+ *
+ * Handlers run on the room's own thread, one at a time and in the order the
+ * server sent what they report. A handler that blocks holds up the room, so
+ * long work belongs on another thread. The futures complete on that thread
+ * too.
+ */
+public interface Room extends AutoCloseable {
+
+ /**
+ * @return the name of the room.
+ */
+ String name();
+
+ /**
+ * @return the connection state.
+ */
+ RoomState state();
+
+ /**
+ * @return this client's participant.
+ */
+ Participant localParticipant();
+
+ /**
+ * @return the other participants, as of the last update of the server.
+ */
+ List remoteParticipants();
+
+ /**
+ * Publishes a local track: the server accepts it, then the publisher peer
+ * negotiates it.
+ *
+ * @param track the track, which stays the application's to close.
+ * @param source where the track comes from.
+ *
+ * @return a future with the publication, which fails if the server
+ * refuses the track or does not answer in time.
+ */
+ CompletableFuture publish(LocalTrack track, TrackSource source);
+
+ /**
+ * Publishes a local track as the options say, such as a video track in
+ * simulcast layers. With simulcast the server pauses the layers no
+ * subscriber wants (LiveKit's dynacast), and resumes them once one does.
+ *
+ * @param track the track, which stays the application's to close.
+ * @param options how to publish it.
+ *
+ * @return a future with the publication, which fails if the server
+ * refuses the track or does not answer in time.
+ */
+ CompletableFuture publish(LocalTrack track, PublishOptions options);
+
+ /**
+ * Asks the server to forward a remote video track in another simulcast
+ * layer. The server forwards a smaller one while the connection takes
+ * no more.
+ *
+ * @param trackSid the server's id of the remote track, its {@link
+ * RemoteTrack#id() id}.
+ * @param quality the layer.
+ */
+ void requestVideoQuality(String trackSid, VideoQuality quality);
+
+ /**
+ * Sends data to every other participant of the room.
+ *
+ * @param payload the data.
+ * @param topic a topic the receivers can tell messages apart by, or
+ * null.
+ * @param reliable true to send it ordered and retransmitted, false to
+ * send it once, for data that is stale soon, such as a
+ * pointer position.
+ *
+ * @return a future that completes once the data is handed to the channel.
+ */
+ CompletableFuture publishData(byte[] payload, String topic, boolean reliable);
+
+ /**
+ * @param handler gets each participant that joins after this client.
+ *
+ * @return removes the handler.
+ */
+ Subscription onParticipantJoined(Consumer handler);
+
+ /**
+ * @param handler gets each participant that leaves.
+ *
+ * @return removes the handler.
+ */
+ Subscription onParticipantLeft(Consumer handler);
+
+ /**
+ * @param handler gets each remote track this client receives, with the
+ * participant that publishes it. Tracks that arrived
+ * before the handler, and have not ended, are handed to it
+ * too.
+ *
+ * @return removes the handler.
+ */
+ Subscription onTrackSubscribed(BiConsumer handler);
+
+ /**
+ * @param handler gets the data other participants publish.
+ *
+ * @return removes the handler.
+ */
+ Subscription onData(Consumer handler);
+
+ /**
+ * @param handler gets the reason each time the connection is lost and
+ * the room starts to get it back.
+ *
+ * @return removes the handler.
+ */
+ Subscription onReconnecting(Consumer handler);
+
+ /**
+ * @param handler runs each time the connection is back. If the session
+ * could not be resumed, the room joined anew: the local
+ * participant then has a new sid, the participants were
+ * reported as left and joined again, and the publications
+ * have new sids.
+ *
+ * @return removes the handler.
+ */
+ Subscription onReconnected(Runnable handler);
+
+ /**
+ * @param handler gets the reason once the room is left, for whatever
+ * reason, the application's close included.
+ *
+ * @return removes the handler.
+ */
+ Subscription onDisconnected(Consumer handler);
+
+ /**
+ * Leaves the room and closes its peers. The tracks stay the
+ * application's.
+ *
+ * @return a future that completes once the peers are closed.
+ */
+ CompletableFuture closeAsync();
+
+ /**
+ * Leaves the room and waits for its peers to close.
+ */
+ @Override
+ void close();
+
+}
diff --git a/webrtc-livekit/src/main/java/dev/onvoid/webrtc/livekit/RoomImpl.java b/webrtc-livekit/src/main/java/dev/onvoid/webrtc/livekit/RoomImpl.java
new file mode 100644
index 00000000..e02330ff
--- /dev/null
+++ b/webrtc-livekit/src/main/java/dev/onvoid/webrtc/livekit/RoomImpl.java
@@ -0,0 +1,1633 @@
+/*
+ * Copyright 2026 Alex Andres
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dev.onvoid.webrtc.livekit;
+
+import static java.util.Objects.requireNonNull;
+
+import java.lang.System.Logger.Level;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.EnumMap;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.function.BiConsumer;
+import java.util.function.Consumer;
+
+import com.google.protobuf.ByteString;
+import com.google.protobuf.InvalidProtocolBufferException;
+
+import dev.onvoid.webrtc.RTCIceCandidate;
+import dev.onvoid.webrtc.RTCSdpType;
+import dev.onvoid.webrtc.RTCSessionDescription;
+import dev.onvoid.webrtc.client.ChannelOptions;
+import dev.onvoid.webrtc.client.DataChannel;
+import dev.onvoid.webrtc.client.Encoding;
+import dev.onvoid.webrtc.client.LocalTrack;
+import dev.onvoid.webrtc.client.MediaKind;
+import dev.onvoid.webrtc.client.Negotiation;
+import dev.onvoid.webrtc.client.Peer;
+import dev.onvoid.webrtc.client.PeerOptions;
+import dev.onvoid.webrtc.client.PeerState;
+import dev.onvoid.webrtc.client.RemoteTrack;
+import dev.onvoid.webrtc.client.Sender;
+import dev.onvoid.webrtc.client.SignalMessage;
+import dev.onvoid.webrtc.client.Simulcast;
+import dev.onvoid.webrtc.client.Subscription;
+import dev.onvoid.webrtc.client.WebRtc;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitModels.DataPacket;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitModels.DisconnectReason;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitModels.ParticipantInfo;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitModels.TrackType;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitModels.UserPacket;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitModels.VideoLayer;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.AddTrackRequest;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.ICEServer;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.JoinResponse;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.LeaveRequest;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.Ping;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.RequestResponse;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SessionDescription;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SignalRequest;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SignalResponse;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SignalTarget;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SimulateScenario;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SubscribedCodec;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SubscribedQuality;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SubscribedQualityUpdate;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.SyncState;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.TrackPublishedResponse;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.TrickleRequest;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.UpdateSubscription;
+import dev.onvoid.webrtc.livekit.internal.proto.LivekitRtc.UpdateTrackSettings;
+
+/**
+ * The room: the state LiveKit's signaling describes, and the publisher and
+ * subscriber peers it negotiates. Everything but the handler lists is
+ * confined to the room's single thread, which also runs the handlers.
+ *
+ * A lost connection is recovered as LiveKit's own clients do. A resume opens
+ * the signaling again for the same session and restarts ICE on both peers,
+ * which keeps the peers, their tracks and data channels. Should the server
+ * refuse it, or should it fail, the room joins anew, with new peers, and
+ * publishes its tracks again.
+ */
+final class RoomImpl implements Room {
+
+ private static final System.Logger LOG = System.getLogger(Room.class.getName());
+
+ private static final String RELIABLE_LABEL = "_reliable";
+
+ private static final String LOSSY_LABEL = "_lossy";
+
+ /** Reconnect attempts before the room gives up, as LiveKit's clients make. */
+ private static final int MAX_ATTEMPTS = 10;
+
+ private static final long BACKOFF_STEP_MILLIS = 300;
+
+ private static final long MAX_BACKOFF_MILLIS = 10_000;
+
+ private final WebRtc rtc;
+
+ private final URI url;
+
+ private final boolean autoSubscribe;
+
+ private final Duration timeout;
+
+ private final HttpClient http = HttpClient.newHttpClient();
+
+ private final ScheduledExecutorService thread;
+
+ /** Runs on the room's thread, and drops what comes after the room is gone. */
+ private final Executor executor;
+
+ private final CompletableFuture joined = new CompletableFuture<>();
+
+ private final CompletableFuture closed = new CompletableFuture<>();
+
+ private final List> joinedHandlers = new CopyOnWriteArrayList<>();
+
+ private final List> leftHandlers = new CopyOnWriteArrayList<>();
+
+ private final List> trackHandlers = new CopyOnWriteArrayList<>();
+
+ private final List> dataHandlers = new CopyOnWriteArrayList<>();
+
+ private final List> reconnectingHandlers = new CopyOnWriteArrayList<>();
+
+ private final List reconnectedHandlers = new CopyOnWriteArrayList<>();
+
+ private final List> disconnectHandlers = new CopyOnWriteArrayList<>();
+
+ /** Hear which layers dynacast left on, once applied. For tests. */
+ final List>> layerListeners = new CopyOnWriteArrayList<>();
+
+ // Room thread only, unless volatile.
+
+ private volatile RoomState state = RoomState.CONNECTING;
+
+ /** The token for the next connection; the server refreshes it. */
+ private volatile String token;
+
+ private volatile String name = "";
+
+ private volatile Participant local;
+
+ /** By sid, in join order. Written on the room thread, read anywhere. */
+ private volatile Map participants = Map.of();
+
+ /** Completes whenever the room is connected; operations wait on it. */
+ private CompletableFuture connectedGate = new CompletableFuture<>();
+
+ private SignalClient signal;
+
+ /** The first message the open signaling waits for: a join or reconnect response. */
+ private CompletableFuture firstResponse;
+
+ private SignalResponse.MessageCase firstResponseCase;
+
+ /** Tracks that arrived before the participant that publishes them, by participant sid. */
+ private final Map> orphanTracks = new HashMap<>();
+
+ /** The remote tracks handed to the track handlers, for handlers added later. */
+ private final List subscribedTracks = new ArrayList<>();
+
+ private final Map pendingPublishes = new HashMap<>();
+
+ /** The published tracks, by sid. */
+ private final Map publications = new LinkedHashMap<>();
+
+ private Peer publisher;
+
+ private Peer subscriber;
+
+ private DataChannel reliable;
+
+ private DataChannel lossy;
+
+ private boolean negotiating;
+
+ private boolean renegotiate;
+
+ private boolean publisherNegotiated;
+
+ private boolean subscriberAnswered;
+
+ /** Wait for the offer that is out. */
+ private List> negotiationWaiters = new ArrayList<>();
+
+ /** Wait for the offer after the one that is out. */
+ private List> nextNegotiationWaiters = new ArrayList<>();
+
+ private int offerId;
+
+ private int dataSequence = 1;
+
+ private int pingIntervalSeconds;
+
+ private int pingTimeoutSeconds;
+
+ private ScheduledFuture> pingTask;
+
+ private long lastPongMillis;
+
+ // Recovery.
+
+ private boolean fullReconnect;
+
+ private int attempts;
+
+ /** Counts the attempts, so that the steps of an abandoned one stop. */
+ private int generation;
+
+ private CompletableFuture currentAttempt;
+
+
+ private RoomImpl(WebRtc rtc, URI url, String token, boolean autoSubscribe, Duration timeout) {
+ this.rtc = rtc;
+ this.url = url;
+ this.token = token;
+ this.autoSubscribe = autoSubscribe;
+ this.timeout = timeout;
+ this.thread = Executors.newSingleThreadScheduledExecutor(task -> {
+ Thread t = new Thread(task, "livekit-room");
+ t.setDaemon(true);
+ return t;
+ });
+ this.executor = task -> {
+ try {
+ thread.execute(task);
+ }
+ catch (RejectedExecutionException e) {
+ // The room is closed and every future of it completed.
+ }
+ };
+ }
+
+ static CompletableFuture connect(WebRtc rtc, URI url, String token, boolean autoSubscribe,
+ Duration timeout) {
+ // Fails here on a URL that is not one.
+ SignalClient.rtcUri(url, LiveKit.PROTOCOL, autoSubscribe);
+
+ RoomImpl room = new RoomImpl(rtc, url, token, autoSubscribe, timeout);
+
+ // Not async: the session is set up before the room thread takes the
+ // messages that follow the join, such as the server's first offer.
+ room.executor.execute(() -> room.openSignal(false).whenComplete((response, error) -> {
+ if (error != null) {
+ room.failJoin(unwrap(error));
+ return;
+ }
+ if (room.joined.isDone()) {
+ return;
+ }
+
+ try {
+ room.setUpSession(response.getJoin(), false);
+ }
+ catch (RuntimeException e) {
+ room.failJoin(e);
+ return;
+ }
+
+ room.state = RoomState.CONNECTED;
+ room.connectedGate.complete(null);
+ room.joined.complete(room);
+ }));
+
+ return room.joined.copy();
+ }
+
+ @Override
+ public String name() {
+ return name;
+ }
+
+ @Override
+ public RoomState state() {
+ return state;
+ }
+
+ @Override
+ public Participant localParticipant() {
+ return local;
+ }
+
+ @Override
+ public List remoteParticipants() {
+ return List.copyOf(participants.values());
+ }
+
+ @Override
+ public CompletableFuture publish(LocalTrack track, TrackSource source) {
+ requireNonNull(source, "source");
+
+ return publish(track, PublishOptions.builder().source(source).build());
+ }
+
+ @Override
+ public CompletableFuture publish(LocalTrack track, PublishOptions options) {
+ requireNonNull(track, "track");
+ requireNonNull(options, "options");
+
+ if (options.simulcast && track.kind() != MediaKind.VIDEO) {
+ throw new IllegalArgumentException("Simulcast takes a video track, got " + track.kind());
+ }
+
+ CompletableFuture result = new CompletableFuture<>();
+ result.orTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS);
+
+ post(() -> whenConnected(result, () -> startPublish(track, options, null, result)), result);
+
+ return result;
+ }
+
+ @Override
+ public void requestVideoQuality(String trackSid, VideoQuality quality) {
+ requireNonNull(trackSid, "trackSid");
+ requireNonNull(quality, "quality");
+
+ executor.execute(() -> {
+ if (state != RoomState.CONNECTED) {
+ return;
+ }
+
+ signal.send(SignalRequest.newBuilder()
+ .setTrackSetting(UpdateTrackSettings.newBuilder()
+ .addTrackSids(trackSid)
+ .setQuality(quality.proto()))
+ .build());
+ });
+ }
+
+ @Override
+ public CompletableFuture publishData(byte[] payload, String topic, boolean reliable) {
+ requireNonNull(payload, "payload");
+
+ byte[] data = payload.clone();
+ CompletableFuture result = new CompletableFuture<>();
+ result.orTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS);
+
+ post(() -> whenConnected(result, () -> sendData(data, topic, reliable, result)), result);
+
+ return result;
+ }
+
+ @Override
+ public Subscription onParticipantJoined(Consumer handler) {
+ return subscribe(joinedHandlers, handler);
+ }
+
+ @Override
+ public Subscription onParticipantLeft(Consumer handler) {
+ return subscribe(leftHandlers, handler);
+ }
+
+ @Override
+ public Subscription onTrackSubscribed(BiConsumer handler) {
+ requireNonNull(handler, "handler");
+
+ // On the room thread, so that a track is handed over either here or
+ // when it arrives, never both.
+ boolean[] removed = new boolean[1];
+ executor.execute(() -> {
+ if (removed[0]) {
+ return;
+ }
+ trackHandlers.add(handler);
+
+ for (SubscribedTrack subscribed : List.copyOf(subscribedTracks)) {
+ if (!subscribed.track.isEnded()) {
+ call(() -> handler.accept(subscribed.participant, subscribed.track));
+ }
+ }
+ });
+
+ return () -> executor.execute(() -> {
+ removed[0] = true;
+ trackHandlers.remove(handler);
+ });
+ }
+
+ @Override
+ public Subscription onData(Consumer handler) {
+ return subscribe(dataHandlers, handler);
+ }
+
+ @Override
+ public Subscription onReconnecting(Consumer handler) {
+ return subscribe(reconnectingHandlers, handler);
+ }
+
+ @Override
+ public Subscription onReconnected(Runnable handler) {
+ return subscribe(reconnectedHandlers, handler);
+ }
+
+ @Override
+ public Subscription onDisconnected(Consumer handler) {
+ return subscribe(disconnectHandlers, handler);
+ }
+
+ @Override
+ public CompletableFuture closeAsync() {
+ executor.execute(() -> disconnect("The application left the room", true));
+ return closed.copy();
+ }
+
+ @Override
+ public void close() {
+ try {
+ closeAsync().get(timeout.toMillis() * 2, TimeUnit.MILLISECONDS);
+ }
+ catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ catch (Exception e) {
+ LOG.log(Level.WARNING, "Closing the room did not finish", e);
+ }
+ }
+
+ // For tests.
+
+ /** Cuts the signaling connection off, as a network failure would. */
+ void cutSignal() {
+ executor.execute(() -> {
+ if (signal != null) {
+ signal.abort();
+ }
+ });
+ }
+
+ /** Has the server act out a scenario, as a server started with --dev does. */
+ void simulate(SimulateScenario scenario) {
+ executor.execute(() -> signal.send(SignalRequest.newBuilder().setSimulate(scenario).build()));
+ }
+
+ // Signaling, on the room thread.
+
+ /**
+ * Opens a signaling connection, which replaces the one before, and waits
+ * for its first response: a join response for a new session, a reconnect
+ * response for a resumed one. The future completes on the room thread,
+ * while it handles that response, so that stages added without async
+ * run before the messages behind it.
+ */
+ private CompletableFuture openSignal(boolean resume) {
+ SignalClient[] self = new SignalClient[1];
+ SignalClient client = new SignalClient(executor, new SignalClient.Handler() {
+
+ @Override
+ public void onSignal(SignalResponse response) {
+ if (signal == self[0]) {
+ RoomImpl.this.onSignal(response);
+ }
+ }
+
+ @Override
+ public void onSignalClosed(String reason) {
+ if (signal == self[0]) {
+ RoomImpl.this.onSignalClosed(reason);
+ }
+ }
+ });
+ self[0] = client;
+
+ if (signal != null) {
+ signal.close();
+ }
+ signal = client;
+
+ CompletableFuture first = new CompletableFuture<>();
+ firstResponse = first;
+ firstResponseCase = resume ? SignalResponse.MessageCase.RECONNECT : SignalResponse.MessageCase.JOIN;
+
+ URI uri = SignalClient.rtcUri(url, LiveKit.PROTOCOL, autoSubscribe, resume ? local.sid() : null);
+
+ client.open(http, uri, token, timeout).whenCompleteAsync((v, error) -> {
+ if (error != null) {
+ first.completeExceptionally(unwrap(error));
+ }
+ }, executor);
+
+ ScheduledFuture> deadline = thread.schedule(() -> first.completeExceptionally(
+ new LiveKitException("No " + (resume ? "reconnect" : "join") + " response within " + timeout)),
+ timeout.toMillis(), TimeUnit.MILLISECONDS);
+ first.whenComplete((r, e) -> deadline.cancel(false));
+
+ return first;
+ }
+
+ private void onSignal(SignalResponse response) {
+ if (state == RoomState.DISCONNECTED) {
+ return;
+ }
+
+ switch (response.getMessageCase()) {
+ case JOIN:
+ case RECONNECT:
+ if (firstResponse != null && firstResponseCase == response.getMessageCase()) {
+ firstResponse.complete(response);
+ }
+ break;
+ case ANSWER:
+ onAnswer(response.getAnswer());
+ break;
+ case OFFER:
+ onOffer(response.getOffer());
+ break;
+ case TRICKLE:
+ onTrickle(response.getTrickle());
+ break;
+ case UPDATE:
+ onParticipants(response.getUpdate().getParticipantsList());
+ break;
+ case TRACK_PUBLISHED:
+ onTrackPublished(response.getTrackPublished());
+ break;
+ case SUBSCRIBED_QUALITY_UPDATE:
+ onSubscribedQualities(response.getSubscribedQualityUpdate());
+ break;
+ case LEAVE:
+ onLeave(response.getLeave());
+ break;
+ case PONG_RESP:
+ lastPongMillis = System.currentTimeMillis();
+ break;
+ case REFRESH_TOKEN:
+ token = response.getRefreshToken();
+ break;
+ case REQUEST_RESPONSE:
+ RequestResponse answer = response.getRequestResponse();
+ if (answer.getReason() != RequestResponse.Reason.OK) {
+ LOG.log(Level.WARNING, "The server refused request " + answer.getRequestId() + ": "
+ + answer.getReason() + " " + answer.getMessage());
+ }
+ break;
+ default:
+ LOG.log(Level.DEBUG, () -> "Ignored " + response.getMessageCase());
+ }
+ }
+
+ private void onSignalClosed(String reason) {
+ if (!joined.isDone()) {
+ failJoin(new LiveKitException(reason));
+ return;
+ }
+
+ if (firstResponse != null && !firstResponse.isDone()) {
+ firstResponse.completeExceptionally(new LiveKitException(reason));
+ }
+
+ if (state == RoomState.CONNECTED) {
+ recover(false, reason);
+ }
+ else if (state == RoomState.RECONNECTING && currentAttempt != null) {
+ currentAttempt.completeExceptionally(new LiveKitException(reason));
+ }
+ }
+
+ private void onLeave(LeaveRequest leave) {
+ String reason = "The server ended the session (" + leave.getReason() + ", " + leave.getAction() + ")";
+
+ if (!joined.isDone()) {
+ failJoin(new LiveKitException(reason));
+ return;
+ }
+
+ LeaveRequest.Action action = leave.getAction();
+ if (action == LeaveRequest.Action.DISCONNECT && leave.getCanReconnect()) {
+ // What older servers send for a reconnect.
+ action = LeaveRequest.Action.RECONNECT;
+ }
+
+ switch (action) {
+ case RESUME:
+ recover(false, reason);
+ break;
+ case RECONNECT:
+ recover(true, reason);
+ break;
+ default:
+ disconnect(reason, false);
+ }
+ }
+
+ /**
+ * Takes the response of a join: the room, the participants and the ICE
+ * servers, from which it makes the peers of the session.
+ *
+ * @param rejoined true if this is a full reconnect, whose participants
+ * are reported as joined again.
+ */
+ private void setUpSession(JoinResponse join, boolean rejoined) {
+ name = join.getRoom().getName();
+ local = new Participant(join.getParticipant());
+
+ Map others = new LinkedHashMap<>();
+ for (ParticipantInfo info : join.getOtherParticipantsList()) {
+ others.put(info.getSid(), new Participant(info));
+ }
+ participants = others;
+
+ publisher = createPeer(SignalTarget.PUBLISHER, join.getIceServersList());
+ subscriber = createPeer(SignalTarget.SUBSCRIBER, join.getIceServersList());
+
+ // The channels go into the first offer of the publisher.
+ reliable = publisher.createChannel(RELIABLE_LABEL, ChannelOptions.builder().ordered(true).build());
+ lossy = publisher.createChannel(LOSSY_LABEL,
+ ChannelOptions.builder().ordered(false).maxRetransmits(0).build());
+
+ receiveData(reliable);
+ receiveData(lossy);
+
+ Peer sessionSubscriber = subscriber;
+ sessionSubscriber.onTrack(track -> executor.execute(() -> {
+ if (sessionSubscriber == subscriber) {
+ onRemoteTrack(track);
+ }
+ }));
+ sessionSubscriber.onChannel(channel -> executor.execute(() -> {
+ if (sessionSubscriber == subscriber) {
+ receiveData(channel);
+ }
+ }));
+
+ pingIntervalSeconds = join.getPingInterval();
+ pingTimeoutSeconds = join.getPingTimeout();
+ startPing();
+
+ if (rejoined) {
+ for (Participant participant : others.values()) {
+ for (Consumer handler : joinedHandlers) {
+ call(() -> handler.accept(participant));
+ }
+ }
+ }
+
+ if (!join.getSubscriberPrimary() || join.getFastPublish()) {
+ negotiatePublisher();
+ }
+ }
+
+ private Peer createPeer(SignalTarget target, List servers) {
+ PeerOptions.Builder options = PeerOptions.builder()
+ .name("livekit-" + target.name().toLowerCase())
+ .negotiation(Negotiation.MANUAL)
+ .signaling(message -> {
+ // A manual peer signals only its candidates.
+ if (message.type() == SignalMessage.Type.CANDIDATE) {
+ RTCIceCandidate candidate = message.candidate();
+ executor.execute(() -> sendTrickle(candidate, target));
+ }
+ });
+
+ for (ICEServer server : servers) {
+ for (String serverUrl : server.getUrlsList()) {
+ if (server.getUsername().isEmpty()) {
+ options.iceServer(serverUrl);
+ }
+ else {
+ options.iceServer(serverUrl, server.getUsername(), server.getCredential());
+ }
+ }
+ }
+
+ Peer peer = rtc.createPeer(options.build());
+ peer.onError(e -> LOG.log(Level.WARNING, "The " + target + " peer failed", e));
+ peer.onStateChange(peerState -> executor.execute(() -> {
+ boolean current = peer == publisher || peer == subscriber;
+ if (current && peerState == PeerState.FAILED && state == RoomState.CONNECTED) {
+ recover(false, "The " + target.name().toLowerCase() + " connection failed");
+ }
+ }));
+
+ return peer;
+ }
+
+ private void sendTrickle(RTCIceCandidate candidate, SignalTarget target) {
+ if (state == RoomState.DISCONNECTED) {
+ return;
+ }
+
+ signal.send(SignalRequest.newBuilder()
+ .setTrickle(TrickleRequest.newBuilder()
+ .setCandidateInit(CandidateJson.encode(candidate))
+ .setTarget(target))
+ .build());
+ }
+
+ private void onTrickle(TrickleRequest trickle) {
+ Peer peer = trickle.getTarget() == SignalTarget.PUBLISHER ? publisher : subscriber;
+ if (peer == null) {
+ return;
+ }
+
+ RTCIceCandidate candidate;
+ try {
+ candidate = CandidateJson.decode(trickle.getCandidateInit());
+ }
+ catch (IllegalArgumentException e) {
+ LOG.log(Level.WARNING, "Dropped a candidate that does not parse", e);
+ return;
+ }
+
+ peer.negotiator().addCandidate(candidate).exceptionally(e -> {
+ LOG.log(Level.DEBUG, () -> "A remote candidate was refused: " + e);
+ return null;
+ });
+ }
+
+ // The publisher offers, the server answers.
+
+ private CompletableFuture negotiatePublisher() {
+ CompletableFuture negotiated = new CompletableFuture<>();
+
+ if (negotiating) {
+ // The offer that is out may predate what the caller changed.
+ nextNegotiationWaiters.add(negotiated);
+ renegotiate = true;
+ }
+ else {
+ negotiationWaiters.add(negotiated);
+ sendOffer();
+ }
+
+ return negotiated;
+ }
+
+ private void sendOffer() {
+ negotiating = true;
+ Peer peer = publisher;
+
+ peer.negotiator().createOffer().whenCompleteAsync((offer, error) -> {
+ if (peer != publisher) {
+ return;
+ }
+ if (error != null) {
+ negotiationDone(error);
+ return;
+ }
+ if (state == RoomState.DISCONNECTED) {
+ return;
+ }
+
+ signal.send(SignalRequest.newBuilder()
+ .setOffer(SessionDescription.newBuilder()
+ .setType("offer")
+ .setSdp(offer.sdp)
+ .setId(++offerId))
+ .build());
+ }, executor);
+ }
+
+ private void onAnswer(SessionDescription answer) {
+ if (publisher == null || !negotiating) {
+ return;
+ }
+
+ Peer peer = publisher;
+ peer.negotiator().applyAnswer(new RTCSessionDescription(RTCSdpType.ANSWER, answer.getSdp()))
+ .whenCompleteAsync((v, error) -> {
+ if (peer == publisher) {
+ negotiationDone(error);
+ }
+ }, executor);
+ }
+
+ private void negotiationDone(Throwable error) {
+ negotiating = false;
+
+ if (error == null) {
+ publisherNegotiated = true;
+ }
+ else {
+ LOG.log(Level.WARNING, "Negotiating the publisher failed", error);
+ }
+
+ List> waiters = negotiationWaiters;
+ negotiationWaiters = nextNegotiationWaiters;
+ nextNegotiationWaiters = new ArrayList<>();
+
+ if (renegotiate && state != RoomState.DISCONNECTED) {
+ renegotiate = false;
+ sendOffer();
+ }
+
+ for (CompletableFuture waiter : waiters) {
+ if (error == null) {
+ waiter.complete(null);
+ }
+ else {
+ waiter.completeExceptionally(error);
+ }
+ }
+ }
+
+ /**
+ * Offers again with ICE restarted, for a resumed session: the answer to an
+ * offer that was out may have been lost with the old connection, so its
+ * waiters wait for the new one.
+ */
+ private void restartPublisherIce() {
+ List> waiters = new ArrayList<>(negotiationWaiters);
+ waiters.addAll(nextNegotiationWaiters);
+ negotiationWaiters = new ArrayList<>();
+ nextNegotiationWaiters = new ArrayList<>();
+ negotiating = false;
+ renegotiate = false;
+
+ if (!publisherNegotiated && waiters.isEmpty()) {
+ return;
+ }
+
+ publisher.restartIce();
+ negotiationWaiters.addAll(waiters);
+ sendOffer();
+ }
+
+ // The server offers, the subscriber answers.
+
+ private void onOffer(SessionDescription offer) {
+ if (subscriber == null) {
+ return;
+ }
+
+ Peer peer = subscriber;
+ peer.negotiator().answer(new RTCSessionDescription(RTCSdpType.OFFER, offer.getSdp()))
+ .whenCompleteAsync((answer, error) -> {
+ if (peer != subscriber || state == RoomState.DISCONNECTED) {
+ return;
+ }
+ if (error != null) {
+ LOG.log(Level.WARNING, "Answering the server's offer failed", error);
+ return;
+ }
+
+ subscriberAnswered = true;
+ signal.send(SignalRequest.newBuilder()
+ .setAnswer(SessionDescription.newBuilder()
+ .setType("answer")
+ .setSdp(answer.sdp)
+ .setId(offer.getId()))
+ .build());
+ }, executor);
+ }
+
+ // Publishing.
+
+ /**
+ * Asks the server to take a track.
+ *
+ * @param existing the publication to publish again in a new session, or
+ * null for a new one.
+ */
+ private void startPublish(LocalTrack track, PublishOptions options, Publication existing,
+ CompletableFuture result) {
+ // The server knows the track by this id until it names its own.
+ String cid = track.id();
+ if (pendingPublishes.containsKey(cid)) {
+ result.completeExceptionally(new LiveKitException("The track " + cid + " is being published"));
+ return;
+ }
+
+ pendingPublishes.put(cid, new PendingPublish(track, options, existing, result));
+ result.whenCompleteAsync((p, e) -> {
+ PendingPublish pending = pendingPublishes.get(cid);
+ if (pending != null && pending.result == result) {
+ pendingPublishes.remove(cid);
+ }
+ }, executor);
+
+ AddTrackRequest.Builder request = AddTrackRequest.newBuilder()
+ .setCid(cid)
+ .setName(options.name != null ? options.name : track.id())
+ .setType(track.kind() == MediaKind.VIDEO ? TrackType.VIDEO : TrackType.AUDIO)
+ .setSource(options.source.proto());
+
+ if (track.kind() == MediaKind.VIDEO && options.height > 0) {
+ request.setWidth(options.width).setHeight(options.height);
+
+ List layers = options.layers();
+ if (layers.isEmpty()) {
+ request.addLayers(VideoLayer.newBuilder()
+ .setQuality(VideoQuality.HIGH.proto())
+ .setWidth(options.width)
+ .setHeight(options.height));
+ }
+ for (PublishOptions.Layer layer : layers) {
+ request.addLayers(VideoLayer.newBuilder()
+ .setQuality(layer.quality.proto())
+ .setRid(layer.quality.rid())
+ .setWidth(layer.width)
+ .setHeight(layer.height)
+ .setBitrate(layer.bitrate));
+ }
+ }
+
+ signal.send(SignalRequest.newBuilder().setAddTrack(request).build());
+ }
+
+ private void onTrackPublished(TrackPublishedResponse published) {
+ PendingPublish pending = pendingPublishes.remove(published.getCid());
+ if (pending == null || pending.result.isDone()) {
+ return;
+ }
+
+ Sender sender;
+ try {
+ Simulcast simulcast = pending.options.simulcastLayers();
+ sender = simulcast != null
+ ? publisher.addTrack(pending.track, simulcast)
+ : publisher.addTrack(pending.track);
+ }
+ catch (RuntimeException e) {
+ pending.result.completeExceptionally(e);
+ return;
+ }
+
+ Publication publication;
+ if (pending.existing != null) {
+ publication = pending.existing;
+ publication.republished(published.getTrack(), sender);
+ }
+ else {
+ publication = new Publication(published.getTrack(), pending.options, pending.track, sender);
+ }
+
+ Peer peer = publisher;
+ negotiatePublisher().whenComplete((v, error) -> {
+ if (error != null) {
+ pending.result.completeExceptionally(error);
+ }
+ else if (peer == publisher) {
+ publications.put(publication.sid(), publication);
+ pending.result.complete(publication);
+ }
+ else {
+ pending.result.completeExceptionally(new LiveKitException("The session ended"));
+ }
+ });
+ }
+
+ /**
+ * Dynacast: the server names the layers some subscriber wants. Sending
+ * the others would only cost bandwidth and CPU, so they pause.
+ */
+ private void onSubscribedQualities(SubscribedQualityUpdate update) {
+ Publication publication = publications.get(update.getTrackSid());
+ if (publication == null || publication.rids().isEmpty()) {
+ return;
+ }
+
+ // One codec is published; older servers send the qualities without it.
+ List qualities = update.getSubscribedQualitiesList();
+ for (SubscribedCodec codec : update.getSubscribedCodecsList()) {
+ qualities = codec.getQualitiesList();
+ }
+
+ Map enabled = new EnumMap<>(VideoQuality.class);
+ List> changes = new ArrayList<>();
+
+ for (SubscribedQuality quality : qualities) {
+ VideoQuality layer = VideoQuality.of(quality.getQuality());
+ if (layer == null || !publication.rids().contains(layer.rid())) {
+ continue;
+ }
+
+ enabled.put(layer, quality.getEnabled());
+ changes.add(publication.sender().configure(layer.rid(),
+ Encoding.builder().active(quality.getEnabled()).build()));
+ }
+
+ CompletableFuture.allOf(changes.toArray(new CompletableFuture[0])).whenCompleteAsync((v, error) -> {
+ if (error != null) {
+ LOG.log(Level.WARNING, "Pausing or resuming layers of " + publication.sid() + " failed", error);
+ return;
+ }
+ for (BiConsumer> listener : layerListeners) {
+ call(() -> listener.accept(publication, enabled));
+ }
+ }, executor);
+ }
+
+ // Data.
+
+ private void sendData(byte[] payload, String topic, boolean reliable, CompletableFuture result) {
+ UserPacket.Builder user = UserPacket.newBuilder().setPayload(ByteString.copyFrom(payload));
+ if (topic != null) {
+ user.setTopic(topic);
+ }
+
+ DataPacket.Builder packet = DataPacket.newBuilder()
+ .setParticipantIdentity(local.identity())
+ .setUser(user);
+ if (reliable) {
+ packet.setSequence(dataSequence++);
+ }
+ byte[] bytes = packet.build().toByteArray();
+
+ DataChannel channel = reliable ? this.reliable : this.lossy;
+ CompletableFuture ready = publisherNegotiated || negotiating
+ ? CompletableFuture.completedFuture(null)
+ : negotiatePublisher();
+
+ ready.thenCompose(v -> channel.whenOpen())
+ .thenCompose(open -> open.send(bytes))
+ .whenComplete((v, error) -> {
+ if (error != null) {
+ result.completeExceptionally(error);
+ }
+ else {
+ result.complete(null);
+ }
+ });
+ }
+
+ private void receiveData(DataChannel channel) {
+ boolean reliableChannel = !LOSSY_LABEL.equals(channel.label());
+
+ channel.onMessage(message -> {
+ if (message.isText()) {
+ return;
+ }
+ byte[] bytes = message.bytes();
+ executor.execute(() -> onDataPacket(bytes, reliableChannel));
+ });
+ }
+
+ private void onDataPacket(byte[] bytes, boolean reliableChannel) {
+ DataPacket packet;
+ try {
+ packet = DataPacket.parseFrom(bytes);
+ }
+ catch (InvalidProtocolBufferException e) {
+ LOG.log(Level.WARNING, "Dropped a data packet that does not parse", e);
+ return;
+ }
+
+ if (!packet.hasUser()) {
+ return;
+ }
+
+ UserPacket user = packet.getUser();
+ DataMessage message = new DataMessage(packet.getParticipantIdentity(),
+ user.hasTopic() ? user.getTopic() : null, user.getPayload().toByteArray(), reliableChannel);
+
+ for (Consumer handler : dataHandlers) {
+ call(() -> handler.accept(message));
+ }
+ }
+
+ // Participants and their tracks.
+
+ private void onParticipants(List infos) {
+ Map next = new LinkedHashMap<>(participants);
+ List joinedNow = new ArrayList<>();
+ List leftNow = new ArrayList<>();
+
+ for (ParticipantInfo info : infos) {
+ if (local != null && info.getSid().equals(local.sid())) {
+ local.update(info);
+ continue;
+ }
+
+ Participant participant = next.get(info.getSid());
+
+ if (info.getState() == ParticipantInfo.State.DISCONNECTED) {
+ if (participant != null) {
+ next.remove(info.getSid());
+ orphanTracks.remove(info.getSid());
+ leftNow.add(participant);
+ }
+ }
+ else if (participant == null) {
+ participant = new Participant(info);
+ next.put(info.getSid(), participant);
+ joinedNow.add(participant);
+ }
+ else {
+ participant.update(info);
+ }
+ }
+
+ participants = next;
+
+ for (Participant participant : joinedNow) {
+ for (Consumer handler : joinedHandlers) {
+ call(() -> handler.accept(participant));
+ }
+
+ List tracks = orphanTracks.remove(participant.sid());
+ if (tracks != null) {
+ tracks.forEach(track -> trackSubscribed(participant, track));
+ }
+ }
+ for (Participant participant : leftNow) {
+ fireLeft(participant);
+ }
+ }
+
+ private void onRemoteTrack(RemoteTrack track) {
+ if (state == RoomState.DISCONNECTED) {
+ return;
+ }
+
+ // LiveKit's stream ids are "|