m3u8.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var fs = require('fs'),
  2. path = require('path'),
  3. should = require('should');
  4. var m3u8 = require('..').m3u8;
  5. var fixtureDir = path.join(__dirname, 'fixtures');
  6. describe('M3U8', function() {
  7. it('should parse a valid file', function(done) {
  8. var stream = fs.createReadStream(path.join(fixtureDir, 'enc.m3u8'));
  9. m3u8.parse(stream, function(err, index) {
  10. should.not.exist(err);
  11. should.exist(index);
  12. index.variant.should.be.false;
  13. done();
  14. });
  15. })
  16. it('should parse a basic variant file', function(done) {
  17. var stream = fs.createReadStream(path.join(fixtureDir, 'variant.m3u8'));
  18. m3u8.parse(stream, function(err, index) {
  19. should.not.exist(err);
  20. should.exist(index);
  21. index.variant.should.be.true;
  22. done();
  23. });
  24. })
  25. it('should parse an advanced variant file', function(done) {
  26. var stream = fs.createReadStream(path.join(fixtureDir, 'variant_v4.m3u8'));
  27. m3u8.parse(stream, function(err, index) {
  28. should.not.exist(err);
  29. should.exist(index);
  30. index.variant.should.be.true;
  31. done();
  32. });
  33. })
  34. })
  35. describe('M3U8Playlist', function() {
  36. var testIndex = null;
  37. var variantIndex = null;
  38. before(function(done) {
  39. var stream = fs.createReadStream(path.join(fixtureDir, 'enc.m3u8'));
  40. m3u8.parse(stream, function(err, index) {
  41. should.not.exist(err);
  42. testIndex = index;
  43. done();
  44. });
  45. })
  46. before(function(done) {
  47. var stream = fs.createReadStream(path.join(fixtureDir, 'variant_v4.m3u8'));
  48. m3u8.parse(stream, function(err, index) {
  49. should.not.exist(err);
  50. variantIndex = index;
  51. done();
  52. });
  53. })
  54. describe('#totalDuration()', function() {
  55. it('should calculate total of all segments durations', function() {
  56. testIndex.totalDuration().should.equal(46.166);
  57. variantIndex.totalDuration().should.equal(0);
  58. })
  59. })
  60. describe('#isLive()', function() {
  61. it('should return true when no #EXT-X-ENDLIST is present', function() {
  62. testIndex.ended.should.be.false;
  63. testIndex.isLive().should.be.true;
  64. })
  65. })
  66. describe('#startSeqNo()', function() {
  67. it('should return the sequence number to start streaming from', function() {
  68. testIndex.startSeqNo().should.equal(7794);
  69. variantIndex.startSeqNo().should.equal(-1);
  70. })
  71. })
  72. describe('#lastSeqNo()', function() {
  73. it('should return the sequence number of the final segment', function() {
  74. testIndex.lastSeqNo().should.equal(7797);
  75. variantIndex.lastSeqNo().should.equal(-1);
  76. })
  77. })
  78. describe('#isValidSeqNo()', function() {
  79. it('should return false for early numbers', function() {
  80. testIndex.isValidSeqNo(-1000).should.be.false;
  81. testIndex.isValidSeqNo(0).should.be.false;
  82. testIndex.isValidSeqNo("100").should.be.false;
  83. })
  84. it('should return false for future numbers', function() {
  85. testIndex.isValidSeqNo(10000).should.be.false;
  86. testIndex.isValidSeqNo("10000").should.be.false;
  87. })
  88. it('should return true for numbers in range', function() {
  89. testIndex.isValidSeqNo(7794).should.be.true;
  90. testIndex.isValidSeqNo("7795").should.be.true;
  91. testIndex.isValidSeqNo(7796).should.be.true;
  92. testIndex.isValidSeqNo(7797).should.be.true;
  93. })
  94. })
  95. describe('#getSegment()', function() {
  96. it('should return segment data for valid sequence numbers', function() {
  97. testIndex.getSegment(7794).should.be.an.instanceof(m3u8.M3U8Segment);
  98. testIndex.getSegment(7797).should.be.an.instanceof(m3u8.M3U8Segment);
  99. })
  100. it('should return null for out of bounds sequence numbers', function() {
  101. should.not.exist(testIndex.getSegment(-1));
  102. should.not.exist(testIndex.getSegment(7793));
  103. should.not.exist(testIndex.getSegment(7798));
  104. should.not.exist(variantIndex.getSegment(0));
  105. })
  106. })
  107. })