From b8adccc0ec838c4cb72043f2a4f1c3d32bf48bfe Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Mon, 1 Mar 2021 18:48:45 +0100 Subject: [PATCH 1/3] Harmonized MTU with fragment size and set default MTU to 1280 --- include/rtc/nalunit.hpp | 2 +- include/rtc/rtc.h | 2 +- src/globals.hpp | 2 +- src/impl/datachannel.cpp | 8 ++++---- src/impl/datachannel.hpp | 10 +++++----- src/impl/dtlstransport.cpp | 4 ++-- src/impl/peerconnection.cpp | 6 +++--- src/impl/sctptransport.cpp | 2 +- src/impl/track.cpp | 16 +++++++++++++--- src/impl/track.hpp | 6 +++++- src/nalunit.cpp | 4 ++++ src/track.cpp | 2 +- 12 files changed, 41 insertions(+), 23 deletions(-) diff --git a/include/rtc/nalunit.hpp b/include/rtc/nalunit.hpp index 966b89c..f42546e 100644 --- a/include/rtc/nalunit.hpp +++ b/include/rtc/nalunit.hpp @@ -144,7 +144,7 @@ protected: class RTC_CPP_EXPORT NalUnits : public std::vector> { public: - static const uint16_t defaultMaximumFragmentSize = 1400; + static const uint16_t defaultMaximumFragmentSize; std::vector> generateFragments(uint16_t maximumFragmentSize); }; diff --git a/include/rtc/rtc.h b/include/rtc/rtc.h index 456a924..d003d9a 100644 --- a/include/rtc/rtc.h +++ b/include/rtc/rtc.h @@ -40,7 +40,7 @@ extern "C" { #endif #if RTC_ENABLE_MEDIA -#define RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE ((uint16_t)1400) +#define RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE ((uint16_t)(1280 - 12 - 8 - 40)) // SRTP/UDP/IPv6 #define RTC_DEFAULT_MAXIMUM_PACKET_COUNT_FOR_NACK_CACHE ((unsigned)512) #endif diff --git a/src/globals.hpp b/src/globals.hpp index 8dbf030..206cd48 100644 --- a/src/globals.hpp +++ b/src/globals.hpp @@ -34,7 +34,7 @@ const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool (>= 2) -const size_t DEFAULT_IPV4_MTU = 1200; // IPv4 safe MTU value recommended by RFC 8261 +const size_t DEFAULT_MTU = 1280; // IPv6 minimum guaranteed MTU } // namespace rtc diff --git a/src/impl/datachannel.cpp b/src/impl/datachannel.cpp index 409c16a..87dad1e 100644 --- a/src/impl/datachannel.cpp +++ b/src/impl/datachannel.cpp @@ -255,13 +255,13 @@ void DataChannel::incoming(message_ptr message) { } } -NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, +NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, uint16_t stream, string label, string protocol, Reliability reliability) : DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {} -NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, - std::weak_ptr transport, +NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, + std::weak_ptr transport, uint16_t stream) : DataChannel(pc, stream, "", "", {}) { mSctpTransport = transport; @@ -269,7 +269,7 @@ NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr NegotiatedDataChannel::~NegotiatedDataChannel() {} -void NegotiatedDataChannel::open(shared_ptr transport) { +void NegotiatedDataChannel::open(shared_ptr transport) { std::unique_lock lock(mMutex); mSctpTransport = transport; diff --git a/src/impl/datachannel.hpp b/src/impl/datachannel.hpp index 4d8eb92..101453c 100644 --- a/src/impl/datachannel.hpp +++ b/src/impl/datachannel.hpp @@ -34,7 +34,7 @@ namespace rtc::impl { struct PeerConnection; struct DataChannel : Channel, std::enable_shared_from_this { - DataChannel(weak_ptr pc, uint16_t stream, string label, string protocol, + DataChannel(weak_ptr pc, uint16_t stream, string label, string protocol, Reliability reliability); ~DataChannel(); @@ -79,13 +79,13 @@ protected: }; struct NegotiatedDataChannel final : public DataChannel { - NegotiatedDataChannel(weak_ptr pc, uint16_t stream, string label, + NegotiatedDataChannel(weak_ptr pc, uint16_t stream, string label, string protocol, Reliability reliability); - NegotiatedDataChannel(weak_ptr pc, - weak_ptr transport, uint16_t stream); + NegotiatedDataChannel(weak_ptr pc, + weak_ptr transport, uint16_t stream); ~NegotiatedDataChannel(); - void open(impl_ptr transport) override; + void open(impl_ptr transport) override; void processOpenMessage(message_ptr message) override; }; diff --git a/src/impl/dtlstransport.cpp b/src/impl/dtlstransport.cpp index bd64c79..fab3b55 100644 --- a/src/impl/dtlstransport.cpp +++ b/src/impl/dtlstransport.cpp @@ -159,7 +159,7 @@ void DtlsTransport::runRecvLoop() { try { changeState(State::Connecting); - size_t mtu = mMtu.value_or(DEFAULT_IPV4_MTU + 20) - 8 - 40; // UDP/IPv6 + size_t mtu = mMtu.value_or(DEFAULT_MTU) - 8 - 40; // UDP/IPv6 gnutls_dtls_set_mtu(mSession, static_cast(mtu)); PLOG_VERBOSE << "SSL MTU set to " << mtu; @@ -445,7 +445,7 @@ void DtlsTransport::runRecvLoop() { try { changeState(State::Connecting); - size_t mtu = mMtu.value_or(DEFAULT_IPV4_MTU + 20) - 8 - 40; // UDP/IPv6 + size_t mtu = mMtu.value_or(DEFAULT_MTU) - 8 - 40; // UDP/IPv6 SSL_set_mtu(mSsl, static_cast(mtu)); PLOG_VERBOSE << "SSL MTU set to " << mtu; diff --git a/src/impl/peerconnection.cpp b/src/impl/peerconnection.cpp index 106e278..897473e 100644 --- a/src/impl/peerconnection.cpp +++ b/src/impl/peerconnection.cpp @@ -19,10 +19,10 @@ #include "peerconnection.hpp" #include "certificate.hpp" +#include "common.hpp" #include "dtlstransport.hpp" #include "globals.hpp" #include "icetransport.hpp" -#include "common.hpp" #include "logcounter.hpp" #include "peerconnection.hpp" #include "processor.hpp" @@ -647,7 +647,7 @@ shared_ptr PeerConnection::emplaceTrack(Description::Media description) { track->setDescription(std::move(description)); if (!track) { - track = std::make_shared(std::move(description)); + track = std::make_shared(shared_from_this(), std::move(description)); mTracks.emplace(std::make_pair(track->mid(), track)); mTrackLines.emplace_back(track); } @@ -663,7 +663,7 @@ void PeerConnection::incomingTrack(Description::Media description) { } #endif if (mTracks.find(description.mid()) == mTracks.end()) { - auto track = std::make_shared(std::move(description)); + auto track = std::make_shared(shared_from_this(), std::move(description)); mTracks.emplace(std::make_pair(track->mid(), track)); mTrackLines.emplace_back(track); triggerTrack(track); diff --git a/src/impl/sctptransport.cpp b/src/impl/sctptransport.cpp index baf7ad7..d61b43a 100644 --- a/src/impl/sctptransport.cpp +++ b/src/impl/sctptransport.cpp @@ -193,7 +193,7 @@ SctpTransport::SctpTransport(std::shared_ptr lower, uint16_t port, spp.spp_flags |= SPP_PMTUD_DISABLE; // The MTU value provided specifies the space available for chunks in the // packet, so we also subtract the SCTP header size. - size_t pmtu = mtu.value_or(DEFAULT_IPV4_MTU + 20) - 12 - 37 - 8 - 40; // SCTP/DTLS/UDP/IPv6 + size_t pmtu = mtu.value_or(DEFAULT_MTU) - 12 - 37 - 8 - 40; // SCTP/DTLS/UDP/IPv6 spp.spp_pathmtu = uint32_t(pmtu); PLOG_VERBOSE << "Path MTU discovery disabled, SCTP MTU set to " << pmtu; } diff --git a/src/impl/track.cpp b/src/impl/track.cpp index 112d073..de1fe69 100644 --- a/src/impl/track.cpp +++ b/src/impl/track.cpp @@ -19,6 +19,7 @@ #include "track.hpp" #include "globals.hpp" #include "logcounter.hpp" +#include "peerconnection.hpp" namespace rtc::impl { @@ -27,8 +28,9 @@ static LogCounter COUNTER_MEDIA_BAD_DIRECTION(plog::warning, static LogCounter COUNTER_QUEUE_FULL(plog::warning, "Number of media packets dropped due to a full queue"); -Track::Track(Description::Media description) - : mMediaDescription(std::move(description)), mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {} +Track::Track(weak_ptr pc, Description::Media description) + : mPeerConnection(pc), mMediaDescription(std::move(description)), + mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {} string Track::mid() const { std::shared_lock lock(mMutex); @@ -74,6 +76,8 @@ std::optional Track::peek() { return nullopt; } +size_t Track::availableAmount() const { return mRecvQueue.amount(); } + bool Track::isOpen(void) const { #if RTC_ENABLE_MEDIA std::shared_lock lock(mMutex); @@ -85,7 +89,13 @@ bool Track::isOpen(void) const { bool Track::isClosed(void) const { return mIsClosed; } -size_t Track::availableAmount() const { return mRecvQueue.amount(); } +size_t Track::maxMessageSize() const { + std::optional mtu; + if (auto pc = mPeerConnection.lock()) + mtu = pc->config.mtu; + + return mtu.value_or(DEFAULT_MTU) - 12 - 8 - 40; // SRTP/UDP/IPv6 +} #if RTC_ENABLE_MEDIA void Track::open(shared_ptr transport) { diff --git a/src/impl/track.hpp b/src/impl/track.hpp index bf3298d..3ae03fa 100644 --- a/src/impl/track.hpp +++ b/src/impl/track.hpp @@ -35,9 +35,11 @@ namespace rtc::impl { +struct PeerConnection; + class Track final : public std::enable_shared_from_this, public Channel { public: - Track(Description::Media description); + Track(weak_ptr pc, Description::Media description); ~Track() = default; void close(); @@ -50,6 +52,7 @@ public: bool isOpen() const; bool isClosed() const; + size_t maxMessageSize() const; string mid() const; Description::Direction direction() const; @@ -66,6 +69,7 @@ public: private: bool transportSend(message_ptr message); + const weak_ptr mPeerConnection; #if RTC_ENABLE_MEDIA weak_ptr mDtlsSrtpTransport; #endif diff --git a/src/nalunit.cpp b/src/nalunit.cpp index 7d1219e..0745efd 100644 --- a/src/nalunit.cpp +++ b/src/nalunit.cpp @@ -19,6 +19,8 @@ #if RTC_ENABLE_MEDIA #include "nalunit.hpp" +#include "globals.hpp" + #include namespace rtc { @@ -90,6 +92,8 @@ void NalUnitFragmentA::setFragmentType(FragmentType type) { } } +const uint16_t NalUnits::defaultMaximumFragmentSize = DEFAULT_MTU - 12 - 8 - 40; // SRTP/UDP/IPv6 + std::vector> NalUnits::generateFragments(uint16_t maximumFragmentSize) { vector> result{}; for (auto nalu : *this) { diff --git a/src/track.cpp b/src/track.cpp index bfb6651..821ed8e 100644 --- a/src/track.cpp +++ b/src/track.cpp @@ -47,7 +47,7 @@ bool Track::isOpen(void) const { return impl()->isOpen(); } bool Track::isClosed(void) const { return impl()->isClosed(); } size_t Track::maxMessageSize() const { - return DEFAULT_IPV4_MTU - 12 - 8 - 20; // SRTP/UDP/IPv4 + return impl()->maxMessageSize(); } void Track::setRtcpHandler(std::shared_ptr handler) { From 83453635a88bf6dc7d779dc9a25285d2efcd8958 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Mon, 1 Mar 2021 20:20:06 +0100 Subject: [PATCH 2/3] Replaced shared_from_this() with weak_from_this() where applicable --- src/impl/peerconnection.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/impl/peerconnection.cpp b/src/impl/peerconnection.cpp index 897473e..ec0b4be 100644 --- a/src/impl/peerconnection.cpp +++ b/src/impl/peerconnection.cpp @@ -393,7 +393,7 @@ void PeerConnection::forwardMessage(message_ptr message) { stream % 2 == remoteParity) { channel = - std::make_shared(shared_from_this(), sctpTransport, stream); + std::make_shared(weak_from_this(), sctpTransport, stream); channel->openCallback = weak_bind(&PeerConnection::triggerDataChannel, this, weak_ptr{channel}); @@ -562,9 +562,9 @@ shared_ptr PeerConnection::emplaceDataChannel(Description::Role rol // If the DataChannel is user-negotiated, do not negociate it here auto channel = init.negotiated - ? std::make_shared(shared_from_this(), stream, std::move(label), + ? std::make_shared(weak_from_this(), stream, std::move(label), std::move(init.protocol), std::move(init.reliability)) - : std::make_shared(shared_from_this(), stream, std::move(label), + : std::make_shared(weak_from_this(), stream, std::move(label), std::move(init.protocol), std::move(init.reliability)); mDataChannels.emplace(std::make_pair(stream, channel)); @@ -647,7 +647,7 @@ shared_ptr PeerConnection::emplaceTrack(Description::Media description) { track->setDescription(std::move(description)); if (!track) { - track = std::make_shared(shared_from_this(), std::move(description)); + track = std::make_shared(weak_from_this(), std::move(description)); mTracks.emplace(std::make_pair(track->mid(), track)); mTrackLines.emplace_back(track); } @@ -663,7 +663,7 @@ void PeerConnection::incomingTrack(Description::Media description) { } #endif if (mTracks.find(description.mid()) == mTracks.end()) { - auto track = std::make_shared(shared_from_this(), std::move(description)); + auto track = std::make_shared(weak_from_this(), std::move(description)); mTracks.emplace(std::make_pair(track->mid(), track)); mTrackLines.emplace_back(track); triggerTrack(track); From 32c4427b96118ca8be21e7855a1ee1fb667c7500 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Tue, 2 Mar 2021 12:13:58 +0100 Subject: [PATCH 3/3] Reorganized defines and added mtu option to C API --- include/rtc/common.hpp | 10 +++++---- include/rtc/nalunit.hpp | 14 ++++++------ include/rtc/peerconnection.hpp | 1 - include/rtc/rtc.h | 13 +++++++++--- include/rtc/rtc.hpp | 12 +++++++++-- src/capi.cpp | 39 +++++++++++++++++----------------- src/globals.hpp | 2 +- src/impl/datachannel.cpp | 9 ++++---- src/impl/peerconnection.hpp | 36 ++++++++++++++++++------------- src/nalunit.cpp | 2 -- 10 files changed, 78 insertions(+), 60 deletions(-) diff --git a/include/rtc/common.hpp b/include/rtc/common.hpp index b96ac7d..8d65aea 100644 --- a/include/rtc/common.hpp +++ b/include/rtc/common.hpp @@ -19,14 +19,14 @@ #ifndef RTC_COMMON_H #define RTC_COMMON_H -#ifndef RTC_ENABLE_MEDIA -#define RTC_ENABLE_MEDIA 1 -#endif - #ifndef RTC_ENABLE_WEBSOCKET #define RTC_ENABLE_WEBSOCKET 1 #endif +#ifndef RTC_ENABLE_MEDIA +#define RTC_ENABLE_MEDIA 1 +#endif + #ifdef _WIN32 #define RTC_CPP_EXPORT __declspec(dllexport) #ifndef _WIN32_WINNT @@ -39,6 +39,8 @@ #define RTC_CPP_EXPORT #endif +#include "rtc.h" // for C API defines + #include "log.hpp" #include "utils.hpp" diff --git a/include/rtc/nalunit.hpp b/include/rtc/nalunit.hpp index f42546e..e719d20 100644 --- a/include/rtc/nalunit.hpp +++ b/include/rtc/nalunit.hpp @@ -62,11 +62,9 @@ private: /// Nal unit struct RTC_CPP_EXPORT NalUnit : binary { NalUnit(const NalUnit &unit) = default; - NalUnit(size_t size, bool includingHeader = true) - : binary(size + (includingHeader ? 0 : 1)) {} + NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : 1)) {} - template - NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {} + template NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {} NalUnit(binary &&data) : binary(std::move(data)) {} @@ -101,9 +99,10 @@ struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit { enum class FragmentType { Start, Middle, End }; NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType, - binary data); + binary data); - static std::vector> fragmentsFrom(std::shared_ptr nalu, uint16_t maximumFragmentSize); + static std::vector> + fragmentsFrom(std::shared_ptr nalu, uint16_t maximumFragmentSize); uint8_t unitType() { return fragmentHeader()->unitType(); } @@ -144,7 +143,8 @@ protected: class RTC_CPP_EXPORT NalUnits : public std::vector> { public: - static const uint16_t defaultMaximumFragmentSize; + static const uint16_t defaultMaximumFragmentSize = + uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6 std::vector> generateFragments(uint16_t maximumFragmentSize); }; diff --git a/include/rtc/peerconnection.hpp b/include/rtc/peerconnection.hpp index 2c61572..01b85f9 100644 --- a/include/rtc/peerconnection.hpp +++ b/include/rtc/peerconnection.hpp @@ -27,7 +27,6 @@ #include "init.hpp" #include "message.hpp" #include "reliability.hpp" -#include "rtc.hpp" #include "track.hpp" #include diff --git a/include/rtc/rtc.h b/include/rtc/rtc.h index d003d9a..9fcfcf5 100644 --- a/include/rtc/rtc.h +++ b/include/rtc/rtc.h @@ -39,8 +39,14 @@ extern "C" { #define RTC_ENABLE_WEBSOCKET 1 #endif +#ifndef RTC_ENABLE_MEDIA +#define RTC_ENABLE_MEDIA 1 +#endif + +#define RTC_DEFAULT_MTU 1280 // IPv6 minimum guaranteed MTU + #if RTC_ENABLE_MEDIA -#define RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE ((uint16_t)(1280 - 12 - 8 - 40)) // SRTP/UDP/IPv6 +#define RTC_DEFAULT_MAXIMUM_FRAGMENT_SIZE ((uint16_t)(RTC_DEFAULT_MTU - 12 - 8 - 40)) // SRTP/UDP/IPv6 #define RTC_DEFAULT_MAXIMUM_PACKET_COUNT_FOR_NACK_CACHE ((unsigned)512) #endif @@ -116,13 +122,14 @@ typedef struct { bool enableIceTcp; uint16_t portRangeBegin; uint16_t portRangeEnd; + int mtu; // <= 0 means automatic } rtcConfiguration; typedef struct { bool unordered; bool unreliable; - unsigned int maxPacketLifeTime; // ignored if reliable - unsigned int maxRetransmits; // ignored if reliable + int maxPacketLifeTime; // ignored if reliable + int maxRetransmits; // ignored if reliable } rtcReliability; typedef struct { diff --git a/include/rtc/rtc.hpp b/include/rtc/rtc.hpp index ec8b286..3987f2f 100644 --- a/include/rtc/rtc.hpp +++ b/include/rtc/rtc.hpp @@ -16,15 +16,25 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +// C API +#include "rtc.h" + // C++ API #include "common.hpp" #include "init.hpp" // for rtc::Cleanup() #include "log.hpp" // #include "datachannel.hpp" +#include "track.hpp" #include "peerconnection.hpp" + +#if RTC_ENABLE_WEBSOCKET + +// WebSocket #include "websocket.hpp" +#endif // RTC_ENABLE_WEBSOCKET + #if RTC_ENABLE_MEDIA // Media handling @@ -39,5 +49,3 @@ #endif // RTC_ENABLE_MEDIA -// C API -#include "rtc.h" diff --git a/src/capi.cpp b/src/capi.cpp index 616344d..a8f857e 100644 --- a/src/capi.cpp +++ b/src/capi.cpp @@ -16,16 +16,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "common.hpp" - #include "rtc.h" -#include "datachannel.hpp" -#include "log.hpp" -#include "peerconnection.hpp" -#if RTC_ENABLE_WEBSOCKET -#include "websocket.hpp" -#endif +#include "rtc.hpp" #include "plog/Formatters/FuncMessageFormatter.h" @@ -359,12 +352,16 @@ int rtcCreatePeerConnection(const rtcConfiguration *config) { for (int i = 0; i < config->iceServersCount; ++i) c.iceServers.emplace_back(string(config->iceServers[i])); - if (config->portRangeBegin || config->portRangeEnd) { + c.enableIceTcp = config->enableIceTcp; + + if (config->portRangeBegin > 0 || config->portRangeEnd > 0) { c.portRangeBegin = config->portRangeBegin; c.portRangeEnd = config->portRangeEnd; } - c.enableIceTcp = config->enableIceTcp; + if(config->mtu > 0) + c.mtu = size_t(config->mtu); + return emplacePeerConnection(std::make_shared(c)); }); } @@ -398,7 +395,7 @@ int rtcAddDataChannelEx(int pc, const char *label, const rtcDataChannelInit *ini dci.reliability.rexmit = milliseconds(reliability->maxPacketLifeTime); } else { dci.reliability.type = Reliability::Type::Rexmit; - dci.reliability.rexmit = int(reliability->maxRetransmits); + dci.reliability.rexmit = reliability->maxRetransmits; } } else { dci.reliability.type = Reliability::Type::Reliable; @@ -447,7 +444,8 @@ int rtcDeleteDataChannel(int dc) { #if RTC_ENABLE_MEDIA -void setSSRC(Description::Media *description, uint32_t ssrc, const char *_name, const char *_msid, const char *_trackID) { +void setSSRC(Description::Media *description, uint32_t ssrc, const char *_name, const char *_msid, + const char *_trackID) { optional name = nullopt; if (_name) { @@ -468,7 +466,8 @@ void setSSRC(Description::Media *description, uint32_t ssrc, const char *_name, } int rtcAddTrackEx(int pc, rtcCodec codec, int payloadType, uint32_t ssrc, const char *_mid, - rtcDirection _direction, const char *_name, const char *_msid, const char *_trackID) { + rtcDirection _direction, const char *_name, const char *_msid, + const char *_trackID) { return wrap([&] { auto peerConnection = getPeerConnection(pc); @@ -545,13 +544,13 @@ int rtcAddTrackEx(int pc, rtcCodec codec, int payloadType, uint32_t ssrc, const } int rtcSetH264PacketizationHandler(int tr, uint32_t ssrc, const char *cname, uint8_t payloadType, - uint32_t clockRate, uint16_t maxFragmentSize, uint16_t sequenceNumber, - uint32_t timestamp) { + uint32_t clockRate, uint16_t maxFragmentSize, + uint16_t sequenceNumber, uint32_t timestamp) { return wrap([&] { auto track = getTrack(tr); // create RTP configuration auto rtpConfig = getNewRtpPacketizationConfig(ssrc, cname, payloadType, clockRate, - sequenceNumber, timestamp); + sequenceNumber, timestamp); // create packetizer auto packetizer = std::make_shared(rtpConfig, maxFragmentSize); // create H264 handler @@ -576,7 +575,7 @@ int rtcSetOpusPacketizationHandler(int tr, uint32_t ssrc, const char *cname, uin auto packetizer = std::make_shared(rtpConfig); // create Opus handler auto opusHandler = std::make_shared(packetizer); - emplaceMediaChainableHandler(opusHandler, tr); + emplaceMediaChainableHandler(opusHandler, tr); emplaceRTPConfig(rtpConfig, tr); // set handler track->setRtcpHandler(opusHandler); @@ -674,7 +673,7 @@ int rtcGetPreviousTrackSenderReportTimestamp(int id, uint32_t *timestamp) { } int rtcSetNeedsToSendRtcpSr(int id) { - return wrap([id]{ + return wrap([id] { auto sender = getRtcpSrReporter(id); sender->setNeedsToReport(); return RTC_ERR_SUCCESS; @@ -1012,10 +1011,10 @@ int rtcGetDataChannelReliability(int dc, rtcReliability *reliability) { reliability->unordered = dcr.unordered; if (dcr.type == Reliability::Type::Timed) { reliability->unreliable = true; - reliability->maxPacketLifeTime = unsigned(std::get(dcr.rexmit).count()); + reliability->maxPacketLifeTime = int(std::get(dcr.rexmit).count()); } else if (dcr.type == Reliability::Type::Rexmit) { reliability->unreliable = true; - reliability->maxRetransmits = unsigned(std::get(dcr.rexmit)); + reliability->maxRetransmits = std::get(dcr.rexmit); } else { reliability->unreliable = false; } diff --git a/src/globals.hpp b/src/globals.hpp index 206cd48..7174428 100644 --- a/src/globals.hpp +++ b/src/globals.hpp @@ -34,7 +34,7 @@ const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool (>= 2) -const size_t DEFAULT_MTU = 1280; // IPv6 minimum guaranteed MTU +const size_t DEFAULT_MTU = RTC_DEFAULT_MTU; // defined in rtc.h } // namespace rtc diff --git a/src/impl/datachannel.cpp b/src/impl/datachannel.cpp index 87dad1e..e079b78 100644 --- a/src/impl/datachannel.cpp +++ b/src/impl/datachannel.cpp @@ -17,8 +17,8 @@ */ #include "datachannel.hpp" -#include "globals.hpp" #include "common.hpp" +#include "globals.hpp" #include "logcounter.hpp" #include "peerconnection.hpp" #include "sctptransport.hpp" @@ -255,9 +255,8 @@ void DataChannel::incoming(message_ptr message) { } } -NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, - uint16_t stream, string label, string protocol, - Reliability reliability) +NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, uint16_t stream, + string label, string protocol, Reliability reliability) : DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {} NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr pc, @@ -278,7 +277,7 @@ void NegotiatedDataChannel::open(shared_ptr transport) { switch (mReliability->type) { case Reliability::Type::Rexmit: channelType = CHANNEL_PARTIAL_RELIABLE_REXMIT; - reliabilityParameter = uint32_t(std::get(mReliability->rexmit)); + reliabilityParameter = uint32_t(std::max(std::get(mReliability->rexmit), 0)); break; case Reliability::Type::Timed: diff --git a/src/impl/peerconnection.hpp b/src/impl/peerconnection.hpp index a85a5cd..4a030e9 100644 --- a/src/impl/peerconnection.hpp +++ b/src/impl/peerconnection.hpp @@ -19,15 +19,21 @@ #ifndef RTC_IMPL_PEER_CONNECTION_H #define RTC_IMPL_PEER_CONNECTION_H +#include "common.hpp" #include "datachannel.hpp" #include "dtlstransport.hpp" #include "icetransport.hpp" -#include "common.hpp" #include "sctptransport.hpp" #include "track.hpp" #include "rtc/peerconnection.hpp" +#include +#include +#include +#include +#include + namespace rtc::impl { struct PeerConnection : std::enable_shared_from_this { @@ -43,12 +49,12 @@ struct PeerConnection : std::enable_shared_from_this { std::optional localDescription() const; std::optional remoteDescription() const; - std::shared_ptr initIceTransport(); - std::shared_ptr initDtlsTransport(); - std::shared_ptr initSctpTransport(); - std::shared_ptr getIceTransport() const; - std::shared_ptr getDtlsTransport() const; - std::shared_ptr getSctpTransport() const; + shared_ptr initIceTransport(); + shared_ptr initDtlsTransport(); + shared_ptr initSctpTransport(); + shared_ptr getIceTransport() const; + shared_ptr getDtlsTransport() const; + shared_ptr getSctpTransport() const; void closeTransports(); void endLocalCandidates(); @@ -63,7 +69,7 @@ struct PeerConnection : std::enable_shared_from_this { DataChannelInit init); shared_ptr findDataChannel(uint16_t stream); void shiftDataChannels(); - void iterateDataChannels(std::function channel)> func); + void iterateDataChannels(std::function channel)> func); void openDataChannels(); void closeDataChannels(); void remoteCloseDataChannels(); @@ -80,7 +86,7 @@ struct PeerConnection : std::enable_shared_from_this { string localBundleMid() const; void triggerDataChannel(std::weak_ptr weakDataChannel); - void triggerTrack(std::shared_ptr track); + void triggerTrack(shared_ptr track); bool changeState(State newState); bool changeGatheringState(GatheringState newState); bool changeSignalingState(SignalingState newState); @@ -95,26 +101,26 @@ struct PeerConnection : std::enable_shared_from_this { std::atomic signalingState = SignalingState::Stable; std::atomic negotiationNeeded = false; - synchronized_callback> dataChannelCallback; + synchronized_callback> dataChannelCallback; synchronized_callback localDescriptionCallback; synchronized_callback localCandidateCallback; synchronized_callback stateChangeCallback; synchronized_callback gatheringStateChangeCallback; synchronized_callback signalingStateChangeCallback; - synchronized_callback> trackCallback; + synchronized_callback> trackCallback; private: const init_token mInitToken = Init::Token(); const future_certificate_ptr mCertificate; - const std::unique_ptr mProcessor; + const unique_ptr mProcessor; std::optional mLocalDescription, mRemoteDescription; std::optional mCurrentLocalDescription; mutable std::mutex mLocalDescriptionMutex, mRemoteDescriptionMutex; - std::shared_ptr mIceTransport; - std::shared_ptr mDtlsTransport; - std::shared_ptr mSctpTransport; + shared_ptr mIceTransport; + shared_ptr mDtlsTransport; + shared_ptr mSctpTransport; std::unordered_map> mDataChannels; // by stream ID std::unordered_map> mTracks; // by mid diff --git a/src/nalunit.cpp b/src/nalunit.cpp index 0745efd..92d3976 100644 --- a/src/nalunit.cpp +++ b/src/nalunit.cpp @@ -92,8 +92,6 @@ void NalUnitFragmentA::setFragmentType(FragmentType type) { } } -const uint16_t NalUnits::defaultMaximumFragmentSize = DEFAULT_MTU - 12 - 8 - 40; // SRTP/UDP/IPv6 - std::vector> NalUnits::generateFragments(uint16_t maximumFragmentSize) { vector> result{}; for (auto nalu : *this) {