1 /*
2  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 package org.webrtc;
12 
13 /** Java wrapper for a C++ DtmfSenderInterface. */
14 public class DtmfSender {
15   private long nativeDtmfSender;
16 
DtmfSender(long nativeDtmfSender)17   public DtmfSender(long nativeDtmfSender) {
18     this.nativeDtmfSender = nativeDtmfSender;
19   }
20 
21   /**
22    * @return true if this DtmfSender is capable of sending DTMF. Otherwise false.
23    */
canInsertDtmf()24   public boolean canInsertDtmf() {
25     checkDtmfSenderExists();
26     return nativeCanInsertDtmf(nativeDtmfSender);
27   }
28 
29   /**
30    * Queues a task that sends the provided DTMF tones.
31    * <p>
32    * If insertDtmf is called on the same object while an existing task for this
33    * object to generate DTMF is still running, the previous task is canceled.
34    *
35    * @param tones        This parameter is treated as a series of characters. The characters 0
36    *                     through 9, A through D, #, and * generate the associated DTMF tones. The
37    *                     characters a to d are equivalent to A to D. The character ',' indicates a
38    *                     delay of 2 seconds before processing the next character in the tones
39    *                     parameter. Unrecognized characters are ignored.
40    * @param duration     Indicates the duration in ms to use for each character passed in the tones
41    *                     parameter. The duration cannot be more than 6000 or less than 70.
42    * @param interToneGap Indicates the gap between tones in ms. Must be at least 50 ms but should be
43    *                     as short as possible.
44    * @return             true on success and false on failure.
45    */
insertDtmf(String tones, int duration, int interToneGap)46   public boolean insertDtmf(String tones, int duration, int interToneGap) {
47     checkDtmfSenderExists();
48     return nativeInsertDtmf(nativeDtmfSender, tones, duration, interToneGap);
49   }
50 
51   /**
52    * @return The tones remaining to be played out
53    */
tones()54   public String tones() {
55     checkDtmfSenderExists();
56     return nativeTones(nativeDtmfSender);
57   }
58 
59   /**
60    * @return The current tone duration value in ms. This value will be the value last set via the
61    *         insertDtmf() method, or the default value of 100 ms if insertDtmf() was never called.
62    */
duration()63   public int duration() {
64     checkDtmfSenderExists();
65     return nativeDuration(nativeDtmfSender);
66   }
67 
68   /**
69    * @return The current value of the between-tone gap in ms. This value will be the value last set
70    *         via the insertDtmf() method, or the default value of 50 ms if insertDtmf() was never
71    *         called.
72    */
interToneGap()73   public int interToneGap() {
74     checkDtmfSenderExists();
75     return nativeInterToneGap(nativeDtmfSender);
76   }
77 
dispose()78   public void dispose() {
79     checkDtmfSenderExists();
80     JniCommon.nativeReleaseRef(nativeDtmfSender);
81     nativeDtmfSender = 0;
82   }
83 
checkDtmfSenderExists()84   private void checkDtmfSenderExists() {
85     if (nativeDtmfSender == 0) {
86       throw new IllegalStateException("DtmfSender has been disposed.");
87     }
88   }
89 
nativeCanInsertDtmf(long dtmfSender)90   private static native boolean nativeCanInsertDtmf(long dtmfSender);
nativeInsertDtmf( long dtmfSender, String tones, int duration, int interToneGap)91   private static native boolean nativeInsertDtmf(
92       long dtmfSender, String tones, int duration, int interToneGap);
nativeTones(long dtmfSender)93   private static native String nativeTones(long dtmfSender);
nativeDuration(long dtmfSender)94   private static native int nativeDuration(long dtmfSender);
nativeInterToneGap(long dtmfSender)95   private static native int nativeInterToneGap(long dtmfSender);
96 };
97