blob: 6b3cd1b46b7f04bc0e5839810eff4d8c0a867103 [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"
Harald Alvestrand8efc9142022-02-07 10:21:51 +000030#include "pc/peer_connection_internal.h"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070031#include "pc/sctp_data_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000032#include "rtc_base/checks.h"
33#include "rtc_base/copy_on_write_buffer.h"
34#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000035#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
Harald Alvestrand7af57c62021-04-16 11:12:14 +000041class DataChannelController : public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010042 public DataChannelSink {
43 public:
Harald Alvestrand8efc9142022-02-07 10:21:51 +000044 explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
Harald Alvestrand05e4d082019-12-03 14:04:21 +010045
Harald Alvestrandab813162020-01-09 13:29:56 +010046 // Not copyable or movable.
47 DataChannelController(DataChannelController&) = delete;
48 DataChannelController& operator=(const DataChannelController& other) = delete;
49 DataChannelController(DataChannelController&&) = delete;
50 DataChannelController& operator=(DataChannelController&& other) = delete;
51
Harald Alvestrand7af57c62021-04-16 11:12:14 +000052 // Implements
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070053 // SctpDataChannelProviderInterface.
Florent Castellid95b1492021-05-10 11:29:56 +020054 bool SendData(int sid,
55 const SendDataParams& params,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010056 const rtc::CopyOnWriteBuffer& payload,
57 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070058 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
59 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010060 void AddSctpDataStream(int sid) override;
61 void RemoveSctpDataStream(int sid) override;
62 bool ReadyToSendData() const override;
63
64 // Implements DataChannelSink.
65 void OnDataReceived(int channel_id,
66 DataMessageType type,
67 const rtc::CopyOnWriteBuffer& buffer) override;
68 void OnChannelClosing(int channel_id) override;
69 void OnChannelClosed(int channel_id) override;
70 void OnReadyToSend() override;
Florent Castellidcb9ffc2021-06-29 14:58:23 +020071 void OnTransportClosed(RTCError error) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010072
73 // Called from PeerConnection::SetupDataChannelTransport_n
74 void SetupDataChannelTransport_n();
75 // Called from PeerConnection::TeardownDataChannelTransport_n
76 void TeardownDataChannelTransport_n();
77
78 // Called from PeerConnection::OnTransportChanged
79 // to make required changes to datachannels' transports.
80 void OnTransportChanged(
81 DataChannelTransportInterface* data_channel_transport);
82
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020083 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070084 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020085
Harald Alvestrand05e4d082019-12-03 14:04:21 +010086 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070087 // be offered in a SessionDescription, and wraps it in a proxy object.
88 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010089 const std::string& label,
90 const InternalDataChannelInit*
91 config) /* RTC_RUN_ON(signaling_thread()) */;
92 void AllocateSctpSids(rtc::SSLRole role);
93
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070094 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010095
96 // Checks if any data channel has been added.
97 bool HasDataChannels() const;
98 bool HasSctpDataChannels() const {
99 RTC_DCHECK_RUN_ON(signaling_thread());
100 return !sctp_data_channels_.empty();
101 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100102
103 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200104 DataChannelTransportInterface* data_channel_transport() const;
105 void set_data_channel_transport(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100106
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000107 template <typename F>
108 void SubscribeDataChannelCreated(F&& callback) {
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700109 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000110 sctp_data_channel_created_callbacks_.AddReceiver(callback);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100111 }
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000112
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100113 // Called when the transport for the data channels is closed or destroyed.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200114 void OnTransportChannelClosed(RTCError error);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100115
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700116 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100117
118 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700119 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
120 const std::string& label,
121 const InternalDataChannelInit*
122 config) /* RTC_RUN_ON(signaling_thread()) */;
123
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100124 // Parses and handles open messages. Returns true if the message is an open
125 // message, false otherwise.
126 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
127 const rtc::CopyOnWriteBuffer& buffer)
128 RTC_RUN_ON(signaling_thread());
129 // Called when a valid data channel OPEN message is received.
130 void OnDataChannelOpenMessage(const std::string& label,
131 const InternalDataChannelInit& config)
132 RTC_RUN_ON(signaling_thread());
133
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200134 // Called from SendData when data_channel_transport() is true.
Florent Castellid95b1492021-05-10 11:29:56 +0200135 bool DataChannelSendData(int sid,
136 const SendDataParams& params,
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200137 const rtc::CopyOnWriteBuffer& payload,
138 cricket::SendDataResult* result);
139
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200140 // Called when all data channels need to be notified of a transport channel
141 // (calls OnTransportChannelCreated on the signaling thread).
142 void NotifyDataChannelsOfTransportCreated();
143
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100144 rtc::Thread* network_thread() const;
145 rtc::Thread* signaling_thread() const;
146
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100147 // Plugin transport used for data channels. Pointer may be accessed and
148 // checked from any thread, but the object may only be touched on the
149 // network thread.
150 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
151 // thread.
152 DataChannelTransportInterface* data_channel_transport_ = nullptr;
153
154 // Cached value of whether the data channel transport is ready to send.
155 bool data_channel_transport_ready_to_send_
156 RTC_GUARDED_BY(signaling_thread()) = false;
157
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100158 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700159 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100160 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700161 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100162 RTC_GUARDED_BY(signaling_thread());
163
Artem Titov880fa812021-07-30 22:30:23 +0200164 // Signals from `data_channel_transport_`. These are invoked on the
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100165 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200166 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
167 // network thread.
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000168 CallbackList<bool> data_transport_writable_callbacks_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100169 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000170 CallbackList<const cricket::ReceiveDataParams&, rtc::CopyOnWriteBuffer>
171 data_channel_transport_received_data_callbacks_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100172 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000173 CallbackList<int> data_channel_transport_channel_closing_callbacks_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100174 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000175 CallbackList<int> data_channel_transport_channel_closed_callbacks_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100176 RTC_GUARDED_BY(signaling_thread());
177
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000178 // Callback listened to for data channel creation.
179 CallbackList<SctpDataChannel*> sctp_data_channel_created_callbacks_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100180 RTC_GUARDED_BY(signaling_thread());
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100181 // Owning PeerConnection.
Harald Alvestrand8efc9142022-02-07 10:21:51 +0000182 PeerConnectionInternal* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100183 // The weak pointers must be dereferenced and invalidated on the signalling
184 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100185 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100186};
187
188} // namespace webrtc
189
190#endif // PC_DATA_CHANNEL_CONTROLLER_H_