transmuxer.js
11.5 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
var Stream = require('../utils/stream.js');
var m2ts = require('../m2ts/m2ts.js');
var codecs = require('../codecs/index.js');
var AudioSegmentStream = require('./audio-segment-stream.js');
var VideoSegmentStream = require('./video-segment-stream.js');
var trackInfo = require('../mp4/track-decode-info.js');
var isLikelyAacData = require('../aac/utils').isLikelyAacData;
var AdtsStream = require('../codecs/adts');
var AacStream = require('../aac/index');
var clock = require('../utils/clock');
var createPipeline = function(object) {
object.prototype = new Stream();
object.prototype.init.call(object);
return object;
};
var tsPipeline = function(options) {
var
pipeline = {
type: 'ts',
tracks: {
audio: null,
video: null
},
packet: new m2ts.TransportPacketStream(),
parse: new m2ts.TransportParseStream(),
elementary: new m2ts.ElementaryStream(),
timestampRollover: new m2ts.TimestampRolloverStream(),
adts: new codecs.Adts(),
h264: new codecs.h264.H264Stream(),
captionStream: new m2ts.CaptionStream(options),
metadataStream: new m2ts.MetadataStream()
};
pipeline.headOfPipeline = pipeline.packet;
// Transport Stream
pipeline.packet
.pipe(pipeline.parse)
.pipe(pipeline.elementary)
.pipe(pipeline.timestampRollover);
// H264
pipeline.timestampRollover
.pipe(pipeline.h264);
// Hook up CEA-608/708 caption stream
pipeline.h264
.pipe(pipeline.captionStream);
pipeline.timestampRollover
.pipe(pipeline.metadataStream);
// ADTS
pipeline.timestampRollover
.pipe(pipeline.adts);
pipeline.elementary.on('data', function(data) {
if (data.type !== 'metadata') {
return;
}
for (var i = 0; i < data.tracks.length; i++) {
if (!pipeline.tracks[data.tracks[i].type]) {
pipeline.tracks[data.tracks[i].type] = data.tracks[i];
pipeline.tracks[data.tracks[i].type].timelineStartInfo.baseMediaDecodeTime = options.baseMediaDecodeTime;
}
}
if (pipeline.tracks.video && !pipeline.videoSegmentStream) {
pipeline.videoSegmentStream = new VideoSegmentStream(pipeline.tracks.video, options);
pipeline.videoSegmentStream.on('timelineStartInfo', function(timelineStartInfo) {
if (pipeline.tracks.audio && !options.keepOriginalTimestamps) {
pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - options.baseMediaDecodeTime);
}
});
pipeline.videoSegmentStream.on('timingInfo',
pipeline.trigger.bind(pipeline, 'videoTimingInfo'));
pipeline.videoSegmentStream.on('data', function(data) {
pipeline.trigger('data', {
type: 'video',
data: data
});
});
pipeline.videoSegmentStream.on('done',
pipeline.trigger.bind(pipeline, 'done'));
pipeline.videoSegmentStream.on('partialdone',
pipeline.trigger.bind(pipeline, 'partialdone'));
pipeline.videoSegmentStream.on('endedtimeline',
pipeline.trigger.bind(pipeline, 'endedtimeline'));
pipeline.h264
.pipe(pipeline.videoSegmentStream);
}
if (pipeline.tracks.audio && !pipeline.audioSegmentStream) {
pipeline.audioSegmentStream = new AudioSegmentStream(pipeline.tracks.audio, options);
pipeline.audioSegmentStream.on('data', function(data) {
pipeline.trigger('data', {
type: 'audio',
data: data
});
});
pipeline.audioSegmentStream.on('done',
pipeline.trigger.bind(pipeline, 'done'));
pipeline.audioSegmentStream.on('partialdone',
pipeline.trigger.bind(pipeline, 'partialdone'));
pipeline.audioSegmentStream.on('endedtimeline',
pipeline.trigger.bind(pipeline, 'endedtimeline'));
pipeline.audioSegmentStream.on('timingInfo',
pipeline.trigger.bind(pipeline, 'audioTimingInfo'));
pipeline.adts
.pipe(pipeline.audioSegmentStream);
}
// emit pmt info
pipeline.trigger('trackinfo', {
hasAudio: !!pipeline.tracks.audio,
hasVideo: !!pipeline.tracks.video
});
});
pipeline.captionStream.on('data', function(caption) {
var timelineStartPts;
if (pipeline.tracks.video) {
timelineStartPts = pipeline.tracks.video.timelineStartInfo.pts || 0;
} else {
// This will only happen if we encounter caption packets before
// video data in a segment. This is an unusual/unlikely scenario,
// so we assume the timeline starts at zero for now.
timelineStartPts = 0;
}
// Translate caption PTS times into second offsets into the
// video timeline for the segment
caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, options.keepOriginalTimestamps);
caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, options.keepOriginalTimestamps);
pipeline.trigger('caption', caption);
});
pipeline = createPipeline(pipeline);
pipeline.metadataStream.on('data', pipeline.trigger.bind(pipeline, 'id3Frame'));
return pipeline;
};
var aacPipeline = function(options) {
var
pipeline = {
type: 'aac',
tracks: {
audio: null
},
metadataStream: new m2ts.MetadataStream(),
aacStream: new AacStream(),
audioRollover: new m2ts.TimestampRolloverStream('audio'),
timedMetadataRollover: new m2ts.TimestampRolloverStream('timed-metadata'),
adtsStream: new AdtsStream(true)
};
// set up the parsing pipeline
pipeline.headOfPipeline = pipeline.aacStream;
pipeline.aacStream
.pipe(pipeline.audioRollover)
.pipe(pipeline.adtsStream);
pipeline.aacStream
.pipe(pipeline.timedMetadataRollover)
.pipe(pipeline.metadataStream);
pipeline.metadataStream.on('timestamp', function(frame) {
pipeline.aacStream.setTimestamp(frame.timeStamp);
});
pipeline.aacStream.on('data', function(data) {
if ((data.type !== 'timed-metadata' && data.type !== 'audio') || pipeline.audioSegmentStream) {
return;
}
pipeline.tracks.audio = pipeline.tracks.audio || {
timelineStartInfo: {
baseMediaDecodeTime: options.baseMediaDecodeTime
},
codec: 'adts',
type: 'audio'
};
// hook up the audio segment stream to the first track with aac data
pipeline.audioSegmentStream = new AudioSegmentStream(pipeline.tracks.audio, options);
pipeline.audioSegmentStream.on('data', function(data) {
pipeline.trigger('data', {
type: 'audio',
data: data
});
});
pipeline.audioSegmentStream.on('partialdone',
pipeline.trigger.bind(pipeline, 'partialdone'));
pipeline.audioSegmentStream.on('done', pipeline.trigger.bind(pipeline, 'done'));
pipeline.audioSegmentStream.on('endedtimeline',
pipeline.trigger.bind(pipeline, 'endedtimeline'));
pipeline.audioSegmentStream.on('timingInfo',
pipeline.trigger.bind(pipeline, 'audioTimingInfo'));
// Set up the final part of the audio pipeline
pipeline.adtsStream
.pipe(pipeline.audioSegmentStream);
pipeline.trigger('trackinfo', {
hasAudio: !!pipeline.tracks.audio,
hasVideo: !!pipeline.tracks.video
});
});
// set the pipeline up as a stream before binding to get access to the trigger function
pipeline = createPipeline(pipeline);
pipeline.metadataStream.on('data', pipeline.trigger.bind(pipeline, 'id3Frame'));
return pipeline;
};
var setupPipelineListeners = function(pipeline, transmuxer) {
pipeline.on('data', transmuxer.trigger.bind(transmuxer, 'data'));
pipeline.on('done', transmuxer.trigger.bind(transmuxer, 'done'));
pipeline.on('partialdone', transmuxer.trigger.bind(transmuxer, 'partialdone'));
pipeline.on('endedtimeline', transmuxer.trigger.bind(transmuxer, 'endedtimeline'));
pipeline.on('audioTimingInfo', transmuxer.trigger.bind(transmuxer, 'audioTimingInfo'));
pipeline.on('videoTimingInfo', transmuxer.trigger.bind(transmuxer, 'videoTimingInfo'));
pipeline.on('trackinfo', transmuxer.trigger.bind(transmuxer, 'trackinfo'));
pipeline.on('id3Frame', function(event) {
// add this to every single emitted segment even though it's only needed for the first
event.dispatchType = pipeline.metadataStream.dispatchType;
// keep original time, can be adjusted if needed at a higher level
event.cueTime = clock.videoTsToSeconds(event.pts);
transmuxer.trigger('id3Frame', event);
});
pipeline.on('caption', function(event) {
transmuxer.trigger('caption', event);
});
};
var Transmuxer = function(options) {
var
pipeline = null,
hasFlushed = true;
options = options || {};
Transmuxer.prototype.init.call(this);
options.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
this.push = function(bytes) {
if (hasFlushed) {
var isAac = isLikelyAacData(bytes);
if (isAac && (!pipeline || pipeline.type !== 'aac')) {
pipeline = aacPipeline(options);
setupPipelineListeners(pipeline, this);
} else if (!isAac && (!pipeline || pipeline.type !== 'ts')) {
pipeline = tsPipeline(options);
setupPipelineListeners(pipeline, this);
}
hasFlushed = false;
}
pipeline.headOfPipeline.push(bytes);
};
this.flush = function() {
if (!pipeline) {
return;
}
hasFlushed = true;
pipeline.headOfPipeline.flush();
};
this.partialFlush = function() {
if (!pipeline) {
return;
}
pipeline.headOfPipeline.partialFlush();
};
this.endTimeline = function() {
if (!pipeline) {
return;
}
pipeline.headOfPipeline.endTimeline();
};
this.reset = function() {
if (!pipeline) {
return;
}
pipeline.headOfPipeline.reset();
};
this.setBaseMediaDecodeTime = function(baseMediaDecodeTime) {
if (!options.keepOriginalTimestamps) {
options.baseMediaDecodeTime = baseMediaDecodeTime;
}
if (!pipeline) {
return;
}
if (pipeline.tracks.audio) {
pipeline.tracks.audio.timelineStartInfo.dts = undefined;
pipeline.tracks.audio.timelineStartInfo.pts = undefined;
trackInfo.clearDtsInfo(pipeline.tracks.audio);
if (pipeline.audioRollover) {
pipeline.audioRollover.discontinuity();
}
}
if (pipeline.tracks.video) {
if (pipeline.videoSegmentStream) {
pipeline.videoSegmentStream.gopCache_ = [];
}
pipeline.tracks.video.timelineStartInfo.dts = undefined;
pipeline.tracks.video.timelineStartInfo.pts = undefined;
trackInfo.clearDtsInfo(pipeline.tracks.video);
// pipeline.captionStream.reset();
}
if (pipeline.timestampRollover) {
pipeline.timestampRollover.discontinuity();
}
};
this.setRemux = function(val) {
options.remux = val;
if (pipeline && pipeline.coalesceStream) {
pipeline.coalesceStream.setRemux(val);
}
};
this.setAudioAppendStart = function(audioAppendStart) {
if (!pipeline || !pipeline.tracks.audio || !pipeline.audioSegmentStream) {
return;
}
pipeline.audioSegmentStream.setAudioAppendStart(audioAppendStart);
};
// TODO GOP alignment support
// Support may be a bit trickier than with full segment appends, as GOPs may be split
// and processed in a more granular fashion
this.alignGopsWith = function(gopsToAlignWith) {
return;
};
};
Transmuxer.prototype = new Stream();
module.exports = Transmuxer;