Skip to content

Commit 81319f0

Browse files
committed
add RTCSctpTransport interface with all methods;
add SctpTransportState enum.
1 parent 5263e57 commit 81319f0

12 files changed

Lines changed: 320 additions & 4 deletions

File tree

docs/source/webrtc.interfaces.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Submodules
2121
webrtc.interfaces.rtc_rtp_receiver
2222
webrtc.interfaces.rtc_rtp_sender
2323
webrtc.interfaces.rtc_rtp_transceiver
24+
webrtc.interfaces.rtc_sctp_transport
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
webrtc.interfaces.rtc\_sctp\_transport
2+
======================================
3+
4+
.. automodule:: webrtc.interfaces.rtc_sctp_transport
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:

python-webrtc/cpp/src/enums/enums.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "webrtc/rtc_ice_role.h"
2020
#include "webrtc/rtc_ice_transport_state.h"
2121
#include "webrtc/dtls_transport_state.h"
22+
#include "webrtc/sctp_transport_state.h"
2223
#include "python_webrtc/rtc_ice_component.h"
2324

2425
namespace python_webrtc {
@@ -42,6 +43,7 @@ namespace python_webrtc {
4243
RTCIceRole::Init(m);
4344
RTCIceTransportState::Init(m);
4445
DtlsTransportState::Init(m);
46+
SctpTransportState::Init(m);
4547

4648
// python_webrtc
4749

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Copyright 2022 Il`ya (Marshal) <https://github.com/MarshalX>. 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.md file in the root of the project.
6+
//
7+
8+
#include "sctp_transport_state.h"
9+
10+
#include <api/sctp_transport_interface.h>
11+
12+
namespace python_webrtc {
13+
14+
void SctpTransportState::Init(pybind11::module &m) {
15+
pybind11::enum_<webrtc::SctpTransportState>(m, "SctpTransportState")
16+
.value("new", webrtc::SctpTransportState::kNew)
17+
.value("connecting", webrtc::SctpTransportState::kConnecting)
18+
.value("connected", webrtc::SctpTransportState::kConnected)
19+
.value("closed", webrtc::SctpTransportState::kClosed)
20+
// .value("numvalues", webrtc::SctpTransportState::kNumValues) // not used
21+
.export_values();
22+
}
23+
24+
} // namespace python_webrtc
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Copyright 2022 Il`ya (Marshal) <https://github.com/MarshalX>. 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.md file in the root of the project.
6+
//
7+
8+
#pragma once
9+
10+
#include <pybind11/pybind11.h>
11+
12+
namespace python_webrtc {
13+
14+
class SctpTransportState {
15+
public:
16+
static void Init(pybind11::module &m);
17+
};
18+
19+
} // namespace python_webrtc

python-webrtc/cpp/src/interfaces/interfaces.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "media_stream.h"
1313
#include "rtc_ice_transport.h"
1414
#include "rtc_dtls_transport.h"
15+
#include "rtc_sctp_transport.h"
1516
#include "rtc_rtp_sender.h"
1617
#include "rtc_rtp_receiver.h"
1718
#include "rtc_rtp_transceiver.h"
@@ -26,6 +27,7 @@ namespace python_webrtc {
2627
MediaStream::Init(m);
2728
RTCIceTransport::Init(m);
2829
RTCDtlsTransport::Init(m);
30+
RTCSctpTransport::Init(m);
2931
RTCRtpSender::Init(m);
3032
RTCRtpReceiver::Init(m);
3133
RTCRtpTransceiver::Init(m);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// Copyright 2022 Il`ya (Marshal) <https://github.com/MarshalX>. 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.md file in the root of the project.
6+
//
7+
8+
#include "rtc_sctp_transport.h"
9+
10+
namespace python_webrtc {
11+
12+
RTCSctpTransport::RTCSctpTransport(
13+
python_webrtc::PeerConnectionFactory *factory, rtc::scoped_refptr<webrtc::SctpTransportInterface> transport
14+
) {
15+
_factory = factory;
16+
_transport = std::move(transport);
17+
18+
_factory->_workerThread->Invoke<void>(RTC_FROM_HERE, [this]() {
19+
_dtls_transport = _transport->dtls_transport();
20+
_transport->RegisterObserver(this);
21+
});
22+
23+
if (_transport->Information().state() == webrtc::SctpTransportState::kClosed) {
24+
Stop();
25+
}
26+
}
27+
28+
RTCSctpTransport::~RTCSctpTransport() {
29+
_factory = nullptr;
30+
holder()->Release(this);
31+
}
32+
33+
void RTCSctpTransport::Init(pybind11::module &m) {
34+
pybind11::class_<RTCSctpTransport>(m, "RTCSctpTransport")
35+
.def_property_readonly("transport", &RTCSctpTransport::GetTransport, pybind11::return_value_policy::reference)
36+
.def_property_readonly("state", &RTCSctpTransport::GetState)
37+
.def_property_readonly("maxMessageSize", &RTCSctpTransport::GetMaxMessageSize)
38+
.def_property_readonly("maxChannels", &RTCSctpTransport::GetMaxChannels);
39+
}
40+
41+
InstanceHolder<RTCSctpTransport *, rtc::scoped_refptr<webrtc::SctpTransportInterface>, PeerConnectionFactory *> *
42+
RTCSctpTransport::holder() {
43+
static auto holder = new InstanceHolder<
44+
RTCSctpTransport *, rtc::scoped_refptr<webrtc::SctpTransportInterface>, PeerConnectionFactory *
45+
>(RTCSctpTransport::Create);
46+
return holder;
47+
}
48+
49+
RTCSctpTransport *RTCSctpTransport::Create(
50+
PeerConnectionFactory *factory, rtc::scoped_refptr<webrtc::SctpTransportInterface> transport
51+
) {
52+
// who caring about freeing memory?
53+
return new RTCSctpTransport(factory, std::move(transport));
54+
}
55+
56+
void RTCSctpTransport::Stop() {
57+
_transport->UnregisterObserver();
58+
}
59+
60+
void RTCSctpTransport::OnStateChange(webrtc::SctpTransportInformation info) {
61+
// TODO call callback
62+
63+
if (info.state() == webrtc::SctpTransportState::kClosed) {
64+
Stop();
65+
}
66+
}
67+
68+
RTCDtlsTransport *RTCSctpTransport::GetTransport() {
69+
return RTCDtlsTransport::holder()->GetOrCreate(_factory, _dtls_transport.get());
70+
}
71+
72+
webrtc::SctpTransportState RTCSctpTransport::GetState() {
73+
return _transport->Information().state();
74+
}
75+
76+
std::optional<double> RTCSctpTransport::GetMaxMessageSize() {
77+
auto size = _transport->Information().MaxMessageSize();
78+
if (size.has_value()) {
79+
return size.value();
80+
}
81+
82+
return {};
83+
}
84+
85+
std::optional<int> RTCSctpTransport::GetMaxChannels() {
86+
auto maxChannels = _transport->Information().MaxChannels();
87+
if (maxChannels.has_value()) {
88+
return maxChannels.value();
89+
}
90+
91+
return {};
92+
}
93+
94+
} // namespace python_webrtc
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Copyright 2022 Il`ya (Marshal) <https://github.com/MarshalX>. 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.md file in the root of the project.
6+
//
7+
8+
#pragma once
9+
10+
#include <api/sctp_transport_interface.h>
11+
12+
#include "rtc_dtls_transport.h"
13+
14+
namespace python_webrtc {
15+
16+
class RTCSctpTransport : public webrtc::SctpTransportObserverInterface {
17+
public:
18+
explicit RTCSctpTransport(PeerConnectionFactory *, rtc::scoped_refptr<webrtc::SctpTransportInterface>);
19+
20+
static RTCSctpTransport *Create(PeerConnectionFactory *, rtc::scoped_refptr<webrtc::SctpTransportInterface>);
21+
22+
~RTCSctpTransport() override;
23+
24+
static void Init(pybind11::module &m);
25+
26+
static InstanceHolder<
27+
RTCSctpTransport *, rtc::scoped_refptr<webrtc::SctpTransportInterface>, PeerConnectionFactory *
28+
> *holder();
29+
30+
void OnStateChange(webrtc::SctpTransportInformation) override;
31+
32+
RTCDtlsTransport *GetTransport();
33+
34+
webrtc::SctpTransportState GetState();
35+
36+
std::optional<double> GetMaxMessageSize();
37+
38+
std::optional<int> GetMaxChannels();
39+
40+
protected:
41+
void Stop();
42+
43+
private:
44+
PeerConnectionFactory *_factory;
45+
rtc::scoped_refptr<webrtc::DtlsTransportInterface> _dtls_transport;
46+
rtc::scoped_refptr<webrtc::SctpTransportInterface> _transport;
47+
};
48+
49+
} // namespace python_webrtc

python-webrtc/python/test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async def main():
3939
wrtc.RTCIceTransportState,
4040
wrtc.CricketIceGatheringState,
4141
wrtc.DtlsTransportState,
42+
wrtc.SctpTransportState,
4243
wrtc.MediaType,
4344
]
4445
for enum in enums:

python-webrtc/python/webrtc/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .interfaces.rtc_rtp_transceiver import RTCRtpTransceiver
1818
from .interfaces.rtc_ice_transport import RTCIceTransport
1919
from .interfaces.rtc_dtls_transport import RTCDtlsTransport
20+
from .interfaces.rtc_sctp_transport import RTCSctpTransport
2021
from .interfaces.rtc_audio_source import RTCAudioSource
2122

2223
from .functions.get_user_media import getUserMedia, get_user_media
@@ -46,6 +47,7 @@
4647
RTCIceTransportState = wrtc.RTCIceTransportState
4748
CricketIceGatheringState = wrtc.CricketIceGatheringState
4849
DtlsTransportState = wrtc.DtlsTransportState
50+
SctpTransportState = wrtc.SctpTransportState
4951
MediaType = wrtc.MediaType
5052

5153
__all__ = [
@@ -67,6 +69,7 @@
6769
'RTCIceTransportState',
6870
'CricketIceGatheringState',
6971
'DtlsTransportState',
72+
'SctpTransportState',
7073
'MediaType',
7174
# base
7275
'WebRTCObject',
@@ -79,6 +82,7 @@
7982
'RTCRtpTransceiver',
8083
'RTCIceTransport',
8184
'RTCDtlsTransport',
85+
'RTCSctpTransport',
8286
'RTCAudioSource',
8387
# functions
8488
'getUserMedia',

0 commit comments

Comments
 (0)