init.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Copyright (c) 2020-2022 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "init.hpp"
  9. #include "certificate.hpp"
  10. #include "dtlstransport.hpp"
  11. #include "icetransport.hpp"
  12. #include "internals.hpp"
  13. #include "pollservice.hpp"
  14. #include "sctptransport.hpp"
  15. #include "threadpool.hpp"
  16. #include "tls.hpp"
  17. #include "utils.hpp"
  18. #if RTC_ENABLE_WEBSOCKET
  19. #include "tlstransport.hpp"
  20. #endif
  21. #if RTC_ENABLE_MEDIA
  22. #include "dtlssrtptransport.hpp"
  23. #endif
  24. #ifdef _WIN32
  25. #include <winsock2.h>
  26. #endif
  27. #include <thread>
  28. namespace rtc::impl {
  29. struct Init::TokenPayload {
  30. TokenPayload(std::shared_future<void> *cleanupFuture) {
  31. Init::Instance().doInit();
  32. if (cleanupFuture)
  33. *cleanupFuture = cleanupPromise.get_future().share();
  34. }
  35. ~TokenPayload() {
  36. std::thread t(
  37. [](std::promise<void> promise) {
  38. utils::this_thread::set_name("RTC cleanup");
  39. try {
  40. Init::Instance().doCleanup();
  41. promise.set_value();
  42. } catch (const std::exception &e) {
  43. PLOG_WARNING << e.what();
  44. promise.set_exception(std::make_exception_ptr(e));
  45. }
  46. },
  47. std::move(cleanupPromise));
  48. t.detach();
  49. }
  50. std::promise<void> cleanupPromise;
  51. };
  52. Init &Init::Instance() {
  53. static Init *instance = new Init;
  54. return *instance;
  55. }
  56. Init::Init() {
  57. std::promise<void> p;
  58. p.set_value();
  59. mCleanupFuture = p.get_future(); // make it ready
  60. }
  61. Init::~Init() {}
  62. init_token Init::token() {
  63. std::lock_guard lock(mMutex);
  64. if (auto locked = mWeak.lock())
  65. return locked;
  66. mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
  67. mWeak = *mGlobal;
  68. return *mGlobal;
  69. }
  70. void Init::preload() {
  71. std::lock_guard lock(mMutex);
  72. if (!mGlobal) {
  73. mGlobal = std::make_shared<TokenPayload>(&mCleanupFuture);
  74. mWeak = *mGlobal;
  75. }
  76. }
  77. std::shared_future<void> Init::cleanup() {
  78. std::lock_guard lock(mMutex);
  79. mGlobal.reset();
  80. return mCleanupFuture;
  81. }
  82. void Init::setSctpSettings(SctpSettings s) {
  83. std::lock_guard lock(mMutex);
  84. if (mGlobal)
  85. SctpTransport::SetSettings(s);
  86. mCurrentSctpSettings = std::move(s); // store for next init
  87. }
  88. void Init::doInit() {
  89. // mMutex needs to be locked
  90. if (std::exchange(mInitialized, true))
  91. return;
  92. PLOG_DEBUG << "Global initialization";
  93. #ifdef _WIN32
  94. WSADATA wsaData;
  95. if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  96. throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
  97. #endif
  98. int concurrency = std::thread::hardware_concurrency();
  99. int count = std::max(concurrency, MIN_THREADPOOL_SIZE);
  100. PLOG_DEBUG << "Spawning " << count << " threads";
  101. ThreadPool::Instance().spawn(count);
  102. #if RTC_ENABLE_WEBSOCKET
  103. PollService::Instance().start();
  104. #endif
  105. #if USE_GNUTLS
  106. // Nothing to do
  107. #elif USE_MBEDTLS
  108. // Nothing to do
  109. #else
  110. openssl::init();
  111. #endif
  112. SctpTransport::Init();
  113. SctpTransport::SetSettings(mCurrentSctpSettings);
  114. DtlsTransport::Init();
  115. #if RTC_ENABLE_WEBSOCKET
  116. TlsTransport::Init();
  117. #endif
  118. #if RTC_ENABLE_MEDIA
  119. DtlsSrtpTransport::Init();
  120. #endif
  121. IceTransport::Init();
  122. }
  123. void Init::doCleanup() {
  124. std::lock_guard lock(mMutex);
  125. if (mGlobal)
  126. return;
  127. if (!std::exchange(mInitialized, false))
  128. return;
  129. PLOG_DEBUG << "Global cleanup";
  130. ThreadPool::Instance().join();
  131. ThreadPool::Instance().clear();
  132. #if RTC_ENABLE_WEBSOCKET
  133. PollService::Instance().join();
  134. #endif
  135. SctpTransport::Cleanup();
  136. DtlsTransport::Cleanup();
  137. #if RTC_ENABLE_WEBSOCKET
  138. TlsTransport::Cleanup();
  139. #endif
  140. #if RTC_ENABLE_MEDIA
  141. DtlsSrtpTransport::Cleanup();
  142. #endif
  143. IceTransport::Cleanup();
  144. #ifdef _WIN32
  145. WSACleanup();
  146. #endif
  147. }
  148. } // namespace rtc::impl