aeaa8493 by David LaPalomento

WIP: calculating data offsets in the trun

1 parent e6559cce
...@@ -422,6 +422,7 @@ tkhd = function(track) { ...@@ -422,6 +422,7 @@ tkhd = function(track) {
422 }; 422 };
423 423
424 traf = function(track) { 424 traf = function(track) {
425 var sampleDependencyTable = sdtp(track);
425 return box(types.traf, 426 return box(types.traf,
426 box(types.tfhd, new Uint8Array([ 427 box(types.tfhd, new Uint8Array([
427 0x00, // version 0 428 0x00, // version 0
...@@ -436,8 +437,15 @@ traf = function(track) { ...@@ -436,8 +437,15 @@ traf = function(track) {
436 0x00, 0x00, 0x00, // flags 437 0x00, 0x00, 0x00, // flags
437 0x00, 0x00, 0x00, 0x00 // baseMediaDecodeTime 438 0x00, 0x00, 0x00, 0x00 // baseMediaDecodeTime
438 ])), 439 ])),
439 trun(track), 440 trun(track,
440 sdtp(track)); 441 sdtp.length +
442 16 + // tfhd
443 16 + // tfdt
444 8 + // traf header
445 16 + // mfhd
446 8 + // moof header
447 8), // mdat header
448 sampleDependencyTable);
441 }; 449 };
442 450
443 /** 451 /**
...@@ -467,10 +475,11 @@ trex = function(track) { ...@@ -467,10 +475,11 @@ trex = function(track) {
467 ])); 475 ]));
468 }; 476 };
469 477
470 trun = function(track) { 478 trun = function(track, offset) {
471 var bytes, samples, sample, i; 479 var bytes, samples, sample, i;
472 480
473 samples = track.samples || []; 481 samples = track.samples || [];
482 offset += 8 + 12 + (16 * samples.length);
474 483
475 bytes = [ 484 bytes = [
476 0x00, // version 0 485 0x00, // version 0
...@@ -479,7 +488,10 @@ trun = function(track) { ...@@ -479,7 +488,10 @@ trun = function(track) {
479 (samples.length & 0xFF0000) >>> 16, 488 (samples.length & 0xFF0000) >>> 16,
480 (samples.length & 0xFF00) >>> 8, 489 (samples.length & 0xFF00) >>> 8,
481 samples.length & 0xFF, // sample_count 490 samples.length & 0xFF, // sample_count
482 0x00, 0x00, 0x00, 0x00 // data_offset 491 (offset & 0xFF000000) >>> 24,
492 (offset & 0xFF0000) >>> 16,
493 (offset & 0xFF00) >>> 8,
494 offset & 0xFF // data_offset
483 ]; 495 ];
484 496
485 for (i = 0; i < samples.length; i++) { 497 for (i = 0; i < samples.length; i++) {
...@@ -503,7 +515,7 @@ trun = function(track) { ...@@ -503,7 +515,7 @@ trun = function(track) {
503 (sample.compositionTimeOffset & 0xFF000000) >>> 24, 515 (sample.compositionTimeOffset & 0xFF000000) >>> 24,
504 (sample.compositionTimeOffset & 0xFF0000) >>> 16, 516 (sample.compositionTimeOffset & 0xFF0000) >>> 16,
505 (sample.compositionTimeOffset & 0xFF00) >>> 8, 517 (sample.compositionTimeOffset & 0xFF00) >>> 8,
506 sample.compositionTimeOffset & 0xFF, // sample_composition_time_offset 518 sample.compositionTimeOffset & 0xFF // sample_composition_time_offset
507 ]); 519 ]);
508 } 520 }
509 return box(types.trun, new Uint8Array(bytes)); 521 return box(types.trun, new Uint8Array(bytes));
......
...@@ -353,6 +353,7 @@ test('generates a minimal moof', function() { ...@@ -353,6 +353,7 @@ test('generates a minimal moof', function() {
353 equal(trun.type, 'trun', 'generated a trun box'); 353 equal(trun.type, 'trun', 'generated a trun box');
354 equal(typeof trun.dataOffset, 'number', 'has a data offset'); 354 equal(typeof trun.dataOffset, 'number', 'has a data offset');
355 ok(trun.dataOffset >= 0, 'has a non-negative data offset'); 355 ok(trun.dataOffset >= 0, 'has a non-negative data offset');
356 equal(trun.dataOffset, moof[0].size + 8, 'sets the data offset past the mdat header');
356 equal(trun.samples.length, 2, 'wrote two samples'); 357 equal(trun.samples.length, 2, 'wrote two samples');
357 358
358 equal(trun.samples[0].duration, 9000, 'wrote a sample duration'); 359 equal(trun.samples[0].duration, 9000, 'wrote a sample duration');
......
...@@ -888,6 +888,12 @@ validateTrackFragment = function(track, metadata) { ...@@ -888,6 +888,12 @@ validateTrackFragment = function(track, metadata) {
888 888
889 trun = track.boxes[2]; 889 trun = track.boxes[2];
890 ok(trun.dataOffset >= 0, 'set data offset'); 890 ok(trun.dataOffset >= 0, 'set data offset');
891 equal(trun.dataOffset,
892 trun.size +
893 16 + // mfhd size
894 8 + // moof header size
895 8, // mdat header size
896 'uses movie fragment relative addressing');
891 ok(trun.samples.length > 0, 'generated media samples'); 897 ok(trun.samples.length > 0, 'generated media samples');
892 for (i = 0; i < trun.samples.length; i++) { 898 for (i = 0; i < trun.samples.length; i++) {
893 sample = trun.samples[i]; 899 sample = trun.samples[i];
......