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