deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 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 { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 15 | private long nativeDtmfSender; |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 16 | |
| 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äki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 25 | checkDtmfSenderExists(); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 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 | */ |
| 46 | public boolean insertDtmf(String tones, int duration, int interToneGap) { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 47 | checkDtmfSenderExists(); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 48 | 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äki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 55 | checkDtmfSenderExists(); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 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 | */ |
| 63 | public int duration() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 64 | checkDtmfSenderExists(); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 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 | */ |
| 73 | public int interToneGap() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 74 | checkDtmfSenderExists(); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 75 | return nativeInterToneGap(nativeDtmfSender); |
| 76 | } |
| 77 | |
| 78 | public void dispose() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 79 | checkDtmfSenderExists(); |
magjed | b1c7453 | 2017-08-27 13:47:20 -0700 | [diff] [blame] | 80 | JniCommon.nativeReleaseRef(nativeDtmfSender); |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 81 | nativeDtmfSender = 0; |
| 82 | } |
| 83 | |
| 84 | private void checkDtmfSenderExists() { |
| 85 | if (nativeDtmfSender == 0) { |
| 86 | throw new IllegalStateException("DtmfSender has been disposed."); |
| 87 | } |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 90 | private static native boolean nativeCanInsertDtmf(long dtmfSender); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 91 | private static native boolean nativeInsertDtmf( |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 92 | 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); |
deadbeef | 24af663 | 2017-02-01 21:53:09 -0800 | [diff] [blame] | 96 | }; |