| 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/task_queue/pending_task_safety_flag.h" |
| 22 | #include "api/transport/data_channel_transport_interface.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/thread.h" |
| 29 | #include "rtc_base/thread_annotations.h" |
| 30 | #include "rtc_base/weak_ptr.h" |
| 31 |
| 32 | namespace webrtc { |
| 33 |
| 34 | class PeerConnectionInternal; |
| 35 |
| 36 | class DataChannelController : public SctpDataChannelControllerInterface, |
| 37 | public DataChannelSink { |
| 38 | public: |
| 39 | explicit DataChannelController(PeerConnectionInternal* pc) : pc_(pc) {} |
| 40 | ~DataChannelController(); |
| 41 |
| 42 | // Not copyable or movable. |
| 43 | DataChannelController(DataChannelController&) = delete; |
| 44 | DataChannelController& operator=(const DataChannelController& other) = delete; |
| 45 | DataChannelController(DataChannelController&&) = delete; |
| 46 | DataChannelController& operator=(DataChannelController&& other) = delete; |
| 47 |
| 48 | // Implements |
| 49 | // SctpDataChannelProviderInterface. |
| 50 | RTCError SendData(StreamId sid, |
| 51 | const SendDataParams& params, |
| 52 | const rtc::CopyOnWriteBuffer& payload) override; |
| 53 | void AddSctpDataStream(StreamId sid) override; |
| 54 | void RemoveSctpDataStream(StreamId sid) override; |
| 55 | void OnChannelStateChanged(SctpDataChannel* channel, |
| 56 | DataChannelInterface::DataState state) override; |
| 57 |
| 58 | // Implements DataChannelSink. |
| 59 | void OnDataReceived(int channel_id, |
| 60 | DataMessageType type, |
| 61 | const rtc::CopyOnWriteBuffer& buffer) override; |
| 62 | void OnChannelClosing(int channel_id) override; |
| 63 | void OnChannelClosed(int channel_id) override; |
| 64 | void OnReadyToSend() override; |
| 65 | void OnTransportClosed(RTCError error) override; |
| 66 |
| 67 | // Called as part of destroying the owning PeerConnection. |
| 68 | void PrepareForShutdown(); |
| 69 |
| 70 | // Called from PeerConnection::SetupDataChannelTransport_n |
| 71 | void SetupDataChannelTransport_n(DataChannelTransportInterface* transport); |
| 72 | // Called from PeerConnection::TeardownDataChannelTransport_n |
| 73 | void TeardownDataChannelTransport_n(RTCError error); |
| 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 | RTCErrorOr<rtc::scoped_refptr<DataChannelInterface>> |
| 86 | InternalCreateDataChannelWithProxy(const std::string& label, |
| 87 | const InternalDataChannelInit& config); |
| 88 | void AllocateSctpSids(rtc::SSLRole role); |
| 89 |
| 90 | // Check if data channels are currently tracked. Used to decide whether a |
| 91 | // rejected m=application section should be reoffered. |
| 92 | bool HasDataChannels() const; |
| 93 |
| 94 | // At some point in time, a data channel has existed. |
| 95 | bool HasUsedDataChannels() const; |
| 96 |
| 97 | protected: |
| 98 | rtc::Thread* network_thread() const; |
| 99 | rtc::Thread* signaling_thread() const; |
| 100 |
| 101 | private: |
| 102 | void OnSctpDataChannelClosed(SctpDataChannel* channel); |
| 103 |
| 104 | // Creates a new SctpDataChannel object on the network thread. |
| 105 | RTCErrorOr<rtc::scoped_refptr<SctpDataChannel>> CreateDataChannel( |
| 106 | const std::string& label, |
| 107 | InternalDataChannelInit& config) RTC_RUN_ON(network_thread()); |
| 108 |
| 109 | // Parses and handles open messages. Returns true if the message is an open |
| 110 | // message and should be considered to be handled, false otherwise. |
| 111 | bool HandleOpenMessage_n(int channel_id, |
| 112 | DataMessageType type, |
| 113 | const rtc::CopyOnWriteBuffer& buffer) |
| 114 | RTC_RUN_ON(network_thread()); |
| 115 | // Called when a valid data channel OPEN message is received. |
| 116 | void OnDataChannelOpenMessage(rtc::scoped_refptr<SctpDataChannel> channel, |
| 117 | bool ready_to_send) |
| 118 | RTC_RUN_ON(signaling_thread()); |
| 119 |
| 120 | // Accepts a `StreamId` which may be pre-negotiated or unassigned. For |
| 121 | // pre-negotiated sids, attempts to reserve the sid in the allocation pool, |
| 122 | // for unassigned sids attempts to generate a new sid if possible. Returns |
| 123 | // RTCError::OK() if the sid is reserved (and may have been generated) or |
| 124 | // if not enough information exists to generate a sid, in which case the sid |
| 125 | // will still be unassigned upon return, but will be assigned later. |
| 126 | // If the pool has been exhausted or a sid has already been reserved, an |
| 127 | // error will be returned. |
| 128 | RTCError ReserveOrAllocateSid(StreamId& sid, |
| 129 | absl::optional<rtc::SSLRole> fallback_ssl_role) |
| 130 | RTC_RUN_ON(network_thread()); |
| 131 |
| 132 | // Called when all data channels need to be notified of a transport channel |
| 133 | // (calls OnTransportChannelCreated on the signaling thread). |
| 134 | void NotifyDataChannelsOfTransportCreated(); |
| 135 |
| 136 | void set_data_channel_transport(DataChannelTransportInterface* transport); |
| 137 |
| 138 | // Plugin transport used for data channels. Pointer may be accessed and |
| 139 | // checked from any thread, but the object may only be touched on the |
| 140 | // network thread. |
| 141 | DataChannelTransportInterface* data_channel_transport_ |
| 142 | RTC_GUARDED_BY(network_thread()) = nullptr; |
| 143 | SctpSidAllocator sid_allocator_ RTC_GUARDED_BY(network_thread()); |
| 144 | std::vector<rtc::scoped_refptr<SctpDataChannel>> sctp_data_channels_n_ |
| 145 | RTC_GUARDED_BY(network_thread()); |
| 146 | enum class DataChannelUsage : uint8_t { |
| 147 | kNeverUsed = 0, |
| 148 | kHaveBeenUsed, |
| 149 | kInUse |
| 150 | }; |
| 151 | DataChannelUsage channel_usage_ RTC_GUARDED_BY(signaling_thread()) = |
| 152 | DataChannelUsage::kNeverUsed; |
| 153 |
| 154 | // Owning PeerConnection. |
| 155 | PeerConnectionInternal* const pc_; |
| 156 | // The weak pointers must be dereferenced and invalidated on the network |
| 157 | // thread only. |
| 158 | rtc::WeakPtrFactory<DataChannelController> weak_factory_ |
| 159 | RTC_GUARDED_BY(network_thread()){this}; |
| 160 | ScopedTaskSafety signaling_safety_; |
| 161 | }; |
| 162 |
| 163 | } // namespace webrtc |
| 164 |
| 165 | #endif // PC_DATA_CHANNEL_CONTROLLER_H_ |
| 166 |