49e93b2e by David LaPalomento

Add mdat support to the generator. Stub out media segment creation

Create a helper method to build mdat boxes. Generate an empty moof and a real mdat when a NAL unit is parsed.
1 parent 85815fdb
(function(window, videojs, undefined) {
'use strict';
var box, dinf, ftyp, minf, moof, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, stbl,
var box, dinf, ftyp, mdat, minf, moof, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, stbl,
stsd, styp, types, MAJOR_BRAND, MINOR_VERSION, VIDEO_HDLR, AUDIO_HDLR, HDLR_TYPES, VMHD, DREF, STCO, STSC, STSZ, STTS, TREX,
Uint8Array, DataView;
......@@ -19,6 +19,7 @@ DataView = window.DataView;
dref: [],
ftyp: [],
hdlr: [],
mdat: [],
mdhd: [],
mdia: [],
mfhd: [],
......@@ -165,6 +166,9 @@ ftyp = function() {
hdlr = function(type) {
return box(types.hdlr, HDLR_TYPES[type]);
};
mdat = function(data) {
return box(types.mdat, data);
};
mdhd = function(duration) {
return box(types.mdhd, new Uint8Array([
0x00, // version 0
......@@ -359,6 +363,7 @@ trak = function(duration, width, height, type) {
window.videojs.mp4 = {
ftyp: ftyp,
mdat: mdat,
moof: moof,
moov: moov,
initSegment: function() {
......
......@@ -440,10 +440,19 @@ Transmuxer = function() {
this.initSegment = mp4.initSegment();
aacStream.on('data', function(data) {
self.trigger('data', data);
});
h264Stream.on('data', function(data) {
self.trigger('data', data);
var
moof = mp4.moof([]),
mdat = mp4.mdat(data.data),
boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
boxes.set(moof);
boxes.set(mdat, moof.byteLength);
self.trigger('data', {
data: boxes
});
});
// feed incoming data to the front of the parsing pipeline
this.push = function(data) {
......
......@@ -264,5 +264,15 @@ test('generates a minimal moof', function() {
equal(moof[0].boxes[1].boxes[1].trackId, 2, 'wrote the second track id');
});
test('generates an mdat', function() {
var
data = mp4.mdat(new Uint8Array([1, 2, 3, 4])),
mdat = videojs.inspectMp4(data);
equal(mdat.length, 1, 'generated one box');
equal(mdat[0].type, 'mdat', 'generated an mdat box');
deepEqual(mdat[0].byteLength, 4, 'encapsulated the data');
});
})(window, window.videojs);
......
......@@ -643,7 +643,7 @@ test('generates an init segment', function() {
});
test('parses an example mp2t file and generates media segments', function() {
var segments = [], i, segment;
var segments = [], i, boxes;
transmuxer.on('data', function(segment) {
segments.push(segment);
});
......@@ -653,9 +653,14 @@ test('parses an example mp2t file and generates media segments', function() {
ok(segments.length, 'generated media segments');
i = segments.length;
while (i--) {
segment = videojs.inspectMp4(segments[i].data);
boxes = videojs.inspectMp4(segments[i].data);
equal(boxes.length, 2, 'segments are composed of two boxes');
equal(boxes[0].type, 'moof', 'first box is a moof');
equal(boxes[0].boxes.length, 2, 'moof has three children');
equal(boxes[0].boxes[0].type, 'mfhd', 'mfhd is a child of the moof');
equal(boxes[0].boxes[1].type, 'traf', 'traf is a child of the moof');
equal(boxes[1].type, 'mdat', 'second box is an mdat');
}
console.log(segments);
});
})(window, window.videojs);
......