playlist.test.js 28.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 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 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
import Playlist from '../src/playlist';
import PlaylistLoader from '../src/playlist-loader';
import QUnit from 'qunit';
import xhrFactory from '../src/xhr';
import { useFakeEnvironment } from './test-helpers';

QUnit.module('Playlist Duration');

QUnit.test('total duration for live playlists is Infinity', function(assert) {
  let duration = Playlist.duration({
    segments: [{
      duration: 4,
      uri: '0.ts'
    }]
  });

  assert.equal(duration, Infinity, 'duration is infinity');
});

QUnit.module('Playlist Interval Duration');

QUnit.test('accounts for non-zero starting VOD media sequences', function(assert) {
  let duration = Playlist.duration({
    mediaSequence: 10,
    endList: true,
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      duration: 10,
      uri: '1.ts'
    }, {
      duration: 10,
      uri: '2.ts'
    }, {
      duration: 10,
      uri: '3.ts'
    }]
  });

  assert.equal(duration, 4 * 10, 'includes only listed segments');
});

QUnit.test('uses timeline values when available', function(assert) {
  let duration = Playlist.duration({
    mediaSequence: 0,
    endList: true,
    segments: [{
      start: 0,
      uri: '0.ts'
    }, {
      duration: 10,
      end: 2 * 10 + 2,
      uri: '1.ts'
    }, {
      duration: 10,
      end: 3 * 10 + 2,
      uri: '2.ts'
    }, {
      duration: 10,
      end: 4 * 10 + 2,
      uri: '3.ts'
    }]
  }, 4);

  assert.equal(duration, 4 * 10 + 2, 'used timeline values');
});

QUnit.test('works when partial timeline information is available', function(assert) {
  let duration = Playlist.duration({
    mediaSequence: 0,
    endList: true,
    segments: [{
      start: 0,
      uri: '0.ts'
    }, {
      duration: 9,
      uri: '1.ts'
    }, {
      duration: 10,
      uri: '2.ts'
    }, {
      duration: 10,
      start: 30.007,
      end: 40.002,
      uri: '3.ts'
    }, {
      duration: 10,
      end: 50.0002,
      uri: '4.ts'
    }]
  }, 5);

  assert.equal(duration, 50.0002, 'calculated with mixed intervals');
});

QUnit.test('uses timeline values for the expired duration of live playlists',
function(assert) {
  let playlist = {
    mediaSequence: 12,
    segments: [{
      duration: 10,
      end: 120.5,
      uri: '0.ts'
    }, {
      duration: 9,
      uri: '1.ts'
    }]
  };
  let duration;

  duration = Playlist.duration(playlist, playlist.mediaSequence);
  assert.equal(duration, 110.5, 'used segment end time');
  duration = Playlist.duration(playlist, playlist.mediaSequence + 1);
  assert.equal(duration, 120.5, 'used segment end time');
  duration = Playlist.duration(playlist, playlist.mediaSequence + 2);
  assert.equal(duration, 120.5 + 9, 'used segment end time');
});

QUnit.test('looks outside the queried interval for live playlist timeline values',
function(assert) {
  let playlist = {
    mediaSequence: 12,
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      duration: 9,
      end: 120.5,
      uri: '1.ts'
    }]
  };
  let duration;

  duration = Playlist.duration(playlist, playlist.mediaSequence);
  assert.equal(duration, 120.5 - 9 - 10, 'used segment end time');
});

QUnit.test('ignores discontinuity sequences later than the end', function(assert) {
  let duration = Playlist.duration({
    mediaSequence: 0,
    discontinuityStarts: [1, 3],
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      discontinuity: true,
      duration: 9,
      uri: '1.ts'
    }, {
      duration: 10,
      uri: '2.ts'
    }, {
      discontinuity: true,
      duration: 10,
      uri: '3.ts'
    }]
  }, 2);

  assert.equal(duration, 19, 'excluded the later segments');
});

QUnit.test('handles trailing segments without timeline information', function(assert) {
  let duration;
  let playlist = {
    mediaSequence: 0,
    endList: true,
    segments: [{
      start: 0,
      end: 10.5,
      uri: '0.ts'
    }, {
      duration: 9,
      uri: '1.ts'
    }, {
      duration: 10,
      uri: '2.ts'
    }, {
      start: 29.45,
      end: 39.5,
      uri: '3.ts'
    }]
  };

  duration = Playlist.duration(playlist, 3);
  assert.equal(duration, 29.45, 'calculated duration');

  duration = Playlist.duration(playlist, 2);
  assert.equal(duration, 19.5, 'calculated duration');
});

QUnit.test('uses timeline intervals when segments have them', function(assert) {
  let duration;
  let playlist = {
    mediaSequence: 0,
    segments: [{
      start: 0,
      end: 10,
      uri: '0.ts'
    }, {
      duration: 9,
      uri: '1.ts'
    }, {
      start: 20.1,
      end: 30.1,
      duration: 10,
      uri: '2.ts'
    }]
  };

  duration = Playlist.duration(playlist, 2);
  assert.equal(duration, 20.1, 'used the timeline-based interval');

  duration = Playlist.duration(playlist, 3);
  assert.equal(duration, 30.1, 'used the timeline-based interval');
});

QUnit.test('counts the time between segments as part of the earlier segment\'s duration',
function(assert) {
  let duration = Playlist.duration({
    mediaSequence: 0,
    endList: true,
    segments: [{
      start: 0,
      end: 10,
      uri: '0.ts'
    }, {
      start: 10.1,
      end: 20.1,
      duration: 10,
      uri: '1.ts'
    }]
  }, 1);

  assert.equal(duration, 10.1, 'included the segment gap');
});

QUnit.test('accounts for discontinuities', function(assert) {
  let duration = Playlist.duration({
    mediaSequence: 0,
    endList: true,
    discontinuityStarts: [1],
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      discontinuity: true,
      duration: 10,
      uri: '1.ts'
    }]
  }, 2);

  assert.equal(duration, 10 + 10, 'handles discontinuities');
});

QUnit.test('a non-positive length interval has zero duration', function(assert) {
  let playlist = {
    mediaSequence: 0,
    discontinuityStarts: [1],
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      discontinuity: true,
      duration: 10,
      uri: '1.ts'
    }]
  };

  assert.equal(Playlist.duration(playlist, 0), 0, 'zero-length duration is zero');
  assert.equal(Playlist.duration(playlist, 0, false), 0, 'zero-length duration is zero');
  assert.equal(Playlist.duration(playlist, -1), 0, 'negative length duration is zero');
});

QUnit.module('Playlist Seekable');

QUnit.test('calculates seekable time ranges from available segments', function(assert) {
  let playlist = {
    mediaSequence: 0,
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      duration: 10,
      uri: '1.ts'
    }],
    endList: true
  };
  let seekable = Playlist.seekable(playlist);

  assert.equal(seekable.length, 1, 'there are seekable ranges');
  assert.equal(seekable.start(0), 0, 'starts at zero');
  assert.equal(seekable.end(0), Playlist.duration(playlist), 'ends at the duration');
});

QUnit.test('calculates playlist end time from the available segments', function(assert) {
  let playlistEnd = Playlist.playlistEnd({
    mediaSequence: 0,
    segments: [{
      duration: 10,
      uri: '0.ts'
    }, {
      duration: 10,
      uri: '1.ts'
    }],
    endList: true
  });

  assert.equal(playlistEnd, 20, 'paylist end at the duration');
});

QUnit.test('master playlists have empty seekable ranges and no playlist end',
function(assert) {
  let playlist = {
    playlists: [{
      uri: 'low.m3u8'
    }, {
      uri: 'high.m3u8'
    }]
  };
  let seekable = Playlist.seekable(playlist);
  let playlistEnd = Playlist.playlistEnd(playlist);

  assert.equal(seekable.length, 0, 'no seekable ranges from a master playlist');
  assert.equal(playlistEnd, null, 'no playlist end from a master playlist');
});

QUnit.test('seekable end is three target durations from the actual end of live playlists',
function(assert) {
  let seekable = Playlist.seekable({
    mediaSequence: 0,
    syncInfo: {
      time: 0,
      mediaSequence: 0
    },
    targetDuration: 10,
    segments: [{
      duration: 7,
      uri: '0.ts'
    }, {
      duration: 10,
      uri: '1.ts'
    }, {
      duration: 10,
      uri: '2.ts'
    }, {
      duration: 10,
      uri: '3.ts'
    }]
  });

  assert.equal(seekable.length, 1, 'there are seekable ranges');
  assert.equal(seekable.start(0), 0, 'starts at zero');
  assert.equal(seekable.end(0), 7, 'ends three target durations from the last segment');
});

QUnit.test('seekable end and playlist end account for non-standard target durations',
function(assert) {
  const playlist = {
    targetDuration: 2,
    mediaSequence: 0,
    syncInfo: {
      time: 0,
      mediaSequence: 0
    },
    segments: [{
      duration: 2,
      uri: '0.ts'
    }, {
      duration: 2,
      uri: '1.ts'
    }, {
      duration: 1,
      uri: '2.ts'
    }, {
      duration: 2,
      uri: '3.ts'
    }, {
      duration: 2,
      uri: '4.ts'
    }]
  };
  let seekable = Playlist.seekable(playlist);
  let playlistEnd = Playlist.playlistEnd(playlist);

  assert.equal(seekable.start(0), 0, 'starts at the earliest available segment');
  assert.equal(seekable.end(0),
               // Playlist duration is 9s. Target duration 2s. Seekable end should be at
               // least 6s from end. Adding segment durations starting from the end to get
               // that 6s target
               9 - (2 + 2 + 1 + 2),
               'allows seeking no further than the start of the segment 2 target' +
               'durations back from the beginning of the last segment');
  assert.equal(playlistEnd, 9, 'playlist end at the last segment');
});

QUnit.test('safeLiveIndex is correct for standard segment durations', function(assert) {
  const playlist = {
    targetDuration: 6,
    mediaSequence: 10,
    syncInfo: {
      time: 0,
      mediaSequence: 10
    },
    segments: [
      {
        duration: 6
      },
      {
        duration: 6
      },
      {
        duration: 6
      },
      {
        duration: 6
      },
      {
        duration: 6
      },
      {
        duration: 6
      }
    ]
  };

  assert.equal(Playlist.safeLiveIndex(playlist), 3,
    'correct media index for standard durations');
});

QUnit.test('safeLiveIndex is correct for variable segment durations', function(assert) {
  const playlist = {
    targetDuration: 6,
    mediaSequence: 10,
    syncInfo: {
      time: 0,
      mediaSequence: 10
    },
    segments: [
      {
        duration: 6
      },
      {
        duration: 4
      },
      {
        duration: 5
      },
      {
        // this segment is 16 seconds from the end of playlist, the safe live point
        duration: 6
      },
      {
        duration: 3
      },
      {
        duration: 4
      },
      {
        duration: 3
      }
    ]
  };

  // safe live point is no less than 15 seconds (3s + 2 * 6s) from the end of the playlist
  assert.equal(Playlist.safeLiveIndex(playlist), 3,
    'correct media index for variable segment durations');
});

QUnit.test('safeLiveIndex is 0 when no safe live point', function(assert) {
  const playlist = {
    targetDuration: 6,
    mediaSequence: 10,
    syncInfo: {
      time: 0,
      mediaSequence: 10
    },
    segments: [
      {
        duration: 6
      },
      {
        duration: 3
      },
      {
        duration: 3
      }
    ]
  };

  assert.equal(Playlist.safeLiveIndex(playlist), 0,
    'returns media index 0 when playlist has no safe live point');
});

QUnit.test(
  'seekable end and playlist end account for non-zero starting VOD media sequence',
function(assert) {
  let playlist = {
    targetDuration: 2,
    mediaSequence: 5,
    endList: true,
    segments: [{
      duration: 2,
      uri: '0.ts'
    }, {
      duration: 2,
      uri: '1.ts'
    }, {
      duration: 1,
      uri: '2.ts'
    }, {
      duration: 2,
      uri: '3.ts'
    }, {
      duration: 2,
      uri: '4.ts'
    }]
  };
  let seekable = Playlist.seekable(playlist);
  let playlistEnd = Playlist.playlistEnd(playlist);

  assert.equal(seekable.start(0), 0, 'starts at the earliest available segment');
  assert.equal(seekable.end(0), 9, 'seekable end is same as duration');
  assert.equal(playlistEnd, 9, 'playlist end at the last segment');
});

QUnit.test('playlist with no sync points has empty seekable range and empty playlist end',
function(assert) {
  let playlist = {
    targetDuration: 10,
    mediaSequence: 0,
    segments: [{
      duration: 7,
      uri: '0.ts'
    }, {
      duration: 10,
      uri: '1.ts'
    }, {
      duration: 10,
      uri: '2.ts'
    }, {
      duration: 10,
      uri: '3.ts'
    }]
  };

  // seekable and playlistEnd take an optional expired parameter that is from
  // SyncController.getExpiredTime which returns null when there is no sync point, so
  // this test passes in null to simulate no sync points
  let seekable = Playlist.seekable(playlist, null);
  let playlistEnd = Playlist.playlistEnd(playlist, null);

  assert.equal(seekable.length, 0, 'no seekable range for playlist with no sync points');
  assert.equal(playlistEnd, null, 'no playlist end for playlist with no sync points');
});

QUnit.test('seekable and playlistEnd use available sync points for calculating',
  function(assert) {
    let playlist = {
      targetDuration: 10,
      mediaSequence: 100,
      syncInfo: {
        time: 50,
        mediaSequence: 95
      },
      segments: [
        {
          duration: 10,
          uri: '0.ts'
        },
        {
          duration: 10,
          uri: '1.ts'
        },
        {
          duration: 10,
          uri: '2.ts'
        },
        {
          duration: 10,
          uri: '3.ts'
        },
        {
          duration: 10,
          uri: '4.ts'
        }
      ]
    };

    // getExpiredTime would return 100 for this playlist
    let seekable = Playlist.seekable(playlist, 100);
    let playlistEnd = Playlist.playlistEnd(playlist, 100);

    assert.ok(seekable.length, 'seekable range calculated');
    assert.equal(seekable.start(0),
                 100,
                 'estimated start time based on expired sync point');
    assert.equal(seekable.end(0),
                 120,
                 'allows seeking no further than three segments from the end');
    assert.equal(playlistEnd, 150, 'playlist end at the last segment end');

    playlist = {
      targetDuration: 10,
      mediaSequence: 100,
      segments: [
        {
          duration: 10,
          uri: '0.ts'
        },
        {
          duration: 10,
          uri: '1.ts',
          start: 108.5,
          end: 118.4
        },
        {
          duration: 10,
          uri: '2.ts'
        },
        {
          duration: 10,
          uri: '3.ts'
        },
        {
          duration: 10,
          uri: '4.ts'
        }
      ]
    };

    // getExpiredTime would return 98.5
    seekable = Playlist.seekable(playlist, 98.5);
    playlistEnd = Playlist.playlistEnd(playlist, 98.5);

    assert.ok(seekable.length, 'seekable range calculated');
    assert.equal(seekable.start(0), 98.5, 'estimated start time using segmentSync');
    assert.equal(seekable.end(0),
                 118.4,
                 'allows seeking no further than three segments from the end');
    assert.equal(playlistEnd, 148.4, 'playlist end at the last segment end');

    playlist = {
      targetDuration: 10,
      mediaSequence: 100,
      syncInfo: {
        time: 50,
        mediaSequence: 95
      },
      segments: [
        {
          duration: 10,
          uri: '0.ts'
        },
        {
          duration: 10,
          uri: '1.ts',
          start: 108.5,
          end: 118.5
        },
        {
          duration: 10,
          uri: '2.ts'
        },
        {
          duration: 10,
          uri: '3.ts'
        },
        {
          duration: 10,
          uri: '4.ts'
        }
      ]
    };

    // getExpiredTime would return 98.5
    seekable = Playlist.seekable(playlist, 98.5);
    playlistEnd = Playlist.playlistEnd(playlist, 98.5);

    assert.ok(seekable.length, 'seekable range calculated');
    assert.equal(
      seekable.start(0),
      98.5,
      'estimated start time using nearest sync point (segmentSync in this case)');
    assert.equal(seekable.end(0),
                 118.5,
                 'allows seeking no further than three segments from the end');
    assert.equal(playlistEnd, 148.5, 'playlist end at the last segment end');

    playlist = {
      targetDuration: 10,
      mediaSequence: 100,
      syncInfo: {
        time: 90.8,
        mediaSequence: 99
      },
      segments: [
        {
          duration: 10,
          uri: '0.ts'
        },
        {
          duration: 10,
          uri: '1.ts'
        },
        {
          duration: 10,
          uri: '2.ts',
          start: 118.5,
          end: 128.5
        },
        {
          duration: 10,
          uri: '3.ts'
        },
        {
          duration: 10,
          uri: '4.ts'
        }
      ]
    };

    // getExpiredTime would return 100.8
    seekable = Playlist.seekable(playlist, 100.8);
    playlistEnd = Playlist.playlistEnd(playlist, 100.8);

    assert.ok(seekable.length, 'seekable range calculated');
    assert.equal(
      seekable.start(0),
      100.8,
      'estimated start time using nearest sync point (expiredSync in this case)');
    assert.equal(seekable.end(0),
                 118.5,
                 'allows seeking no further than three segments from the end');
    assert.equal(playlistEnd, 148.5, 'playlist end at the last segment end');
  });

QUnit.module('Playlist hasAttribute');

QUnit.test('correctly checks for existence of playlist attribute', function(assert) {
  const playlist = {};

  assert.notOk(Playlist.hasAttribute('BANDWIDTH', playlist),
    'false for playlist with no attributes property');

  playlist.attributes = {};

  assert.notOk(Playlist.hasAttribute('BANDWIDTH', playlist),
    'false for playlist with without specified attribute');

  playlist.attributes.BANDWIDTH = 100;

  assert.ok(Playlist.hasAttribute('BANDWIDTH', playlist),
    'true for playlist with specified attribute');
});

QUnit.module('Playlist estimateSegmentRequestTime');

QUnit.test('estimates segment request time based on bandwidth', function(assert) {
  let segmentDuration = 10;
  let bandwidth = 100;
  let playlist = { attributes: { } };
  let bytesReceived = 0;

  let estimate = Playlist.estimateSegmentRequestTime(segmentDuration,
                                                     bandwidth,
                                                     playlist,
                                                     bytesReceived);

  assert.ok(isNaN(estimate), 'returns NaN when no BANDWIDTH information on playlist');

  playlist.attributes.BANDWIDTH = 100;

  estimate = Playlist.estimateSegmentRequestTime(segmentDuration,
                                                 bandwidth,
                                                 playlist,
                                                 bytesReceived);

  assert.equal(estimate, 10, 'calculated estimated download time');

  bytesReceived = 25;

  estimate = Playlist.estimateSegmentRequestTime(segmentDuration,
                                                 bandwidth,
                                                 playlist,
                                                 bytesReceived);

  assert.equal(estimate, 8, 'takes into account bytes already received from download');
});

QUnit.module('Playlist enabled states', {
  beforeEach(assert) {
    this.env = useFakeEnvironment(assert);
    this.clock = this.env.clock;
  },
  afterEach() {
    this.env.restore();
  }
});

QUnit.test('determines if a playlist is incompatible', function(assert) {
  // incompatible means that the playlist was blacklisted due to incompatible
  // configuration e.g. audio only stream when trying to playback audio and video.
  // incompaatibility is denoted by a blacklist of Infinity.
  assert.notOk(Playlist.isIncompatible({}),
    'playlist not incompatible if no excludeUntil');

  assert.notOk(Playlist.isIncompatible({ excludeUntil: 1 }),
    'playlist not incompatible if expired blacklist');

  assert.notOk(Playlist.isIncompatible({ excludeUntil: Date.now() + 9999 }),
    'playlist not incompatible if temporarily blacklisted');

  assert.ok(Playlist.isIncompatible({ excludeUntil: Infinity }),
    'playlist is incompatible if excludeUntil is Infinity');
});

QUnit.test('determines if a playlist is blacklisted', function(assert) {
  assert.notOk(Playlist.isBlacklisted({}),
    'playlist not blacklisted if no excludeUntil');

  assert.notOk(Playlist.isBlacklisted({ excludeUntil: Date.now() - 1 }),
    'playlist not blacklisted if expired excludeUntil');

  assert.ok(Playlist.isBlacklisted({ excludeUntil: Date.now() + 9999 }),
    'playlist is blacklisted');

  assert.ok(Playlist.isBlacklisted({ excludeUntil: Infinity }),
    'playlist is blacklisted if excludeUntil is Infinity');
});

QUnit.test('determines if a playlist is disabled', function(assert) {
  assert.notOk(Playlist.isDisabled({}), 'playlist not disabled');

  assert.ok(Playlist.isDisabled({ disabled: true }), 'playlist is disabled');
});

QUnit.test('playlists with no or expired blacklist are enabled', function(assert) {
  // enabled means not blacklisted and not disabled
  assert.ok(Playlist.isEnabled({}), 'playlist with no blacklist is enabled');
  assert.ok(Playlist.isEnabled({ excludeUntil: Date.now() - 1 }),
    'playlist with expired blacklist is enabled');
});

QUnit.test('blacklisted playlists are not enabled', function(assert) {
  // enabled means not blacklisted and not disabled
  assert.notOk(Playlist.isEnabled({ excludeUntil: Date.now() + 9999 }),
    'playlist with temporary blacklist is not enabled');
  assert.notOk(Playlist.isEnabled({ excludeUntil: Infinity }),
    'playlist with permanent is not enabled');
});

QUnit.test('manually disabled playlists are not enabled regardless of blacklist state',
function(assert) {
  // enabled means not blacklisted and not disabled
  assert.notOk(Playlist.isEnabled({ disabled: true }),
    'disabled playlist with no blacklist is not enabled');
  assert.notOk(Playlist.isEnabled({ disabled: true, excludeUntil: Date.now() - 1 }),
    'disabled playlist with expired blacklist is not enabled');
  assert.notOk(Playlist.isEnabled({ disabled: true, excludeUntil: Date.now() + 9999 }),
    'disabled playlist with temporary blacklist is not enabled');
  assert.notOk(Playlist.isEnabled({ disabled: true, excludeUntil: Infinity }),
    'disabled playlist with permanent blacklist is not enabled');
});

QUnit.test('isLowestEnabledRendition detects if we are on lowest rendition',
function(assert) {
  assert.ok(
    Playlist.isLowestEnabledRendition(
      {
        playlists: [
          {attributes: {BANDWIDTH: 10}},
          {attributes: {BANDWIDTH: 20}}
        ]
      },
      {attributes: {BANDWIDTH: 10}}),
    'Detected on lowest rendition');

  assert.ok(
    Playlist.isLowestEnabledRendition(
      {
        playlists: [
          {attributes: {BANDWIDTH: 10}},
          {attributes: {BANDWIDTH: 10}},
          {attributes: {BANDWIDTH: 10}},
          {attributes: {BANDWIDTH: 20}}
        ]
      },
      {attributes: {BANDWIDTH: 10}}),
    'Detected on lowest rendition');

  assert.notOk(
    Playlist.isLowestEnabledRendition(
      {
        playlists: [
          {attributes: {BANDWIDTH: 10}},
          {attributes: {BANDWIDTH: 20}}
        ]
      },
      {attributes: {BANDWIDTH: 20}}),
    'Detected not on lowest rendition');
});

QUnit.module('Playlist isAes and isFmp4', {
  beforeEach(assert) {
    this.env = useFakeEnvironment(assert);
    this.clock = this.env.clock;
    this.requests = this.env.requests;
    this.fakeHls = {
      xhr: xhrFactory()
    };
  },
  afterEach() {
    this.env.restore();
  }
});

QUnit.test('determine if playlist is an AES encrypted HLS stream', function(assert) {
  let media;
  let loader = new PlaylistLoader('media.m3u8', this.fakeHls);

  loader.load();
  this.requests.shift().respond(
    200,
    null,
    '#EXTM3U\n' +
    '#EXT-X-TARGETDURATION:15\n' +
    '#EXT-X-KEY:METHOD=AES-128,URI="http://example.com/keys/key.php"\n' +
    '#EXTINF:2.833,\n' +
    'http://example.com/000001.ts\n' +
    '#EXT-X-ENDLIST\n'
  );

  media = loader.media();

  assert.ok(Playlist.isAes(media), 'media is an AES encrypted HLS stream');
});

QUnit.test('determine if playlist contains an fmp4 segment', function(assert) {
  let media;
  let loader = new PlaylistLoader('video/fmp4.m3u8', this.fakeHls);

  loader.load();
  this.requests.shift().respond(200, null,
                                '#EXTM3U\n' +
                                '#EXT-X-MAP:URI="main.mp4",BYTERANGE="720@0"\n' +
                                '#EXTINF:10,\n' +
                                '0.mp4\n' +
                                '#EXT-X-ENDLIST\n');

  media = loader.media();

  assert.ok(Playlist.isFmp4(media), 'media contains fmp4 segment');
});

QUnit.module('Playlist Media Index For Time', {
  beforeEach(assert) {
    this.env = useFakeEnvironment(assert);
    this.clock = this.env.clock;
    this.requests = this.env.requests;
    this.fakeHls = {
      xhr: xhrFactory()
    };
  },
  afterEach() {
    this.env.restore();
  }
});

QUnit.test('can get media index by playback position for non-live videos',
function(assert) {
  let media;
  let loader = new PlaylistLoader('media.m3u8', this.fakeHls);

  loader.load();

  this.requests.shift().respond(200, null,
    '#EXTM3U\n' +
    '#EXT-X-MEDIA-SEQUENCE:0\n' +
    '#EXTINF:4,\n' +
    '0.ts\n' +
    '#EXTINF:5,\n' +
    '1.ts\n' +
    '#EXTINF:6,\n' +
    '2.ts\n' +
    '#EXT-X-ENDLIST\n'
  );

  media = loader.media();

  assert.equal(Playlist.getMediaInfoForTime(media, -1, 0, 0).mediaIndex, 0,
              'the index is never less than zero');
  assert.equal(Playlist.getMediaInfoForTime(media, 0, 0, 0).mediaIndex, 0,
    'time zero is index zero');
  assert.equal(Playlist.getMediaInfoForTime(media, 3, 0, 0).mediaIndex, 0,
    'time three is index zero');
  assert.equal(Playlist.getMediaInfoForTime(media, 10, 0, 0).mediaIndex, 2,
    'time 10 is index 2');
  assert.equal(Playlist.getMediaInfoForTime(media, 22, 0, 0).mediaIndex, 2,
              'time greater than the length is index 2');
});

QUnit.test('returns the lower index when calculating for a segment boundary',
function(assert) {
  let media;
  let loader = new PlaylistLoader('media.m3u8', this.fakeHls);

  loader.load();

  this.requests.shift().respond(200, null,
    '#EXTM3U\n' +
    '#EXT-X-MEDIA-SEQUENCE:0\n' +
    '#EXTINF:4,\n' +
    '0.ts\n' +
    '#EXTINF:5,\n' +
    '1.ts\n' +
    '#EXT-X-ENDLIST\n'
  );

  media = loader.media();

  assert.equal(Playlist.getMediaInfoForTime(media, 4, 0, 0).mediaIndex, 0,
    'rounds down exact matches');
  assert.equal(Playlist.getMediaInfoForTime(media, 3.7, 0, 0).mediaIndex, 0,
    'rounds down');
  assert.equal(Playlist.getMediaInfoForTime(media, 4.5, 0, 0).mediaIndex, 1,
    'rounds up at 0.5');
});

QUnit.test(
'accounts for non-zero starting segment time when calculating media index',
function(assert) {
  let media;
  let loader = new PlaylistLoader('media.m3u8', this.fakeHls);

  loader.load();

  this.requests.shift().respond(200, null,
    '#EXTM3U\n' +
    '#EXT-X-MEDIA-SEQUENCE:1001\n' +
    '#EXTINF:4,\n' +
    '1001.ts\n' +
    '#EXTINF:5,\n' +
    '1002.ts\n'
  );

  media = loader.media();

  assert.equal(
    Playlist.getMediaInfoForTime(media, 45, 0, 150).mediaIndex,
    0,
    'expired content returns 0 for earliest segment available'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 75, 0, 150).mediaIndex,
    0,
    'expired content returns 0 for earliest segment available'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 0, 0, 150).mediaIndex,
    0,
    'time of 0 with no expired time returns first segment'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 50 + 100, 0, 150).mediaIndex,
    0,
    'calculates the earliest available position'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 50 + 100 + 2, 0, 150).mediaIndex,
    0,
    'calculates within the first segment'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 50 + 100 + 2, 0, 150).mediaIndex,
    0,
    'calculates within the first segment'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 50 + 100 + 4, 0, 150).mediaIndex,
    0,
    'calculates earlier segment on exact boundary match'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 50 + 100 + 4.5, 0, 150).mediaIndex,
    1,
    'calculates within the second segment'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 50 + 100 + 6, 0, 150).mediaIndex,
    1,
    'calculates within the second segment'
  );

  assert.equal(
    Playlist.getMediaInfoForTime(media, 159, 0, 150).mediaIndex,
    1,
    'returns last segment when time is equal to end of last segment'
  );
  assert.equal(
    Playlist.getMediaInfoForTime(media, 160, 0, 150).mediaIndex,
    1,
    'returns last segment when time is past end of last segment'
  );
});