blob: a63abe0d8993856233daff424e63feebbb810fce [file] [log] [blame]
Bjorn A Mellem05497f22019-08-01 10:48:20 -07001/* 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_DATA_CHANNEL_TRANSPORT_INTERFACE_H_
13#define API_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
19namespace webrtc {
20
21// Supported types of application data messages.
22enum 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.
37struct SendDataParams {
38 SendDataParams();
39 SendDataParams(const SendDataParams&);
40
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 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
51 // simultaneously.
52 absl::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 // Must be non-negative. |max_rtx_count| and |max_rtx_ms| may not be set
58 // simultaneously.
59 absl::optional<int> max_rtx_ms;
60};
61
62// Sink for callbacks related to a data channel.
63class 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 rtc::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
82// Transport for data channels.
83class DataChannelTransportInterface {
84 public:
85 virtual ~DataChannelTransportInterface() = default;
86
87 // Opens a data |channel_id| for sending. May return an error if the
88 // specified |channel_id| is unusable. Must be called before |SendData|.
89 virtual RTCError OpenChannel(int channel_id);
90
91 // Sends a data buffer to the remote endpoint using the given send parameters.
92 // |buffer| may not be larger than 256 KiB. Returns an error if the send
93 // fails.
94 virtual RTCError SendData(int channel_id,
95 const SendDataParams& params,
96 const rtc::CopyOnWriteBuffer& buffer);
97
98 // Closes |channel_id| gracefully. Returns an error if |channel_id| is not
99 // open. Data sent after the closing procedure begins will not be
100 // transmitted. The channel becomes closed after pending data is transmitted.
101 virtual RTCError CloseChannel(int channel_id);
102
103 // Sets a sink for data messages and channel state callbacks. Before media
104 // transport is destroyed, the sink must be unregistered by setting it to
105 // nullptr.
106 virtual void SetDataSink(DataChannelSink* sink);
107};
108
109} // namespace webrtc
110
111#endif // API_DATA_CHANNEL_TRANSPORT_INTERFACE_H_