From 711933f337b31d110ed0319f96802efe0bd00dd6 Mon Sep 17 00:00:00 2001 From: Tim Branyen Date: Fri, 5 Apr 2019 12:41:26 -0700 Subject: [PATCH] Support senc, encryption sampling --- src/isofile-write.js | 24 +++++++++++++++++------- src/parsing/senc.js | 43 ++++++++++++++++++++++--------------------- src/writing/senc.js | 16 ++++++++++++++++ 3 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 src/writing/senc.js diff --git a/src/isofile-write.js b/src/isofile-write.js index dc3e3862..5a3a3a17 100644 --- a/src/isofile-write.js +++ b/src/isofile-write.js @@ -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); @@ -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) { @@ -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() { @@ -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; diff --git a/src/parsing/senc.js b/src/parsing/senc.js index 2dc9e372..2ea7abad 100644 --- a/src/parsing/senc.js +++ b/src/parsing/senc.js @@ -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); + //} +}); diff --git a/src/writing/senc.js b/src/writing/senc.js new file mode 100644 index 00000000..1f5d05a1 --- /dev/null +++ b/src/writing/senc.js @@ -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 pssh data. + stream.writeUint32(this.data.byteLength); + stream.writeUint8Array(this.data); +}