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_SCTP_TRANSPORT_H_ 12 #define PC_SCTP_TRANSPORT_H_ 13 14 #include <memory> 15 16 #include "api/dtls_transport_interface.h" 17 #include "api/scoped_refptr.h" 18 #include "api/sctp_transport_interface.h" 19 #include "api/sequence_checker.h" 20 #include "api/transport/data_channel_transport_interface.h" 21 #include "media/sctp/sctp_transport_internal.h" 22 #include "p2p/base/dtls_transport_internal.h" 23 #include "pc/dtls_transport.h" 24 #include "rtc_base/checks.h" 25 #include "rtc_base/thread.h" 26 #include "rtc_base/thread_annotations.h" 27 28 namespace webrtc { 29 30 // This implementation wraps a cricket::SctpTransport, and takes 31 // ownership of it. 32 // This object must be constructed and updated on the networking thread, 33 // the same thread as the one the cricket::SctpTransportInternal object 34 // lives on. 35 class SctpTransport : public SctpTransportInterface, 36 public DataChannelTransportInterface { 37 public: 38 explicit SctpTransport( 39 std::unique_ptr<cricket::SctpTransportInternal> internal); 40 41 // SctpTransportInterface 42 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override; 43 SctpTransportInformation Information() const override; 44 void RegisterObserver(SctpTransportObserverInterface* observer) override; 45 void UnregisterObserver() override; 46 47 // DataChannelTransportInterface 48 RTCError OpenChannel(int channel_id) override; 49 RTCError SendData(int channel_id, 50 const SendDataParams& params, 51 const rtc::CopyOnWriteBuffer& buffer) override; 52 RTCError CloseChannel(int channel_id) override; 53 void SetDataSink(DataChannelSink* sink) override; 54 bool IsReadyToSend() const override; 55 56 // Internal functions 57 void Clear(); 58 void SetDtlsTransport(rtc::scoped_refptr<DtlsTransport>); 59 // Initialize the cricket::SctpTransport. This can be called from 60 // the signaling thread. 61 void Start(int local_port, int remote_port, int max_message_size); 62 63 // TODO(https://bugs.webrtc.org/10629): Move functions that need 64 // internal() to be functions on the webrtc::SctpTransport interface, 65 // and make the internal() function private. internal()66 cricket::SctpTransportInternal* internal() { 67 RTC_DCHECK_RUN_ON(owner_thread_); 68 return internal_sctp_transport_.get(); 69 } 70 internal()71 const cricket::SctpTransportInternal* internal() const { 72 RTC_DCHECK_RUN_ON(owner_thread_); 73 return internal_sctp_transport_.get(); 74 } 75 76 protected: 77 ~SctpTransport() override; 78 79 private: 80 void UpdateInformation(SctpTransportState state); 81 void OnInternalReadyToSendData(); 82 void OnAssociationChangeCommunicationUp(); 83 void OnInternalClosingProcedureStartedRemotely(int sid); 84 void OnInternalClosingProcedureComplete(int sid); 85 void OnDtlsStateChange(cricket::DtlsTransportInternal* transport, 86 DtlsTransportState state); 87 88 // NOTE: `owner_thread_` is the thread that the SctpTransport object is 89 // constructed on. In the context of PeerConnection, it's the network thread. 90 rtc::Thread* const owner_thread_; 91 SctpTransportInformation info_ RTC_GUARDED_BY(owner_thread_); 92 std::unique_ptr<cricket::SctpTransportInternal> internal_sctp_transport_ 93 RTC_GUARDED_BY(owner_thread_); 94 SctpTransportObserverInterface* observer_ RTC_GUARDED_BY(owner_thread_) = 95 nullptr; 96 rtc::scoped_refptr<DtlsTransport> dtls_transport_ 97 RTC_GUARDED_BY(owner_thread_); 98 }; 99 100 } // namespace webrtc 101 #endif // PC_SCTP_TRANSPORT_H_ 102