- 1)基础知识回顾
- 2)各模块初始化
- DepOpenSSL::ClassInit();
- DepLibSRTP::ClassInit();
- DepUsrSCTP::ClassInit();
- DepLibWebRTC::ClassInit();
- Utils::Crypto::ClassInit();
- RTC::DtlsTransport::ClassInit();
- RTC::SrtpSession::ClassInit();
- Channel::ChannelNotifier::ClassInit(channel.get());
- PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel.get());
1)基础知识回顾
2)各模块初始化
新版本在mediasoup\worker\src\lib.cpp
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. If Worker instance runs properly, this socket is closed by
// it in its destructor. Otherwise it's closed here by also letting libuv
// deallocate its UV handles.
std::unique_ptr<Channel::ChannelSocket> channel{ nullptr };
// PayloadChannel socket. If Worker instance runs properly, this socket is
// closed by it in its destructor. Otherwise it's closed here by also letting
// libuv deallocate its UV handles.
std::unique_ptr<PayloadChannel::PayloadChannelSocket> payloadChannel{ nullptr };
try
{
channel.reset(new Channel::ChannelSocket(consumerChannelFd, producerChannelFd));
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the Channel: %s", error.what());
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
return 1;
}
try
{
payloadChannel.reset(
new PayloadChannel::PayloadChannelSocket(payloadConsumeChannelFd, payloadProduceChannelFd));
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("error creating the RTC Channel: %s", error.what());
channel->Close();
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
return 1;
}
// Initialize the Logger.
Logger::ClassInit(channel.get());
try
{
Settings::SetConfiguration(argc, argv);
}
catch (const MediaSoupTypeError& error)
{
MS_ERROR_STD("settings error: %s", error.what());
channel->Close();
payloadChannel->Close();
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
// 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());
channel->Close();
payloadChannel->Close();
DepLibUV::RunLoop();
DepLibUV::ClassDestroy();
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::ChannelNotifier::ClassInit(channel.get());
PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel.get());
#ifdef MS_EXECUTABLE
// Ignore some signals.
IgnoreSignals();
#endif
// Run the Worker.
Worker worker(channel.get(), payloadChannel.get());
// Free static stuff.
DepLibSRTP::ClassDestroy();
Utils::Crypto::ClassDestroy();
DepLibWebRTC::ClassDestroy();
RTC::DtlsTransport::ClassDestroy();
DepUsrSCTP::ClassDestroy();
DepLibUV::ClassDestroy();
#ifdef MS_EXECUTABLE
// Wait a bit so pending messages to stdout/Channel arrive to the Node
// process.
uv_sleep(200);
#endif
return 0;
}
catch (const MediaSoupError& error)
{
MS_ERROR_STD("failure exit: %s", error.what());
return 1;
}
}
DepOpenSSL::ClassInit();
位置mediasoup\worker\src\DepOpenSSL.cpp
void DepOpenSSL::ClassInit()
{
MS_TRACE();
std::call_once(globalInitOnce, [] {
MS_DEBUG_TAG(info, "openssl version: \"%s\"", OpenSSL_version(OPENSSL_VERSION));
// Initialize some crypto stuff.
RAND_poll();
});
}
DepLibSRTP::ClassInit();
位置mediasoup\worker\src\DepLibSRTP.cpp
void DepLibSRTP::ClassInit()
{
MS_TRACE();
{
std::lock_guard<std::mutex> lock(globalSyncMutex);
if (globalInstances == 0)
{
MS_DEBUG_TAG(info, "libsrtp version: \"%s\"", srtp_get_version_string());
srtp_err_status_t err = srtp_init();
if (DepLibSRTP::IsError(err))
MS_THROW_ERROR("srtp_init() failed: %s", DepLibSRTP::GetErrorString(err));
}
++globalInstances;
}
}
DepUsrSCTP::ClassInit();
位置:mediasoup\worker\src\DepUsrSCTP.cpp
void DepUsrSCTP::ClassInit()
{
MS_TRACE();
MS_DEBUG_TAG(info, "usrsctp");
{
std::lock_guard<std::mutex> lock(globalSyncMutex);
if (globalInstances == 0)
{
usrsctp_init_nothreads(0, onSendSctpData, sctpDebug);
// Disable explicit congestion notifications (ecn).
usrsctp_sysctl_set_sctp_ecn_enable(0);
#ifdef SCTP_DEBUG
usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
#endif
}
++globalInstances;
}
}
DepLibWebRTC::ClassInit();
位置:mediasoup\worker\src\DepLibWebRTC.cpp
void DepLibWebRTC::ClassInit()
{
MS_TRACE();
std::call_once(globalInitOnce, [] { webrtc::field_trial::InitFieldTrialsFromString(FieldTrials); });
}
Utils::Crypto::ClassInit();
位置:mediasoup\worker\src\Utils\Crypto.cpp
void Crypto::ClassInit()
{
MS_TRACE();
// Init the vrypto seed with a random number taken from the address
// of the seed variable itself (which is random).
Crypto::seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(std::addressof(Crypto::seed)));
// Create an OpenSSL HMAC_CTX context for HMAC SHA1 calculation.
Crypto::hmacSha1Ctx = HMAC_CTX_new();
}
RTC::DtlsTransport::ClassInit();
位置mediasoup\worker\src\RTC\DtlsTransport.cpp
void DtlsTransport::ClassInit()
{
MS_TRACE();
// Generate a X509 certificate and private key (unless PEM files are provided).
if (
Settings::configuration.dtlsCertificateFile.empty() ||
Settings::configuration.dtlsPrivateKeyFile.empty())
{
GenerateCertificateAndPrivateKey();
}
else
{
ReadCertificateAndPrivateKeyFromFiles();
}
// Create a global SSL_CTX.
CreateSslCtx();
// Generate certificate fingerprints.
GenerateFingerprints();
}
RTC::SrtpSession::ClassInit();
位置:mediasoup\worker\src\RTC\SrtpSession.cpp
void SrtpSession::ClassInit()
{
// Set libsrtp event handler.
srtp_err_status_t err =
srtp_install_event_handler(static_cast<srtp_event_handler_func_t*>(OnSrtpEvent));
if (DepLibSRTP::IsError(err))
{
MS_THROW_ERROR("srtp_install_event_handler() failed: %s", DepLibSRTP::GetErrorString(err));
}
}
Channel::ChannelNotifier::ClassInit(channel.get());
位置:mediasoup\worker\src\Channel\ChannelNotifier.cpp
void ChannelNotifier::ClassInit(Channel::ChannelSocket* channel)
{
MS_TRACE();
ChannelNotifier::channel = channel;
}
PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel.get());
位置:mediasoup\worker\src\PayloadChannel\PayloadChannelNotifier.cpp
void PayloadChannelNotifier::ClassInit(PayloadChannel::PayloadChannelSocket* payloadChannel)
{
MS_TRACE();
PayloadChannelNotifier::payloadChannel = payloadChannel;
}
3)传输处理申请
void Transport::HandleRequest(Channel::ChannelRequest* request)