|
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/Scoped.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_media_stream_ nr_ice_media_stream; |
|
64 |
|
65 class NrIceCtx; |
|
66 |
|
67 struct NrIceAddr { |
|
68 std::string host; |
|
69 uint16_t port; |
|
70 std::string transport; |
|
71 }; |
|
72 |
|
73 /* A summary of a candidate, for use in asking which candidate |
|
74 pair is active */ |
|
75 struct NrIceCandidate { |
|
76 enum Type { |
|
77 ICE_HOST, |
|
78 ICE_SERVER_REFLEXIVE, |
|
79 ICE_PEER_REFLEXIVE, |
|
80 ICE_RELAYED |
|
81 }; |
|
82 |
|
83 NrIceAddr cand_addr; |
|
84 NrIceAddr local_addr; |
|
85 Type type; |
|
86 std::string codeword; |
|
87 }; |
|
88 |
|
89 struct NrIceCandidatePair { |
|
90 |
|
91 enum State { |
|
92 STATE_FROZEN, |
|
93 STATE_WAITING, |
|
94 STATE_IN_PROGRESS, |
|
95 STATE_FAILED, |
|
96 STATE_SUCCEEDED, |
|
97 STATE_CANCELLED |
|
98 }; |
|
99 |
|
100 State state; |
|
101 uint64_t priority; |
|
102 // Set regardless of who nominated it. Does not necessarily mean that it is |
|
103 // ready to be selected (ie; nominated by peer, but our check has not |
|
104 // succeeded yet.) Note: since this implementation uses aggressive nomination, |
|
105 // when we are the controlling agent, this will always be set if the pair is |
|
106 // in STATE_SUCCEEDED. |
|
107 bool nominated; |
|
108 // Set if this candidate pair has been selected. Note: Since we are using |
|
109 // aggressive nomination, this could change frequently as ICE runs. |
|
110 bool selected; |
|
111 NrIceCandidate local; |
|
112 NrIceCandidate remote; |
|
113 // TODO(bcampen@mozilla.com): Is it important to put the foundation in here? |
|
114 std::string codeword; |
|
115 }; |
|
116 |
|
117 // Abstract base class for opaque values. |
|
118 class NrIceOpaque { |
|
119 public: |
|
120 virtual ~NrIceOpaque() {} |
|
121 }; |
|
122 |
|
123 class NrIceMediaStream { |
|
124 public: |
|
125 static RefPtr<NrIceMediaStream> Create(NrIceCtx *ctx, |
|
126 const std::string& name, |
|
127 int components); |
|
128 ~NrIceMediaStream(); |
|
129 |
|
130 enum State { ICE_CONNECTING, ICE_OPEN, ICE_CLOSED}; |
|
131 |
|
132 State state() const { return state_; } |
|
133 |
|
134 // The name of the stream |
|
135 const std::string& name() const { return name_; } |
|
136 |
|
137 // Get all the candidates |
|
138 std::vector<std::string> GetCandidates() const; |
|
139 |
|
140 nsresult GetLocalCandidates(std::vector<NrIceCandidate>* candidates) const; |
|
141 nsresult GetRemoteCandidates(std::vector<NrIceCandidate>* candidates) const; |
|
142 |
|
143 // Get all candidate pairs, whether in the check list or triggered check |
|
144 // queue, in priority order. |out_pairs| is cleared before being filled. |
|
145 nsresult GetCandidatePairs(std::vector<NrIceCandidatePair>* out_pairs) const; |
|
146 |
|
147 // Get the default candidate as host and port |
|
148 nsresult GetDefaultCandidate(int component, std::string *host, int *port); |
|
149 |
|
150 // Parse remote attributes |
|
151 nsresult ParseAttributes(std::vector<std::string>& candidates); |
|
152 |
|
153 // Parse trickle ICE candidate |
|
154 nsresult ParseTrickleCandidate(const std::string& candidate); |
|
155 |
|
156 // Disable a component |
|
157 nsresult DisableComponent(int component); |
|
158 |
|
159 // Get the candidate pair currently active. It's the |
|
160 // caller's responsibility to free these. |
|
161 nsresult GetActivePair(int component, |
|
162 NrIceCandidate** local, NrIceCandidate** remote); |
|
163 |
|
164 // The number of components |
|
165 int components() const { return components_; } |
|
166 |
|
167 // The underlying nICEr stream |
|
168 nr_ice_media_stream *stream() { return stream_; } |
|
169 // Signals to indicate events. API users can (and should) |
|
170 // register for these. |
|
171 |
|
172 // Send a packet |
|
173 nsresult SendPacket(int component_id, const unsigned char *data, size_t len); |
|
174 |
|
175 // Set your state to ready. Called by the NrIceCtx; |
|
176 void Ready(); |
|
177 |
|
178 // Close the stream. Called by the NrIceCtx. |
|
179 // Different from the destructor because other people |
|
180 // might be holding RefPtrs but we want those writes to fail once |
|
181 // the context has been destroyed. |
|
182 void Close(); |
|
183 |
|
184 // Set an opaque value. Owned by the media stream. |
|
185 void SetOpaque(NrIceOpaque *opaque) { opaque_ = opaque; } |
|
186 |
|
187 // Get the opaque |
|
188 NrIceOpaque* opaque() const { return opaque_; } |
|
189 |
|
190 sigslot::signal2<NrIceMediaStream *, const std::string& > |
|
191 SignalCandidate; // A new ICE candidate: |
|
192 sigslot::signal1<NrIceMediaStream *> SignalReady; // Candidate pair ready. |
|
193 sigslot::signal1<NrIceMediaStream *> SignalFailed; // Candidate pair failed. |
|
194 sigslot::signal4<NrIceMediaStream *, int, const unsigned char *, int> |
|
195 SignalPacketReceived; // Incoming packet |
|
196 |
|
197 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceMediaStream) |
|
198 |
|
199 private: |
|
200 NrIceMediaStream(NrIceCtx *ctx, const std::string& name, |
|
201 int components) : |
|
202 state_(ICE_CONNECTING), |
|
203 ctx_(ctx), |
|
204 name_(name), |
|
205 components_(components), |
|
206 stream_(nullptr), |
|
207 opaque_(nullptr) {} |
|
208 |
|
209 DISALLOW_COPY_ASSIGN(NrIceMediaStream); |
|
210 |
|
211 State state_; |
|
212 NrIceCtx *ctx_; |
|
213 const std::string name_; |
|
214 const int components_; |
|
215 nr_ice_media_stream *stream_; |
|
216 ScopedDeletePtr<NrIceOpaque> opaque_; |
|
217 }; |
|
218 |
|
219 |
|
220 } // close namespace |
|
221 #endif |