blob: 6b034096dcac71141a3cfd5f1ae7ed82b99bfd55 [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 = "";
fischman@webrtc.orga06ebab2014-02-03 19:11:29 +000025 public boolean negotiated = false;
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
105 private final long nativeDataChannel;
106 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) {
115 if (nativeObserver != 0) {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100116 nativeUnregisterObserver(nativeObserver);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000117 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100118 nativeObserver = nativeRegisterObserver(observer);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000119 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000120
121 /** Unregister the (only) observer. */
122 public void unregisterObserver() {
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100123 nativeUnregisterObserver(nativeObserver);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000124 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000125
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100126 public String label() {
127 return nativeLabel();
128 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000129
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100130 public int id() {
131 return nativeId();
132 }
deadbeefee8ad2b2016-11-01 14:59:00 -0700133
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100134 public State state() {
135 return nativeState();
136 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000137
138 /**
139 * Return the number of bytes of application data (UTF-8 text and binary data)
140 * that have been queued using SendBuffer but have not yet been transmitted
141 * to the network.
142 */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100143 public long bufferedAmount() {
144 return nativeBufferedAmount();
145 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000146
147 /** Close the channel. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100148 public void close() {
149 nativeClose();
150 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000151
152 /** Send |data| to the remote peer; return success. */
153 public boolean send(Buffer buffer) {
154 // TODO(fischman): this could be cleverer about avoiding copies if the
155 // ByteBuffer is direct and/or is backed by an array.
156 byte[] data = new byte[buffer.data.remaining()];
157 buffer.data.get(data);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100158 return nativeSend(data, buffer.binary);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000159 }
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000160
161 /** Dispose of native resources attached to this channel. */
Magnus Jedvert7bd6ccc2017-11-24 14:42:47 +0100162 public void dispose() {
163 JniCommon.nativeReleaseRef(nativeDataChannel);
164 }
165
166 @CalledByNative
167 long getNativeDataChannel() {
168 return nativeDataChannel;
169 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100170
171 private native long nativeRegisterObserver(Observer observer);
172 private native void nativeUnregisterObserver(long observer);
173 private native String nativeLabel();
174 private native int nativeId();
175 private native State nativeState();
176 private native long nativeBufferedAmount();
177 private native void nativeClose();
178 private native boolean nativeSend(byte[] data, boolean binary);
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000179};