Line data Source code
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 "sigslot.h"
51 :
52 : #include "mozilla/RefPtr.h"
53 : #include "mozilla/UniquePtr.h"
54 : #include "nsCOMPtr.h"
55 : #include "nsIEventTarget.h"
56 : #include "nsITimer.h"
57 :
58 : #include "m_cpp_utils.h"
59 :
60 :
61 : namespace mozilla {
62 :
63 : typedef struct nr_ice_ctx_ nr_ice_ctx;
64 : typedef struct nr_ice_peer_ctx_ nr_ice_peer_ctx;
65 : typedef struct nr_ice_media_stream_ nr_ice_media_stream;
66 :
67 : class NrIceCtx;
68 :
69 0 : struct NrIceAddr {
70 : std::string host;
71 : uint16_t port;
72 : std::string transport;
73 : };
74 :
75 : /* A summary of a candidate, for use in asking which candidate
76 : pair is active */
77 0 : struct NrIceCandidate {
78 : enum Type {
79 : ICE_HOST,
80 : ICE_SERVER_REFLEXIVE,
81 : ICE_PEER_REFLEXIVE,
82 : ICE_RELAYED
83 : };
84 :
85 : enum TcpType {
86 : ICE_NONE,
87 : ICE_ACTIVE,
88 : ICE_PASSIVE,
89 : ICE_SO
90 : };
91 :
92 : NrIceAddr cand_addr;
93 : NrIceAddr local_addr;
94 : Type type;
95 : TcpType tcp_type;
96 : std::string codeword;
97 : };
98 :
99 0 : struct NrIceCandidatePair {
100 :
101 : enum State {
102 : STATE_FROZEN,
103 : STATE_WAITING,
104 : STATE_IN_PROGRESS,
105 : STATE_FAILED,
106 : STATE_SUCCEEDED,
107 : STATE_CANCELLED
108 : };
109 :
110 : State state;
111 : uint64_t priority;
112 : // Set regardless of who nominated it. Does not necessarily mean that it is
113 : // ready to be selected (ie; nominated by peer, but our check has not
114 : // succeeded yet.) Note: since this implementation uses aggressive nomination,
115 : // when we are the controlling agent, this will always be set if the pair is
116 : // in STATE_SUCCEEDED.
117 : bool nominated;
118 : bool writable;
119 : bool readable;
120 : // Set if this candidate pair has been selected. Note: Since we are using
121 : // aggressive nomination, this could change frequently as ICE runs.
122 : bool selected;
123 : NrIceCandidate local;
124 : NrIceCandidate remote;
125 : // TODO(bcampen@mozilla.com): Is it important to put the foundation in here?
126 : std::string codeword;
127 :
128 : // for RTCIceCandidatePairStats
129 : uint64_t bytes_sent;
130 : uint64_t bytes_recvd;
131 : uint64_t ms_since_last_send;
132 : uint64_t ms_since_last_recv;
133 : };
134 :
135 : class NrIceMediaStream {
136 : public:
137 : static RefPtr<NrIceMediaStream> Create(NrIceCtx *ctx,
138 : const std::string& name,
139 : int components);
140 : enum State { ICE_CONNECTING, ICE_OPEN, ICE_CLOSED};
141 :
142 0 : State state() const { return state_; }
143 :
144 : // The name of the stream
145 0 : const std::string& name() const { return name_; }
146 :
147 : // Get all the candidates
148 : std::vector<std::string> GetCandidates() const;
149 :
150 : nsresult GetLocalCandidates(std::vector<NrIceCandidate>* candidates) const;
151 : nsresult GetRemoteCandidates(std::vector<NrIceCandidate>* candidates) const;
152 :
153 : // Get all candidate pairs, whether in the check list or triggered check
154 : // queue, in priority order. |out_pairs| is cleared before being filled.
155 : nsresult GetCandidatePairs(std::vector<NrIceCandidatePair>* out_pairs) const;
156 :
157 : nsresult GetDefaultCandidate(int component, NrIceCandidate* candidate) const;
158 :
159 : // Parse remote attributes
160 : nsresult ParseAttributes(std::vector<std::string>& candidates);
161 0 : bool HasParsedAttributes() const { return has_parsed_attrs_; }
162 :
163 : // Parse trickle ICE candidate
164 : nsresult ParseTrickleCandidate(const std::string& candidate);
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,
172 : UniquePtr<NrIceCandidate>* local,
173 : UniquePtr<NrIceCandidate>* remote);
174 :
175 : // Get the current ICE consent send status plus the timeval of the last
176 : // consent update time.
177 : nsresult GetConsentStatus(int component, bool *can_send, struct timeval *ts);
178 :
179 : // The number of components
180 0 : size_t components() const { return components_; }
181 :
182 : // The underlying nICEr stream
183 0 : nr_ice_media_stream *stream() { return stream_; }
184 : // Signals to indicate events. API users can (and should)
185 : // register for these.
186 :
187 : // Send a packet
188 : nsresult SendPacket(int component_id, const unsigned char *data, size_t len);
189 :
190 : // Set your state to ready. Called by the NrIceCtx;
191 : void Ready();
192 :
193 : // Close the stream. Called by the NrIceCtx.
194 : // Different from the destructor because other people
195 : // might be holding RefPtrs but we want those writes to fail once
196 : // the context has been destroyed.
197 : void Close();
198 :
199 : // So the receiver of SignalCandidate can determine which level
200 : // (ie; m-line index) the candidate belongs to.
201 0 : void SetLevel(uint16_t level) { level_ = level; }
202 :
203 0 : uint16_t GetLevel() const { return level_; }
204 :
205 : sigslot::signal2<NrIceMediaStream *, const std::string& >
206 : SignalCandidate; // A new ICE candidate:
207 :
208 : sigslot::signal1<NrIceMediaStream *> SignalReady; // Candidate pair ready.
209 : sigslot::signal1<NrIceMediaStream *> SignalFailed; // Candidate pair failed.
210 : sigslot::signal4<NrIceMediaStream *, int, const unsigned char *, int>
211 : SignalPacketReceived; // Incoming packet
212 :
213 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceMediaStream)
214 :
215 : private:
216 : NrIceMediaStream(NrIceCtx *ctx,
217 : const std::string& name,
218 : size_t components);
219 :
220 : ~NrIceMediaStream();
221 :
222 : DISALLOW_COPY_ASSIGN(NrIceMediaStream);
223 :
224 : State state_;
225 : nr_ice_ctx *ctx_;
226 : nr_ice_peer_ctx *ctx_peer_;
227 : const std::string name_;
228 : const size_t components_;
229 : nr_ice_media_stream *stream_;
230 : uint16_t level_;
231 : bool has_parsed_attrs_;
232 : };
233 :
234 :
235 : } // close namespace
236 : #endif
|