metadata-stream.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * mux.js
  3. *
  4. * Copyright (c) Brightcove
  5. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6. *
  7. * Accepts program elementary stream (PES) data events and parses out
  8. * ID3 metadata from them, if present.
  9. * @see http://id3.org/id3v2.3.0
  10. */
  11. 'use strict';
  12. var Stream = require('../utils/stream'),
  13. StreamTypes = require('./stream-types'),
  14. id3 = require('../tools/parse-id3'),
  15. _MetadataStream;
  16. _MetadataStream = function MetadataStream(options) {
  17. var settings = {
  18. // the bytes of the program-level descriptor field in MP2T
  19. // see ISO/IEC 13818-1:2013 (E), section 2.6 "Program and
  20. // program element descriptors"
  21. descriptor: options && options.descriptor
  22. },
  23. // the total size in bytes of the ID3 tag being parsed
  24. tagSize = 0,
  25. // tag data that is not complete enough to be parsed
  26. buffer = [],
  27. // the total number of bytes currently in the buffer
  28. bufferSize = 0,
  29. i;
  30. _MetadataStream.prototype.init.call(this); // calculate the text track in-band metadata track dispatch type
  31. // https://html.spec.whatwg.org/multipage/embedded-content.html#steps-to-expose-a-media-resource-specific-text-track
  32. this.dispatchType = StreamTypes.METADATA_STREAM_TYPE.toString(16);
  33. if (settings.descriptor) {
  34. for (i = 0; i < settings.descriptor.length; i++) {
  35. this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
  36. }
  37. }
  38. this.push = function (chunk) {
  39. var tag, frameStart, frameSize, frame, i, frameHeader;
  40. if (chunk.type !== 'timed-metadata') {
  41. return;
  42. } // if data_alignment_indicator is set in the PES header,
  43. // we must have the start of a new ID3 tag. Assume anything
  44. // remaining in the buffer was malformed and throw it out
  45. if (chunk.dataAlignmentIndicator) {
  46. bufferSize = 0;
  47. buffer.length = 0;
  48. } // ignore events that don't look like ID3 data
  49. if (buffer.length === 0 && (chunk.data.length < 10 || chunk.data[0] !== 'I'.charCodeAt(0) || chunk.data[1] !== 'D'.charCodeAt(0) || chunk.data[2] !== '3'.charCodeAt(0))) {
  50. this.trigger('log', {
  51. level: 'warn',
  52. message: 'Skipping unrecognized metadata packet'
  53. });
  54. return;
  55. } // add this chunk to the data we've collected so far
  56. buffer.push(chunk);
  57. bufferSize += chunk.data.byteLength; // grab the size of the entire frame from the ID3 header
  58. if (buffer.length === 1) {
  59. // the frame size is transmitted as a 28-bit integer in the
  60. // last four bytes of the ID3 header.
  61. // The most significant bit of each byte is dropped and the
  62. // results concatenated to recover the actual value.
  63. tagSize = id3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  64. // convenient for our comparisons to include it
  65. tagSize += 10;
  66. } // if the entire frame has not arrived, wait for more data
  67. if (bufferSize < tagSize) {
  68. return;
  69. } // collect the entire frame so it can be parsed
  70. tag = {
  71. data: new Uint8Array(tagSize),
  72. frames: [],
  73. pts: buffer[0].pts,
  74. dts: buffer[0].dts
  75. };
  76. for (i = 0; i < tagSize;) {
  77. tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
  78. i += buffer[0].data.byteLength;
  79. bufferSize -= buffer[0].data.byteLength;
  80. buffer.shift();
  81. } // find the start of the first frame and the end of the tag
  82. frameStart = 10;
  83. if (tag.data[5] & 0x40) {
  84. // advance the frame start past the extended header
  85. frameStart += 4; // header size field
  86. frameStart += id3.parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end
  87. tagSize -= id3.parseSyncSafeInteger(tag.data.subarray(16, 20));
  88. } // parse one or more ID3 frames
  89. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  90. do {
  91. // determine the number of bytes in this frame
  92. frameSize = id3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
  93. if (frameSize < 1) {
  94. this.trigger('log', {
  95. level: 'warn',
  96. message: 'Malformed ID3 frame encountered. Skipping remaining metadata parsing.'
  97. }); // If the frame is malformed, don't parse any further frames but allow previous valid parsed frames
  98. // to be sent along.
  99. break;
  100. }
  101. frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
  102. frame = {
  103. id: frameHeader,
  104. data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
  105. };
  106. frame.key = frame.id; // parse frame values
  107. if (id3.frameParsers[frame.id]) {
  108. // use frame specific parser
  109. id3.frameParsers[frame.id](frame);
  110. } else if (frame.id[0] === 'T') {
  111. // use text frame generic parser
  112. id3.frameParsers['T*'](frame);
  113. } else if (frame.id[0] === 'W') {
  114. // use URL link frame generic parser
  115. id3.frameParsers['W*'](frame);
  116. } // handle the special PRIV frame used to indicate the start
  117. // time for raw AAC data
  118. if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
  119. var d = frame.data,
  120. size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  121. size *= 4;
  122. size += d[7] & 0x03;
  123. frame.timeStamp = size; // in raw AAC, all subsequent data will be timestamped based
  124. // on the value of this frame
  125. // we couldn't have known the appropriate pts and dts before
  126. // parsing this ID3 tag so set those values now
  127. if (tag.pts === undefined && tag.dts === undefined) {
  128. tag.pts = frame.timeStamp;
  129. tag.dts = frame.timeStamp;
  130. }
  131. this.trigger('timestamp', frame);
  132. }
  133. tag.frames.push(frame);
  134. frameStart += 10; // advance past the frame header
  135. frameStart += frameSize; // advance past the frame body
  136. } while (frameStart < tagSize);
  137. this.trigger('data', tag);
  138. };
  139. };
  140. _MetadataStream.prototype = new Stream();
  141. module.exports = _MetadataStream;