media/mtransport/nricemediastream.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/nricemediastream.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// Original author: ekr@rtfm.com
    1.11 +
    1.12 +// Some of this code is cut-and-pasted from nICEr. Copyright is:
    1.13 +
    1.14 +/*
    1.15 +Copyright (c) 2007, Adobe Systems, Incorporated
    1.16 +All rights reserved.
    1.17 +
    1.18 +Redistribution and use in source and binary forms, with or without
    1.19 +modification, are permitted provided that the following conditions are
    1.20 +met:
    1.21 +
    1.22 +* Redistributions of source code must retain the above copyright
    1.23 +  notice, this list of conditions and the following disclaimer.
    1.24 +
    1.25 +* Redistributions in binary form must reproduce the above copyright
    1.26 +  notice, this list of conditions and the following disclaimer in the
    1.27 +  documentation and/or other materials provided with the distribution.
    1.28 +
    1.29 +* Neither the name of Adobe Systems, Network Resonance nor the names of its
    1.30 +  contributors may be used to endorse or promote products derived from
    1.31 +  this software without specific prior written permission.
    1.32 +
    1.33 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.34 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.35 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.36 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.37 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.38 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.39 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.40 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.41 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.42 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.43 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.44 +*/
    1.45 +
    1.46 +// This is a wrapper around the nICEr ICE stack
    1.47 +#ifndef nricemediastream_h__
    1.48 +#define nricemediastream_h__
    1.49 +
    1.50 +#include <string>
    1.51 +#include <vector>
    1.52 +
    1.53 +#include "sigslot.h"
    1.54 +
    1.55 +#include "mozilla/RefPtr.h"
    1.56 +#include "mozilla/Scoped.h"
    1.57 +#include "nsCOMPtr.h"
    1.58 +#include "nsIEventTarget.h"
    1.59 +#include "nsITimer.h"
    1.60 +
    1.61 +#include "m_cpp_utils.h"
    1.62 +
    1.63 +
    1.64 +namespace mozilla {
    1.65 +
    1.66 +typedef struct nr_ice_media_stream_ nr_ice_media_stream;
    1.67 +
    1.68 +class NrIceCtx;
    1.69 +
    1.70 +struct NrIceAddr {
    1.71 +  std::string host;
    1.72 +  uint16_t port;
    1.73 +  std::string transport;
    1.74 +};
    1.75 +
    1.76 +/* A summary of a candidate, for use in asking which candidate
    1.77 +   pair is active */
    1.78 +struct NrIceCandidate {
    1.79 +  enum Type {
    1.80 +    ICE_HOST,
    1.81 +    ICE_SERVER_REFLEXIVE,
    1.82 +    ICE_PEER_REFLEXIVE,
    1.83 +    ICE_RELAYED
    1.84 +  };
    1.85 +
    1.86 +  NrIceAddr cand_addr;
    1.87 +  NrIceAddr local_addr;
    1.88 +  Type type;
    1.89 +  std::string codeword;
    1.90 +};
    1.91 +
    1.92 +struct NrIceCandidatePair {
    1.93 +
    1.94 +  enum State {
    1.95 +    STATE_FROZEN,
    1.96 +    STATE_WAITING,
    1.97 +    STATE_IN_PROGRESS,
    1.98 +    STATE_FAILED,
    1.99 +    STATE_SUCCEEDED,
   1.100 +    STATE_CANCELLED
   1.101 +  };
   1.102 +
   1.103 +  State state;
   1.104 +  uint64_t priority;
   1.105 +  // Set regardless of who nominated it. Does not necessarily mean that it is
   1.106 +  // ready to be selected (ie; nominated by peer, but our check has not
   1.107 +  // succeeded yet.) Note: since this implementation uses aggressive nomination,
   1.108 +  // when we are the controlling agent, this will always be set if the pair is
   1.109 +  // in STATE_SUCCEEDED.
   1.110 +  bool nominated;
   1.111 +  // Set if this candidate pair has been selected. Note: Since we are using
   1.112 +  // aggressive nomination, this could change frequently as ICE runs.
   1.113 +  bool selected;
   1.114 +  NrIceCandidate local;
   1.115 +  NrIceCandidate remote;
   1.116 +  // TODO(bcampen@mozilla.com): Is it important to put the foundation in here?
   1.117 +  std::string codeword;
   1.118 +};
   1.119 +
   1.120 +// Abstract base class for opaque values.
   1.121 +class NrIceOpaque {
   1.122 + public:
   1.123 +  virtual ~NrIceOpaque() {}
   1.124 +};
   1.125 +
   1.126 +class NrIceMediaStream {
   1.127 + public:
   1.128 +  static RefPtr<NrIceMediaStream> Create(NrIceCtx *ctx,
   1.129 +                                         const std::string& name,
   1.130 +                                         int components);
   1.131 +  ~NrIceMediaStream();
   1.132 +
   1.133 +  enum State { ICE_CONNECTING, ICE_OPEN, ICE_CLOSED};
   1.134 +
   1.135 +  State state() const { return state_; }
   1.136 +
   1.137 +  // The name of the stream
   1.138 +  const std::string& name() const { return name_; }
   1.139 +
   1.140 +  // Get all the candidates
   1.141 +  std::vector<std::string> GetCandidates() const;
   1.142 +
   1.143 +  nsresult GetLocalCandidates(std::vector<NrIceCandidate>* candidates) const;
   1.144 +  nsresult GetRemoteCandidates(std::vector<NrIceCandidate>* candidates) const;
   1.145 +
   1.146 +  // Get all candidate pairs, whether in the check list or triggered check
   1.147 +  // queue, in priority order. |out_pairs| is cleared before being filled.
   1.148 +  nsresult GetCandidatePairs(std::vector<NrIceCandidatePair>* out_pairs) const;
   1.149 +
   1.150 +  // Get the default candidate as host and port
   1.151 +  nsresult GetDefaultCandidate(int component, std::string *host, int *port);
   1.152 +
   1.153 +  // Parse remote attributes
   1.154 +  nsresult ParseAttributes(std::vector<std::string>& candidates);
   1.155 +
   1.156 +  // Parse trickle ICE candidate
   1.157 +  nsresult ParseTrickleCandidate(const std::string& candidate);
   1.158 +
   1.159 +  // Disable a component
   1.160 +  nsresult DisableComponent(int component);
   1.161 +
   1.162 +  // Get the candidate pair currently active. It's the
   1.163 +  // caller's responsibility to free these.
   1.164 +  nsresult GetActivePair(int component,
   1.165 +                         NrIceCandidate** local, NrIceCandidate** remote);
   1.166 +
   1.167 +  // The number of components
   1.168 +  int components() const { return components_; }
   1.169 +
   1.170 +  // The underlying nICEr stream
   1.171 +  nr_ice_media_stream *stream() { return stream_; }
   1.172 +  // Signals to indicate events. API users can (and should)
   1.173 +  // register for these.
   1.174 +
   1.175 +  // Send a packet
   1.176 +  nsresult SendPacket(int component_id, const unsigned char *data, size_t len);
   1.177 +
   1.178 +  // Set your state to ready. Called by the NrIceCtx;
   1.179 +  void Ready();
   1.180 +
   1.181 +  // Close the stream. Called by the NrIceCtx.
   1.182 +  // Different from the destructor because other people
   1.183 +  // might be holding RefPtrs but we want those writes to fail once
   1.184 +  // the context has been destroyed.
   1.185 +  void Close();
   1.186 +
   1.187 +  // Set an opaque value. Owned by the media stream.
   1.188 +  void SetOpaque(NrIceOpaque *opaque) { opaque_ = opaque; }
   1.189 +
   1.190 +  // Get the opaque
   1.191 +  NrIceOpaque* opaque() const { return opaque_; }
   1.192 +
   1.193 +  sigslot::signal2<NrIceMediaStream *, const std::string& >
   1.194 +  SignalCandidate;  // A new ICE candidate:
   1.195 +  sigslot::signal1<NrIceMediaStream *> SignalReady;  // Candidate pair ready.
   1.196 +  sigslot::signal1<NrIceMediaStream *> SignalFailed;  // Candidate pair failed.
   1.197 +  sigslot::signal4<NrIceMediaStream *, int, const unsigned char *, int>
   1.198 +  SignalPacketReceived;  // Incoming packet
   1.199 +
   1.200 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceMediaStream)
   1.201 +
   1.202 + private:
   1.203 +  NrIceMediaStream(NrIceCtx *ctx,  const std::string& name,
   1.204 +                   int components) :
   1.205 +      state_(ICE_CONNECTING),
   1.206 +      ctx_(ctx),
   1.207 +      name_(name),
   1.208 +      components_(components),
   1.209 +      stream_(nullptr),
   1.210 +      opaque_(nullptr) {}
   1.211 +
   1.212 +  DISALLOW_COPY_ASSIGN(NrIceMediaStream);
   1.213 +
   1.214 +  State state_;
   1.215 +  NrIceCtx *ctx_;
   1.216 +  const std::string name_;
   1.217 +  const int components_;
   1.218 +  nr_ice_media_stream *stream_;
   1.219 +  ScopedDeletePtr<NrIceOpaque> opaque_;
   1.220 +};
   1.221 +
   1.222 +
   1.223 +}  // close namespace
   1.224 +#endif

mercurial