Skip to content

Commit 714d0ab

Browse files
authored
fix: keep TURN credentials intact and settle DTMF tones reliably (#545)
* fix: keep quoted Link parameter values intact * fix: tie DTMF events to their tones and bound their end
1 parent 9eb8a62 commit 714d0ab

6 files changed

Lines changed: 281 additions & 27 deletions

File tree

‎docs/guide/client-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ audioSender.sendTones("1234#"); // 100 ms
478478
audioSender.sendTones("*99,1", Duration.ofMillis(200), Duration.ofMillis(100));
479479
```
480480

481-
The characters 0 to 9, A to D, `#` and `*` are tones, and a comma is a pause of 2 seconds. Tones still queued from an earlier call are replaced, and that call's future is cancelled. A video sender, or one whose remote peer accepts no telephone events, fails the future with an `IllegalStateException`. The [DTMF Sender](/guide/audio/dtmf-sender) guide has the details.
481+
The characters 0 to 9, A to D, `#` and `*` are tones, and a comma is a pause of 2 seconds. Tones still queued from an earlier call are replaced, and that call's future is cancelled. A video sender, or one whose remote peer accepts no telephone events, fails the future with an `IllegalStateException`. So do tones WebRTC stops sending midway, for example because the sender was paused with `configure`, a few seconds after their planned end. The [DTMF Sender](/guide/audio/dtmf-sender) guide has the details.
482482

483483
### Switching Cameras
484484

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ private static int parameters(String field, int start, Map<String, String> param
596596
if (i < field.length() && field.charAt(i) == '=') {
597597
i++;
598598

599-
while (i < field.length() && field.charAt(i) == ' ') {
599+
while (i < field.length() && " \t".indexOf(field.charAt(i)) >= 0) {
600600
i++;
601601
}
602602

@@ -614,14 +614,15 @@ private static int parameters(String field, int start, Map<String, String> param
614614
}
615615

616616
i++;
617+
value = text.toString();
617618
}
618619
else {
619620
while (i < field.length() && ";,".indexOf(field.charAt(i)) < 0) {
620621
text.append(field.charAt(i++));
621622
}
622-
}
623623

624-
value = text.toString().trim();
624+
value = text.toString().trim();
625+
}
625626
}
626627

627628
parameters.putIfAbsent(name, value);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public interface Sender {
102102
*
103103
* @return a future that completes once the tones have been sent. It fails
104104
* with an {@link IllegalStateException} if the sender cannot send tones,
105-
* because it sends video or the remote peer does not accept them.
105+
* because it sends video or the remote peer does not accept them, and a
106+
* few seconds after their planned end if WebRTC stopped sending them
107+
* midway.
106108
*/
107109
CompletableFuture<Void> sendTones(String tones, Duration duration, Duration gap);
108110

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

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.List;
2323
import java.util.concurrent.CancellationException;
2424
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.ScheduledFuture;
26+
import java.util.concurrent.TimeUnit;
2527
import java.util.function.Consumer;
2628

2729
import dev.onvoid.webrtc.RTCDtmfSender;
@@ -41,12 +43,25 @@
4143
*/
4244
final class SenderImpl implements Sender {
4345

46+
/** How much later than planned the end of tones may come. */
47+
static final Duration TONE_GRACE = Duration.ofSeconds(5);
48+
49+
/** The pause of a comma, WebRTC's default. */
50+
private static final long COMMA_MILLIS = 2000;
51+
4452
private final PeerImpl peer;
4553

4654
private final MediaKind kind;
4755

4856
private volatile LocalTrackImpl track;
4957

58+
/**
59+
* Identifies the tones WebRTC plays, written on the control executor. A
60+
* tone event takes it on WebRTC's thread, when it happens, so that it
61+
* still names its tones after later ones started.
62+
*/
63+
private volatile long toneRun;
64+
5065
// Confined to the peer's control executor.
5166

5267
private RTCPeerConnection connection;
@@ -62,6 +77,9 @@ final class SenderImpl implements Sender {
6277
/** Completes once the tones sent last have been played. */
6378
private CompletableFuture<Void> pendingTones;
6479

80+
/** Ends the pending tones should WebRTC never report their end. */
81+
private ScheduledFuture<?> tonesDeadline;
82+
6583
// Confined to the peer's event executor.
6684

6785
private final Handlers<String> toneHandlers;
@@ -268,16 +286,22 @@ public CompletableFuture<Void> sendTones(String tones, Duration duration, Durati
268286
throw new IllegalStateException("WebRTC refused the tones \"" + tones + "\"");
269287
}
270288

271-
if (pendingTones != null) {
272-
pendingTones.completeExceptionally(new CancellationException("Replaced by later tones"));
273-
}
289+
// Set only now: the tones before play until this call stops
290+
// them, and the first of these takes at least a millisecond.
291+
long run = toneRun + 1;
292+
toneRun = run;
293+
294+
finishTones(new CancellationException("Replaced by later tones"));
274295

275-
if (hasTone(tones)) {
296+
long playMillis = playMillis(tones, durationMs, gapMs);
297+
298+
if (playMillis > 0) {
299+
tonesDeadline = peer.rtc.timer.schedule(() -> peer.control.execute(() -> tonesOverdue(run)),
300+
playMillis + TONE_GRACE.toMillis(), TimeUnit.MILLISECONDS);
276301
pendingTones = sent;
277302
}
278303
else {
279304
// Nothing to play, so no event would complete the future.
280-
pendingTones = null;
281305
sent.complete(null);
282306
}
283307
}
@@ -408,11 +432,7 @@ private void release() {
408432
dtmf.dispose();
409433
dtmf = null;
410434
}
411-
if (pendingTones != null) {
412-
pendingTones.completeExceptionally(peer.isClosed()
413-
? peer.closedError() : new IllegalStateException("The sender was removed"));
414-
pendingTones = null;
415-
}
435+
finishTones(peer.isClosed() ? peer.closedError() : new IllegalStateException("The sender was removed"));
416436

417437
if (sender != null) {
418438
sender.dispose();
@@ -439,21 +459,69 @@ private LocalTrackImpl checkTrack(LocalTrack replacement) {
439459
}
440460

441461
/** Called on a WebRTC thread, so it only posts. */
442-
private void toneChanged(String tone) {
462+
void toneChanged(String tone) {
443463
String current = tone == null ? "" : tone;
464+
long run = toneRun;
444465

445466
peer.control.execute(() -> {
446-
// The handlers hear of the tone before the future of the tones
447-
// completes, which also goes through the event executor.
448-
peer.events.execute(() -> toneHandlers.accept(current));
449-
450-
if (current.isEmpty() && pendingTones != null) {
451-
pendingTones.complete(null);
452-
pendingTones = null;
467+
if (!current.isEmpty()) {
468+
peer.events.execute(() -> toneHandlers.accept(current));
469+
}
470+
else if (run == toneRun) {
471+
tonesEnded();
453472
}
473+
// The end of tones replaced meanwhile says nothing of the new
474+
// ones. Replaced a moment earlier, they would have had no end.
454475
});
455476
}
456477

478+
private void tonesEnded() {
479+
// The handlers hear of the end before the future of the tones
480+
// completes, which also goes through the event executor.
481+
peer.events.execute(() -> toneHandlers.accept(""));
482+
finishTones(null);
483+
}
484+
485+
/**
486+
* WebRTC gives up without an event when it can no longer send a tone,
487+
* which it then keeps in its queue. With an empty queue only the event is
488+
* missing.
489+
*/
490+
private void tonesOverdue(long run) {
491+
if (run != toneRun || pendingTones == null) {
492+
return;
493+
}
494+
495+
if (dtmf.tones().isEmpty()) {
496+
tonesEnded();
497+
}
498+
else {
499+
finishTones(new IllegalStateException("WebRTC could no longer send the tones"));
500+
}
501+
}
502+
503+
/**
504+
* Settles the pending tones, if any.
505+
*
506+
* @param error why they failed, or null if they have been sent.
507+
*/
508+
private void finishTones(Throwable error) {
509+
if (pendingTones == null) {
510+
return;
511+
}
512+
513+
tonesDeadline.cancel(false);
514+
515+
if (error != null) {
516+
pendingTones.completeExceptionally(error);
517+
}
518+
else {
519+
pendingTones.complete(null);
520+
}
521+
522+
pendingTones = null;
523+
}
524+
457525
private void checkUsable() {
458526
if (peer.isClosed()) {
459527
throw peer.closedError();
@@ -466,14 +534,24 @@ private void checkUsable() {
466534
}
467535
}
468536

469-
private static boolean hasTone(String tones) {
537+
/**
538+
* @return how long WebRTC takes for the tones, 0 if they hold none.
539+
*/
540+
private static long playMillis(String tones, long durationMs, long gapMs) {
541+
long millis = 0;
542+
470543
for (int i = 0; i < tones.length(); i++) {
471-
if ("0123456789ABCDabcd#*,".indexOf(tones.charAt(i)) >= 0) {
472-
return true;
544+
char tone = tones.charAt(i);
545+
546+
if (tone == ',') {
547+
millis += COMMA_MILLIS;
548+
}
549+
else if ("0123456789ABCDabcd#*".indexOf(tone) >= 0) {
550+
millis += durationMs + gapMs;
473551
}
474552
}
475553

476-
return false;
554+
return millis;
477555
}
478556

479557
private CompletableFuture<Void> run(Runnable operation) {

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,26 @@ void linksRead() {
8181
assertEquals("pw", servers.get(2).password);
8282
}
8383

84+
@Test
85+
void quotedSpacesKept() {
86+
List<RTCIceServer> servers = HttpIce.iceServers(Arrays.asList(
87+
"<turn:turn.example.net>; rel=\"ice-server\"; username=\"\\ alice\\\t\"; credential=\" secret \""));
88+
89+
assertEquals(1, servers.size(), () -> "Servers: " + servers);
90+
assertEquals(" alice\t", servers.get(0).username);
91+
assertEquals(" secret ", servers.get(0).password);
92+
}
93+
94+
@Test
95+
void tabsAroundValuesSkipped() {
96+
List<RTCIceServer> servers = HttpIce.iceServers(Arrays.asList(
97+
"<turn:turn.example.net>;\trel=\t\"ice-server\"\t;\tusername=\t\"alice\"\t;\tcredential \t= \tpw\t ; x=y"));
98+
99+
assertEquals(1, servers.size(), () -> "Servers: " + servers);
100+
assertEquals("alice", servers.get(0).username);
101+
assertEquals("pw", servers.get(0).password);
102+
}
103+
84104
@Test
85105
void fragmentOfOffer() {
86106
// The m-line that tags the BUNDLE group stands for all, as RFC 9725

0 commit comments

Comments
 (0)