blob: fa10b745c688aca129bb5588a32d32752ea2e8b2 [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"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000022#include "api/rtc_error.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 14:31:24 +010024#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000025#include "api/transport/data_channel_transport_interface.h"
26#include "media/base/media_channel.h"
27#include "media/base/media_engine.h"
28#include "media/base/stream_params.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010029#include "pc/channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000030#include "pc/data_channel_utils.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"
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +000035#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000036#include "rtc_base/thread.h"
37#include "rtc_base/thread_annotations.h"
Harald Alvestrand246724b2019-12-03 22:31:42 +010038#include "rtc_base/weak_ptr.h"
Harald Alvestrand05e4d082019-12-03 14:04:21 +010039
40namespace webrtc {
41
Harald Alvestrand5b84f382022-02-08 10:49:09 +000042class PeerConnectionInternal;
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +000043
Harald Alvestrand7af57c62021-04-16 11:12:14 +000044class DataChannelController : public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010045 public DataChannelSink {
46 public:
Harald Alvestrand5b84f382022-02-08 10:49:09 +000047 explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
Harald Alvestrand05e4d082019-12-03 14:04:21 +010048
Harald Alvestrandab813162020-01-09 13:29:56 +010049 // 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:14 +000055 // Implements
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070056 // SctpDataChannelProviderInterface.
Florent Castellid95b1492021-05-10 11:29:56 +020057 bool SendData(int sid,
58 const SendDataParams& params,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010059 const rtc::CopyOnWriteBuffer& payload,
60 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070061 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
62 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010063 void AddSctpDataStream(int sid) override;
64 void RemoveSctpDataStream(int sid) override;
65 bool ReadyToSendData() const override;
66
67 // Implements DataChannelSink.
68 void OnDataReceived(int channel_id,
69 DataMessageType type,
70 const rtc::CopyOnWriteBuffer& buffer) override;
71 void OnChannelClosing(int channel_id) override;
72 void OnChannelClosed(int channel_id) override;
73 void OnReadyToSend() override;
Florent Castellidcb9ffc2021-06-29 14:58:23 +020074 void OnTransportClosed(RTCError error) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010075
76 // Called from PeerConnection::SetupDataChannelTransport_n
77 void SetupDataChannelTransport_n();
78 // Called from PeerConnection::TeardownDataChannelTransport_n
79 void TeardownDataChannelTransport_n();
80
81 // Called from PeerConnection::OnTransportChanged
82 // to make required changes to datachannels' transports.
83 void OnTransportChanged(
84 DataChannelTransportInterface* data_channel_transport);
85
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020086 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070087 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020088
Harald Alvestrand05e4d082019-12-03 14:04:21 +010089 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070090 // be offered in a SessionDescription, and wraps it in a proxy object.
91 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010092 const std::string& label,
93 const InternalDataChannelInit*
94 config) /* RTC_RUN_ON(signaling_thread()) */;
95 void AllocateSctpSids(rtc::SSLRole role);
96
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070097 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010098
99 // Checks if any data channel has been added.
100 bool HasDataChannels() const;
101 bool HasSctpDataChannels() const {
102 RTC_DCHECK_RUN_ON(signaling_thread());
103 return !sctp_data_channels_.empty();
104 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100105
106 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200107 DataChannelTransportInterface* data_channel_transport() const;
108 void set_data_channel_transport(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100109
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000110 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700111 RTC_DCHECK_RUN_ON(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000112 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100113 }
114 // Called when the transport for the data channels is closed or destroyed.
Florent Castellidcb9ffc2021-06-29 14:58:23 +0200115 void OnTransportChannelClosed(RTCError error);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100116
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700117 void OnSctpDataChannelClosed(SctpDataChannel* channel);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100118
119 private:
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700120 rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
121 const std::string& label,
122 const InternalDataChannelInit*
123 config) /* RTC_RUN_ON(signaling_thread()) */;
124
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100125 // Parses and handles open messages. Returns true if the message is an open
126 // message, false otherwise.
127 bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
128 const rtc::CopyOnWriteBuffer& buffer)
129 RTC_RUN_ON(signaling_thread());
130 // Called when a valid data channel OPEN message is received.
131 void OnDataChannelOpenMessage(const std::string& label,
132 const InternalDataChannelInit& config)
133 RTC_RUN_ON(signaling_thread());
134
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200135 // Called from SendData when data_channel_transport() is true.
Florent Castellid95b1492021-05-10 11:29:56 +0200136 bool DataChannelSendData(int sid,
137 const SendDataParams& params,
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200138 const rtc::CopyOnWriteBuffer& payload,
139 cricket::SendDataResult* result);
140
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +0200141 // Called when all data channels need to be notified of a transport channel
142 // (calls OnTransportChannelCreated on the signaling thread).
143 void NotifyDataChannelsOfTransportCreated();
144
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100145 rtc::Thread* network_thread() const;
146 rtc::Thread* signaling_thread() const;
147
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100148 // Plugin transport used for data channels. Pointer may be accessed and
149 // checked from any thread, but the object may only be touched on the
150 // network thread.
151 // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
152 // thread.
153 DataChannelTransportInterface* data_channel_transport_ = nullptr;
154
155 // Cached value of whether the data channel transport is ready to send.
156 bool data_channel_transport_ready_to_send_
157 RTC_GUARDED_BY(signaling_thread()) = false;
158
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100159 SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700160 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100161 RTC_GUARDED_BY(signaling_thread());
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700162 std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100163 RTC_GUARDED_BY(signaling_thread());
164
Artem Titov880fa812021-07-30 22:30:23 +0200165 // Signals from `data_channel_transport_`. These are invoked on the
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100166 // signaling thread.
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200167 // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
168 // network thread.
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000169 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100170 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000171 sigslot::signal2<const cricket::ReceiveDataParams&,
172 const rtc::CopyOnWriteBuffer&>
173 SignalDataChannelTransportReceivedData_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100174 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000175 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100176 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000177 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100178 RTC_GUARDED_BY(signaling_thread());
179
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000180 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100181 RTC_GUARDED_BY(signaling_thread());
Mirko Bonadeie0bc8d22022-02-08 07:41:25 +0000182
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100183 // Owning PeerConnection.
Harald Alvestrand5b84f382022-02-08 10:49:09 +0000184 PeerConnectionInternal* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100185 // The weak pointers must be dereferenced and invalidated on the signalling
186 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100187 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100188};
189
190} // namespace webrtc
191
192#endif // PC_DATA_CHANNEL_CONTROLLER_H_