Generate a minimal moof box
moof boxes are one of the primary components of segmented mp4 files. Expose a function to create a very simple one and add a test for it.
Showing
2 changed files
with
46 additions
and
1 deletions
1 | (function(window, videojs, undefined) { | 1 | (function(window, videojs, undefined) { |
2 | 'use strict'; | 2 | 'use strict'; |
3 | 3 | ||
4 | var box, dinf, ftyp, minf, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, stbl, | 4 | var box, dinf, ftyp, minf, moof, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, stbl, |
5 | stsd, types, MAJOR_BRAND, MINOR_VERSION, VIDEO_HDLR, VMHD, DREF, STCO, STSC, STSZ, STTS, TREX, | 5 | stsd, types, MAJOR_BRAND, MINOR_VERSION, VIDEO_HDLR, VMHD, DREF, STCO, STSC, STSZ, STTS, TREX, |
6 | Uint8Array, DataView; | 6 | Uint8Array, DataView; |
7 | 7 | ||
... | @@ -21,7 +21,9 @@ DataView = window.DataView; | ... | @@ -21,7 +21,9 @@ DataView = window.DataView; |
21 | hdlr: [], | 21 | hdlr: [], |
22 | mdhd: [], | 22 | mdhd: [], |
23 | mdia: [], | 23 | mdia: [], |
24 | mfhd: [], | ||
24 | minf: [], | 25 | minf: [], |
26 | moof: [], | ||
25 | moov: [], | 27 | moov: [], |
26 | mvex: [], | 28 | mvex: [], |
27 | mvhd: [], | 29 | mvhd: [], |
... | @@ -31,6 +33,8 @@ DataView = window.DataView; | ... | @@ -31,6 +33,8 @@ DataView = window.DataView; |
31 | stsd: [], | 33 | stsd: [], |
32 | stsz: [], | 34 | stsz: [], |
33 | stts: [], | 35 | stts: [], |
36 | tfhd: [], | ||
37 | traf: [], | ||
34 | trak: [], | 38 | trak: [], |
35 | trex: [], | 39 | trex: [], |
36 | tkhd: [], | 40 | tkhd: [], |
... | @@ -166,6 +170,26 @@ mdia = function(duration, width, height) { | ... | @@ -166,6 +170,26 @@ mdia = function(duration, width, height) { |
166 | minf = function(width, height) { | 170 | minf = function(width, height) { |
167 | return box(types.minf, box(types.vmhd, VMHD), dinf(), stbl(width, height)); | 171 | return box(types.minf, box(types.vmhd, VMHD), dinf(), stbl(width, height)); |
168 | }; | 172 | }; |
173 | moof = function(tracks) { | ||
174 | var | ||
175 | trafCall = [], | ||
176 | i = tracks.length; | ||
177 | // build tfhd boxes for each track fragment | ||
178 | while (i--) { | ||
179 | trafCall[i] = box(types.tfhd, new Uint8Array([ | ||
180 | 0x00, // version 0 | ||
181 | 0x00, 0x00, 0x00, // flags | ||
182 | (tracks[i].trackId & 0xFF000000) >> 24, | ||
183 | (tracks[i].trackId & 0xFF0000) >> 16, | ||
184 | (tracks[i].trackId & 0xFF00) >> 8, | ||
185 | (tracks[i].trackId & 0xFF), | ||
186 | ])); | ||
187 | } | ||
188 | trafCall.unshift(types.traf); | ||
189 | return box(types.moof, | ||
190 | box(types.mfhd), | ||
191 | box.apply(null, trafCall)); | ||
192 | }; | ||
169 | moov = function(duration, width, height) { | 193 | moov = function(duration, width, height) { |
170 | return box(types.moov, mvhd(duration), trak(duration, width, height), mvex()); | 194 | return box(types.moov, mvhd(duration), trak(duration, width, height), mvex()); |
171 | }; | 195 | }; |
... | @@ -314,6 +338,7 @@ trak = function(duration, width, height) { | ... | @@ -314,6 +338,7 @@ trak = function(duration, width, height) { |
314 | 338 | ||
315 | window.videojs.mp4 = { | 339 | window.videojs.mp4 = { |
316 | ftyp: ftyp, | 340 | ftyp: ftyp, |
341 | moof: moof, | ||
317 | moov: moov, | 342 | moov: moov, |
318 | initSegment: function() { | 343 | initSegment: function() { |
319 | var | 344 | var | ... | ... |
... | @@ -216,5 +216,25 @@ test('generates an initialization segment', function() { | ... | @@ -216,5 +216,25 @@ test('generates an initialization segment', function() { |
216 | equal(init[1].boxes[0].duration, 0xffffffff, 'wrote a maximum duration'); | 216 | equal(init[1].boxes[0].duration, 0xffffffff, 'wrote a maximum duration'); |
217 | }); | 217 | }); |
218 | 218 | ||
219 | test('generates a minimal moof', function() { | ||
220 | var | ||
221 | data = mp4.moof([{ | ||
222 | trackId: 1 | ||
223 | }, { | ||
224 | trackId: 2 | ||
225 | }]), | ||
226 | moof = videojs.inspectMp4(data); | ||
227 | |||
228 | equal(moof.length, 1, 'generated one box'); | ||
229 | equal(moof[0].type, 'moof', 'generated a moof box'); | ||
230 | equal(moof[0].boxes.length, 2, 'generated two child boxes'); | ||
231 | equal(moof[0].boxes[0].type, 'mfhd', 'generated an mfhd box'); | ||
232 | equal(moof[0].boxes[1].type, 'traf', 'generated a traf box'); | ||
233 | equal(moof[0].boxes[1].boxes.length, 2, 'generated two fragment headers'); | ||
234 | equal(moof[0].boxes[1].boxes[0].type, 'tfhd', 'generated a tfhd box'); | ||
235 | equal(moof[0].boxes[1].boxes[0].trackId, 1, 'wrote the first track id'); | ||
236 | equal(moof[0].boxes[1].boxes[1].trackId, 2, 'wrote the second track id'); | ||
237 | }); | ||
238 | |||
219 | 239 | ||
220 | })(window, window.videojs); | 240 | })(window, window.videojs); | ... | ... |
-
Please register or sign in to post a comment