data_channel_interface.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. // This file contains interfaces for DataChannels
  11. // http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel
  12. #ifndef API_DATA_CHANNEL_INTERFACE_H_
  13. #define API_DATA_CHANNEL_INTERFACE_H_
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. #include <string>
  17. #include "absl/types/optional.h"
  18. #include "api/priority.h"
  19. #include "api/rtc_error.h"
  20. #include "rtc_base/checks.h"
  21. #include "rtc_base/copy_on_write_buffer.h"
  22. #include "rtc_base/ref_count.h"
  23. #include "rtc_base/system/rtc_export.h"
  24. namespace webrtc {
  25. // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
  26. // TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
  27. struct DataChannelInit {
  28. // Deprecated. Reliability is assumed, and channel will be unreliable if
  29. // maxRetransmitTime or MaxRetransmits is set.
  30. bool reliable = false;
  31. // True if ordered delivery is required.
  32. bool ordered = true;
  33. // The max period of time in milliseconds in which retransmissions will be
  34. // sent. After this time, no more retransmissions will be sent.
  35. //
  36. // Cannot be set along with |maxRetransmits|.
  37. // This is called |maxPacketLifeTime| in the WebRTC JS API.
  38. absl::optional<int> maxRetransmitTime;
  39. // The max number of retransmissions.
  40. //
  41. // Cannot be set along with |maxRetransmitTime|.
  42. absl::optional<int> maxRetransmits;
  43. // This is set by the application and opaque to the WebRTC implementation.
  44. std::string protocol;
  45. // True if the channel has been externally negotiated and we do not send an
  46. // in-band signalling in the form of an "open" message. If this is true, |id|
  47. // below must be set; otherwise it should be unset and will be negotiated
  48. // in-band.
  49. bool negotiated = false;
  50. // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
  51. int id = -1;
  52. // https://w3c.github.io/webrtc-priority/#new-rtcdatachannelinit-member
  53. absl::optional<Priority> priority;
  54. };
  55. // At the JavaScript level, data can be passed in as a string or a blob, so
  56. // this structure's |binary| flag tells whether the data should be interpreted
  57. // as binary or text.
  58. struct DataBuffer {
  59. DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
  60. : data(data), binary(binary) {}
  61. // For convenience for unit tests.
  62. explicit DataBuffer(const std::string& text)
  63. : data(text.data(), text.length()), binary(false) {}
  64. size_t size() const { return data.size(); }
  65. rtc::CopyOnWriteBuffer data;
  66. // Indicates if the received data contains UTF-8 or binary data.
  67. // Note that the upper layers are left to verify the UTF-8 encoding.
  68. // TODO(jiayl): prefer to use an enum instead of a bool.
  69. bool binary;
  70. };
  71. // Used to implement RTCDataChannel events.
  72. //
  73. // The code responding to these callbacks should unwind the stack before
  74. // using any other webrtc APIs; re-entrancy is not supported.
  75. class DataChannelObserver {
  76. public:
  77. // The data channel state have changed.
  78. virtual void OnStateChange() = 0;
  79. // A data buffer was successfully received.
  80. virtual void OnMessage(const DataBuffer& buffer) = 0;
  81. // The data channel's buffered_amount has changed.
  82. virtual void OnBufferedAmountChange(uint64_t sent_data_size) {}
  83. protected:
  84. virtual ~DataChannelObserver() = default;
  85. };
  86. class RTC_EXPORT DataChannelInterface : public rtc::RefCountInterface {
  87. public:
  88. // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
  89. // Unlikely to change, but keep in sync with DataChannel.java:State and
  90. // RTCDataChannel.h:RTCDataChannelState.
  91. enum DataState {
  92. kConnecting,
  93. kOpen, // The DataChannel is ready to send data.
  94. kClosing,
  95. kClosed
  96. };
  97. static const char* DataStateString(DataState state) {
  98. switch (state) {
  99. case kConnecting:
  100. return "connecting";
  101. case kOpen:
  102. return "open";
  103. case kClosing:
  104. return "closing";
  105. case kClosed:
  106. return "closed";
  107. }
  108. RTC_CHECK(false) << "Unknown DataChannel state: " << state;
  109. return "";
  110. }
  111. // Used to receive events from the data channel. Only one observer can be
  112. // registered at a time. UnregisterObserver should be called before the
  113. // observer object is destroyed.
  114. virtual void RegisterObserver(DataChannelObserver* observer) = 0;
  115. virtual void UnregisterObserver() = 0;
  116. // The label attribute represents a label that can be used to distinguish this
  117. // DataChannel object from other DataChannel objects.
  118. virtual std::string label() const = 0;
  119. // The accessors below simply return the properties from the DataChannelInit
  120. // the data channel was constructed with.
  121. virtual bool reliable() const = 0;
  122. // TODO(deadbeef): Remove these dummy implementations when all classes have
  123. // implemented these APIs. They should all just return the values the
  124. // DataChannel was created with.
  125. virtual bool ordered() const;
  126. // TODO(hta): Deprecate and remove the following two functions.
  127. virtual uint16_t maxRetransmitTime() const;
  128. virtual uint16_t maxRetransmits() const;
  129. virtual absl::optional<int> maxRetransmitsOpt() const;
  130. virtual absl::optional<int> maxPacketLifeTime() const;
  131. virtual std::string protocol() const;
  132. virtual bool negotiated() const;
  133. // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
  134. // If negotiated in-band, this ID will be populated once the DTLS role is
  135. // determined, and until then this will return -1.
  136. virtual int id() const = 0;
  137. virtual Priority priority() const { return Priority::kLow; }
  138. virtual DataState state() const = 0;
  139. // When state is kClosed, and the DataChannel was not closed using
  140. // the closing procedure, returns the error information about the closing.
  141. // The default implementation returns "no error".
  142. virtual RTCError error() const { return RTCError(); }
  143. virtual uint32_t messages_sent() const = 0;
  144. virtual uint64_t bytes_sent() const = 0;
  145. virtual uint32_t messages_received() const = 0;
  146. virtual uint64_t bytes_received() const = 0;
  147. // Returns the number of bytes of application data (UTF-8 text and binary
  148. // data) that have been queued using Send but have not yet been processed at
  149. // the SCTP level. See comment above Send below.
  150. virtual uint64_t buffered_amount() const = 0;
  151. // Begins the graceful data channel closing procedure. See:
  152. // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
  153. virtual void Close() = 0;
  154. // Sends |data| to the remote peer. If the data can't be sent at the SCTP
  155. // level (due to congestion control), it's buffered at the data channel level,
  156. // up to a maximum of 16MB. If Send is called while this buffer is full, the
  157. // data channel will be closed abruptly.
  158. //
  159. // So, it's important to use buffered_amount() and OnBufferedAmountChange to
  160. // ensure the data channel is used efficiently but without filling this
  161. // buffer.
  162. virtual bool Send(const DataBuffer& buffer) = 0;
  163. protected:
  164. ~DataChannelInterface() override = default;
  165. };
  166. } // namespace webrtc
  167. #endif // API_DATA_CHANNEL_INTERFACE_H_