Home | History | Annotate | Download | only in webrtc
      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 
     28 package org.webrtc;
     29 
     30 import java.nio.ByteBuffer;
     31 
     32 /** Java wrapper for a C++ DataChannelInterface. */
     33 public 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's bufferedAmount has changed. */
     81     public void onBufferedAmountChange(long previousAmount);
     82     /** The data channel state has changed. */
     83     public void onStateChange();
     84     /**
     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      */
     89     public void onMessage(Buffer buffer);
     90   }
     91 
     92   /** Keep in sync with DataChannelInterface::DataState. */
     93   public enum State { CONNECTING, OPEN, CLOSING, CLOSED };
     94 
     95   private final long nativeDataChannel;
     96   private long nativeObserver;
     97 
     98   public DataChannel(long nativeDataChannel) {
     99     this.nativeDataChannel = nativeDataChannel;
    100   }
    101 
    102   /** Register |observer|, replacing any previously-registered observer. */
    103   public void registerObserver(Observer observer) {
    104     if (nativeObserver != 0) {
    105       unregisterObserverNative(nativeObserver);
    106     }
    107     nativeObserver = registerObserverNative(observer);
    108   }
    109   private native long registerObserverNative(Observer observer);
    110 
    111   /** Unregister the (only) observer. */
    112   public void unregisterObserver() {
    113     unregisterObserverNative(nativeObserver);
    114   }
    115   private native void unregisterObserverNative(long nativeObserver);
    116 
    117   public native String label();
    118 
    119   public native State state();
    120 
    121   /**
    122    * Return the number of bytes of application data (UTF-8 text and binary data)
    123    * that have been queued using SendBuffer but have not yet been transmitted
    124    * to the network.
    125    */
    126   public native long bufferedAmount();
    127 
    128   /** Close the channel. */
    129   public native void close();
    130 
    131   /** Send |data| to the remote peer; return success. */
    132   public boolean send(Buffer buffer) {
    133     // TODO(fischman): this could be cleverer about avoiding copies if the
    134     // ByteBuffer is direct and/or is backed by an array.
    135     byte[] data = new byte[buffer.data.remaining()];
    136     buffer.data.get(data);
    137     return sendNative(data, buffer.binary);
    138   }
    139   private native boolean sendNative(byte[] data, boolean binary);
    140 
    141   /** Dispose of native resources attached to this channel. */
    142   public native void dispose();
    143 };
    144