blob: 4c42b8a345cea96e9eb9142e11b6934408f0c88e [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/sctp_data_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000031#include "rtc_base/checks.h"
32#include "rtc_base/copy_on_write_buffer.h"
33#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000034#include "rtc_base/third_party/sigslot/sigslot.h"
35#include "rtc_base/thread.h"
36#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 22:31:42 +010037#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010038
39namespace webrtc {
40
41class PeerConnection;
42
Harald Alvestrand7af57c62021-04-16 11:12:14 +000043class DataChannelController : public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010044 public DataChannelSink {
45 public:
46 explicit DataChannelController(PeerConnection* pc) : pc_(pc) {}
47
Harald Alvestrandab813162020-01-09 13:29:56 +010048 // Not copyable or movable.
49 DataChannelController(DataChannelController&) = delete;
50 DataChannelController& operator=(const DataChannelController& other) = delete;
51 DataChannelController(DataChannelController&&) = delete;
52 DataChannelController& operator=(DataChannelController&& other) = delete;
53
Harald Alvestrand7af57c62021-04-16 11:12:14 +000054 // Implements
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070055 // SctpDataChannelProviderInterface.
Harald Alvestrand05e4d082019-12-03 14:04:21 +010056 bool SendData(const cricket::SendDataParams& params,
57 const rtc::CopyOnWriteBuffer& payload,
58 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070059 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
60 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010061 void AddSctpDataStream(int sid) override;
62 void RemoveSctpDataStream(int sid) override;
63 bool ReadyToSendData() const override;
64
65 // Implements DataChannelSink.
66 void OnDataReceived(int channel_id,
67 DataMessageType type,
68 const rtc::CopyOnWriteBuffer& buffer) override;
69 void OnChannelClosing(int channel_id) override;
70 void OnChannelClosed(int channel_id) override;
71 void OnReadyToSend() override;
Harald Alvestrand2697ac12019-12-16 10:37:04 +010072 void OnTransportClosed() override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010073
74 // Called from PeerConnection::SetupDataChannelTransport_n
75 void SetupDataChannelTransport_n();
76 // Called from PeerConnection::TeardownDataChannelTransport_n
77 void TeardownDataChannelTransport_n();
78
79 // Called from PeerConnection::OnTransportChanged
80 // to make required changes to datachannels' transports.
81 void OnTransportChanged(
82 DataChannelTransportInterface* data_channel_transport);
83
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020084 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070085 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020086
Harald Alvestrand05e4d082019-12-03 14:04:21 +010087 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070088 // be offered in a SessionDescription, and wraps it in a proxy object.
89 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010090 const std::string& label,
91 const InternalDataChannelInit*
92 config) /* RTC_RUN_ON(signaling_thread()) */;
93 void AllocateSctpSids(rtc::SSLRole role);
94
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070095 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010096
97 // Checks if any data channel has been added.
98 bool HasDataChannels() const;
99 bool HasSctpDataChannels() const {
100 RTC_DCHECK_RUN_ON(signaling_thread());
101 return !sctp_data_channels_.empty();
102 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100103
104 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200105 DataChannelTransportInterface* data_channel_transport() const;
106 void set_data_channel_transport(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100107
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700108 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
109 RTC_DCHECK_RUN_ON(signaling_thread());
110 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100111 }
112 // Called when the transport for the data channels is closed or destroyed.
113 void OnTransportChannelClosed();
114
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700115 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100116
117 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700118 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
119 const std::string& label,
120 const InternalDataChannelInit*
121 config) /* RTC_RUN_ON(signaling_thread()) */;
122
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100123 // Parses and handles open messages. Returns true if the message is an open
124 // message, false otherwise.
125 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
126 const rtc::CopyOnWriteBuffer& buffer)
127 RTC_RUN_ON(signaling_thread());
128 // Called when a valid data channel OPEN message is received.
129 void OnDataChannelOpenMessage(const std::string& label,
130 const InternalDataChannelInit& config)
131 RTC_RUN_ON(signaling_thread());
132
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200133 // Called from SendData when data_channel_transport() is true.
134 bool DataChannelSendData(const cricket::SendDataParams& params,
135 const rtc::CopyOnWriteBuffer& payload,
136 cricket::SendDataResult* result);
137
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200138 // Called when all data channels need to be notified of a transport channel
139 // (calls OnTransportChannelCreated on the signaling thread).
140 void NotifyDataChannelsOfTransportCreated();
141
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100142 rtc::Thread* network_thread() const;
143 rtc::Thread* signaling_thread() const;
144
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100145 // Plugin transport used for data channels. Pointer may be accessed and
146 // checked from any thread, but the object may only be touched on the
147 // network thread.
148 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
149 // thread.
150 DataChannelTransportInterface* data_channel_transport_ = nullptr;
151
152 // Cached value of whether the data channel transport is ready to send.
153 bool data_channel_transport_ready_to_send_
154 RTC_GUARDED_BY(signaling_thread()) = false;
155
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100156 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700157 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100158 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700159 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100160 RTC_GUARDED_BY(signaling_thread());
161
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100162 // Signals from |data_channel_transport_|. These are invoked on the
163 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200164 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
165 // network thread.
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100166 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
167 RTC_GUARDED_BY(signaling_thread());
168 sigslot::signal2<const cricket::ReceiveDataParams&,
169 const rtc::CopyOnWriteBuffer&>
170 SignalDataChannelTransportReceivedData_s
171 RTC_GUARDED_BY(signaling_thread());
172 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
173 RTC_GUARDED_BY(signaling_thread());
174 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
175 RTC_GUARDED_BY(signaling_thread());
176
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700177 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100178 RTC_GUARDED_BY(signaling_thread());
179
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100180 // Owning PeerConnection.
181 PeerConnection* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100182 // The weak pointers must be dereferenced and invalidated on the signalling
183 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100184 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100185};
186
187} // namespace webrtc
188
189#endif // PC_DATA_CHANNEL_CONTROLLER_H_