1)基础知识回顾

图片.png

图片.png

2)各模块初始化

图片.png
新版本在mediasoup\worker\src\lib.cpp

  1. extern "C" int run_worker(
  2. int argc,
  3. char* argv[],
  4. const char* version,
  5. int consumerChannelFd,
  6. int producerChannelFd,
  7. int payloadConsumeChannelFd,
  8. int payloadProduceChannelFd)
  9. {
  10. // Initialize libuv stuff (we need it for the Channel).
  11. DepLibUV::ClassInit();
  12. // Channel socket. If Worker instance runs properly, this socket is closed by
  13. // it in its destructor. Otherwise it's closed here by also letting libuv
  14. // deallocate its UV handles.
  15. std::unique_ptr<Channel::ChannelSocket> channel{ nullptr };
  16. // PayloadChannel socket. If Worker instance runs properly, this socket is
  17. // closed by it in its destructor. Otherwise it's closed here by also letting
  18. // libuv deallocate its UV handles.
  19. std::unique_ptr<PayloadChannel::PayloadChannelSocket> payloadChannel{ nullptr };
  20. try
  21. {
  22. channel.reset(new Channel::ChannelSocket(consumerChannelFd, producerChannelFd));
  23. }
  24. catch (const MediaSoupError& error)
  25. {
  26. MS_ERROR_STD("error creating the Channel: %s", error.what());
  27. DepLibUV::RunLoop();
  28. DepLibUV::ClassDestroy();
  29. return 1;
  30. }
  31. try
  32. {
  33. payloadChannel.reset(
  34. new PayloadChannel::PayloadChannelSocket(payloadConsumeChannelFd, payloadProduceChannelFd));
  35. }
  36. catch (const MediaSoupError& error)
  37. {
  38. MS_ERROR_STD("error creating the RTC Channel: %s", error.what());
  39. channel->Close();
  40. DepLibUV::RunLoop();
  41. DepLibUV::ClassDestroy();
  42. return 1;
  43. }
  44. // Initialize the Logger.
  45. Logger::ClassInit(channel.get());
  46. try
  47. {
  48. Settings::SetConfiguration(argc, argv);
  49. }
  50. catch (const MediaSoupTypeError& error)
  51. {
  52. MS_ERROR_STD("settings error: %s", error.what());
  53. channel->Close();
  54. payloadChannel->Close();
  55. DepLibUV::RunLoop();
  56. DepLibUV::ClassDestroy();
  57. // 42 is a custom exit code to notify "settings error" to the Node library.
  58. return 42;
  59. }
  60. catch (const MediaSoupError& error)
  61. {
  62. MS_ERROR_STD("unexpected settings error: %s", error.what());
  63. channel->Close();
  64. payloadChannel->Close();
  65. DepLibUV::RunLoop();
  66. DepLibUV::ClassDestroy();
  67. return 1;
  68. }
  69. MS_DEBUG_TAG(info, "starting mediasoup-worker process [version:%s]", version);
  70. #if defined(MS_LITTLE_ENDIAN)
  71. MS_DEBUG_TAG(info, "little-endian CPU detected");
  72. #elif defined(MS_BIG_ENDIAN)
  73. MS_DEBUG_TAG(info, "big-endian CPU detected");
  74. #else
  75. MS_WARN_TAG(info, "cannot determine whether little-endian or big-endian");
  76. #endif
  77. #if defined(INTPTR_MAX) && defined(INT32_MAX) && (INTPTR_MAX == INT32_MAX)
  78. MS_DEBUG_TAG(info, "32 bits architecture detected");
  79. #elif defined(INTPTR_MAX) && defined(INT64_MAX) && (INTPTR_MAX == INT64_MAX)
  80. MS_DEBUG_TAG(info, "64 bits architecture detected");
  81. #else
  82. MS_WARN_TAG(info, "cannot determine 32 or 64 bits architecture");
  83. #endif
  84. Settings::PrintConfiguration();
  85. DepLibUV::PrintVersion();
  86. try
  87. {
  88. // Initialize static stuff.
  89. DepOpenSSL::ClassInit();
  90. DepLibSRTP::ClassInit();
  91. DepUsrSCTP::ClassInit();
  92. DepLibWebRTC::ClassInit();
  93. Utils::Crypto::ClassInit();
  94. RTC::DtlsTransport::ClassInit();
  95. RTC::SrtpSession::ClassInit();
  96. Channel::ChannelNotifier::ClassInit(channel.get());
  97. PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel.get());
  98. #ifdef MS_EXECUTABLE
  99. // Ignore some signals.
  100. IgnoreSignals();
  101. #endif
  102. // Run the Worker.
  103. Worker worker(channel.get(), payloadChannel.get());
  104. // Free static stuff.
  105. DepLibSRTP::ClassDestroy();
  106. Utils::Crypto::ClassDestroy();
  107. DepLibWebRTC::ClassDestroy();
  108. RTC::DtlsTransport::ClassDestroy();
  109. DepUsrSCTP::ClassDestroy();
  110. DepLibUV::ClassDestroy();
  111. #ifdef MS_EXECUTABLE
  112. // Wait a bit so pending messages to stdout/Channel arrive to the Node
  113. // process.
  114. uv_sleep(200);
  115. #endif
  116. return 0;
  117. }
  118. catch (const MediaSoupError& error)
  119. {
  120. MS_ERROR_STD("failure exit: %s", error.what());
  121. return 1;
  122. }
  123. }

讲解各个初始化的函数

DepOpenSSL::ClassInit();

位置mediasoup\worker\src\DepOpenSSL.cpp

  1. void DepOpenSSL::ClassInit()
  2. {
  3. MS_TRACE();
  4. std::call_once(globalInitOnce, [] {
  5. MS_DEBUG_TAG(info, "openssl version: \"%s\"", OpenSSL_version(OPENSSL_VERSION));
  6. // Initialize some crypto stuff.
  7. RAND_poll();
  8. });
  9. }

DepLibSRTP::ClassInit();

位置mediasoup\worker\src\DepLibSRTP.cpp

  1. void DepLibSRTP::ClassInit()
  2. {
  3. MS_TRACE();
  4. {
  5. std::lock_guard<std::mutex> lock(globalSyncMutex);
  6. if (globalInstances == 0)
  7. {
  8. MS_DEBUG_TAG(info, "libsrtp version: \"%s\"", srtp_get_version_string());
  9. srtp_err_status_t err = srtp_init();
  10. if (DepLibSRTP::IsError(err))
  11. MS_THROW_ERROR("srtp_init() failed: %s", DepLibSRTP::GetErrorString(err));
  12. }
  13. ++globalInstances;
  14. }
  15. }

DepUsrSCTP::ClassInit();

位置:mediasoup\worker\src\DepUsrSCTP.cpp

  1. void DepUsrSCTP::ClassInit()
  2. {
  3. MS_TRACE();
  4. MS_DEBUG_TAG(info, "usrsctp");
  5. {
  6. std::lock_guard<std::mutex> lock(globalSyncMutex);
  7. if (globalInstances == 0)
  8. {
  9. usrsctp_init_nothreads(0, onSendSctpData, sctpDebug);
  10. // Disable explicit congestion notifications (ecn).
  11. usrsctp_sysctl_set_sctp_ecn_enable(0);
  12. #ifdef SCTP_DEBUG
  13. usrsctp_sysctl_set_sctp_debug_on(SCTP_DEBUG_ALL);
  14. #endif
  15. }
  16. ++globalInstances;
  17. }
  18. }

DepLibWebRTC::ClassInit();

位置:mediasoup\worker\src\DepLibWebRTC.cpp

  1. void DepLibWebRTC::ClassInit()
  2. {
  3. MS_TRACE();
  4. std::call_once(globalInitOnce, [] { webrtc::field_trial::InitFieldTrialsFromString(FieldTrials); });
  5. }

Utils::Crypto::ClassInit();

位置:mediasoup\worker\src\Utils\Crypto.cpp

  1. void Crypto::ClassInit()
  2. {
  3. MS_TRACE();
  4. // Init the vrypto seed with a random number taken from the address
  5. // of the seed variable itself (which is random).
  6. Crypto::seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(std::addressof(Crypto::seed)));
  7. // Create an OpenSSL HMAC_CTX context for HMAC SHA1 calculation.
  8. Crypto::hmacSha1Ctx = HMAC_CTX_new();
  9. }

RTC::DtlsTransport::ClassInit();

位置mediasoup\worker\src\RTC\DtlsTransport.cpp

  1. void DtlsTransport::ClassInit()
  2. {
  3. MS_TRACE();
  4. // Generate a X509 certificate and private key (unless PEM files are provided).
  5. if (
  6. Settings::configuration.dtlsCertificateFile.empty() ||
  7. Settings::configuration.dtlsPrivateKeyFile.empty())
  8. {
  9. GenerateCertificateAndPrivateKey();
  10. }
  11. else
  12. {
  13. ReadCertificateAndPrivateKeyFromFiles();
  14. }
  15. // Create a global SSL_CTX.
  16. CreateSslCtx();
  17. // Generate certificate fingerprints.
  18. GenerateFingerprints();
  19. }

RTC::SrtpSession::ClassInit();

位置:mediasoup\worker\src\RTC\SrtpSession.cpp

  1. void SrtpSession::ClassInit()
  2. {
  3. // Set libsrtp event handler.
  4. srtp_err_status_t err =
  5. srtp_install_event_handler(static_cast<srtp_event_handler_func_t*>(OnSrtpEvent));
  6. if (DepLibSRTP::IsError(err))
  7. {
  8. MS_THROW_ERROR("srtp_install_event_handler() failed: %s", DepLibSRTP::GetErrorString(err));
  9. }
  10. }

Channel::ChannelNotifier::ClassInit(channel.get());

位置:mediasoup\worker\src\Channel\ChannelNotifier.cpp

  1. void ChannelNotifier::ClassInit(Channel::ChannelSocket* channel)
  2. {
  3. MS_TRACE();
  4. ChannelNotifier::channel = channel;
  5. }

PayloadChannel::PayloadChannelNotifier::ClassInit(payloadChannel.get());

位置:mediasoup\worker\src\PayloadChannel\PayloadChannelNotifier.cpp

  1. void PayloadChannelNotifier::ClassInit(PayloadChannel::PayloadChannelSocket* payloadChannel)
  2. {
  3. MS_TRACE();
  4. PayloadChannelNotifier::payloadChannel = payloadChannel;
  5. }

3)传输处理申请
void Transport::HandleRequest(Channel::ChannelRequest* request)