blob: 6549823089f5403b0659086d649c7d706d21f96e [file] [log] [blame]
deadbeef24af6632017-02-01 21:53:09 -08001/*
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
11package org.webrtc;
12
13/** Java wrapper for a C++ DtmfSenderInterface. */
14public class DtmfSender {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020015 private long nativeDtmfSender;
deadbeef24af6632017-02-01 21:53:09 -080016
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 */
24 public boolean canInsertDtmf() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020025 checkDtmfSenderExists();
deadbeef24af6632017-02-01 21:53:09 -080026 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 */
46 public boolean insertDtmf(String tones, int duration, int interToneGap) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020047 checkDtmfSenderExists();
deadbeef24af6632017-02-01 21:53:09 -080048 return nativeInsertDtmf(nativeDtmfSender, tones, duration, interToneGap);
49 }
50
51 /**
52 * @return The tones remaining to be played out
53 */
54 public String tones() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020055 checkDtmfSenderExists();
deadbeef24af6632017-02-01 21:53:09 -080056 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 */
63 public int duration() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020064 checkDtmfSenderExists();
deadbeef24af6632017-02-01 21:53:09 -080065 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 */
73 public int interToneGap() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020074 checkDtmfSenderExists();
deadbeef24af6632017-02-01 21:53:09 -080075 return nativeInterToneGap(nativeDtmfSender);
76 }
77
78 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020079 checkDtmfSenderExists();
magjedb1c74532017-08-27 13:47:20 -070080 JniCommon.nativeReleaseRef(nativeDtmfSender);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +020081 nativeDtmfSender = 0;
82 }
83
84 private void checkDtmfSenderExists() {
85 if (nativeDtmfSender == 0) {
86 throw new IllegalStateException("DtmfSender has been disposed.");
87 }
deadbeef24af6632017-02-01 21:53:09 -080088 }
89
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010090 private static native boolean nativeCanInsertDtmf(long dtmfSender);
deadbeef24af6632017-02-01 21:53:09 -080091 private static native boolean nativeInsertDtmf(
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010092 long dtmfSender, String tones, int duration, int interToneGap);
93 private static native String nativeTones(long dtmfSender);
94 private static native int nativeDuration(long dtmfSender);
95 private static native int nativeInterToneGap(long dtmfSender);
deadbeef24af6632017-02-01 21:53:09 -080096};