transportlayerice.cpp (5784B)
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 #include <string> 44 45 #include "nsComponentManagerUtils.h" 46 #include "nsError.h" 47 #include "nsNetCID.h" 48 49 // nICEr includes 50 extern "C" { 51 #include "ice_util.h" 52 #include "nr_api.h" 53 #include "transport_addr.h" 54 } 55 56 // Local includes 57 #include "logging.h" 58 #include "nricemediastream.h" 59 #include "transportlayerice.h" 60 61 namespace mozilla { 62 63 #ifdef ERROR 64 # undef ERROR 65 #endif 66 67 MOZ_MTLOG_MODULE("mtransport") 68 69 TransportLayerIce::TransportLayerIce() : stream_(nullptr), component_(0) { 70 // setup happens later 71 } 72 73 TransportLayerIce::~TransportLayerIce() { 74 // No need to do anything here, since we use smart pointers 75 } 76 77 void TransportLayerIce::SetParameters(RefPtr<NrIceMediaStream> stream, 78 int component) { 79 // Stream could be null in the case of some badly written js that causes 80 // us to be in an ICE restart case, but not have valid streams due to 81 // not calling PeerConnectionImpl::EnsureTransports if 82 // PeerConnectionImpl::SetSignalingState_m thinks the conditions were 83 // not correct. We also solved a case where an incoming answer was 84 // incorrectly beginning an ICE restart when the offer did not indicate one. 85 if (!stream) { 86 MOZ_ASSERT(false); 87 return; 88 } 89 90 stream_ = stream; 91 component_ = component; 92 93 PostSetup(); 94 } 95 96 void TransportLayerIce::PostSetup() { 97 stream_->SignalReady.connect(this, &TransportLayerIce::IceReady); 98 stream_->SignalFailed.connect(this, &TransportLayerIce::IceFailed); 99 stream_->SignalPacketReceived.connect(this, 100 &TransportLayerIce::IcePacketReceived); 101 if (stream_->state() == NrIceMediaStream::ICE_OPEN) { 102 TL_SET_STATE(TS_OPEN); 103 } 104 } 105 106 TransportResult TransportLayerIce::SendPacket(MediaPacket& packet) { 107 CheckThread(); 108 nsresult res = stream_->SendPacket(component_, packet.data(), packet.len()); 109 int len = packet.len(); 110 // We're done with packet. 111 SignalPacketSending(this, packet); 112 113 if (!NS_SUCCEEDED(res)) { 114 return (res == NS_BASE_STREAM_WOULD_BLOCK) ? TE_WOULDBLOCK : TE_ERROR; 115 } 116 117 MOZ_MTLOG(ML_DEBUG, 118 LAYER_INFO << " SendPacket(" << packet.len() << ") succeeded"); 119 120 return len; 121 } 122 123 void TransportLayerIce::IceCandidate(NrIceMediaStream* stream, 124 const std::string&) { 125 // NO-OP for now 126 } 127 128 void TransportLayerIce::IceReady(NrIceMediaStream* stream) { 129 CheckThread(); 130 // only handle the current stream (not the old stream during restart) 131 if (stream != stream_) { 132 return; 133 } 134 MOZ_MTLOG(ML_INFO, LAYER_INFO << "ICE Ready(" << stream->name() << "," 135 << component_ << ")"); 136 TL_SET_STATE(TS_OPEN); 137 } 138 139 void TransportLayerIce::IceFailed(NrIceMediaStream* stream) { 140 CheckThread(); 141 // only handle the current stream (not the old stream during restart) 142 if (stream != stream_) { 143 return; 144 } 145 MOZ_MTLOG(ML_INFO, LAYER_INFO << "ICE Failed(" << stream->name() << "," 146 << component_ << ")"); 147 TL_SET_STATE(TS_ERROR); 148 } 149 150 void TransportLayerIce::IcePacketReceived(NrIceMediaStream* stream, 151 int component, 152 const unsigned char* data, int len) { 153 CheckThread(); 154 // We get packets for both components, so ignore the ones that aren't 155 // for us. 156 if (component_ != component) return; 157 158 MOZ_MTLOG(ML_DEBUG, LAYER_INFO << "PacketReceived(" << stream->name() << "," 159 << component << "," << len << ")"); 160 // Might be useful to allow MediaPacket to borrow a buffer (ie; not take 161 // ownership, but copy it if the MediaPacket is moved). This could be a 162 // footgun though with MediaPackets that end up on the heap. 163 MediaPacket packet; 164 packet.Copy(data, len); 165 packet.Categorize(); 166 167 SignalPacketReceived(this, packet); 168 } 169 170 } // namespace mozilla