OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * 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. | |
9 */ | |
10 | |
11 package org.webrtc; | |
12 | |
13 import java.nio.ByteBuffer; | |
14 | |
15 /** Java wrapper for a C++ DataChannelInterface. */ | |
16 public 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 = ""; | |
25 public boolean negotiated = false; | |
26 // Optional unsigned short in WebIDL, -1 means unspecified. | |
27 public int id = -1; | |
28 | |
29 public Init() {} | |
30 | |
31 // Called only by native code. | |
32 private Init(boolean ordered, int maxRetransmitTimeMs, int maxRetransmits, S
tring protocol, | |
33 boolean negotiated, int id) { | |
34 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 { | |
62 /** The data channel's bufferedAmount has changed. */ | |
63 public void onBufferedAmountChange(long previousAmount); | |
64 /** 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. */ | |
75 public enum State { CONNECTING, OPEN, CLOSING, CLOSED } | |
76 | |
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 | |
101 public native int id(); | |
102 | |
103 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 }; | |
OLD | NEW |