Unverified 提交 1a805366 authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi 提交者: GitHub

Mediasoup worker as a library: libmediasoup-worker (#533)

上级 fce23d47
...@@ -83,6 +83,12 @@ $ MEDIASOUP_WORKER_BIN="/home/xxx/src/foo/mediasoup-worker" node myapp.js ...@@ -83,6 +83,12 @@ $ MEDIASOUP_WORKER_BIN="/home/xxx/src/foo/mediasoup-worker" node myapp.js
``` ```
### `make libmediasoup-worker`
Builds the `libmediasoup-worker` static library at `worker/out/Release/`.
`MEDIASOUP_MAX_CORES` and `MEDIASOUP_BUILDTYPE` environment variables from above still apply for static library build.
### `make clean` ### `make clean`
Cleans built objects and binaries. Cleans built objects and binaries.
......
...@@ -16,7 +16,7 @@ DOCKER ?= docker ...@@ -16,7 +16,7 @@ DOCKER ?= docker
.PHONY: \ .PHONY: \
default clean clean-all xcode lint format test bear tidy \ default clean clean-all xcode lint format test bear tidy \
fuzzer fuzzer-run-all docker-build docker-run fuzzer fuzzer-run-all docker-build docker-run libmediasoup-worker
default: default:
ifeq ($(MEDIASOUP_WORKER_BIN),) ifeq ($(MEDIASOUP_WORKER_BIN),)
...@@ -101,3 +101,7 @@ docker-run: ...@@ -101,3 +101,7 @@ docker-run:
--cap-add SYS_PTRACE \ --cap-add SYS_PTRACE \
-v $(shell pwd)/../:/mediasoup \ -v $(shell pwd)/../:/mediasoup \
mediasoup/docker:latest mediasoup/docker:latest
libmediasoup-worker:
$(PYTHON) ./scripts/configure.py --no-duplicate-basename-check -R libmediasoup-worker
$(MAKE) -j$(CORES) BUILDTYPE=$(MEDIASOUP_BUILDTYPE) -C out
...@@ -17,7 +17,7 @@ namespace Channel ...@@ -17,7 +17,7 @@ namespace Channel
public: public:
// Passed by argument. // Passed by argument.
static Channel::UnixStreamSocket* channel; thread_local static Channel::UnixStreamSocket* channel;
}; };
} // namespace Channel } // namespace Channel
......
...@@ -19,7 +19,8 @@ namespace Channel ...@@ -19,7 +19,8 @@ namespace Channel
public: public:
enum class MethodId enum class MethodId
{ {
WORKER_DUMP = 1, WORKER_CLOSE = 1,
WORKER_DUMP,
WORKER_GET_RESOURCE_USAGE, WORKER_GET_RESOURCE_USAGE,
WORKER_UPDATE_SETTINGS, WORKER_UPDATE_SETTINGS,
WORKER_CREATE_ROUTER, WORKER_CREATE_ROUTER,
......
...@@ -80,6 +80,7 @@ namespace Channel ...@@ -80,6 +80,7 @@ namespace Channel
// Others. // Others.
ConsumerSocket consumerSocket; ConsumerSocket consumerSocket;
ProducerSocket producerSocket; ProducerSocket producerSocket;
uint8_t* WriteBuffer;
}; };
} // namespace Channel } // namespace Channel
......
...@@ -41,7 +41,7 @@ public: ...@@ -41,7 +41,7 @@ public:
} }
private: private:
static uv_loop_t* loop; thread_local static uv_loop_t* loop;
}; };
#endif #endif
...@@ -37,7 +37,7 @@ public: ...@@ -37,7 +37,7 @@ public:
static RTC::SctpAssociation* RetrieveSctpAssociation(uintptr_t id); static RTC::SctpAssociation* RetrieveSctpAssociation(uintptr_t id);
private: private:
static Checker* checker; thread_local static Checker* checker;
static uint64_t numSctpAssociations; static uint64_t numSctpAssociations;
static uintptr_t nextSctpAssociationId; static uintptr_t nextSctpAssociationId;
static std::unordered_map<uintptr_t, RTC::SctpAssociation*> mapIdSctpAssociation; static std::unordered_map<uintptr_t, RTC::SctpAssociation*> mapIdSctpAssociation;
......
...@@ -133,7 +133,7 @@ public: ...@@ -133,7 +133,7 @@ public:
public: public:
static const int64_t pid; static const int64_t pid;
static Channel::UnixStreamSocket* channel; thread_local static Channel::UnixStreamSocket* channel;
static const size_t bufferSize {50000}; static const size_t bufferSize {50000};
static char buffer[]; static char buffer[];
}; };
......
...@@ -23,7 +23,7 @@ namespace PayloadChannel ...@@ -23,7 +23,7 @@ namespace PayloadChannel
public: public:
// Passed by argument. // Passed by argument.
static PayloadChannel::UnixStreamSocket* payloadChannel; thread_local static PayloadChannel::UnixStreamSocket* payloadChannel;
}; };
} // namespace PayloadChannel } // namespace PayloadChannel
......
...@@ -88,6 +88,7 @@ namespace PayloadChannel ...@@ -88,6 +88,7 @@ namespace PayloadChannel
ProducerSocket producerSocket; ProducerSocket producerSocket;
PayloadChannel::Notification* ongoingNotification{ nullptr }; PayloadChannel::Notification* ongoingNotification{ nullptr };
PayloadChannel::Request* ongoingRequest{ nullptr }; PayloadChannel::Request* ongoingRequest{ nullptr };
uint8_t* WriteBuffer;
}; };
} // namespace PayloadChannel } // namespace PayloadChannel
......
...@@ -137,14 +137,14 @@ namespace RTC ...@@ -137,14 +137,14 @@ namespace RTC
static void GenerateFingerprints(); static void GenerateFingerprints();
private: private:
static X509* certificate; thread_local static X509* certificate;
static EVP_PKEY* privateKey; thread_local static EVP_PKEY* privateKey;
static SSL_CTX* sslCtx; thread_local static SSL_CTX* sslCtx;
static uint8_t sslReadBuffer[]; thread_local static uint8_t sslReadBuffer[];
static std::map<std::string, Role> string2Role; static std::map<std::string, Role> string2Role;
static std::map<std::string, FingerprintAlgorithm> string2FingerprintAlgorithm; static std::map<std::string, FingerprintAlgorithm> string2FingerprintAlgorithm;
static std::map<FingerprintAlgorithm, std::string> fingerprintAlgorithm2String; static std::map<FingerprintAlgorithm, std::string> fingerprintAlgorithm2String;
static std::vector<Fingerprint> localFingerprints; thread_local static std::vector<Fingerprint> localFingerprints;
static std::vector<SrtpCryptoSuiteMapEntry> srtpCryptoSuites; static std::vector<SrtpCryptoSuiteMapEntry> srtpCryptoSuites;
public: public:
......
...@@ -51,7 +51,7 @@ private: ...@@ -51,7 +51,7 @@ private:
static void SetDtlsCertificateAndPrivateKeyFiles(); static void SetDtlsCertificateAndPrivateKeyFiles();
public: public:
static struct Configuration configuration; thread_local static struct Configuration configuration;
private: private:
static std::map<std::string, LogLevel> string2LogLevel; static std::map<std::string, LogLevel> string2LogLevel;
......
...@@ -254,9 +254,9 @@ namespace Utils ...@@ -254,9 +254,9 @@ namespace Utils
static const uint8_t* GetHmacShA1(const std::string& key, const uint8_t* data, size_t len); static const uint8_t* GetHmacShA1(const std::string& key, const uint8_t* data, size_t len);
private: private:
static uint32_t seed; thread_local static uint32_t seed;
static HMAC_CTX* hmacSha1Ctx; thread_local static HMAC_CTX* hmacSha1Ctx;
static uint8_t hmacSha1Buffer[]; thread_local static uint8_t hmacSha1Buffer[];
static const uint32_t crc32Table[256]; static const uint32_t crc32Table[256];
}; };
......
extern "C" int run_worker(
int argc,
char* argv[],
const char* version,
int consumerChannelFd,
int producerChannelFd,
int payloadConsumeChannelFd,
int payloadProduceChannelFd);
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
'sources': 'sources':
[ [
# C++ source files. # C++ source files.
'src/lib.cpp',
'src/DepLibSRTP.cpp', 'src/DepLibSRTP.cpp',
'src/DepLibUV.cpp', 'src/DepLibUV.cpp',
'src/DepLibWebRTC.cpp', 'src/DepLibWebRTC.cpp',
...@@ -127,6 +128,7 @@ ...@@ -127,6 +128,7 @@
'src/RTC/RTCP/XrDelaySinceLastRr.cpp', 'src/RTC/RTCP/XrDelaySinceLastRr.cpp',
'src/RTC/RTCP/XrReceiverReferenceTime.cpp', 'src/RTC/RTCP/XrReceiverReferenceTime.cpp',
# C++ include files. # C++ include files.
'include/lib.hpp',
'include/DepLibSRTP.hpp', 'include/DepLibSRTP.hpp',
'include/DepLibUV.hpp', 'include/DepLibUV.hpp',
'include/DepLibWebRTC.hpp', 'include/DepLibWebRTC.hpp',
...@@ -330,7 +332,12 @@ ...@@ -330,7 +332,12 @@
[ [
# C++ source files. # C++ source files.
'src/main.cpp' 'src/main.cpp'
] ],
'defines': [ 'MS_EXECUTABLE' ]
},
{
'target_name': 'libmediasoup-worker',
'type': 'static_library'
}, },
{ {
'target_name': 'mediasoup-worker-test', 'target_name': 'mediasoup-worker-test',
......
...@@ -8,7 +8,7 @@ namespace Channel ...@@ -8,7 +8,7 @@ namespace Channel
{ {
/* Class variables. */ /* Class variables. */
Channel::UnixStreamSocket* Notifier::channel{ nullptr }; thread_local Channel::UnixStreamSocket* Notifier::channel{ nullptr };
/* Static methods. */ /* Static methods. */
......
...@@ -13,6 +13,7 @@ namespace Channel ...@@ -13,6 +13,7 @@ namespace Channel
// clang-format off // clang-format off
std::unordered_map<std::string, Request::MethodId> Request::string2MethodId = std::unordered_map<std::string, Request::MethodId> Request::string2MethodId =
{ {
{ "worker.close", Request::MethodId::WORKER_CLOSE },
{ "worker.dump", Request::MethodId::WORKER_DUMP }, { "worker.dump", Request::MethodId::WORKER_DUMP },
{ "worker.getResourceUsage", Request::MethodId::WORKER_GET_RESOURCE_USAGE }, { "worker.getResourceUsage", Request::MethodId::WORKER_GET_RESOURCE_USAGE },
{ "worker.updateSettings", Request::MethodId::WORKER_UPDATE_SETTINGS }, { "worker.updateSettings", Request::MethodId::WORKER_UPDATE_SETTINGS },
......
...@@ -19,18 +19,21 @@ namespace Channel ...@@ -19,18 +19,21 @@ namespace Channel
// netstring length for a 4194304 bytes payload. // netstring length for a 4194304 bytes payload.
static constexpr size_t NsMessageMaxLen{ 4194313 }; static constexpr size_t NsMessageMaxLen{ 4194313 };
static constexpr size_t NsPayloadMaxLen{ 4194304 }; static constexpr size_t NsPayloadMaxLen{ 4194304 };
static uint8_t WriteBuffer[NsMessageMaxLen];
/* Instance methods. */ /* Instance methods. */
UnixStreamSocket::UnixStreamSocket(int consumerFd, int producerFd) UnixStreamSocket::UnixStreamSocket(int consumerFd, int producerFd)
: consumerSocket(consumerFd, NsMessageMaxLen, this), producerSocket(producerFd, NsMessageMaxLen) : consumerSocket(consumerFd, NsMessageMaxLen, this), producerSocket(producerFd, NsMessageMaxLen)
{ {
MS_TRACE_STD(); MS_TRACE_STD();
this->WriteBuffer = static_cast<uint8_t*>(std::malloc(NsMessageMaxLen));
} }
UnixStreamSocket::~UnixStreamSocket() UnixStreamSocket::~UnixStreamSocket()
{ {
MS_TRACE_STD(); MS_TRACE_STD();
std::free(this->WriteBuffer);
} }
void UnixStreamSocket::SetListener(Listener* listener) void UnixStreamSocket::SetListener(Listener* listener)
...@@ -85,21 +88,21 @@ namespace Channel ...@@ -85,21 +88,21 @@ namespace Channel
if (nsPayloadLen == 0) if (nsPayloadLen == 0)
{ {
nsNumLen = 1; nsNumLen = 1;
WriteBuffer[0] = '0'; this->WriteBuffer[0] = '0';
WriteBuffer[1] = ':'; this->WriteBuffer[1] = ':';
WriteBuffer[2] = ','; this->WriteBuffer[2] = ',';
} }
else else
{ {
nsNumLen = static_cast<size_t>(std::ceil(std::log10(static_cast<double>(nsPayloadLen) + 1))); nsNumLen = static_cast<size_t>(std::ceil(std::log10(static_cast<double>(nsPayloadLen) + 1)));
std::sprintf(reinterpret_cast<char*>(WriteBuffer), "%zu:", nsPayloadLen); std::sprintf(reinterpret_cast<char*>(this->WriteBuffer), "%zu:", nsPayloadLen);
std::memcpy(WriteBuffer + nsNumLen + 1, nsPayload, nsPayloadLen); std::memcpy(this->WriteBuffer + nsNumLen + 1, nsPayload, nsPayloadLen);
WriteBuffer[nsNumLen + nsPayloadLen + 1] = ','; this->WriteBuffer[nsNumLen + nsPayloadLen + 1] = ',';
} }
size_t nsLen = nsNumLen + nsPayloadLen + 2; size_t nsLen = nsNumLen + nsPayloadLen + 2;
this->producerSocket.Write(WriteBuffer, nsLen); this->producerSocket.Write(this->WriteBuffer, nsLen);
} }
void UnixStreamSocket::OnConsumerSocketMessage( void UnixStreamSocket::OnConsumerSocketMessage(
......
...@@ -4,9 +4,13 @@ ...@@ -4,9 +4,13 @@
#include "DepLibSRTP.hpp" #include "DepLibSRTP.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include <mutex>
/* Static variables. */ /* Static variables. */
static std::mutex globalSyncMutex;
static size_t globalInstances = 0;
// clang-format off // clang-format off
std::vector<const char*> DepLibSRTP::errors = std::vector<const char*> DepLibSRTP::errors =
{ {
...@@ -45,17 +49,34 @@ void DepLibSRTP::ClassInit() ...@@ -45,17 +49,34 @@ void DepLibSRTP::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
{
std::lock_guard<std::mutex> lock(globalSyncMutex);
if (globalInstances == 0)
{
MS_DEBUG_TAG(info, "libsrtp version: \"%s\"", srtp_get_version_string()); MS_DEBUG_TAG(info, "libsrtp version: \"%s\"", srtp_get_version_string());
srtp_err_status_t err = srtp_init(); srtp_err_status_t err = srtp_init();
if (DepLibSRTP::IsError(err)) if (DepLibSRTP::IsError(err))
MS_THROW_ERROR("srtp_init() failed: %s", DepLibSRTP::GetErrorString(err)); MS_THROW_ERROR("srtp_init() failed: %s", DepLibSRTP::GetErrorString(err));
}
++globalInstances;
}
} }
void DepLibSRTP::ClassDestroy() void DepLibSRTP::ClassDestroy()
{ {
MS_TRACE(); MS_TRACE();
{
std::lock_guard<std::mutex> lock(globalSyncMutex);
--globalInstances;
if (globalInstances == 0)
{
srtp_shutdown(); srtp_shutdown();
}
}
} }
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
/* Static variables. */ /* Static variables. */
uv_loop_t* DepLibUV::loop{ nullptr }; thread_local uv_loop_t* DepLibUV::loop{ nullptr };
/* Static methods. */ /* Static methods. */
......
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
#include "DepLibWebRTC.hpp" #include "DepLibWebRTC.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "system_wrappers/source/field_trial.h" // webrtc::field_trial #include "system_wrappers/source/field_trial.h" // webrtc::field_trial
#include <mutex>
/* Static. */ /* Static. */
static std::once_flag globalInitOnce;
static constexpr char FieldTrials[] = "WebRTC-Bwe-AlrLimitedBackoff/Enabled/"; static constexpr char FieldTrials[] = "WebRTC-Bwe-AlrLimitedBackoff/Enabled/";
/* Static methods. */ /* Static methods. */
...@@ -15,7 +17,7 @@ void DepLibWebRTC::ClassInit() ...@@ -15,7 +17,7 @@ void DepLibWebRTC::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
webrtc::field_trial::InitFieldTrialsFromString(FieldTrials); std::call_once(globalInitOnce, [] { webrtc::field_trial::InitFieldTrialsFromString(FieldTrials); });
} }
void DepLibWebRTC::ClassDestroy() void DepLibWebRTC::ClassDestroy()
......
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
#include "Logger.hpp" #include "Logger.hpp"
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/rand.h> #include <openssl/rand.h>
#include <mutex>
/* Static. */
static std::once_flag globalInitOnce;
/* Static methods. */ /* Static methods. */
...@@ -12,8 +17,10 @@ void DepOpenSSL::ClassInit() ...@@ -12,8 +17,10 @@ void DepOpenSSL::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
std::call_once(globalInitOnce, [] {
MS_DEBUG_TAG(info, "openssl version: \"%s\"", OpenSSL_version(OPENSSL_VERSION)); MS_DEBUG_TAG(info, "openssl version: \"%s\"", OpenSSL_version(OPENSSL_VERSION));
// Initialize some crypto stuff. // Initialize some crypto stuff.
RAND_poll(); RAND_poll();
});
} }
...@@ -5,10 +5,13 @@ ...@@ -5,10 +5,13 @@
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include <usrsctp.h> #include <usrsctp.h>
#include <mutex>
/* Static. */ /* Static. */
static constexpr size_t CheckerInterval{ 10u }; // In ms. static constexpr size_t CheckerInterval{ 10u }; // In ms.
static std::mutex globalSyncMutex;
static size_t globalInstances = 0;
/* Static methods for usrsctp global callbacks. */ /* Static methods for usrsctp global callbacks. */
...@@ -49,7 +52,7 @@ inline static void sctpDebug(const char* format, ...) ...@@ -49,7 +52,7 @@ inline static void sctpDebug(const char* format, ...)
/* Static variables. */ /* Static variables. */
DepUsrSCTP::Checker* DepUsrSCTP::checker{ nullptr }; thread_local DepUsrSCTP::Checker* DepUsrSCTP::checker{ nullptr };
uint64_t DepUsrSCTP::numSctpAssociations{ 0u }; uint64_t DepUsrSCTP::numSctpAssociations{ 0u };
uintptr_t DepUsrSCTP::nextSctpAssociationId{ 0u }; uintptr_t DepUsrSCTP::nextSctpAssociationId{ 0u };
std::unordered_map<uintptr_t, RTC::SctpAssociation*> DepUsrSCTP::mapIdSctpAssociation; std::unordered_map<uintptr_t, RTC::SctpAssociation*> DepUsrSCTP::mapIdSctpAssociation;
...@@ -62,6 +65,11 @@ void DepUsrSCTP::ClassInit() ...@@ -62,6 +65,11 @@ void DepUsrSCTP::ClassInit()
MS_DEBUG_TAG(info, "usrsctp"); MS_DEBUG_TAG(info, "usrsctp");
{
std::lock_guard<std::mutex> lock(globalSyncMutex);
if (globalInstances == 0)
{
usrsctp_init_nothreads(0, onSendSctpData, sctpDebug); usrsctp_init_nothreads(0, onSendSctpData, sctpDebug);
// Disable explicit congestion notifications (ecn). // Disable explicit congestion notifications (ecn).
...@@ -70,6 +78,10 @@ void DepUsrSCTP::ClassInit() ...@@ -70,6 +78,10 @@ void DepUsrSCTP::ClassInit()
#ifdef SCTP_DEBUG #ifdef SCTP_DEBUG
usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL); usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
#endif #endif
}
++globalInstances;
}
DepUsrSCTP::checker = new DepUsrSCTP::Checker(); DepUsrSCTP::checker = new DepUsrSCTP::Checker();
} }
...@@ -78,7 +90,20 @@ void DepUsrSCTP::ClassDestroy() ...@@ -78,7 +90,20 @@ void DepUsrSCTP::ClassDestroy()
{ {
MS_TRACE(); MS_TRACE();
{
std::lock_guard<std::mutex> lock(globalSyncMutex);
--globalInstances;
if (globalInstances == 0)
{
usrsctp_finish(); usrsctp_finish();
// TODO: This cleanup currently causes assertion errors in
// DepUsrSCTP::DeregisterSctpAssociation()
// numSctpAssociations = 0u; nextSctpAssociationId = 0u;
// DepUsrSCTP::mapIdSctpAssociation.clear();
}
}
delete DepUsrSCTP::checker; delete DepUsrSCTP::checker;
} }
...@@ -87,6 +112,8 @@ uintptr_t DepUsrSCTP::GetNextSctpAssociationId() ...@@ -87,6 +112,8 @@ uintptr_t DepUsrSCTP::GetNextSctpAssociationId()
{ {
MS_TRACE(); MS_TRACE();
std::lock_guard<std::mutex> lock(globalSyncMutex);
// NOTE: usrsctp_connect() fails with a value of 0. // NOTE: usrsctp_connect() fails with a value of 0.
if (DepUsrSCTP::nextSctpAssociationId == 0u) if (DepUsrSCTP::nextSctpAssociationId == 0u)
++DepUsrSCTP::nextSctpAssociationId; ++DepUsrSCTP::nextSctpAssociationId;
...@@ -109,6 +136,8 @@ void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation) ...@@ -109,6 +136,8 @@ void DepUsrSCTP::RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation)
{ {
MS_TRACE(); MS_TRACE();
std::lock_guard<std::mutex> lock(globalSyncMutex);
auto it = DepUsrSCTP::mapIdSctpAssociation.find(sctpAssociation->id); auto it = DepUsrSCTP::mapIdSctpAssociation.find(sctpAssociation->id);
MS_ASSERT( MS_ASSERT(
...@@ -125,6 +154,8 @@ void DepUsrSCTP::DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation ...@@ -125,6 +154,8 @@ void DepUsrSCTP::DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation
{ {
MS_TRACE(); MS_TRACE();
std::lock_guard<std::mutex> lock(globalSyncMutex);
auto found = DepUsrSCTP::mapIdSctpAssociation.erase(sctpAssociation->id); auto found = DepUsrSCTP::mapIdSctpAssociation.erase(sctpAssociation->id);
MS_ASSERT(found > 0, "SctpAssociation not found"); MS_ASSERT(found > 0, "SctpAssociation not found");
...@@ -138,6 +169,8 @@ RTC::SctpAssociation* DepUsrSCTP::RetrieveSctpAssociation(uintptr_t id) ...@@ -138,6 +169,8 @@ RTC::SctpAssociation* DepUsrSCTP::RetrieveSctpAssociation(uintptr_t id)
{ {
MS_TRACE(); MS_TRACE();
std::lock_guard<std::mutex> lock(globalSyncMutex);
auto it = DepUsrSCTP::mapIdSctpAssociation.find(id); auto it = DepUsrSCTP::mapIdSctpAssociation.find(id);
if (it == DepUsrSCTP::mapIdSctpAssociation.end()) if (it == DepUsrSCTP::mapIdSctpAssociation.end())
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
/* Class variables. */ /* Class variables. */
const int64_t Logger::pid{ static_cast<int64_t>(uv_os_getpid()) }; const int64_t Logger::pid{ static_cast<int64_t>(uv_os_getpid()) };
Channel::UnixStreamSocket* Logger::channel{ nullptr }; thread_local Channel::UnixStreamSocket* Logger::channel{ nullptr };
char Logger::buffer[Logger::bufferSize]; char Logger::buffer[Logger::bufferSize];
/* Class methods. */ /* Class methods. */
......
...@@ -8,7 +8,7 @@ namespace PayloadChannel ...@@ -8,7 +8,7 @@ namespace PayloadChannel
{ {
/* Class variables. */ /* Class variables. */
PayloadChannel::UnixStreamSocket* Notifier::payloadChannel{ nullptr }; thread_local PayloadChannel::UnixStreamSocket* Notifier::payloadChannel{ nullptr };
/* Static methods. */ /* Static methods. */
......
...@@ -20,19 +20,21 @@ namespace PayloadChannel ...@@ -20,19 +20,21 @@ namespace PayloadChannel
// netstring length for a 4194304 bytes payload. // netstring length for a 4194304 bytes payload.
static constexpr size_t NsMessageMaxLen{ 4194313 }; static constexpr size_t NsMessageMaxLen{ 4194313 };
static constexpr size_t NsPayloadMaxLen{ 4194304 }; static constexpr size_t NsPayloadMaxLen{ 4194304 };
static uint8_t WriteBuffer[NsMessageMaxLen];
/* Instance methods. */ /* Instance methods. */
UnixStreamSocket::UnixStreamSocket(int consumerFd, int producerFd) UnixStreamSocket::UnixStreamSocket(int consumerFd, int producerFd)
: consumerSocket(consumerFd, NsMessageMaxLen, this), producerSocket(producerFd, NsMessageMaxLen) : consumerSocket(consumerFd, NsMessageMaxLen, this), producerSocket(producerFd, NsMessageMaxLen)
{ {
MS_TRACE(); MS_TRACE();
this->WriteBuffer = (uint8_t*)std::malloc(NsMessageMaxLen);
} }
UnixStreamSocket::~UnixStreamSocket() UnixStreamSocket::~UnixStreamSocket()
{ {
MS_TRACE(); MS_TRACE();
std::free(this->WriteBuffer);
delete this->ongoingNotification; delete this->ongoingNotification;
} }
...@@ -97,21 +99,21 @@ namespace PayloadChannel ...@@ -97,21 +99,21 @@ namespace PayloadChannel
if (nsPayloadLen == 0) if (nsPayloadLen == 0)
{ {
nsNumLen = 1; nsNumLen = 1;
WriteBuffer[0] = '0'; this->WriteBuffer[0] = '0';
WriteBuffer[1] = ':'; this->WriteBuffer[1] = ':';
WriteBuffer[2] = ','; this->WriteBuffer[2] = ',';
} }
else else
{ {
nsNumLen = static_cast<size_t>(std::ceil(std::log10(static_cast<double>(nsPayloadLen) + 1))); nsNumLen = static_cast<size_t>(std::ceil(std::log10(static_cast<double>(nsPayloadLen) + 1)));
std::sprintf(reinterpret_cast<char*>(WriteBuffer), "%zu:", nsPayloadLen); std::sprintf(reinterpret_cast<char*>(this->WriteBuffer), "%zu:", nsPayloadLen);
std::memcpy(WriteBuffer + nsNumLen + 1, nsPayload, nsPayloadLen); std::memcpy(this->WriteBuffer + nsNumLen + 1, nsPayload, nsPayloadLen);
WriteBuffer[nsNumLen + nsPayloadLen + 1] = ','; this->WriteBuffer[nsNumLen + nsPayloadLen + 1] = ',';
} }
size_t nsLen = nsNumLen + nsPayloadLen + 2; size_t nsLen = nsNumLen + nsPayloadLen + 2;
this->producerSocket.Write(WriteBuffer, nsLen); this->producerSocket.Write(this->WriteBuffer, nsLen);
} }
void UnixStreamSocket::OnConsumerSocketMessage( void UnixStreamSocket::OnConsumerSocketMessage(
......
...@@ -78,10 +78,10 @@ namespace RTC ...@@ -78,10 +78,10 @@ namespace RTC
/* Class variables. */ /* Class variables. */
X509* DtlsTransport::certificate{ nullptr }; thread_local X509* DtlsTransport::certificate{ nullptr };
EVP_PKEY* DtlsTransport::privateKey{ nullptr }; thread_local EVP_PKEY* DtlsTransport::privateKey{ nullptr };
SSL_CTX* DtlsTransport::sslCtx{ nullptr }; thread_local SSL_CTX* DtlsTransport::sslCtx{ nullptr };
uint8_t DtlsTransport::sslReadBuffer[SslReadBufferSize]; thread_local uint8_t DtlsTransport::sslReadBuffer[SslReadBufferSize];
// clang-format off // clang-format off
std::map<std::string, DtlsTransport::FingerprintAlgorithm> DtlsTransport::string2FingerprintAlgorithm = std::map<std::string, DtlsTransport::FingerprintAlgorithm> DtlsTransport::string2FingerprintAlgorithm =
{ {
...@@ -105,7 +105,7 @@ namespace RTC ...@@ -105,7 +105,7 @@ namespace RTC
{ "client", DtlsTransport::Role::CLIENT }, { "client", DtlsTransport::Role::CLIENT },
{ "server", DtlsTransport::Role::SERVER } { "server", DtlsTransport::Role::SERVER }
}; };
std::vector<DtlsTransport::Fingerprint> DtlsTransport::localFingerprints; thread_local std::vector<DtlsTransport::Fingerprint> DtlsTransport::localFingerprints;
std::vector<DtlsTransport::SrtpCryptoSuiteMapEntry> DtlsTransport::srtpCryptoSuites = std::vector<DtlsTransport::SrtpCryptoSuiteMapEntry> DtlsTransport::srtpCryptoSuites =
{ {
{ RTC::SrtpSession::CryptoSuite::AEAD_AES_256_GCM, "SRTP_AEAD_AES_256_GCM" }, { RTC::SrtpSession::CryptoSuite::AEAD_AES_256_GCM, "SRTP_AEAD_AES_256_GCM" },
......
...@@ -11,7 +11,7 @@ namespace RTC ...@@ -11,7 +11,7 @@ namespace RTC
/* Static. */ /* Static. */
static constexpr size_t StunSerializeBufferSize{ 65536 }; static constexpr size_t StunSerializeBufferSize{ 65536 };
static uint8_t StunSerializeBuffer[StunSerializeBufferSize]; thread_local static uint8_t StunSerializeBuffer[StunSerializeBufferSize];
/* Instance methods. */ /* Instance methods. */
......
...@@ -12,7 +12,8 @@ namespace RTC ...@@ -12,7 +12,8 @@ namespace RTC
// 17: 16 bit mask + the initial sequence number. // 17: 16 bit mask + the initial sequence number.
static constexpr size_t MaxRequestedPackets{ 17 }; static constexpr size_t MaxRequestedPackets{ 17 };
static std::vector<RTC::RtpStreamSend::StorageItem*> RetransmissionContainer(MaxRequestedPackets + 1); thread_local static std::vector<RTC::RtpStreamSend::StorageItem*> RetransmissionContainer(
MaxRequestedPackets + 1);
// Don't retransmit packets older than this (ms). // Don't retransmit packets older than this (ms).
static constexpr uint32_t MaxRetransmissionDelay{ 2000 }; static constexpr uint32_t MaxRetransmissionDelay{ 2000 };
static constexpr uint32_t DefaultRtt{ 100 }; static constexpr uint32_t DefaultRtt{ 100 };
......
...@@ -12,7 +12,7 @@ namespace RTC ...@@ -12,7 +12,7 @@ namespace RTC
/* Static. */ /* Static. */
static constexpr size_t EncryptBufferSize{ 65536 }; static constexpr size_t EncryptBufferSize{ 65536 };
static uint8_t EncryptBuffer[EncryptBufferSize]; thread_local static uint8_t EncryptBuffer[EncryptBufferSize];
/* Class methods. */ /* Class methods. */
......
...@@ -11,7 +11,7 @@ namespace RTC ...@@ -11,7 +11,7 @@ namespace RTC
/* Static. */ /* Static. */
static constexpr size_t ReadBufferSize{ 65536 }; static constexpr size_t ReadBufferSize{ 65536 };
static uint8_t ReadBuffer[ReadBufferSize]; thread_local static uint8_t ReadBuffer[ReadBufferSize];
/* Instance methods. */ /* Instance methods. */
......
...@@ -9,15 +9,20 @@ ...@@ -9,15 +9,20 @@
#include <cctype> // isprint() #include <cctype> // isprint()
#include <cerrno> #include <cerrno>
#include <iterator> // std::ostream_iterator #include <iterator> // std::ostream_iterator
#include <mutex>
#include <sstream> // std::ostringstream #include <sstream> // std::ostringstream
extern "C" extern "C"
{ {
#include <getopt.h> #include <getopt.h>
} }
/* Static. */
static std::mutex globalSyncMutex;
/* Class variables. */ /* Class variables. */
struct Settings::Configuration Settings::configuration; thread_local struct Settings::Configuration Settings::configuration;
// clang-format off // clang-format off
std::map<std::string, LogLevel> Settings::string2LogLevel = std::map<std::string, LogLevel> Settings::string2LogLevel =
{ {
...@@ -62,6 +67,10 @@ void Settings::SetConfiguration(int argc, char* argv[]) ...@@ -62,6 +67,10 @@ void Settings::SetConfiguration(int argc, char* argv[])
/* Parse command line options. */ /* Parse command line options. */
// getopt_long_only() is not thread-safe
std::lock_guard<std::mutex> lock(globalSyncMutex);
optind = 1; // Set explicitly, otherwise subsequent runs will fail.
opterr = 0; // Don't allow getopt to print error messages. opterr = 0; // Don't allow getopt to print error messages.
while ((c = getopt_long_only(argc, argv, "", options, &optionIdx)) != -1) while ((c = getopt_long_only(argc, argv, "", options, &optionIdx)) != -1)
{ {
......
...@@ -9,9 +9,9 @@ namespace Utils ...@@ -9,9 +9,9 @@ namespace Utils
{ {
/* Static variables. */ /* Static variables. */
uint32_t Crypto::seed; thread_local uint32_t Crypto::seed;
HMAC_CTX* Crypto::hmacSha1Ctx{ nullptr }; thread_local HMAC_CTX* Crypto::hmacSha1Ctx{ nullptr };
uint8_t Crypto::hmacSha1Buffer[20]; // SHA-1 result is 20 bytes long. thread_local uint8_t Crypto::hmacSha1Buffer[20]; // SHA-1 result is 20 bytes long.
// clang-format off // clang-format off
const uint32_t Crypto::crc32Table[] = const uint32_t Crypto::crc32Table[] =
{ {
......
...@@ -24,9 +24,13 @@ Worker::Worker(::Channel::UnixStreamSocket* channel, PayloadChannel::UnixStreamS ...@@ -24,9 +24,13 @@ Worker::Worker(::Channel::UnixStreamSocket* channel, PayloadChannel::UnixStreamS
// Set the signals handler. // Set the signals handler.
this->signalsHandler = new SignalsHandler(this); this->signalsHandler = new SignalsHandler(this);
#ifdef MS_EXECUTABLE
{
// Add signals to handle. // Add signals to handle.
this->signalsHandler->AddSignal(SIGINT, "INT"); this->signalsHandler->AddSignal(SIGINT, "INT");
this->signalsHandler->AddSignal(SIGTERM, "TERM"); this->signalsHandler->AddSignal(SIGTERM, "TERM");
}
#endif
// Tell the Node process that we are running. // Tell the Node process that we are running.
Channel::Notifier::Emit(std::to_string(Logger::pid), "running"); Channel::Notifier::Emit(std::to_string(Logger::pid), "running");
...@@ -197,6 +201,18 @@ inline void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Cha ...@@ -197,6 +201,18 @@ inline void Worker::OnChannelRequest(Channel::UnixStreamSocket* /*channel*/, Cha
switch (request->methodId) switch (request->methodId)
{ {
case Channel::Request::MethodId::WORKER_CLOSE:
{
if (this->closed)
return;
MS_DEBUG_DEV("Worker close request, stopping");
Close();
break;
}
case Channel::Request::MethodId::WORKER_DUMP: case Channel::Request::MethodId::WORKER_DUMP:
{ {
json data = json::object(); json data = json::object();
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
/* Static. */ /* Static. */
static constexpr size_t ReadBufferSize{ 65536 }; static constexpr size_t ReadBufferSize{ 65536 };
static uint8_t ReadBuffer[ReadBufferSize]; thread_local static uint8_t ReadBuffer[ReadBufferSize];
/* Static methods for UV callbacks. */ /* Static methods for UV callbacks. */
......
...@@ -143,7 +143,7 @@ void UnixStreamSocket::Close() ...@@ -143,7 +143,7 @@ void UnixStreamSocket::Close()
} }
// If there is no error and the peer didn't close its pipe side then close gracefully. // If there is no error and the peer didn't close its pipe side then close gracefully.
if (!this->hasError && !this->isClosedByPeer) if (this->role == UnixStreamSocket::Role::PRODUCER && !this->hasError && !this->isClosedByPeer)
{ {
// Use uv_shutdown() so pending data to be written will be sent to the peer before closing. // Use uv_shutdown() so pending data to be written will be sent to the peer before closing.
auto req = new uv_shutdown_t; auto req = new uv_shutdown_t;
......
#define MS_CLASS "mediasoup-worker"
// #define MS_LOG_DEV_LEVEL 3
#include "common.hpp"
#include "DepLibSRTP.hpp"
#include "DepLibUV.hpp"
#include "DepLibWebRTC.hpp"
#include "DepOpenSSL.hpp"
#include "DepUsrSCTP.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include "Settings.hpp"
#include "Utils.hpp"
#include "Worker.hpp"
#include "Channel/Notifier.hpp"
#include "Channel/UnixStreamSocket.hpp"
#include "PayloadChannel/Notifier.hpp"
#include "PayloadChannel/UnixStreamSocket.hpp"
#include "RTC/DtlsTransport.hpp"
#include "RTC/SrtpSession.hpp"
#include <uv.h>
#include <cerrno>
#include <csignal> // sigaction()
#include <cstdlib> // std::_Exit(), std::genenv()
#include <iostream> // std::cerr, std::endl
#include <map>
#include <string>
void IgnoreSignals();
extern "C" int run_worker(
int argc,
char* argv[],
const char* version,
int consumerChannelFd,
int producerChannelFd,
int payloadConsumeChannelFd,
int payloadProduceChannelFd)
{
// Initialize libuv stuff (we need it for the Channel).
DepLibUV::ClassInit();
// Channel socket (it will be handled and deleted by the Worker).
Channel::UnixStreamSocket* channel{ nullptr };
// PayloadChannel socket (it will be handled and deleted by the Worker).
PayloadChannel::UnixStreamSocket* payloadChannel{ nullptr };
try
{
channel = new Channel::UnixStreamSocket(consumerChannelFd, producerChannelFd);
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the Channel: %s", error.what());
return 1;
}
try
{
payloadChannel =
new PayloadChannel::UnixStreamSocket(payloadConsumeChannelFd, payloadProduceChannelFd);
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the RTC Channel: %s", error.what());
return 1;
}
// Initialize the Logger.
Logger::ClassInit(channel);
try
{
Settings::SetConfiguration(argc, argv);
}
catch (const MediaSoupTypeError& error)
{
MS_ERROR_STD("settings error: %s", error.what());
// 42 is a custom exit code to notify "settings error" to the Node library.
return 42;
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("unexpected settings error: %s", error.what());
return 1;
}
MS_DEBUG_TAG(info, "starting mediasoup-worker process [version:%s]", version);
#if defined(MS_LITTLE_ENDIAN)
MS_DEBUG_TAG(info, "little-endian CPU detected");
#elif defined(MS_BIG_ENDIAN)
MS_DEBUG_TAG(info, "big-endian CPU detected");
#else
MS_WARN_TAG(info, "cannot determine whether little-endian or big-endian");
#endif
#if defined(INTPTR_MAX) && defined(INT32_MAX) && (INTPTR_MAX == INT32_MAX)
MS_DEBUG_TAG(info, "32 bits architecture detected");
#elif defined(INTPTR_MAX) && defined(INT64_MAX) && (INTPTR_MAX == INT64_MAX)
MS_DEBUG_TAG(info, "64 bits architecture detected");
#else
MS_WARN_TAG(info, "cannot determine 32 or 64 bits architecture");
#endif
Settings::PrintConfiguration();
DepLibUV::PrintVersion();
try
{
// Initialize static stuff.
DepOpenSSL::ClassInit();
DepLibSRTP::ClassInit();
DepUsrSCTP::ClassInit();
DepLibWebRTC::ClassInit();
Utils::Crypto::ClassInit();
RTC::DtlsTransport::ClassInit();
RTC::SrtpSession::ClassInit();
Channel::Notifier::ClassInit(channel);
PayloadChannel::Notifier::ClassInit(payloadChannel);
#ifdef MS_EXECUTABLE
{
// Ignore some signals.
IgnoreSignals();
}
#endif
// Run the Worker.
Worker worker(channel, payloadChannel);
// Free static stuff.
DepLibUV::ClassDestroy();
DepLibSRTP::ClassDestroy();
Utils::Crypto::ClassDestroy();
DepLibWebRTC::ClassDestroy();
RTC::DtlsTransport::ClassDestroy();
DepUsrSCTP::ClassDestroy();
// Wait a bit so peding messages to stdout/Channel arrive to the Node
// process.
uv_sleep(200);
return 0;
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("failure exit: %s", error.what());
return 1;
}
}
void IgnoreSignals()
{
#ifndef _WIN32
MS_TRACE();
int err;
struct sigaction act; // NOLINT(cppcoreguidelines-pro-type-member-init)
// clang-format off
std::map<std::string, int> ignoredSignals =
{
{ "PIPE", SIGPIPE },
{ "HUP", SIGHUP },
{ "ALRM", SIGALRM },
{ "USR1", SIGUSR1 },
{ "USR2", SIGUSR2 }
};
// clang-format on
act.sa_handler = SIG_IGN; // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
act.sa_flags = 0;
err = sigfillset(&act.sa_mask);
if (err != 0)
MS_THROW_ERROR("sigfillset() failed: %s", std::strerror(errno));
for (auto& kv : ignoredSignals)
{
const auto& sigName = kv.first;
int sigId = kv.second;
err = sigaction(sigId, &act, nullptr);
if (err != 0)
MS_THROW_ERROR("sigaction() failed for signal %s: %s", sigName.c_str(), std::strerror(errno));
}
#endif
}
#define MS_CLASS "mediasoup-worker" #define MS_CLASS "mediasoup-worker"
// #define MS_LOG_DEV_LEVEL 3 // #define MS_LOG_DEV_LEVEL 3
#include "common.hpp"
#include "DepLibSRTP.hpp"
#include "DepLibUV.hpp"
#include "DepLibWebRTC.hpp"
#include "DepOpenSSL.hpp"
#include "DepUsrSCTP.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "Settings.hpp" #include "lib.hpp"
#include "Utils.hpp"
#include "Worker.hpp"
#include "Channel/Notifier.hpp"
#include "Channel/UnixStreamSocket.hpp"
#include "PayloadChannel/Notifier.hpp"
#include "PayloadChannel/UnixStreamSocket.hpp"
#include "RTC/DtlsTransport.hpp"
#include "RTC/SrtpSession.hpp"
#include <uv.h>
#include <cerrno>
#include <csignal> // sigaction()
#include <cstdlib> // std::_Exit(), std::genenv() #include <cstdlib> // std::_Exit(), std::genenv()
#include <iostream> // std::cerr, std::endl
#include <map>
#include <string> #include <string>
static constexpr int ConsumerChannelFd{ 3 }; static constexpr int ConsumerChannelFd{ 3 };
...@@ -31,8 +11,6 @@ static constexpr int ProducerChannelFd{ 4 }; ...@@ -31,8 +11,6 @@ static constexpr int ProducerChannelFd{ 4 };
static constexpr int PayloadConsumerChannelFd{ 5 }; static constexpr int PayloadConsumerChannelFd{ 5 };
static constexpr int PayloadProducerChannelFd{ 6 }; static constexpr int PayloadProducerChannelFd{ 6 };
void IgnoreSignals();
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Ensure we are called by our Node library. // Ensure we are called by our Node library.
...@@ -45,156 +23,22 @@ int main(int argc, char* argv[]) ...@@ -45,156 +23,22 @@ int main(int argc, char* argv[])
std::string version = std::getenv("MEDIASOUP_VERSION"); std::string version = std::getenv("MEDIASOUP_VERSION");
// Initialize libuv stuff (we need it for the Channel). auto statusCode = run_worker(
DepLibUV::ClassInit(); argc,
argv,
// Channel socket (it will be handled and deleted by the Worker). version.c_str(),
Channel::UnixStreamSocket* channel{ nullptr }; ConsumerChannelFd,
ProducerChannelFd,
// PayloadChannel socket (it will be handled and deleted by the Worker). PayloadConsumerChannelFd,
PayloadChannel::UnixStreamSocket* payloadChannel{ nullptr }; PayloadProducerChannelFd);
try
{
channel = new Channel::UnixStreamSocket(ConsumerChannelFd, ProducerChannelFd);
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the Channel: %s", error.what());
std::_Exit(EXIT_FAILURE);
}
try
{
payloadChannel =
new PayloadChannel::UnixStreamSocket(PayloadConsumerChannelFd, PayloadProducerChannelFd);
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the RTC Channel: %s", error.what());
std::_Exit(EXIT_FAILURE);
}
// Initialize the Logger.
Logger::ClassInit(channel);
try
{
Settings::SetConfiguration(argc, argv);
}
catch (const MediaSoupTypeError& error)
{
MS_ERROR_STD("settings error: %s", error.what());
// 42 is a custom exit code to notify "settings error" to the Node library. switch (statusCode)
std::_Exit(42);
}
catch (const MediaSoupError& error)
{ {
MS_ERROR_STD("unexpected settings error: %s", error.what()); case 0:
std::_Exit(EXIT_FAILURE);
}
MS_DEBUG_TAG(info, "starting mediasoup-worker process [version:%s]", version.c_str());
#if defined(MS_LITTLE_ENDIAN)
MS_DEBUG_TAG(info, "little-endian CPU detected");
#elif defined(MS_BIG_ENDIAN)
MS_DEBUG_TAG(info, "big-endian CPU detected");
#else
MS_WARN_TAG(info, "cannot determine whether little-endian or big-endian");
#endif
#if defined(INTPTR_MAX) && defined(INT32_MAX) && (INTPTR_MAX == INT32_MAX)
MS_DEBUG_TAG(info, "32 bits architecture detected");
#elif defined(INTPTR_MAX) && defined(INT64_MAX) && (INTPTR_MAX == INT64_MAX)
MS_DEBUG_TAG(info, "64 bits architecture detected");
#else
MS_WARN_TAG(info, "cannot determine 32 or 64 bits architecture");
#endif
Settings::PrintConfiguration();
DepLibUV::PrintVersion();
try
{
// Initialize static stuff.
DepOpenSSL::ClassInit();
DepLibSRTP::ClassInit();
DepUsrSCTP::ClassInit();
DepLibWebRTC::ClassInit();
Utils::Crypto::ClassInit();
RTC::DtlsTransport::ClassInit();
RTC::SrtpSession::ClassInit();
Channel::Notifier::ClassInit(channel);
PayloadChannel::Notifier::ClassInit(payloadChannel);
// Ignore some signals.
IgnoreSignals();
// Run the Worker.
Worker worker(channel, payloadChannel);
// Free static stuff.
DepLibUV::ClassDestroy();
DepLibSRTP::ClassDestroy();
Utils::Crypto::ClassDestroy();
DepLibWebRTC::ClassDestroy();
RTC::DtlsTransport::ClassDestroy();
DepUsrSCTP::ClassDestroy();
// Wait a bit so peding messages to stdout/Channel arrive to the Node
// process.
uv_sleep(200);
std::_Exit(EXIT_SUCCESS); std::_Exit(EXIT_SUCCESS);
} case 1:
catch (const MediaSoupError& error)
{
MS_ERROR_STD("failure exit: %s", error.what());
std::_Exit(EXIT_FAILURE); std::_Exit(EXIT_FAILURE);
case 42:
std::_Exit(42);
} }
} }
void IgnoreSignals()
{
#ifndef _WIN32
MS_TRACE();
int err;
struct sigaction act; // NOLINT(cppcoreguidelines-pro-type-member-init)
// clang-format off
std::map<std::string, int> ignoredSignals =
{
{ "PIPE", SIGPIPE },
{ "HUP", SIGHUP },
{ "ALRM", SIGALRM },
{ "USR1", SIGUSR1 },
{ "USR2", SIGUSR2 }
};
// clang-format on
act.sa_handler = SIG_IGN; // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
act.sa_flags = 0;
err = sigfillset(&act.sa_mask);
if (err != 0)
MS_THROW_ERROR("sigfillset() failed: %s", std::strerror(errno));
for (auto& kv : ignoredSignals)
{
const auto& sigName = kv.first;
int sigId = kv.second;
err = sigaction(sigId, &act, nullptr);
if (err != 0)
MS_THROW_ERROR("sigaction() failed for signal %s: %s", sigName.c_str(), std::strerror(errno));
}
#endif
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论