tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nricemediastream.h (8203B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 // Original author: ekr@rtfm.com
      8 
      9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
     10 
     11 /*
     12 Copyright (c) 2007, Adobe Systems, Incorporated
     13 All rights reserved.
     14 
     15 Redistribution and use in source and binary forms, with or without
     16 modification, are permitted provided that the following conditions are
     17 met:
     18 
     19 * Redistributions of source code must retain the above copyright
     20  notice, this list of conditions and the following disclaimer.
     21 
     22 * Redistributions in binary form must reproduce the above copyright
     23  notice, this list of conditions and the following disclaimer in the
     24  documentation and/or other materials provided with the distribution.
     25 
     26 * Neither the name of Adobe Systems, Network Resonance nor the names of its
     27  contributors may be used to endorse or promote products derived from
     28  this software without specific prior written permission.
     29 
     30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41 */
     42 
     43 // This is a wrapper around the nICEr ICE stack
     44 #ifndef nricemediastream_h__
     45 #define nricemediastream_h__
     46 
     47 #include <string>
     48 #include <vector>
     49 
     50 #include "m_cpp_utils.h"
     51 #include "mozilla/RefPtr.h"
     52 #include "mozilla/UniquePtr.h"
     53 #include "nscore.h"
     54 #include "sigslot.h"
     55 
     56 namespace mozilla {
     57 
     58 typedef struct nr_ice_ctx_ nr_ice_ctx;
     59 typedef struct nr_ice_peer_ctx_ nr_ice_peer_ctx;
     60 typedef struct nr_ice_media_stream_ nr_ice_media_stream;
     61 
     62 class NrIceCtx;
     63 
     64 struct NrIceAddr {
     65  std::string host;
     66  uint16_t port;
     67  std::string transport;
     68 };
     69 
     70 /* A summary of a candidate, for use in asking which candidate
     71   pair is active */
     72 struct NrIceCandidate {
     73  enum Type { ICE_HOST, ICE_SERVER_REFLEXIVE, ICE_PEER_REFLEXIVE, ICE_RELAYED };
     74 
     75  enum TcpType { ICE_NONE, ICE_ACTIVE, ICE_PASSIVE, ICE_SO };
     76 
     77  NrIceAddr cand_addr;
     78  NrIceAddr local_addr;
     79  std::string mdns_addr;
     80  Type type;
     81  TcpType tcp_type;
     82  std::string codeword;
     83  std::string label;
     84  bool trickled;
     85  uint32_t priority;
     86  bool is_proxied = false;
     87 };
     88 
     89 struct NrIceCandidatePair {
     90  enum State {
     91    STATE_FROZEN,
     92    STATE_WAITING,
     93    STATE_IN_PROGRESS,
     94    STATE_FAILED,
     95    STATE_SUCCEEDED,
     96    STATE_CANCELLED
     97  };
     98 
     99  State state;
    100  uint64_t priority;
    101  // Set regardless of who nominated it. Does not necessarily mean that it is
    102  // ready to be selected (ie; nominated by peer, but our check has not
    103  // succeeded yet.) Note: since this implementation uses aggressive nomination,
    104  // when we are the controlling agent, this will always be set if the pair is
    105  // in STATE_SUCCEEDED.
    106  bool nominated;
    107  bool writable;
    108  bool readable;
    109  // Set if this candidate pair has been selected. Note: Since we are using
    110  // aggressive nomination, this could change frequently as ICE runs.
    111  bool selected;
    112  NrIceCandidate local;
    113  NrIceCandidate remote;
    114  // TODO(bcampen@mozilla.com): Is it important to put the foundation in here?
    115  std::string codeword;
    116  uint64_t component_id;
    117 
    118  // for RTCIceCandidatePairStats
    119  uint64_t bytes_sent;
    120  uint64_t bytes_recvd;
    121  uint64_t ms_since_last_send;
    122  uint64_t ms_since_last_recv;
    123  uint64_t responses_recvd;
    124  uint64_t current_rtt_ms;
    125  uint64_t total_rtt_ms;
    126 };
    127 
    128 class NrIceMediaStream {
    129 public:
    130  enum GatheringState {
    131    ICE_STREAM_GATHER_INIT,
    132    ICE_STREAM_GATHER_STARTED,
    133    ICE_STREAM_GATHER_COMPLETE
    134  };
    135 
    136  NrIceMediaStream(NrIceCtx* ctx, const std::string& id,
    137                   const std::string& name, size_t components);
    138 
    139  nsresult SetIceCredentials(const std::string& ufrag, const std::string& pwd);
    140  nsresult ConnectToPeer(const std::string& ufrag, const std::string& pwd,
    141                         const std::vector<std::string>& peer_attrs);
    142  enum State { ICE_CONNECTING, ICE_OPEN, ICE_CLOSED };
    143 
    144  State state() const { return state_; }
    145 
    146  // The name of the stream
    147  const std::string& name() const { return name_; }
    148 
    149  // Get all the ICE attributes; used for testing
    150  std::vector<std::string> GetAttributes() const;
    151 
    152  nsresult GetLocalCandidates(std::vector<NrIceCandidate>* candidates) const;
    153  nsresult GetRemoteCandidates(std::vector<NrIceCandidate>* candidates) const;
    154 
    155  // Get all candidate pairs, whether in the check list or triggered check
    156  // queue, in priority order. |out_pairs| is cleared before being filled.
    157  nsresult GetCandidatePairs(std::vector<NrIceCandidatePair>* out_pairs) const;
    158 
    159  nsresult GetDefaultCandidate(int component, NrIceCandidate* candidate) const;
    160 
    161  // Parse trickle ICE candidate
    162  nsresult ParseTrickleCandidate(const std::string& candidate,
    163                                 const std::string& ufrag,
    164                                 const std::string& mdns_addr);
    165 
    166  // Disable a component
    167  nsresult DisableComponent(int component);
    168 
    169  // Get the candidate pair currently active. It's the
    170  // caller's responsibility to free these.
    171  nsresult GetActivePair(int component, UniquePtr<NrIceCandidate>* local,
    172                         UniquePtr<NrIceCandidate>* remote);
    173 
    174  // Get the current ICE consent send status plus the timeval of the last
    175  // consent update time.
    176  nsresult GetConsentStatus(int component, bool* can_send, struct timeval* ts);
    177 
    178  // The number of components
    179  size_t components() const { return components_; }
    180 
    181  bool HasStream(nr_ice_media_stream* stream) const;
    182  // Signals to indicate events. API users can (and should)
    183  // register for these.
    184 
    185  // Send a packet
    186  nsresult SendPacket(int component_id, const unsigned char* data, size_t len);
    187 
    188  // Set your state to ready. Called by the NrIceCtx;
    189  void Ready(nr_ice_media_stream* stream);
    190  void Failed();
    191 
    192  void OnGatheringStarted(nr_ice_media_stream* stream);
    193  void OnGatheringComplete(nr_ice_media_stream* stream);
    194 
    195  // Close the stream. Called by the NrIceCtx.
    196  // Different from the destructor because other people
    197  // might be holding RefPtrs but we want those writes to fail once
    198  // the context has been destroyed.
    199  void Close();
    200 
    201  // So the receiver of SignalCandidate can determine which transport
    202  // the candidate belongs to.
    203  const std::string& GetId() const { return id_; }
    204 
    205  bool AllGenerationsDoneGathering() const;
    206  bool AnyGenerationIsConnected() const;
    207 
    208  sigslot::signal5<NrIceMediaStream*, const std::string&, const std::string&,
    209                   const std::string&, const std::string&>
    210      SignalCandidate;  // A new ICE candidate:
    211  sigslot::signal2<const std::string&, NrIceMediaStream::GatheringState>
    212      SignalGatheringStateChange;
    213 
    214  sigslot::signal1<NrIceMediaStream*> SignalReady;   // Candidate pair ready.
    215  sigslot::signal1<NrIceMediaStream*> SignalFailed;  // Candidate pair failed.
    216  sigslot::signal4<NrIceMediaStream*, int, const unsigned char*, int>
    217      SignalPacketReceived;  // Incoming packet
    218 
    219  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceMediaStream)
    220 
    221 private:
    222  ~NrIceMediaStream();
    223 
    224  DISALLOW_COPY_ASSIGN(NrIceMediaStream);
    225 
    226  void CloseStream(nr_ice_media_stream** stream);
    227  void DeferredCloseOldStream(const nr_ice_media_stream* old);
    228  nr_ice_media_stream* GetStreamForRemoteUfrag(const std::string& ufrag);
    229 
    230  State state_;
    231  RefPtr<NrIceCtx> ctx_;
    232  const std::string name_;
    233  const size_t components_;
    234  nr_ice_media_stream* stream_;
    235  nr_ice_media_stream* old_stream_;
    236  const std::string id_;
    237 };
    238 
    239 }  // namespace mozilla
    240 #endif