blob: 05fcff0e0334f2193296d8fcf428af6d42e5423e [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"
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070030#include "pc/sctp_data_channel.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000031#include "rtc_base/checks.h"
32#include "rtc_base/copy_on_write_buffer.h"
33#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000034#include "rtc_base/third_party/sigslot/sigslot.h"
35#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
41class PeerConnection;
42
Harald Alvestrand7af57c62021-04-16 11:12:14 +000043class DataChannelController : public SctpDataChannelProviderInterface,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010044 public DataChannelSink {
45 public:
46 explicit DataChannelController(PeerConnection* pc) : pc_(pc) {}
47
Harald Alvestrandab813162020-01-09 13:29:56 +010048 // Not copyable or movable.
49 DataChannelController(DataChannelController&) = delete;
50 DataChannelController& operator=(const DataChannelController& other) = delete;
51 DataChannelController(DataChannelController&&) = delete;
52 DataChannelController& operator=(DataChannelController&& other) = delete;
53
Harald Alvestrand7af57c62021-04-16 11:12:14 +000054 // Implements
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070055 // SctpDataChannelProviderInterface.
Florent Castellid95b1492021-05-10 11:29:56 +020056 bool SendData(int sid,
57 const SendDataParams& params,
Harald Alvestrand05e4d082019-12-03 14:04:21 +010058 const rtc::CopyOnWriteBuffer& payload,
59 cricket::SendDataResult* result) override;
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070060 bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
61 void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010062 void AddSctpDataStream(int sid) override;
63 void RemoveSctpDataStream(int sid) override;
64 bool ReadyToSendData() const override;
65
66 // Implements DataChannelSink.
67 void OnDataReceived(int channel_id,
68 DataMessageType type,
69 const rtc::CopyOnWriteBuffer& buffer) override;
70 void OnChannelClosing(int channel_id) override;
71 void OnChannelClosed(int channel_id) override;
72 void OnReadyToSend() override;
Harald Alvestrand2697ac12019-12-16 10:37:04 +010073 void OnTransportClosed() override;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010074
75 // Called from PeerConnection::SetupDataChannelTransport_n
76 void SetupDataChannelTransport_n();
77 // Called from PeerConnection::TeardownDataChannelTransport_n
78 void TeardownDataChannelTransport_n();
79
80 // Called from PeerConnection::OnTransportChanged
81 // to make required changes to datachannels' transports.
82 void OnTransportChanged(
83 DataChannelTransportInterface* data_channel_transport);
84
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020085 // Called from PeerConnection::GetDataChannelStats on the signaling thread.
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070086 std::vector<DataChannelStats> GetDataChannelStats() const;
Tomas Gunnarsson2e94de52020-06-16 16:54:10 +020087
Harald Alvestrand05e4d082019-12-03 14:04:21 +010088 // Creates channel and adds it to the collection of DataChannels that will
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070089 // be offered in a SessionDescription, and wraps it in a proxy object.
90 rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
Harald Alvestrand05e4d082019-12-03 14:04:21 +010091 const std::string& label,
92 const InternalDataChannelInit*
93 config) /* RTC_RUN_ON(signaling_thread()) */;
94 void AllocateSctpSids(rtc::SSLRole role);
95
Taylor Brandstetter3a034e12020-07-09 15:32:34 -070096 SctpDataChannel* FindDataChannelBySid(int sid) const;
Harald Alvestrand05e4d082019-12-03 14:04:21 +010097
98 // Checks if any data channel has been added.
99 bool HasDataChannels() const;
100 bool HasSctpDataChannels() const {
101 RTC_DCHECK_RUN_ON(signaling_thread());
102 return !sctp_data_channels_.empty();
103 }
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100104
105 // Accessors
Tomas Gunnarsson7d3cfbf2020-06-15 13:47:42 +0200106 DataChannelTransportInterface* data_channel_transport() const;
107 void set_data_channel_transport(DataChannelTransportInterface* transport);
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100108
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700109 sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
110 RTC_DCHECK_RUN_ON(signaling_thread());
111 return SignalSctpDataChannelCreated_;
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100112 }
113 // Called when the transport for the data channels is closed or destroyed.
114 void OnTransportChannelClosed();
115
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
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100164 // Signals from |data_channel_transport_|. These are invoked on the
165 // 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 Alvestrand05e4d082019-12-03 14:04:21 +0100168 sigslot::signal1<bool> SignalDataChannelTransportWritable_s
169 RTC_GUARDED_BY(signaling_thread());
170 sigslot::signal2<const cricket::ReceiveDataParams&,
171 const rtc::CopyOnWriteBuffer&>
172 SignalDataChannelTransportReceivedData_s
173 RTC_GUARDED_BY(signaling_thread());
174 sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
175 RTC_GUARDED_BY(signaling_thread());
176 sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
177 RTC_GUARDED_BY(signaling_thread());
178
Taylor Brandstetter3a034e12020-07-09 15:32:34 -0700179 sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100180 RTC_GUARDED_BY(signaling_thread());
181
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100182 // Owning PeerConnection.
183 PeerConnection* const pc_;
Niels Möller236e36c2021-03-23 09:23:10 +0100184 // The weak pointers must be dereferenced and invalidated on the signalling
185 // thread only.
Harald Alvestrand246724b2019-12-03 22:31:42 +0100186 rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
Harald Alvestrand05e4d082019-12-03 14:04:21 +0100187};
188
189} // namespace webrtc
190
191#endif // PC_DATA_CHANNEL_CONTROLLER_H_