aac-stream.test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. 'use strict';
  2. var
  3. aacStream,
  4. AacStream = require('../lib/aac'),
  5. QUnit = require('qunit'),
  6. utils = require('./utils'),
  7. createId3Header,
  8. createId3FrameHeader,
  9. createAdtsHeader;
  10. createId3Header = function(tagSize) {
  11. var header = [];
  12. header[0] = 'I'.charCodeAt(0);
  13. header[1] = 'D'.charCodeAt(0);
  14. header[2] = '3'.charCodeAt(0);
  15. // 2 version bytes, ID3v2.4.0 (major 4, revision 0)
  16. header[3] = 4;
  17. header[4] = 0;
  18. // unsynchronization, extended header, experimental indicator, footer present flags
  19. header[5] = 0;
  20. // "The ID3v2 tag size is the sum of the byte length of the extended
  21. // header, the padding and the frames after unsynchronisation. If a
  22. // footer is present this equals to ('total size' - 20) bytes, otherwise
  23. // ('total size' - 10) bytes."
  24. // http://id3.org/id3v2.4.0-structure
  25. header[6] = 0;
  26. header[7] = 0;
  27. header[8] = 0;
  28. header[9] = tagSize;
  29. return header;
  30. };
  31. createId3FrameHeader = function() {
  32. var header = [];
  33. // four byte frame ID, XYZ are experimental
  34. header[0] = 'X'.charCodeAt(0);
  35. header[1] = 'Y'.charCodeAt(0);
  36. header[2] = 'Z'.charCodeAt(0);
  37. header[3] = '0'.charCodeAt(0);
  38. // four byte sync safe integer size (excluding frame header)
  39. header[4] = 0;
  40. header[5] = 0;
  41. header[6] = 0;
  42. header[7] = 10;
  43. // two bytes for flags
  44. header[8] = 0;
  45. header[9] = 0;
  46. return header;
  47. };
  48. createAdtsHeader = function(frameLength) {
  49. // Header consists of 7 or 9 bytes (without or with CRC).
  50. // see: https://wiki.multimedia.cx/index.php/ADTS
  51. return utils.binaryStringToArrayOfBytes(''.concat(
  52. // 12 bits for syncword (0xFFF)
  53. '111111111111',
  54. // 1 bit MPEG version
  55. '0',
  56. // 2 bit layer (always 0)
  57. '00',
  58. // 1 bit protection absent (1 for no CRC)
  59. '1',
  60. // 2 bit profile
  61. '10',
  62. // 4 bit sampling frequency index
  63. '0110',
  64. // 1 bit private bit
  65. '0',
  66. // 3 bit channel config
  67. '100',
  68. // 2 bit (ignore)
  69. '00',
  70. // 2 bit (copright bits)
  71. '00',
  72. // 13 bit frame length (includes header length)
  73. utils.leftPad(frameLength.toString(2), 13),
  74. // 11 bit buffer fullness
  75. '11111111111',
  76. // 2 bit number of AAC frames minus 1
  77. '00'
  78. // 16 bit CRC (if present)
  79. ));
  80. };
  81. QUnit.module('AAC Stream', {
  82. beforeEach: function() {
  83. aacStream = new AacStream();
  84. }
  85. });
  86. QUnit.test('parses ID3 tag', function(assert) {
  87. var
  88. id3Count = 0,
  89. adtsCount = 0,
  90. frameHeader = createId3FrameHeader(),
  91. id3Tag = createId3Header(frameHeader.length).concat(frameHeader);
  92. aacStream.on('data', function(frame) {
  93. if (frame.type === 'timed-metadata') {
  94. id3Count += 1;
  95. } else if (frame.type === 'audio') {
  96. adtsCount += 1;
  97. }
  98. });
  99. aacStream.push(new Uint8Array(id3Tag));
  100. assert.equal(adtsCount, 0, 'no adts frames');
  101. assert.equal(id3Count, 1, 'one id3 chunk');
  102. });
  103. QUnit.test('parses two ID3 tags in sequence', function(assert) {
  104. var
  105. id3Count = 0,
  106. adtsCount = 0,
  107. frameHeader = createId3FrameHeader(),
  108. id3Tag = createId3Header(frameHeader.length).concat(frameHeader);
  109. aacStream.on('data', function(frame) {
  110. if (frame.type === 'timed-metadata') {
  111. id3Count += 1;
  112. } else if (frame.type === 'audio') {
  113. adtsCount += 1;
  114. }
  115. });
  116. aacStream.push(new Uint8Array(id3Tag.concat(id3Tag)));
  117. assert.equal(adtsCount, 0, 'no adts frames');
  118. assert.equal(id3Count, 2, 'two id3 chunks');
  119. });
  120. QUnit.test('does not parse second ID3 tag if it\'s incomplete', function(assert) {
  121. var
  122. id3Count = 0,
  123. adtsCount = 0,
  124. frameHeader = createId3FrameHeader(),
  125. id3Tag = createId3Header(frameHeader.length).concat(frameHeader);
  126. aacStream.on('data', function(frame) {
  127. if (frame.type === 'timed-metadata') {
  128. id3Count += 1;
  129. } else if (frame.type === 'audio') {
  130. adtsCount += 1;
  131. }
  132. });
  133. aacStream.push(new Uint8Array(id3Tag.concat(id3Tag.slice(0, id3Tag.length - 1))));
  134. assert.equal(adtsCount, 0, 'no adts frames');
  135. assert.equal(id3Count, 1, 'one id3 chunk');
  136. });
  137. QUnit.test('handles misaligned adts header', function(assert) {
  138. var
  139. id3Count = 0,
  140. adtsCount = 0,
  141. // fake adts frame
  142. adtsFrame = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  143. packetStream = createAdtsHeader(adtsFrame.length).concat(adtsFrame);
  144. aacStream.on('data', function(frame) {
  145. if (frame.type === 'timed-metadata') {
  146. id3Count += 1;
  147. } else if (frame.type === 'audio') {
  148. adtsCount += 1;
  149. }
  150. });
  151. // misalign by two bytes specific to a bug related to detecting sync bytes
  152. // (where we were only properly checking the second byte)
  153. aacStream.push(new Uint8Array([0x01, 0xf0].concat(packetStream)));
  154. assert.equal(adtsCount, 1, 'one adts frames');
  155. assert.equal(id3Count, 0, 'no id3 chunk');
  156. });
  157. QUnit.test('handles incomplete adts frame after id3 frame', function(assert) {
  158. var
  159. id3Count = 0,
  160. adtsCount = 0,
  161. id3FrameHeader = createId3FrameHeader(),
  162. id3Tag = createId3Header(id3FrameHeader.length).concat(id3FrameHeader),
  163. // in this case:
  164. // id3 tag = 20 bytes
  165. // adts header = 7 bytes
  166. // total = 27 bytes
  167. // report the ADTS frame size as 20 bytes
  168. adtsHeader = createAdtsHeader(20),
  169. // no adts frame, stream was cut off
  170. packetStream = id3Tag.concat(adtsHeader);
  171. aacStream.on('data', function(frame) {
  172. if (frame.type === 'timed-metadata') {
  173. id3Count += 1;
  174. } else if (frame.type === 'audio') {
  175. adtsCount += 1;
  176. }
  177. });
  178. aacStream.push(new Uint8Array(packetStream));
  179. assert.equal(adtsCount, 0, 'no adts frame');
  180. assert.equal(id3Count, 1, 'one id3 chunk');
  181. });
  182. QUnit.test('emits data after receiving push', function(assert) {
  183. var
  184. array = new Uint8Array(109),
  185. count = 0;
  186. array[0] = 255;
  187. array[1] = 241;
  188. array[2] = 92;
  189. array[3] = 128;
  190. array[4] = 13;
  191. array[5] = 191;
  192. array[6] = 252;
  193. array[7] = 33;
  194. array[8] = 32;
  195. array[9] = 3;
  196. array[10] = 64;
  197. array[11] = 104;
  198. array[12] = 27;
  199. array[13] = 212;
  200. aacStream.setTimestamp(90);
  201. aacStream.on('data', function(frame) {
  202. if (frame.pts === 90 && frame.dts === 90) {
  203. count += 1;
  204. }
  205. });
  206. aacStream.push(array);
  207. assert.equal(count, 1);
  208. });
  209. QUnit.test('continues parsing after corrupted stream', function(assert) {
  210. var
  211. array = new Uint8Array(10000),
  212. adtsCount = 0,
  213. id3Count = 0;
  214. // an ID3 frame
  215. array[0] = 73;
  216. array[1] = 68;
  217. array[2] = 51;
  218. array[3] = 4;
  219. array[4] = 0;
  220. array[5] = 0;
  221. array[6] = 0;
  222. array[7] = 0;
  223. array[8] = 0;
  224. array[9] = 63;
  225. array[10] = 80;
  226. array[11] = 82;
  227. array[12] = 73;
  228. array[13] = 86;
  229. // an atds frame
  230. array[1020] = 255;
  231. array[1021] = 241;
  232. array[1022] = 92;
  233. array[1023] = 128;
  234. array[1024] = 13;
  235. array[1025] = 191;
  236. array[1026] = 252;
  237. array[1027] = 33;
  238. array[1028] = 32;
  239. array[1029] = 3;
  240. array[1030] = 64;
  241. array[1031] = 104;
  242. array[1032] = 27;
  243. array[1033] = 212;
  244. aacStream.on('data', function(frame) {
  245. if (frame.type === 'timed-metadata') {
  246. id3Count += 1;
  247. } else if (frame.type === 'audio') {
  248. adtsCount += 1;
  249. }
  250. });
  251. aacStream.push(array);
  252. assert.equal(adtsCount, 1);
  253. assert.equal(id3Count, 1);
  254. });