Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 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 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 16 | #include "api/dtls_transport_interface.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 17 | #include "api/scoped_refptr.h" |
| 18 | #include "api/sctp_transport_interface.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 19 | #include "api/sequence_checker.h" |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 20 | #include "api/transport/data_channel_transport_interface.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 21 | #include "media/sctp/sctp_transport_internal.h" |
| 22 | #include "p2p/base/dtls_transport_internal.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 23 | #include "pc/dtls_transport.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 24 | #include "rtc_base/checks.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 25 | #include "rtc_base/thread.h" |
| 26 | #include "rtc_base/thread_annotations.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 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, |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 36 | public DataChannelTransportInterface { |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 37 | public: |
| 38 | explicit SctpTransport( |
| 39 | std::unique_ptr<cricket::SctpTransportInternal> internal); |
| 40 | |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 41 | // SctpTransportInterface |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 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 | |
Fredrik Solenberg | 5cb3a90 | 2022-08-22 09:34:29 | [diff] [blame] | 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 | |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 56 | // Internal functions |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 57 | void Clear(); |
| 58 | void SetDtlsTransport(rtc::scoped_refptr<DtlsTransport>); |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 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); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 62 | |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 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. |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 66 | cricket::SctpTransportInternal* internal() { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 67 | RTC_DCHECK_RUN_ON(owner_thread_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 68 | return internal_sctp_transport_.get(); |
| 69 | } |
| 70 | |
| 71 | const cricket::SctpTransportInternal* internal() const { |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 72 | RTC_DCHECK_RUN_ON(owner_thread_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 73 | return internal_sctp_transport_.get(); |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | ~SctpTransport() override; |
| 78 | |
| 79 | private: |
| 80 | void UpdateInformation(SctpTransportState state); |
| 81 | void OnInternalReadyToSendData(); |
Harald Alvestrand | 97716c0 | 2019-05-21 08:52:59 | [diff] [blame] | 82 | void OnAssociationChangeCommunicationUp(); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 83 | void OnInternalClosingProcedureStartedRemotely(int sid); |
| 84 | void OnInternalClosingProcedureComplete(int sid); |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 85 | void OnDtlsStateChange(cricket::DtlsTransportInternal* transport, |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 86 | DtlsTransportState state); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 87 | |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 88 | // NOTE: `owner_thread_` is the thread that the SctpTransport object is |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 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_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 92 | std::unique_ptr<cricket::SctpTransportInternal> internal_sctp_transport_ |
Tomas Gunnarsson | 92eebef | 2021-02-10 12:05:44 | [diff] [blame] | 93 | RTC_GUARDED_BY(owner_thread_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 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_ |