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
2 changes: 1 addition & 1 deletion docs/guide/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ audioSender.sendTones("1234#"); // 100 ms
audioSender.sendTones("*99,1", Duration.ofMillis(200), Duration.ofMillis(100));
```

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.
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.

### Switching Cameras

Expand Down
7 changes: 4 additions & 3 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/HttpIce.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ private static int parameters(String field, int start, Map<String, String> param
if (i < field.length() && field.charAt(i) == '=') {
i++;

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

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

i++;
value = text.toString();
}
else {
while (i < field.length() && ";,".indexOf(field.charAt(i)) < 0) {
text.append(field.charAt(i++));
}
}

value = text.toString().trim();
value = text.toString().trim();
}
}

parameters.putIfAbsent(name, value);
Expand Down
4 changes: 3 additions & 1 deletion webrtc/src/main/java/dev/onvoid/webrtc/client/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public interface Sender {
*
* @return a future that completes once the tones have been sent. It fails
* with an {@link IllegalStateException} if the sender cannot send tones,
* because it sends video or the remote peer does not accept them.
* because it sends video or the remote peer does not accept them, and a
* few seconds after their planned end if WebRTC stopped sending them
* midway.
*/
CompletableFuture<Void> sendTones(String tones, Duration duration, Duration gap);

Expand Down
122 changes: 100 additions & 22 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/SenderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import dev.onvoid.webrtc.RTCDtmfSender;
Expand All @@ -41,12 +43,25 @@
*/
final class SenderImpl implements Sender {

/** How much later than planned the end of tones may come. */
static final Duration TONE_GRACE = Duration.ofSeconds(5);

/** The pause of a comma, WebRTC's default. */
private static final long COMMA_MILLIS = 2000;

private final PeerImpl peer;

private final MediaKind kind;

private volatile LocalTrackImpl track;

/**
* Identifies the tones WebRTC plays, written on the control executor. A
* tone event takes it on WebRTC's thread, when it happens, so that it
* still names its tones after later ones started.
*/
private volatile long toneRun;

// Confined to the peer's control executor.

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

/** Ends the pending tones should WebRTC never report their end. */
private ScheduledFuture<?> tonesDeadline;

// Confined to the peer's event executor.

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

if (pendingTones != null) {
pendingTones.completeExceptionally(new CancellationException("Replaced by later tones"));
}
// Set only now: the tones before play until this call stops
// them, and the first of these takes at least a millisecond.
long run = toneRun + 1;
toneRun = run;

finishTones(new CancellationException("Replaced by later tones"));

if (hasTone(tones)) {
long playMillis = playMillis(tones, durationMs, gapMs);

if (playMillis > 0) {
tonesDeadline = peer.rtc.timer.schedule(() -> peer.control.execute(() -> tonesOverdue(run)),
playMillis + TONE_GRACE.toMillis(), TimeUnit.MILLISECONDS);
pendingTones = sent;
}
else {
// Nothing to play, so no event would complete the future.
pendingTones = null;
sent.complete(null);
}
}
Expand Down Expand Up @@ -408,11 +432,7 @@ private void release() {
dtmf.dispose();
dtmf = null;
}
if (pendingTones != null) {
pendingTones.completeExceptionally(peer.isClosed()
? peer.closedError() : new IllegalStateException("The sender was removed"));
pendingTones = null;
}
finishTones(peer.isClosed() ? peer.closedError() : new IllegalStateException("The sender was removed"));

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

/** Called on a WebRTC thread, so it only posts. */
private void toneChanged(String tone) {
void toneChanged(String tone) {
String current = tone == null ? "" : tone;
long run = toneRun;

peer.control.execute(() -> {
// The handlers hear of the tone before the future of the tones
// completes, which also goes through the event executor.
peer.events.execute(() -> toneHandlers.accept(current));

if (current.isEmpty() && pendingTones != null) {
pendingTones.complete(null);
pendingTones = null;
if (!current.isEmpty()) {
peer.events.execute(() -> toneHandlers.accept(current));
}
else if (run == toneRun) {
tonesEnded();
}
// The end of tones replaced meanwhile says nothing of the new
// ones. Replaced a moment earlier, they would have had no end.
});
}

private void tonesEnded() {
// The handlers hear of the end before the future of the tones
// completes, which also goes through the event executor.
peer.events.execute(() -> toneHandlers.accept(""));
finishTones(null);
}

/**
* WebRTC gives up without an event when it can no longer send a tone,
* which it then keeps in its queue. With an empty queue only the event is
* missing.
*/
private void tonesOverdue(long run) {
if (run != toneRun || pendingTones == null) {
return;
}

if (dtmf.tones().isEmpty()) {
tonesEnded();
}
else {
finishTones(new IllegalStateException("WebRTC could no longer send the tones"));
}
}

/**
* Settles the pending tones, if any.
*
* @param error why they failed, or null if they have been sent.
*/
private void finishTones(Throwable error) {
if (pendingTones == null) {
return;
}

tonesDeadline.cancel(false);

if (error != null) {
pendingTones.completeExceptionally(error);
}
else {
pendingTones.complete(null);
}

pendingTones = null;
}

private void checkUsable() {
if (peer.isClosed()) {
throw peer.closedError();
Expand All @@ -466,14 +534,24 @@ private void checkUsable() {
}
}

private static boolean hasTone(String tones) {
/**
* @return how long WebRTC takes for the tones, 0 if they hold none.
*/
private static long playMillis(String tones, long durationMs, long gapMs) {
long millis = 0;

for (int i = 0; i < tones.length(); i++) {
if ("0123456789ABCDabcd#*,".indexOf(tones.charAt(i)) >= 0) {
return true;
char tone = tones.charAt(i);

if (tone == ',') {
millis += COMMA_MILLIS;
}
else if ("0123456789ABCDabcd#*".indexOf(tone) >= 0) {
millis += durationMs + gapMs;
}
}

return false;
return millis;
}

private CompletableFuture<Void> run(Runnable operation) {
Expand Down
20 changes: 20 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/client/HttpIceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ void linksRead() {
assertEquals("pw", servers.get(2).password);
}

@Test
void quotedSpacesKept() {
List<RTCIceServer> servers = HttpIce.iceServers(Arrays.asList(
"<turn:turn.example.net>; rel=\"ice-server\"; username=\"\\ alice\\\t\"; credential=\" secret \""));

assertEquals(1, servers.size(), () -> "Servers: " + servers);
assertEquals(" alice\t", servers.get(0).username);
assertEquals(" secret ", servers.get(0).password);
}

@Test
void tabsAroundValuesSkipped() {
List<RTCIceServer> servers = HttpIce.iceServers(Arrays.asList(
"<turn:turn.example.net>;\trel=\t\"ice-server\"\t;\tusername=\t\"alice\"\t;\tcredential \t= \tpw\t ; x=y"));

assertEquals(1, servers.size(), () -> "Servers: " + servers);
assertEquals("alice", servers.get(0).username);
assertEquals("pw", servers.get(0).password);
}

@Test
void fragmentOfOffer() {
// The m-line that tags the BUNDLE group stands for all, as RFC 9725
Expand Down
Loading
Loading