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 (409f3966645a)

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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({
    bug: "1337525",
    title: "webRtc Stats composition and sanity"
  });
var statsExpectedByType = {
  "inbound-rtp": {
    expected: ["id", "timestamp", "type", "ssrc", "isRemote", "mediaType",
      "kind", "packetsReceived", "packetsLost", "bytesReceived", "jitter",],
    optional: ["roundTripTime", "remoteId", "nackCount",],
    localVideoOnly: ["discardedPackets", "framerateStdDev", "framerateMean",
      "bitrateMean", "bitrateStdDev", "firCount", "pliCount", "framesDecoded",],
    unimplemented: ["mediaTrackId", "transportId", "codecId",
      "packetsDiscarded", "associateStatsId",
      "sliCount", "qpSum", "packetsRepaired", "fractionLost",
      "burstPacketsLost", "burstLossCount", "burstDiscardCount",
      "gapDiscardRate", "gapLossRate",],
    deprecated: ["mozRtt"],
  },
  "outbound-rtp": {
    expected: ["id", "timestamp", "type", "ssrc", "isRemote", "mediaType",
      "kind", "packetsSent", "bytesSent", "remoteId",],
    optional: ["remoteId", "nackCount",],
    localVideoOnly: ["droppedFrames", "bitrateMean", "bitrateStdDev",
      "framerateMean", "framerateStdDev", "framesEncoded", "firCount",
      "pliCount",],
    unimplemented: ["mediaTrackId", "transportId", "codecId",
      "sliCount", "qpSum", "targetBitrate",],
    deprecated: [],
  },
  "csrc": { skip: true },
  "codec": { skip: true },
  "peer-connection": { skip: true },
  "data-channel": { skip: true },
  "track": { skip: true },
  "transport": { skip: true },
  "candidate-pair": {
    expected: ["id", "timestamp", "type",
      "transportId", "localCandidateId", "remoteCandidateId", "state",
      "priority", "nominated", "writable", "readable",
      "bytesSent", "bytesReceived",
      "lastPacketSentTimestamp", "lastPacketReceivedTimestamp",
      "componentId",],
    optional: ["selected",],
    unimplemented: ["totalRoundTripTime", "currentRoundTripTime",
      "availableOutgoingBitrate", "availableIncomingBitrate",
      "requestsReceived", "requestsSent", "responsesReceived",
      "responsesSent", "retransmissionsReceived", "retransmissionsSent",
      "consentRequestsSent",],
    deprecated: [],
  },
  "local-candidate": {
    expected: ["id", "timestamp", "type", "componentId", "ipAddress",
               "transport", "mozLocalTransport", "portNumber",
               "candidateType"],
    optional: [],
    unimplemented: ["address", "port", "networkType", "protocol",
                    "relayProtocol", "priority", "url"],
    deprecated: ["candidateId"],
  },
  "remote-candidate": {
    expected: ["id", "timestamp", "type", "componentId", "ipAddress",
               "transport", "portNumber", "candidateType"],
    optional: [],
    unimplemented: ["address", "port", "networkType", "protocol",
                    "relayProtocol", "priority", "url"],
    deprecated: ["candidateId"],
  },
  "certificate": { skip: true },
};
["in", "out"].forEach(pre => {
  let s = statsExpectedByType[pre + "bound-rtp"];
  s.optional = [...s.optional, ...s.localVideoOnly];
});

//
//  Checks that the fields in a report conform to the expectations in
// statExpectedByType
//
var checkExpectedFields = report => report.forEach(stat => {
  let expectations = statsExpectedByType[stat.type];
  ok(expectations, "Stats type " + stat.type + " was expected");
  // If the type is not expected or if it is flagged for skipping continue to
  // the next
  if (!expectations || expectations.skip) {
    return;
  }
  // Check that all required fields exist
  expectations.expected.forEach(field => {
    ok(field in stat, "Expected stat field " + stat.type + "." + field
      + " exists");
  });
  // Check that each field is either expected or optional
  let allowed = [...expectations.expected, ...expectations.optional];
  Object.keys(stat).forEach(field => {
    ok(allowed.includes(field), "Stat field " + stat.type + "." + field
      + " is allowed");
  });

  //
  // Ensure that unimplemented fields are not implemented
  //   note: if a field is implemented it should be moved to expected or
  //   optional.
  //
  expectations.unimplemented.forEach(field => {
    ok(!Object.keys(stat).includes(field), "Unimplemented field " + stat.type
      + "." + field + " does not exist.");
  });

  //
  // Ensure that all deprecated fields are not present
  //
  expectations.deprecated.forEach(field => {
    ok(!Object.keys(stat).includes(field), "Deprecated field " + stat.type
      + "." + field + " does not exist.");
  });
});

var pedanticChecks = report => {
  report.forEach((statObj, mapKey) => {
    let tested = {};
    // Record what fields get tested.
    // To access a field foo without marking it as tested use stat.inner.foo
    let stat = new Proxy(statObj, {
      get(stat, key) {
        if (key == "inner") return stat;
        tested[key] = true;
        return stat[key];
      }
    });

    let expectations = statsExpectedByType[stat.type];

    if (expectations.skip) {
      return;
    }

    // All stats share the following attributes inherited from RTCStats
    is(stat.id, mapKey, stat.type + ".id is the same as the report key.");

    // timestamp
    ok(stat.timestamp >= 0, stat.type + ".timestamp is not less than 0");

    //
    // RTCStreamStats attributes with common behavior
    //
    // inbound-rtp and outbound-rtp inherit from RTCStreamStats
    if (["inbound-rtp", "outbound-rtp"].includes(stat.type)) {
      //
      // Common RTCStreamStats fields
      //

      // SSRC
      ok(stat.ssrc, stat.type + ".ssrc has a value");

      // isRemote
      ok(stat.isRemote !== undefined, stat.type + ".isRemote exists.");

      // kind
      ok(["audio", "video"].includes(stat.kind),
        stat.type + ".kind is 'audio' or 'video'");

      // mediaType, renamed to kind but remains for backward compability.
      ok(["audio", "video"].includes(stat.mediaType),
        stat.type + ".mediaType is 'audio' or 'video'");

      ok(stat.kind == stat.mediaType, "kind equals legacy mediaType");

      // remote id
      if (stat.remoteId) {
        ok(report.has(stat.remoteId), "remoteId exists in report.");
        is(report.get(stat.remoteId).ssrc, stat.ssrc,
          "remote ssrc and local ssrc match.");
        is(report.get(stat.remoteId).remoteId, stat.id,
          "remote object has local object as it's own remote object.");
      }

      // nackCount
      if (!stat.inner.isRemote) {
        ok(stat.nackCount >= 0, stat.type + ".nackCount is sane.");
      } else {
        is(stat.nackCount, undefined, stat.type
          + ".nackCount is only set when isRemote is false");
      }

      if (!stat.inner.isRemote && stat.inner.kind == "video") {
        // firCount
        ok(stat.firCount >= 0 && stat.firCount < 100,
          stat.type + ".firCount is a sane number for a short test. value="
          + stat.firCount);

        // pliCount
        ok(stat.pliCount >= 0 && stat.pliCount < 200,
          stat.type + ".pliCount is a sane number for a short test. value="
          + stat.pliCount);
      }
    }

    if (stat.type == "inbound-rtp") {
      //
      // Required fields
      //

      // packetsReceived
      ok(stat.packetsReceived >= 0
        && stat.packetsReceived < 10 ** 5,
        stat.type + ".packetsReceived is a sane number for a short test. value="
        + stat.packetsReceived);

      // bytesReceived
      ok(stat.bytesReceived >= 0
        && stat.bytesReceived < 10 ** 9, // Not a magic number, just a guess
        stat.type + ".bytesReceived is a sane number for a short test. value="
        + stat.bytesReceived);

      // packetsLost
      ok(stat.packetsLost < 100,
        stat.type + ".packetsLost is a sane number for a short test. value="
        + stat.packetsLost);

      // This should be much lower for audio, TODO: Bug 1330575
      let expectedJitter = stat.kind == "video" ? 0.5 : 1;
      // jitter
      ok(stat.jitter < expectedJitter,
        stat.type + ".jitter is sane number for a local only test. value="
        + stat.jitter);

      // packetsDiscarded
      // special exception for, TODO: Bug 1335967
      // if (!stat.inner.isRemote && stat.discardedPackets !== undefined) {
      //   ok(stat.packetsDiscarded < 100, stat.type
      //     + ".packetsDiscarded is a sane number for a short test. value="
      //     + stat.packetsDiscarded);
      // }
      // if (stat.packetsDiscarded !== undefined) {
      //   ok(!stat.inner.isRemote,
      //     stat.type + ".packetsDiscarded is only set when isRemote is "
      //     + "false");
      // }

      //
      // Optional fields
      //

      // roundTripTime
      if (stat.inner.isRemote) {
        ok(stat.roundTripTime >= 0, stat.type + ".roundTripTime is sane with" +
          "value of:" + stat.roundTripTime);
      } else {
        is(stat.roundTripTime, undefined, stat.type
          + ".roundTripTime is only set when isRemote is true");
      }

      //
      // Local video only stats
      //
      if (stat.inner.isRemote || stat.inner.kind != "video") {
        expectations.localVideoOnly.forEach(field => {
          if (stat.inner.isRemote) {
            ok(stat[field] === undefined, stat.type + " does not have field "
              + field + " when isRemote is true");
          } else { // kind != video
            ok(stat[field] === undefined, stat.type + " does not have field "
              + field + " when kind is not 'video'");
          }
        });
      } else {
        expectations.localVideoOnly.forEach(field => {
          ok(stat.inner[field] !== undefined, stat.type + " has field " + field
            + " when kind is video");
        });
        // discardedPackets
        ok(stat.discardedPackets < 100, stat.type
          + ".discardedPackets is a sane number for a short test. value="
          + stat.discardedPackets);
        // framesDecoded
        ok(stat.framesDecoded > 0 && stat.framesDecoded < 1000000, stat.type
          + ".framesDecoded is a sane number for a short test. value="
          + stat.framesDecoded);
        // bitrateMean
        // special exception, TODO: Bug 1341533
        if (stat.bitrateMean !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.bitrateMean >= 0 && stat.bitrateMean < 2 ** 25,
          //   stat.type + ".bitrateMean is sane. value="
          //   + stat.bitrateMean);
        }

        // bitrateStdDev
        // special exception, TODO Bug 1341533
        if (stat.bitrateStdDev !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.bitrateStdDev >= 0 && stat.bitrateStdDev < 2 ** 25,
          //   stat.type + ".bitrateStdDev is sane. value="
          //   + stat.bitrateStdDev);
        }

        // framerateMean
        // special exception, TODO: Bug 1341533
        if (stat.framerateMean !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.framerateMean >= 0 && stat.framerateMean < 120,
          //   stat.type + ".framerateMean is sane. value="
          //   + stat.framerateMean);
        }

        // framerateStdDev
        // special exception, TODO: Bug 1341533
        if (stat.framerateStdDev !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.framerateStdDev >= 0 && stat.framerateStdDev < 120,
          //   stat.type + ".framerateStdDev is sane. value="
          //   + stat.framerateStdDev);
        }
      }
    } else if (stat.type == "outbound-rtp") {
      //
      // Required fields
      //

      // packetsSent
      ok(stat.packetsSent > 0 && stat.packetsSent < 10000,
        stat.type + ".packetsSent is a sane number for a short test. value="
        + stat.packetsSent);

      // bytesSent
      ok(stat.bytesSent, stat.type + ".bytesSent has a value."
        + " Value not expected to be sane, bug 1339104. value="
        + stat.bytesSent);

      //
      // Optional fields
      //

      //
      // Local video only stats
      //
      if (stat.inner.isRemote || stat.inner.kind != "video") {
        expectations.localVideoOnly.forEach(field => {
          if (stat.inner.isRemote) {
            ok(stat[field] === undefined, stat.type + " does not have field "
              + field + " when isRemote is true");
          } else { // kind != video
            ok(stat[field] === undefined, stat.type + " does not have field "
              + field + " when kind is not 'video'");
          }
        });
      } else {
        expectations.localVideoOnly.forEach(field => {
          ok(stat.inner[field] !== undefined, stat.type + " has field " + field
            + " when kind is video and isRemote is false");
        });

        // bitrateMean
        if (stat.bitrateMean !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.bitrateMean >= 0 && stat.bitrateMean < 2 ** 25,
          //   stat.type + ".bitrateMean is sane. value="
          //   + stat.bitrateMean);
        }

        // bitrateStdDev
        if (stat.bitrateStdDev !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.bitrateStdDev >= 0 && stat.bitrateStdDev < 2 ** 25,
          //   stat.type + ".bitrateStdDev is sane. value="
          //   + stat.bitrateStdDev);
        }

        // framerateMean
        if (stat.framerateMean !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.framerateMean >= 0 && stat.framerateMean < 120,
          //   stat.type + ".framerateMean is sane. value="
          //   + stat.framerateMean);
        }

        // framerateStdDev
        if (stat.framerateStdDev !== undefined) {
          // TODO: uncomment when Bug 1341533 lands
          // ok(stat.framerateStdDev >= 0 && stat.framerateStdDev < 120,
          //   stat.type + ".framerateStdDev is sane. value="
          //   + stat.framerateStdDev);
        }

        // droppedFrames
        ok(stat.droppedFrames >= 0,
          stat.type + ".droppedFrames is not negative. value="
          + stat.droppedFrames);

        // framesEncoded
        ok(stat.framesEncoded >= 0 && stat.framesEncoded < 100000, stat.type
          + ".framesEncoded is a sane number for a short test. value="
          + stat.framesEncoded);
      }
    } else if (stat.type == "candidate-pair") {
      info("candidate-pair is: " + JSON.stringify(stat));
      //
      // Required fields
      //

      // transportId
      ok(stat.transportId,
        stat.type + ".transportId has a value. value="
        + stat.transportId);

      // localCandidateId
      ok(stat.localCandidateId,
        stat.type + ".localCandidateId has a value. value="
        + stat.localCandidateId);

      // remoteCandidateId
      ok(stat.remoteCandidateId,
        stat.type + ".remoteCandidateId has a value. value="
        + stat.remoteCandidateId);

      // priority
      ok(stat.priority,
        stat.type + ".priority has a value. value="
        + stat.priority);

      // readable
      ok(stat.readable,
        stat.type + ".readable is true. value="
        + stat.readable);

      // writable
      ok(stat.writable,
        stat.type + ".writable is true. value="
        + stat.writable);

      // state
      if (stat.state == "succeeded" &&
          stat.selected !== undefined &&
          stat.selected) {
        info("candidate-pair state is succeeded and selected is true");
        // nominated
        ok(stat.nominated,
          stat.type + ".nominated is true. value="
          + stat.nominated);

        // bytesSent
        ok(stat.bytesSent > 5000,
          stat.type + ".bytesSent is a sane number (>5,000) for a short test. value="
          + stat.bytesSent);

        // bytesReceived
        ok(stat.bytesReceived > 5000,
          stat.type + ".bytesReceived is a sane number (>5,000) for a short test. value="
          + stat.bytesReceived);

        // lastPacketSentTimestamp
        ok(stat.lastPacketSentTimestamp,
          stat.type + ".lastPacketSentTimestamp has a value. value="
          + stat.lastPacketSentTimestamp);

        // lastPacketReceivedTimestamp
        ok(stat.lastPacketReceivedTimestamp,
          stat.type + ".lastPacketReceivedTimestamp has a value. value="
          + stat.lastPacketReceivedTimestamp);

      } else {
        info("candidate-pair is _not_ both state == succeeded and selected");
        // nominated
        ok(stat.nominated !== undefined,
          stat.type + ".nominated exists. value="
          + stat.nominated);
        ok(stat.bytesSent !== undefined,
          stat.type + ".bytesSent exists. value="
          + stat.bytesSent);
        ok(stat.bytesReceived !== undefined,
          stat.type + ".bytesReceived exists. value="
          + stat.bytesReceived);
        ok(stat.lastPacketSentTimestamp !== undefined,
          stat.type + ".lastPacketSentTimestamp exists. value="
          + stat.lastPacketSentTimestamp);
        ok(stat.lastPacketReceivedTimestamp !== undefined,
          stat.type + ".lastPacketReceivedTimestamp exists. value="
          + stat.lastPacketReceivedTimestamp);
      }


      //
      // Optional fields
      //
      // selected
      ok(stat.selected === undefined ||
         ((stat.state == "succeeded" && stat.selected) ||
          !stat.selected),
        stat.type + ".selected is undefined, true when state is succeeded, "
        + "or false. value="
        + stat.selected);

    } else if (stat.type == "local-candidate" || stat.type == "remote-candidate") {
      info(`candidate is ${JSON.stringify(stat)}`);

      // componentId
      ok(stat.componentId, `${stat.type} has componentId. value=${stat.componentId}`);

      // ipAddress
      ok(stat.ipAddress, `${stat.type} has ipAddress. value=${stat.ipAddress}`);

      // transport
      ok(stat.transport, `${stat.type} has transport. value=${stat.transport}`);

      if (stat.type == "local-candidate") {
        // mozLocalTransport
        ok(stat.mozLocalTransport, `${stat.type} has mozLocalTransport. value=${stat.mozLocalTransport}`);
      }

      // portNumber
      ok(stat.portNumber >= 0, `${stat.type} has portNumber >= 0. value=${stat.portNumber}`);
      ok(stat.portNumber <= 65535, `${stat.type} has portNumber <= 65535. value=${stat.portNumber}`);

      // candidateType
      ok(stat.candidateType, `${stat.type} has candidateType. value=${stat.candidateType}`);

    }

    //
    // Ensure everything was tested
    //
    [...expectations.expected, ...expectations.optional].forEach(field => {
      ok(Object.keys(tested).includes(field), stat.type + "." + field
        + " was tested.");
    });
  });
}

// This MUST be run after PC_*_WAIT_FOR_MEDIA_FLOW to ensure that we have RTP
// before checking for RTCP.
var waitForSyncedRtcp = async pc => {
  // Ensures that RTCP is present
  let ensureSyncedRtcp = async () => {
    let stats = await pc.getStats();
    for (let [k, v] of stats) {
      if (v.type.endsWith("bound-rtp") && !v.remoteId) {
        throw new Error(v.id + " is missing remoteId: "
          + JSON.stringify(v));
      }
      if (v.type == "inbound-rtp" && v.isRemote == true
          && v.roundTripTime === undefined) {
        throw new Error(v.id + " is missing roundTripTime: "
          + JSON.stringify(v));
      }
    }
    return stats;
  }
  const waitPeriod = 500;
  const maxTime = 15000;
  for (let totalTime = maxTime; totalTime > 0; totalTime -= waitPeriod) {
    try {
      return await ensureSyncedRtcp();
    } catch (e) {
      info(e);
      await wait(waitPeriod);
    }
  }
  throw new Error("Waiting for synced RTCP timed out after at least " + maxTime
    + "ms");
}

var PC_LOCAL_TEST_LOCAL_STATS = test => {
  return waitForSyncedRtcp(test.pcLocal).then(stats => {
    checkExpectedFields(stats);
    pedanticChecks(stats);
  });
}

var PC_REMOTE_TEST_REMOTE_STATS = test => {
  return waitForSyncedRtcp(test.pcRemote).then(stats => {
    checkExpectedFields(stats);
    pedanticChecks(stats);
  });
}

var test;
runNetworkTest(function (options) {
  test = new PeerConnectionTest(options);

  test.chain.insertAfter("PC_LOCAL_WAIT_FOR_MEDIA_FLOW",
    [PC_LOCAL_TEST_LOCAL_STATS]);

  test.chain.insertAfter("PC_REMOTE_WAIT_FOR_MEDIA_FLOW",
    [PC_REMOTE_TEST_REMOTE_STATS]);

  test.setMediaConstraints([{audio: true}, {video: true}],
                           [{audio: true}, {video: true}]);
  test.run();
});
</script>
</pre>
</body>
</html>