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