1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/transportlayerice.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Original author: ekr@rtfm.com 1.11 + 1.12 +// Some of this code is cut-and-pasted from nICEr. Copyright is: 1.13 + 1.14 +/* 1.15 +Copyright (c) 2007, Adobe Systems, Incorporated 1.16 +All rights reserved. 1.17 + 1.18 +Redistribution and use in source and binary forms, with or without 1.19 +modification, are permitted provided that the following conditions are 1.20 +met: 1.21 + 1.22 +* Redistributions of source code must retain the above copyright 1.23 + notice, this list of conditions and the following disclaimer. 1.24 + 1.25 +* Redistributions in binary form must reproduce the above copyright 1.26 + notice, this list of conditions and the following disclaimer in the 1.27 + documentation and/or other materials provided with the distribution. 1.28 + 1.29 +* Neither the name of Adobe Systems, Network Resonance nor the names of its 1.30 + contributors may be used to endorse or promote products derived from 1.31 + this software without specific prior written permission. 1.32 + 1.33 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.34 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.35 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.36 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.37 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.38 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.39 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.40 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.41 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.42 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.43 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.44 +*/ 1.45 + 1.46 + 1.47 +#include <string> 1.48 +#include <vector> 1.49 + 1.50 +#include "nsCOMPtr.h" 1.51 +#include "nsComponentManagerUtils.h" 1.52 +#include "nsError.h" 1.53 +#include "nsIEventTarget.h" 1.54 +#include "nsNetCID.h" 1.55 +#include "nsComponentManagerUtils.h" 1.56 +#include "nsServiceManagerUtils.h" 1.57 + 1.58 +// nICEr includes 1.59 +extern "C" { 1.60 +#include "nr_api.h" 1.61 +#include "registry.h" 1.62 +#include "async_timer.h" 1.63 +#include "ice_util.h" 1.64 +#include "transport_addr.h" 1.65 +#include "nr_crypto.h" 1.66 +#include "nr_socket.h" 1.67 +#include "nr_socket_local.h" 1.68 +#include "stun_client_ctx.h" 1.69 +#include "stun_server_ctx.h" 1.70 +#include "ice_ctx.h" 1.71 +#include "ice_candidate.h" 1.72 +#include "ice_handler.h" 1.73 +} 1.74 + 1.75 +// Local includes 1.76 +#include "logging.h" 1.77 +#include "nricectx.h" 1.78 +#include "nricemediastream.h" 1.79 +#include "transportflow.h" 1.80 +#include "transportlayerice.h" 1.81 + 1.82 +namespace mozilla { 1.83 + 1.84 +#ifdef ERROR 1.85 +#undef ERROR 1.86 +#endif 1.87 + 1.88 +MOZ_MTLOG_MODULE("mtransport") 1.89 + 1.90 +TransportLayerIce::TransportLayerIce(const std::string& name, 1.91 + RefPtr<NrIceCtx> ctx, RefPtr<NrIceMediaStream> stream, 1.92 + int component) 1.93 + : name_(name), ctx_(ctx), stream_(stream), component_(component) { 1.94 + target_ = ctx->thread(); 1.95 + 1.96 + stream_->SignalReady.connect(this, &TransportLayerIce::IceReady); 1.97 + stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed); 1.98 + stream_->SignalPacketReceived.connect(this, 1.99 + &TransportLayerIce::IcePacketReceived); 1.100 + if (stream_->state() == NrIceMediaStream::ICE_OPEN) { 1.101 + TL_SET_STATE(TS_OPEN); 1.102 + } 1.103 +} 1.104 + 1.105 +TransportLayerIce::~TransportLayerIce() { 1.106 + // No need to do anything here, since we use smart pointers 1.107 +} 1.108 + 1.109 +TransportResult TransportLayerIce::SendPacket(const unsigned char *data, 1.110 + size_t len) { 1.111 + CheckThread(); 1.112 + nsresult res = stream_->SendPacket(component_, data, len); 1.113 + 1.114 + if (!NS_SUCCEEDED(res)) { 1.115 + return (res == NS_BASE_STREAM_WOULD_BLOCK) ? 1.116 + TE_WOULDBLOCK : TE_ERROR; 1.117 + } 1.118 + 1.119 + MOZ_MTLOG(ML_DEBUG, LAYER_INFO << " SendPacket(" << len << ") succeeded"); 1.120 + 1.121 + return len; 1.122 +} 1.123 + 1.124 + 1.125 +void TransportLayerIce::IceCandidate(NrIceMediaStream *stream, 1.126 + const std::string&) { 1.127 + // NO-OP for now 1.128 +} 1.129 + 1.130 +void TransportLayerIce::IceReady(NrIceMediaStream *stream) { 1.131 + CheckThread(); 1.132 + TL_SET_STATE(TS_OPEN); 1.133 +} 1.134 + 1.135 +void TransportLayerIce::IceFailed(NrIceMediaStream *stream) { 1.136 + CheckThread(); 1.137 + TL_SET_STATE(TS_ERROR); 1.138 +} 1.139 + 1.140 +void TransportLayerIce::IcePacketReceived(NrIceMediaStream *stream, int component, 1.141 + const unsigned char *data, int len) { 1.142 + CheckThread(); 1.143 + // We get packets for both components, so ignore the ones that aren't 1.144 + // for us. 1.145 + if (component_ != component) 1.146 + return; 1.147 + 1.148 + MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << "," 1.149 + << component << "," << len << ")"); 1.150 + SignalPacketReceived(this, data, len); 1.151 +} 1.152 + 1.153 +} // close namespace