nricectxhandler.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <sstream>
  2. #include "logging.h"
  3. // nICEr includes
  4. extern "C" {
  5. #include "nr_api.h"
  6. #include "ice_ctx.h"
  7. }
  8. // Local includes
  9. #include "nricectxhandler.h"
  10. #include "nricemediastream.h"
  11. #include "nriceresolver.h"
  12. namespace mozilla {
  13. MOZ_MTLOG_MODULE("mtransport")
  14. NrIceCtxHandler::NrIceCtxHandler(const std::string& name,
  15. bool offerer,
  16. NrIceCtx::Policy policy)
  17. : current_ctx(new NrIceCtx(name, offerer, policy)),
  18. old_ctx(nullptr),
  19. restart_count(0)
  20. {
  21. }
  22. RefPtr<NrIceCtxHandler>
  23. NrIceCtxHandler::Create(const std::string& name,
  24. bool offerer,
  25. bool allow_loopback,
  26. bool tcp_enabled,
  27. bool allow_link_local,
  28. NrIceCtx::Policy policy)
  29. {
  30. // InitializeGlobals only executes once
  31. NrIceCtx::InitializeGlobals(allow_loopback, tcp_enabled, allow_link_local);
  32. RefPtr<NrIceCtxHandler> ctx = new NrIceCtxHandler(name, offerer, policy);
  33. if (ctx == nullptr ||
  34. ctx->current_ctx == nullptr ||
  35. !ctx->current_ctx->Initialize()) {
  36. return nullptr;
  37. }
  38. return ctx;
  39. }
  40. RefPtr<NrIceMediaStream>
  41. NrIceCtxHandler::CreateStream(const std::string& name, int components)
  42. {
  43. // To make tracking NrIceMediaStreams easier during ICE restart
  44. // prepend an int to the name that increments with each ICE restart
  45. std::ostringstream os;
  46. os << restart_count << "-" << name;
  47. return NrIceMediaStream::Create(this->current_ctx, os.str(), components);
  48. }
  49. RefPtr<NrIceCtx>
  50. NrIceCtxHandler::CreateCtx() const
  51. {
  52. return CreateCtx(NrIceCtx::GetNewUfrag(), NrIceCtx::GetNewPwd());
  53. }
  54. RefPtr<NrIceCtx>
  55. NrIceCtxHandler::CreateCtx(const std::string& ufrag,
  56. const std::string& pwd) const
  57. {
  58. RefPtr<NrIceCtx> new_ctx = new NrIceCtx(this->current_ctx->name(),
  59. true, // offerer (hardcoded per bwc)
  60. this->current_ctx->policy());
  61. if (new_ctx == nullptr) {
  62. return nullptr;
  63. }
  64. if (!new_ctx->Initialize(ufrag, pwd)) {
  65. return nullptr;
  66. }
  67. // copy the stun, and turn servers from the current context
  68. int r = nr_ice_ctx_set_stun_servers(new_ctx->ctx_,
  69. this->current_ctx->ctx_->stun_servers,
  70. this->current_ctx->ctx_->stun_server_ct);
  71. if (r) {
  72. MOZ_MTLOG(ML_ERROR, "Error while setting STUN servers in CreateCtx"
  73. << " (likely ice restart related)");
  74. return nullptr;
  75. }
  76. r = nr_ice_ctx_copy_turn_servers(new_ctx->ctx_,
  77. this->current_ctx->ctx_->turn_servers,
  78. this->current_ctx->ctx_->turn_server_ct);
  79. if (r) {
  80. MOZ_MTLOG(ML_ERROR, "Error while copying TURN servers in CreateCtx"
  81. << " (likely ice restart related)");
  82. return nullptr;
  83. }
  84. // grab the NrIceResolver stashed in the nr_resolver and allocate another
  85. // for the new ctx. Note: there may not be an nr_resolver.
  86. if (this->current_ctx->ctx_->resolver) {
  87. NrIceResolver* resolver =
  88. static_cast<NrIceResolver*>(this->current_ctx->ctx_->resolver->obj);
  89. if (!resolver ||
  90. NS_FAILED(new_ctx->SetResolver(resolver->AllocateResolver()))) {
  91. MOZ_MTLOG(ML_ERROR, "Error while setting dns resolver in CreateCtx"
  92. << " (likely ice restart related)");
  93. return nullptr;
  94. }
  95. }
  96. return new_ctx;
  97. }
  98. bool
  99. NrIceCtxHandler::BeginIceRestart(RefPtr<NrIceCtx> new_ctx)
  100. {
  101. MOZ_ASSERT(!old_ctx, "existing ice restart in progress");
  102. if (old_ctx) {
  103. MOZ_MTLOG(ML_ERROR, "Existing ice restart in progress");
  104. return false; // ice restart already in progress
  105. }
  106. if (new_ctx == nullptr) {
  107. return false;
  108. }
  109. ++restart_count;
  110. old_ctx = current_ctx;
  111. current_ctx = new_ctx;
  112. return true;
  113. }
  114. void
  115. NrIceCtxHandler::FinalizeIceRestart()
  116. {
  117. // no harm calling this even if we're not in the middle of restarting
  118. old_ctx = nullptr;
  119. }
  120. void
  121. NrIceCtxHandler::RollbackIceRestart()
  122. {
  123. if (old_ctx == nullptr) {
  124. return;
  125. }
  126. current_ctx = old_ctx;
  127. old_ctx = nullptr;
  128. }
  129. } // close namespace