Skip to content

Commit 4708fb1

Browse files
committed
feat: use vhs-utils
1 parent f573bdc commit 4708fb1

11 files changed

+48
-250
lines changed

lib/aac/utils.js

+4-45
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,8 @@ var ADTS_SAMPLING_FREQUENCIES = [
2424
7350
2525
];
2626

27-
var parseId3TagSize = function(header, byteIndex) {
28-
var
29-
returnSize = (header[byteIndex + 6] << 21) |
30-
(header[byteIndex + 7] << 14) |
31-
(header[byteIndex + 8] << 7) |
32-
(header[byteIndex + 9]),
33-
flags = header[byteIndex + 5],
34-
footerPresent = (flags & 16) >> 4;
35-
36-
// if we get a negative returnSize clamp it to 0
37-
returnSize = returnSize >= 0 ? returnSize : 0;
38-
39-
if (footerPresent) {
40-
return returnSize + 20;
41-
}
42-
return returnSize + 10;
43-
};
44-
45-
var getId3Offset = function(data, offset) {
46-
if (data.length - offset < 10 ||
47-
data[offset] !== 'I'.charCodeAt(0) ||
48-
data[offset + 1] !== 'D'.charCodeAt(0) ||
49-
data[offset + 2] !== '3'.charCodeAt(0)) {
50-
return offset;
51-
}
52-
53-
offset += parseId3TagSize(data, offset);
54-
55-
return getId3Offset(data, offset);
56-
};
57-
58-
59-
// TODO: use vhs-utils
60-
var isLikelyAacData = function(data) {
61-
var offset = getId3Offset(data, 0);
62-
63-
return data.length >= offset + 2 &&
64-
(data[offset] & 0xFF) === 0xFF &&
65-
(data[offset + 1] & 0xF0) === 0xF0 &&
66-
// verify that the 2 layer bits are 0, aka this
67-
// is not mp3 data but aac data.
68-
(data[offset + 1] & 0x16) === 0x10;
69-
};
27+
var {isLikely} = require('@videojs/vhs-utils/cjs/containers.js');
28+
var {getId3Size} = require('@videojs/vhs-utils/cjs/id3-helpers.js');
7029

7130
var parseSyncSafeInteger = function(data) {
7231
return (data[0] << 21) |
@@ -182,8 +141,8 @@ var parseAacTimestamp = function(packet) {
182141
};
183142

184143
module.exports = {
185-
isLikelyAacData: isLikelyAacData,
186-
parseId3TagSize: parseId3TagSize,
144+
isLikelyAacData: isLikely.aac,
145+
parseId3TagSize: getId3Size,
187146
parseAdtsSize: parseAdtsSize,
188147
parseType: parseType,
189148
parseSampleRate: parseSampleRate,

lib/mp4/caption-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
var discardEmulationPreventionBytes = require('../tools/caption-packet-parser').discardEmulationPreventionBytes;
1313
var CaptionStream = require('../m2ts/caption-stream').CaptionStream;
14-
var findBox = require('../mp4/find-box.js');
14+
var {findBox} = require('@videojs/vhs-utils/cjs/mp4-helpers.js');
1515
var parseTfdt = require('../tools/parse-tfdt.js');
1616
var parseTrun = require('../tools/parse-trun.js');
1717
var parseTfhd = require('../tools/parse-tfhd.js');

lib/mp4/probe.js

+7-94
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
var toUnsigned = require('../utils/bin').toUnsigned;
1212
var toHexString = require('../utils/bin').toHexString;
13-
var findBox = require('../mp4/find-box.js');
13+
var {findBox, parseTracks} = require('@videojs/vhs-utils/cjs/mp4-helpers.js');
1414
var parseType = require('../mp4/parse-type.js');
1515
var parseTfhd = require('../tools/parse-tfhd.js');
1616
var parseTrun = require('../tools/parse-trun.js');
@@ -36,6 +36,7 @@ var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks,
3636
* @return {object} a hash of track ids to timescale values or null if
3737
* the init segment is malformed.
3838
*/
39+
// TODO: remove in a major version
3940
timescale = function(init) {
4041
var
4142
result = {},
@@ -208,6 +209,7 @@ compositionStartTime = function(timescales, fragment) {
208209
*
209210
* @see ISO-BMFF-12/2015, Section 8.4.3
210211
**/
212+
// TODO: remove in major version.
211213
getVideoTrackIds = function(init) {
212214
var traks = findBox(init, ['moov', 'trak']);
213215
var videoTrackIds = [];
@@ -236,6 +238,7 @@ getVideoTrackIds = function(init) {
236238
return videoTrackIds;
237239
};
238240

241+
// TODO: remove in major version
239242
getTimescaleFromMediaHeader = function(mdhd) {
240243
// mdhd is a FullBox, meaning it will have its own version as the first byte
241244
var version = mdhd[0];
@@ -254,100 +257,10 @@ getTimescaleFromMediaHeader = function(mdhd) {
254257
* mp4 segment
255258
*/
256259
getTracks = function(init) {
257-
var traks = findBox(init, ['moov', 'trak']);
258-
var tracks = [];
259-
260-
traks.forEach(function(trak) {
261-
var track = {};
262-
var tkhd = findBox(trak, ['tkhd'])[0];
263-
var view, tkhdVersion;
264-
265-
// id
266-
if (tkhd) {
267-
view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
268-
tkhdVersion = view.getUint8(0);
269-
270-
track.id = (tkhdVersion === 0) ? view.getUint32(12) : view.getUint32(20);
271-
}
272-
273-
var hdlr = findBox(trak, ['mdia', 'hdlr'])[0];
274-
275-
// type
276-
if (hdlr) {
277-
var type = parseType(hdlr.subarray(8, 12));
278-
279-
if (type === 'vide') {
280-
track.type = 'video';
281-
} else if (type === 'soun') {
282-
track.type = 'audio';
283-
} else {
284-
track.type = type;
285-
}
286-
}
287-
288-
289-
// codec
290-
var stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
291-
292-
if (stsd) {
293-
var sampleDescriptions = stsd.subarray(8);
294-
// gives the codec type string
295-
track.codec = parseType(sampleDescriptions.subarray(4, 8));
296-
297-
var codecBox = findBox(sampleDescriptions, [track.codec])[0];
298-
var codecConfig, codecConfigType;
299-
300-
if (codecBox) {
301-
// https://tools.ietf.org/html/rfc6381#section-3.3
302-
if ((/^[a-z]vc[1-9]$/i).test(track.codec)) {
303-
// we don't need anything but the "config" parameter of the
304-
// avc1 codecBox
305-
codecConfig = codecBox.subarray(78);
306-
codecConfigType = parseType(codecConfig.subarray(4, 8));
307-
308-
if (codecConfigType === 'avcC' && codecConfig.length > 11) {
309-
track.codec += '.';
310-
311-
// left padded with zeroes for single digit hex
312-
// profile idc
313-
track.codec += toHexString(codecConfig[9]);
314-
// the byte containing the constraint_set flags
315-
track.codec += toHexString(codecConfig[10]);
316-
// level idc
317-
track.codec += toHexString(codecConfig[11]);
318-
} else {
319-
// TODO: show a warning that we couldn't parse the codec
320-
// and are using the default
321-
track.codec = 'avc1.4d400d';
322-
}
323-
} else if ((/^mp4[a,v]$/i).test(track.codec)) {
324-
// we do not need anything but the streamDescriptor of the mp4a codecBox
325-
codecConfig = codecBox.subarray(28);
326-
codecConfigType = parseType(codecConfig.subarray(4, 8));
327-
328-
if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
329-
track.codec += '.' + toHexString(codecConfig[19]);
330-
// this value is only a single digit
331-
track.codec += '.' + toHexString((codecConfig[20] >>> 2) & 0x3f).replace(/^0/, '');
332-
} else {
333-
// TODO: show a warning that we couldn't parse the codec
334-
// and are using the default
335-
track.codec = 'mp4a.40.2';
336-
}
337-
} else {
338-
// flac, opus, etc
339-
track.codec = track.codec.toLowerCase();
340-
}
341-
}
342-
}
343-
344-
var mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
345-
346-
if (mdhd) {
347-
track.timescale = getTimescaleFromMediaHeader(mdhd);
348-
}
260+
var tracks = parseTracks(init, false);
349261

350-
tracks.push(track);
262+
tracks.forEach(function(track) {
263+
track.id = track.number;
351264
});
352265

353266
return tracks;

lib/partial/transmuxer.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ var AacStream = require('../aac/index');
1010
var clock = require('../utils/clock');
1111

1212
var createPipeline = function(object) {
13-
object.prototype = new Stream();
14-
object.prototype.init.call(object);
15-
16-
return object;
13+
return Object.assign(new Stream(), object);
1714
};
1815

1916
var tsPipeline = function(options) {

lib/tools/mp4-inspector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var
1616
return new Date(seconds * 1000 - 2082844800000);
1717
},
1818
parseType = require('../mp4/parse-type'),
19-
findBox = require('../mp4/find-box'),
19+
{findBox} = require('@videojs/vhs-utils/cjs/mp4-helpers.js'),
2020
nalParse = function(avcStream) {
2121
var
2222
avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),

lib/utils/stream.js

+12-78
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,12 @@
88
* Objects that inherit from streams should call init in their constructors.
99
*/
1010
'use strict';
11+
var Stream = require('@videojs/vhs-utils/cjs/stream');
1112

12-
var Stream = function() {
13-
this.init = function() {
14-
var listeners = {};
15-
/**
16-
* Add a listener for a specified event type.
17-
* @param type {string} the event name
18-
* @param listener {function} the callback to be invoked when an event of
19-
* the specified type occurs
20-
*/
21-
this.on = function(type, listener) {
22-
if (!listeners[type]) {
23-
listeners[type] = [];
24-
}
25-
listeners[type] = listeners[type].concat(listener);
26-
};
27-
/**
28-
* Remove a listener for a specified event type.
29-
* @param type {string} the event name
30-
* @param listener {function} a function previously registered for this
31-
* type of event through `on`
32-
*/
33-
this.off = function(type, listener) {
34-
var index;
35-
if (!listeners[type]) {
36-
return false;
37-
}
38-
index = listeners[type].indexOf(listener);
39-
listeners[type] = listeners[type].slice();
40-
listeners[type].splice(index, 1);
41-
return index > -1;
42-
};
43-
/**
44-
* Trigger an event of the specified type on this stream. Any additional
45-
* arguments to this function are passed as parameters to event listeners.
46-
* @param type {string} the event name
47-
*/
48-
this.trigger = function(type) {
49-
var callbacks, i, length, args;
50-
callbacks = listeners[type];
51-
if (!callbacks) {
52-
return;
53-
}
54-
// Slicing the arguments on every invocation of this method
55-
// can add a significant amount of overhead. Avoid the
56-
// intermediate object creation for the common case of a
57-
// single callback argument
58-
if (arguments.length === 2) {
59-
length = callbacks.length;
60-
for (i = 0; i < length; ++i) {
61-
callbacks[i].call(this, arguments[1]);
62-
}
63-
} else {
64-
args = [];
65-
i = arguments.length;
66-
for (i = 1; i < arguments.length; ++i) {
67-
args.push(arguments[i]);
68-
}
69-
length = callbacks.length;
70-
for (i = 0; i < length; ++i) {
71-
callbacks[i].apply(this, args);
72-
}
73-
}
74-
};
75-
/**
76-
* Destroys the stream and cleans up.
77-
*/
78-
this.dispose = function() {
79-
listeners = {};
80-
};
81-
};
82-
};
13+
var MuxStream = function() {};
14+
15+
MuxStream.prototype = new Stream();
16+
MuxStream.prototype.init = MuxStream.prototype.constructor;
8317

8418
/**
8519
* Forwards all `data` events on this stream to the destination stream. The
@@ -90,7 +24,7 @@ var Stream = function() {
9024
* when the current stream emits a 'done' event
9125
* @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
9226
*/
93-
Stream.prototype.pipe = function(destination) {
27+
MuxStream.prototype.pipe = function(destination) {
9428
this.on('data', function(data) {
9529
destination.push(data);
9630
});
@@ -118,24 +52,24 @@ Stream.prototype.pipe = function(destination) {
11852
// actual work. These are provided by the prototype as a sort of no-op
11953
// implementation so that we don't have to check for their existence in the
12054
// `pipe` function above.
121-
Stream.prototype.push = function(data) {
55+
MuxStream.prototype.push = function(data) {
12256
this.trigger('data', data);
12357
};
12458

125-
Stream.prototype.flush = function(flushSource) {
59+
MuxStream.prototype.flush = function(flushSource) {
12660
this.trigger('done', flushSource);
12761
};
12862

129-
Stream.prototype.partialFlush = function(flushSource) {
63+
MuxStream.prototype.partialFlush = function(flushSource) {
13064
this.trigger('partialdone', flushSource);
13165
};
13266

133-
Stream.prototype.endTimeline = function(flushSource) {
67+
MuxStream.prototype.endTimeline = function(flushSource) {
13468
this.trigger('endedtimeline', flushSource);
13569
};
13670

137-
Stream.prototype.reset = function(flushSource) {
71+
MuxStream.prototype.reset = function(flushSource) {
13872
this.trigger('reset', flushSource);
13973
};
14074

141-
module.exports = Stream;
75+
module.exports = MuxStream;

0 commit comments

Comments
 (0)