Bumping manifests a=b2g-bump
[gecko.git] / media / mtransport / transportlayerice.cpp
blob2c40226525cda0a61c26fc5a97e1921beb497a9a
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/. */
7 // Original author: ekr@rtfm.com
9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
12 Copyright (c) 2007, Adobe Systems, Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are
17 met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
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.
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.
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.
44 #include <string>
45 #include <vector>
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"
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"
72 // Local includes
73 #include "logging.h"
74 #include "nricectx.h"
75 #include "nricemediastream.h"
76 #include "transportflow.h"
77 #include "transportlayerice.h"
79 namespace mozilla {
81 #ifdef ERROR
82 #undef ERROR
83 #endif
85 MOZ_MTLOG_MODULE("mtransport")
87 TransportLayerIce::TransportLayerIce(const std::string& name)
88 : name_(name), ctx_(nullptr), stream_(nullptr), component_(0) {}
90 TransportLayerIce::~TransportLayerIce() {
91 // No need to do anything here, since we use smart pointers
94 void TransportLayerIce::SetParameters(RefPtr<NrIceCtx> ctx,
95 RefPtr<NrIceMediaStream> stream,
96 int component) {
97 ctx_ = ctx;
98 stream_ = stream;
99 component_ = component;
101 PostSetup();
104 void TransportLayerIce::PostSetup() {
105 target_ = ctx_->thread();
107 stream_->SignalReady.connect(this, &TransportLayerIce::IceReady);
108 stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed);
109 stream_->SignalPacketReceived.connect(this,
110 &TransportLayerIce::IcePacketReceived);
111 if (stream_->state() == NrIceMediaStream::ICE_OPEN) {
112 TL_SET_STATE(TS_OPEN);
116 TransportResult TransportLayerIce::SendPacket(const unsigned char *data,
117 size_t len) {
118 CheckThread();
119 nsresult res = stream_->SendPacket(component_, data, len);
121 if (!NS_SUCCEEDED(res)) {
122 return (res == NS_BASE_STREAM_WOULD_BLOCK) ?
123 TE_WOULDBLOCK : TE_ERROR;
126 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " SendPacket(" << len << ") succeeded");
128 return len;
132 void TransportLayerIce::IceCandidate(NrIceMediaStream *stream,
133 const std::string&) {
134 // NO-OP for now
137 void TransportLayerIce::IceReady(NrIceMediaStream *stream) {
138 CheckThread();
139 TL_SET_STATE(TS_OPEN);
142 void TransportLayerIce::IceFailed(NrIceMediaStream *stream) {
143 CheckThread();
144 TL_SET_STATE(TS_ERROR);
147 void TransportLayerIce::IcePacketReceived(NrIceMediaStream *stream, int component,
148 const unsigned char *data, int len) {
149 CheckThread();
150 // We get packets for both components, so ignore the ones that aren't
151 // for us.
152 if (component_ != component)
153 return;
155 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << ","
156 << component << "," << len << ")");
157 SignalPacketReceived(this, data, len);
160 } // close namespace