m3u8_test.js 19.3 KB
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
(function(window, undefined) {
  var
    //manifestController = this.manifestController,
    ParseStream = window.videojs.m3u8.ParseStream,
    parseStream,
    LineStream = window.videojs.m3u8.LineStream,
    lineStream,
    Parser = window.videojs.m3u8.Parser,
    parser;

  module('environment');

  test('is sane', function() {
    expect(1);
    ok(true);
  });

  /*
    Manifest controller
  */

  // module('manifest controller', {
  //   setup: function() {
  //     manifestController = new window.videojs.hls.ManifestController();
  //     this.vjsget = window.videojs.get;
  //     window.videojs.get = function(url, success) {
  //       success(window.brightcove_playlist_data);
  //     };
  //   },
  //   teardown: function() {
  //     window.videojs.get = this.vjsget;
  //   }
  // });

  // test('should create', function() {
  //   ok(manifestController);
  // });

  // test('should return a parsed object', function() {
  //   parser.push(window.brightcove_playlist_data);

  //   strictEqual(parser.manifest.playlists.length, 4, 'Has correct rendition count');
  //   strictEqual(parser.manifest.playlists[0].attributes.BANDWIDTH, 240000, 'First rendition index bandwidth is correct');
  //   strictEqual(parser.manifest.playlists[0].attributes['PROGRAM-ID'], 1, 'First rendition index program-id is correct');
  //   strictEqual(parser.manifest.playlists[0].attributes.RESOLUTION.width, 396, 'First rendition index resolution width is correct');
  //   strictEqual(parser.manifest.playlists[0].attributes.RESOLUTION.height, 224, 'First rendition index resolution height is correct');
  // });

  // test('should get a manifest from an external URL', function() {
  //   manifestController.loadManifest('http://example.com/16x9-master.m3u8',
  //                                   function(responseData) {
  //                                     ok(responseData);
  //                                   },
  //                                   function() {
  //                                     ok(false, 'does not error');
  //                                   },
  //                                   function() {});
  // });

  /*
    M3U8 Test Suite
  */

  module('LineStream', {
    setup: function() {
      lineStream = new LineStream();
    }
  });
  test('empty inputs produce no tokens', function() {
    var data = false;
    lineStream.on('data', function() {
      data = true;
    });
    lineStream.push('');
    ok(!data, 'no tokens were produced');
  });
  test('splits on newlines', function() {
    var lines = [];
    lineStream.on('data', function(line) {
      lines.push(line);
    });
    lineStream.push('#EXTM3U\nmovie.ts\n');

    strictEqual(2, lines.length, 'two lines are ready');
    strictEqual('#EXTM3U', lines.shift(), 'the first line is the first token');
    strictEqual('movie.ts', lines.shift(), 'the second line is the second token');
  });
  test('empty lines become empty strings', function() {
    var lines = [];
    lineStream.on('data', function(line) {
      lines.push(line);
    });
    lineStream.push('\n\n');

    strictEqual(2, lines.length, 'two lines are ready');
    strictEqual('', lines.shift(), 'the first line is empty');
    strictEqual('', lines.shift(), 'the second line is empty');
  });
  test('handles lines broken across appends', function() {
    var lines = [];
    lineStream.on('data', function(line) {
      lines.push(line);
    });
    lineStream.push('#EXTM');
    strictEqual(0, lines.length, 'no lines are ready');

    lineStream.push('3U\nmovie.ts\n');
    strictEqual(2, lines.length, 'two lines are ready');
    strictEqual('#EXTM3U', lines.shift(), 'the first line is the first token');
    strictEqual('movie.ts', lines.shift(), 'the second line is the second token');
  });
  test('stops sending events after deregistering', function() {
    var
      temporaryLines = [],
      temporary = function(line) {
        temporaryLines.push(line);
      },
      permanentLines = [],
      permanent = function(line) {
        permanentLines.push(line);
      };

    lineStream.on('data', temporary);
    lineStream.on('data', permanent);
    lineStream.push('line one\n');
    strictEqual(temporaryLines.length, permanentLines.length, 'both callbacks receive the event');

    ok(lineStream.off('data', temporary), 'a listener was removed');
    lineStream.push('line two\n');
    strictEqual(1, temporaryLines.length, 'no new events are received');
    strictEqual(2, permanentLines.length, 'new events are still received');
  });

  module('ParseStream', {
    setup: function() {
      lineStream = new LineStream();
      parseStream = new ParseStream();
      lineStream.pipe(parseStream);
    }
  });
  test('parses comment lines', function() {
    var
      manifest = '# a line that starts with a hash mark without "EXT" is a comment\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'comment', 'the type is comment');
    strictEqual(element.text,
          manifest.slice(1, manifest.length - 1),
          'the comment text is parsed');
  });
  test('parses uri lines', function() {
    var
      manifest = 'any non-blank line that does not start with a hash-mark is a URI\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'uri', 'the type is uri');
    strictEqual(element.uri,
          manifest.substring(0, manifest.length - 1),
          'the uri text is parsed');
  });
  test('parses unknown tag types', function() {
    var
      manifest = '#EXT-X-EXAMPLE-TAG:some,additional,stuff\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the type is tag');
    strictEqual(element.data,
          manifest.slice(4, manifest.length - 1),
          'unknown tag data is preserved');
  });

  // #EXTM3U
  test('parses #EXTM3U tags', function() {
    var
      manifest = '#EXTM3U\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'm3u', 'the tag type is m3u');
  });

  // #EXTINF
  test('parses minimal #EXTINF tags', function() {
    var
      manifest = '#EXTINF\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'inf', 'the tag type is inf');
  });
  test('parses #EXTINF tags with durations', function() {
    var
      manifest = '#EXTINF:15\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'inf', 'the tag type is inf');
    strictEqual(element.duration, 15, 'the duration is parsed');
    ok(!('title' in element), 'no title is parsed');

    manifest = '#EXTINF:21,\n';
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'inf', 'the tag type is inf');
    strictEqual(element.duration, 21, 'the duration is parsed');
    ok(!('title' in element), 'no title is parsed');
  });
  test('parses #EXTINF tags with a duration and title', function() {
    var
      manifest = '#EXTINF:13,Does anyone really use the title attribute?\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'inf', 'the tag type is inf');
    strictEqual(element.duration, 13, 'the duration is parsed');
    strictEqual(element.title,
          manifest.substring(manifest.indexOf(',') + 1, manifest.length - 1),
          'the title is parsed');
  });

  // #EXT-X-TARGETDURATION
  test('parses minimal #EXT-X-TARGETDURATION tags', function() {
    var
      manifest = '#EXT-X-TARGETDURATION\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'targetduration', 'the tag type is targetduration');
    ok(!('duration' in element), 'no duration is parsed');
  });
  test('parses #EXT-X-TARGETDURATION with duration', function() {
    var
      manifest = '#EXT-X-TARGETDURATION:47\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'targetduration', 'the tag type is targetduration');
    strictEqual(element.duration, 47, 'the duration is parsed');
  });

  // #EXT-X-VERSION
  test('parses minimal #EXT-X-VERSION tags', function() {
    var
      manifest = '#EXT-X-VERSION:\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'version', 'the tag type is version');
    ok(!('version' in element), 'no version is present');
  });
  test('parses #EXT-X-VERSION with a version', function() {
    var
      manifest = '#EXT-X-VERSION:99\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'version', 'the tag type is version');
    strictEqual(element.version, 99, 'the version is parsed');
  });

  // #EXT-X-MEDIA-SEQUENCE
  test('parses minimal #EXT-X-MEDIA-SEQUENCE tags', function() {
    var
      manifest = '#EXT-X-MEDIA-SEQUENCE\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'media-sequence', 'the tag type is media-sequence');
    ok(!('number' in element), 'no number is present');
  });
  test('parses #EXT-X-MEDIA-SEQUENCE with sequence numbers', function() {
    var
      manifest = '#EXT-X-MEDIA-SEQUENCE:109\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'media-sequence', 'the tag type is media-sequence');
    ok(element.number, 109, 'the number is parsed');
  });

  // #EXT-X-PLAYLIST-TYPE
  test('parses minimal #EXT-X-PLAYLIST-TYPE tags', function() {
    var
      manifest = '#EXT-X-PLAYLIST-TYPE:\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'playlist-type', 'the tag type is playlist-type');
    ok(!('playlistType' in element), 'no playlist type is present');
  });
  test('parses #EXT-X-PLAYLIST-TYPE with mutability info', function() {
    var
      manifest = '#EXT-X-PLAYLIST-TYPE:EVENT\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'playlist-type', 'the tag type is playlist-type');
    strictEqual(element.playlistType, 'EVENT', 'the playlist type is EVENT');

    manifest = '#EXT-X-PLAYLIST-TYPE:VOD\n';
    lineStream.push(manifest);
    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'playlist-type', 'the tag type is playlist-type');
    strictEqual(element.playlistType, 'VOD', 'the playlist type is VOD');

    manifest = '#EXT-X-PLAYLIST-TYPE:nonsense\n';
    lineStream.push(manifest);
    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'playlist-type', 'the tag type is playlist-type');
    strictEqual(element.playlistType, 'nonsense', 'the playlist type is parsed');
  });

  // #EXT-X-BYTERANGE
  test('parses minimal #EXT-X-BYTERANGE tags', function() {
    var
      manifest = '#EXT-X-BYTERANGE\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'byterange', 'the tag type is byterange');
    ok(!('length' in element), 'no length is present');
    ok(!('offset' in element), 'no offset is present');
  });
  test('parses #EXT-X-BYTERANGE with length and offset', function() {
    var
      manifest = '#EXT-X-BYTERANGE:45\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'byterange', 'the tag type is byterange');
    strictEqual(element.length, 45, 'length is parsed');
    ok(!('offset' in element), 'no offset is present');

    manifest = '#EXT-X-BYTERANGE:108@16\n';
    lineStream.push(manifest);
    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'byterange', 'the tag type is byterange');
    strictEqual(element.length, 108, 'length is parsed');
    strictEqual(element.offset, 16, 'offset is parsed');
  });

  // #EXT-X-ALLOW-CACHE
  test('parses minimal #EXT-X-ALLOW-CACHE tags', function() {
    var
      manifest = '#EXT-X-ALLOW-CACHE:\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'allow-cache', 'the tag type is allow-cache');
    ok(!('allowed' in element), 'no allowed is present');
  });
  test('parses valid #EXT-X-ALLOW-CACHE tags', function() {
    var
      manifest = '#EXT-X-ALLOW-CACHE:YES\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'allow-cache', 'the tag type is allow-cache');
    ok(element.allowed, 'allowed is parsed');

    manifest = '#EXT-X-ALLOW-CACHE:NO\n';
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'allow-cache', 'the tag type is allow-cache');
    ok(!element.allowed, 'allowed is parsed');
  });
  // #EXT-X-STREAM-INF
  test('parses minimal #EXT-X-STREAM-INF tags', function() {
    var
      manifest = '#EXT-X-STREAM-INF\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'stream-inf', 'the tag type is stream-inf');
    ok(!('attributes' in element), 'no attributes are present');
  });
  test('parses #EXT-X-STREAM-INF with common attributes', function() {
    var
      manifest = '#EXT-X-STREAM-INF:BANDWIDTH=14400\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'stream-inf', 'the tag type is stream-inf');
    strictEqual(element.attributes.BANDWIDTH, 14400, 'bandwidth is parsed');

    manifest = '#EXT-X-STREAM-INF:PROGRAM-ID=7\n';
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'stream-inf', 'the tag type is stream-inf');
    strictEqual(element.attributes['PROGRAM-ID'], 7, 'program-id is parsed');

    manifest = '#EXT-X-STREAM-INF:RESOLUTION=396x224\n';
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'stream-inf', 'the tag type is stream-inf');
    strictEqual(element.attributes.RESOLUTION.width, 396, 'width is parsed');
    strictEqual(element.attributes.RESOLUTION.height, 224, 'heigth is parsed');
  });
  test('parses #EXT-X-STREAM-INF with arbitrary attributes', function() {
    var
      manifest = '#EXT-X-STREAM-INF:NUMERIC=24,ALPHA=Value,MIXED=123abc\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'stream-inf', 'the tag type is stream-inf');
    strictEqual(element.attributes.NUMERIC, '24', 'numeric attributes are parsed');
    strictEqual(element.attributes.ALPHA, 'Value', 'alphabetic attributes are parsed');
    strictEqual(element.attributes.MIXED, '123abc', 'mixed attributes are parsed');
  });
  // #EXT-X-ENDLIST
  test('parses #EXT-X-ENDLIST tags', function() {
    var
      manifest = '#EXT-X-ENDLIST\n',
      element;
    parseStream.on('data', function(elem) {
      element = elem;
    });
    lineStream.push(manifest);

    ok(element, 'an event was triggered');
    strictEqual(element.type, 'tag', 'the line type is tag');
    strictEqual(element.tagType, 'endlist', 'the tag type is stream-inf');
  });

  test('ignores empty lines', function() {
    var
      manifest = '\n',
      event = false;
    parseStream.on('data', function() {
      event = true;
    });
    lineStream.push(manifest);

    ok(!event, 'no event is triggered');
  });

  module('m3u8 parser', {
    setup: function() {
      parser = new Parser();
    }
  });

  test('should create a parser', function() {
    notStrictEqual(parser, undefined, 'parser is defined');
  });

  module('m3u8s');

  test('parses the example manifests as expected', function() {
    var key;
    for (key in window.manifests) {
      if (window.expected[key]) {
        parser = new Parser();
        parser.push(window.manifests[key]);
        deepEqual(parser.manifest,
                  window.expected[key],
                  key + '.m3u8 was parsed correctly');
      }
    }
  });

})(window, window.console);