blob: dc64ccc0489ec83629bb5a1dccf32c3a194772cf [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
29 public Init() {}
30
31 // Called only by native code.
sakalb6760f92016-09-29 04:12:44 -070032 private Init(boolean ordered, int maxRetransmitTimeMs, int maxRetransmits, String protocol,
33 boolean negotiated, int id) {
henrike@webrtc.org723d6832013-07-12 16:04:50 +000034 this.ordered = ordered;
35 this.maxRetransmitTimeMs = maxRetransmitTimeMs;
36 this.maxRetransmits = maxRetransmits;
37 this.protocol = protocol;
38 this.negotiated = negotiated;
39 this.id = id;
40 }
41 }
42
43 /** Java version of C++ DataBuffer. The atom of data in a DataChannel. */
44 public static class Buffer {
45 /** The underlying data. */
46 public final ByteBuffer data;
47
48 /**
49 * Indicates whether |data| contains UTF-8 text or "binary data"
50 * (i.e. anything else).
51 */
52 public final boolean binary;
53
54 public Buffer(ByteBuffer data, boolean binary) {
55 this.data = data;
56 this.binary = binary;
57 }
58 }
59
60 /** Java version of C++ DataChannelObserver. */
61 public interface Observer {
bemasc0edd50c2015-07-01 13:34:33 -070062 /** The data channel's bufferedAmount has changed. */
63 public void onBufferedAmountChange(long previousAmount);
henrike@webrtc.org723d6832013-07-12 16:04:50 +000064 /** The data channel state has changed. */
65 public void onStateChange();
66 /**
67 * A data buffer was successfully received. NOTE: |buffer.data| will be
68 * freed once this function returns so callers who want to use the data
69 * asynchronously must make sure to copy it first.
70 */
71 public void onMessage(Buffer buffer);
72 }
73
74 /** Keep in sync with DataChannelInterface::DataState. */
sakalb6760f92016-09-29 04:12:44 -070075 public enum State { CONNECTING, OPEN, CLOSING, CLOSED }
henrike@webrtc.org723d6832013-07-12 16:04:50 +000076
77 private final long nativeDataChannel;
78 private long nativeObserver;
79
80 public DataChannel(long nativeDataChannel) {
81 this.nativeDataChannel = nativeDataChannel;
82 }
83
84 /** Register |observer|, replacing any previously-registered observer. */
85 public void registerObserver(Observer observer) {
86 if (nativeObserver != 0) {
87 unregisterObserverNative(nativeObserver);
88 }
89 nativeObserver = registerObserverNative(observer);
90 }
91 private native long registerObserverNative(Observer observer);
92
93 /** Unregister the (only) observer. */
94 public void unregisterObserver() {
95 unregisterObserverNative(nativeObserver);
96 }
97 private native void unregisterObserverNative(long nativeObserver);
98
99 public native String label();
100
deadbeefee8ad2b2016-11-01 14:59:00 -0700101 public native int id();
102
henrike@webrtc.org723d6832013-07-12 16:04:50 +0000103 public native State state();
104
105 /**
106 * Return the number of bytes of application data (UTF-8 text and binary data)
107 * that have been queued using SendBuffer but have not yet been transmitted
108 * to the network.
109 */
110 public native long bufferedAmount();
111
112 /** Close the channel. */
113 public native void close();
114
115 /** Send |data| to the remote peer; return success. */
116 public boolean send(Buffer buffer) {
117 // TODO(fischman): this could be cleverer about avoiding copies if the
118 // ByteBuffer is direct and/or is backed by an array.
119 byte[] data = new byte[buffer.data.remaining()];
120 buffer.data.get(data);
121 return sendNative(data, buffer.binary);
122 }
123 private native boolean sendNative(byte[] data, boolean binary);
124
125 /** Dispose of native resources attached to this channel. */
126 public native void dispose();
127};