| 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 <map> |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "pc/channel.h" |
| 20 | #include "pc/rtp_data_channel.h" |
| 21 | #include "pc/sctp_data_channel.h" |
| 22 | #include "rtc_base/weak_ptr.h" |
| 23 |
| 24 | namespace webrtc { |
| 25 |
| 26 | class PeerConnection; |
| 27 |
| 28 | class DataChannelController : public RtpDataChannelProviderInterface, |
| 29 | public SctpDataChannelProviderInterface, |
| 30 | public DataChannelSink { |
| 31 | public: |
| 32 | explicit DataChannelController(PeerConnection* pc) : pc_(pc) {} |
| 33 |
| 34 | // Not copyable or movable. |
| 35 | DataChannelController(DataChannelController&) = delete; |
| 36 | DataChannelController& operator=(const DataChannelController& other) = delete; |
| 37 | DataChannelController(DataChannelController&&) = delete; |
| 38 | DataChannelController& operator=(DataChannelController&& other) = delete; |
| 39 |
| 40 | // Implements RtpDataChannelProviderInterface/ |
| 41 | // SctpDataChannelProviderInterface. |
| 42 | bool SendData(const cricket::SendDataParams& params, |
| 43 | const rtc::CopyOnWriteBuffer& payload, |
| 44 | cricket::SendDataResult* result) override; |
| 45 | bool ConnectDataChannel(RtpDataChannel* webrtc_data_channel) override; |
| 46 | void DisconnectDataChannel(RtpDataChannel* webrtc_data_channel) override; |
| 47 | bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override; |
| 48 | void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override; |
| 49 | void AddSctpDataStream(int sid) override; |
| 50 | void RemoveSctpDataStream(int sid) override; |
| 51 | bool ReadyToSendData() const override; |
| 52 |
| 53 | // Implements DataChannelSink. |
| 54 | void OnDataReceived(int channel_id, |
| 55 | DataMessageType type, |
| 56 | const rtc::CopyOnWriteBuffer& buffer) override; |
| 57 | void OnChannelClosing(int channel_id) override; |
| 58 | void OnChannelClosed(int channel_id) override; |
| 59 | void OnReadyToSend() override; |
| 60 | void OnTransportClosed() override; |
| 61 |
| 62 | // Called from PeerConnection::SetupDataChannelTransport_n |
| 63 | void SetupDataChannelTransport_n(); |
| 64 | // Called from PeerConnection::TeardownDataChannelTransport_n |
| 65 | void TeardownDataChannelTransport_n(); |
| 66 |
| 67 | // Called from PeerConnection::OnTransportChanged |
| 68 | // to make required changes to datachannels' transports. |
| 69 | void OnTransportChanged( |
| 70 | DataChannelTransportInterface* data_channel_transport); |
| 71 |
| 72 | // Called from PeerConnection::GetDataChannelStats on the signaling thread. |
| 73 | std::vector<DataChannelStats> GetDataChannelStats() const; |
| 74 |
| 75 | // Creates channel and adds it to the collection of DataChannels that will |
| 76 | // be offered in a SessionDescription, and wraps it in a proxy object. |
| 77 | rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy( |
| 78 | const std::string& label, |
| 79 | const InternalDataChannelInit* |
| 80 | config) /* RTC_RUN_ON(signaling_thread()) */; |
| 81 | void AllocateSctpSids(rtc::SSLRole role); |
| 82 |
| 83 | SctpDataChannel* FindDataChannelBySid(int sid) const; |
| 84 |
| 85 | // Checks if any data channel has been added. |
| 86 | bool HasDataChannels() const; |
| 87 | bool HasSctpDataChannels() const { |
| 88 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 89 | return !sctp_data_channels_.empty(); |
| 90 | } |
| 91 | bool HasRtpDataChannels() const { |
| 92 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 93 | return !rtp_data_channels_.empty(); |
| 94 | } |
| 95 |
| 96 | void UpdateLocalRtpDataChannels(const cricket::StreamParamsVec& streams); |
| 97 | void UpdateRemoteRtpDataChannels(const cricket::StreamParamsVec& streams); |
| 98 |
| 99 | // Accessors |
| 100 | cricket::DataChannelType data_channel_type() const; |
| 101 | void set_data_channel_type(cricket::DataChannelType type); |
| 102 | cricket::RtpDataChannel* rtp_data_channel() const { |
| 103 | return rtp_data_channel_; |
| 104 | } |
| 105 | void set_rtp_data_channel(cricket::RtpDataChannel* channel) { |
| 106 | rtp_data_channel_ = channel; |
| 107 | } |
| 108 | DataChannelTransportInterface* data_channel_transport() const; |
| 109 | void set_data_channel_transport(DataChannelTransportInterface* transport); |
| 110 | const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>* |
| 111 | rtp_data_channels() const; |
| 112 |
| 113 | sigslot::signal1<RtpDataChannel*>& SignalRtpDataChannelCreated() { |
| 114 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 115 | return SignalRtpDataChannelCreated_; |
| 116 | } |
| 117 | sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() { |
| 118 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 119 | return SignalSctpDataChannelCreated_; |
| 120 | } |
| 121 | // Called when the transport for the data channels is closed or destroyed. |
| 122 | void OnTransportChannelClosed(); |
| 123 |
| 124 | void OnSctpDataChannelClosed(SctpDataChannel* channel); |
| 125 |
| 126 | private: |
| 127 | rtc::scoped_refptr<RtpDataChannel> InternalCreateRtpDataChannel( |
| 128 | const std::string& label, |
| 129 | const DataChannelInit* config) /* RTC_RUN_ON(signaling_thread()) */; |
| 130 |
| 131 | rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel( |
| 132 | const std::string& label, |
| 133 | const InternalDataChannelInit* |
| 134 | config) /* RTC_RUN_ON(signaling_thread()) */; |
| 135 |
| 136 | // Parses and handles open messages. Returns true if the message is an open |
| 137 | // message, false otherwise. |
| 138 | bool HandleOpenMessage_s(const cricket::ReceiveDataParams& params, |
| 139 | const rtc::CopyOnWriteBuffer& buffer) |
| 140 | RTC_RUN_ON(signaling_thread()); |
| 141 | // Called when a valid data channel OPEN message is received. |
| 142 | void OnDataChannelOpenMessage(const std::string& label, |
| 143 | const InternalDataChannelInit& config) |
| 144 | RTC_RUN_ON(signaling_thread()); |
| 145 |
| 146 | void CreateRemoteRtpDataChannel(const std::string& label, |
| 147 | uint32_t remote_ssrc) |
| 148 | RTC_RUN_ON(signaling_thread()); |
| 149 |
| 150 | void UpdateClosingRtpDataChannels( |
| 151 | const std::vector<std::string>& active_channels, |
| 152 | bool is_local_update) RTC_RUN_ON(signaling_thread()); |
| 153 |
| 154 | // Called from SendData when data_channel_transport() is true. |
| 155 | bool DataChannelSendData(const cricket::SendDataParams& params, |
| 156 | const rtc::CopyOnWriteBuffer& payload, |
| 157 | cricket::SendDataResult* result); |
| 158 |
| 159 | // Called when all data channels need to be notified of a transport channel |
| 160 | // (calls OnTransportChannelCreated on the signaling thread). |
| 161 | void NotifyDataChannelsOfTransportCreated(); |
| 162 |
| 163 | rtc::Thread* network_thread() const; |
| 164 | rtc::Thread* signaling_thread() const; |
| 165 |
| 166 | // Specifies which kind of data channel is allowed. This is controlled |
| 167 | // by the chrome command-line flag and constraints: |
| 168 | // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled, |
| 169 | // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is |
| 170 | // not set or false, SCTP is allowed (DCT_SCTP); |
| 171 | // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP); |
| 172 | // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE). |
| 173 | cricket::DataChannelType data_channel_type_ = |
| 174 | cricket::DCT_NONE; // TODO(bugs.webrtc.org/9987): Accessed on both |
| 175 | // signaling and network thread. |
| 176 |
| 177 | // Plugin transport used for data channels. Pointer may be accessed and |
| 178 | // checked from any thread, but the object may only be touched on the |
| 179 | // network thread. |
| 180 | // TODO(bugs.webrtc.org/9987): Accessed on both signaling and network |
| 181 | // thread. |
| 182 | DataChannelTransportInterface* data_channel_transport_ = nullptr; |
| 183 |
| 184 | // Cached value of whether the data channel transport is ready to send. |
| 185 | bool data_channel_transport_ready_to_send_ |
| 186 | RTC_GUARDED_BY(signaling_thread()) = false; |
| 187 |
| 188 | // |rtp_data_channel_| is used if in RTP data channel mode, |
| 189 | // |data_channel_transport_| when using SCTP. |
| 190 | cricket::RtpDataChannel* rtp_data_channel_ = nullptr; |
| 191 | // TODO(bugs.webrtc.org/9987): Accessed on both |
| 192 | // signaling and some other thread. |
| 193 |
| 194 | SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */; |
| 195 | std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_ |
| 196 | RTC_GUARDED_BY(signaling_thread()); |
| 197 | std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_ |
| 198 | RTC_GUARDED_BY(signaling_thread()); |
| 199 |
| 200 | // Map of label -> DataChannel |
| 201 | std::map<std::string, rtc::scoped_refptr<RtpDataChannel>> rtp_data_channels_ |
| 202 | RTC_GUARDED_BY(signaling_thread()); |
| 203 |
| 204 | // Signals from |data_channel_transport_|. These are invoked on the |
| 205 | // signaling thread. |
| 206 | // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the |
| 207 | // network thread. |
| 208 | sigslot::signal1<bool> SignalDataChannelTransportWritable_s |
| 209 | RTC_GUARDED_BY(signaling_thread()); |
| 210 | sigslot::signal2<const cricket::ReceiveDataParams&, |
| 211 | const rtc::CopyOnWriteBuffer&> |
| 212 | SignalDataChannelTransportReceivedData_s |
| 213 | RTC_GUARDED_BY(signaling_thread()); |
| 214 | sigslot::signal1<int> SignalDataChannelTransportChannelClosing_s |
| 215 | RTC_GUARDED_BY(signaling_thread()); |
| 216 | sigslot::signal1<int> SignalDataChannelTransportChannelClosed_s |
| 217 | RTC_GUARDED_BY(signaling_thread()); |
| 218 |
| 219 | sigslot::signal1<RtpDataChannel*> SignalRtpDataChannelCreated_ |
| 220 | RTC_GUARDED_BY(signaling_thread()); |
| 221 | sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_ |
| 222 | RTC_GUARDED_BY(signaling_thread()); |
| 223 |
| 224 | // Used to invoke data channel transport signals on the signaling thread. |
| 225 | std::unique_ptr<rtc::AsyncInvoker> data_channel_transport_invoker_ |
| 226 | RTC_GUARDED_BY(network_thread()); |
| 227 |
| 228 | // Owning PeerConnection. |
| 229 | PeerConnection* const pc_; |
| 230 | rtc::WeakPtrFactory<DataChannelController> weak_factory_{this}; |
| 231 | }; |
| 232 |
| 233 | } // namespace webrtc |
| 234 |
| 235 | #endif // PC_DATA_CHANNEL_CONTROLLER_H_ |
| 236 |