blob: bf3ac03437141f01ff3c9b47e37ad7b4d44c973c [file] [log] [blame]
Harald Alvestrand05e4d082019-12-03 13:04:211/*
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 13:04:2114#include <string>
15#include <vector>
16
Harald Alvestrand5761e7b2021-01-29 14:45:0817#include "api/data_channel_interface.h"
Harald Alvestrandc24a2182022-02-23 13:44:5918#include "api/rtc_error.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0819#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2420#include "api/sequence_checker.h"
Tommi13759ba2023-03-06 11:51:3921#include "api/task_queue/pending_task_safety_flag.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0822#include "api/transport/data_channel_transport_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "pc/data_channel_utils.h"
Taylor Brandstetter3a034e12020-07-09 22:32:3424#include "pc/sctp_data_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0825#include "rtc_base/checks.h"
26#include "rtc_base/copy_on_write_buffer.h"
27#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0828#include "rtc_base/thread.h"
29#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 21:31:4230#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 13:04:2131
32namespace webrtc {
33
Harald Alvestrand5b84f382022-02-08 10:49:0934class PeerConnectionInternal;
Mirko Bonadeie0bc8d22022-02-08 07:41:2535
Harald Alvestrand9e5aeb92022-05-11 09:35:3636class DataChannelController : public SctpDataChannelControllerInterface,
Harald Alvestrand05e4d082019-12-03 13:04:2137 public DataChannelSink {
38 public:
Harald Alvestrand5b84f382022-02-08 10:49:0939 explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
Harald Alvestrand9e5aeb92022-05-11 09:35:3640 ~DataChannelController();
Harald Alvestrand05e4d082019-12-03 13:04:2141
Harald Alvestrandab813162020-01-09 12:29:5642 // Not copyable or movable.
43 DataChannelController(DataChannelController&) = delete;
44 DataChannelController& operator=(const DataChannelController& other) = delete;
45 DataChannelController(DataChannelController&&) = delete;
46 DataChannelController& operator=(DataChannelController&& other) = delete;
47
Harald Alvestrand7af57c62021-04-16 11:12:1448 // Implements
Taylor Brandstetter3a034e12020-07-09 22:32:3449 // SctpDataChannelProviderInterface.
Tommi1fabbac2023-03-21 13:48:5150 RTCError SendData(StreamId sid,
51 const SendDataParams& params,
52 const rtc::CopyOnWriteBuffer& payload) override;
Tommi4c842222023-03-21 10:35:2453 void AddSctpDataStream(StreamId sid) override;
54 void RemoveSctpDataStream(StreamId sid) override;
Tommid2afbaf2023-03-02 09:51:1655 void OnChannelStateChanged(SctpDataChannel* channel,
56 DataChannelInterface::DataState state) override;
Harald Alvestrand05e4d082019-12-03 13:04:2157
58 // Implements DataChannelSink.
59 void OnDataReceived(int channel_id,
60 DataMessageType type,
61 const rtc::CopyOnWriteBuffer& buffer) override;
62 void OnChannelClosing(int channel_id) override;
63 void OnChannelClosed(int channel_id) override;
64 void OnReadyToSend() override;
Florent Castellidcb9ffc2021-06-29 12:58:2365 void OnTransportClosed(RTCError error) override;
Harald Alvestrand05e4d082019-12-03 13:04:2166
Tommi1f708ef2023-03-31 16:40:5067 // Called as part of destroying the owning PeerConnection.
68 void PrepareForShutdown();
69
Harald Alvestrand05e4d082019-12-03 13:04:2170 // Called from PeerConnection::SetupDataChannelTransport_n
Tommiaa3c9f22023-04-18 10:19:1971 void SetupDataChannelTransport_n(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 13:04:2172 // Called from PeerConnection::TeardownDataChannelTransport_n
Tommib00d63c2023-04-12 17:49:5373 void TeardownDataChannelTransport_n(RTCError error);
Harald Alvestrand05e4d082019-12-03 13:04:2174
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 14:54:1080 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 22:32:3481 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 14:54:1082
Harald Alvestrand05e4d082019-12-03 13:04:2183 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 22:32:3484 // be offered in a SessionDescription, and wraps it in a proxy object.
Tommi4f7ade52023-03-29 18:46:5985 RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>>
86 InternalCreateDataChannelWithProxy(const std::string& label,
87 const InternalDataChannelInit& config);
Harald Alvestrand05e4d082019-12-03 13:04:2188 void AllocateSctpSids(rtc::SSLRole role);
89
Philipp Hancke522380f2023-05-09 07:41:0390 // Check if data channels are currently tracked. Used to decide whether a
91 // rejected m=application section should be reoffered.
92 bool HasDataChannels() const;
Tommi77158ac2023-03-30 07:05:2393
Harald Alvestrand5da3eb02023-03-15 20:39:4294 // At some point in time, a data channel has existed.
95 bool HasUsedDataChannels() const;
Harald Alvestrand05e4d082019-12-03 13:04:2196
Tommi52719652023-04-04 09:59:5597 protected:
98 rtc::Thread* network_thread() const;
99 rtc::Thread* signaling_thread() const;
100
Harald Alvestrand05e4d082019-12-03 13:04:21101 private:
Tommi44ebe2a2023-05-15 13:14:10102 void OnSctpDataChannelClosed(SctpDataChannel* channel);
103
Tommif9e13f82023-04-06 19:21:45104 // Creates a new SctpDataChannel object on the network thread.
105 RTCErrorOr<rtc::scoped_refptr<SctpDataChannel>> CreateDataChannel(
106 const std::string& label,
107 InternalDataChannelInit& config) RTC_RUN_ON(network_thread());
108
Harald Alvestrand05e4d082019-12-03 13:04:21109 // Parses and handles open messages. Returns true if the message is an open
Tommi5bbfb002023-03-04 15:47:53110 // message and should be considered to be handled, false otherwise.
Tommi4e1c9572023-03-15 11:36:20111 bool HandleOpenMessage_n(int channel_id,
112 DataMessageType type,
Harald Alvestrand05e4d082019-12-03 13:04:21113 const rtc::CopyOnWriteBuffer& buffer)
Tommi5bbfb002023-03-04 15:47:53114 RTC_RUN_ON(network_thread());
Harald Alvestrand05e4d082019-12-03 13:04:21115 // Called when a valid data channel OPEN message is received.
Tommif9e13f82023-04-06 19:21:45116 void OnDataChannelOpenMessage(rtc::scoped_refptr<SctpDataChannel> channel,
117 bool ready_to_send)
Harald Alvestrand05e4d082019-12-03 13:04:21118 RTC_RUN_ON(signaling_thread());
119
Tommi4f7ade52023-03-29 18:46:59120 // Accepts a `StreamId` which may be pre-negotiated or unassigned. For
121 // pre-negotiated sids, attempts to reserve the sid in the allocation pool,
122 // for unassigned sids attempts to generate a new sid if possible. Returns
123 // RTCError::OK() if the sid is reserved (and may have been generated) or
124 // if not enough information exists to generate a sid, in which case the sid
125 // will still be unassigned upon return, but will be assigned later.
126 // If the pool has been exhausted or a sid has already been reserved, an
127 // error will be returned.
128 RTCError ReserveOrAllocateSid(StreamId& sid,
129 absl::optional<rtc::SSLRole> fallback_ssl_role)
130 RTC_RUN_ON(network_thread());
131
Tomas Gunnarsson2e94de52020-06-16 14:54:10132 // Called when all data channels need to be notified of a transport channel
133 // (calls OnTransportChannelCreated on the signaling thread).
134 void NotifyDataChannelsOfTransportCreated();
135
Tommiaa3c9f22023-04-18 10:19:19136 void set_data_channel_transport(DataChannelTransportInterface* transport);
137
Harald Alvestrand05e4d082019-12-03 13:04:21138 // Plugin transport used for data channels. Pointer may be accessed and
139 // checked from any thread, but the object may only be touched on the
140 // network thread.
Tommiadd7ac02023-04-12 10:01:10141 DataChannelTransportInterface* data_channel_transport_
142 RTC_GUARDED_BY(network_thread()) = nullptr;
Tommi4f7ade52023-03-29 18:46:59143 SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(network_thread());
Tommi4f7ade52023-03-29 18:46:59144 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_n_
145 RTC_GUARDED_BY(network_thread());
Tommi44ebe2a2023-05-15 13:14:10146 enum class DataChannelUsage : uint8_t {
147 kNeverUsed = 0,
148 kHaveBeenUsed,
149 kInUse
150 };
151 DataChannelUsage channel_usage_ RTC_GUARDED_BY(signaling_thread()) =
152 DataChannelUsage::kNeverUsed;
Harald Alvestrand05e4d082019-12-03 13:04:21153
Harald Alvestrand05e4d082019-12-03 13:04:21154 // Owning PeerConnection.
Harald Alvestrand5b84f382022-02-08 10:49:09155 PeerConnectionInternal* const pc_;
Tommif9e13f82023-04-06 19:21:45156 // The weak pointers must be dereferenced and invalidated on the network
Niels Möller236e36c2021-03-23 08:23:10157 // thread only.
Tommif9e13f82023-04-06 19:21:45158 rtc::WeakPtrFactory<DataChannelController> weak_factory_
159 RTC_GUARDED_BY(network_thread()){this};
Tommi13759ba2023-03-06 11:51:39160 ScopedTaskSafety signaling_safety_;
Harald Alvestrand05e4d082019-12-03 13:04:21161};
162
163} // namespace webrtc
164
165#endif // PC_DATA_CHANNEL_CONTROLLER_H_