You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.8 KiB

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