henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | package org.webrtc; |
| 12 | |
| 13 | import java.nio.ByteBuffer; |
| 14 | |
| 15 | /** Java wrapper for a C++ DataChannelInterface. */ |
| 16 | public class DataChannel { |
| 17 | /** Java wrapper for WebIDL RTCDataChannel. */ |
| 18 | public static class Init { |
| 19 | public boolean ordered = true; |
| 20 | // Optional unsigned short in WebIDL, -1 means unspecified. |
| 21 | public int maxRetransmitTimeMs = -1; |
| 22 | // Optional unsigned short in WebIDL, -1 means unspecified. |
| 23 | public int maxRetransmits = -1; |
| 24 | public String protocol = ""; |
Sami Kalliomäki | 3d50a31 | 2018-09-11 11:11:47 +0200 | [diff] [blame] | 25 | public boolean negotiated; |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 26 | // Optional unsigned short in WebIDL, -1 means unspecified. |
| 27 | public int id = -1; |
| 28 | |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 29 | @CalledByNative("Init") |
| 30 | boolean getOrdered() { |
| 31 | return ordered; |
| 32 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 33 | |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 34 | @CalledByNative("Init") |
| 35 | int getMaxRetransmitTimeMs() { |
| 36 | return maxRetransmitTimeMs; |
| 37 | } |
| 38 | |
| 39 | @CalledByNative("Init") |
| 40 | int getMaxRetransmits() { |
| 41 | return maxRetransmits; |
| 42 | } |
| 43 | |
| 44 | @CalledByNative("Init") |
| 45 | String getProtocol() { |
| 46 | return protocol; |
| 47 | } |
| 48 | |
| 49 | @CalledByNative("Init") |
| 50 | boolean getNegotiated() { |
| 51 | return negotiated; |
| 52 | } |
| 53 | |
| 54 | @CalledByNative("Init") |
| 55 | int getId() { |
| 56 | return id; |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | /** Java version of C++ DataBuffer. The atom of data in a DataChannel. */ |
| 61 | public static class Buffer { |
| 62 | /** The underlying data. */ |
| 63 | public final ByteBuffer data; |
| 64 | |
| 65 | /** |
| 66 | * Indicates whether |data| contains UTF-8 text or "binary data" |
| 67 | * (i.e. anything else). |
| 68 | */ |
| 69 | public final boolean binary; |
| 70 | |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 71 | @CalledByNative("Buffer") |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 72 | public Buffer(ByteBuffer data, boolean binary) { |
| 73 | this.data = data; |
| 74 | this.binary = binary; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** Java version of C++ DataChannelObserver. */ |
| 79 | public interface Observer { |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 80 | /** The data channel's bufferedAmount has changed. */ |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 81 | @CalledByNative("Observer") public void onBufferedAmountChange(long previousAmount); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 82 | /** The data channel state has changed. */ |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 83 | @CalledByNative("Observer") public void onStateChange(); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 84 | /** |
| 85 | * A data buffer was successfully received. NOTE: |buffer.data| will be |
| 86 | * freed once this function returns so callers who want to use the data |
| 87 | * asynchronously must make sure to copy it first. |
| 88 | */ |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 89 | @CalledByNative("Observer") public void onMessage(Buffer buffer); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** Keep in sync with DataChannelInterface::DataState. */ |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 93 | public enum State { |
| 94 | CONNECTING, |
| 95 | OPEN, |
| 96 | CLOSING, |
| 97 | CLOSED; |
| 98 | |
| 99 | @CalledByNative("State") |
| 100 | static State fromNativeIndex(int nativeIndex) { |
| 101 | return values()[nativeIndex]; |
| 102 | } |
| 103 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 104 | |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 105 | private long nativeDataChannel; |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 106 | private long nativeObserver; |
| 107 | |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 108 | @CalledByNative |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 109 | public DataChannel(long nativeDataChannel) { |
| 110 | this.nativeDataChannel = nativeDataChannel; |
| 111 | } |
| 112 | |
| 113 | /** Register |observer|, replacing any previously-registered observer. */ |
| 114 | public void registerObserver(Observer observer) { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 115 | checkDataChannelExists(); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 116 | if (nativeObserver != 0) { |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 117 | nativeUnregisterObserver(nativeObserver); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 118 | } |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 119 | nativeObserver = nativeRegisterObserver(observer); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 120 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 121 | |
| 122 | /** Unregister the (only) observer. */ |
| 123 | public void unregisterObserver() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 124 | checkDataChannelExists(); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 125 | nativeUnregisterObserver(nativeObserver); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 126 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 127 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 128 | public String label() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 129 | checkDataChannelExists(); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 130 | return nativeLabel(); |
| 131 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 132 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 133 | public int id() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 134 | checkDataChannelExists(); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 135 | return nativeId(); |
| 136 | } |
deadbeef | ee8ad2b | 2016-11-01 14:59:00 -0700 | [diff] [blame] | 137 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 138 | public State state() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 139 | checkDataChannelExists(); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 140 | return nativeState(); |
| 141 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Return the number of bytes of application data (UTF-8 text and binary data) |
| 145 | * that have been queued using SendBuffer but have not yet been transmitted |
| 146 | * to the network. |
| 147 | */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 148 | public long bufferedAmount() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 149 | checkDataChannelExists(); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 150 | return nativeBufferedAmount(); |
| 151 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 152 | |
| 153 | /** Close the channel. */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 154 | public void close() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 155 | checkDataChannelExists(); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 156 | nativeClose(); |
| 157 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 158 | |
| 159 | /** Send |data| to the remote peer; return success. */ |
| 160 | public boolean send(Buffer buffer) { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 161 | checkDataChannelExists(); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 162 | // TODO(fischman): this could be cleverer about avoiding copies if the |
| 163 | // ByteBuffer is direct and/or is backed by an array. |
| 164 | byte[] data = new byte[buffer.data.remaining()]; |
| 165 | buffer.data.get(data); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 166 | return nativeSend(data, buffer.binary); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 167 | } |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 168 | |
| 169 | /** Dispose of native resources attached to this channel. */ |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 170 | public void dispose() { |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 171 | checkDataChannelExists(); |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 172 | JniCommon.nativeReleaseRef(nativeDataChannel); |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 173 | nativeDataChannel = 0; |
Magnus Jedvert | 7bd6ccc | 2017-11-24 14:42:47 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @CalledByNative |
| 177 | long getNativeDataChannel() { |
| 178 | return nativeDataChannel; |
| 179 | } |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 180 | |
Sami Kalliomäki | ee05e90 | 2018-09-28 14:38:21 +0200 | [diff] [blame] | 181 | private void checkDataChannelExists() { |
| 182 | if (nativeDataChannel == 0) { |
| 183 | throw new IllegalStateException("DataChannel has been disposed."); |
| 184 | } |
| 185 | } |
| 186 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 187 | private native long nativeRegisterObserver(Observer observer); |
| 188 | private native void nativeUnregisterObserver(long observer); |
| 189 | private native String nativeLabel(); |
| 190 | private native int nativeId(); |
| 191 | private native State nativeState(); |
| 192 | private native long nativeBufferedAmount(); |
| 193 | private native void nativeClose(); |
| 194 | private native boolean nativeSend(byte[] data, boolean binary); |
henrike@webrtc.org | 723d683 | 2013-07-12 16:04:50 +0000 | [diff] [blame] | 195 | }; |