audio-segment-stream.js
4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
var Stream = require('../utils/stream.js');
var mp4 = require('../mp4/mp4-generator.js');
var audioFrameUtils = require('../mp4/audio-frame-utils');
var trackInfo = require('../mp4/track-decode-info.js');
var ONE_SECOND_IN_TS = require('../utils/clock').ONE_SECOND_IN_TS;
var AUDIO_PROPERTIES = require('../constants/audio-properties.js');
/**
* 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.
*/
var AudioSegmentStream = function(track, options) {
var
adtsFrames = [],
sequenceNumber = 0,
earliestAllowedDts = 0,
audioAppendStartTs = 0,
videoBaseMediaDecodeTime = Infinity,
segmentStartPts = null,
segmentEndPts = null;
options = options || {};
AudioSegmentStream.prototype.init.call(this);
this.push = function(data) {
trackInfo.collectDtsInfo(track, data);
if (track) {
AUDIO_PROPERTIES.forEach(function(prop) {
track[prop] = data[prop];
});
}
// buffer audio data until end() is called
adtsFrames.push(data);
};
this.setEarliestDts = function(earliestDts) {
earliestAllowedDts = earliestDts;
};
this.setVideoBaseMediaDecodeTime = function(baseMediaDecodeTime) {
videoBaseMediaDecodeTime = baseMediaDecodeTime;
};
this.setAudioAppendStart = function(timestamp) {
audioAppendStartTs = timestamp;
};
this.processFrames_ = function() {
var
frames,
moof,
mdat,
boxes,
timingInfo;
// return early if no audio data has been observed
if (adtsFrames.length === 0) {
return;
}
frames = audioFrameUtils.trimAdtsFramesByEarliestDts(
adtsFrames, track, earliestAllowedDts);
if (frames.length === 0) {
// return early if the frames are all after the earliest allowed DTS
// TODO should we clear the adtsFrames?
return;
}
track.baseMediaDecodeTime = trackInfo.calculateTrackBaseMediaDecodeTime(
track, options.keepOriginalTimestamps);
audioFrameUtils.prefixWithSilence(
track, frames, audioAppendStartTs, videoBaseMediaDecodeTime);
// we have to build the index from byte locations to
// samples (that is, adts frames) in the audio data
track.samples = audioFrameUtils.generateSampleTable(frames);
// concatenate the audio data to constuct the mdat
mdat = mp4.mdat(audioFrameUtils.concatenateFrameData(frames));
adtsFrames = [];
moof = mp4.moof(sequenceNumber, [track]);
// bump the sequence number for next time
sequenceNumber++;
track.initSegment = mp4.initSegment([track]);
// it would be great to allocate this array up front instead of
// throwing away hundreds of media segment fragments
boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
boxes.set(moof);
boxes.set(mdat, moof.byteLength);
trackInfo.clearDtsInfo(track);
if (segmentStartPts === null) {
segmentEndPts = segmentStartPts = frames[0].pts;
}
segmentEndPts += frames.length * (ONE_SECOND_IN_TS * 1024 / track.samplerate);
timingInfo = { start: segmentStartPts };
this.trigger('timingInfo', timingInfo);
this.trigger('data', {track: track, boxes: boxes});
};
this.flush = function() {
this.processFrames_();
// trigger final timing info
this.trigger('timingInfo', {
start: segmentStartPts,
end: segmentEndPts
});
this.resetTiming_();
this.trigger('done', 'AudioSegmentStream');
};
this.partialFlush = function() {
this.processFrames_();
this.trigger('partialdone', 'AudioSegmentStream');
};
this.endTimeline = function() {
this.flush();
this.trigger('endedtimeline', 'AudioSegmentStream');
};
this.resetTiming_ = function() {
trackInfo.clearDtsInfo(track);
segmentStartPts = null;
segmentEndPts = null;
};
this.reset = function() {
this.resetTiming_();
adtsFrames = [];
this.trigger('reset');
};
};
AudioSegmentStream.prototype = new Stream();
module.exports = AudioSegmentStream;