1010
1111var toUnsigned = require ( '../utils/bin' ) . toUnsigned ;
1212var toHexString = require ( '../utils/bin' ) . toHexString ;
13- var findBox = require ( '../mp4/find-box .js' ) ;
13+ var { findBox, parseTracks } = require ( '@videojs/vhs-utils/es/mp4-helpers .js' ) ;
1414var parseType = require ( '../mp4/parse-type.js' ) ;
1515var parseTfhd = require ( '../tools/parse-tfhd.js' ) ;
1616var 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
3940timescale = 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.
211213getVideoTrackIds = 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
239242getTimescaleFromMediaHeader = 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 */
256259getTracks = 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 ] v c [ 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 ( ( / ^ m p 4 [ 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 ;
0 commit comments