Skip to content

Commit

Permalink
Add option to keep original timestamps in MP4 transmuxer.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheModMaker committed Jan 5, 2018
1 parent ffc3195 commit 2049f1f
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions lib/mp4/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,20 @@ sumFrameByteLengths = function(array) {
* Constructs a single-track, ISO BMFF media segment from AAC data
* events. The output of this stream can be fed to a SourceBuffer
* configured with a suitable initialization segment.
* @param options {object} transmuxer options object
* @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
* in the source; false to adjust the first segment to start at 0.
*/
AudioSegmentStream = function(track) {
AudioSegmentStream = function(track, options) {
var
adtsFrames = [],
sequenceNumber = 0,
earliestAllowedDts = 0,
audioAppendStartTs = 0,
videoBaseMediaDecodeTime = Infinity;

options = options || {};

AudioSegmentStream.prototype.init.call(this);

this.push = function(data) {
Expand Down Expand Up @@ -170,7 +175,8 @@ AudioSegmentStream = function(track) {
}

frames = this.trimAdtsFramesByEarliestDts_(adtsFrames);
track.baseMediaDecodeTime = calculateTrackBaseMediaDecodeTime(track);
track.baseMediaDecodeTime =
calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);

this.prefixWithSilence_(track, frames);

Expand Down Expand Up @@ -320,6 +326,8 @@ AudioSegmentStream.prototype = new Stream();
* @param options {object} transmuxer options object
* @param options.alignGopsAtEnd {boolean} If true, start from the end of the
* gopsToAlignWith list when attempting to align gop pts
* @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
* in the source; false to adjust the first segment to start at 0.
*/
VideoSegmentStream = function(track, options) {
var
Expand Down Expand Up @@ -474,7 +482,8 @@ VideoSegmentStream = function(track, options) {
// Concatenate the video data and construct the mdat
mdat = mp4.mdat(this.concatenateNalData_(gops));

track.baseMediaDecodeTime = calculateTrackBaseMediaDecodeTime(track);
track.baseMediaDecodeTime =
calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);

this.trigger('processedGopsInfo', gops.map(function(gop) {
return {
Expand Down Expand Up @@ -978,20 +987,23 @@ clearDtsInfo = function(track) {
* DTS the transmuxer has ever seen and the minimum DTS for the
* current track
*/
calculateTrackBaseMediaDecodeTime = function(track) {
calculateTrackBaseMediaDecodeTime = function(track, keepOriginalTimestamps) {
var
baseMediaDecodeTime,
scale,
// Calculate the distance, in time, that this segment starts from the start
// of the timeline (earliest time seen since the transmuxer initialized)
timeSinceStartOfTimeline = track.minSegmentDts - track.timelineStartInfo.dts;
minSegmentDts = track.minSegmentDts;

// Optionally adjust the time so the first segment starts at zero.
if (!keepOriginalTimestamps) {
minSegmentDts -= track.timelineStartInfo.dts;
}

// track.timelineStartInfo.baseMediaDecodeTime is the location, in time, where
// we want the start of the first segment to be placed
baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime;

// Add to that the distance this segment is from the very first
baseMediaDecodeTime += timeSinceStartOfTimeline;
baseMediaDecodeTime += minSegmentDts;

// baseMediaDecodeTime must not become negative
baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
Expand Down Expand Up @@ -1243,7 +1255,7 @@ Transmuxer = function(options) {
};
// hook up the audio segment stream to the first track with aac data
pipeline.coalesceStream.numberOfTracks++;
pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack);
pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
// Set up the final part of the audio pipeline
pipeline.adtsStream
.pipe(pipeline.audioSegmentStream)
Expand Down Expand Up @@ -1354,7 +1366,7 @@ Transmuxer = function(options) {
if (audioTrack && !pipeline.audioSegmentStream) {
// hook up the audio segment stream to the first track with aac data
pipeline.coalesceStream.numberOfTracks++;
pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack);
pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);

// Set up the final part of the audio pipeline
pipeline.adtsStream
Expand Down

0 comments on commit 2049f1f

Please sign in to comment.