Reorganized defines and added mtu option to C API

This commit is contained in:
Paul-Louis Ageneau
2021-03-02 12:13:58 +01:00
parent 83453635a8
commit 32c4427b96
10 changed files with 78 additions and 60 deletions

View File

@ -19,14 +19,14 @@
#ifndef RTC_COMMON_H #ifndef RTC_COMMON_H
#define RTC_COMMON_H #define RTC_COMMON_H
#ifndef RTC_ENABLE_MEDIA
#define RTC_ENABLE_MEDIA 1
#endif
#ifndef RTC_ENABLE_WEBSOCKET #ifndef RTC_ENABLE_WEBSOCKET
#define RTC_ENABLE_WEBSOCKET 1 #define RTC_ENABLE_WEBSOCKET 1
#endif #endif
#ifndef RTC_ENABLE_MEDIA
#define RTC_ENABLE_MEDIA 1
#endif
#ifdef _WIN32 #ifdef _WIN32
#define RTC_CPP_EXPORT __declspec(dllexport) #define RTC_CPP_EXPORT __declspec(dllexport)
#ifndef _WIN32_WINNT #ifndef _WIN32_WINNT
@ -39,6 +39,8 @@
#define RTC_CPP_EXPORT #define RTC_CPP_EXPORT
#endif #endif
#include "rtc.h" // for C API defines
#include "log.hpp" #include "log.hpp"
#include "utils.hpp" #include "utils.hpp"

View File

@ -62,11 +62,9 @@ private:
/// Nal unit /// Nal unit
struct RTC_CPP_EXPORT NalUnit : binary { struct RTC_CPP_EXPORT NalUnit : binary {
NalUnit(const NalUnit &unit) = default; NalUnit(const NalUnit &unit) = default;
NalUnit(size_t size, bool includingHeader = true) NalUnit(size_t size, bool includingHeader = true) : binary(size + (includingHeader ? 0 : 1)) {}
: binary(size + (includingHeader ? 0 : 1)) {}
template <typename Iterator> template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
NalUnit(binary &&data) : binary(std::move(data)) {} NalUnit(binary &&data) : binary(std::move(data)) {}
@ -103,7 +101,8 @@ struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType, NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType,
binary data); binary data);
static std::vector<std::shared_ptr<NalUnitFragmentA>> fragmentsFrom(std::shared_ptr<NalUnit> nalu, uint16_t maximumFragmentSize); static std::vector<std::shared_ptr<NalUnitFragmentA>>
fragmentsFrom(std::shared_ptr<NalUnit> nalu, uint16_t maximumFragmentSize);
uint8_t unitType() { return fragmentHeader()->unitType(); } uint8_t unitType() { return fragmentHeader()->unitType(); }
@ -144,7 +143,8 @@ protected:
class RTC_CPP_EXPORT NalUnits : public std::vector<std::shared_ptr<NalUnit>> { class RTC_CPP_EXPORT NalUnits : public std::vector<std::shared_ptr<NalUnit>> {
public: public:
static const uint16_t defaultMaximumFragmentSize; static const uint16_t defaultMaximumFragmentSize =
uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
std::vector<std::shared_ptr<binary>> generateFragments(uint16_t maximumFragmentSize); std::vector<std::shared_ptr<binary>> generateFragments(uint16_t maximumFragmentSize);
}; };

View File

@ -27,7 +27,6 @@
#include "init.hpp" #include "init.hpp"
#include "message.hpp" #include "message.hpp"
#include "reliability.hpp" #include "reliability.hpp"
#include "rtc.hpp"
#include "track.hpp" #include "track.hpp"
#include <chrono> #include <chrono>

View File

@ -39,8 +39,14 @@ extern "C" {
#define RTC_ENABLE_WEBSOCKET 1 #define RTC_ENABLE_WEBSOCKET 1
#endif #endif
#ifndef RTC_ENABLE_MEDIA
#define RTC_ENABLE_MEDIA 1
#endif
#define RTC_DEFAULT_MTU 1280 // IPv6 minimum guaranteed MTU
#if RTC_ENABLE_MEDIA #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) #define RTC_DEFAULT_MAXIMUM_PACKET_COUNT_FOR_NACK_CACHE ((unsigned)512)
#endif #endif
@ -116,13 +122,14 @@ typedef struct {
bool enableIceTcp; bool enableIceTcp;
uint16_t portRangeBegin; uint16_t portRangeBegin;
uint16_t portRangeEnd; uint16_t portRangeEnd;
int mtu; // <= 0 means automatic
} rtcConfiguration; } rtcConfiguration;
typedef struct { typedef struct {
bool unordered; bool unordered;
bool unreliable; bool unreliable;
unsigned int maxPacketLifeTime; // ignored if reliable int maxPacketLifeTime; // ignored if reliable
unsigned int maxRetransmits; // ignored if reliable int maxRetransmits; // ignored if reliable
} rtcReliability; } rtcReliability;
typedef struct { typedef struct {

View File

@ -16,15 +16,25 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
// C API
#include "rtc.h"
// C++ API // C++ API
#include "common.hpp" #include "common.hpp"
#include "init.hpp" // for rtc::Cleanup() #include "init.hpp" // for rtc::Cleanup()
#include "log.hpp" #include "log.hpp"
// //
#include "datachannel.hpp" #include "datachannel.hpp"
#include "track.hpp"
#include "peerconnection.hpp" #include "peerconnection.hpp"
#if RTC_ENABLE_WEBSOCKET
// WebSocket
#include "websocket.hpp" #include "websocket.hpp"
#endif // RTC_ENABLE_WEBSOCKET
#if RTC_ENABLE_MEDIA #if RTC_ENABLE_MEDIA
// Media handling // Media handling
@ -39,5 +49,3 @@
#endif // RTC_ENABLE_MEDIA #endif // RTC_ENABLE_MEDIA
// C API
#include "rtc.h"

View File

@ -16,16 +16,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "common.hpp"
#include "rtc.h" #include "rtc.h"
#include "datachannel.hpp" #include "rtc.hpp"
#include "log.hpp"
#include "peerconnection.hpp"
#if RTC_ENABLE_WEBSOCKET
#include "websocket.hpp"
#endif
#include "plog/Formatters/FuncMessageFormatter.h" #include "plog/Formatters/FuncMessageFormatter.h"
@ -359,12 +352,16 @@ int rtcCreatePeerConnection(const rtcConfiguration *config) {
for (int i = 0; i < config->iceServersCount; ++i) for (int i = 0; i < config->iceServersCount; ++i)
c.iceServers.emplace_back(string(config->iceServers[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.portRangeBegin = config->portRangeBegin;
c.portRangeEnd = config->portRangeEnd; c.portRangeEnd = config->portRangeEnd;
} }
c.enableIceTcp = config->enableIceTcp; if(config->mtu > 0)
c.mtu = size_t(config->mtu);
return emplacePeerConnection(std::make_shared<PeerConnection>(c)); return emplacePeerConnection(std::make_shared<PeerConnection>(c));
}); });
} }
@ -398,7 +395,7 @@ int rtcAddDataChannelEx(int pc, const char *label, const rtcDataChannelInit *ini
dci.reliability.rexmit = milliseconds(reliability->maxPacketLifeTime); dci.reliability.rexmit = milliseconds(reliability->maxPacketLifeTime);
} else { } else {
dci.reliability.type = Reliability::Type::Rexmit; dci.reliability.type = Reliability::Type::Rexmit;
dci.reliability.rexmit = int(reliability->maxRetransmits); dci.reliability.rexmit = reliability->maxRetransmits;
} }
} else { } else {
dci.reliability.type = Reliability::Type::Reliable; dci.reliability.type = Reliability::Type::Reliable;
@ -447,7 +444,8 @@ int rtcDeleteDataChannel(int dc) {
#if RTC_ENABLE_MEDIA #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<string> name = nullopt; optional<string> name = nullopt;
if (_name) { 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, 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([&] { return wrap([&] {
auto peerConnection = getPeerConnection(pc); auto peerConnection = getPeerConnection(pc);
@ -545,8 +544,8 @@ 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, int rtcSetH264PacketizationHandler(int tr, uint32_t ssrc, const char *cname, uint8_t payloadType,
uint32_t clockRate, uint16_t maxFragmentSize, uint16_t sequenceNumber, uint32_t clockRate, uint16_t maxFragmentSize,
uint32_t timestamp) { uint16_t sequenceNumber, uint32_t timestamp) {
return wrap([&] { return wrap([&] {
auto track = getTrack(tr); auto track = getTrack(tr);
// create RTP configuration // create RTP configuration
@ -1012,10 +1011,10 @@ int rtcGetDataChannelReliability(int dc, rtcReliability *reliability) {
reliability->unordered = dcr.unordered; reliability->unordered = dcr.unordered;
if (dcr.type == Reliability::Type::Timed) { if (dcr.type == Reliability::Type::Timed) {
reliability->unreliable = true; reliability->unreliable = true;
reliability->maxPacketLifeTime = unsigned(std::get<milliseconds>(dcr.rexmit).count()); reliability->maxPacketLifeTime = int(std::get<milliseconds>(dcr.rexmit).count());
} else if (dcr.type == Reliability::Type::Rexmit) { } else if (dcr.type == Reliability::Type::Rexmit) {
reliability->unreliable = true; reliability->unreliable = true;
reliability->maxRetransmits = unsigned(std::get<int>(dcr.rexmit)); reliability->maxRetransmits = std::get<int>(dcr.rexmit);
} else { } else {
reliability->unreliable = false; reliability->unreliable = false;
} }

View File

@ -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 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 } // namespace rtc

View File

@ -17,8 +17,8 @@
*/ */
#include "datachannel.hpp" #include "datachannel.hpp"
#include "globals.hpp"
#include "common.hpp" #include "common.hpp"
#include "globals.hpp"
#include "logcounter.hpp" #include "logcounter.hpp"
#include "peerconnection.hpp" #include "peerconnection.hpp"
#include "sctptransport.hpp" #include "sctptransport.hpp"
@ -255,9 +255,8 @@ void DataChannel::incoming(message_ptr message) {
} }
} }
NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr<PeerConnection> pc, NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream,
uint16_t stream, string label, string protocol, string label, string protocol, Reliability reliability)
Reliability reliability)
: DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {} : DataChannel(pc, stream, std::move(label), std::move(protocol), std::move(reliability)) {}
NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr<PeerConnection> pc, NegotiatedDataChannel::NegotiatedDataChannel(std::weak_ptr<PeerConnection> pc,
@ -278,7 +277,7 @@ void NegotiatedDataChannel::open(shared_ptr<SctpTransport> transport) {
switch (mReliability->type) { switch (mReliability->type) {
case Reliability::Type::Rexmit: case Reliability::Type::Rexmit:
channelType = CHANNEL_PARTIAL_RELIABLE_REXMIT; channelType = CHANNEL_PARTIAL_RELIABLE_REXMIT;
reliabilityParameter = uint32_t(std::get<int>(mReliability->rexmit)); reliabilityParameter = uint32_t(std::max(std::get<int>(mReliability->rexmit), 0));
break; break;
case Reliability::Type::Timed: case Reliability::Type::Timed:

View File

@ -19,15 +19,21 @@
#ifndef RTC_IMPL_PEER_CONNECTION_H #ifndef RTC_IMPL_PEER_CONNECTION_H
#define RTC_IMPL_PEER_CONNECTION_H #define RTC_IMPL_PEER_CONNECTION_H
#include "common.hpp"
#include "datachannel.hpp" #include "datachannel.hpp"
#include "dtlstransport.hpp" #include "dtlstransport.hpp"
#include "icetransport.hpp" #include "icetransport.hpp"
#include "common.hpp"
#include "sctptransport.hpp" #include "sctptransport.hpp"
#include "track.hpp" #include "track.hpp"
#include "rtc/peerconnection.hpp" #include "rtc/peerconnection.hpp"
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <unordered_map>
#include <vector>
namespace rtc::impl { namespace rtc::impl {
struct PeerConnection : std::enable_shared_from_this<PeerConnection> { struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
@ -43,12 +49,12 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
std::optional<Description> localDescription() const; std::optional<Description> localDescription() const;
std::optional<Description> remoteDescription() const; std::optional<Description> remoteDescription() const;
std::shared_ptr<IceTransport> initIceTransport(); shared_ptr<IceTransport> initIceTransport();
std::shared_ptr<DtlsTransport> initDtlsTransport(); shared_ptr<DtlsTransport> initDtlsTransport();
std::shared_ptr<SctpTransport> initSctpTransport(); shared_ptr<SctpTransport> initSctpTransport();
std::shared_ptr<IceTransport> getIceTransport() const; shared_ptr<IceTransport> getIceTransport() const;
std::shared_ptr<DtlsTransport> getDtlsTransport() const; shared_ptr<DtlsTransport> getDtlsTransport() const;
std::shared_ptr<SctpTransport> getSctpTransport() const; shared_ptr<SctpTransport> getSctpTransport() const;
void closeTransports(); void closeTransports();
void endLocalCandidates(); void endLocalCandidates();
@ -63,7 +69,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
DataChannelInit init); DataChannelInit init);
shared_ptr<DataChannel> findDataChannel(uint16_t stream); shared_ptr<DataChannel> findDataChannel(uint16_t stream);
void shiftDataChannels(); void shiftDataChannels();
void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func); void iterateDataChannels(std::function<void(shared_ptr<DataChannel> channel)> func);
void openDataChannels(); void openDataChannels();
void closeDataChannels(); void closeDataChannels();
void remoteCloseDataChannels(); void remoteCloseDataChannels();
@ -80,7 +86,7 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
string localBundleMid() const; string localBundleMid() const;
void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel); void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
void triggerTrack(std::shared_ptr<Track> track); void triggerTrack(shared_ptr<Track> track);
bool changeState(State newState); bool changeState(State newState);
bool changeGatheringState(GatheringState newState); bool changeGatheringState(GatheringState newState);
bool changeSignalingState(SignalingState newState); bool changeSignalingState(SignalingState newState);
@ -95,26 +101,26 @@ struct PeerConnection : std::enable_shared_from_this<PeerConnection> {
std::atomic<SignalingState> signalingState = SignalingState::Stable; std::atomic<SignalingState> signalingState = SignalingState::Stable;
std::atomic<bool> negotiationNeeded = false; std::atomic<bool> negotiationNeeded = false;
synchronized_callback<std::shared_ptr<rtc::DataChannel>> dataChannelCallback; synchronized_callback<shared_ptr<rtc::DataChannel>> dataChannelCallback;
synchronized_callback<Description> localDescriptionCallback; synchronized_callback<Description> localDescriptionCallback;
synchronized_callback<Candidate> localCandidateCallback; synchronized_callback<Candidate> localCandidateCallback;
synchronized_callback<State> stateChangeCallback; synchronized_callback<State> stateChangeCallback;
synchronized_callback<GatheringState> gatheringStateChangeCallback; synchronized_callback<GatheringState> gatheringStateChangeCallback;
synchronized_callback<SignalingState> signalingStateChangeCallback; synchronized_callback<SignalingState> signalingStateChangeCallback;
synchronized_callback<std::shared_ptr<rtc::Track>> trackCallback; synchronized_callback<shared_ptr<rtc::Track>> trackCallback;
private: private:
const init_token mInitToken = Init::Token(); const init_token mInitToken = Init::Token();
const future_certificate_ptr mCertificate; const future_certificate_ptr mCertificate;
const std::unique_ptr<Processor> mProcessor; const unique_ptr<Processor> mProcessor;
std::optional<Description> mLocalDescription, mRemoteDescription; std::optional<Description> mLocalDescription, mRemoteDescription;
std::optional<Description> mCurrentLocalDescription; std::optional<Description> mCurrentLocalDescription;
mutable std::mutex mLocalDescriptionMutex, mRemoteDescriptionMutex; mutable std::mutex mLocalDescriptionMutex, mRemoteDescriptionMutex;
std::shared_ptr<IceTransport> mIceTransport; shared_ptr<IceTransport> mIceTransport;
std::shared_ptr<DtlsTransport> mDtlsTransport; shared_ptr<DtlsTransport> mDtlsTransport;
std::shared_ptr<SctpTransport> mSctpTransport; shared_ptr<SctpTransport> mSctpTransport;
std::unordered_map<uint16_t, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID std::unordered_map<uint16_t, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID
std::unordered_map<string, std::weak_ptr<Track>> mTracks; // by mid std::unordered_map<string, std::weak_ptr<Track>> mTracks; // by mid

View File

@ -92,8 +92,6 @@ void NalUnitFragmentA::setFragmentType(FragmentType type) {
} }
} }
const uint16_t NalUnits::defaultMaximumFragmentSize = DEFAULT_MTU - 12 - 8 - 40; // SRTP/UDP/IPv6
std::vector<std::shared_ptr<binary>> NalUnits::generateFragments(uint16_t maximumFragmentSize) { std::vector<std::shared_ptr<binary>> NalUnits::generateFragments(uint16_t maximumFragmentSize) {
vector<std::shared_ptr<binary>> result{}; vector<std::shared_ptr<binary>> result{};
for (auto nalu : *this) { for (auto nalu : *this) {