blob: 6759288825b0f0ae8ec277d5da12925839dc2652 [file] [log] [blame]
Harald Alvestrand05e4d082019-12-03 14:04:21 +01001/*
2 * Copyright 2019 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
11#ifndef PC_DATA_CHANNEL_CONTROLLER_H_
12#define PC_DATA_CHANNEL_CONTROLLER_H_
13
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
19#include "pc/channel.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070020#include "pc/rtp_data_channel.h"
21#include "pc/sctp_data_channel.h"
Harald Alvestrand246724b2019-12-03 22:31:42 +010022#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010023
24namespace webrtc {
25
26class PeerConnection;
27
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070028class DataChannelController : public RtpDataChannelProviderInterface,
29 public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010030 public DataChannelSink {
31 public:
32 explicit DataChannelController(PeerConnection* pc) : pc_(pc) {}
33
Harald Alvestrandab813162020-01-09 13:29:56 +010034 // Not copyable or movable.
35 DataChannelController(DataChannelController&) = delete;
36 DataChannelController& operator=(const DataChannelController& other) = delete;
37 DataChannelController(DataChannelController&&) = delete;
38 DataChannelController& operator=(DataChannelController&& other) = delete;
39
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070040 // Implements RtpDataChannelProviderInterface/
41 // SctpDataChannelProviderInterface.
Harald Alvestrand05e4d082019-12-03 14:04:21 +010042 bool SendData(const cricket::SendDataParams& params,
43 const rtc::CopyOnWriteBuffer& payload,
44 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070045 bool ConnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
46 void DisconnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
47 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
48 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010049 void AddSctpDataStream(int sid) override;
50 void RemoveSctpDataStream(int sid) override;
51 bool ReadyToSendData() const override;
52
53 // Implements DataChannelSink.
54 void OnDataReceived(int channel_id,
55 DataMessageType type,
56 const rtc::CopyOnWriteBuffer& buffer) override;
57 void OnChannelClosing(int channel_id) override;
58 void OnChannelClosed(int channel_id) override;
59 void OnReadyToSend() override;
Harald Alvestrand2697ac12019-12-16 10:37:04 +010060 void OnTransportClosed() override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010061
62 // Called from PeerConnection::SetupDataChannelTransport_n
63 void SetupDataChannelTransport_n();
64 // Called from PeerConnection::TeardownDataChannelTransport_n
65 void TeardownDataChannelTransport_n();
66
67 // Called from PeerConnection::OnTransportChanged
68 // to make required changes to datachannels' transports.
69 void OnTransportChanged(
70 DataChannelTransportInterface* data_channel_transport);
71
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020072 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070073 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020074
Harald Alvestrand05e4d082019-12-03 14:04:21 +010075 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070076 // be offered in a SessionDescription, and wraps it in a proxy object.
77 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010078 const std::string& label,
79 const InternalDataChannelInit*
80 config) /* RTC_RUN_ON(signaling_thread()) */;
81 void AllocateSctpSids(rtc::SSLRole role);
82
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070083 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010084
85 // Checks if any data channel has been added.
86 bool HasDataChannels() const;
87 bool HasSctpDataChannels() const {
88 RTC_DCHECK_RUN_ON(signaling_thread());
89 return !sctp_data_channels_.empty();
90 }
91 bool HasRtpDataChannels() const {
92 RTC_DCHECK_RUN_ON(signaling_thread());
93 return !rtp_data_channels_.empty();
94 }
95
Harald Alvestrand05e4d082019-12-03 14:04:21 +010096 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
97 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
98
99 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200100 cricket::DataChannelType data_channel_type() const;
101 void set_data_channel_type(cricket::DataChannelType type);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100102 cricket::RtpDataChannel* rtp_data_channel() const {
103 return rtp_data_channel_;
104 }
105 void set_rtp_data_channel(cricket::RtpDataChannel* channel) {
106 rtp_data_channel_ = channel;
107 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200108 DataChannelTransportInterface* data_channel_transport() const;
109 void set_data_channel_transport(DataChannelTransportInterface* transport);
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700110 const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>*
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200111 rtp_data_channels() const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100112
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700113 sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100114 RTC_DCHECK_RUN_ON(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700115 return SignalRtpDataChannelCreated_;
116 }
117 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
118 RTC_DCHECK_RUN_ON(signaling_thread());
119 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100120 }
121 // Called when the transport for the data channels is closed or destroyed.
122 void OnTransportChannelClosed();
123
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700124 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100125
126 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700127 rtc::scoped_refptr<RtpDataChannel> InternalCreateRtpDataChannel(
128 const std::string& label,
129 const DataChannelInit* config) /* RTC_RUN_ON(signaling_thread()) */;
130
131 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
132 const std::string& label,
133 const InternalDataChannelInit*
134 config) /* RTC_RUN_ON(signaling_thread()) */;
135
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100136 // Parses and handles open messages. Returns true if the message is an open
137 // message, false otherwise.
138 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
139 const rtc::CopyOnWriteBuffer& buffer)
140 RTC_RUN_ON(signaling_thread());
141 // Called when a valid data channel OPEN message is received.
142 void OnDataChannelOpenMessage(const std::string& label,
143 const InternalDataChannelInit& config)
144 RTC_RUN_ON(signaling_thread());
145
146 void CreateRemoteRtpDataChannel(const std::string& label,
147 uint32_t remote_ssrc)
148 RTC_RUN_ON(signaling_thread());
149
150 void UpdateClosingRtpDataChannels(
151 const std::vector<std::string>& active_channels,
152 bool is_local_update) RTC_RUN_ON(signaling_thread());
153
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200154 // Called from SendData when data_channel_transport() is true.
155 bool DataChannelSendData(const cricket::SendDataParams& params,
156 const rtc::CopyOnWriteBuffer& payload,
157 cricket::SendDataResult* result);
158
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200159 // Called when all data channels need to be notified of a transport channel
160 // (calls OnTransportChannelCreated on the signaling thread).
161 void NotifyDataChannelsOfTransportCreated();
162
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100163 rtc::Thread* network_thread() const;
164 rtc::Thread* signaling_thread() const;
165
166 // Specifies which kind of data channel is allowed. This is controlled
167 // by the chrome command-line flag and constraints:
168 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
169 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
170 // not set or false, SCTP is allowed (DCT_SCTP);
171 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
172 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
173 cricket::DataChannelType data_channel_type_ =
174 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
175 // signaling and network thread.
176
177 // Plugin transport used for data channels. Pointer may be accessed and
178 // checked from any thread, but the object may only be touched on the
179 // network thread.
180 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
181 // thread.
182 DataChannelTransportInterface* data_channel_transport_ = nullptr;
183
184 // Cached value of whether the data channel transport is ready to send.
185 bool data_channel_transport_ready_to_send_
186 RTC_GUARDED_BY(signaling_thread()) = false;
187
188 // |rtp_data_channel_| is used if in RTP data channel mode,
189 // |data_channel_transport_| when using SCTP.
190 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
191 // TODO(bugs.webrtc.org/9987): Accessed on both
192 // signaling and some other thread.
193
194 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700195 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100196 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700197 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100198 RTC_GUARDED_BY(signaling_thread());
199
200 // Map of label -> DataChannel
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700201 std::map<std::string, rtc::scoped_refptr<RtpDataChannel>> rtp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100202 RTC_GUARDED_BY(signaling_thread());
203
204 // Signals from |data_channel_transport_|. These are invoked on the
205 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200206 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
207 // network thread.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100208 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
209 RTC_GUARDED_BY(signaling_thread());
210 sigslot::signal2<const cricket::ReceiveDataParams&,
211 const rtc::CopyOnWriteBuffer&>
212 SignalDataChannelTransportReceivedData_s
213 RTC_GUARDED_BY(signaling_thread());
214 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
215 RTC_GUARDED_BY(signaling_thread());
216 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
217 RTC_GUARDED_BY(signaling_thread());
218
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700219 sigslot::signal1<RtpDataChannel*> SignalRtpDataChannelCreated_
220 RTC_GUARDED_BY(signaling_thread());
221 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100222 RTC_GUARDED_BY(signaling_thread());
223
Tomas Gunnarsson6da27182020-09-12 23:10:17 +0200224 // Used from the network thread to invoke data channel transport signals on
225 // the signaling thread.
226 rtc::AsyncInvoker data_channel_transport_invoker_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100227 RTC_GUARDED_BY(network_thread());
228
229 // Owning PeerConnection.
230 PeerConnection* const pc_;
Harald Alvestrand246724b2019-12-03 22:31:42 +0100231 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100232};
233
234} // namespace webrtc
235
236#endif // PC_DATA_CHANNEL_CONTROLLER_H_