blob: f6d4409f55254358540a4ff57649a5ec587d7cbf [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
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000014#include <stdint.h>
15
Harald Alvestrand05e4d082019-12-03 14:04:21 +010016#include <map>
17#include <memory>
18#include <string>
19#include <vector>
20
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000021#include "api/data_channel_interface.h"
22#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010023#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000024#include "api/transport/data_channel_transport_interface.h"
25#include "media/base/media_channel.h"
26#include "media/base/media_engine.h"
27#include "media/base/stream_params.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010028#include "pc/channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000029#include "pc/data_channel_utils.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070030#include "pc/rtp_data_channel.h"
31#include "pc/sctp_data_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000032#include "rtc_base/checks.h"
33#include "rtc_base/copy_on_write_buffer.h"
34#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000035#include "rtc_base/third_party/sigslot/sigslot.h"
36#include "rtc_base/thread.h"
37#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 22:31:42 +010038#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010039
40namespace webrtc {
41
42class PeerConnection;
43
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070044class DataChannelController : public RtpDataChannelProviderInterface,
45 public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010046 public DataChannelSink {
47 public:
48 explicit DataChannelController(PeerConnection* pc) : pc_(pc) {}
49
Harald Alvestrandab813162020-01-09 13:29:56 +010050 // Not copyable or movable.
51 DataChannelController(DataChannelController&) = delete;
52 DataChannelController& operator=(const DataChannelController& other) = delete;
53 DataChannelController(DataChannelController&&) = delete;
54 DataChannelController& operator=(DataChannelController&& other) = delete;
55
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070056 // Implements RtpDataChannelProviderInterface/
57 // SctpDataChannelProviderInterface.
Harald Alvestrand05e4d082019-12-03 14:04:21 +010058 bool SendData(const cricket::SendDataParams& params,
59 const rtc::CopyOnWriteBuffer& payload,
60 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070061 bool ConnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
62 void DisconnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
63 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
64 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010065 void AddSctpDataStream(int sid) override;
66 void RemoveSctpDataStream(int sid) override;
67 bool ReadyToSendData() const override;
68
69 // Implements DataChannelSink.
70 void OnDataReceived(int channel_id,
71 DataMessageType type,
72 const rtc::CopyOnWriteBuffer& buffer) override;
73 void OnChannelClosing(int channel_id) override;
74 void OnChannelClosed(int channel_id) override;
75 void OnReadyToSend() override;
Harald Alvestrand2697ac12019-12-16 10:37:04 +010076 void OnTransportClosed() override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010077
78 // Called from PeerConnection::SetupDataChannelTransport_n
79 void SetupDataChannelTransport_n();
80 // Called from PeerConnection::TeardownDataChannelTransport_n
81 void TeardownDataChannelTransport_n();
82
83 // Called from PeerConnection::OnTransportChanged
84 // to make required changes to datachannels' transports.
85 void OnTransportChanged(
86 DataChannelTransportInterface* data_channel_transport);
87
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020088 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070089 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020090
Harald Alvestrand05e4d082019-12-03 14:04:21 +010091 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070092 // be offered in a SessionDescription, and wraps it in a proxy object.
93 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010094 const std::string& label,
95 const InternalDataChannelInit*
96 config) /* RTC_RUN_ON(signaling_thread()) */;
97 void AllocateSctpSids(rtc::SSLRole role);
98
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070099 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100100
101 // Checks if any data channel has been added.
102 bool HasDataChannels() const;
103 bool HasSctpDataChannels() const {
104 RTC_DCHECK_RUN_ON(signaling_thread());
105 return !sctp_data_channels_.empty();
106 }
107 bool HasRtpDataChannels() const {
108 RTC_DCHECK_RUN_ON(signaling_thread());
109 return !rtp_data_channels_.empty();
110 }
111
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100112 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
113 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
114
115 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200116 cricket::DataChannelType data_channel_type() const;
117 void set_data_channel_type(cricket::DataChannelType type);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100118 cricket::RtpDataChannel* rtp_data_channel() const {
119 return rtp_data_channel_;
120 }
121 void set_rtp_data_channel(cricket::RtpDataChannel* channel) {
122 rtp_data_channel_ = channel;
123 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200124 DataChannelTransportInterface* data_channel_transport() const;
125 void set_data_channel_transport(DataChannelTransportInterface* transport);
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700126 const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>*
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200127 rtp_data_channels() const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100128
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700129 sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100130 RTC_DCHECK_RUN_ON(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700131 return SignalRtpDataChannelCreated_;
132 }
133 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
134 RTC_DCHECK_RUN_ON(signaling_thread());
135 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100136 }
137 // Called when the transport for the data channels is closed or destroyed.
138 void OnTransportChannelClosed();
139
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700140 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100141
142 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700143 rtc::scoped_refptr<RtpDataChannel> InternalCreateRtpDataChannel(
144 const std::string& label,
145 const DataChannelInit* config) /* RTC_RUN_ON(signaling_thread()) */;
146
147 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
148 const std::string& label,
149 const InternalDataChannelInit*
150 config) /* RTC_RUN_ON(signaling_thread()) */;
151
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100152 // Parses and handles open messages. Returns true if the message is an open
153 // message, false otherwise.
154 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
155 const rtc::CopyOnWriteBuffer& buffer)
156 RTC_RUN_ON(signaling_thread());
157 // Called when a valid data channel OPEN message is received.
158 void OnDataChannelOpenMessage(const std::string& label,
159 const InternalDataChannelInit& config)
160 RTC_RUN_ON(signaling_thread());
161
162 void CreateRemoteRtpDataChannel(const std::string& label,
163 uint32_t remote_ssrc)
164 RTC_RUN_ON(signaling_thread());
165
166 void UpdateClosingRtpDataChannels(
167 const std::vector<std::string>& active_channels,
168 bool is_local_update) RTC_RUN_ON(signaling_thread());
169
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200170 // Called from SendData when data_channel_transport() is true.
171 bool DataChannelSendData(const cricket::SendDataParams& params,
172 const rtc::CopyOnWriteBuffer& payload,
173 cricket::SendDataResult* result);
174
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200175 // Called when all data channels need to be notified of a transport channel
176 // (calls OnTransportChannelCreated on the signaling thread).
177 void NotifyDataChannelsOfTransportCreated();
178
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100179 rtc::Thread* network_thread() const;
180 rtc::Thread* signaling_thread() const;
181
182 // Specifies which kind of data channel is allowed. This is controlled
183 // by the chrome command-line flag and constraints:
184 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
185 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
186 // not set or false, SCTP is allowed (DCT_SCTP);
187 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
188 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
189 cricket::DataChannelType data_channel_type_ =
190 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
191 // signaling and network thread.
192
193 // Plugin transport used for data channels. Pointer may be accessed and
194 // checked from any thread, but the object may only be touched on the
195 // network thread.
196 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
197 // thread.
198 DataChannelTransportInterface* data_channel_transport_ = nullptr;
199
200 // Cached value of whether the data channel transport is ready to send.
201 bool data_channel_transport_ready_to_send_
202 RTC_GUARDED_BY(signaling_thread()) = false;
203
204 // |rtp_data_channel_| is used if in RTP data channel mode,
205 // |data_channel_transport_| when using SCTP.
206 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
207 // TODO(bugs.webrtc.org/9987): Accessed on both
208 // signaling and some other thread.
209
210 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700211 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100212 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700213 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100214 RTC_GUARDED_BY(signaling_thread());
215
216 // Map of label -> DataChannel
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700217 std::map<std::string, rtc::scoped_refptr<RtpDataChannel>> rtp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100218 RTC_GUARDED_BY(signaling_thread());
219
220 // Signals from |data_channel_transport_|. These are invoked on the
221 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200222 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
223 // network thread.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100224 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
225 RTC_GUARDED_BY(signaling_thread());
226 sigslot::signal2<const cricket::ReceiveDataParams&,
227 const rtc::CopyOnWriteBuffer&>
228 SignalDataChannelTransportReceivedData_s
229 RTC_GUARDED_BY(signaling_thread());
230 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
231 RTC_GUARDED_BY(signaling_thread());
232 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
233 RTC_GUARDED_BY(signaling_thread());
234
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700235 sigslot::signal1<RtpDataChannel*> SignalRtpDataChannelCreated_
236 RTC_GUARDED_BY(signaling_thread());
237 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100238 RTC_GUARDED_BY(signaling_thread());
239
Tomas Gunnarsson6da27182020-09-12 23:10:17 +0200240 // Used from the network thread to invoke data channel transport signals on
241 // the signaling thread.
242 rtc::AsyncInvoker data_channel_transport_invoker_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100243 RTC_GUARDED_BY(network_thread());
244
245 // Owning PeerConnection.
246 PeerConnection* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100247 // The weak pointers must be dereferenced and invalidated on the signalling
248 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100249 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100250};
251
252} // namespace webrtc
253
254#endif // PC_DATA_CHANNEL_CONTROLLER_H_