Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support senc, encryption sampling #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/isofile-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@ ISOFile.prototype.createFragment = function(track_id, sampleNumber, stream_) {
}
return null;
}

var stream = stream_ || new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;

var moof = ISOFile.createSingleSampleMoof(sample);

// Extract sample encryption.
var senc = this.getBox('senc');

// Add sample encryption if it exists.
if (senc) {
moof.trafs[0].senc = senc;
moof.trafs[0].boxes.push(senc);
}

moof.write(stream);

/* adjusting the data_offset now that the moof size is known*/
moof.trafs[0].truns[0].data_offset = moof.size+8; //8 is mdat header
Log.debug("MP4Box", "Adjusting data_offset with new value "+moof.trafs[0].truns[0].data_offset);
stream.adjustUint32(moof.trafs[0].truns[0].data_offset_position, moof.trafs[0].truns[0].data_offset);

var mdat = new BoxParser.mdatBox();
mdat.data = sample.data;
mdat.write(stream);
Expand All @@ -47,7 +57,7 @@ ISOFile.writeInitializationSegment = function(ftyp, moov, total_duration, sample
var stream = new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
ftyp.write(stream);

/* we can now create the new mvex box */
var mvex = moov.add("mvex");
if (total_duration) {
Expand All @@ -70,7 +80,7 @@ ISOFile.prototype.save = function(name) {
var stream = new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
this.write(stream);
stream.save(name);
stream.save(name);
}

ISOFile.prototype.getBuffer = function() {
Expand All @@ -91,11 +101,11 @@ ISOFile.prototype.initializeSegmentation = function() {
Log.warn("MP4Box", "No segmentation callback set!");
}
if (!this.isFragmentationInitialized) {
this.isFragmentationInitialized = true;
this.isFragmentationInitialized = true;
this.nextMoofNumber = 0;
this.resetTables();
}
initSegs = [];
}
initSegs = [];
for (i = 0; i < this.fragmentedTracks.length; i++) {
var moov = new BoxParser.moovBox();
moov.mvhd = this.moov.mvhd;
Expand Down
43 changes: 22 additions & 21 deletions src/parsing/senc.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// Cannot be fully parsed because Per_Sample_IV_Size needs to be known
BoxParser.createFullBoxCtor("senc" /*, function(stream) {
BoxParser.createFullBoxCtor("senc", function(stream) {
this.parseFullHeader(stream);
var sample_count = stream.readUint32();
this.samples = [];
for (var i = 0; i < sample_count; i++) {
var sample = {};
// tenc.default_Per_Sample_IV_Size or seig.Per_Sample_IV_Size
sample.InitializationVector = this.readUint8Array(Per_Sample_IV_Size*8);
if (this.flags & 0x2) {
sample.subsamples = [];
subsample_count = stream.readUint16();
for (var j = 0; j < subsample_count; j++) {
var subsample = {};
subsample.BytesOfClearData = stream.readUint16();
subsample.BytesOfProtectedData = stream.readUint32();
sample.subsamples.push(subsample);
}
}
// TODO
this.samples.push(sample);
}
}*/);
this.data = stream.readUint8Array(this.size - this.hdr_size - 4);
// Cannot be fully parsed because Per_Sample_IV_Size needs to be known
//this.samples = [];
//for (var i = 0; i < sample_count; i++) {
// var sample = {};
// // tenc.default_Per_Sample_IV_Size or seig.Per_Sample_IV_Size
// sample.InitializationVector = this.readUint8Array(Per_Sample_IV_Size*8);
// if (this.flags & 0x2) {
// sample.subsamples = [];
// subsample_count = stream.readUint16();
// for (var j = 0; j < subsample_count; j++) {
// var subsample = {};
// subsample.BytesOfClearData = stream.readUint16();
// subsample.BytesOfProtectedData = stream.readUint32();
// sample.subsamples.push(subsample);
// }
// }
// // TODO
// this.samples.push(sample);
//}
});
16 changes: 16 additions & 0 deletions src/writing/senc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
BoxParser.sencBox.prototype.write = function(stream) {
// Calculate the full box size.
this.size = this.hdr_size + this.data.byteLength;

// Write out the BMFF box header.
stream.writeInt32(this.size);
stream.writeString(this.type, null, 4);

// Write out the full box header.
stream.writeUint8(this.version);
stream.writeUint24(this.flags);

// Write out the senc data.
stream.writeUint32(this.data.byteLength);
stream.writeUint8Array(this.data);
}