media/mtransport/nricemediastream.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 // Original author: ekr@rtfm.com
michael@0 8
michael@0 9 // Some of this code is cut-and-pasted from nICEr. Copyright is:
michael@0 10
michael@0 11 /*
michael@0 12 Copyright (c) 2007, Adobe Systems, Incorporated
michael@0 13 All rights reserved.
michael@0 14
michael@0 15 Redistribution and use in source and binary forms, with or without
michael@0 16 modification, are permitted provided that the following conditions are
michael@0 17 met:
michael@0 18
michael@0 19 * Redistributions of source code must retain the above copyright
michael@0 20 notice, this list of conditions and the following disclaimer.
michael@0 21
michael@0 22 * Redistributions in binary form must reproduce the above copyright
michael@0 23 notice, this list of conditions and the following disclaimer in the
michael@0 24 documentation and/or other materials provided with the distribution.
michael@0 25
michael@0 26 * Neither the name of Adobe Systems, Network Resonance nor the names of its
michael@0 27 contributors may be used to endorse or promote products derived from
michael@0 28 this software without specific prior written permission.
michael@0 29
michael@0 30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 41 */
michael@0 42
michael@0 43
michael@0 44 #include <string>
michael@0 45 #include <vector>
michael@0 46
michael@0 47 #include "logging.h"
michael@0 48 #include "nsError.h"
michael@0 49 #include "mozilla/Scoped.h"
michael@0 50
michael@0 51 // nICEr includes
michael@0 52 extern "C" {
michael@0 53 #include "nr_api.h"
michael@0 54 #include "registry.h"
michael@0 55 #include "async_timer.h"
michael@0 56 #include "ice_util.h"
michael@0 57 #include "transport_addr.h"
michael@0 58 #include "nr_crypto.h"
michael@0 59 #include "nr_socket.h"
michael@0 60 #include "nr_socket_local.h"
michael@0 61 #include "stun_client_ctx.h"
michael@0 62 #include "stun_server_ctx.h"
michael@0 63 #include "ice_ctx.h"
michael@0 64 #include "ice_candidate.h"
michael@0 65 #include "ice_handler.h"
michael@0 66 }
michael@0 67
michael@0 68 // Local includes
michael@0 69 #include "nricectx.h"
michael@0 70 #include "nricemediastream.h"
michael@0 71
michael@0 72 namespace mozilla {
michael@0 73
michael@0 74 MOZ_MTLOG_MODULE("mtransport")
michael@0 75
michael@0 76 static bool ToNrIceAddr(nr_transport_addr &addr,
michael@0 77 NrIceAddr *out) {
michael@0 78 int r;
michael@0 79 char addrstring[INET6_ADDRSTRLEN + 1];
michael@0 80
michael@0 81 r = nr_transport_addr_get_addrstring(&addr, addrstring, sizeof(addrstring));
michael@0 82 if (r)
michael@0 83 return false;
michael@0 84 out->host = addrstring;
michael@0 85
michael@0 86 int port;
michael@0 87 r = nr_transport_addr_get_port(&addr, &port);
michael@0 88 if (r)
michael@0 89 return false;
michael@0 90
michael@0 91 out->port = port;
michael@0 92
michael@0 93 switch (addr.protocol) {
michael@0 94 case IPPROTO_TCP:
michael@0 95 out->transport = kNrIceTransportTcp;
michael@0 96 break;
michael@0 97 case IPPROTO_UDP:
michael@0 98 out->transport = kNrIceTransportUdp;
michael@0 99 break;
michael@0 100 default:
michael@0 101 MOZ_CRASH();
michael@0 102 return false;
michael@0 103 }
michael@0 104
michael@0 105 return true;
michael@0 106 }
michael@0 107
michael@0 108 static bool ToNrIceCandidate(const nr_ice_candidate& candc,
michael@0 109 NrIceCandidate* out) {
michael@0 110 MOZ_ASSERT(out);
michael@0 111 int r;
michael@0 112 // Const-cast because the internal nICEr code isn't const-correct.
michael@0 113 nr_ice_candidate *cand = const_cast<nr_ice_candidate *>(&candc);
michael@0 114
michael@0 115 if (!ToNrIceAddr(cand->addr, &out->cand_addr))
michael@0 116 return false;
michael@0 117
michael@0 118 if (cand->isock) {
michael@0 119 nr_transport_addr addr;
michael@0 120 r = nr_socket_getaddr(cand->isock->sock, &addr);
michael@0 121 if (r)
michael@0 122 return false;
michael@0 123
michael@0 124 if (!ToNrIceAddr(addr, &out->local_addr))
michael@0 125 return false;
michael@0 126 }
michael@0 127
michael@0 128 NrIceCandidate::Type type;
michael@0 129
michael@0 130 switch (cand->type) {
michael@0 131 case HOST:
michael@0 132 type = NrIceCandidate::ICE_HOST;
michael@0 133 break;
michael@0 134 case SERVER_REFLEXIVE:
michael@0 135 type = NrIceCandidate::ICE_SERVER_REFLEXIVE;
michael@0 136 break;
michael@0 137 case PEER_REFLEXIVE:
michael@0 138 type = NrIceCandidate::ICE_PEER_REFLEXIVE;
michael@0 139 break;
michael@0 140 case RELAYED:
michael@0 141 type = NrIceCandidate::ICE_RELAYED;
michael@0 142 break;
michael@0 143 default:
michael@0 144 return false;
michael@0 145 }
michael@0 146
michael@0 147 out->type = type;
michael@0 148 out->codeword = candc.codeword;
michael@0 149 return true;
michael@0 150 }
michael@0 151
michael@0 152 // Make an NrIceCandidate from the candidate |cand|.
michael@0 153 // This is not a member fxn because we want to hide the
michael@0 154 // defn of nr_ice_candidate but we pass by reference.
michael@0 155 static NrIceCandidate* MakeNrIceCandidate(const nr_ice_candidate& candc) {
michael@0 156 ScopedDeletePtr<NrIceCandidate> out(new NrIceCandidate());
michael@0 157
michael@0 158 if (!ToNrIceCandidate(candc, out)) {
michael@0 159 return nullptr;
michael@0 160 }
michael@0 161 return out.forget();
michael@0 162 }
michael@0 163
michael@0 164 // NrIceMediaStream
michael@0 165 RefPtr<NrIceMediaStream>
michael@0 166 NrIceMediaStream::Create(NrIceCtx *ctx,
michael@0 167 const std::string& name,
michael@0 168 int components) {
michael@0 169 RefPtr<NrIceMediaStream> stream =
michael@0 170 new NrIceMediaStream(ctx, name, components);
michael@0 171
michael@0 172 int r = nr_ice_add_media_stream(ctx->ctx(),
michael@0 173 const_cast<char *>(name.c_str()),
michael@0 174 components, &stream->stream_);
michael@0 175 if (r) {
michael@0 176 MOZ_MTLOG(ML_ERROR, "Couldn't create ICE media stream for '"
michael@0 177 << name << "'");
michael@0 178 return nullptr;
michael@0 179 }
michael@0 180
michael@0 181 return stream;
michael@0 182 }
michael@0 183
michael@0 184 NrIceMediaStream::~NrIceMediaStream() {
michael@0 185 // We do not need to destroy anything. All major resources
michael@0 186 // are attached to the ice ctx.
michael@0 187 }
michael@0 188
michael@0 189 nsresult NrIceMediaStream::ParseAttributes(std::vector<std::string>&
michael@0 190 attributes) {
michael@0 191 if (!stream_)
michael@0 192 return NS_ERROR_FAILURE;
michael@0 193
michael@0 194 std::vector<char *> attributes_in;
michael@0 195
michael@0 196 for (size_t i=0; i<attributes.size(); ++i) {
michael@0 197 attributes_in.push_back(const_cast<char *>(attributes[i].c_str()));
michael@0 198 }
michael@0 199
michael@0 200 // Still need to call nr_ice_ctx_parse_stream_attributes.
michael@0 201 int r = nr_ice_peer_ctx_parse_stream_attributes(ctx_->peer(),
michael@0 202 stream_,
michael@0 203 attributes_in.size() ?
michael@0 204 &attributes_in[0] : nullptr,
michael@0 205 attributes_in.size());
michael@0 206 if (r) {
michael@0 207 MOZ_MTLOG(ML_ERROR, "Couldn't parse attributes for stream "
michael@0 208 << name_ << "'");
michael@0 209 return NS_ERROR_FAILURE;
michael@0 210 }
michael@0 211
michael@0 212 return NS_OK;
michael@0 213 }
michael@0 214
michael@0 215 // Parse trickle ICE candidate
michael@0 216 nsresult NrIceMediaStream::ParseTrickleCandidate(const std::string& candidate) {
michael@0 217 int r;
michael@0 218
michael@0 219 MOZ_MTLOG(ML_DEBUG, "NrIceCtx(" << ctx_->name() << ")/STREAM(" <<
michael@0 220 name() << ") : parsing trickle candidate " << candidate);
michael@0 221
michael@0 222 r = nr_ice_peer_ctx_parse_trickle_candidate(ctx_->peer(),
michael@0 223 stream_,
michael@0 224 const_cast<char *>(
michael@0 225 candidate.c_str())
michael@0 226 );
michael@0 227 if (r) {
michael@0 228 if (r == R_ALREADY) {
michael@0 229 MOZ_MTLOG(ML_ERROR, "Trickle candidates are redundant for stream '"
michael@0 230 << name_ << "' because it is completed");
michael@0 231
michael@0 232 } else {
michael@0 233 MOZ_MTLOG(ML_ERROR, "Couldn't parse trickle candidate for stream '"
michael@0 234 << name_ << "'");
michael@0 235 return NS_ERROR_FAILURE;
michael@0 236 }
michael@0 237 }
michael@0 238
michael@0 239 return NS_OK;
michael@0 240 }
michael@0 241
michael@0 242 // Returns NS_ERROR_NOT_AVAILABLE if component is unpaired or disabled.
michael@0 243 nsresult NrIceMediaStream::GetActivePair(int component,
michael@0 244 NrIceCandidate **localp,
michael@0 245 NrIceCandidate **remotep) {
michael@0 246 int r;
michael@0 247 nr_ice_candidate *local_int;
michael@0 248 nr_ice_candidate *remote_int;
michael@0 249
michael@0 250 r = nr_ice_media_stream_get_active(ctx_->peer(),
michael@0 251 stream_,
michael@0 252 component,
michael@0 253 &local_int, &remote_int);
michael@0 254 // If result is R_REJECTED then component is unpaired or disabled.
michael@0 255 if (r == R_REJECTED)
michael@0 256 return NS_ERROR_NOT_AVAILABLE;
michael@0 257
michael@0 258 if (r)
michael@0 259 return NS_ERROR_FAILURE;
michael@0 260
michael@0 261 ScopedDeletePtr<NrIceCandidate> local(
michael@0 262 MakeNrIceCandidate(*local_int));
michael@0 263 if (!local)
michael@0 264 return NS_ERROR_FAILURE;
michael@0 265
michael@0 266 ScopedDeletePtr<NrIceCandidate> remote(
michael@0 267 MakeNrIceCandidate(*remote_int));
michael@0 268 if (!remote)
michael@0 269 return NS_ERROR_FAILURE;
michael@0 270
michael@0 271 if (localp)
michael@0 272 *localp = local.forget();
michael@0 273 if (remotep)
michael@0 274 *remotep = remote.forget();
michael@0 275
michael@0 276 return NS_OK;
michael@0 277 }
michael@0 278
michael@0 279
michael@0 280 nsresult NrIceMediaStream::GetCandidatePairs(std::vector<NrIceCandidatePair>*
michael@0 281 out_pairs) const {
michael@0 282 MOZ_ASSERT(out_pairs);
michael@0 283
michael@0 284 // Get the check_list on the peer stream (this is where the check_list
michael@0 285 // actually lives, not in stream_)
michael@0 286 nr_ice_media_stream* peer_stream;
michael@0 287 int r = nr_ice_peer_ctx_find_pstream(ctx_->peer(), stream_, &peer_stream);
michael@0 288 if (r != 0) {
michael@0 289 return NS_ERROR_FAILURE;
michael@0 290 }
michael@0 291
michael@0 292 nr_ice_cand_pair *p1;
michael@0 293 out_pairs->clear();
michael@0 294
michael@0 295 TAILQ_FOREACH(p1, &peer_stream->check_list, entry) {
michael@0 296 MOZ_ASSERT(p1);
michael@0 297 MOZ_ASSERT(p1->local);
michael@0 298 MOZ_ASSERT(p1->remote);
michael@0 299 NrIceCandidatePair pair;
michael@0 300
michael@0 301 switch (p1->state) {
michael@0 302 case NR_ICE_PAIR_STATE_FROZEN:
michael@0 303 pair.state = NrIceCandidatePair::State::STATE_FROZEN;
michael@0 304 break;
michael@0 305 case NR_ICE_PAIR_STATE_WAITING:
michael@0 306 pair.state = NrIceCandidatePair::State::STATE_WAITING;
michael@0 307 break;
michael@0 308 case NR_ICE_PAIR_STATE_IN_PROGRESS:
michael@0 309 pair.state = NrIceCandidatePair::State::STATE_IN_PROGRESS;
michael@0 310 break;
michael@0 311 case NR_ICE_PAIR_STATE_FAILED:
michael@0 312 pair.state = NrIceCandidatePair::State::STATE_FAILED;
michael@0 313 break;
michael@0 314 case NR_ICE_PAIR_STATE_SUCCEEDED:
michael@0 315 pair.state = NrIceCandidatePair::State::STATE_SUCCEEDED;
michael@0 316 break;
michael@0 317 case NR_ICE_PAIR_STATE_CANCELLED:
michael@0 318 pair.state = NrIceCandidatePair::State::STATE_CANCELLED;
michael@0 319 break;
michael@0 320 default:
michael@0 321 MOZ_ASSERT(0);
michael@0 322 }
michael@0 323
michael@0 324 pair.priority = p1->priority;
michael@0 325 pair.nominated = p1->peer_nominated || p1->nominated;
michael@0 326 pair.selected = p1->remote->component &&
michael@0 327 p1->remote->component->active == p1;
michael@0 328 pair.codeword = p1->codeword;
michael@0 329
michael@0 330 if (!ToNrIceCandidate(*(p1->local), &pair.local) ||
michael@0 331 !ToNrIceCandidate(*(p1->remote), &pair.remote)) {
michael@0 332 return NS_ERROR_FAILURE;
michael@0 333 }
michael@0 334
michael@0 335 out_pairs->push_back(pair);
michael@0 336 }
michael@0 337
michael@0 338 return NS_OK;
michael@0 339 }
michael@0 340
michael@0 341 nsresult NrIceMediaStream::GetDefaultCandidate(int component,
michael@0 342 std::string *addrp,
michael@0 343 int *portp) {
michael@0 344 nr_ice_candidate *cand;
michael@0 345 int r;
michael@0 346
michael@0 347 r = nr_ice_media_stream_get_default_candidate(stream_,
michael@0 348 component, &cand);
michael@0 349 if (r) {
michael@0 350 if (ctx_->generating_trickle()) {
michael@0 351 // Generate default trickle candidates.
michael@0 352 // draft-ivov-mmusic-trickle-ice-01.txt says to use port 9
michael@0 353 // but "::" instead of "0.0.0.0". Since we don't do any
michael@0 354 // IPv6 we are ignoring that for now.
michael@0 355 *addrp = "0.0.0.0";
michael@0 356 *portp = 9;
michael@0 357 }
michael@0 358 else {
michael@0 359 MOZ_MTLOG(ML_ERROR, "Couldn't get default ICE candidate for '"
michael@0 360 << name_ << "'");
michael@0 361
michael@0 362 return NS_ERROR_NOT_AVAILABLE;
michael@0 363 }
michael@0 364 }
michael@0 365 else {
michael@0 366 char addr[64]; // Enough for IPv6 with colons.
michael@0 367 r = nr_transport_addr_get_addrstring(&cand->addr,addr,sizeof(addr));
michael@0 368 if (r)
michael@0 369 return NS_ERROR_FAILURE;
michael@0 370
michael@0 371 int port;
michael@0 372 r=nr_transport_addr_get_port(&cand->addr,&port);
michael@0 373 if (r)
michael@0 374 return NS_ERROR_FAILURE;
michael@0 375
michael@0 376 *addrp = addr;
michael@0 377 *portp = port;
michael@0 378 }
michael@0 379
michael@0 380 return NS_OK;
michael@0 381 }
michael@0 382
michael@0 383 std::vector<std::string> NrIceMediaStream::GetCandidates() const {
michael@0 384 char **attrs = 0;
michael@0 385 int attrct;
michael@0 386 int r;
michael@0 387 std::vector<std::string> ret;
michael@0 388
michael@0 389 r = nr_ice_media_stream_get_attributes(stream_,
michael@0 390 &attrs, &attrct);
michael@0 391 if (r) {
michael@0 392 MOZ_MTLOG(ML_ERROR, "Couldn't get ICE candidates for '"
michael@0 393 << name_ << "'");
michael@0 394 return ret;
michael@0 395 }
michael@0 396
michael@0 397 for (int i=0; i<attrct; i++) {
michael@0 398 ret.push_back(attrs[i]);
michael@0 399 RFREE(attrs[i]);
michael@0 400 }
michael@0 401
michael@0 402 RFREE(attrs);
michael@0 403
michael@0 404 return ret;
michael@0 405 }
michael@0 406
michael@0 407 static nsresult GetCandidatesFromStream(
michael@0 408 nr_ice_media_stream *stream,
michael@0 409 std::vector<NrIceCandidate> *candidates) {
michael@0 410 MOZ_ASSERT(candidates);
michael@0 411 nr_ice_component* comp=STAILQ_FIRST(&stream->components);
michael@0 412 while(comp){
michael@0 413 if (comp->state != NR_ICE_COMPONENT_DISABLED) {
michael@0 414 nr_ice_candidate *cand = TAILQ_FIRST(&comp->candidates);
michael@0 415 while(cand){
michael@0 416 NrIceCandidate new_cand;
michael@0 417 // This can fail if the candidate is server reflexive or relayed, and
michael@0 418 // has not yet received a response (ie; it doesn't know its address
michael@0 419 // yet). For the purposes of this code, this isn't a candidate we're
michael@0 420 // interested in, since it is not fully baked yet.
michael@0 421 if (ToNrIceCandidate(*cand, &new_cand)) {
michael@0 422 candidates->push_back(new_cand);
michael@0 423 }
michael@0 424 cand=TAILQ_NEXT(cand,entry_comp);
michael@0 425 }
michael@0 426 }
michael@0 427 comp=STAILQ_NEXT(comp,entry);
michael@0 428 }
michael@0 429
michael@0 430 return NS_OK;
michael@0 431 }
michael@0 432
michael@0 433 nsresult NrIceMediaStream::GetLocalCandidates(
michael@0 434 std::vector<NrIceCandidate>* candidates) const {
michael@0 435 return GetCandidatesFromStream(stream_, candidates);
michael@0 436 }
michael@0 437
michael@0 438 nsresult NrIceMediaStream::GetRemoteCandidates(
michael@0 439 std::vector<NrIceCandidate>* candidates) const {
michael@0 440 nr_ice_media_stream* peer_stream;
michael@0 441 int r = nr_ice_peer_ctx_find_pstream(ctx_->peer(), stream_, &peer_stream);
michael@0 442 if (r != 0) {
michael@0 443 return NS_ERROR_FAILURE;
michael@0 444 }
michael@0 445
michael@0 446 return GetCandidatesFromStream(peer_stream, candidates);
michael@0 447 }
michael@0 448
michael@0 449
michael@0 450 nsresult NrIceMediaStream::DisableComponent(int component_id) {
michael@0 451 if (!stream_)
michael@0 452 return NS_ERROR_FAILURE;
michael@0 453
michael@0 454 int r = nr_ice_media_stream_disable_component(stream_,
michael@0 455 component_id);
michael@0 456 if (r) {
michael@0 457 MOZ_MTLOG(ML_ERROR, "Couldn't disable '" << name_ << "':" <<
michael@0 458 component_id);
michael@0 459 return NS_ERROR_FAILURE;
michael@0 460 }
michael@0 461
michael@0 462 return NS_OK;
michael@0 463 }
michael@0 464
michael@0 465 nsresult NrIceMediaStream::SendPacket(int component_id,
michael@0 466 const unsigned char *data,
michael@0 467 size_t len) {
michael@0 468 if (!stream_)
michael@0 469 return NS_ERROR_FAILURE;
michael@0 470
michael@0 471 int r = nr_ice_media_stream_send(ctx_->peer(), stream_,
michael@0 472 component_id,
michael@0 473 const_cast<unsigned char *>(data), len);
michael@0 474 if (r) {
michael@0 475 MOZ_MTLOG(ML_ERROR, "Couldn't send media on '" << name_ << "'");
michael@0 476 if (r == R_WOULDBLOCK) {
michael@0 477 return NS_BASE_STREAM_WOULD_BLOCK;
michael@0 478 }
michael@0 479
michael@0 480 return NS_BASE_STREAM_OSERROR;
michael@0 481 }
michael@0 482
michael@0 483 return NS_OK;
michael@0 484 }
michael@0 485
michael@0 486
michael@0 487 void NrIceMediaStream::Ready() {
michael@0 488 // This function is called whenever a stream becomes ready, but it
michael@0 489 // gets fired multiple times when a stream gets nominated repeatedly.
michael@0 490 if (state_ != ICE_OPEN) {
michael@0 491 MOZ_MTLOG(ML_DEBUG, "Marking stream ready '" << name_ << "'");
michael@0 492 state_ = ICE_OPEN;
michael@0 493 SignalReady(this);
michael@0 494 }
michael@0 495 else {
michael@0 496 MOZ_MTLOG(ML_DEBUG, "Stream ready callback fired again for '" << name_ << "'");
michael@0 497 }
michael@0 498 }
michael@0 499
michael@0 500 void NrIceMediaStream::Close() {
michael@0 501 MOZ_MTLOG(ML_DEBUG, "Marking stream closed '" << name_ << "'");
michael@0 502 state_ = ICE_CLOSED;
michael@0 503 stream_ = nullptr;
michael@0 504 }
michael@0 505 } // close namespace

mercurial