container.test.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import QUnit from 'qunit';
  2. import {detectContainerForBytes, isLikelyFmp4MediaSegment} from '../src/containers.js';
  3. import {stringToBytes, concatTypedArrays, toUint8} from '../src/byte-helpers.js';
  4. const filler = (size) => {
  5. const view = new Uint8Array(size);
  6. for (let i = 0; i < size; i++) {
  7. view[i] = 0;
  8. }
  9. return view;
  10. };
  11. const otherMp4Data = concatTypedArrays([0x00, 0x00, 0x00, 0x00], stringToBytes('stypiso'));
  12. const id3Data = Array.prototype.slice.call(concatTypedArrays(
  13. stringToBytes('ID3'),
  14. // id3 header is 10 bytes without footer
  15. // 10th byte is length 0x23 or 35 in decimal
  16. // so a total length of 45
  17. [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23],
  18. // add in the id3 content
  19. filler(35)
  20. ));
  21. const id3DataWithFooter = Array.prototype.slice.call(concatTypedArrays(
  22. stringToBytes('ID3'),
  23. // id3 header is 20 bytes with footer
  24. // "we have a footer" is the sixth byte
  25. // 10th byte is length of 0x23 or 35 in decimal
  26. // so a total length of 55
  27. [0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x23],
  28. // add in the id3 content
  29. filler(45)
  30. ));
  31. const testData = {
  32. // EBML tag + dataSize
  33. // followed by DocType + dataSize and then actual data for that tag
  34. 'mkv': concatTypedArrays([0x1a, 0x45, 0xdf, 0xa3, 0x99, 0x42, 0x82, 0x88], stringToBytes('matroska')),
  35. 'webm': concatTypedArrays([0x1a, 0x45, 0xdf, 0xa3, 0x99, 0x42, 0x82, 0x88], stringToBytes('webm')),
  36. 'flac': stringToBytes('fLaC'),
  37. 'ogg': stringToBytes('OggS'),
  38. 'aac': toUint8([0xFF, 0xF1]),
  39. 'ac3': toUint8([0x0B, 0x77]),
  40. 'mp3': toUint8([0xFF, 0xFB]),
  41. '3gp': concatTypedArrays([0x00, 0x00, 0x00, 0x00], stringToBytes('ftyp3g')),
  42. 'mp4': concatTypedArrays([0x00, 0x00, 0x00, 0x00], stringToBytes('ftypiso')),
  43. 'mov': concatTypedArrays([0x00, 0x00, 0x00, 0x00], stringToBytes('ftypqt')),
  44. 'avi': toUint8([0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x41, 0x56, 0x49]),
  45. 'wav': toUint8([0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45]),
  46. 'ts': toUint8([0x47]),
  47. // seq_parameter_set_rbsp
  48. 'h264': toUint8([0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x0d, 0xd9, 0x01, 0xa1, 0xfa, 0x10, 0x00, 0x00, 0x03, 0x20, 0x00, 0x00, 0x95, 0xe0, 0xf1, 0x42, 0xa4, 0x80, 0x00, 0x00, 0x00, 0x01]),
  49. // video_parameter_set_rbsp
  50. 'h265': toUint8([0x00, 0x00, 0x00, 0x01, 0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x24, 0x08, 0x00, 0x00, 0x00, 0x9c, 0x08, 0x00, 0x00, 0x00, 0x00, 0x78, 0x95, 0x98, 0x09, 0x00, 0x00, 0x00, 0x01])
  51. };
  52. // seq_parameter_set_rbsp
  53. const h265seq = toUint8([
  54. 0x00, 0x00, 0x00, 0x01,
  55. 0x42, 0x01, 0x01, 0x21,
  56. 0x60, 0x00, 0x00, 0x00,
  57. 0x90, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x78, 0xa0,
  59. 0x0d, 0x08, 0x0f, 0x16,
  60. 0x59, 0x59, 0xa4, 0x93,
  61. 0x2b, 0x9a, 0x02, 0x00,
  62. 0x00, 0x00, 0x64, 0x00,
  63. 0x00, 0x09, 0x5e, 0x10,
  64. 0x00, 0x00, 0x00, 0x01
  65. ]);
  66. const h264shortnal = Array.prototype.slice.call(testData.h264);
  67. // remove 0x00 from the front
  68. h264shortnal.splice(0, 1);
  69. // remove 0x00 from the back
  70. h264shortnal.splice(h264shortnal.length - 2, 1);
  71. const h265shortnal = Array.prototype.slice.call(testData.h265);
  72. // remove 0x00 from the front
  73. h265shortnal.splice(0, 1);
  74. // remove 0x00 from the back
  75. h265shortnal.splice(h265shortnal.length - 2, 1);
  76. const mp4Variants = {
  77. 'start with moov': concatTypedArrays(filler(4), [0x6D, 0x6F, 0x6F, 0x76]),
  78. 'start with moof': concatTypedArrays(filler(4), [0x6D, 0x6F, 0x6F, 0x66]),
  79. 'start with styp': concatTypedArrays(filler(4), [0x73, 0x74, 0x79, 0x70])
  80. };
  81. QUnit.module('detectContainerForBytes');
  82. QUnit.test('should identify known types', function(assert) {
  83. Object.keys(testData).forEach(function(key) {
  84. const data = new Uint8Array(testData[key]);
  85. assert.equal(detectContainerForBytes(testData[key]), key, `found ${key} with Array`);
  86. assert.equal(detectContainerForBytes(data.buffer), key, `found ${key} with ArrayBuffer`);
  87. assert.equal(detectContainerForBytes(data), key, `found ${key} with Uint8Array`);
  88. });
  89. Object.keys(mp4Variants).forEach(function(name) {
  90. const bytes = mp4Variants[name];
  91. assert.equal(detectContainerForBytes(bytes), 'mp4', `${name} detected as mp4`);
  92. });
  93. // mp3/aac/flac/ac3 audio can have id3 data before the
  94. // signature for the file, so we need to handle that.
  95. ['mp3', 'aac', 'flac', 'ac3'].forEach(function(type) {
  96. const dataWithId3 = concatTypedArrays(id3Data, testData[type]);
  97. const dataWithId3Footer = concatTypedArrays(id3DataWithFooter, testData[type]);
  98. const recursiveDataWithId3 = concatTypedArrays(
  99. id3Data,
  100. id3Data,
  101. id3Data,
  102. testData[type]
  103. );
  104. const recursiveDataWithId3Footer = concatTypedArrays(
  105. id3DataWithFooter,
  106. id3DataWithFooter,
  107. id3DataWithFooter,
  108. testData[type]
  109. );
  110. const differentId3Sections = concatTypedArrays(
  111. id3DataWithFooter,
  112. id3Data,
  113. id3DataWithFooter,
  114. id3Data,
  115. testData[type]
  116. );
  117. assert.equal(detectContainerForBytes(dataWithId3), type, `id3 skipped and ${type} detected`);
  118. assert.equal(detectContainerForBytes(dataWithId3Footer), type, `id3 + footer skipped and ${type} detected`);
  119. assert.equal(detectContainerForBytes(recursiveDataWithId3), type, `id3 x3 skipped and ${type} detected`);
  120. assert.equal(detectContainerForBytes(recursiveDataWithId3Footer), type, `id3 + footer x3 skipped and ${type} detected`);
  121. assert.equal(detectContainerForBytes(differentId3Sections), type, `id3 with/without footer skipped and ${type} detected`);
  122. });
  123. const notTs = concatTypedArrays(testData.ts, filler(188));
  124. const longTs = concatTypedArrays(testData.ts, filler(187), testData.ts);
  125. const unsyncTs = concatTypedArrays(filler(187), testData.ts, filler(187), testData.ts);
  126. const badTs = concatTypedArrays(filler(188), testData.ts, filler(187), testData.ts);
  127. assert.equal(detectContainerForBytes(longTs), 'ts', 'long ts data is detected');
  128. assert.equal(detectContainerForBytes(unsyncTs), 'ts', 'unsynced ts is detected');
  129. assert.equal(detectContainerForBytes(badTs), '', 'ts without a sync byte in 188 bytes is not detected');
  130. assert.equal(detectContainerForBytes(notTs), '', 'ts missing 0x47 at 188 is not ts at all');
  131. assert.equal(detectContainerForBytes(otherMp4Data), 'mp4', 'fmp4 detected as mp4');
  132. assert.equal(detectContainerForBytes(new Uint8Array()), '', 'no type');
  133. assert.equal(detectContainerForBytes(), '', 'no type');
  134. assert.equal(detectContainerForBytes(h265seq), 'h265', 'h265 with only seq_parameter_set_rbsp, works');
  135. assert.equal(detectContainerForBytes(h265shortnal), 'h265', 'h265 with short nals works');
  136. assert.equal(detectContainerForBytes(h264shortnal), 'h264', 'h265 with short nals works');
  137. });
  138. const createBox = function(type) {
  139. const size = 0x20;
  140. return concatTypedArrays(
  141. // size bytes
  142. [0x00, 0x00, 0x00, size],
  143. // box identfier styp
  144. stringToBytes(type),
  145. // filler data for size minus identfier and size bytes
  146. filler(size - 8)
  147. );
  148. };
  149. QUnit.module('isLikelyFmp4MediaSegment');
  150. QUnit.test('works as expected', function(assert) {
  151. const fmp4Data = concatTypedArrays(
  152. createBox('styp'),
  153. createBox('sidx'),
  154. createBox('moof')
  155. );
  156. const mp4Data = concatTypedArrays(
  157. createBox('ftyp'),
  158. createBox('sidx'),
  159. createBox('moov')
  160. );
  161. const fmp4Fake = concatTypedArrays(
  162. createBox('test'),
  163. createBox('moof'),
  164. createBox('fooo'),
  165. createBox('bar')
  166. );
  167. assert.ok(isLikelyFmp4MediaSegment(fmp4Data), 'fmp4 is recognized as fmp4');
  168. assert.ok(isLikelyFmp4MediaSegment(fmp4Fake), 'fmp4 with moof and unknown boxes is still fmp4');
  169. assert.ok(isLikelyFmp4MediaSegment(createBox('moof')), 'moof alone is recognized as fmp4');
  170. assert.notOk(isLikelyFmp4MediaSegment(mp4Data), 'mp4 is not recognized');
  171. assert.notOk(isLikelyFmp4MediaSegment(concatTypedArrays(id3DataWithFooter, testData.mp3)), 'bad data is not recognized');
  172. assert.notOk(isLikelyFmp4MediaSegment(new Uint8Array()), 'no errors on empty data');
  173. assert.notOk(isLikelyFmp4MediaSegment(), 'no errors on empty data');
  174. });