|
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 |
|
44 #include <string> |
|
45 #include <vector> |
|
46 |
|
47 #include "nsCOMPtr.h" |
|
48 #include "nsComponentManagerUtils.h" |
|
49 #include "nsError.h" |
|
50 #include "nsIEventTarget.h" |
|
51 #include "nsNetCID.h" |
|
52 #include "nsComponentManagerUtils.h" |
|
53 #include "nsServiceManagerUtils.h" |
|
54 |
|
55 // nICEr includes |
|
56 extern "C" { |
|
57 #include "nr_api.h" |
|
58 #include "registry.h" |
|
59 #include "async_timer.h" |
|
60 #include "ice_util.h" |
|
61 #include "transport_addr.h" |
|
62 #include "nr_crypto.h" |
|
63 #include "nr_socket.h" |
|
64 #include "nr_socket_local.h" |
|
65 #include "stun_client_ctx.h" |
|
66 #include "stun_server_ctx.h" |
|
67 #include "ice_ctx.h" |
|
68 #include "ice_candidate.h" |
|
69 #include "ice_handler.h" |
|
70 } |
|
71 |
|
72 // Local includes |
|
73 #include "logging.h" |
|
74 #include "nricectx.h" |
|
75 #include "nricemediastream.h" |
|
76 #include "transportflow.h" |
|
77 #include "transportlayerice.h" |
|
78 |
|
79 namespace mozilla { |
|
80 |
|
81 #ifdef ERROR |
|
82 #undef ERROR |
|
83 #endif |
|
84 |
|
85 MOZ_MTLOG_MODULE("mtransport") |
|
86 |
|
87 TransportLayerIce::TransportLayerIce(const std::string& name, |
|
88 RefPtr<NrIceCtx> ctx, RefPtr<NrIceMediaStream> stream, |
|
89 int component) |
|
90 : name_(name), ctx_(ctx), stream_(stream), component_(component) { |
|
91 target_ = ctx->thread(); |
|
92 |
|
93 stream_->SignalReady.connect(this, &TransportLayerIce::IceReady); |
|
94 stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed); |
|
95 stream_->SignalPacketReceived.connect(this, |
|
96 &TransportLayerIce::IcePacketReceived); |
|
97 if (stream_->state() == NrIceMediaStream::ICE_OPEN) { |
|
98 TL_SET_STATE(TS_OPEN); |
|
99 } |
|
100 } |
|
101 |
|
102 TransportLayerIce::~TransportLayerIce() { |
|
103 // No need to do anything here, since we use smart pointers |
|
104 } |
|
105 |
|
106 TransportResult TransportLayerIce::SendPacket(const unsigned char *data, |
|
107 size_t len) { |
|
108 CheckThread(); |
|
109 nsresult res = stream_->SendPacket(component_, data, len); |
|
110 |
|
111 if (!NS_SUCCEEDED(res)) { |
|
112 return (res == NS_BASE_STREAM_WOULD_BLOCK) ? |
|
113 TE_WOULDBLOCK : TE_ERROR; |
|
114 } |
|
115 |
|
116 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " SendPacket(" << len << ") succeeded"); |
|
117 |
|
118 return len; |
|
119 } |
|
120 |
|
121 |
|
122 void TransportLayerIce::IceCandidate(NrIceMediaStream *stream, |
|
123 const std::string&) { |
|
124 // NO-OP for now |
|
125 } |
|
126 |
|
127 void TransportLayerIce::IceReady(NrIceMediaStream *stream) { |
|
128 CheckThread(); |
|
129 TL_SET_STATE(TS_OPEN); |
|
130 } |
|
131 |
|
132 void TransportLayerIce::IceFailed(NrIceMediaStream *stream) { |
|
133 CheckThread(); |
|
134 TL_SET_STATE(TS_ERROR); |
|
135 } |
|
136 |
|
137 void TransportLayerIce::IcePacketReceived(NrIceMediaStream *stream, int component, |
|
138 const unsigned char *data, int len) { |
|
139 CheckThread(); |
|
140 // We get packets for both components, so ignore the ones that aren't |
|
141 // for us. |
|
142 if (component_ != component) |
|
143 return; |
|
144 |
|
145 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << "," |
|
146 << component << "," << len << ")"); |
|
147 SignalPacketReceived(this, data, len); |
|
148 } |
|
149 |
|
150 } // close namespace |