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