blob: 11f06072480e8ed6091a0cc3000624b4eb7f0609 [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);
Tomas Gunnarssond69e0702021-04-07 15:14:43 +0200118 cricket::RtpDataChannel* rtp_data_channel() const;
119 void set_rtp_data_channel(cricket::RtpDataChannel* channel);
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200120 DataChannelTransportInterface* data_channel_transport() const;
121 void set_data_channel_transport(DataChannelTransportInterface* transport);
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700122 const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>*
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200123 rtp_data_channels() const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100124
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700125 sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100126 RTC_DCHECK_RUN_ON(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700127 return SignalRtpDataChannelCreated_;
128 }
129 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
130 RTC_DCHECK_RUN_ON(signaling_thread());
131 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100132 }
133 // Called when the transport for the data channels is closed or destroyed.
134 void OnTransportChannelClosed();
135
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700136 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100137
138 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700139 rtc::scoped_refptr<RtpDataChannel> InternalCreateRtpDataChannel(
140 const std::string& label,
141 const DataChannelInit* config) /* RTC_RUN_ON(signaling_thread()) */;
142
143 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
144 const std::string& label,
145 const InternalDataChannelInit*
146 config) /* RTC_RUN_ON(signaling_thread()) */;
147
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100148 // Parses and handles open messages. Returns true if the message is an open
149 // message, false otherwise.
150 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
151 const rtc::CopyOnWriteBuffer& buffer)
152 RTC_RUN_ON(signaling_thread());
153 // Called when a valid data channel OPEN message is received.
154 void OnDataChannelOpenMessage(const std::string& label,
155 const InternalDataChannelInit& config)
156 RTC_RUN_ON(signaling_thread());
157
158 void CreateRemoteRtpDataChannel(const std::string& label,
159 uint32_t remote_ssrc)
160 RTC_RUN_ON(signaling_thread());
161
162 void UpdateClosingRtpDataChannels(
163 const std::vector<std::string>& active_channels,
164 bool is_local_update) RTC_RUN_ON(signaling_thread());
165
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200166 // Called from SendData when data_channel_transport() is true.
167 bool DataChannelSendData(const cricket::SendDataParams& params,
168 const rtc::CopyOnWriteBuffer& payload,
169 cricket::SendDataResult* result);
170
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200171 // Called when all data channels need to be notified of a transport channel
172 // (calls OnTransportChannelCreated on the signaling thread).
173 void NotifyDataChannelsOfTransportCreated();
174
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100175 rtc::Thread* network_thread() const;
176 rtc::Thread* signaling_thread() const;
177
178 // Specifies which kind of data channel is allowed. This is controlled
179 // by the chrome command-line flag and constraints:
180 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
181 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
182 // not set or false, SCTP is allowed (DCT_SCTP);
183 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
184 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
185 cricket::DataChannelType data_channel_type_ =
186 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
187 // signaling and network thread.
188
189 // Plugin transport used for data channels. Pointer may be accessed and
190 // checked from any thread, but the object may only be touched on the
191 // network thread.
192 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
193 // thread.
194 DataChannelTransportInterface* data_channel_transport_ = nullptr;
195
196 // Cached value of whether the data channel transport is ready to send.
197 bool data_channel_transport_ready_to_send_
198 RTC_GUARDED_BY(signaling_thread()) = false;
199
200 // |rtp_data_channel_| is used if in RTP data channel mode,
201 // |data_channel_transport_| when using SCTP.
Tomas Gunnarssond69e0702021-04-07 15:14:43 +0200202 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
203 // thread.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100204 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100205
206 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700207 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100208 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700209 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100210 RTC_GUARDED_BY(signaling_thread());
211
212 // Map of label -> DataChannel
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700213 std::map<std::string, rtc::scoped_refptr<RtpDataChannel>> rtp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100214 RTC_GUARDED_BY(signaling_thread());
215
216 // Signals from |data_channel_transport_|. These are invoked on the
217 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200218 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
219 // network thread.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100220 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
221 RTC_GUARDED_BY(signaling_thread());
222 sigslot::signal2<const cricket::ReceiveDataParams&,
223 const rtc::CopyOnWriteBuffer&>
224 SignalDataChannelTransportReceivedData_s
225 RTC_GUARDED_BY(signaling_thread());
226 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
227 RTC_GUARDED_BY(signaling_thread());
228 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
229 RTC_GUARDED_BY(signaling_thread());
230
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700231 sigslot::signal1<RtpDataChannel*> SignalRtpDataChannelCreated_
232 RTC_GUARDED_BY(signaling_thread());
233 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100234 RTC_GUARDED_BY(signaling_thread());
235
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100236 // Owning PeerConnection.
237 PeerConnection* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100238 // The weak pointers must be dereferenced and invalidated on the signalling
239 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100240 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100241};
242
243} // namespace webrtc
244
245#endif // PC_DATA_CHANNEL_CONTROLLER_H_