transmuxer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. 'use strict';
  2. var Stream = require('../utils/stream.js');
  3. var FlvTag = require('./flv-tag.js');
  4. var m2ts = require('../m2ts/m2ts.js');
  5. var AdtsStream = require('../codecs/adts.js');
  6. var H264Stream = require('../codecs/h264').H264Stream;
  7. var CoalesceStream = require('./coalesce-stream.js');
  8. var TagList = require('./tag-list.js');
  9. var
  10. Transmuxer,
  11. VideoSegmentStream,
  12. AudioSegmentStream,
  13. collectTimelineInfo,
  14. metaDataTag,
  15. extraDataTag;
  16. /**
  17. * Store information about the start and end of the tracka and the
  18. * duration for each frame/sample we process in order to calculate
  19. * the baseMediaDecodeTime
  20. */
  21. collectTimelineInfo = function(track, data) {
  22. if (typeof data.pts === 'number') {
  23. if (track.timelineStartInfo.pts === undefined) {
  24. track.timelineStartInfo.pts = data.pts;
  25. } else {
  26. track.timelineStartInfo.pts =
  27. Math.min(track.timelineStartInfo.pts, data.pts);
  28. }
  29. }
  30. if (typeof data.dts === 'number') {
  31. if (track.timelineStartInfo.dts === undefined) {
  32. track.timelineStartInfo.dts = data.dts;
  33. } else {
  34. track.timelineStartInfo.dts =
  35. Math.min(track.timelineStartInfo.dts, data.dts);
  36. }
  37. }
  38. };
  39. metaDataTag = function(track, pts) {
  40. var
  41. tag = new FlvTag(FlvTag.METADATA_TAG); // :FlvTag
  42. tag.dts = pts;
  43. tag.pts = pts;
  44. tag.writeMetaDataDouble('videocodecid', 7);
  45. tag.writeMetaDataDouble('width', track.width);
  46. tag.writeMetaDataDouble('height', track.height);
  47. return tag;
  48. };
  49. extraDataTag = function(track, pts) {
  50. var
  51. i,
  52. tag = new FlvTag(FlvTag.VIDEO_TAG, true);
  53. tag.dts = pts;
  54. tag.pts = pts;
  55. tag.writeByte(0x01);// version
  56. tag.writeByte(track.profileIdc);// profile
  57. tag.writeByte(track.profileCompatibility);// compatibility
  58. tag.writeByte(track.levelIdc);// level
  59. tag.writeByte(0xFC | 0x03); // reserved (6 bits), NULA length size - 1 (2 bits)
  60. tag.writeByte(0xE0 | 0x01); // reserved (3 bits), num of SPS (5 bits)
  61. tag.writeShort(track.sps[0].length); // data of SPS
  62. tag.writeBytes(track.sps[0]); // SPS
  63. tag.writeByte(track.pps.length); // num of PPS (will there ever be more that 1 PPS?)
  64. for (i = 0; i < track.pps.length; ++i) {
  65. tag.writeShort(track.pps[i].length); // 2 bytes for length of PPS
  66. tag.writeBytes(track.pps[i]); // data of PPS
  67. }
  68. return tag;
  69. };
  70. /**
  71. * Constructs a single-track, media segment from AAC data
  72. * events. The output of this stream can be fed to flash.
  73. */
  74. AudioSegmentStream = function(track) {
  75. var
  76. adtsFrames = [],
  77. videoKeyFrames = [],
  78. oldExtraData;
  79. AudioSegmentStream.prototype.init.call(this);
  80. this.push = function(data) {
  81. collectTimelineInfo(track, data);
  82. if (track) {
  83. track.audioobjecttype = data.audioobjecttype;
  84. track.channelcount = data.channelcount;
  85. track.samplerate = data.samplerate;
  86. track.samplingfrequencyindex = data.samplingfrequencyindex;
  87. track.samplesize = data.samplesize;
  88. track.extraData = (track.audioobjecttype << 11) |
  89. (track.samplingfrequencyindex << 7) |
  90. (track.channelcount << 3);
  91. }
  92. data.pts = Math.round(data.pts / 90);
  93. data.dts = Math.round(data.dts / 90);
  94. // buffer audio data until end() is called
  95. adtsFrames.push(data);
  96. };
  97. this.flush = function() {
  98. var currentFrame, adtsFrame, lastMetaPts, tags = new TagList();
  99. // return early if no audio data has been observed
  100. if (adtsFrames.length === 0) {
  101. this.trigger('done', 'AudioSegmentStream');
  102. return;
  103. }
  104. lastMetaPts = -Infinity;
  105. while (adtsFrames.length) {
  106. currentFrame = adtsFrames.shift();
  107. // write out a metadata frame at every video key frame
  108. if (videoKeyFrames.length && currentFrame.pts >= videoKeyFrames[0]) {
  109. lastMetaPts = videoKeyFrames.shift();
  110. this.writeMetaDataTags(tags, lastMetaPts);
  111. }
  112. // also write out metadata tags every 1 second so that the decoder
  113. // is re-initialized quickly after seeking into a different
  114. // audio configuration.
  115. if (track.extraData !== oldExtraData || currentFrame.pts - lastMetaPts >= 1000) {
  116. this.writeMetaDataTags(tags, currentFrame.pts);
  117. oldExtraData = track.extraData;
  118. lastMetaPts = currentFrame.pts;
  119. }
  120. adtsFrame = new FlvTag(FlvTag.AUDIO_TAG);
  121. adtsFrame.pts = currentFrame.pts;
  122. adtsFrame.dts = currentFrame.dts;
  123. adtsFrame.writeBytes(currentFrame.data);
  124. tags.push(adtsFrame.finalize());
  125. }
  126. videoKeyFrames.length = 0;
  127. oldExtraData = null;
  128. this.trigger('data', {track: track, tags: tags.list});
  129. this.trigger('done', 'AudioSegmentStream');
  130. };
  131. this.writeMetaDataTags = function(tags, pts) {
  132. var adtsFrame;
  133. adtsFrame = new FlvTag(FlvTag.METADATA_TAG);
  134. // For audio, DTS is always the same as PTS. We want to set the DTS
  135. // however so we can compare with video DTS to determine approximate
  136. // packet order
  137. adtsFrame.pts = pts;
  138. adtsFrame.dts = pts;
  139. // AAC is always 10
  140. adtsFrame.writeMetaDataDouble('audiocodecid', 10);
  141. adtsFrame.writeMetaDataBoolean('stereo', track.channelcount === 2);
  142. adtsFrame.writeMetaDataDouble('audiosamplerate', track.samplerate);
  143. // Is AAC always 16 bit?
  144. adtsFrame.writeMetaDataDouble('audiosamplesize', 16);
  145. tags.push(adtsFrame.finalize());
  146. adtsFrame = new FlvTag(FlvTag.AUDIO_TAG, true);
  147. // For audio, DTS is always the same as PTS. We want to set the DTS
  148. // however so we can compare with video DTS to determine approximate
  149. // packet order
  150. adtsFrame.pts = pts;
  151. adtsFrame.dts = pts;
  152. adtsFrame.view.setUint16(adtsFrame.position, track.extraData);
  153. adtsFrame.position += 2;
  154. adtsFrame.length = Math.max(adtsFrame.length, adtsFrame.position);
  155. tags.push(adtsFrame.finalize());
  156. };
  157. this.onVideoKeyFrame = function(pts) {
  158. videoKeyFrames.push(pts);
  159. };
  160. };
  161. AudioSegmentStream.prototype = new Stream();
  162. /**
  163. * Store FlvTags for the h264 stream
  164. * @param track {object} track metadata configuration
  165. */
  166. VideoSegmentStream = function(track) {
  167. var
  168. nalUnits = [],
  169. config,
  170. h264Frame;
  171. VideoSegmentStream.prototype.init.call(this);
  172. this.finishFrame = function(tags, frame) {
  173. if (!frame) {
  174. return;
  175. }
  176. // Check if keyframe and the length of tags.
  177. // This makes sure we write metadata on the first frame of a segment.
  178. if (config && track && track.newMetadata &&
  179. (frame.keyFrame || tags.length === 0)) {
  180. // Push extra data on every IDR frame in case we did a stream change + seek
  181. var metaTag = metaDataTag(config, frame.dts).finalize();
  182. var extraTag = extraDataTag(track, frame.dts).finalize();
  183. metaTag.metaDataTag = extraTag.metaDataTag = true;
  184. tags.push(metaTag);
  185. tags.push(extraTag);
  186. track.newMetadata = false;
  187. this.trigger('keyframe', frame.dts);
  188. }
  189. frame.endNalUnit();
  190. tags.push(frame.finalize());
  191. h264Frame = null;
  192. };
  193. this.push = function(data) {
  194. collectTimelineInfo(track, data);
  195. data.pts = Math.round(data.pts / 90);
  196. data.dts = Math.round(data.dts / 90);
  197. // buffer video until flush() is called
  198. nalUnits.push(data);
  199. };
  200. this.flush = function() {
  201. var
  202. currentNal,
  203. tags = new TagList();
  204. // Throw away nalUnits at the start of the byte stream until we find
  205. // the first AUD
  206. while (nalUnits.length) {
  207. if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
  208. break;
  209. }
  210. nalUnits.shift();
  211. }
  212. // return early if no video data has been observed
  213. if (nalUnits.length === 0) {
  214. this.trigger('done', 'VideoSegmentStream');
  215. return;
  216. }
  217. while (nalUnits.length) {
  218. currentNal = nalUnits.shift();
  219. // record the track config
  220. if (currentNal.nalUnitType === 'seq_parameter_set_rbsp') {
  221. track.newMetadata = true;
  222. config = currentNal.config;
  223. track.width = config.width;
  224. track.height = config.height;
  225. track.sps = [currentNal.data];
  226. track.profileIdc = config.profileIdc;
  227. track.levelIdc = config.levelIdc;
  228. track.profileCompatibility = config.profileCompatibility;
  229. h264Frame.endNalUnit();
  230. } else if (currentNal.nalUnitType === 'pic_parameter_set_rbsp') {
  231. track.newMetadata = true;
  232. track.pps = [currentNal.data];
  233. h264Frame.endNalUnit();
  234. } else if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
  235. if (h264Frame) {
  236. this.finishFrame(tags, h264Frame);
  237. }
  238. h264Frame = new FlvTag(FlvTag.VIDEO_TAG);
  239. h264Frame.pts = currentNal.pts;
  240. h264Frame.dts = currentNal.dts;
  241. } else {
  242. if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
  243. // the current sample is a key frame
  244. h264Frame.keyFrame = true;
  245. }
  246. h264Frame.endNalUnit();
  247. }
  248. h264Frame.startNalUnit();
  249. h264Frame.writeBytes(currentNal.data);
  250. }
  251. if (h264Frame) {
  252. this.finishFrame(tags, h264Frame);
  253. }
  254. this.trigger('data', {track: track, tags: tags.list});
  255. // Continue with the flush process now
  256. this.trigger('done', 'VideoSegmentStream');
  257. };
  258. };
  259. VideoSegmentStream.prototype = new Stream();
  260. /**
  261. * An object that incrementally transmuxes MPEG2 Trasport Stream
  262. * chunks into an FLV.
  263. */
  264. Transmuxer = function(options) {
  265. var
  266. self = this,
  267. packetStream, parseStream, elementaryStream,
  268. videoTimestampRolloverStream, audioTimestampRolloverStream,
  269. timedMetadataTimestampRolloverStream,
  270. adtsStream, h264Stream,
  271. videoSegmentStream, audioSegmentStream, captionStream,
  272. coalesceStream;
  273. Transmuxer.prototype.init.call(this);
  274. options = options || {};
  275. // expose the metadata stream
  276. this.metadataStream = new m2ts.MetadataStream();
  277. options.metadataStream = this.metadataStream;
  278. // set up the parsing pipeline
  279. packetStream = new m2ts.TransportPacketStream();
  280. parseStream = new m2ts.TransportParseStream();
  281. elementaryStream = new m2ts.ElementaryStream();
  282. videoTimestampRolloverStream = new m2ts.TimestampRolloverStream('video');
  283. audioTimestampRolloverStream = new m2ts.TimestampRolloverStream('audio');
  284. timedMetadataTimestampRolloverStream = new m2ts.TimestampRolloverStream('timed-metadata');
  285. adtsStream = new AdtsStream();
  286. h264Stream = new H264Stream();
  287. coalesceStream = new CoalesceStream(options);
  288. // disassemble MPEG2-TS packets into elementary streams
  289. packetStream
  290. .pipe(parseStream)
  291. .pipe(elementaryStream);
  292. // !!THIS ORDER IS IMPORTANT!!
  293. // demux the streams
  294. elementaryStream
  295. .pipe(videoTimestampRolloverStream)
  296. .pipe(h264Stream);
  297. elementaryStream
  298. .pipe(audioTimestampRolloverStream)
  299. .pipe(adtsStream);
  300. elementaryStream
  301. .pipe(timedMetadataTimestampRolloverStream)
  302. .pipe(this.metadataStream)
  303. .pipe(coalesceStream);
  304. // if CEA-708 parsing is available, hook up a caption stream
  305. captionStream = new m2ts.CaptionStream();
  306. h264Stream.pipe(captionStream)
  307. .pipe(coalesceStream);
  308. // hook up the segment streams once track metadata is delivered
  309. elementaryStream.on('data', function(data) {
  310. var i, videoTrack, audioTrack;
  311. if (data.type === 'metadata') {
  312. i = data.tracks.length;
  313. // scan the tracks listed in the metadata
  314. while (i--) {
  315. if (data.tracks[i].type === 'video') {
  316. videoTrack = data.tracks[i];
  317. } else if (data.tracks[i].type === 'audio') {
  318. audioTrack = data.tracks[i];
  319. }
  320. }
  321. // hook up the video segment stream to the first track with h264 data
  322. if (videoTrack && !videoSegmentStream) {
  323. coalesceStream.numberOfTracks++;
  324. videoSegmentStream = new VideoSegmentStream(videoTrack);
  325. // Set up the final part of the video pipeline
  326. h264Stream
  327. .pipe(videoSegmentStream)
  328. .pipe(coalesceStream);
  329. }
  330. if (audioTrack && !audioSegmentStream) {
  331. // hook up the audio segment stream to the first track with aac data
  332. coalesceStream.numberOfTracks++;
  333. audioSegmentStream = new AudioSegmentStream(audioTrack);
  334. // Set up the final part of the audio pipeline
  335. adtsStream
  336. .pipe(audioSegmentStream)
  337. .pipe(coalesceStream);
  338. if (videoSegmentStream) {
  339. videoSegmentStream.on('keyframe', audioSegmentStream.onVideoKeyFrame);
  340. }
  341. }
  342. }
  343. });
  344. // feed incoming data to the front of the parsing pipeline
  345. this.push = function(data) {
  346. packetStream.push(data);
  347. };
  348. // flush any buffered data
  349. this.flush = function() {
  350. // Start at the top of the pipeline and flush all pending work
  351. packetStream.flush();
  352. };
  353. // Caption data has to be reset when seeking outside buffered range
  354. this.resetCaptions = function() {
  355. captionStream.reset();
  356. };
  357. // Re-emit any data coming from the coalesce stream to the outside world
  358. coalesceStream.on('data', function(event) {
  359. self.trigger('data', event);
  360. });
  361. // Let the consumer know we have finished flushing the entire pipeline
  362. coalesceStream.on('done', function() {
  363. self.trigger('done');
  364. });
  365. };
  366. Transmuxer.prototype = new Stream();
  367. // forward compatibility
  368. module.exports = Transmuxer;