blob: cec79038c68e94f4d3a1511a472b7faf1e09a37f [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 Alvestrand05e4d082019-12-03 14:04:21 +010014#include <string>
15#include <vector>
16
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000017#include "api/data_channel_interface.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000018#include "api/rtc_error.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000019#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000021#include "api/transport/data_channel_transport_interface.h"
22#include "media/base/media_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "pc/data_channel_utils.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070024#include "pc/sctp_data_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000025#include "rtc_base/checks.h"
26#include "rtc_base/copy_on_write_buffer.h"
27#include "rtc_base/ssl_stream_adapter.h"
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +000028#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000029#include "rtc_base/thread.h"
30#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 22:31:42 +010031#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010032
33namespace webrtc {
34
Harald Alvestrand5b84f382022-02-08 10:49:09 +000035class PeerConnectionInternal;
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +000036
Harald Alvestrand9e5aeb92022-05-11 09:35:36 +000037class DataChannelController : public SctpDataChannelControllerInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010038 public DataChannelSink {
39 public:
Harald Alvestrand5b84f382022-02-08 10:49:09 +000040 explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
Harald Alvestrand9e5aeb92022-05-11 09:35:36 +000041 ~DataChannelController();
Harald Alvestrand05e4d082019-12-03 14:04:21 +010042
Harald Alvestrandab813162020-01-09 13:29:56 +010043 // Not copyable or movable.
44 DataChannelController(DataChannelController&) = delete;
45 DataChannelController& operator=(const DataChannelController& other) = delete;
46 DataChannelController(DataChannelController&&) = delete;
47 DataChannelController& operator=(DataChannelController&& other) = delete;
48
Harald Alvestrand7af57c62021-04-16 11:12:14 +000049 // Implements
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070050 // SctpDataChannelProviderInterface.
Florent Castellid95b1492021-05-10 11:29:56 +020051 bool SendData(int sid,
52 const SendDataParams& params,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010053 const rtc::CopyOnWriteBuffer& payload,
54 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070055 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
56 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010057 void AddSctpDataStream(int sid) override;
58 void RemoveSctpDataStream(int sid) override;
59 bool ReadyToSendData() const override;
60
61 // Implements DataChannelSink.
62 void OnDataReceived(int channel_id,
63 DataMessageType type,
64 const rtc::CopyOnWriteBuffer& buffer) override;
65 void OnChannelClosing(int channel_id) override;
66 void OnChannelClosed(int channel_id) override;
67 void OnReadyToSend() override;
Florent Castellidcb9ffc2021-06-29 14:58:23 +020068 void OnTransportClosed(RTCError error) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010069
70 // Called from PeerConnection::SetupDataChannelTransport_n
71 void SetupDataChannelTransport_n();
72 // Called from PeerConnection::TeardownDataChannelTransport_n
73 void TeardownDataChannelTransport_n();
74
75 // Called from PeerConnection::OnTransportChanged
76 // to make required changes to datachannels' transports.
77 void OnTransportChanged(
78 DataChannelTransportInterface* data_channel_transport);
79
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020080 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070081 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020082
Harald Alvestrand05e4d082019-12-03 14:04:21 +010083 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070084 // be offered in a SessionDescription, and wraps it in a proxy object.
85 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010086 const std::string& label,
87 const InternalDataChannelInit*
88 config) /* RTC_RUN_ON(signaling_thread()) */;
89 void AllocateSctpSids(rtc::SSLRole role);
90
Harald Alvestrand05e4d082019-12-03 14:04:21 +010091 // Checks if any data channel has been added.
92 bool HasDataChannels() const;
93 bool HasSctpDataChannels() const {
94 RTC_DCHECK_RUN_ON(signaling_thread());
95 return !sctp_data_channels_.empty();
96 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +010097
98 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +020099 DataChannelTransportInterface* data_channel_transport() const;
100 void set_data_channel_transport(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100101
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000102 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700103 RTC_DCHECK_RUN_ON(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000104 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100105 }
106 // Called when the transport for the data channels is closed or destroyed.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200107 void OnTransportChannelClosed(RTCError error);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100108
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700109 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100110
111 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700112 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
113 const std::string& label,
114 const InternalDataChannelInit*
115 config) /* RTC_RUN_ON(signaling_thread()) */;
116
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100117 // Parses and handles open messages. Returns true if the message is an open
118 // message, false otherwise.
119 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
120 const rtc::CopyOnWriteBuffer& buffer)
121 RTC_RUN_ON(signaling_thread());
122 // Called when a valid data channel OPEN message is received.
123 void OnDataChannelOpenMessage(const std::string& label,
124 const InternalDataChannelInit& config)
125 RTC_RUN_ON(signaling_thread());
126
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200127 // Called from SendData when data_channel_transport() is true.
Florent Castellid95b1492021-05-10 11:29:56 +0200128 bool DataChannelSendData(int sid,
129 const SendDataParams& params,
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200130 const rtc::CopyOnWriteBuffer& payload,
131 cricket::SendDataResult* result);
132
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200133 // Called when all data channels need to be notified of a transport channel
134 // (calls OnTransportChannelCreated on the signaling thread).
135 void NotifyDataChannelsOfTransportCreated();
136
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100137 rtc::Thread* network_thread() const;
138 rtc::Thread* signaling_thread() const;
139
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100140 // Plugin transport used for data channels. Pointer may be accessed and
141 // checked from any thread, but the object may only be touched on the
142 // network thread.
143 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
144 // thread.
145 DataChannelTransportInterface* data_channel_transport_ = nullptr;
146
147 // Cached value of whether the data channel transport is ready to send.
148 bool data_channel_transport_ready_to_send_
149 RTC_GUARDED_BY(signaling_thread()) = false;
150
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100151 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700152 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100153 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700154 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100155 RTC_GUARDED_BY(signaling_thread());
156
Artem Titov880fa812021-07-30 22:30:23 +0200157 // Signals from `data_channel_transport_`. These are invoked on the
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100158 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200159 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
160 // network thread.
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000161 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100162 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000163 sigslot::signal2<const cricket::ReceiveDataParams&,
164 const rtc::CopyOnWriteBuffer&>
165 SignalDataChannelTransportReceivedData_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100166 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000167 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100168 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000169 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100170 RTC_GUARDED_BY(signaling_thread());
171
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000172 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100173 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000174
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100175 // Owning PeerConnection.
Harald Alvestrand5b84f382022-02-08 10:49:09 +0000176 PeerConnectionInternal* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100177 // The weak pointers must be dereferenced and invalidated on the signalling
178 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100179 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100180};
181
182} // namespace webrtc
183
184#endif // PC_DATA_CHANNEL_CONTROLLER_H_