nricemediastream.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // This is a wrapper around the nICEr ICE stack
  34. #ifndef nricemediastream_h__
  35. #define nricemediastream_h__
  36. #include <string>
  37. #include <vector>
  38. #include "sigslot.h"
  39. #include "mozilla/RefPtr.h"
  40. #include "mozilla/UniquePtr.h"
  41. #include "nsCOMPtr.h"
  42. #include "nsIEventTarget.h"
  43. #include "nsITimer.h"
  44. #include "m_cpp_utils.h"
  45. namespace mozilla {
  46. typedef struct nr_ice_ctx_ nr_ice_ctx;
  47. typedef struct nr_ice_peer_ctx_ nr_ice_peer_ctx;
  48. typedef struct nr_ice_media_stream_ nr_ice_media_stream;
  49. class NrIceCtx;
  50. struct NrIceAddr {
  51. std::string host;
  52. uint16_t port;
  53. std::string transport;
  54. };
  55. /* A summary of a candidate, for use in asking which candidate
  56. pair is active */
  57. struct NrIceCandidate {
  58. enum Type {
  59. ICE_HOST,
  60. ICE_SERVER_REFLEXIVE,
  61. ICE_PEER_REFLEXIVE,
  62. ICE_RELAYED
  63. };
  64. enum TcpType {
  65. ICE_NONE,
  66. ICE_ACTIVE,
  67. ICE_PASSIVE,
  68. ICE_SO
  69. };
  70. NrIceAddr cand_addr;
  71. NrIceAddr local_addr;
  72. Type type;
  73. TcpType tcp_type;
  74. std::string codeword;
  75. };
  76. struct NrIceCandidatePair {
  77. enum State {
  78. STATE_FROZEN,
  79. STATE_WAITING,
  80. STATE_IN_PROGRESS,
  81. STATE_FAILED,
  82. STATE_SUCCEEDED,
  83. STATE_CANCELLED
  84. };
  85. State state;
  86. uint64_t priority;
  87. // Set regardless of who nominated it. Does not necessarily mean that it is
  88. // ready to be selected (ie; nominated by peer, but our check has not
  89. // succeeded yet.) Note: since this implementation uses aggressive nomination,
  90. // when we are the controlling agent, this will always be set if the pair is
  91. // in STATE_SUCCEEDED.
  92. bool nominated;
  93. // Set if this candidate pair has been selected. Note: Since we are using
  94. // aggressive nomination, this could change frequently as ICE runs.
  95. bool selected;
  96. NrIceCandidate local;
  97. NrIceCandidate remote;
  98. // TODO(bcampen@mozilla.com): Is it important to put the foundation in here?
  99. std::string codeword;
  100. };
  101. class NrIceMediaStream {
  102. public:
  103. static RefPtr<NrIceMediaStream> Create(NrIceCtx *ctx,
  104. const std::string& name,
  105. int components);
  106. enum State { ICE_CONNECTING, ICE_OPEN, ICE_CLOSED};
  107. State state() const { return state_; }
  108. // The name of the stream
  109. const std::string& name() const { return name_; }
  110. // Get all the candidates
  111. std::vector<std::string> GetCandidates() const;
  112. nsresult GetLocalCandidates(std::vector<NrIceCandidate>* candidates) const;
  113. nsresult GetRemoteCandidates(std::vector<NrIceCandidate>* candidates) const;
  114. // Get all candidate pairs, whether in the check list or triggered check
  115. // queue, in priority order. |out_pairs| is cleared before being filled.
  116. nsresult GetCandidatePairs(std::vector<NrIceCandidatePair>* out_pairs) const;
  117. nsresult GetDefaultCandidate(int component, NrIceCandidate* candidate) const;
  118. // Parse remote attributes
  119. nsresult ParseAttributes(std::vector<std::string>& candidates);
  120. bool HasParsedAttributes() const { return has_parsed_attrs_; }
  121. // Parse trickle ICE candidate
  122. nsresult ParseTrickleCandidate(const std::string& candidate);
  123. // Disable a component
  124. nsresult DisableComponent(int component);
  125. // Get the candidate pair currently active. It's the
  126. // caller's responsibility to free these.
  127. nsresult GetActivePair(int component,
  128. UniquePtr<NrIceCandidate>* local,
  129. UniquePtr<NrIceCandidate>* remote);
  130. // Get the current ICE consent send status plus the timeval of the last
  131. // consent update time.
  132. nsresult GetConsentStatus(int component, bool *can_send, struct timeval *ts);
  133. // The number of components
  134. size_t components() const { return components_; }
  135. // The underlying nICEr stream
  136. nr_ice_media_stream *stream() { return stream_; }
  137. // Signals to indicate events. API users can (and should)
  138. // register for these.
  139. // Send a packet
  140. nsresult SendPacket(int component_id, const unsigned char *data, size_t len);
  141. // Set your state to ready. Called by the NrIceCtx;
  142. void Ready();
  143. // Close the stream. Called by the NrIceCtx.
  144. // Different from the destructor because other people
  145. // might be holding RefPtrs but we want those writes to fail once
  146. // the context has been destroyed.
  147. void Close();
  148. // So the receiver of SignalCandidate can determine which level
  149. // (ie; m-line index) the candidate belongs to.
  150. void SetLevel(uint16_t level) { level_ = level; }
  151. uint16_t GetLevel() const { return level_; }
  152. sigslot::signal2<NrIceMediaStream *, const std::string& >
  153. SignalCandidate; // A new ICE candidate:
  154. sigslot::signal1<NrIceMediaStream *> SignalReady; // Candidate pair ready.
  155. sigslot::signal1<NrIceMediaStream *> SignalFailed; // Candidate pair failed.
  156. sigslot::signal4<NrIceMediaStream *, int, const unsigned char *, int>
  157. SignalPacketReceived; // Incoming packet
  158. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceMediaStream)
  159. private:
  160. NrIceMediaStream(NrIceCtx *ctx,
  161. const std::string& name,
  162. size_t components);
  163. ~NrIceMediaStream();
  164. DISALLOW_COPY_ASSIGN(NrIceMediaStream);
  165. State state_;
  166. nr_ice_ctx *ctx_;
  167. nr_ice_peer_ctx *ctx_peer_;
  168. const std::string name_;
  169. const size_t components_;
  170. nr_ice_media_stream *stream_;
  171. uint16_t level_;
  172. bool has_parsed_attrs_;
  173. };
  174. } // close namespace
  175. #endif