2222import java .util .List ;
2323import java .util .concurrent .CancellationException ;
2424import java .util .concurrent .CompletableFuture ;
25+ import java .util .concurrent .ScheduledFuture ;
26+ import java .util .concurrent .TimeUnit ;
2527import java .util .function .Consumer ;
2628
2729import dev .onvoid .webrtc .RTCDtmfSender ;
4143 */
4244final 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 ) {
0 commit comments