Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: b9e8c6000897b9877e6907fa60e60727fd22ebd1 [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 Alvestrandf0d5caf2025-04-07 09:08:5114#include <cstddef>
15#include <cstdint>
Philip Eliasson17ddd312025-04-08 08:33:4916#include <memory>
Harald Alvestrandf0d5caf2025-04-07 09:08:5117#include <optional>
Harald Alvestrand05e4d082019-12-03 13:04:2118#include <string>
19#include <vector>
20
Philip Eliasson17ddd312025-04-08 08:33:4921#include "api/array_view.h"
22#include "api/data_channel_event_observer_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0823#include "api/data_channel_interface.h"
Florent Castelli0012bfa2024-07-26 16:16:4124#include "api/priority.h"
Harald Alvestrandc24a2182022-02-23 13:44:5925#include "api/rtc_error.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0826#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2427#include "api/sequence_checker.h"
Tommi13759ba2023-03-06 11:51:3928#include "api/task_queue/pending_task_safety_flag.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0829#include "api/transport/data_channel_transport_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0830#include "pc/data_channel_utils.h"
Taylor Brandstetter3a034e12020-07-09 22:32:3431#include "pc/sctp_data_channel.h"
Harald Alvestrandf0d5caf2025-04-07 09:08:5132#include "pc/sctp_utils.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0833#include "rtc_base/copy_on_write_buffer.h"
34#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0835#include "rtc_base/thread.h"
36#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 21:31:4237#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 13:04:2138
39namespace webrtc {
40
Harald Alvestrand5b84f382022-02-08 10:49:0941class PeerConnectionInternal;
Mirko Bonadeie0bc8d22022-02-08 07:41:2542
Harald Alvestrand9e5aeb92022-05-11 09:35:3643class DataChannelController : public SctpDataChannelControllerInterface,
Harald Alvestrand05e4d082019-12-03 13:04:2144 public DataChannelSink {
45 public:
Harald Alvestrand5b84f382022-02-08 10:49:0946 explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
Harald Alvestrand9e5aeb92022-05-11 09:35:3647 ~DataChannelController();
Harald Alvestrand05e4d082019-12-03 13:04:2148
Harald Alvestrandab813162020-01-09 12:29:5649 // Not copyable or movable.
50 DataChannelController(DataChannelController&) = delete;
51 DataChannelController& operator=(const DataChannelController& other) = delete;
52 DataChannelController(DataChannelController&&) = delete;
53 DataChannelController& operator=(DataChannelController&& other) = delete;
54
Harald Alvestrand7af57c62021-04-16 11:12:1455 // Implements
Taylor Brandstetter3a034e12020-07-09 22:32:3456 // SctpDataChannelProviderInterface.
Tommi1fabbac2023-03-21 13:48:5157 RTCError SendData(StreamId sid,
58 const SendDataParams& params,
Evan Shrubsolee6a1f702025-04-15 14:55:4259 const CopyOnWriteBuffer& payload) override;
Florent Castelli0012bfa2024-07-26 16:16:4160 void AddSctpDataStream(StreamId sid, PriorityValue priority) override;
Tommi4c842222023-03-21 10:35:2461 void RemoveSctpDataStream(StreamId sid) override;
Tommid2afbaf2023-03-02 09:51:1662 void OnChannelStateChanged(SctpDataChannel* channel,
63 DataChannelInterface::DataState state) override;
Victor Boiviefea41f52024-03-11 15:43:3164 size_t buffered_amount(StreamId sid) const override;
Victor Boiviecdecc4e2024-03-18 12:47:3465 size_t buffered_amount_low_threshold(StreamId sid) const override;
66 void SetBufferedAmountLowThreshold(StreamId sid, size_t bytes) override;
Harald Alvestrand05e4d082019-12-03 13:04:2167
68 // Implements DataChannelSink.
69 void OnDataReceived(int channel_id,
70 DataMessageType type,
Evan Shrubsolee6a1f702025-04-15 14:55:4271 const CopyOnWriteBuffer& buffer) override;
Harald Alvestrand05e4d082019-12-03 13:04:2172 void OnChannelClosing(int channel_id) override;
73 void OnChannelClosed(int channel_id) override;
74 void OnReadyToSend() override;
Florent Castellidcb9ffc2021-06-29 12:58:2375 void OnTransportClosed(RTCError error) override;
Victor Boiviecdecc4e2024-03-18 12:47:3476 void OnBufferedAmountLow(int channel_id) override;
Harald Alvestrand05e4d082019-12-03 13:04:2177
Tommi1f708ef2023-03-31 16:40:5078 // Called as part of destroying the owning PeerConnection.
79 void PrepareForShutdown();
80
Harald Alvestrand05e4d082019-12-03 13:04:2181 // Called from PeerConnection::SetupDataChannelTransport_n
Tommiaa3c9f22023-04-18 10:19:1982 void SetupDataChannelTransport_n(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 13:04:2183 // Called from PeerConnection::TeardownDataChannelTransport_n
Tommib00d63c2023-04-12 17:49:5384 void TeardownDataChannelTransport_n(RTCError error);
Harald Alvestrand05e4d082019-12-03 13:04:2185
86 // Called from PeerConnection::OnTransportChanged
87 // to make required changes to datachannels' transports.
88 void OnTransportChanged(
89 DataChannelTransportInterface* data_channel_transport);
90
Tomas Gunnarsson2e94de52020-06-16 14:54:1091 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 22:32:3492 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 14:54:1093
Harald Alvestrand05e4d082019-12-03 13:04:2194 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 22:32:3495 // be offered in a SessionDescription, and wraps it in a proxy object.
Evan Shrubsolee6a1f702025-04-15 14:55:4296 RTCErrorOr<scoped_refptr<DataChannelInterface>>
Tommi4f7ade52023-03-29 18:46:5997 InternalCreateDataChannelWithProxy(const std::string& label,
98 const InternalDataChannelInit& config);
Evan Shrubsoleeb835d02025-03-12 09:41:0699 void AllocateSctpSids(SSLRole role);
Harald Alvestrand05e4d082019-12-03 13:04:21100
Philipp Hancke522380f2023-05-09 07:41:03101 // Check if data channels are currently tracked. Used to decide whether a
102 // rejected m=application section should be reoffered.
103 bool HasDataChannels() const;
Tommi77158ac2023-03-30 07:05:23104
Harald Alvestrand5da3eb02023-03-15 20:39:42105 // At some point in time, a data channel has existed.
106 bool HasUsedDataChannels() const;
Harald Alvestrand05e4d082019-12-03 13:04:21107
Philip Eliasson17ddd312025-04-08 08:33:49108 void SetEventObserver(
109 std::unique_ptr<DataChannelEventObserverInterface> observer);
110
Tommi52719652023-04-04 09:59:55111 protected:
Evan Shrubsole6a9a1ae2025-03-21 12:54:15112 Thread* network_thread() const;
113 Thread* signaling_thread() const;
Tommi52719652023-04-04 09:59:55114
Harald Alvestrand05e4d082019-12-03 13:04:21115 private:
Tommi44ebe2a2023-05-15 13:14:10116 void OnSctpDataChannelClosed(SctpDataChannel* channel);
117
Tommif9e13f82023-04-06 19:21:45118 // Creates a new SctpDataChannel object on the network thread.
Evan Shrubsolee6a1f702025-04-15 14:55:42119 RTCErrorOr<scoped_refptr<SctpDataChannel>> CreateDataChannel(
Tommif9e13f82023-04-06 19:21:45120 const std::string& label,
121 InternalDataChannelInit& config) RTC_RUN_ON(network_thread());
122
Harald Alvestrand05e4d082019-12-03 13:04:21123 // Parses and handles open messages. Returns true if the message is an open
Tommi5bbfb002023-03-04 15:47:53124 // message and should be considered to be handled, false otherwise.
Tommi4e1c9572023-03-15 11:36:20125 bool HandleOpenMessage_n(int channel_id,
126 DataMessageType type,
Evan Shrubsolee6a1f702025-04-15 14:55:42127 const CopyOnWriteBuffer& buffer)
Tommi5bbfb002023-03-04 15:47:53128 RTC_RUN_ON(network_thread());
Harald Alvestrand05e4d082019-12-03 13:04:21129 // Called when a valid data channel OPEN message is received.
Evan Shrubsolee6a1f702025-04-15 14:55:42130 void OnDataChannelOpenMessage(scoped_refptr<SctpDataChannel> channel,
Tommif9e13f82023-04-06 19:21:45131 bool ready_to_send)
Harald Alvestrand05e4d082019-12-03 13:04:21132 RTC_RUN_ON(signaling_thread());
133
Tommi4f7ade52023-03-29 18:46:59134 // Accepts a `StreamId` which may be pre-negotiated or unassigned. For
135 // pre-negotiated sids, attempts to reserve the sid in the allocation pool,
136 // for unassigned sids attempts to generate a new sid if possible. Returns
137 // RTCError::OK() if the sid is reserved (and may have been generated) or
138 // if not enough information exists to generate a sid, in which case the sid
139 // will still be unassigned upon return, but will be assigned later.
140 // If the pool has been exhausted or a sid has already been reserved, an
141 // error will be returned.
Florent Castelli8037fc62024-08-29 13:00:40142 RTCError ReserveOrAllocateSid(std::optional<StreamId>& sid,
Evan Shrubsoleeb835d02025-03-12 09:41:06143 std::optional<SSLRole> fallback_ssl_role)
Tommi4f7ade52023-03-29 18:46:59144 RTC_RUN_ON(network_thread());
145
Tomas Gunnarsson2e94de52020-06-16 14:54:10146 // Called when all data channels need to be notified of a transport channel
147 // (calls OnTransportChannelCreated on the signaling thread).
148 void NotifyDataChannelsOfTransportCreated();
149
Tommiaa3c9f22023-04-18 10:19:19150 void set_data_channel_transport(DataChannelTransportInterface* transport);
151
Philip Eliasson17ddd312025-04-08 08:33:49152 std::optional<DataChannelEventObserverInterface::Message>
153 BuildObserverMessage(
154 StreamId sid,
155 DataMessageType type,
156 ArrayView<const uint8_t> payload,
157 DataChannelEventObserverInterface::Message::Direction direction) const
158 RTC_RUN_ON(network_thread());
159
Harald Alvestrand05e4d082019-12-03 13:04:21160 // Plugin transport used for data channels. Pointer may be accessed and
161 // checked from any thread, but the object may only be touched on the
162 // network thread.
Tommiadd7ac02023-04-12 10:01:10163 DataChannelTransportInterface* data_channel_transport_
164 RTC_GUARDED_BY(network_thread()) = nullptr;
Tommi4f7ade52023-03-29 18:46:59165 SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(network_thread());
Evan Shrubsolee6a1f702025-04-15 14:55:42166 std::vector<scoped_refptr<SctpDataChannel>> sctp_data_channels_n_
Tommi4f7ade52023-03-29 18:46:59167 RTC_GUARDED_BY(network_thread());
Tommi44ebe2a2023-05-15 13:14:10168 enum class DataChannelUsage : uint8_t {
169 kNeverUsed = 0,
170 kHaveBeenUsed,
171 kInUse
172 };
173 DataChannelUsage channel_usage_ RTC_GUARDED_BY(signaling_thread()) =
174 DataChannelUsage::kNeverUsed;
Harald Alvestrand05e4d082019-12-03 13:04:21175
Philip Eliasson17ddd312025-04-08 08:33:49176 std::unique_ptr<DataChannelEventObserverInterface> event_observer_;
177
Harald Alvestrand05e4d082019-12-03 13:04:21178 // Owning PeerConnection.
Harald Alvestrand5b84f382022-02-08 10:49:09179 PeerConnectionInternal* const pc_;
Tommif9e13f82023-04-06 19:21:45180 // The weak pointers must be dereferenced and invalidated on the network
Niels Möller236e36c2021-03-23 08:23:10181 // thread only.
Evan Shrubsoled01d07d2025-02-25 09:34:57182 WeakPtrFactory<DataChannelController> weak_factory_
Tommif9e13f82023-04-06 19:21:45183 RTC_GUARDED_BY(network_thread()){this};
Tommi13759ba2023-03-06 11:51:39184 ScopedTaskSafety signaling_safety_;
Harald Alvestrand05e4d082019-12-03 13:04:21185};
186
187} // namespace webrtc
188
189#endif // PC_DATA_CHANNEL_CONTROLLER_H_