15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2012, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// This file contains interfaces for DataChannels
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifndef TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <string>
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/basictypes.h"
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/buffer.h"
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/refcount.h"
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace webrtc {
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstruct DataChannelInit {
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DataChannelInit()
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      : reliable(false),
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        ordered(true),
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        maxRetransmitTime(-1),
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        maxRetransmits(-1),
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        negotiated(false),
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        id(-1) {
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool reliable;           // Deprecated.
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool ordered;            // True if ordered delivery is required.
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int maxRetransmitTime;   // The max period of time in milliseconds in which
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           // retransmissions will be sent.  After this time, no
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           // more retransmissions will be sent. -1 if unset.
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int maxRetransmits;      // The max number of retransmissions. -1 if unset.
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  std::string protocol;    // This is set by the application and opaque to the
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           // WebRTC implementation.
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool negotiated;         // True if the channel has been externally negotiated
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           // and we do not send an in-band signalling in the
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           // form of an "open" message.
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int id;                  // The stream id, or SID, for SCTP data channels. -1
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           // if unset.
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstruct DataBuffer {
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DataBuffer(const talk_base::Buffer& data, bool binary)
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      : data(data),
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        binary(binary) {
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // For convenience for unit tests.
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  explicit DataBuffer(const std::string& text)
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      : data(text.data(), text.length()),
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org        binary(false) {
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
78f69a59cbfe7dcb7e8b9737052e27f12176c97d53andrew@webrtc.org  size_t size() const { return data.length(); }
79f69a59cbfe7dcb7e8b9737052e27f12176c97d53andrew@webrtc.org
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::Buffer data;
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Indicates if the received data contains UTF-8 or binary data.
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Note that the upper layers are left to verify the UTF-8 encoding.
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // TODO(jiayl): prefer to use an enum instead of a bool.
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool binary;
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass DataChannelObserver {
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // The data channel state have changed.
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual void OnStateChange() = 0;
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //  A data buffer was successfully received.
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual void OnMessage(const DataBuffer& buffer) = 0;
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org protected:
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual ~DataChannelObserver() {}
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass DataChannelInterface : public talk_base::RefCountInterface {
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  enum DataState {  // Keep in sync with DataChannel.java:State.
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    kConnecting,
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    kOpen,  // The DataChannel is ready to send data.
1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    kClosing,
1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    kClosed
1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  };
1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual void RegisterObserver(DataChannelObserver* observer) = 0;
1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual void UnregisterObserver() = 0;
1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // The label attribute represents a label that can be used to distinguish this
1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // DataChannel object from other DataChannel objects.
1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual std::string label() const = 0;
1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool reliable() const = 0;
1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual int id() const = 0;
1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual DataState state() const = 0;
1155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // The buffered_amount returns the number of bytes of application data
1165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // (UTF-8 text and binary data) that have been queued using SendBuffer but
1175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // have not yet been transmitted to the network.
1185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual uint64 buffered_amount() const = 0;
1195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual void Close() = 0;
1205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Sends |data| to the remote peer.
1215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool Send(const DataBuffer& buffer) = 0;
1225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org protected:
1245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual ~DataChannelInterface() {}
1255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
1265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}  // namespace webrtc
1285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif  // TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_
130