blob: f51dab97aff04a88cceae51df7129f40b709fade [file] [log] [blame]
henrike@webrtc.org723d6832013-07-12 16:04:50 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org723d6832013-07-12 16:04:50 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.org723d6832013-07-12 16:04:50 +00009 */
10
11package org.webrtc;
12
13import java.nio.ByteBuffer;
14
15/** Java wrapper for a C++ DataChannelInterface. */
16public 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äki3d50a312018-09-11 11:11:47 +020025 public boolean negotiated;
henrike@webrtc.org723d6832013-07-12 16:04:50 +000026 // Optional unsigned short in WebIDL, -1 means unspecified.
27 public int id = -1;
28
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010029 @CalledByNative("Init")
30 boolean getOrdered() {
31 return ordered;
32 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +000033
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010034 @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.org723d6832013-07-12 16:04:50 +000057 }
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 Jedvert7bd6ccc2017-11-24 14:42:47 +010071 @CalledByNative("Buffer")
henrike@webrtc.org723d6832013-07-12 16:04:50 +000072 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 {
bemasc0edd50c2015-07-01 13:34:33 -070080 /** The data channel's bufferedAmount has changed. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010081 @CalledByNative("Observer") public void onBufferedAmountChange(long previousAmount);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000082 /** The data channel state has changed. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010083 @CalledByNative("Observer") public void onStateChange();
henrike@webrtc.org723d6832013-07-12 16:04:50 +000084 /**
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 Jedvert7bd6ccc2017-11-24 14:42:47 +010089 @CalledByNative("Observer") public void onMessage(Buffer buffer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000090 }
91
92 /** Keep in sync with DataChannelInterface::DataState. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010093 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.org723d6832013-07-12 16:04:50 +0000104
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200105 private long nativeDataChannel;
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000106 private long nativeObserver;
107
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100108 @CalledByNative
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000109 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äkiee05e902018-09-28 14:38:21 +0200115 checkDataChannelExists();
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000116 if (nativeObserver != 0) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100117 nativeUnregisterObserver(nativeObserver);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000118 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100119 nativeObserver = nativeRegisterObserver(observer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000120 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000121
122 /** Unregister the (only) observer. */
123 public void unregisterObserver() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200124 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100125 nativeUnregisterObserver(nativeObserver);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000126 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000127
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100128 public String label() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200129 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100130 return nativeLabel();
131 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000132
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100133 public int id() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200134 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100135 return nativeId();
136 }
deadbeefee8ad2b2016-11-01 14:59:00 -0700137
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100138 public State state() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200139 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100140 return nativeState();
141 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000142
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 Jedvert84d8ae52017-12-20 15:12:10 +0100148 public long bufferedAmount() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200149 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100150 return nativeBufferedAmount();
151 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000152
153 /** Close the channel. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100154 public void close() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200155 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100156 nativeClose();
157 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000158
159 /** Send |data| to the remote peer; return success. */
160 public boolean send(Buffer buffer) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200161 checkDataChannelExists();
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000162 // 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 Jedvert84d8ae52017-12-20 15:12:10 +0100166 return nativeSend(data, buffer.binary);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000167 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000168
169 /** Dispose of native resources attached to this channel. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100170 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200171 checkDataChannelExists();
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100172 JniCommon.nativeReleaseRef(nativeDataChannel);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200173 nativeDataChannel = 0;
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100174 }
175
176 @CalledByNative
177 long getNativeDataChannel() {
178 return nativeDataChannel;
179 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100180
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200181 private void checkDataChannelExists() {
182 if (nativeDataChannel == 0) {
183 throw new IllegalStateException("DataChannel has been disposed.");
184 }
185 }
186
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100187 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.org723d6832013-07-12 16:04:50 +0000195};