data_channel_transport_interface.h (5557B)
1 /* Copyright 2019 The WebRTC project authors. All Rights Reserved. 2 * 3 * Use of this source code is governed by a BSD-style license 4 * that can be found in the LICENSE file in the root of the source 5 * tree. An additional intellectual property rights grant can be found 6 * in the file PATENTS. All contributing project authors may 7 * be found in the AUTHORS file in the root of the source tree. 8 */ 9 10 // This is an experimental interface and is subject to change without notice. 11 12 #ifndef API_TRANSPORT_DATA_CHANNEL_TRANSPORT_INTERFACE_H_ 13 #define API_TRANSPORT_DATA_CHANNEL_TRANSPORT_INTERFACE_H_ 14 15 #include <cstddef> 16 #include <optional> 17 18 #include "api/priority.h" 19 #include "api/rtc_error.h" 20 #include "rtc_base/copy_on_write_buffer.h" 21 22 namespace webrtc { 23 24 // Supported types of application data messages. 25 enum class DataMessageType { 26 // Application data buffer with the binary bit unset. 27 kText, 28 29 // Application data buffer with the binary bit set. 30 kBinary, 31 32 // Transport-agnostic control messages, such as open or open-ack messages. 33 kControl, 34 }; 35 36 // Parameters for sending data. The parameters may change from message to 37 // message, even within a single channel. For example, control messages may be 38 // sent reliably and in-order, even if the data channel is configured for 39 // unreliable delivery. 40 struct SendDataParams { 41 DataMessageType type = DataMessageType::kText; 42 43 // Whether to deliver the message in order with respect to other ordered 44 // messages with the same channel_id. 45 bool ordered = false; 46 47 // If set, the maximum number of times this message may be 48 // retransmitted by the transport before it is dropped. 49 // Setting this value to zero disables retransmission. 50 // Valid values are in the range [0-UINT16_MAX]. 51 // `max_rtx_count` and `max_rtx_ms` may not be set simultaneously. 52 std::optional<int> max_rtx_count; 53 54 // If set, the maximum number of milliseconds for which the transport 55 // may retransmit this message before it is dropped. 56 // Setting this value to zero disables retransmission. 57 // Valid values are in the range [0-UINT16_MAX]. 58 // `max_rtx_count` and `max_rtx_ms` may not be set simultaneously. 59 std::optional<int> max_rtx_ms; 60 }; 61 62 // Sink for callbacks related to a data channel. 63 class DataChannelSink { 64 public: 65 virtual ~DataChannelSink() = default; 66 67 // Callback issued when data is received by the transport. 68 virtual void OnDataReceived(int channel_id, 69 DataMessageType type, 70 const CopyOnWriteBuffer& buffer) = 0; 71 72 // Callback issued when a remote data channel begins the closing procedure. 73 // Messages sent after the closing procedure begins will not be transmitted. 74 virtual void OnChannelClosing(int channel_id) = 0; 75 76 // Callback issued when a (remote or local) data channel completes the closing 77 // procedure. Closing channels become closed after all pending data has been 78 // transmitted. 79 virtual void OnChannelClosed(int channel_id) = 0; 80 81 // Callback issued when the data channel becomes ready to send. 82 // This callback will be issued immediately when the data channel sink is 83 // registered if the transport is ready at that time. This callback may be 84 // invoked again following send errors (eg. due to the transport being 85 // temporarily blocked or unavailable). 86 virtual void OnReadyToSend() = 0; 87 88 // Callback issued when the data channel becomes unusable (closed). 89 // TODO(https://crbug.com/webrtc/10360): Make pure virtual when all 90 // consumers updated. 91 virtual void OnTransportClosed(RTCError /* error */) {} 92 93 // The data channel's buffered_amount has fallen to or below the threshold 94 // set when calling `SetBufferedAmountLowThreshold` 95 virtual void OnBufferedAmountLow(int channel_id) = 0; 96 }; 97 98 // Transport for data channels. 99 class DataChannelTransportInterface { 100 public: 101 virtual ~DataChannelTransportInterface() = default; 102 103 // Opens a data `channel_id` for sending. May return an error if the 104 // specified `channel_id` is unusable. Must be called before `SendData`. 105 virtual RTCError OpenChannel(int channel_id, PriorityValue priority) = 0; 106 107 // Sends a data buffer to the remote endpoint using the given send parameters. 108 // `buffer` may not be larger than 256 KiB. Returns an error if the send 109 // fails. 110 virtual RTCError SendData(int channel_id, 111 const SendDataParams& params, 112 const CopyOnWriteBuffer& buffer) = 0; 113 114 // Closes `channel_id` gracefully. Returns an error if `channel_id` is not 115 // open. Data sent after the closing procedure begins will not be 116 // transmitted. The channel becomes closed after pending data is transmitted. 117 virtual RTCError CloseChannel(int channel_id) = 0; 118 119 // Sets a sink for data messages and channel state callbacks. Before media 120 // transport is destroyed, the sink must be unregistered by setting it to 121 // nullptr. 122 virtual void SetDataSink(DataChannelSink* sink) = 0; 123 124 // Returns whether this data channel transport is ready to send. 125 // Note: the default implementation always returns false (as it assumes no one 126 // has implemented the interface). This default implementation is temporary. 127 virtual bool IsReadyToSend() const = 0; 128 129 virtual size_t buffered_amount(int channel_id) const = 0; 130 virtual size_t buffered_amount_low_threshold(int channel_id) const = 0; 131 virtual void SetBufferedAmountLowThreshold(int channel_id, size_t bytes) = 0; 132 }; 133 134 } // namespace webrtc 135 136 #endif // API_TRANSPORT_DATA_CHANNEL_TRANSPORT_INTERFACE_H_