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