probe.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * Utilities to detect basic properties and metadata about TS Segments.
  8. */
  9. 'use strict';
  10. var StreamTypes = require('./stream-types.js');
  11. var parsePid = function(packet) {
  12. var pid = packet[1] & 0x1f;
  13. pid <<= 8;
  14. pid |= packet[2];
  15. return pid;
  16. };
  17. var parsePayloadUnitStartIndicator = function(packet) {
  18. return !!(packet[1] & 0x40);
  19. };
  20. var parseAdaptionField = function(packet) {
  21. var offset = 0;
  22. // if an adaption field is present, its length is specified by the
  23. // fifth byte of the TS packet header. The adaptation field is
  24. // used to add stuffing to PES packets that don't fill a complete
  25. // TS packet, and to specify some forms of timing and control data
  26. // that we do not currently use.
  27. if (((packet[3] & 0x30) >>> 4) > 0x01) {
  28. offset += packet[4] + 1;
  29. }
  30. return offset;
  31. };
  32. var parseType = function(packet, pmtPid) {
  33. var pid = parsePid(packet);
  34. if (pid === 0) {
  35. return 'pat';
  36. } else if (pid === pmtPid) {
  37. return 'pmt';
  38. } else if (pmtPid) {
  39. return 'pes';
  40. }
  41. return null;
  42. };
  43. var parsePat = function(packet) {
  44. var pusi = parsePayloadUnitStartIndicator(packet);
  45. var offset = 4 + parseAdaptionField(packet);
  46. if (pusi) {
  47. offset += packet[offset] + 1;
  48. }
  49. return (packet[offset + 10] & 0x1f) << 8 | packet[offset + 11];
  50. };
  51. var parsePmt = function(packet) {
  52. var programMapTable = {};
  53. var pusi = parsePayloadUnitStartIndicator(packet);
  54. var payloadOffset = 4 + parseAdaptionField(packet);
  55. if (pusi) {
  56. payloadOffset += packet[payloadOffset] + 1;
  57. }
  58. // PMTs can be sent ahead of the time when they should actually
  59. // take effect. We don't believe this should ever be the case
  60. // for HLS but we'll ignore "forward" PMT declarations if we see
  61. // them. Future PMT declarations have the current_next_indicator
  62. // set to zero.
  63. if (!(packet[payloadOffset + 5] & 0x01)) {
  64. return;
  65. }
  66. var sectionLength, tableEnd, programInfoLength;
  67. // the mapping table ends at the end of the current section
  68. sectionLength = (packet[payloadOffset + 1] & 0x0f) << 8 | packet[payloadOffset + 2];
  69. tableEnd = 3 + sectionLength - 4;
  70. // to determine where the table is, we have to figure out how
  71. // long the program info descriptors are
  72. programInfoLength = (packet[payloadOffset + 10] & 0x0f) << 8 | packet[payloadOffset + 11];
  73. // advance the offset to the first entry in the mapping table
  74. var offset = 12 + programInfoLength;
  75. while (offset < tableEnd) {
  76. var i = payloadOffset + offset;
  77. // add an entry that maps the elementary_pid to the stream_type
  78. programMapTable[(packet[i + 1] & 0x1F) << 8 | packet[i + 2]] = packet[i];
  79. // move to the next table entry
  80. // skip past the elementary stream descriptors, if present
  81. offset += ((packet[i + 3] & 0x0F) << 8 | packet[i + 4]) + 5;
  82. }
  83. return programMapTable;
  84. };
  85. var parsePesType = function(packet, programMapTable) {
  86. var pid = parsePid(packet);
  87. var type = programMapTable[pid];
  88. switch (type) {
  89. case StreamTypes.H264_STREAM_TYPE:
  90. return 'video';
  91. case StreamTypes.ADTS_STREAM_TYPE:
  92. return 'audio';
  93. case StreamTypes.METADATA_STREAM_TYPE:
  94. return 'timed-metadata';
  95. default:
  96. return null;
  97. }
  98. };
  99. var parsePesTime = function(packet) {
  100. var pusi = parsePayloadUnitStartIndicator(packet);
  101. if (!pusi) {
  102. return null;
  103. }
  104. var offset = 4 + parseAdaptionField(packet);
  105. if (offset >= packet.byteLength) {
  106. // From the H 222.0 MPEG-TS spec
  107. // "For transport stream packets carrying PES packets, stuffing is needed when there
  108. // is insufficient PES packet data to completely fill the transport stream packet
  109. // payload bytes. Stuffing is accomplished by defining an adaptation field longer than
  110. // the sum of the lengths of the data elements in it, so that the payload bytes
  111. // remaining after the adaptation field exactly accommodates the available PES packet
  112. // data."
  113. //
  114. // If the offset is >= the length of the packet, then the packet contains no data
  115. // and instead is just adaption field stuffing bytes
  116. return null;
  117. }
  118. var pes = null;
  119. var ptsDtsFlags;
  120. // PES packets may be annotated with a PTS value, or a PTS value
  121. // and a DTS value. Determine what combination of values is
  122. // available to work with.
  123. ptsDtsFlags = packet[offset + 7];
  124. // PTS and DTS are normally stored as a 33-bit number. Javascript
  125. // performs all bitwise operations on 32-bit integers but javascript
  126. // supports a much greater range (52-bits) of integer using standard
  127. // mathematical operations.
  128. // We construct a 31-bit value using bitwise operators over the 31
  129. // most significant bits and then multiply by 4 (equal to a left-shift
  130. // of 2) before we add the final 2 least significant bits of the
  131. // timestamp (equal to an OR.)
  132. if (ptsDtsFlags & 0xC0) {
  133. pes = {};
  134. // the PTS and DTS are not written out directly. For information
  135. // on how they are encoded, see
  136. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  137. pes.pts = (packet[offset + 9] & 0x0E) << 27 |
  138. (packet[offset + 10] & 0xFF) << 20 |
  139. (packet[offset + 11] & 0xFE) << 12 |
  140. (packet[offset + 12] & 0xFF) << 5 |
  141. (packet[offset + 13] & 0xFE) >>> 3;
  142. pes.pts *= 4; // Left shift by 2
  143. pes.pts += (packet[offset + 13] & 0x06) >>> 1; // OR by the two LSBs
  144. pes.dts = pes.pts;
  145. if (ptsDtsFlags & 0x40) {
  146. pes.dts = (packet[offset + 14] & 0x0E) << 27 |
  147. (packet[offset + 15] & 0xFF) << 20 |
  148. (packet[offset + 16] & 0xFE) << 12 |
  149. (packet[offset + 17] & 0xFF) << 5 |
  150. (packet[offset + 18] & 0xFE) >>> 3;
  151. pes.dts *= 4; // Left shift by 2
  152. pes.dts += (packet[offset + 18] & 0x06) >>> 1; // OR by the two LSBs
  153. }
  154. }
  155. return pes;
  156. };
  157. var parseNalUnitType = function(type) {
  158. switch (type) {
  159. case 0x05:
  160. return 'slice_layer_without_partitioning_rbsp_idr';
  161. case 0x06:
  162. return 'sei_rbsp';
  163. case 0x07:
  164. return 'seq_parameter_set_rbsp';
  165. case 0x08:
  166. return 'pic_parameter_set_rbsp';
  167. case 0x09:
  168. return 'access_unit_delimiter_rbsp';
  169. default:
  170. return null;
  171. }
  172. };
  173. var videoPacketContainsKeyFrame = function(packet) {
  174. var offset = 4 + parseAdaptionField(packet);
  175. var frameBuffer = packet.subarray(offset);
  176. var frameI = 0;
  177. var frameSyncPoint = 0;
  178. var foundKeyFrame = false;
  179. var nalType;
  180. // advance the sync point to a NAL start, if necessary
  181. for (; frameSyncPoint < frameBuffer.byteLength - 3; frameSyncPoint++) {
  182. if (frameBuffer[frameSyncPoint + 2] === 1) {
  183. // the sync point is properly aligned
  184. frameI = frameSyncPoint + 5;
  185. break;
  186. }
  187. }
  188. while (frameI < frameBuffer.byteLength) {
  189. // look at the current byte to determine if we've hit the end of
  190. // a NAL unit boundary
  191. switch (frameBuffer[frameI]) {
  192. case 0:
  193. // skip past non-sync sequences
  194. if (frameBuffer[frameI - 1] !== 0) {
  195. frameI += 2;
  196. break;
  197. } else if (frameBuffer[frameI - 2] !== 0) {
  198. frameI++;
  199. break;
  200. }
  201. if (frameSyncPoint + 3 !== frameI - 2) {
  202. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  203. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  204. foundKeyFrame = true;
  205. }
  206. }
  207. // drop trailing zeroes
  208. do {
  209. frameI++;
  210. } while (frameBuffer[frameI] !== 1 && frameI < frameBuffer.length);
  211. frameSyncPoint = frameI - 2;
  212. frameI += 3;
  213. break;
  214. case 1:
  215. // skip past non-sync sequences
  216. if (frameBuffer[frameI - 1] !== 0 ||
  217. frameBuffer[frameI - 2] !== 0) {
  218. frameI += 3;
  219. break;
  220. }
  221. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  222. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  223. foundKeyFrame = true;
  224. }
  225. frameSyncPoint = frameI - 2;
  226. frameI += 3;
  227. break;
  228. default:
  229. // the current byte isn't a one or zero, so it cannot be part
  230. // of a sync sequence
  231. frameI += 3;
  232. break;
  233. }
  234. }
  235. frameBuffer = frameBuffer.subarray(frameSyncPoint);
  236. frameI -= frameSyncPoint;
  237. frameSyncPoint = 0;
  238. // parse the final nal
  239. if (frameBuffer && frameBuffer.byteLength > 3) {
  240. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  241. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  242. foundKeyFrame = true;
  243. }
  244. }
  245. return foundKeyFrame;
  246. };
  247. module.exports = {
  248. parseType: parseType,
  249. parsePat: parsePat,
  250. parsePmt: parsePmt,
  251. parsePayloadUnitStartIndicator: parsePayloadUnitStartIndicator,
  252. parsePesType: parsePesType,
  253. parsePesTime: parsePesTime,
  254. videoPacketContainsKeyFrame: videoPacketContainsKeyFrame
  255. };