Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 1 | /* |
kjellander | 95ed4e6 | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 3 | * |
kjellander | 95ed4e6 | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contains interfaces for DataChannels |
| 12 | // http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel |
| 13 | |
| 14 | #ifndef WEBRTC_API_DATACHANNELINTERFACE_H_ |
| 15 | #define WEBRTC_API_DATACHANNELINTERFACE_H_ |
| 16 | |
| 17 | #include <string> |
| 18 | |
| 19 | #include "webrtc/base/basictypes.h" |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 20 | #include "webrtc/base/checks.h" |
jbauch | bb6decf | 2016-03-20 13:15:43 | [diff] [blame] | 21 | #include "webrtc/base/copyonwritebuffer.h" |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 22 | #include "webrtc/base/refcount.h" |
| 23 | |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | struct DataChannelInit { |
| 28 | DataChannelInit() |
| 29 | : reliable(false), |
| 30 | ordered(true), |
| 31 | maxRetransmitTime(-1), |
| 32 | maxRetransmits(-1), |
| 33 | negotiated(false), |
| 34 | id(-1) { |
| 35 | } |
| 36 | |
| 37 | bool reliable; // Deprecated. |
| 38 | bool ordered; // True if ordered delivery is required. |
| 39 | int maxRetransmitTime; // The max period of time in milliseconds in which |
| 40 | // retransmissions will be sent. After this time, no |
| 41 | // more retransmissions will be sent. -1 if unset. |
| 42 | int maxRetransmits; // The max number of retransmissions. -1 if unset. |
| 43 | std::string protocol; // This is set by the application and opaque to the |
| 44 | // WebRTC implementation. |
| 45 | bool negotiated; // True if the channel has been externally negotiated |
| 46 | // and we do not send an in-band signalling in the |
| 47 | // form of an "open" message. |
| 48 | int id; // The stream id, or SID, for SCTP data channels. -1 |
| 49 | // if unset. |
| 50 | }; |
| 51 | |
| 52 | struct DataBuffer { |
jbauch | bb6decf | 2016-03-20 13:15:43 | [diff] [blame] | 53 | DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary) |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 54 | : data(data), |
| 55 | binary(binary) { |
| 56 | } |
| 57 | // For convenience for unit tests. |
| 58 | explicit DataBuffer(const std::string& text) |
| 59 | : data(text.data(), text.length()), |
| 60 | binary(false) { |
| 61 | } |
| 62 | size_t size() const { return data.size(); } |
| 63 | |
jbauch | bb6decf | 2016-03-20 13:15:43 | [diff] [blame] | 64 | rtc::CopyOnWriteBuffer data; |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 65 | // Indicates if the received data contains UTF-8 or binary data. |
| 66 | // Note that the upper layers are left to verify the UTF-8 encoding. |
| 67 | // TODO(jiayl): prefer to use an enum instead of a bool. |
| 68 | bool binary; |
| 69 | }; |
| 70 | |
| 71 | class DataChannelObserver { |
| 72 | public: |
| 73 | // The data channel state have changed. |
| 74 | virtual void OnStateChange() = 0; |
| 75 | // A data buffer was successfully received. |
| 76 | virtual void OnMessage(const DataBuffer& buffer) = 0; |
| 77 | // The data channel's buffered_amount has changed. |
| 78 | virtual void OnBufferedAmountChange(uint64_t previous_amount){}; |
| 79 | |
| 80 | protected: |
| 81 | virtual ~DataChannelObserver() {} |
| 82 | }; |
| 83 | |
| 84 | class DataChannelInterface : public rtc::RefCountInterface { |
| 85 | public: |
| 86 | // Keep in sync with DataChannel.java:State and |
| 87 | // RTCDataChannel.h:RTCDataChannelState. |
| 88 | enum DataState { |
| 89 | kConnecting, |
| 90 | kOpen, // The DataChannel is ready to send data. |
| 91 | kClosing, |
| 92 | kClosed |
| 93 | }; |
| 94 | |
| 95 | static const char* DataStateString(DataState state) { |
| 96 | switch (state) { |
| 97 | case kConnecting: |
| 98 | return "connecting"; |
| 99 | case kOpen: |
| 100 | return "open"; |
| 101 | case kClosing: |
| 102 | return "closing"; |
| 103 | case kClosed: |
| 104 | return "closed"; |
| 105 | } |
| 106 | RTC_CHECK(false) << "Unknown DataChannel state: " << state; |
| 107 | return ""; |
| 108 | } |
| 109 | |
| 110 | virtual void RegisterObserver(DataChannelObserver* observer) = 0; |
| 111 | virtual void UnregisterObserver() = 0; |
| 112 | // The label attribute represents a label that can be used to distinguish this |
| 113 | // DataChannel object from other DataChannel objects. |
| 114 | virtual std::string label() const = 0; |
| 115 | virtual bool reliable() const = 0; |
| 116 | |
| 117 | // TODO(tommyw): Remove these dummy implementations when all classes have |
| 118 | // implemented these APIs. They should all just return the values the |
| 119 | // DataChannel was created with. |
| 120 | virtual bool ordered() const { return false; } |
| 121 | virtual uint16_t maxRetransmitTime() const { return 0; } |
| 122 | virtual uint16_t maxRetransmits() const { return 0; } |
| 123 | virtual std::string protocol() const { return std::string(); } |
| 124 | virtual bool negotiated() const { return false; } |
| 125 | |
| 126 | virtual int id() const = 0; |
| 127 | virtual DataState state() const = 0; |
hbos | 76d5ec8 | 2016-11-24 09:17:52 | [diff] [blame] | 128 | virtual uint32_t messages_sent() const = 0; |
| 129 | virtual uint64_t bytes_sent() const = 0; |
| 130 | virtual uint32_t messages_received() const = 0; |
| 131 | virtual uint64_t bytes_received() const = 0; |
Henrik Kjellander | bd0ae45 | 2016-02-10 09:53:12 | [diff] [blame] | 132 | // The buffered_amount returns the number of bytes of application data |
| 133 | // (UTF-8 text and binary data) that have been queued using SendBuffer but |
| 134 | // have not yet been transmitted to the network. |
| 135 | virtual uint64_t buffered_amount() const = 0; |
| 136 | virtual void Close() = 0; |
| 137 | // Sends |data| to the remote peer. |
| 138 | virtual bool Send(const DataBuffer& buffer) = 0; |
| 139 | |
| 140 | protected: |
| 141 | virtual ~DataChannelInterface() {} |
| 142 | }; |
| 143 | |
| 144 | } // namespace webrtc |
| 145 | |
| 146 | #endif // WEBRTC_API_DATACHANNELINTERFACE_H_ |