blob: 21873df2a06f77749db813afa968d5cf06b21fdf [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. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010016@JNINamespace("webrtc::jni")
henrike@webrtc.org723d6832013-07-12 16:04:50 +000017public 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.orga06ebab2014-02-03 19:11:29 +000026 public boolean negotiated = false;
henrike@webrtc.org723d6832013-07-12 16:04:50 +000027 // Optional unsigned short in WebIDL, -1 means unspecified.
28 public int id = -1;
29
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010030 @CalledByNative("Init")
31 boolean getOrdered() {
32 return ordered;
33 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +000034
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010035 @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.org723d6832013-07-12 16:04:50 +000058 }
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 Jedvert7bd6ccc2017-11-24 14:42:47 +010072 @CalledByNative("Buffer")
henrike@webrtc.org723d6832013-07-12 16:04:50 +000073 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 {
bemasc0edd50c2015-07-01 13:34:33 -070081 /** The data channel's bufferedAmount has changed. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010082 @CalledByNative("Observer") public void onBufferedAmountChange(long previousAmount);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000083 /** The data channel state has changed. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010084 @CalledByNative("Observer") public void onStateChange();
henrike@webrtc.org723d6832013-07-12 16:04:50 +000085 /**
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 Jedvert7bd6ccc2017-11-24 14:42:47 +010090 @CalledByNative("Observer") public void onMessage(Buffer buffer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000091 }
92
93 /** Keep in sync with DataChannelInterface::DataState. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +010094 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.org723d6832013-07-12 16:04:50 +0000105
106 private final long nativeDataChannel;
107 private long nativeObserver;
108
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100109 @CalledByNative
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000110 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 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() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100124 nativeUnregisterObserver(nativeObserver);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000125 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000126
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100127 public String label() {
128 return nativeLabel();
129 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000130
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100131 public int id() {
132 return nativeId();
133 }
deadbeefee8ad2b2016-11-01 14:59:00 -0700134
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100135 public State state() {
136 return nativeState();
137 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000138
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 Jedvert84d8ae52017-12-20 15:12:10 +0100144 public long bufferedAmount() {
145 return nativeBufferedAmount();
146 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000147
148 /** Close the channel. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100149 public void close() {
150 nativeClose();
151 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000152
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 Jedvert84d8ae52017-12-20 15:12:10 +0100159 return nativeSend(data, buffer.binary);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000160 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000161
162 /** Dispose of native resources attached to this channel. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100163 public void dispose() {
164 JniCommon.nativeReleaseRef(nativeDataChannel);
165 }
166
167 @CalledByNative
168 long getNativeDataChannel() {
169 return nativeDataChannel;
170 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100171
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.org723d6832013-07-12 16:04:50 +0000180};