m2ts.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. * A stream-based mp2t to mp4 converter. This utility can be used to
  8. * deliver mp4s to a SourceBuffer on platforms that support native
  9. * Media Source Extensions.
  10. */
  11. 'use strict';
  12. var Stream = require('../utils/stream.js'),
  13. CaptionStream = require('./caption-stream'),
  14. StreamTypes = require('./stream-types'),
  15. TimestampRolloverStream = require('./timestamp-rollover-stream').TimestampRolloverStream;
  16. // object types
  17. var TransportPacketStream, TransportParseStream, ElementaryStream;
  18. // constants
  19. var
  20. MP2T_PACKET_LENGTH = 188, // bytes
  21. SYNC_BYTE = 0x47;
  22. /**
  23. * Splits an incoming stream of binary data into MPEG-2 Transport
  24. * Stream packets.
  25. */
  26. TransportPacketStream = function() {
  27. var
  28. buffer = new Uint8Array(MP2T_PACKET_LENGTH),
  29. bytesInBuffer = 0;
  30. TransportPacketStream.prototype.init.call(this);
  31. // Deliver new bytes to the stream.
  32. /**
  33. * Split a stream of data into M2TS packets
  34. **/
  35. this.push = function(bytes) {
  36. var
  37. startIndex = 0,
  38. endIndex = MP2T_PACKET_LENGTH,
  39. everything;
  40. // If there are bytes remaining from the last segment, prepend them to the
  41. // bytes that were pushed in
  42. if (bytesInBuffer) {
  43. everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
  44. everything.set(buffer.subarray(0, bytesInBuffer));
  45. everything.set(bytes, bytesInBuffer);
  46. bytesInBuffer = 0;
  47. } else {
  48. everything = bytes;
  49. }
  50. // While we have enough data for a packet
  51. while (endIndex < everything.byteLength) {
  52. // Look for a pair of start and end sync bytes in the data..
  53. if (everything[startIndex] === SYNC_BYTE && everything[endIndex] === SYNC_BYTE) {
  54. // We found a packet so emit it and jump one whole packet forward in
  55. // the stream
  56. this.trigger('data', everything.subarray(startIndex, endIndex));
  57. startIndex += MP2T_PACKET_LENGTH;
  58. endIndex += MP2T_PACKET_LENGTH;
  59. continue;
  60. }
  61. // If we get here, we have somehow become de-synchronized and we need to step
  62. // forward one byte at a time until we find a pair of sync bytes that denote
  63. // a packet
  64. startIndex++;
  65. endIndex++;
  66. }
  67. // If there was some data left over at the end of the segment that couldn't
  68. // possibly be a whole packet, keep it because it might be the start of a packet
  69. // that continues in the next segment
  70. if (startIndex < everything.byteLength) {
  71. buffer.set(everything.subarray(startIndex), 0);
  72. bytesInBuffer = everything.byteLength - startIndex;
  73. }
  74. };
  75. /**
  76. * Passes identified M2TS packets to the TransportParseStream to be parsed
  77. **/
  78. this.flush = function() {
  79. // If the buffer contains a whole packet when we are being flushed, emit it
  80. // and empty the buffer. Otherwise hold onto the data because it may be
  81. // important for decoding the next segment
  82. if (bytesInBuffer === MP2T_PACKET_LENGTH && buffer[0] === SYNC_BYTE) {
  83. this.trigger('data', buffer);
  84. bytesInBuffer = 0;
  85. }
  86. this.trigger('done');
  87. };
  88. this.endTimeline = function() {
  89. this.flush();
  90. this.trigger('endedtimeline');
  91. };
  92. this.reset = function() {
  93. bytesInBuffer = 0;
  94. this.trigger('reset');
  95. };
  96. };
  97. TransportPacketStream.prototype = new Stream();
  98. /**
  99. * Accepts an MP2T TransportPacketStream and emits data events with parsed
  100. * forms of the individual transport stream packets.
  101. */
  102. TransportParseStream = function() {
  103. var parsePsi, parsePat, parsePmt, self;
  104. TransportParseStream.prototype.init.call(this);
  105. self = this;
  106. this.packetsWaitingForPmt = [];
  107. this.programMapTable = undefined;
  108. parsePsi = function(payload, psi) {
  109. var offset = 0;
  110. // PSI packets may be split into multiple sections and those
  111. // sections may be split into multiple packets. If a PSI
  112. // section starts in this packet, the payload_unit_start_indicator
  113. // will be true and the first byte of the payload will indicate
  114. // the offset from the current position to the start of the
  115. // section.
  116. if (psi.payloadUnitStartIndicator) {
  117. offset += payload[offset] + 1;
  118. }
  119. if (psi.type === 'pat') {
  120. parsePat(payload.subarray(offset), psi);
  121. } else {
  122. parsePmt(payload.subarray(offset), psi);
  123. }
  124. };
  125. parsePat = function(payload, pat) {
  126. pat.section_number = payload[7]; // eslint-disable-line camelcase
  127. pat.last_section_number = payload[8]; // eslint-disable-line camelcase
  128. // skip the PSI header and parse the first PMT entry
  129. self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
  130. pat.pmtPid = self.pmtPid;
  131. };
  132. /**
  133. * Parse out the relevant fields of a Program Map Table (PMT).
  134. * @param payload {Uint8Array} the PMT-specific portion of an MP2T
  135. * packet. The first byte in this array should be the table_id
  136. * field.
  137. * @param pmt {object} the object that should be decorated with
  138. * fields parsed from the PMT.
  139. */
  140. parsePmt = function(payload, pmt) {
  141. var sectionLength, tableEnd, programInfoLength, offset;
  142. // PMTs can be sent ahead of the time when they should actually
  143. // take effect. We don't believe this should ever be the case
  144. // for HLS but we'll ignore "forward" PMT declarations if we see
  145. // them. Future PMT declarations have the current_next_indicator
  146. // set to zero.
  147. if (!(payload[5] & 0x01)) {
  148. return;
  149. }
  150. // overwrite any existing program map table
  151. self.programMapTable = {
  152. video: null,
  153. audio: null,
  154. 'timed-metadata': {}
  155. };
  156. // the mapping table ends at the end of the current section
  157. sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
  158. tableEnd = 3 + sectionLength - 4;
  159. // to determine where the table is, we have to figure out how
  160. // long the program info descriptors are
  161. programInfoLength = (payload[10] & 0x0f) << 8 | payload[11];
  162. // advance the offset to the first entry in the mapping table
  163. offset = 12 + programInfoLength;
  164. while (offset < tableEnd) {
  165. var streamType = payload[offset];
  166. var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2];
  167. // only map a single elementary_pid for audio and video stream types
  168. // TODO: should this be done for metadata too? for now maintain behavior of
  169. // multiple metadata streams
  170. if (streamType === StreamTypes.H264_STREAM_TYPE &&
  171. self.programMapTable.video === null) {
  172. self.programMapTable.video = pid;
  173. } else if (streamType === StreamTypes.ADTS_STREAM_TYPE &&
  174. self.programMapTable.audio === null) {
  175. self.programMapTable.audio = pid;
  176. } else if (streamType === StreamTypes.METADATA_STREAM_TYPE) {
  177. // map pid to stream type for metadata streams
  178. self.programMapTable['timed-metadata'][pid] = streamType;
  179. }
  180. // move to the next table entry
  181. // skip past the elementary stream descriptors, if present
  182. offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
  183. }
  184. // record the map on the packet as well
  185. pmt.programMapTable = self.programMapTable;
  186. };
  187. /**
  188. * Deliver a new MP2T packet to the next stream in the pipeline.
  189. */
  190. this.push = function(packet) {
  191. var
  192. result = {},
  193. offset = 4;
  194. result.payloadUnitStartIndicator = !!(packet[1] & 0x40);
  195. // pid is a 13-bit field starting at the last bit of packet[1]
  196. result.pid = packet[1] & 0x1f;
  197. result.pid <<= 8;
  198. result.pid |= packet[2];
  199. // if an adaption field is present, its length is specified by the
  200. // fifth byte of the TS packet header. The adaptation field is
  201. // used to add stuffing to PES packets that don't fill a complete
  202. // TS packet, and to specify some forms of timing and control data
  203. // that we do not currently use.
  204. if (((packet[3] & 0x30) >>> 4) > 0x01) {
  205. offset += packet[offset] + 1;
  206. }
  207. // parse the rest of the packet based on the type
  208. if (result.pid === 0) {
  209. result.type = 'pat';
  210. parsePsi(packet.subarray(offset), result);
  211. this.trigger('data', result);
  212. } else if (result.pid === this.pmtPid) {
  213. result.type = 'pmt';
  214. parsePsi(packet.subarray(offset), result);
  215. this.trigger('data', result);
  216. // if there are any packets waiting for a PMT to be found, process them now
  217. while (this.packetsWaitingForPmt.length) {
  218. this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
  219. }
  220. } else if (this.programMapTable === undefined) {
  221. // When we have not seen a PMT yet, defer further processing of
  222. // PES packets until one has been parsed
  223. this.packetsWaitingForPmt.push([packet, offset, result]);
  224. } else {
  225. this.processPes_(packet, offset, result);
  226. }
  227. };
  228. this.processPes_ = function(packet, offset, result) {
  229. // set the appropriate stream type
  230. if (result.pid === this.programMapTable.video) {
  231. result.streamType = StreamTypes.H264_STREAM_TYPE;
  232. } else if (result.pid === this.programMapTable.audio) {
  233. result.streamType = StreamTypes.ADTS_STREAM_TYPE;
  234. } else {
  235. // if not video or audio, it is timed-metadata or unknown
  236. // if unknown, streamType will be undefined
  237. result.streamType = this.programMapTable['timed-metadata'][result.pid];
  238. }
  239. result.type = 'pes';
  240. result.data = packet.subarray(offset);
  241. this.trigger('data', result);
  242. };
  243. };
  244. TransportParseStream.prototype = new Stream();
  245. TransportParseStream.STREAM_TYPES = {
  246. h264: 0x1b,
  247. adts: 0x0f
  248. };
  249. /**
  250. * Reconsistutes program elementary stream (PES) packets from parsed
  251. * transport stream packets. That is, if you pipe an
  252. * mp2t.TransportParseStream into a mp2t.ElementaryStream, the output
  253. * events will be events which capture the bytes for individual PES
  254. * packets plus relevant metadata that has been extracted from the
  255. * container.
  256. */
  257. ElementaryStream = function() {
  258. var
  259. self = this,
  260. segmentHadPmt = false,
  261. // PES packet fragments
  262. video = {
  263. data: [],
  264. size: 0
  265. },
  266. audio = {
  267. data: [],
  268. size: 0
  269. },
  270. timedMetadata = {
  271. data: [],
  272. size: 0
  273. },
  274. programMapTable,
  275. parsePes = function(payload, pes) {
  276. var ptsDtsFlags;
  277. const startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2]
  278. // default to an empty array
  279. pes.data = new Uint8Array()
  280. // In certain live streams, the start of a TS fragment has ts packets
  281. // that are frame data that is continuing from the previous fragment. This
  282. // is to check that the pes data is the start of a new pes payload
  283. if (startPrefix !== 1) {
  284. return;
  285. }
  286. // get the packet length, this will be 0 for video
  287. pes.packetLength = 6 + ((payload[4] << 8) | payload[5]);
  288. // find out if this packets starts a new keyframe
  289. pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0;
  290. // PES packets may be annotated with a PTS value, or a PTS value
  291. // and a DTS value. Determine what combination of values is
  292. // available to work with.
  293. ptsDtsFlags = payload[7];
  294. // PTS and DTS are normally stored as a 33-bit number. Javascript
  295. // performs all bitwise operations on 32-bit integers but javascript
  296. // supports a much greater range (52-bits) of integer using standard
  297. // mathematical operations.
  298. // We construct a 31-bit value using bitwise operators over the 31
  299. // most significant bits and then multiply by 4 (equal to a left-shift
  300. // of 2) before we add the final 2 least significant bits of the
  301. // timestamp (equal to an OR.)
  302. if (ptsDtsFlags & 0xC0) {
  303. // the PTS and DTS are not written out directly. For information
  304. // on how they are encoded, see
  305. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  306. pes.pts = (payload[9] & 0x0E) << 27 |
  307. (payload[10] & 0xFF) << 20 |
  308. (payload[11] & 0xFE) << 12 |
  309. (payload[12] & 0xFF) << 5 |
  310. (payload[13] & 0xFE) >>> 3;
  311. pes.pts *= 4; // Left shift by 2
  312. pes.pts += (payload[13] & 0x06) >>> 1; // OR by the two LSBs
  313. pes.dts = pes.pts;
  314. if (ptsDtsFlags & 0x40) {
  315. pes.dts = (payload[14] & 0x0E) << 27 |
  316. (payload[15] & 0xFF) << 20 |
  317. (payload[16] & 0xFE) << 12 |
  318. (payload[17] & 0xFF) << 5 |
  319. (payload[18] & 0xFE) >>> 3;
  320. pes.dts *= 4; // Left shift by 2
  321. pes.dts += (payload[18] & 0x06) >>> 1; // OR by the two LSBs
  322. }
  323. }
  324. // the data section starts immediately after the PES header.
  325. // pes_header_data_length specifies the number of header bytes
  326. // that follow the last byte of the field.
  327. pes.data = payload.subarray(9 + payload[8]);
  328. },
  329. /**
  330. * Pass completely parsed PES packets to the next stream in the pipeline
  331. **/
  332. flushStream = function(stream, type, forceFlush) {
  333. var
  334. packetData = new Uint8Array(stream.size),
  335. event = {
  336. type: type
  337. },
  338. i = 0,
  339. offset = 0,
  340. packetFlushable = false,
  341. fragment;
  342. // do nothing if there is not enough buffered data for a complete
  343. // PES header
  344. if (!stream.data.length || stream.size < 9) {
  345. return;
  346. }
  347. event.trackId = stream.data[0].pid;
  348. // reassemble the packet
  349. for (i = 0; i < stream.data.length; i++) {
  350. fragment = stream.data[i];
  351. packetData.set(fragment.data, offset);
  352. offset += fragment.data.byteLength;
  353. }
  354. // parse assembled packet's PES header
  355. parsePes(packetData, event);
  356. // non-video PES packets MUST have a non-zero PES_packet_length
  357. // check that there is enough stream data to fill the packet
  358. packetFlushable = type === 'video' || event.packetLength <= stream.size;
  359. // flush pending packets if the conditions are right
  360. if (forceFlush || packetFlushable) {
  361. stream.size = 0;
  362. stream.data.length = 0;
  363. }
  364. // only emit packets that are complete. this is to avoid assembling
  365. // incomplete PES packets due to poor segmentation
  366. if (packetFlushable) {
  367. self.trigger('data', event);
  368. }
  369. };
  370. ElementaryStream.prototype.init.call(this);
  371. /**
  372. * Identifies M2TS packet types and parses PES packets using metadata
  373. * parsed from the PMT
  374. **/
  375. this.push = function(data) {
  376. ({
  377. pat: function() {
  378. // we have to wait for the PMT to arrive as well before we
  379. // have any meaningful metadata
  380. },
  381. pes: function() {
  382. var stream, streamType;
  383. switch (data.streamType) {
  384. case StreamTypes.H264_STREAM_TYPE:
  385. stream = video;
  386. streamType = 'video';
  387. break;
  388. case StreamTypes.ADTS_STREAM_TYPE:
  389. stream = audio;
  390. streamType = 'audio';
  391. break;
  392. case StreamTypes.METADATA_STREAM_TYPE:
  393. stream = timedMetadata;
  394. streamType = 'timed-metadata';
  395. break;
  396. default:
  397. // ignore unknown stream types
  398. return;
  399. }
  400. // if a new packet is starting, we can flush the completed
  401. // packet
  402. if (data.payloadUnitStartIndicator) {
  403. flushStream(stream, streamType, true);
  404. }
  405. // buffer this fragment until we are sure we've received the
  406. // complete payload
  407. stream.data.push(data);
  408. stream.size += data.data.byteLength;
  409. },
  410. pmt: function() {
  411. var
  412. event = {
  413. type: 'metadata',
  414. tracks: []
  415. };
  416. programMapTable = data.programMapTable;
  417. // translate audio and video streams to tracks
  418. if (programMapTable.video !== null) {
  419. event.tracks.push({
  420. timelineStartInfo: {
  421. baseMediaDecodeTime: 0
  422. },
  423. id: +programMapTable.video,
  424. codec: 'avc',
  425. type: 'video'
  426. });
  427. }
  428. if (programMapTable.audio !== null) {
  429. event.tracks.push({
  430. timelineStartInfo: {
  431. baseMediaDecodeTime: 0
  432. },
  433. id: +programMapTable.audio,
  434. codec: 'adts',
  435. type: 'audio'
  436. });
  437. }
  438. segmentHadPmt = true;
  439. self.trigger('data', event);
  440. }
  441. })[data.type]();
  442. };
  443. this.reset = function() {
  444. video.size = 0;
  445. video.data.length = 0;
  446. audio.size = 0;
  447. audio.data.length = 0;
  448. this.trigger('reset');
  449. };
  450. /**
  451. * Flush any remaining input. Video PES packets may be of variable
  452. * length. Normally, the start of a new video packet can trigger the
  453. * finalization of the previous packet. That is not possible if no
  454. * more video is forthcoming, however. In that case, some other
  455. * mechanism (like the end of the file) has to be employed. When it is
  456. * clear that no additional data is forthcoming, calling this method
  457. * will flush the buffered packets.
  458. */
  459. this.flushStreams_ = function() {
  460. // !!THIS ORDER IS IMPORTANT!!
  461. // video first then audio
  462. flushStream(video, 'video');
  463. flushStream(audio, 'audio');
  464. flushStream(timedMetadata, 'timed-metadata');
  465. };
  466. this.flush = function() {
  467. // if on flush we haven't had a pmt emitted
  468. // and we have a pmt to emit. emit the pmt
  469. // so that we trigger a trackinfo downstream.
  470. if (!segmentHadPmt && programMapTable) {
  471. var
  472. pmt = {
  473. type: 'metadata',
  474. tracks: []
  475. };
  476. // translate audio and video streams to tracks
  477. if (programMapTable.video !== null) {
  478. pmt.tracks.push({
  479. timelineStartInfo: {
  480. baseMediaDecodeTime: 0
  481. },
  482. id: +programMapTable.video,
  483. codec: 'avc',
  484. type: 'video'
  485. });
  486. }
  487. if (programMapTable.audio !== null) {
  488. pmt.tracks.push({
  489. timelineStartInfo: {
  490. baseMediaDecodeTime: 0
  491. },
  492. id: +programMapTable.audio,
  493. codec: 'adts',
  494. type: 'audio'
  495. });
  496. }
  497. self.trigger('data', pmt);
  498. }
  499. segmentHadPmt = false;
  500. this.flushStreams_();
  501. this.trigger('done');
  502. };
  503. };
  504. ElementaryStream.prototype = new Stream();
  505. var m2ts = {
  506. PAT_PID: 0x0000,
  507. MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH,
  508. TransportPacketStream: TransportPacketStream,
  509. TransportParseStream: TransportParseStream,
  510. ElementaryStream: ElementaryStream,
  511. TimestampRolloverStream: TimestampRolloverStream,
  512. CaptionStream: CaptionStream.CaptionStream,
  513. Cea608Stream: CaptionStream.Cea608Stream,
  514. Cea708Stream: CaptionStream.Cea708Stream,
  515. MetadataStream: require('./metadata-stream')
  516. };
  517. for (var type in StreamTypes) {
  518. if (StreamTypes.hasOwnProperty(type)) {
  519. m2ts[type] = StreamTypes[type];
  520. }
  521. }
  522. module.exports = m2ts;