blob: b9301f1faa15fd2f7f0dc15fa1b176747a9af0e2 [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 /**
Artem Titovd7ac5812021-07-27 12:23:39 +020066 * Indicates whether `data` contains UTF-8 text or "binary data"
henrike@webrtc.org723d6832013-07-12 16:04:50 +000067 * (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 /**
Artem Titovcfea2182021-08-10 01:22:31 +020085 * A data buffer was successfully received. NOTE: `buffer.data` will be
henrike@webrtc.org723d6832013-07-12 16:04:50 +000086 * 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
Artem Titovd7ac5812021-07-27 12:23:39 +0200113 /** Register `observer`, replacing any previously-registered observer. */
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000114 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);
Tian Tanc2310b22021-05-13 15:36:21 -0700126 nativeObserver = 0;
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000127 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000128
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100129 public String label() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200130 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100131 return nativeLabel();
132 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000133
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100134 public int id() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200135 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100136 return nativeId();
137 }
deadbeefee8ad2b2016-11-01 14:59:00 -0700138
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100139 public State state() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200140 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100141 return nativeState();
142 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000143
144 /**
145 * Return the number of bytes of application data (UTF-8 text and binary data)
146 * that have been queued using SendBuffer but have not yet been transmitted
147 * to the network.
148 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100149 public long bufferedAmount() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200150 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100151 return nativeBufferedAmount();
152 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000153
154 /** Close the channel. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100155 public void close() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200156 checkDataChannelExists();
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100157 nativeClose();
158 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000159
Artem Titovd7ac5812021-07-27 12:23:39 +0200160 /** Send `data` to the remote peer; return success. */
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000161 public boolean send(Buffer buffer) {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200162 checkDataChannelExists();
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000163 // TODO(fischman): this could be cleverer about avoiding copies if the
164 // ByteBuffer is direct and/or is backed by an array.
165 byte[] data = new byte[buffer.data.remaining()];
166 buffer.data.get(data);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100167 return nativeSend(data, buffer.binary);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000168 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000169
170 /** Dispose of native resources attached to this channel. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100171 public void dispose() {
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200172 checkDataChannelExists();
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100173 JniCommon.nativeReleaseRef(nativeDataChannel);
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200174 nativeDataChannel = 0;
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100175 }
176
177 @CalledByNative
178 long getNativeDataChannel() {
179 return nativeDataChannel;
180 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100181
Sami Kalliomäkiee05e902018-09-28 14:38:21 +0200182 private void checkDataChannelExists() {
183 if (nativeDataChannel == 0) {
184 throw new IllegalStateException("DataChannel has been disposed.");
185 }
186 }
187
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100188 private native long nativeRegisterObserver(Observer observer);
189 private native void nativeUnregisterObserver(long observer);
190 private native String nativeLabel();
191 private native int nativeId();
192 private native State nativeState();
193 private native long nativeBufferedAmount();
194 private native void nativeClose();
195 private native boolean nativeSend(byte[] data, boolean binary);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000196};