michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Original author: ekr@rtfm.com michael@0: michael@0: // Some of this code is cut-and-pasted from nICEr. Copyright is: michael@0: michael@0: /* michael@0: Copyright (c) 2007, Adobe Systems, Incorporated michael@0: All rights reserved. michael@0: michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions are michael@0: met: michael@0: michael@0: * Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: * Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: * Neither the name of Adobe Systems, Network Resonance nor the names of its michael@0: contributors may be used to endorse or promote products derived from michael@0: this software without specific prior written permission. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsError.h" michael@0: #include "nsIEventTarget.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: michael@0: // nICEr includes michael@0: extern "C" { michael@0: #include "nr_api.h" michael@0: #include "registry.h" michael@0: #include "async_timer.h" michael@0: #include "ice_util.h" michael@0: #include "transport_addr.h" michael@0: #include "nr_crypto.h" michael@0: #include "nr_socket.h" michael@0: #include "nr_socket_local.h" michael@0: #include "stun_client_ctx.h" michael@0: #include "stun_server_ctx.h" michael@0: #include "ice_ctx.h" michael@0: #include "ice_candidate.h" michael@0: #include "ice_handler.h" michael@0: } michael@0: michael@0: // Local includes michael@0: #include "logging.h" michael@0: #include "nricectx.h" michael@0: #include "nricemediastream.h" michael@0: #include "transportflow.h" michael@0: #include "transportlayerice.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: #ifdef ERROR michael@0: #undef ERROR michael@0: #endif michael@0: michael@0: MOZ_MTLOG_MODULE("mtransport") michael@0: michael@0: TransportLayerIce::TransportLayerIce(const std::string& name, michael@0: RefPtr ctx, RefPtr stream, michael@0: int component) michael@0: : name_(name), ctx_(ctx), stream_(stream), component_(component) { michael@0: target_ = ctx->thread(); michael@0: michael@0: stream_->SignalReady.connect(this, &TransportLayerIce::IceReady); michael@0: stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed); michael@0: stream_->SignalPacketReceived.connect(this, michael@0: &TransportLayerIce::IcePacketReceived); michael@0: if (stream_->state() == NrIceMediaStream::ICE_OPEN) { michael@0: TL_SET_STATE(TS_OPEN); michael@0: } michael@0: } michael@0: michael@0: TransportLayerIce::~TransportLayerIce() { michael@0: // No need to do anything here, since we use smart pointers michael@0: } michael@0: michael@0: TransportResult TransportLayerIce::SendPacket(const unsigned char *data, michael@0: size_t len) { michael@0: CheckThread(); michael@0: nsresult res = stream_->SendPacket(component_, data, len); michael@0: michael@0: if (!NS_SUCCEEDED(res)) { michael@0: return (res == NS_BASE_STREAM_WOULD_BLOCK) ? michael@0: TE_WOULDBLOCK : TE_ERROR; michael@0: } michael@0: michael@0: MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " SendPacket(" << len << ") succeeded"); michael@0: michael@0: return len; michael@0: } michael@0: michael@0: michael@0: void TransportLayerIce::IceCandidate(NrIceMediaStream *stream, michael@0: const std::string&) { michael@0: // NO-OP for now michael@0: } michael@0: michael@0: void TransportLayerIce::IceReady(NrIceMediaStream *stream) { michael@0: CheckThread(); michael@0: TL_SET_STATE(TS_OPEN); michael@0: } michael@0: michael@0: void TransportLayerIce::IceFailed(NrIceMediaStream *stream) { michael@0: CheckThread(); michael@0: TL_SET_STATE(TS_ERROR); michael@0: } michael@0: michael@0: void TransportLayerIce::IcePacketReceived(NrIceMediaStream *stream, int component, michael@0: const unsigned char *data, int len) { michael@0: CheckThread(); michael@0: // We get packets for both components, so ignore the ones that aren't michael@0: // for us. michael@0: if (component_ != component) michael@0: return; michael@0: michael@0: MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << "," michael@0: << component << "," << len << ")"); michael@0: SignalPacketReceived(this, data, len); michael@0: } michael@0: michael@0: } // close namespace