| 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 <stdint.h> |
| 15 |
| 16 | #include <map> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 |
| 21 | #include "api/data_channel_interface.h" |
| 22 | #include "api/scoped_refptr.h" |
| 23 | #include "api/sequence_checker.h" |
| 24 | #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" |
| 28 | #include "pc/channel.h" |
| 29 | #include "pc/data_channel_utils.h" |
| 30 | #include "pc/sctp_data_channel.h" |
| 31 | #include "rtc_base/checks.h" |
| 32 | #include "rtc_base/copy_on_write_buffer.h" |
| 33 | #include "rtc_base/ssl_stream_adapter.h" |
| 34 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 35 | #include "rtc_base/thread.h" |
| 36 | #include "rtc_base/thread_annotations.h" |
| 37 | #include "rtc_base/weak_ptr.h" |
| 38 |
| 39 | namespace webrtc { |
| 40 |
| 41 | class PeerConnection; |
| 42 |
| 43 | class DataChannelController : public SctpDataChannelProviderInterface, |
| 44 | public DataChannelSink { |
| 45 | public: |
| 46 | explicit DataChannelController(PeerConnection* pc) : pc_(pc) {} |
| 47 |
| 48 | // 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 |
| 54 | // Implements |
| 55 | // SctpDataChannelProviderInterface. |
| 56 | bool SendData(int sid, |
| 57 | const SendDataParams& params, |
| 58 | const rtc::CopyOnWriteBuffer& payload, |
| 59 | cricket::SendDataResult* result) override; |
| 60 | bool ConnectDataChannel(SctpDataChannel* webrtc_data_channel) override; |
| 61 | void DisconnectDataChannel(SctpDataChannel* webrtc_data_channel) override; |
| 62 | 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; |
| 73 | void OnTransportClosed() override; |
| 74 |
| 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 |
| 85 | // Called from PeerConnection::GetDataChannelStats on the signaling thread. |
| 86 | std::vector<DataChannelStats> GetDataChannelStats() const; |
| 87 |
| 88 | // Creates channel and adds it to the collection of DataChannels that will |
| 89 | // be offered in a SessionDescription, and wraps it in a proxy object. |
| 90 | rtc::scoped_refptr<DataChannelInterface> InternalCreateDataChannelWithProxy( |
| 91 | const std::string& label, |
| 92 | const InternalDataChannelInit* |
| 93 | config) /* RTC_RUN_ON(signaling_thread()) */; |
| 94 | void AllocateSctpSids(rtc::SSLRole role); |
| 95 |
| 96 | SctpDataChannel* FindDataChannelBySid(int sid) const; |
| 97 |
| 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 | } |
| 104 |
| 105 | // Accessors |
| 106 | DataChannelTransportInterface* data_channel_transport() const; |
| 107 | void set_data_channel_transport(DataChannelTransportInterface* transport); |
| 108 |
| 109 | sigslot::signal1<SctpDataChannel*>& SignalSctpDataChannelCreated() { |
| 110 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 111 | return SignalSctpDataChannelCreated_; |
| 112 | } |
| 113 | // Called when the transport for the data channels is closed or destroyed. |
| 114 | void OnTransportChannelClosed(); |
| 115 |
| 116 | void OnSctpDataChannelClosed(SctpDataChannel* channel); |
| 117 |
| 118 | private: |
| 119 | rtc::scoped_refptr<SctpDataChannel> InternalCreateSctpDataChannel( |
| 120 | const std::string& label, |
| 121 | const InternalDataChannelInit* |
| 122 | config) /* RTC_RUN_ON(signaling_thread()) */; |
| 123 |
| 124 | // 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 |
| 134 | // Called from SendData when data_channel_transport() is true. |
| 135 | bool DataChannelSendData(int sid, |
| 136 | const SendDataParams& params, |
| 137 | const rtc::CopyOnWriteBuffer& payload, |
| 138 | cricket::SendDataResult* result); |
| 139 |
| 140 | // Called when all data channels need to be notified of a transport channel |
| 141 | // (calls OnTransportChannelCreated on the signaling thread). |
| 142 | void NotifyDataChannelsOfTransportCreated(); |
| 143 |
| 144 | rtc::Thread* network_thread() const; |
| 145 | rtc::Thread* signaling_thread() const; |
| 146 |
| 147 | // 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 |
| 158 | SctpSidAllocator sid_allocator_ /* RTC_GUARDED_BY(signaling_thread()) */; |
| 159 | std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_ |
| 160 | RTC_GUARDED_BY(signaling_thread()); |
| 161 | std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_to_free_ |
| 162 | RTC_GUARDED_BY(signaling_thread()); |
| 163 |
| 164 | // Signals from |data_channel_transport_|. These are invoked on the |
| 165 | // signaling thread. |
| 166 | // TODO(bugs.webrtc.org/11547): These '_s' signals likely all belong on the |
| 167 | // network thread. |
| 168 | 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 |
| 179 | sigslot::signal1<SctpDataChannel*> SignalSctpDataChannelCreated_ |
| 180 | RTC_GUARDED_BY(signaling_thread()); |
| 181 |
| 182 | // Owning PeerConnection. |
| 183 | PeerConnection* const pc_; |
| 184 | // The weak pointers must be dereferenced and invalidated on the signalling |
| 185 | // thread only. |
| 186 | rtc::WeakPtrFactory<DataChannelController> weak_factory_{this}; |
| 187 | }; |
| 188 |
| 189 | } // namespace webrtc |
| 190 |
| 191 | #endif // PC_DATA_CHANNEL_CONTROLLER_H_ |
| 192 |