• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
14 #include <string>
15 #include <vector>
16 
17 #include "api/data_channel_interface.h"
18 #include "api/rtc_error.h"
19 #include "api/scoped_refptr.h"
20 #include "api/sequence_checker.h"
21 #include "api/transport/data_channel_transport_interface.h"
22 #include "media/base/media_channel.h"
23 #include "pc/data_channel_utils.h"
24 #include "pc/sctp_data_channel.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/copy_on_write_buffer.h"
27 #include "rtc_base/ssl_stream_adapter.h"
28 #include "rtc_base/third_party/sigslot/sigslot.h"
29 #include "rtc_base/thread.h"
30 #include "rtc_base/thread_annotations.h"
31 #include "rtc_base/weak_ptr.h"
32 
33 namespace webrtc {
34 
35 class PeerConnectionInternal;
36 
37 class DataChannelController : public SctpDataChannelControllerInterface,
38                               public DataChannelSink {
39  public:
DataChannelController(PeerConnectionInternal * pc)40   explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {}
41   ~DataChannelController();
42 
43   // Not copyable or movable.
44   DataChannelController(DataChannelController&) = delete;
45   DataChannelController& operator=(const DataChannelController& other) = delete;
46   DataChannelController(DataChannelController&&) = delete;
47   DataChannelController& operator=(DataChannelController&& other) = delete;
48 
49   // Implements
50   // SctpDataChannelProviderInterface.
51   bool SendData(int sid,
52                 const SendDataParams& params,
53                 const rtc::CopyOnWriteBuffer& payload,
54                 cricket::SendDataResult* result) override;
55   bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
56   void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override;
57   void AddSctpDataStream(int sid) override;
58   void RemoveSctpDataStream(int sid) override;
59   bool ReadyToSendData() const override;
60 
61   // Implements DataChannelSink.
62   void OnDataReceived(int channel_id,
63                       DataMessageType type,
64                       const rtc::CopyOnWriteBuffer& buffer) override;
65   void OnChannelClosing(int channel_id) override;
66   void OnChannelClosed(int channel_id) override;
67   void OnReadyToSend() override;
68   void OnTransportClosed(RTCError error) override;
69 
70   // Called from PeerConnection::SetupDataChannelTransport_n
71   void SetupDataChannelTransport_n();
72   // Called from PeerConnection::TeardownDataChannelTransport_n
73   void TeardownDataChannelTransport_n();
74 
75   // Called from PeerConnection::OnTransportChanged
76   // to make required changes to datachannels' transports.
77   void OnTransportChanged(
78       DataChannelTransportInterface* data_channel_transport);
79 
80   // Called from PeerConnection::GetDataChannelStats on the signaling thread.
81   std::vector<DataChannelStats> GetDataChannelStats() const;
82 
83   // Creates channel and adds it to the collection of DataChannels that will
84   // be offered in a SessionDescription, and wraps it in a proxy object.
85   rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy(
86       const std::string& label,
87       const InternalDataChannelInit*
88           config) /* RTC_RUN_ON(signaling_thread()) */;
89   void AllocateSctpSids(rtc::SSLRole role);
90 
91   // Checks if any data channel has been added.
92   bool HasDataChannels() const;
HasSctpDataChannels()93   bool HasSctpDataChannels() const {
94     RTC_DCHECK_RUN_ON(signaling_thread());
95     return !sctp_data_channels_.empty();
96   }
97 
98   // Accessors
99   DataChannelTransportInterface* data_channel_transport() const;
100   void set_data_channel_transport(DataChannelTransportInterface* transport);
101 
SignalSctpDataChannelCreated()102   sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() {
103     RTC_DCHECK_RUN_ON(signaling_thread());
104     return SignalSctpDataChannelCreated_;
105   }
106   // Called when the transport for the data channels is closed or destroyed.
107   void OnTransportChannelClosed(RTCError error);
108 
109   void OnSctpDataChannelClosed(SctpDataChannel* channel);
110 
111  private:
112   rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel(
113       const std::string& label,
114       const InternalDataChannelInit*
115           config) /* RTC_RUN_ON(signaling_thread()) */;
116 
117   // Parses and handles open messages.  Returns true if the message is an open
118   // message, false otherwise.
119   bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params,
120                            const rtc::CopyOnWriteBuffer& buffer)
121       RTC_RUN_ON(signaling_thread());
122   // Called when a valid data channel OPEN message is received.
123   void OnDataChannelOpenMessage(const std::string& label,
124                                 const InternalDataChannelInit& config)
125       RTC_RUN_ON(signaling_thread());
126 
127   // Called from SendData when data_channel_transport() is true.
128   bool DataChannelSendData(int sid,
129                            const SendDataParams& params,
130                            const rtc::CopyOnWriteBuffer& payload,
131                            cricket::SendDataResult* result);
132 
133   // Called when all data channels need to be notified of a transport channel
134   // (calls OnTransportChannelCreated on the signaling thread).
135   void NotifyDataChannelsOfTransportCreated();
136 
137   rtc::Thread* network_thread() const;
138   rtc::Thread* signaling_thread() const;
139 
140   // Plugin transport used for data channels.  Pointer may be accessed and
141   // checked from any thread, but the object may only be touched on the
142   // network thread.
143   // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network
144   // thread.
145   DataChannelTransportInterface* data_channel_transport_ = nullptr;
146 
147   // Cached value of whether the data channel transport is ready to send.
148   bool data_channel_transport_ready_to_send_
149       RTC_GUARDED_BY(signaling_thread()) = false;
150 
151   SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */;
152   std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_
153       RTC_GUARDED_BY(signaling_thread());
154   std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_
155       RTC_GUARDED_BY(signaling_thread());
156 
157   // Signals from `data_channel_transport_`.  These are invoked on the
158   // signaling thread.
159   // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the
160   // network thread.
161   sigslot::signal1<bool> SignalDataChannelTransportWritable_s
162       RTC_GUARDED_BY(signaling_thread());
163   sigslot::signal2<const cricket::ReceiveDataParams&,
164                    const rtc::CopyOnWriteBuffer&>
165       SignalDataChannelTransportReceivedData_s
166           RTC_GUARDED_BY(signaling_thread());
167   sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s
168       RTC_GUARDED_BY(signaling_thread());
169   sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s
170       RTC_GUARDED_BY(signaling_thread());
171 
172   sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_
173       RTC_GUARDED_BY(signaling_thread());
174 
175   // Owning PeerConnection.
176   PeerConnectionInternal* const pc_;
177   // The weak pointers must be dereferenced and invalidated on the signalling
178   // thread only.
179   rtc::WeakPtrFactory<DataChannelController> weak_factory_{this};
180 };
181 
182 }  // namespace webrtc
183 
184 #endif  // PC_DATA_CHANNEL_CONTROLLER_H_
185