Skip to content

Commit fa75fd7

Browse files
committed
fix: let a candidate held for its description free the operation chain
A candidate that came before the remote description kept its operation open, so the description queued behind it waited for the operation timeout. Holding it now finishes the operation; a held candidate WebRTC rejects later reaches the error handler.
1 parent 4e2447e commit fa75fd7

5 files changed

Lines changed: 155 additions & 36 deletions

File tree

‎docs/guide/client-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ viewer.negotiator().createOffer(Gathering.COMPLETE)
807807
| `createOffer()`, `createOffer(Gathering)` | Creates the local offer and applies it. |
808808
| `answer(offer)`, `answer(offer, Gathering)` | Applies the remote offer, then creates the answer and applies it. |
809809
| `applyAnswer(answer)` | Applies the remote answer to the local offer. |
810-
| `addCandidate(candidate)` | Adds a remote candidate, or holds it until there is a remote description to add it to. |
810+
| `addCandidate(candidate)` | Adds a remote candidate, or holds it until there is a remote description to add it to. The future completes once the candidate is added or held; a held candidate that WebRTC rejects later reaches the error handler. |
811811

812812
A manual peer with a `Signaling` still sends its local candidates through it. `peer.negotiator()` throws an `IllegalStateException` on a peer that negotiates automatically.
813813

‎webrtc/src/main/java/dev/onvoid/webrtc/client/NegotiationSteps.java‎

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.ScheduledExecutorService;
2525
import java.util.concurrent.TimeUnit;
2626
import java.util.function.BooleanSupplier;
27+
import java.util.function.Consumer;
2728
import java.util.function.Supplier;
2829

2930
import dev.onvoid.webrtc.AddIceCandidateObserver;
@@ -59,8 +60,10 @@ final class NegotiationSteps {
5960

6061
private final BooleanSupplier ignoringOffer;
6162

63+
private final Consumer<WebRtcException> candidateRejected;
64+
6265
/** The remote candidates that came before a remote description. */
63-
private final List<HeldCandidate> heldCandidates = new ArrayList<>();
66+
private final List<RTCIceCandidate> heldCandidates = new ArrayList<>();
6467

6568
private final List<CompletableFuture<Void>> gatheringWaiters = new ArrayList<>();
6669

@@ -73,15 +76,19 @@ final class NegotiationSteps {
7376
* @param gatheringTimeout how long to wait for gathering to complete.
7477
* @param ignoringOffer whether the peer ignored a remote offer, whose
7578
* candidates then fail to be added, harmlessly.
79+
* @param candidateRejected told of a held candidate WebRTC rejected, whose
80+
* operation is over by then.
7681
*/
7782
NegotiationSteps(SerialExecutor control, ScheduledExecutorService timer, OperationChain chain,
78-
Supplier<RTCPeerConnection> connection, Duration gatheringTimeout, BooleanSupplier ignoringOffer) {
83+
Supplier<RTCPeerConnection> connection, Duration gatheringTimeout, BooleanSupplier ignoringOffer,
84+
Consumer<WebRtcException> candidateRejected) {
7985
this.control = control;
8086
this.timer = timer;
8187
this.chain = chain;
8288
this.connection = connection;
8389
this.gatheringTimeout = gatheringTimeout;
8490
this.ignoringOffer = ignoringOffer;
91+
this.candidateRejected = candidateRejected;
8592
}
8693

8794
/**
@@ -140,18 +147,17 @@ CompletableFuture<Void> rollback() {
140147

141148
/**
142149
* Adds a remote candidate, or holds it while there is no remote
143-
* description to add it to.
150+
* description to add it to. Holding it finishes the operation at once,
151+
* since the description may be queued right behind it.
144152
*/
145153
CompletableFuture<Void> addRemoteCandidate(RTCIceCandidate candidate) {
146-
CompletableFuture<Void> future = chain.step();
147-
148154
if (connection.get().getRemoteDescription() == null) {
149-
heldCandidates.add(new HeldCandidate(candidate, future));
150-
}
151-
else {
152-
addCandidate(candidate, future);
155+
heldCandidates.add(candidate);
156+
return CompletableFuture.completedFuture(null);
153157
}
154158

159+
CompletableFuture<Void> future = chain.step();
160+
addCandidate(candidate, future);
155161
return future;
156162
}
157163

@@ -160,11 +166,19 @@ CompletableFuture<Void> addRemoteCandidate(RTCIceCandidate candidate) {
160166
* description.
161167
*/
162168
void releaseHeldCandidates() {
163-
List<HeldCandidate> held = new ArrayList<>(heldCandidates);
169+
List<RTCIceCandidate> held = new ArrayList<>(heldCandidates);
164170
heldCandidates.clear();
165171

166-
for (HeldCandidate candidate : held) {
167-
addCandidate(candidate.candidate, candidate.future);
172+
for (RTCIceCandidate candidate : held) {
173+
CompletableFuture<Void> added = new CompletableFuture<>();
174+
175+
added.whenComplete((value, error) -> {
176+
if (error != null) {
177+
candidateRejected.accept(PeerImpl.asWebRtcException(error, "Adding a remote candidate failed"));
178+
}
179+
});
180+
181+
addCandidate(candidate, added);
168182
}
169183
}
170184

@@ -208,18 +222,15 @@ void gatheringComplete() {
208222
}
209223

210224
/**
211-
* Fails the waiting for gathering and the candidates held, once the peer
212-
* is closing.
225+
* Fails the waiting for gathering and drops the candidates held, once the
226+
* peer is closing.
213227
*/
214228
void close(PeerClosedException error) {
215229
for (CompletableFuture<Void> waiter : gatheringWaiters) {
216230
waiter.completeExceptionally(error);
217231
}
218232
gatheringWaiters.clear();
219233

220-
for (HeldCandidate candidate : heldCandidates) {
221-
candidate.future.completeExceptionally(error);
222-
}
223234
heldCandidates.clear();
224235
}
225236

@@ -277,19 +288,4 @@ public void onFailure(String error) {
277288
}
278289
};
279290
}
280-
281-
282-
283-
private static final class HeldCandidate {
284-
285-
final RTCIceCandidate candidate;
286-
287-
final CompletableFuture<Void> future;
288-
289-
290-
HeldCandidate(RTCIceCandidate candidate, CompletableFuture<Void> future) {
291-
this.candidate = candidate;
292-
this.future = future;
293-
}
294-
}
295291
}

‎webrtc/src/main/java/dev/onvoid/webrtc/client/Negotiator.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ public interface Negotiator {
7474

7575
/**
7676
* Adds a remote candidate. One that arrives before the remote description
77-
* is held until the description is applied.
77+
* is held until the description is applied, and the steps behind it go
78+
* on. If WebRTC then rejects it, the error handlers of the peer hear of it.
7879
*
7980
* @param candidate the remote candidate.
8081
*
81-
* @return a future that completes once the candidate is added.
82+
* @return a future that completes once the candidate is added or held.
8283
*/
8384
CompletableFuture<Void> addCandidate(RTCIceCandidate candidate);
8485

‎webrtc/src/main/java/dev/onvoid/webrtc/client/PeerImpl.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ final class PeerImpl implements Peer {
213213
this.errorHandlers = new Handlers<>(rtc::reportError);
214214
this.chain = new OperationChain(control, rtc.timer, rtc.operationTimeout, name, new ChainHost());
215215
this.steps = new NegotiationSteps(control, rtc.timer, chain, () -> connection, options.gatheringTimeout,
216-
this::ignoresOffer);
216+
this::ignoresOffer, error -> {
217+
if (!closed) {
218+
deliverError(error);
219+
}
220+
});
217221
this.automatic = new PerfectNegotiation(this, options, chain, steps);
218222

219223
rtc.track(connected, events);

‎webrtc/src/test/java/dev/onvoid/webrtc/client/NegotiationTimeoutTests.java‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424

2525
import java.net.http.HttpClient;
2626
import java.time.Duration;
27+
import java.util.List;
2728
import java.util.concurrent.CompletableFuture;
29+
import java.util.concurrent.CopyOnWriteArrayList;
2830
import java.util.concurrent.CountDownLatch;
2931
import java.util.concurrent.ExecutionException;
3032
import java.util.concurrent.TimeUnit;
3133
import java.util.concurrent.TimeoutException;
3234
import java.util.concurrent.atomic.AtomicBoolean;
3335
import java.util.concurrent.atomic.AtomicReference;
36+
import java.util.function.Consumer;
3437
import java.util.function.UnaryOperator;
3538

39+
import dev.onvoid.webrtc.RTCIceCandidate;
3640
import dev.onvoid.webrtc.RTCSessionDescription;
3741
import dev.onvoid.webrtc.RTCSignalingState;
3842

@@ -42,6 +46,10 @@ class NegotiationTimeoutTests extends ClientTestBase {
4246

4347
private static final Duration OPERATION_TIMEOUT = Duration.ofSeconds(37);
4448

49+
private static final String CANDIDATE_ADDRESS = "203.0.113.7";
50+
51+
private static final String CANDIDATE = "candidate:1 1 udp 2122260223 " + CANDIDATE_ADDRESS + " 49203 typ host";
52+
4553
@Test
4654
void lateRestartAnswerDiscarded() throws Exception {
4755
lateRestartAnswer(false);
@@ -162,6 +170,94 @@ void restartCleanupPrecedesEvents() throws Exception {
162170
}
163171
}
164172

173+
@Test
174+
void candidateBeforeOffer() throws Exception {
175+
ManualExecutor control = new ManualExecutor();
176+
ManualExecutor events = new ManualExecutor();
177+
ManualTimer timer = new ManualTimer();
178+
WebRtc client = runtime(control, events, timer);
179+
try {
180+
RemoteSignaling signaling = new RemoteSignaling();
181+
// Nothing to receive, so that it makes no offer of its own to collide.
182+
PeerImpl peer = (PeerImpl) drive(CompletableFuture.supplyAsync(() -> client.createPeer(
183+
PeerOptions.builder().signaling(signaling).build())), control, events);
184+
185+
// The timeout never fires here, so only the candidate letting the
186+
// chain go on can bring the offer to its answer.
187+
signaling.inbound.accept(SignalMessage.of(new RTCIceCandidate("0", 0, CANDIDATE)));
188+
signaling.inbound.accept(SignalMessage.of(remoteOffer()));
189+
control.runUntil(() -> {
190+
events.runAll();
191+
return signaling.sent.stream().anyMatch(message -> message.type() == SignalMessage.Type.ANSWER);
192+
}, TIMEOUT_SECONDS);
193+
194+
assertTrue(remoteSdp(peer, control, events).contains(CANDIDATE_ADDRESS));
195+
assertEquals(0, timer.scheduled(OPERATION_TIMEOUT));
196+
}
197+
finally {
198+
close(client, control, events);
199+
}
200+
}
201+
202+
@Test
203+
void manualCandidateBeforeOffer() throws Exception {
204+
ManualExecutor control = new ManualExecutor();
205+
ManualExecutor events = new ManualExecutor();
206+
ManualTimer timer = new ManualTimer();
207+
WebRtc client = runtime(control, events, timer);
208+
try {
209+
PeerImpl peer = manualPeer(client, control, events);
210+
211+
// Done once held, so a caller may wait for it before it answers.
212+
drive(peer.negotiator().addCandidate(new RTCIceCandidate("0", 0, CANDIDATE)), control, events);
213+
drive(peer.negotiator().answer(remoteOffer()), control, events);
214+
215+
assertTrue(remoteSdp(peer, control, events).contains(CANDIDATE_ADDRESS));
216+
assertEquals(0, timer.scheduled(OPERATION_TIMEOUT));
217+
}
218+
finally {
219+
close(client, control, events);
220+
}
221+
}
222+
223+
@Test
224+
void rejectedHeldCandidateReported() throws Exception {
225+
ManualExecutor control = new ManualExecutor();
226+
ManualExecutor events = new ManualExecutor();
227+
WebRtc client = runtime(control, events, new ManualTimer());
228+
try {
229+
PeerImpl peer = manualPeer(client, control, events);
230+
231+
// The offer has no m-line of this mid.
232+
drive(peer.negotiator().addCandidate(new RTCIceCandidate("9", 9, CANDIDATE)), control, events);
233+
drive(peer.negotiator().answer(remoteOffer()), control, events);
234+
235+
control.runUntil(() -> {
236+
events.runAll();
237+
return !errors.isEmpty();
238+
}, TIMEOUT_SECONDS);
239+
240+
assertInstanceOf(NegotiationException.class, errors.remove(0));
241+
}
242+
finally {
243+
close(client, control, events);
244+
}
245+
}
246+
247+
private PeerImpl manualPeer(WebRtc client, ManualExecutor control, ManualExecutor events) throws Exception {
248+
return (PeerImpl) drive(CompletableFuture.supplyAsync(() -> client.createPeer(PeerOptions.builder()
249+
.negotiation(Negotiation.MANUAL).receive(MediaKind.AUDIO).build())), control, events);
250+
}
251+
252+
private RTCSessionDescription remoteOffer() throws Exception {
253+
return await(rtc.createPeer(PeerOptions.builder().negotiation(Negotiation.MANUAL)
254+
.receive(MediaKind.AUDIO).build()).negotiator().createOffer());
255+
}
256+
257+
private static String remoteSdp(PeerImpl peer, ManualExecutor control, ManualExecutor events) throws Exception {
258+
return drive(peer.withNative(connection -> connection.getRemoteDescription().sdp), control, events);
259+
}
260+
165261
private WebRtc runtime(ManualExecutor control, ManualExecutor events, ManualTimer timer) {
166262
return WebRtc.builder().audio(AudioMode.HEADLESS).controlPool(control).eventExecutor(events)
167263
.timer(timer).operationTimeout(OPERATION_TIMEOUT).errorHandler(errors::add).build();
@@ -199,4 +295,26 @@ private static <T> T drive(CompletableFuture<T> future, ManualExecutor control,
199295
private static void close(WebRtc client, ManualExecutor control, ManualExecutor events) throws Exception {
200296
drive(client.closeAsync(), control, events);
201297
}
298+
299+
300+
301+
/** The signaling of a peer, with the test as the remote side. */
302+
private static final class RemoteSignaling implements Signaling {
303+
304+
final List<SignalMessage> sent = new CopyOnWriteArrayList<>();
305+
306+
volatile Consumer<SignalMessage> inbound;
307+
308+
309+
@Override
310+
public void send(SignalMessage message) {
311+
sent.add(message);
312+
}
313+
314+
@Override
315+
public Subscription attach(Consumer<SignalMessage> inbound) {
316+
this.inbound = inbound;
317+
return () -> { };
318+
}
319+
}
202320
}

0 commit comments

Comments
 (0)