128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org/*
228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * libjingle
35f93d0a140515e3b8cdd1b9a4c6f5871144e5deejlmiller@webrtc.org * Copyright 2012 Google Inc.
428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     this list of conditions and the following disclaimer.
1028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
1128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
1228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     and/or other materials provided with the distribution.
1328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
1428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     derived from this software without specific prior written permission.
1528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
1628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
1728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
1928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org */
2728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
2828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org// This file contains interfaces for DataChannels
2928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org// http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel
3028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
3128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#ifndef TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_
3228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#define TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_
3328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
3428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <string>
3528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
36d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/basictypes.h"
37d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/buffer.h"
38487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org#include "webrtc/base/checks.h"
39d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/refcount.h"
4028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgnamespace webrtc {
4328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgstruct DataChannelInit {
4528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  DataChannelInit()
4628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      : reliable(false),
4728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        ordered(true),
4828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        maxRetransmitTime(-1),
4928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        maxRetransmits(-1),
5028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        negotiated(false),
5128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        id(-1) {
5228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
5328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
5428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  bool reliable;           // Deprecated.
5528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  bool ordered;            // True if ordered delivery is required.
5628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  int maxRetransmitTime;   // The max period of time in milliseconds in which
5728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                           // retransmissions will be sent.  After this time, no
5828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                           // more retransmissions will be sent. -1 if unset.
5928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  int maxRetransmits;      // The max number of retransmissions. -1 if unset.
6028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  std::string protocol;    // This is set by the application and opaque to the
6128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                           // WebRTC implementation.
6228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  bool negotiated;         // True if the channel has been externally negotiated
6328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                           // and we do not send an in-band signalling in the
6428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                           // form of an "open" message.
6528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  int id;                  // The stream id, or SID, for SCTP data channels. -1
6628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org                           // if unset.
6728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org};
6828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
6928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgstruct DataBuffer {
70d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  DataBuffer(const rtc::Buffer& data, bool binary)
7128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      : data(data),
7228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        binary(binary) {
7328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
7428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // For convenience for unit tests.
7528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  explicit DataBuffer(const std::string& text)
7628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      : data(text.data(), text.length()),
7728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        binary(false) {
7828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
79eebcab5ce99d3e8641dd92a569916b0d24e29fcakwiberg@webrtc.org  size_t size() const { return data.size(); }
80d64719d8954262fee94e7615422f3d027dc1ae6bwu@webrtc.org
81d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  rtc::Buffer data;
8228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Indicates if the received data contains UTF-8 or binary data.
8328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Note that the upper layers are left to verify the UTF-8 encoding.
8428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // TODO(jiayl): prefer to use an enum instead of a bool.
8528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  bool binary;
8628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org};
8728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
8828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgclass DataChannelObserver {
8928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org public:
9028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // The data channel state have changed.
9128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual void OnStateChange() = 0;
9228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  //  A data buffer was successfully received.
9328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual void OnMessage(const DataBuffer& buffer) = 0;
940edd50ccb34cc2dc4746137fdce1f5cf66808274bemasc  // The data channel's buffered_amount has changed.
950c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström  virtual void OnBufferedAmountChange(uint64_t previous_amount){};
9628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
9728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org protected:
9828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual ~DataChannelObserver() {}
9928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org};
10028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
101d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.orgclass DataChannelInterface : public rtc::RefCountInterface {
10228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org public:
103ff2733204dd2cc894206716e111dcffabc8898f2tkchin@webrtc.org  // Keep in sync with DataChannel.java:State and
104ff2733204dd2cc894206716e111dcffabc8898f2tkchin@webrtc.org  // RTCDataChannel.h:RTCDataChannelState.
105ff2733204dd2cc894206716e111dcffabc8898f2tkchin@webrtc.org  enum DataState {
10628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    kConnecting,
10728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    kOpen,  // The DataChannel is ready to send data.
10828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    kClosing,
10928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    kClosed
11028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  };
11128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
112487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org  static const char* DataStateString(DataState state) {
113487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org    switch (state) {
114487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org      case kConnecting:
115487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org        return "connecting";
116487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org      case kOpen:
117487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org        return "open";
118487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org      case kClosing:
119487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org        return "closing";
120487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org      case kClosed:
121487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org        return "closed";
122487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org    }
12391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    RTC_CHECK(false) << "Unknown DataChannel state: " << state;
124487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org    return "";
125487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org  }
126487a4442152e2c70146aa2d2c6ccb370233c056cdecurtis@webrtc.org
12728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual void RegisterObserver(DataChannelObserver* observer) = 0;
12828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual void UnregisterObserver() = 0;
12928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // The label attribute represents a label that can be used to distinguish this
13028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // DataChannel object from other DataChannel objects.
13128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual std::string label() const = 0;
13228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual bool reliable() const = 0;
133822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org
134822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org  // TODO(tommyw): Remove these dummy implementations when all classes have
135822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org  // implemented these APIs. They should all just return the values the
136822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org  // DataChannel was created with.
137822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org  virtual bool ordered() const { return false; }
1380c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström  virtual uint16_t maxRetransmitTime() const { return 0; }
1390c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström  virtual uint16_t maxRetransmits() const { return 0; }
140822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org  virtual std::string protocol() const { return std::string(); }
141822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org  virtual bool negotiated() const { return false; }
142822fbd8b68ffdb481b9557e2950ae8d6657c8ce6wu@webrtc.org
14328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual int id() const = 0;
14428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual DataState state() const = 0;
14528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // The buffered_amount returns the number of bytes of application data
14628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // (UTF-8 text and binary data) that have been queued using SendBuffer but
14728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // have not yet been transmitted to the network.
1480c4e06b4c6107a1b94f764e279e4fb4161e905b0Peter Boström  virtual uint64_t buffered_amount() const = 0;
14928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual void Close() = 0;
15028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Sends |data| to the remote peer.
15128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual bool Send(const DataBuffer& buffer) = 0;
15228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
15328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org protected:
15428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  virtual ~DataChannelInterface() {}
15528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org};
15628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
15728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}  // namespace webrtc
15828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
15928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#endif  // TALK_APP_WEBRTC_DATACHANNELINTERFACE_H_
160