blob: 35e7656100e384cfa2d65dc41bbc9f5edcac0dc6 [file] [log] [blame]
Harald Alvestrandc85328f2019-02-28 06:51:001/*
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 Alvestrand5761e7b2021-01-29 14:45:0816#include "api/dtls_transport_interface.h"
Harald Alvestrandc85328f2019-02-28 06:51:0017#include "api/scoped_refptr.h"
18#include "api/sctp_transport_interface.h"
Harald Alvestrandc24a2182022-02-23 13:44:5919#include "api/sequence_checker.h"
Fredrik Solenberg5cb3a902022-08-22 09:34:2920#include "api/transport/data_channel_transport_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0821#include "media/sctp/sctp_transport_internal.h"
22#include "p2p/base/dtls_transport_internal.h"
Harald Alvestrandc85328f2019-02-28 06:51:0023#include "pc/dtls_transport.h"
Harald Alvestrandc24a2182022-02-23 13:44:5924#include "rtc_base/checks.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0825#include "rtc_base/thread.h"
26#include "rtc_base/thread_annotations.h"
Harald Alvestrandc85328f2019-02-28 06:51:0027
28namespace 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.
35class SctpTransport : public SctpTransportInterface,
Fredrik Solenberg5cb3a902022-08-22 09:34:2936 public DataChannelTransportInterface {
Harald Alvestrandc85328f2019-02-28 06:51:0037 public:
38 explicit SctpTransport(
39 std::unique_ptr<cricket::SctpTransportInternal> internal);
40
Fredrik Solenberg5cb3a902022-08-22 09:34:2941 // SctpTransportInterface
Harald Alvestrandc85328f2019-02-28 06:51:0042 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 Solenberg5cb3a902022-08-22 09:34:2947 // 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 Alvestrand8d3d6cf2019-05-16 09:49:1756 // Internal functions
Harald Alvestrandc85328f2019-02-28 06:51:0057 void Clear();
58 void SetDtlsTransport(rtc::scoped_refptr<DtlsTransport>);
Harald Alvestrand8d3d6cf2019-05-16 09:49:1759 // 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 Alvestrandc85328f2019-02-28 06:51:0062
Harald Alvestrand8d3d6cf2019-05-16 09:49:1763 // 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 Alvestrandc85328f2019-02-28 06:51:0066 cricket::SctpTransportInternal* internal() {
Tomas Gunnarsson92eebef2021-02-10 12:05:4467 RTC_DCHECK_RUN_ON(owner_thread_);
Harald Alvestrandc85328f2019-02-28 06:51:0068 return internal_sctp_transport_.get();
69 }
70
71 const cricket::SctpTransportInternal* internal() const {
Tomas Gunnarsson92eebef2021-02-10 12:05:4472 RTC_DCHECK_RUN_ON(owner_thread_);
Harald Alvestrandc85328f2019-02-28 06:51:0073 return internal_sctp_transport_.get();
74 }
75
76 protected:
77 ~SctpTransport() override;
78
79 private:
80 void UpdateInformation(SctpTransportState state);
81 void OnInternalReadyToSendData();
Harald Alvestrand97716c02019-05-21 08:52:5982 void OnAssociationChangeCommunicationUp();
Harald Alvestrandc85328f2019-02-28 06:51:0083 void OnInternalClosingProcedureStartedRemotely(int sid);
84 void OnInternalClosingProcedureComplete(int sid);
Harald Alvestrand408cb4b2019-11-16 11:09:0885 void OnDtlsStateChange(cricket::DtlsTransportInternal* transport,
Mirko Bonadei9f6808b2021-05-21 18:46:0986 DtlsTransportState state);
Harald Alvestrandc85328f2019-02-28 06:51:0087
Artem Titov880fa812021-07-30 20:30:2388 // NOTE: `owner_thread_` is the thread that the SctpTransport object is
Tomas Gunnarsson92eebef2021-02-10 12:05:4489 // 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 Alvestrandc85328f2019-02-28 06:51:0092 std::unique_ptr<cricket::SctpTransportInternal> internal_sctp_transport_
Tomas Gunnarsson92eebef2021-02-10 12:05:4493 RTC_GUARDED_BY(owner_thread_);
Harald Alvestrandc85328f2019-02-28 06:51:0094 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_