First release

This commit is contained in:
Owen Quinlan 2021-07-02 19:29:34 +10:00
commit fa6c85266e
2339 changed files with 761050 additions and 0 deletions

147
node_modules/mux.js/cjs/flv/coalesce-stream.js generated vendored Normal file
View file

@ -0,0 +1,147 @@
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*/
'use strict';
var Stream = require('../utils/stream.js');
/**
* The final stage of the transmuxer that emits the flv tags
* for audio, video, and metadata. Also tranlates in time and
* outputs caption data and id3 cues.
*/
var CoalesceStream = function CoalesceStream(options) {
// Number of Tracks per output segment
// If greater than 1, we combine multiple
// tracks into a single segment
this.numberOfTracks = 0;
this.metadataStream = options.metadataStream;
this.videoTags = [];
this.audioTags = [];
this.videoTrack = null;
this.audioTrack = null;
this.pendingCaptions = [];
this.pendingMetadata = [];
this.pendingTracks = 0;
this.processedTracks = 0;
CoalesceStream.prototype.init.call(this); // Take output from multiple
this.push = function (output) {
// buffer incoming captions until the associated video segment
// finishes
if (output.text) {
return this.pendingCaptions.push(output);
} // buffer incoming id3 tags until the final flush
if (output.frames) {
return this.pendingMetadata.push(output);
}
if (output.track.type === 'video') {
this.videoTrack = output.track;
this.videoTags = output.tags;
this.pendingTracks++;
}
if (output.track.type === 'audio') {
this.audioTrack = output.track;
this.audioTags = output.tags;
this.pendingTracks++;
}
};
};
CoalesceStream.prototype = new Stream();
CoalesceStream.prototype.flush = function (flushSource) {
var id3,
caption,
i,
timelineStartPts,
event = {
tags: {},
captions: [],
captionStreams: {},
metadata: []
};
if (this.pendingTracks < this.numberOfTracks) {
if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
// Return because we haven't received a flush from a data-generating
// portion of the segment (meaning that we have only recieved meta-data
// or captions.)
return;
} else if (this.pendingTracks === 0) {
// In the case where we receive a flush without any data having been
// received we consider it an emitted track for the purposes of coalescing
// `done` events.
// We do this for the case where there is an audio and video track in the
// segment but no audio data. (seen in several playlists with alternate
// audio tracks and no audio present in the main TS segments.)
this.processedTracks++;
if (this.processedTracks < this.numberOfTracks) {
return;
}
}
}
this.processedTracks += this.pendingTracks;
this.pendingTracks = 0;
if (this.processedTracks < this.numberOfTracks) {
return;
}
if (this.videoTrack) {
timelineStartPts = this.videoTrack.timelineStartInfo.pts;
} else if (this.audioTrack) {
timelineStartPts = this.audioTrack.timelineStartInfo.pts;
}
event.tags.videoTags = this.videoTags;
event.tags.audioTags = this.audioTags; // Translate caption PTS times into second offsets into the
// video timeline for the segment, and add track info
for (i = 0; i < this.pendingCaptions.length; i++) {
caption = this.pendingCaptions[i];
caption.startTime = caption.startPts - timelineStartPts;
caption.startTime /= 90e3;
caption.endTime = caption.endPts - timelineStartPts;
caption.endTime /= 90e3;
event.captionStreams[caption.stream] = true;
event.captions.push(caption);
} // Translate ID3 frame PTS times into second offsets into the
// video timeline for the segment
for (i = 0; i < this.pendingMetadata.length; i++) {
id3 = this.pendingMetadata[i];
id3.cueTime = id3.pts - timelineStartPts;
id3.cueTime /= 90e3;
event.metadata.push(id3);
} // We add this to every single emitted segment even though we only need
// it for the first
event.metadata.dispatchType = this.metadataStream.dispatchType; // Reset stream state
this.videoTrack = null;
this.audioTrack = null;
this.videoTags = [];
this.audioTags = [];
this.pendingCaptions.length = 0;
this.pendingMetadata.length = 0;
this.pendingTracks = 0;
this.processedTracks = 0; // Emit the final segment
this.trigger('data', event);
this.trigger('done');
};
module.exports = CoalesceStream;

62
node_modules/mux.js/cjs/flv/flv-header.js generated vendored Normal file
View file

@ -0,0 +1,62 @@
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*/
'use strict';
var FlvTag = require('./flv-tag.js'); // For information on the FLV format, see
// http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf.
// Technically, this function returns the header and a metadata FLV tag
// if duration is greater than zero
// duration in seconds
// @return {object} the bytes of the FLV header as a Uint8Array
var getFlvHeader = function getFlvHeader(duration, audio, video) {
// :ByteArray {
var headBytes = new Uint8Array(3 + 1 + 1 + 4),
head = new DataView(headBytes.buffer),
metadata,
result,
metadataLength; // default arguments
duration = duration || 0;
audio = audio === undefined ? true : audio;
video = video === undefined ? true : video; // signature
head.setUint8(0, 0x46); // 'F'
head.setUint8(1, 0x4c); // 'L'
head.setUint8(2, 0x56); // 'V'
// version
head.setUint8(3, 0x01); // flags
head.setUint8(4, (audio ? 0x04 : 0x00) | (video ? 0x01 : 0x00)); // data offset, should be 9 for FLV v1
head.setUint32(5, headBytes.byteLength); // init the first FLV tag
if (duration <= 0) {
// no duration available so just write the first field of the first
// FLV tag
result = new Uint8Array(headBytes.byteLength + 4);
result.set(headBytes);
result.set([0, 0, 0, 0], headBytes.byteLength);
return result;
} // write out the duration metadata tag
metadata = new FlvTag(FlvTag.METADATA_TAG);
metadata.pts = metadata.dts = 0;
metadata.writeMetaDataDouble('duration', duration);
metadataLength = metadata.finalize().length;
result = new Uint8Array(headBytes.byteLength + metadataLength);
result.set(headBytes);
result.set(head.byteLength, metadataLength);
return result;
};
module.exports = getFlvHeader;

372
node_modules/mux.js/cjs/flv/flv-tag.js generated vendored Normal file
View file

@ -0,0 +1,372 @@
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*
* An object that stores the bytes of an FLV tag and methods for
* querying and manipulating that data.
* @see http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf
*/
'use strict';
var _FlvTag; // (type:uint, extraData:Boolean = false) extends ByteArray
_FlvTag = function FlvTag(type, extraData) {
var // Counter if this is a metadata tag, nal start marker if this is a video
// tag. unused if this is an audio tag
adHoc = 0,
// :uint
// The default size is 16kb but this is not enough to hold iframe
// data and the resizing algorithm costs a bit so we create a larger
// starting buffer for video tags
bufferStartSize = 16384,
// checks whether the FLV tag has enough capacity to accept the proposed
// write and re-allocates the internal buffers if necessary
prepareWrite = function prepareWrite(flv, count) {
var bytes,
minLength = flv.position + count;
if (minLength < flv.bytes.byteLength) {
// there's enough capacity so do nothing
return;
} // allocate a new buffer and copy over the data that will not be modified
bytes = new Uint8Array(minLength * 2);
bytes.set(flv.bytes.subarray(0, flv.position), 0);
flv.bytes = bytes;
flv.view = new DataView(flv.bytes.buffer);
},
// commonly used metadata properties
widthBytes = _FlvTag.widthBytes || new Uint8Array('width'.length),
heightBytes = _FlvTag.heightBytes || new Uint8Array('height'.length),
videocodecidBytes = _FlvTag.videocodecidBytes || new Uint8Array('videocodecid'.length),
i;
if (!_FlvTag.widthBytes) {
// calculating the bytes of common metadata names ahead of time makes the
// corresponding writes faster because we don't have to loop over the
// characters
// re-test with test/perf.html if you're planning on changing this
for (i = 0; i < 'width'.length; i++) {
widthBytes[i] = 'width'.charCodeAt(i);
}
for (i = 0; i < 'height'.length; i++) {
heightBytes[i] = 'height'.charCodeAt(i);
}
for (i = 0; i < 'videocodecid'.length; i++) {
videocodecidBytes[i] = 'videocodecid'.charCodeAt(i);
}
_FlvTag.widthBytes = widthBytes;
_FlvTag.heightBytes = heightBytes;
_FlvTag.videocodecidBytes = videocodecidBytes;
}
this.keyFrame = false; // :Boolean
switch (type) {
case _FlvTag.VIDEO_TAG:
this.length = 16; // Start the buffer at 256k
bufferStartSize *= 6;
break;
case _FlvTag.AUDIO_TAG:
this.length = 13;
this.keyFrame = true;
break;
case _FlvTag.METADATA_TAG:
this.length = 29;
this.keyFrame = true;
break;
default:
throw new Error('Unknown FLV tag type');
}
this.bytes = new Uint8Array(bufferStartSize);
this.view = new DataView(this.bytes.buffer);
this.bytes[0] = type;
this.position = this.length;
this.keyFrame = extraData; // Defaults to false
// presentation timestamp
this.pts = 0; // decoder timestamp
this.dts = 0; // ByteArray#writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0)
this.writeBytes = function (bytes, offset, length) {
var start = offset || 0,
end;
length = length || bytes.byteLength;
end = start + length;
prepareWrite(this, length);
this.bytes.set(bytes.subarray(start, end), this.position);
this.position += length;
this.length = Math.max(this.length, this.position);
}; // ByteArray#writeByte(value:int):void
this.writeByte = function (byte) {
prepareWrite(this, 1);
this.bytes[this.position] = byte;
this.position++;
this.length = Math.max(this.length, this.position);
}; // ByteArray#writeShort(value:int):void
this.writeShort = function (short) {
prepareWrite(this, 2);
this.view.setUint16(this.position, short);
this.position += 2;
this.length = Math.max(this.length, this.position);
}; // Negative index into array
// (pos:uint):int
this.negIndex = function (pos) {
return this.bytes[this.length - pos];
}; // The functions below ONLY work when this[0] == VIDEO_TAG.
// We are not going to check for that because we dont want the overhead
// (nal:ByteArray = null):int
this.nalUnitSize = function () {
if (adHoc === 0) {
return 0;
}
return this.length - (adHoc + 4);
};
this.startNalUnit = function () {
// remember position and add 4 bytes
if (adHoc > 0) {
throw new Error('Attempted to create new NAL wihout closing the old one');
} // reserve 4 bytes for nal unit size
adHoc = this.length;
this.length += 4;
this.position = this.length;
}; // (nal:ByteArray = null):void
this.endNalUnit = function (nalContainer) {
var nalStart, // :uint
nalLength; // :uint
// Rewind to the marker and write the size
if (this.length === adHoc + 4) {
// we started a nal unit, but didnt write one, so roll back the 4 byte size value
this.length -= 4;
} else if (adHoc > 0) {
nalStart = adHoc + 4;
nalLength = this.length - nalStart;
this.position = adHoc;
this.view.setUint32(this.position, nalLength);
this.position = this.length;
if (nalContainer) {
// Add the tag to the NAL unit
nalContainer.push(this.bytes.subarray(nalStart, nalStart + nalLength));
}
}
adHoc = 0;
};
/**
* Write out a 64-bit floating point valued metadata property. This method is
* called frequently during a typical parse and needs to be fast.
*/
// (key:String, val:Number):void
this.writeMetaDataDouble = function (key, val) {
var i;
prepareWrite(this, 2 + key.length + 9); // write size of property name
this.view.setUint16(this.position, key.length);
this.position += 2; // this next part looks terrible but it improves parser throughput by
// 10kB/s in my testing
// write property name
if (key === 'width') {
this.bytes.set(widthBytes, this.position);
this.position += 5;
} else if (key === 'height') {
this.bytes.set(heightBytes, this.position);
this.position += 6;
} else if (key === 'videocodecid') {
this.bytes.set(videocodecidBytes, this.position);
this.position += 12;
} else {
for (i = 0; i < key.length; i++) {
this.bytes[this.position] = key.charCodeAt(i);
this.position++;
}
} // skip null byte
this.position++; // write property value
this.view.setFloat64(this.position, val);
this.position += 8; // update flv tag length
this.length = Math.max(this.length, this.position);
++adHoc;
}; // (key:String, val:Boolean):void
this.writeMetaDataBoolean = function (key, val) {
var i;
prepareWrite(this, 2);
this.view.setUint16(this.position, key.length);
this.position += 2;
for (i = 0; i < key.length; i++) {
// if key.charCodeAt(i) >= 255, handle error
prepareWrite(this, 1);
this.bytes[this.position] = key.charCodeAt(i);
this.position++;
}
prepareWrite(this, 2);
this.view.setUint8(this.position, 0x01);
this.position++;
this.view.setUint8(this.position, val ? 0x01 : 0x00);
this.position++;
this.length = Math.max(this.length, this.position);
++adHoc;
}; // ():ByteArray
this.finalize = function () {
var dtsDelta, // :int
len; // :int
switch (this.bytes[0]) {
// Video Data
case _FlvTag.VIDEO_TAG:
// We only support AVC, 1 = key frame (for AVC, a seekable
// frame), 2 = inter frame (for AVC, a non-seekable frame)
this.bytes[11] = (this.keyFrame || extraData ? 0x10 : 0x20) | 0x07;
this.bytes[12] = extraData ? 0x00 : 0x01;
dtsDelta = this.pts - this.dts;
this.bytes[13] = (dtsDelta & 0x00FF0000) >>> 16;
this.bytes[14] = (dtsDelta & 0x0000FF00) >>> 8;
this.bytes[15] = (dtsDelta & 0x000000FF) >>> 0;
break;
case _FlvTag.AUDIO_TAG:
this.bytes[11] = 0xAF; // 44 kHz, 16-bit stereo
this.bytes[12] = extraData ? 0x00 : 0x01;
break;
case _FlvTag.METADATA_TAG:
this.position = 11;
this.view.setUint8(this.position, 0x02); // String type
this.position++;
this.view.setUint16(this.position, 0x0A); // 10 Bytes
this.position += 2; // set "onMetaData"
this.bytes.set([0x6f, 0x6e, 0x4d, 0x65, 0x74, 0x61, 0x44, 0x61, 0x74, 0x61], this.position);
this.position += 10;
this.bytes[this.position] = 0x08; // Array type
this.position++;
this.view.setUint32(this.position, adHoc);
this.position = this.length;
this.bytes.set([0, 0, 9], this.position);
this.position += 3; // End Data Tag
this.length = this.position;
break;
}
len = this.length - 11; // write the DataSize field
this.bytes[1] = (len & 0x00FF0000) >>> 16;
this.bytes[2] = (len & 0x0000FF00) >>> 8;
this.bytes[3] = (len & 0x000000FF) >>> 0; // write the Timestamp
this.bytes[4] = (this.dts & 0x00FF0000) >>> 16;
this.bytes[5] = (this.dts & 0x0000FF00) >>> 8;
this.bytes[6] = (this.dts & 0x000000FF) >>> 0;
this.bytes[7] = (this.dts & 0xFF000000) >>> 24; // write the StreamID
this.bytes[8] = 0;
this.bytes[9] = 0;
this.bytes[10] = 0; // Sometimes we're at the end of the view and have one slot to write a
// uint32, so, prepareWrite of count 4, since, view is uint8
prepareWrite(this, 4);
this.view.setUint32(this.length, this.length);
this.length += 4;
this.position += 4; // trim down the byte buffer to what is actually being used
this.bytes = this.bytes.subarray(0, this.length);
this.frameTime = _FlvTag.frameTime(this.bytes); // if bytes.bytelength isn't equal to this.length, handle error
return this;
};
};
_FlvTag.AUDIO_TAG = 0x08; // == 8, :uint
_FlvTag.VIDEO_TAG = 0x09; // == 9, :uint
_FlvTag.METADATA_TAG = 0x12; // == 18, :uint
// (tag:ByteArray):Boolean {
_FlvTag.isAudioFrame = function (tag) {
return _FlvTag.AUDIO_TAG === tag[0];
}; // (tag:ByteArray):Boolean {
_FlvTag.isVideoFrame = function (tag) {
return _FlvTag.VIDEO_TAG === tag[0];
}; // (tag:ByteArray):Boolean {
_FlvTag.isMetaData = function (tag) {
return _FlvTag.METADATA_TAG === tag[0];
}; // (tag:ByteArray):Boolean {
_FlvTag.isKeyFrame = function (tag) {
if (_FlvTag.isVideoFrame(tag)) {
return tag[11] === 0x17;
}
if (_FlvTag.isAudioFrame(tag)) {
return true;
}
if (_FlvTag.isMetaData(tag)) {
return true;
}
return false;
}; // (tag:ByteArray):uint {
_FlvTag.frameTime = function (tag) {
var pts = tag[4] << 16; // :uint
pts |= tag[5] << 8;
pts |= tag[6] << 0;
pts |= tag[7] << 24;
return pts;
};
module.exports = _FlvTag;

13
node_modules/mux.js/cjs/flv/index.js generated vendored Normal file
View file

@ -0,0 +1,13 @@
"use strict";
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*/
module.exports = {
tag: require('./flv-tag'),
Transmuxer: require('./transmuxer'),
getFlvHeader: require('./flv-header')
};

30
node_modules/mux.js/cjs/flv/tag-list.js generated vendored Normal file
View file

@ -0,0 +1,30 @@
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*/
'use strict';
var TagList = function TagList() {
var self = this;
this.list = [];
this.push = function (tag) {
this.list.push({
bytes: tag.bytes,
dts: tag.dts,
pts: tag.pts,
keyFrame: tag.keyFrame,
metaDataTag: tag.metaDataTag
});
};
Object.defineProperty(this, 'length', {
get: function get() {
return self.list.length;
}
});
};
module.exports = TagList;

425
node_modules/mux.js/cjs/flv/transmuxer.js generated vendored Normal file
View file

@ -0,0 +1,425 @@
/**
* mux.js
*
* Copyright (c) Brightcove
* Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
*/
'use strict';
var Stream = require('../utils/stream.js');
var FlvTag = require('./flv-tag.js');
var m2ts = require('../m2ts/m2ts.js');
var AdtsStream = require('../codecs/adts.js');
var H264Stream = require('../codecs/h264').H264Stream;
var CoalesceStream = require('./coalesce-stream.js');
var TagList = require('./tag-list.js');
var _Transmuxer, _VideoSegmentStream, _AudioSegmentStream, collectTimelineInfo, metaDataTag, extraDataTag;
/**
* Store information about the start and end of the tracka and the
* duration for each frame/sample we process in order to calculate
* the baseMediaDecodeTime
*/
collectTimelineInfo = function collectTimelineInfo(track, data) {
if (typeof data.pts === 'number') {
if (track.timelineStartInfo.pts === undefined) {
track.timelineStartInfo.pts = data.pts;
} else {
track.timelineStartInfo.pts = Math.min(track.timelineStartInfo.pts, data.pts);
}
}
if (typeof data.dts === 'number') {
if (track.timelineStartInfo.dts === undefined) {
track.timelineStartInfo.dts = data.dts;
} else {
track.timelineStartInfo.dts = Math.min(track.timelineStartInfo.dts, data.dts);
}
}
};
metaDataTag = function metaDataTag(track, pts) {
var tag = new FlvTag(FlvTag.METADATA_TAG); // :FlvTag
tag.dts = pts;
tag.pts = pts;
tag.writeMetaDataDouble('videocodecid', 7);
tag.writeMetaDataDouble('width', track.width);
tag.writeMetaDataDouble('height', track.height);
return tag;
};
extraDataTag = function extraDataTag(track, pts) {
var i,
tag = new FlvTag(FlvTag.VIDEO_TAG, true);
tag.dts = pts;
tag.pts = pts;
tag.writeByte(0x01); // version
tag.writeByte(track.profileIdc); // profile
tag.writeByte(track.profileCompatibility); // compatibility
tag.writeByte(track.levelIdc); // level
tag.writeByte(0xFC | 0x03); // reserved (6 bits), NULA length size - 1 (2 bits)
tag.writeByte(0xE0 | 0x01); // reserved (3 bits), num of SPS (5 bits)
tag.writeShort(track.sps[0].length); // data of SPS
tag.writeBytes(track.sps[0]); // SPS
tag.writeByte(track.pps.length); // num of PPS (will there ever be more that 1 PPS?)
for (i = 0; i < track.pps.length; ++i) {
tag.writeShort(track.pps[i].length); // 2 bytes for length of PPS
tag.writeBytes(track.pps[i]); // data of PPS
}
return tag;
};
/**
* Constructs a single-track, media segment from AAC data
* events. The output of this stream can be fed to flash.
*/
_AudioSegmentStream = function AudioSegmentStream(track) {
var adtsFrames = [],
videoKeyFrames = [],
oldExtraData;
_AudioSegmentStream.prototype.init.call(this);
this.push = function (data) {
collectTimelineInfo(track, data);
if (track) {
track.audioobjecttype = data.audioobjecttype;
track.channelcount = data.channelcount;
track.samplerate = data.samplerate;
track.samplingfrequencyindex = data.samplingfrequencyindex;
track.samplesize = data.samplesize;
track.extraData = track.audioobjecttype << 11 | track.samplingfrequencyindex << 7 | track.channelcount << 3;
}
data.pts = Math.round(data.pts / 90);
data.dts = Math.round(data.dts / 90); // buffer audio data until end() is called
adtsFrames.push(data);
};
this.flush = function () {
var currentFrame,
adtsFrame,
lastMetaPts,
tags = new TagList(); // return early if no audio data has been observed
if (adtsFrames.length === 0) {
this.trigger('done', 'AudioSegmentStream');
return;
}
lastMetaPts = -Infinity;
while (adtsFrames.length) {
currentFrame = adtsFrames.shift(); // write out a metadata frame at every video key frame
if (videoKeyFrames.length && currentFrame.pts >= videoKeyFrames[0]) {
lastMetaPts = videoKeyFrames.shift();
this.writeMetaDataTags(tags, lastMetaPts);
} // also write out metadata tags every 1 second so that the decoder
// is re-initialized quickly after seeking into a different
// audio configuration.
if (track.extraData !== oldExtraData || currentFrame.pts - lastMetaPts >= 1000) {
this.writeMetaDataTags(tags, currentFrame.pts);
oldExtraData = track.extraData;
lastMetaPts = currentFrame.pts;
}
adtsFrame = new FlvTag(FlvTag.AUDIO_TAG);
adtsFrame.pts = currentFrame.pts;
adtsFrame.dts = currentFrame.dts;
adtsFrame.writeBytes(currentFrame.data);
tags.push(adtsFrame.finalize());
}
videoKeyFrames.length = 0;
oldExtraData = null;
this.trigger('data', {
track: track,
tags: tags.list
});
this.trigger('done', 'AudioSegmentStream');
};
this.writeMetaDataTags = function (tags, pts) {
var adtsFrame;
adtsFrame = new FlvTag(FlvTag.METADATA_TAG); // For audio, DTS is always the same as PTS. We want to set the DTS
// however so we can compare with video DTS to determine approximate
// packet order
adtsFrame.pts = pts;
adtsFrame.dts = pts; // AAC is always 10
adtsFrame.writeMetaDataDouble('audiocodecid', 10);
adtsFrame.writeMetaDataBoolean('stereo', track.channelcount === 2);
adtsFrame.writeMetaDataDouble('audiosamplerate', track.samplerate); // Is AAC always 16 bit?
adtsFrame.writeMetaDataDouble('audiosamplesize', 16);
tags.push(adtsFrame.finalize());
adtsFrame = new FlvTag(FlvTag.AUDIO_TAG, true); // For audio, DTS is always the same as PTS. We want to set the DTS
// however so we can compare with video DTS to determine approximate
// packet order
adtsFrame.pts = pts;
adtsFrame.dts = pts;
adtsFrame.view.setUint16(adtsFrame.position, track.extraData);
adtsFrame.position += 2;
adtsFrame.length = Math.max(adtsFrame.length, adtsFrame.position);
tags.push(adtsFrame.finalize());
};
this.onVideoKeyFrame = function (pts) {
videoKeyFrames.push(pts);
};
};
_AudioSegmentStream.prototype = new Stream();
/**
* Store FlvTags for the h264 stream
* @param track {object} track metadata configuration
*/
_VideoSegmentStream = function VideoSegmentStream(track) {
var nalUnits = [],
config,
h264Frame;
_VideoSegmentStream.prototype.init.call(this);
this.finishFrame = function (tags, frame) {
if (!frame) {
return;
} // Check if keyframe and the length of tags.
// This makes sure we write metadata on the first frame of a segment.
if (config && track && track.newMetadata && (frame.keyFrame || tags.length === 0)) {
// Push extra data on every IDR frame in case we did a stream change + seek
var metaTag = metaDataTag(config, frame.dts).finalize();
var extraTag = extraDataTag(track, frame.dts).finalize();
metaTag.metaDataTag = extraTag.metaDataTag = true;
tags.push(metaTag);
tags.push(extraTag);
track.newMetadata = false;
this.trigger('keyframe', frame.dts);
}
frame.endNalUnit();
tags.push(frame.finalize());
h264Frame = null;
};
this.push = function (data) {
collectTimelineInfo(track, data);
data.pts = Math.round(data.pts / 90);
data.dts = Math.round(data.dts / 90); // buffer video until flush() is called
nalUnits.push(data);
};
this.flush = function () {
var currentNal,
tags = new TagList(); // Throw away nalUnits at the start of the byte stream until we find
// the first AUD
while (nalUnits.length) {
if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
break;
}
nalUnits.shift();
} // return early if no video data has been observed
if (nalUnits.length === 0) {
this.trigger('done', 'VideoSegmentStream');
return;
}
while (nalUnits.length) {
currentNal = nalUnits.shift(); // record the track config
if (currentNal.nalUnitType === 'seq_parameter_set_rbsp') {
track.newMetadata = true;
config = currentNal.config;
track.width = config.width;
track.height = config.height;
track.sps = [currentNal.data];
track.profileIdc = config.profileIdc;
track.levelIdc = config.levelIdc;
track.profileCompatibility = config.profileCompatibility;
h264Frame.endNalUnit();
} else if (currentNal.nalUnitType === 'pic_parameter_set_rbsp') {
track.newMetadata = true;
track.pps = [currentNal.data];
h264Frame.endNalUnit();
} else if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
if (h264Frame) {
this.finishFrame(tags, h264Frame);
}
h264Frame = new FlvTag(FlvTag.VIDEO_TAG);
h264Frame.pts = currentNal.pts;
h264Frame.dts = currentNal.dts;
} else {
if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
// the current sample is a key frame
h264Frame.keyFrame = true;
}
h264Frame.endNalUnit();
}
h264Frame.startNalUnit();
h264Frame.writeBytes(currentNal.data);
}
if (h264Frame) {
this.finishFrame(tags, h264Frame);
}
this.trigger('data', {
track: track,
tags: tags.list
}); // Continue with the flush process now
this.trigger('done', 'VideoSegmentStream');
};
};
_VideoSegmentStream.prototype = new Stream();
/**
* An object that incrementally transmuxes MPEG2 Trasport Stream
* chunks into an FLV.
*/
_Transmuxer = function Transmuxer(options) {
var self = this,
packetStream,
parseStream,
elementaryStream,
videoTimestampRolloverStream,
audioTimestampRolloverStream,
timedMetadataTimestampRolloverStream,
adtsStream,
h264Stream,
videoSegmentStream,
audioSegmentStream,
captionStream,
coalesceStream;
_Transmuxer.prototype.init.call(this);
options = options || {}; // expose the metadata stream
this.metadataStream = new m2ts.MetadataStream();
options.metadataStream = this.metadataStream; // set up the parsing pipeline
packetStream = new m2ts.TransportPacketStream();
parseStream = new m2ts.TransportParseStream();
elementaryStream = new m2ts.ElementaryStream();
videoTimestampRolloverStream = new m2ts.TimestampRolloverStream('video');
audioTimestampRolloverStream = new m2ts.TimestampRolloverStream('audio');
timedMetadataTimestampRolloverStream = new m2ts.TimestampRolloverStream('timed-metadata');
adtsStream = new AdtsStream();
h264Stream = new H264Stream();
coalesceStream = new CoalesceStream(options); // disassemble MPEG2-TS packets into elementary streams
packetStream.pipe(parseStream).pipe(elementaryStream); // !!THIS ORDER IS IMPORTANT!!
// demux the streams
elementaryStream.pipe(videoTimestampRolloverStream).pipe(h264Stream);
elementaryStream.pipe(audioTimestampRolloverStream).pipe(adtsStream);
elementaryStream.pipe(timedMetadataTimestampRolloverStream).pipe(this.metadataStream).pipe(coalesceStream); // if CEA-708 parsing is available, hook up a caption stream
captionStream = new m2ts.CaptionStream(options);
h264Stream.pipe(captionStream).pipe(coalesceStream); // hook up the segment streams once track metadata is delivered
elementaryStream.on('data', function (data) {
var i, videoTrack, audioTrack;
if (data.type === 'metadata') {
i = data.tracks.length; // scan the tracks listed in the metadata
while (i--) {
if (data.tracks[i].type === 'video') {
videoTrack = data.tracks[i];
} else if (data.tracks[i].type === 'audio') {
audioTrack = data.tracks[i];
}
} // hook up the video segment stream to the first track with h264 data
if (videoTrack && !videoSegmentStream) {
coalesceStream.numberOfTracks++;
videoSegmentStream = new _VideoSegmentStream(videoTrack); // Set up the final part of the video pipeline
h264Stream.pipe(videoSegmentStream).pipe(coalesceStream);
}
if (audioTrack && !audioSegmentStream) {
// hook up the audio segment stream to the first track with aac data
coalesceStream.numberOfTracks++;
audioSegmentStream = new _AudioSegmentStream(audioTrack); // Set up the final part of the audio pipeline
adtsStream.pipe(audioSegmentStream).pipe(coalesceStream);
if (videoSegmentStream) {
videoSegmentStream.on('keyframe', audioSegmentStream.onVideoKeyFrame);
}
}
}
}); // feed incoming data to the front of the parsing pipeline
this.push = function (data) {
packetStream.push(data);
}; // flush any buffered data
this.flush = function () {
// Start at the top of the pipeline and flush all pending work
packetStream.flush();
}; // Caption data has to be reset when seeking outside buffered range
this.resetCaptions = function () {
captionStream.reset();
}; // Re-emit any data coming from the coalesce stream to the outside world
coalesceStream.on('data', function (event) {
self.trigger('data', event);
}); // Let the consumer know we have finished flushing the entire pipeline
coalesceStream.on('done', function () {
self.trigger('done');
});
};
_Transmuxer.prototype = new Stream(); // forward compatibility
module.exports = _Transmuxer;