1/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28package org.webrtc;
29
30import java.nio.ByteBuffer;
31
32/** Java wrapper for a C++ DataChannelInterface. */
33public class DataChannel {
34  /** Java wrapper for WebIDL RTCDataChannel. */
35  public static class Init {
36    public boolean ordered = true;
37    // Optional unsigned short in WebIDL, -1 means unspecified.
38    public int maxRetransmitTimeMs = -1;
39    // Optional unsigned short in WebIDL, -1 means unspecified.
40    public int maxRetransmits = -1;
41    public String protocol = "";
42    public boolean negotiated = false;
43    // Optional unsigned short in WebIDL, -1 means unspecified.
44    public int id = -1;
45
46    public Init() {}
47
48    // Called only by native code.
49    private Init(
50        boolean ordered, int maxRetransmitTimeMs, int maxRetransmits,
51        String protocol, boolean negotiated, int id) {
52      this.ordered = ordered;
53      this.maxRetransmitTimeMs = maxRetransmitTimeMs;
54      this.maxRetransmits = maxRetransmits;
55      this.protocol = protocol;
56      this.negotiated = negotiated;
57      this.id = id;
58    }
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
72    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 {
80    /** The data channel state has changed. */
81    public void onStateChange();
82    /**
83     * A data buffer was successfully received.  NOTE: |buffer.data| will be
84     * freed once this function returns so callers who want to use the data
85     * asynchronously must make sure to copy it first.
86     */
87    public void onMessage(Buffer buffer);
88  }
89
90  /** Keep in sync with DataChannelInterface::DataState. */
91  public enum State { CONNECTING, OPEN, CLOSING, CLOSED };
92
93  private final long nativeDataChannel;
94  private long nativeObserver;
95
96  public DataChannel(long nativeDataChannel) {
97    this.nativeDataChannel = nativeDataChannel;
98  }
99
100  /** Register |observer|, replacing any previously-registered observer. */
101  public void registerObserver(Observer observer) {
102    if (nativeObserver != 0) {
103      unregisterObserverNative(nativeObserver);
104    }
105    nativeObserver = registerObserverNative(observer);
106  }
107  private native long registerObserverNative(Observer observer);
108
109  /** Unregister the (only) observer. */
110  public void unregisterObserver() {
111    unregisterObserverNative(nativeObserver);
112  }
113  private native void unregisterObserverNative(long nativeObserver);
114
115  public native String label();
116
117  public native State state();
118
119  /**
120   * Return the number of bytes of application data (UTF-8 text and binary data)
121   * that have been queued using SendBuffer but have not yet been transmitted
122   * to the network.
123   */
124  public native long bufferedAmount();
125
126  /** Close the channel. */
127  public native void close();
128
129  /** Send |data| to the remote peer; return success. */
130  public boolean send(Buffer buffer) {
131    // TODO(fischman): this could be cleverer about avoiding copies if the
132    // ByteBuffer is direct and/or is backed by an array.
133    byte[] data = new byte[buffer.data.remaining()];
134    buffer.data.get(data);
135    return sendNative(data, buffer.binary);
136  }
137  private native boolean sendNative(byte[] data, boolean binary);
138
139  /** Dispose of native resources attached to this channel. */
140  public native void dispose();
141};
142