Skip to content

Commit

Permalink
read/write esds much better
Browse files Browse the repository at this point in the history
  • Loading branch information
sannies committed May 20, 2015
1 parent 69bf74b commit 9cf7f91
Show file tree
Hide file tree
Showing 9 changed files with 376 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ public static void main(String[] args) throws IOException {

Movie mOrig = MovieCreator.build(CencEncryptDecrypt.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/1365070268951.mp4");


Movie mEncryptOut = new Movie();
SecretKey sk = new SecretKeySpec(new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, "AES");
for (Track track : mOrig.getTracks()) {
mEncryptOut.addTrack(new CencEncryptingTrackImpl(track, UUID.randomUUID(), sk, false));
mEncryptOut.addTrack(new CencEncryptingTrackImpl(track, UUID.randomUUID(), sk, true));
}

Container cEncrypted = mp4Builder.build(mEncryptOut);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
cEncrypted.writeContainer(Channels.newChannel(baos));

FileOutputStream fos = new FileOutputStream("output-enc.mp4");
fos.write(baos.toByteArray());

Movie mEncryptIn = MovieCreator.build (new MemoryDataSourceImpl(baos.toByteArray()));
Movie mDecrypt = new Movie();

Expand All @@ -48,8 +52,8 @@ public static void main(String[] args) throws IOException {
}

Container cDecrypted = mp4Builder.build(mDecrypt);
FileOutputStream fos = new FileOutputStream("output.mp4");
cDecrypted.writeContainer(fos.getChannel());
FileOutputStream fos2 = new FileOutputStream("output.mp4");
cDecrypted.writeContainer(fos2.getChannel());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public AACTrackImpl(DataSource dataSource, String lang) throws IOException {
decoderConfigDescriptor.setAvgBitRate(avgBitRate);

AudioSpecificConfig audioSpecificConfig = new AudioSpecificConfig();
audioSpecificConfig.setAudioObjectType(2); // AAC LC
audioSpecificConfig.setOriginalAudioObjectType(2); // AAC LC
audioSpecificConfig.setSamplingFrequencyIndex(firstHeader.sampleFrequencyIndex);
audioSpecificConfig.setChannelConfiguration(firstHeader.channelconfig);
decoderConfigDescriptor.setAudioSpecificInfo(audioSpecificConfig);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract aligned(8) expandable(228-1) class BaseDescriptor : bit(8) tag=0 {
public abstract class BaseDescriptor {
int tag;
int sizeOfInstance;
private int sizeBytes;
int sizeBytes;

public BaseDescriptor() {
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public final void parse(int tag, ByteBuffer bb) throws IOException {

public abstract void parseDetail(ByteBuffer bb) throws IOException;

public abstract Buffer serialize();
public abstract ByteBuffer serialize();

/**
* without tag and size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public BitWriterBuffer(ByteBuffer buffer) {
this.initialPos = buffer.position();
}

public void writeBool(boolean b) {
writeBits(b ? 1 : 0, 1);
}

public void writeBits(int i, int numBits) {
assert i <= ((1 << numBits) - 1) : String.format("Trying to write a value bigger (%s) than the number bits (%s) allows. " +
"Please mask the value before writing it and make your code is really working as intended.", i, (1 << numBits) - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ByteBuffer serialize() {
for (ProfileLevelIndicationDescriptor profileLevelIndicationDescriptor : profileLevelIndicationDescriptors) {
out.put(profileLevelIndicationDescriptor.serialize());
}
return out;
return (ByteBuffer) out.rewind();
}

public DecoderSpecificInfo getDecoderSpecificInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ int getContentSize() {
}

public ByteBuffer serialize() {
ByteBuffer out = ByteBuffer.allocate(getSize()); // Usually is around 30 bytes, so 200 should be enough...
byte[] aaa = new byte[getSize()];
ByteBuffer out = ByteBuffer.wrap(aaa); // Usually is around 30 bytes, so 200 should be enough...
IsoTypeWriter.writeUInt8(out, 3);
writeSize(out, getContentSize());
IsoTypeWriter.writeUInt16(out, esId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ public static BaseDescriptor createFrom(int objectTypeIndication, ByteBuffer bb)
throw new RuntimeException(e);
}
}

//ByteBuffer orig = bb.slice();
baseDescriptor.parse(tag, bb);
//byte[] b1 = new byte[baseDescriptor.sizeOfInstance + baseDescriptor.sizeBytes];
//orig.get(b1);
//byte[] b2 = baseDescriptor.serialize().array();
//System.err.println(baseDescriptor.getClass().getName() + " orig: " + Hex.encodeHex(b1) + " serialized: " + Hex.encodeHex(b2));

return baseDescriptor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.googlecode.mp4parser.boxes.mp4;

import com.coremedia.iso.Hex;
import com.coremedia.iso.IsoFile;
import com.googlecode.mp4parser.MemoryDataSourceImpl;
import com.googlecode.mp4parser.util.Path;
import org.junit.Test;

/**
* Created by sannies on 20.05.2015.
*/
public class ESDescriptorBoxTest {
@Test
public void testEsDescriptor() throws Exception {
String esdsBytes = "0000002A6573647300000000031C000000041440150018000001F4000001F4000505131056E598060102";
//String esdsBytes = "0000003365736473000000000380808022000200048080801440150000000006AD650006AD65058080800211B0068080800102";
IsoFile isoFile = new IsoFile(new MemoryDataSourceImpl(Hex.decodeHex(esdsBytes)));
ESDescriptorBox esds = Path.getPath(isoFile, "esds");
System.err.println(esds.getEsDescriptor());

}
}

0 comments on commit 9cf7f91

Please sign in to comment.