transportlayerice.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original author: ekr@rtfm.com
  6. // Some of this code is cut-and-pasted from nICEr. Copyright is:
  7. /*
  8. Copyright (c) 2007, Adobe Systems, Incorporated
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are
  12. met:
  13. * Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. * Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. * Neither the name of Adobe Systems, Network Resonance nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <string>
  34. #include <vector>
  35. #include "nsCOMPtr.h"
  36. #include "nsComponentManagerUtils.h"
  37. #include "nsError.h"
  38. #include "nsIEventTarget.h"
  39. #include "nsNetCID.h"
  40. #include "nsComponentManagerUtils.h"
  41. #include "nsServiceManagerUtils.h"
  42. // nICEr includes
  43. extern "C" {
  44. #include "nr_api.h"
  45. #include "registry.h"
  46. #include "async_timer.h"
  47. #include "ice_util.h"
  48. #include "transport_addr.h"
  49. #include "nr_crypto.h"
  50. #include "nr_socket.h"
  51. #include "nr_socket_local.h"
  52. #include "stun_client_ctx.h"
  53. #include "stun_server_ctx.h"
  54. #include "ice_ctx.h"
  55. #include "ice_candidate.h"
  56. #include "ice_handler.h"
  57. }
  58. // Local includes
  59. #include "logging.h"
  60. #include "nricectx.h"
  61. #include "nricemediastream.h"
  62. #include "transportflow.h"
  63. #include "transportlayerice.h"
  64. namespace mozilla {
  65. #ifdef ERROR
  66. #undef ERROR
  67. #endif
  68. MOZ_MTLOG_MODULE("mtransport")
  69. TransportLayerIce::TransportLayerIce(const std::string& name)
  70. : name_(name),
  71. ctx_(nullptr), stream_(nullptr), component_(0),
  72. old_stream_(nullptr)
  73. {
  74. // setup happens later
  75. }
  76. TransportLayerIce::~TransportLayerIce() {
  77. // No need to do anything here, since we use smart pointers
  78. }
  79. void TransportLayerIce::SetParameters(RefPtr<NrIceCtx> ctx,
  80. RefPtr<NrIceMediaStream> stream,
  81. int component) {
  82. // If SetParameters is called and we already have a stream_, this means
  83. // we're handling an ICE restart. We need to hold the old stream until
  84. // we know the new stream is working.
  85. if (stream_ && !old_stream_ && (stream_ != stream)) {
  86. // Here we leave the old stream's signals connected until we don't need
  87. // it anymore. They will be disconnected if ice restart is successful.
  88. old_stream_ = stream_;
  89. MOZ_MTLOG(ML_INFO, LAYER_INFO << "SetParameters save old stream("
  90. << old_stream_->name() << ")");
  91. }
  92. ctx_ = ctx;
  93. stream_ = stream;
  94. component_ = component;
  95. PostSetup();
  96. }
  97. void TransportLayerIce::PostSetup() {
  98. target_ = ctx_->thread();
  99. stream_->SignalReady.connect(this, &TransportLayerIce::IceReady);
  100. stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed);
  101. stream_->SignalPacketReceived.connect(this,
  102. &TransportLayerIce::IcePacketReceived);
  103. if (stream_->state() == NrIceMediaStream::ICE_OPEN) {
  104. TL_SET_STATE(TS_OPEN);
  105. }
  106. }
  107. void TransportLayerIce::ResetOldStream() {
  108. if (old_stream_ == nullptr) {
  109. return; // no work to do
  110. }
  111. // ICE restart successful on the new stream, we can forget the old stream now
  112. MOZ_MTLOG(ML_INFO, LAYER_INFO << "ResetOldStream(" << old_stream_->name()
  113. << ")");
  114. old_stream_->SignalReady.disconnect(this);
  115. old_stream_->SignalFailed.disconnect(this);
  116. old_stream_->SignalPacketReceived.disconnect(this);
  117. old_stream_ = nullptr;
  118. }
  119. void TransportLayerIce::RestoreOldStream() {
  120. if (old_stream_ == nullptr) {
  121. return; // no work to do
  122. }
  123. // ICE restart rollback, we need to restore the old stream
  124. MOZ_MTLOG(ML_INFO, LAYER_INFO << "RestoreOldStream(" << old_stream_->name()
  125. << ")");
  126. stream_->SignalReady.disconnect(this);
  127. stream_->SignalFailed.disconnect(this);
  128. stream_->SignalPacketReceived.disconnect(this);
  129. stream_ = old_stream_;
  130. old_stream_ = nullptr;
  131. if (stream_->state() == NrIceMediaStream::ICE_OPEN) {
  132. IceReady(stream_);
  133. } else if (stream_->state() == NrIceMediaStream::ICE_CLOSED) {
  134. IceFailed(stream_);
  135. }
  136. // No events are fired when the stream is ICE_CONNECTING. If the
  137. // restored stream is ICE_CONNECTING, IceReady/IceFailed will fire
  138. // later.
  139. }
  140. TransportResult TransportLayerIce::SendPacket(const unsigned char *data,
  141. size_t len) {
  142. CheckThread();
  143. // use old_stream_ until stream_ is ready
  144. nsresult res = (old_stream_?old_stream_:stream_)->SendPacket(component_,
  145. data,
  146. len);
  147. if (!NS_SUCCEEDED(res)) {
  148. return (res == NS_BASE_STREAM_WOULD_BLOCK) ?
  149. TE_WOULDBLOCK : TE_ERROR;
  150. }
  151. MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " SendPacket(" << len << ") succeeded");
  152. return len;
  153. }
  154. void TransportLayerIce::IceCandidate(NrIceMediaStream *stream,
  155. const std::string&) {
  156. // NO-OP for now
  157. }
  158. void TransportLayerIce::IceReady(NrIceMediaStream *stream) {
  159. CheckThread();
  160. // only handle the current stream (not the old stream during restart)
  161. if (stream != stream_) {
  162. return;
  163. }
  164. MOZ_MTLOG(ML_INFO, LAYER_INFO << "ICE Ready(" << stream->name() << ","
  165. << component_ << ")");
  166. TL_SET_STATE(TS_OPEN);
  167. }
  168. void TransportLayerIce::IceFailed(NrIceMediaStream *stream) {
  169. CheckThread();
  170. // only handle the current stream (not the old stream during restart)
  171. if (stream != stream_) {
  172. return;
  173. }
  174. MOZ_MTLOG(ML_INFO, LAYER_INFO << "ICE Failed(" << stream->name() << ","
  175. << component_ << ")");
  176. TL_SET_STATE(TS_ERROR);
  177. }
  178. void TransportLayerIce::IcePacketReceived(NrIceMediaStream *stream, int component,
  179. const unsigned char *data, int len) {
  180. CheckThread();
  181. // We get packets for both components, so ignore the ones that aren't
  182. // for us.
  183. if (component_ != component)
  184. return;
  185. MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << ","
  186. << component << "," << len << ")");
  187. SignalPacketReceived(this, data, len);
  188. }
  189. } // close namespace