blob: 40f4e4c98995a637e6eae9d45a25533736f75625 [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/async_invoker.h"
33#include "rtc_base/checks.h"
34#include "rtc_base/copy_on_write_buffer.h"
35#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000036#include "rtc_base/third_party/sigslot/sigslot.h"
37#include "rtc_base/thread.h"
38#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 22:31:42 +010039#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010040
41namespace webrtc {
42
43class PeerConnection;
44
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070045class DataChannelController : public RtpDataChannelProviderInterface,
46 public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010047 public DataChannelSink {
48 public:
49 explicit DataChannelController(PeerConnection* pc) : pc_(pc) {}
50
Harald Alvestrandab813162020-01-09 13:29:56 +010051 // Not copyable or movable.
52 DataChannelController(DataChannelController&) = delete;
53 DataChannelController& operator=(const DataChannelController& other) = delete;
54 DataChannelController(DataChannelController&&) = delete;
55 DataChannelController& operator=(DataChannelController&& other) = delete;
56
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070057 // Implements RtpDataChannelProviderInterface/
58 // SctpDataChannelProviderInterface.
Harald Alvestrand05e4d082019-12-03 14:04:21 +010059 bool SendData(const cricket::SendDataParams& params,
60 const rtc::CopyOnWriteBuffer& payload,
61 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070062 bool ConnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
63 void DisconnectDataChannel(RtpDataChannel* webrtc_data_channel) override;
64 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
65 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010066 void AddSctpDataStream(int sid) override;
67 void RemoveSctpDataStream(int sid) override;
68 bool ReadyToSendData() const override;
69
70 // Implements DataChannelSink.
71 void OnDataReceived(int channel_id,
72 DataMessageType type,
73 const rtc::CopyOnWriteBuffer& buffer) override;
74 void OnChannelClosing(int channel_id) override;
75 void OnChannelClosed(int channel_id) override;
76 void OnReadyToSend() override;
Harald Alvestrand2697ac12019-12-16 10:37:04 +010077 void OnTransportClosed() override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010078
79 // Called from PeerConnection::SetupDataChannelTransport_n
80 void SetupDataChannelTransport_n();
81 // Called from PeerConnection::TeardownDataChannelTransport_n
82 void TeardownDataChannelTransport_n();
83
84 // Called from PeerConnection::OnTransportChanged
85 // to make required changes to datachannels' transports.
86 void OnTransportChanged(
87 DataChannelTransportInterface* data_channel_transport);
88
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020089 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070090 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020091
Harald Alvestrand05e4d082019-12-03 14:04:21 +010092 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070093 // be offered in a SessionDescription, and wraps it in a proxy object.
94 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010095 const std::string& label,
96 const InternalDataChannelInit*
97 config) /* RTC_RUN_ON(signaling_thread()) */;
98 void AllocateSctpSids(rtc::SSLRole role);
99
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700100 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100101
102 // Checks if any data channel has been added.
103 bool HasDataChannels() const;
104 bool HasSctpDataChannels() const {
105 RTC_DCHECK_RUN_ON(signaling_thread());
106 return !sctp_data_channels_.empty();
107 }
108 bool HasRtpDataChannels() const {
109 RTC_DCHECK_RUN_ON(signaling_thread());
110 return !rtp_data_channels_.empty();
111 }
112
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100113 void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams);
114 void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams);
115
116 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200117 cricket::DataChannelType data_channel_type() const;
118 void set_data_channel_type(cricket::DataChannelType type);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100119 cricket::RtpDataChannel* rtp_data_channel() const {
120 return rtp_data_channel_;
121 }
122 void set_rtp_data_channel(cricket::RtpDataChannel* channel) {
123 rtp_data_channel_ = channel;
124 }
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200125 DataChannelTransportInterface* data_channel_transport() const;
126 void set_data_channel_transport(DataChannelTransportInterface* transport);
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700127 const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>*
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200128 rtp_data_channels() const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100129
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700130 sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() {
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100131 RTC_DCHECK_RUN_ON(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700132 return SignalRtpDataChannelCreated_;
133 }
134 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
135 RTC_DCHECK_RUN_ON(signaling_thread());
136 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100137 }
138 // Called when the transport for the data channels is closed or destroyed.
139 void OnTransportChannelClosed();
140
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700141 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100142
143 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700144 rtc::scoped_refptr<RtpDataChannel> InternalCreateRtpDataChannel(
145 const std::string& label,
146 const DataChannelInit* config) /* RTC_RUN_ON(signaling_thread()) */;
147
148 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
149 const std::string& label,
150 const InternalDataChannelInit*
151 config) /* RTC_RUN_ON(signaling_thread()) */;
152
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100153 // Parses and handles open messages. Returns true if the message is an open
154 // message, false otherwise.
155 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
156 const rtc::CopyOnWriteBuffer& buffer)
157 RTC_RUN_ON(signaling_thread());
158 // Called when a valid data channel OPEN message is received.
159 void OnDataChannelOpenMessage(const std::string& label,
160 const InternalDataChannelInit& config)
161 RTC_RUN_ON(signaling_thread());
162
163 void CreateRemoteRtpDataChannel(const std::string& label,
164 uint32_t remote_ssrc)
165 RTC_RUN_ON(signaling_thread());
166
167 void UpdateClosingRtpDataChannels(
168 const std::vector<std::string>& active_channels,
169 bool is_local_update) RTC_RUN_ON(signaling_thread());
170
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200171 // Called from SendData when data_channel_transport() is true.
172 bool DataChannelSendData(const cricket::SendDataParams& params,
173 const rtc::CopyOnWriteBuffer& payload,
174 cricket::SendDataResult* result);
175
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200176 // Called when all data channels need to be notified of a transport channel
177 // (calls OnTransportChannelCreated on the signaling thread).
178 void NotifyDataChannelsOfTransportCreated();
179
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100180 rtc::Thread* network_thread() const;
181 rtc::Thread* signaling_thread() const;
182
183 // Specifies which kind of data channel is allowed. This is controlled
184 // by the chrome command-line flag and constraints:
185 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
186 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
187 // not set or false, SCTP is allowed (DCT_SCTP);
188 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
189 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
190 cricket::DataChannelType data_channel_type_ =
191 cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both
192 // signaling and network thread.
193
194 // Plugin transport used for data channels. Pointer may be accessed and
195 // checked from any thread, but the object may only be touched on the
196 // network thread.
197 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
198 // thread.
199 DataChannelTransportInterface* data_channel_transport_ = nullptr;
200
201 // Cached value of whether the data channel transport is ready to send.
202 bool data_channel_transport_ready_to_send_
203 RTC_GUARDED_BY(signaling_thread()) = false;
204
205 // |rtp_data_channel_| is used if in RTP data channel mode,
206 // |data_channel_transport_| when using SCTP.
207 cricket::RtpDataChannel* rtp_data_channel_ = nullptr;
208 // TODO(bugs.webrtc.org/9987): Accessed on both
209 // signaling and some other thread.
210
211 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700212 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100213 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700214 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100215 RTC_GUARDED_BY(signaling_thread());
216
217 // Map of label -> DataChannel
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700218 std::map<std::string, rtc::scoped_refptr<RtpDataChannel>> rtp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100219 RTC_GUARDED_BY(signaling_thread());
220
221 // Signals from |data_channel_transport_|. These are invoked on the
222 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200223 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
224 // network thread.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100225 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
226 RTC_GUARDED_BY(signaling_thread());
227 sigslot::signal2<const cricket::ReceiveDataParams&,
228 const rtc::CopyOnWriteBuffer&>
229 SignalDataChannelTransportReceivedData_s
230 RTC_GUARDED_BY(signaling_thread());
231 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
232 RTC_GUARDED_BY(signaling_thread());
233 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
234 RTC_GUARDED_BY(signaling_thread());
235
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700236 sigslot::signal1<RtpDataChannel*> SignalRtpDataChannelCreated_
237 RTC_GUARDED_BY(signaling_thread());
238 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100239 RTC_GUARDED_BY(signaling_thread());
240
Tomas Gunnarsson6da27182020-09-12 23:10:17 +0200241 // Used from the network thread to invoke data channel transport signals on
242 // the signaling thread.
243 rtc::AsyncInvoker data_channel_transport_invoker_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100244 RTC_GUARDED_BY(network_thread());
245
246 // Owning PeerConnection.
247 PeerConnection* const pc_;
Harald Alvestrand246724b2019-12-03 22:31:42 +0100248 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100249};
250
251} // namespace webrtc
252
253#endif // PC_DATA_CHANNEL_CONTROLLER_H_