DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (205d1b4e8f69)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
<!doctype html>
<meta charset=utf-8>
<meta name="timeout" content="long">
<title>RTCPeerConnection.prototype.getStats</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="RTCPeerConnection-helper.js"></script>
<script src="dictionary-helper.js"></script>
<script src="RTCStats-helper.js"></script>
<script>
  'use strict';

  // The following helper functions are called from RTCPeerConnection-helper.js:
  //   doSignalingHandshake
  //   getUserMediaTracksAndStreams
  //   waitForRtpAndRtcpStats

  // The following helper functions are called from RTCStats-helper.js
  // (depends on dictionary-helper.js):
  //   validateRtcStats

  async_test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let track;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      track = tracks[0];
      pc.addTrack(track, streams[0]);
      return pc.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', track.id);
      assert_true(trackStats != null, 'Has stats for track');
      // TODO(hbos): Here and elsewhere, validateRtcStats() only tests id,
      // timestamp and type is correct type. Should validate based on stats type
      // but it expects both audio and video members.
      // https://github.com/web-platform-tests/wpt/issues/9010
      validateRtcStats(report, trackStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'addTrack() without setLocalDescription() yields track stats');

  async_test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let stream;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      let track = tracks[0];
      stream = streams[0];
      pc.addTrack(track, stream);
      return pc.getStats();
    }))
    .then(t.step_func(report => {
      let streamStats = findStatsByTypeAndId(report, 'stream', stream.id);
      assert_true(streamStats != null, 'Has stats for stream');
      validateRtcStats(report, streamStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'addTrack() without setLocalDescription() yields media stream stats');

  async_test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let track;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      track = tracks[0];
      pc.addTrack(track, streams[0]);
      return pc.createOffer();
    }))
    .then(t.step_func(offer => {
      return pc.setLocalDescription(offer);
    }))
    .then(t.step_func(() => {
      return pc.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', track.id);
      assert_true(trackStats != null, 'Has stats for track');
      validateRtcStats(report, trackStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'addTrack() with setLocalDescription() yields track stats');

  async_test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let stream;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      let track = tracks[0];
      stream = streams[0];
      pc.addTrack(track, stream);
      return pc.createOffer();
    }))
    .then(t.step_func(offer => {
      return pc.setLocalDescription(offer);
    }))
    .then(t.step_func(() => {
      return pc.getStats();
    }))
    .then(t.step_func(report => {
      let streamStats = findStatsByTypeAndId(report, 'stream', stream.id);
      assert_true(streamStats != null, 'Has stats for stream');
      validateRtcStats(report, streamStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'addTrack() with setLocalDescription() yields media stream stats');

  async_test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let track;
    let stream;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      track = tracks[0];
      stream = streams[0];
      pc.addTrack(track, stream);
      return pc.createOffer();
    }))
    .then(t.step_func(offer => {
      return pc.setLocalDescription(offer);
    }))
    .then(t.step_func(() => {
      return pc.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', track.id);
      let streamStats = findStatsByTypeAndId(report, 'stream', stream.id);
      assert_true(trackStats != null && streamStats != null,
                  'Has stats for track and stream');
      assert_array_equals(streamStats.trackIds, [ trackStats.id ],
                          'streamStats.trackIds == [ trackStats.id ]');
      validateRtcStats(report, trackStats);
      validateRtcStats(report, streamStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'addTrack(): Media stream stats references track stats');

  async_test(t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let track;
    let stream;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      track = tracks[0];
      stream = streams[0];
      stream.addTrack(track);
      for (const track of stream.getTracks())
        pc.addTrack(track, stream);
      return pc.createOffer();
    }))
    .then(t.step_func(offer => {
      return pc.setLocalDescription(offer);
    }))
    .then(t.step_func(() => {
      return pc.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', track.id);
      let streamStats = findStatsByTypeAndId(report, 'stream', stream.id);
      assert_true(trackStats != null && streamStats != null,
                  'Has stats for track and stream');
      assert_array_equals(streamStats.trackIds, [ trackStats.id ],
                          'streamStats.trackIds == [ trackStats.id ]');
      validateRtcStats(report, trackStats);
      validateRtcStats(report, streamStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'Media stream stats references track stats');

  async_test(t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let sendingTrack;
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      sendingTrack = tracks[0];
      caller.addTrack(sendingTrack, streams[0]);
      return doSignalingHandshake(caller, callee);
    }))
    .then(t.step_func(() => {
      return caller.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', sendingTrack.id);
      assert_true(trackStats != null, 'Has stats for sending track');
      let outboundStats = findStatsByTypeAndMember(report, 'outbound-rtp',
                                                   'trackId', trackStats.id);
      assert_true(outboundStats != null, 'Has stats for outbound RTP stream');
      validateRtcStats(report, trackStats);
      validateRtcStats(report, outboundStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'O/A exchange yields outbound RTP stream stats for sending track');

  async_test(t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let receivingTrack;
    callee.ontrack = trackEvent => {
      assert_true(receivingTrack == undefined, 'ontrack has not fired before');
      receivingTrack = trackEvent.track;
    };
    return getUserMediaTracksAndStreams(1)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      caller.addTrack(tracks[0], streams[0]);
      return doSignalingHandshake(caller, callee);
    }))
    .then(t.step_func(() => {
      return callee.getStats();
    }))
    .then(t.step_func(report => {
      assert_true(receivingTrack != null, 'Has a receiving track');
      let trackStats = findStatsByTypeAndId(report, 'track', receivingTrack.id);
      assert_true(trackStats != null, 'Has stats for receiving track');
      let inboundStats = findStatsByTypeAndMember(report, 'inbound-rtp',
                                                  'trackId', trackStats.id);
      assert_true(inboundStats != null, 'Has stats for outbound RTP stream');
      validateRtcStats(report, trackStats);
      validateRtcStats(report, inboundStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'O/A exchange yields inbound RTP stream stats for receiving track');

  async_test(t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let sendingTrack1;
    let sendingTrack2;
    let sender;
    return getUserMediaTracksAndStreams(2)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      sendingTrack1 = tracks[0];
      sendingTrack2 = tracks[1];
      sender = caller.addTrack(sendingTrack1, streams[0]);
      return sender.replaceTrack(sendingTrack2);
    }))
    .then(t.step_func(() => {
      return caller.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', sendingTrack2.id);
      assert_true(trackStats != null, 'Has stats for replaced track');
      validateRtcStats(report, trackStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'replaceTrack() before offer: new track attachment stats present');

  async_test(t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let sendingTrack1;
    let sendingTrack2;
    let sender;
    return getUserMediaTracksAndStreams(2)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      sendingTrack1 = tracks[0];
      sendingTrack2 = tracks[1];
      sender = caller.addTrack(sendingTrack1, streams[0]);
      return exchangeOffer(caller, callee);
    }))
    .then(t.step_func(() => {
      return sender.replaceTrack(sendingTrack2);
    }))
    .then(t.step_func(() => {
      return caller.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', sendingTrack2.id);
      assert_true(trackStats != null, 'Has stats for replaced track');
      let outboundStats = findStatsByTypeAndMember(report, 'outbound-rtp',
                                                   'trackId', trackStats.id);
      assert_true(outboundStats != null, 'Has stats for outbound RTP stream');
      validateRtcStats(report, trackStats);
      validateRtcStats(report, outboundStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'replaceTrack() after offer, before answer: new track attachment stats ' +
     'present');

  async_test(t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let sendingTrack1;
    let sendingTrack2;
    let sender;
    return getUserMediaTracksAndStreams(2)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      sendingTrack1 = tracks[0];
      sendingTrack2 = tracks[1];
      sender = caller.addTrack(sendingTrack1, streams[0]);
      return doSignalingHandshake(caller, callee);
    }))
    .then(t.step_func(() => {
      return sender.replaceTrack(sendingTrack2);
    }))
    .then(t.step_func(() => {
      return caller.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', sendingTrack2.id);
      assert_true(trackStats != null, 'Has stats for replaced track');
      let outboundStats = findStatsByTypeAndMember(report, 'outbound-rtp',
                                                   'trackId', trackStats.id);
      assert_true(outboundStats != null, 'Has stats for outbound RTP stream');
      validateRtcStats(report, trackStats);
      validateRtcStats(report, outboundStats);
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'replaceTrack() after answer: new track attachment stats present');

  async_test(t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let sendingTrack1;
    let sendingTrack2;
    let sender;
    return getUserMediaTracksAndStreams(2)
    .then(t.step_func(([tracks, streams]) => {
      t.add_cleanup(() => tracks.forEach(track => track.stop()));
      sendingTrack1 = tracks[0];
      sendingTrack2 = tracks[1];
      sender = caller.addTrack(sendingTrack1, streams[0]);
      return doSignalingHandshake(caller, callee);
    }))
    .then(t.step_func(() => {
      return sender.replaceTrack(sendingTrack2);
    }))
    .then(t.step_func(() => {
      return caller.getStats();
    }))
    .then(t.step_func(report => {
      let trackStats = findStatsByTypeAndId(report, 'track', sendingTrack1.id);
      assert_true(trackStats != null, 'Has stats for original track');
      assert_true(trackStats.objectDeleted);
      let outboundStats = findStatsByTypeAndMember(report, 'outbound-rtp',
                                                   'trackId', trackStats.id);
      assert_true(outboundStats == null,
                  'The outbound RTP stream should no longer reference the ' +
                  'original attachment');
      t.done();
    }))
    .catch(t.step_func(reason => {
      assert_unreached(reason);
    }));
  }, 'replaceTrack(): original track attachment stats present after replacing');

  promise_test(async t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let [tracks, streams] = await getUserMediaTracksAndStreams(2);
    t.add_cleanup(() => tracks.forEach(track => track.stop()));
    let sender = caller.addTrack(tracks[0], streams[0]);
    callee.addTrack(tracks[1], streams[1]);
    exchangeIceCandidates(caller, callee);
    await doSignalingHandshake(caller, callee);
    await listenToConnected(caller);
    let receiver = caller.getReceivers()[0];

    // Obtain inbound and outbound RTP stream stats on a full stats report.
    let fullReport = await caller.getStats();
    let outboundTrackStats = findStatsByTypeAndId(
        fullReport, 'track', sender.track.id);
    let outboundStats = findStatsByTypeAndMember(
        fullReport, 'outbound-rtp', 'trackId', outboundTrackStats.id);
    assert_true(outboundStats != null, 'Has stats for outbound RTP stream');
    let inboundTrackStats = findStatsByTypeAndId(
        fullReport, 'track', receiver.track.id);
    let inboundStats = findStatsByTypeAndMember(
        fullReport, 'inbound-rtp', 'trackId', inboundTrackStats.id);
    assert_true(inboundStats != null, 'Has stats for inbound RTP stream');

    // Perform stats selection algorithm with sender selector. The result should
    // contain the outbound-rtp but not the inbound-rtp.
    let senderReport = await sender.getStats();
    assert_true(senderReport.has(outboundStats.id));
    assert_false(senderReport.has(inboundStats.id));

    // Validate the stats graph, ensuring all stats objects are reachable and
    // valid from the outbound-rtp stats.
    validateStatsGraph(senderReport, senderReport.get(outboundStats.id));
    // Ensure that the stats graph contains some expected dictionaries.
    assert_equals(findStatsOfType(senderReport, 'track').length, 1,
        'senderReport should contain track stats');
    assert_equals(findStatsOfType(senderReport, 'transport').length, 1,
        'senderReport should contain transport stats');
    assert_equals(findStatsOfType(senderReport, 'candidate-pair').length, 1,
        'senderReport should contain candidate-pair stats');
    assert_equals(findStatsOfType(senderReport, 'local-candidate').length, 1,
        'senderReport should contain local-candidate stats');
    assert_equals(findStatsOfType(senderReport, 'remote-candidate').length, 1,
        'senderReport should contain remote-candidate stats');
  }, 'RTCRtpSender.getStats() contains only outbound-rtp and related stats');

  promise_test(async t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let [tracks, streams] = await getUserMediaTracksAndStreams(2);
    t.add_cleanup(() => tracks.forEach(track => track.stop()));
    let sender = caller.addTrack(tracks[0], streams[0]);
    callee.addTrack(tracks[1], streams[1]);
    exchangeIceCandidates(caller, callee);
    await doSignalingHandshake(caller, callee);
    await listenToConnected(caller);
    let receiver = caller.getReceivers()[0];

    // Obtain inbound and outbound RTP stream stats on a full stats report.
    let fullReport = await caller.getStats();
    let outboundTrackStats = findStatsByTypeAndId(
        fullReport, 'track', sender.track.id);
    let outboundStats = findStatsByTypeAndMember(
        fullReport, 'outbound-rtp', 'trackId', outboundTrackStats.id);
    assert_true(outboundStats != null, 'Has stats for outbound RTP stream');
    let inboundTrackStats = findStatsByTypeAndId(
        fullReport, 'track', receiver.track.id);
    let inboundStats = findStatsByTypeAndMember(
        fullReport, 'inbound-rtp', 'trackId', inboundTrackStats.id);
    assert_true(inboundStats != null, 'Has stats for inbound RTP stream');

    // Perform stats selection algorithm with receiver selector. The result
    // should contain the inbound-rtp but not the outbound-rtp.
    let receiverReport = await receiver.getStats();
    assert_true(receiverReport.has(inboundStats.id));
    assert_false(receiverReport.has(outboundStats.id));

    // Validate the stats graph, ensuring all stats objects are reachable and
    // valid from the outbound-rtp stats.
    validateStatsGraph(receiverReport, receiverReport.get(inboundStats.id));
    // Ensure that the stats graph contains some expected dictionaries.
    assert_equals(findStatsOfType(receiverReport, 'track').length, 1,
        'receiverReport should contain track stats');
    assert_equals(findStatsOfType(receiverReport, 'transport').length, 1,
        'receiverReport should contain transport stats');
    assert_equals(findStatsOfType(receiverReport, 'candidate-pair').length, 1,
        'receiverReport should contain candidate-pair stats');
    assert_equals(findStatsOfType(receiverReport, 'local-candidate').length, 1,
        'receiverReport should contain local-candidate stats');
    assert_equals(findStatsOfType(receiverReport, 'remote-candidate').length, 1,
        'receiverReport should contain remote-candidate stats');
  }, 'RTCRtpReceiver.getStats() contains only inbound-rtp and related stats');

  promise_test(async t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let [tracks, streams] = await getUserMediaTracksAndStreams(2);
    t.add_cleanup(() => tracks.forEach(track => track.stop()));
    let sender = caller.addTrack(tracks[0], streams[0]);
    callee.addTrack(tracks[1], streams[1]);
    exchangeIceCandidates(caller, callee);
    await doSignalingHandshake(caller, callee);
    await listenToIceConnected(caller);

    // Wait until RTCP has arrived so that it can not arrive between
    // the two get stats calls.
    await waitForRtpAndRtcpStats(caller);

    let senderReport = await sender.getStats();
    let trackReport = await caller.getStats(sender.track);

    // Verify the same stats objects are returned but don't compare each
    // individual metric because timestamps and counters could have gone up
    // between the two getStats() calls.
    senderReport.forEach(senderReportStat => {
      assert_true(trackReport.has(senderReportStat.id));
    });
    trackReport.forEach(trackReportStat => {
      assert_true(senderReport.has(trackReportStat.id));
    });
  }, 'RTCPeerConnection.getStats(sendingTrack) is the same as ' +
     'RTCRtpSender.getStats()');

  promise_test(async t => {
    const caller = new RTCPeerConnection();
    t.add_cleanup(() => caller.close());
    const callee = new RTCPeerConnection();
    t.add_cleanup(() => callee.close());
    let [tracks, streams] = await getUserMediaTracksAndStreams(2);
    t.add_cleanup(() => tracks.forEach(track => track.stop()));
    let sender = caller.addTrack(tracks[0], streams[0]);
    callee.addTrack(tracks[1], streams[1]);
    exchangeIceCandidates(caller, callee);
    await doSignalingHandshake(caller, callee);
    await listenToIceConnected(caller);
    let receiver = caller.getReceivers()[0];

    // Wait until RTCP has arrived so that it can not arrive between
    // the two get stats calls.
    await waitForRtpAndRtcpStats(caller);

    let receiverReport = await receiver.getStats();
    let trackReport = await caller.getStats(receiver.track);

    // Verify the same stats objects are returned but don't compare each
    // individual metric because timestamps and counters could have gone up
    // between the two getStats() calls.
    receiverReport.forEach(receiverReportStat => {
      assert_true(trackReport.has(receiverReportStat.id));
    });
    trackReport.forEach(trackReportStat => {
      assert_true(receiverReport.has(trackReportStat.id));
    });
  }, 'RTCPeerConnection.getStats(receivingTrack) is the same as ' +
     'RTCRtpReceiver.getStats()');

  promise_test(async t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let [tracks, streams] = await getUserMediaTracksAndStreams(1);
    t.add_cleanup(() => tracks.forEach(track => track.stop()));
    await promise_rejects_dom(t, 'InvalidAccessError', pc.getStats(tracks[0]));
  }, 'RTCPeerConnection.getStats(track) throws InvalidAccessError when there ' +
     'are zero senders or receivers for the track');

  promise_test(async t => {
    const pc = new RTCPeerConnection();
    t.add_cleanup(() => pc.close());
    let [tracks, streams] = await getUserMediaTracksAndStreams(2);
    t.add_cleanup(() => tracks.forEach(track => track.stop()));
    let sender1 = pc.addTrack(tracks[0], streams[0]);
    let sender2 = pc.addTrack(tracks[1], streams[1]);
    await sender2.replaceTrack(sender1.track);
    await promise_rejects_dom(t, 'InvalidAccessError', pc.getStats(sender1.track));
  }, 'RTCPeerConnection.getStats(track) throws InvalidAccessError when there ' +
     'are multiple senders for the track');

  // Helpers.

  function findStatsByTypeAndId(report, type, identifier) {
    return findStats(report, stats => {
      return stats.type == type && stats[type + 'Identifier'] == identifier;
    });
  }

  function findStatsByTypeAndMember(report, type, member, value) {
    return findStats(report, stats => {
      return stats.type == type && stats[member] == value;
    });
  }

  function findStats(report, findFunc) {
    for (let it = report.values(), n = it.next(); !n.done; n = it.next()) {
      if (findFunc(n.value))
        return n.value;
    }
    return null;
  }

  function findStatsOfType(report, type) {
    let stats = [];
    for (let it = report.values(), n = it.next(); !n.done; n = it.next()) {
      if (n.value.type == type)
        stats.push(n.value);
    }
    return stats;
  }

  // Explores the stats graph starting from |stat|, validating each stat
  // (validateRtcStats) and asserting that all stats of the report were visited.
  function validateStatsGraph(report, stat) {
    let visitedIds = new Set();
    validateStatsGraphRecursively(report, stat.id, visitedIds);
    assert_equals(visitedIds.size, report.size,
                  'Entire stats graph should have been explored.')
  }

  function validateStatsGraphRecursively(report, currentId, visitedIds) {
    if (visitedIds.has(currentId))
      return;
    visitedIds.add(currentId);
    assert_true(report.has(currentId), 'Broken reference.');
    let stat = report.get(currentId);
    validateRtcStats(report, stat);
    for (let member in stat) {
      if (member.endsWith('Id')) {
        validateStatsGraphRecursively(report, stat[member], visitedIds);
      } else if (member.endsWith('Ids')) {
        let ids = stat[member];
        for (let i = 0; i < ids.length; ++i) {
          validateStatsGraphRecursively(report, ids[i], visitedIds);
        }
      }
    }
  }
</script>