FileDocCategorySizeDatePackage
AbstractSampleEncryptionBox.javaAPI Docmp4parser 1.0-RC-1710292Wed Dec 19 20:10:37 GMT 2012com.googlecode.mp4parser.boxes

AbstractSampleEncryptionBox

public abstract class AbstractSampleEncryptionBox extends com.googlecode.mp4parser.AbstractFullBox

Fields Summary
int
algorithmId
int
ivSize
byte[]
kid
List
entries
Constructors Summary
protected AbstractSampleEncryptionBox(String type)


       
        super(type);
    
Methods Summary
public void_parseDetails(java.nio.ByteBuffer content)

        parseVersionAndFlags(content);
        int useThisIvSize = -1;
        if ((getFlags() & 0x1) > 0) {
            algorithmId = IsoTypeReader.readUInt24(content);
            ivSize = IsoTypeReader.readUInt8(content);
            useThisIvSize = ivSize;
            kid = new byte[16];
            content.get(kid);
        } else {
            List<Box> tkhds = Path.getPaths(this, "/moov[0]/trak/tkhd");
            for (Box tkhd : tkhds) {
                if (((TrackHeaderBox) tkhd).getTrackId() == this.getParent().getBoxes(TrackFragmentHeaderBox.class).get(0).getTrackId()) {
                    AbstractTrackEncryptionBox tenc = (AbstractTrackEncryptionBox) Path.getPath(tkhd, "../mdia[0]/minf[0]/stbl[0]/stsd[0]/enc.[0]/sinf[0]/schi[0]/tenc[0]");
                    if (tenc == null) {
                        tenc = (AbstractTrackEncryptionBox) Path.getPath(tkhd, "../mdia[0]/minf[0]/stbl[0]/stsd[0]/enc.[0]/sinf[0]/schi[0]/uuid[0]");
                    }
                    useThisIvSize = tenc.getDefaultIvSize();
                }
            }
        }
        long numOfEntries = IsoTypeReader.readUInt32(content);

        while (numOfEntries-- > 0) {
            Entry e = new Entry();
            e.iv = new byte[useThisIvSize < 0 ? 8 : useThisIvSize];  // default to 8
            content.get(e.iv);
            if ((getFlags() & 0x2) > 0) {
                int numOfPairs = IsoTypeReader.readUInt16(content);
                e.pairs = new LinkedList<Entry.Pair>();
                while (numOfPairs-- > 0) {
                    e.pairs.add(e.createPair(IsoTypeReader.readUInt16(content), IsoTypeReader.readUInt32(content)));
                }
            }
            entries.add(e);

        }
    
public com.googlecode.mp4parser.boxes.AbstractSampleEncryptionBox$EntrycreateEntry()

        return new Entry();
    
public booleanequals(java.lang.Object o)

        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        AbstractSampleEncryptionBox that = (AbstractSampleEncryptionBox) o;

        if (algorithmId != that.algorithmId) {
            return false;
        }
        if (ivSize != that.ivSize) {
            return false;
        }
        if (entries != null ? !entries.equals(that.entries) : that.entries != null) {
            return false;
        }
        if (!Arrays.equals(kid, that.kid)) {
            return false;
        }

        return true;
    
public intgetAlgorithmId()

        return algorithmId;
    
public voidgetBox(java.nio.channels.WritableByteChannel os)

        super.getBox(os);
    
protected voidgetContent(java.nio.ByteBuffer byteBuffer)

        writeVersionAndFlags(byteBuffer);
        if (isOverrideTrackEncryptionBoxParameters()) {
            IsoTypeWriter.writeUInt24(byteBuffer, algorithmId);
            IsoTypeWriter.writeUInt8(byteBuffer, ivSize);
            byteBuffer.put(kid);
        }
        IsoTypeWriter.writeUInt32(byteBuffer, entries.size());
        for (Entry entry : entries) {
            if (isOverrideTrackEncryptionBoxParameters()) {
                byte[] ivFull = new byte[ivSize];
                System.arraycopy(entry.iv, 0, ivFull, ivSize - entry.iv.length, entry.iv.length);
                byteBuffer.put(ivFull);
            } else {
                // just put the iv - i don't know any better
                byteBuffer.put(entry.iv);
            }
            if (isSubSampleEncryption()) {
                IsoTypeWriter.writeUInt16(byteBuffer, entry.pairs.size());
                for (Entry.Pair pair : entry.pairs) {
                    IsoTypeWriter.writeUInt16(byteBuffer, pair.clear);
                    IsoTypeWriter.writeUInt32(byteBuffer, pair.encrypted);
                }
            }
        }
    
protected longgetContentSize()

        long contentSize = 4;
        if (isOverrideTrackEncryptionBoxParameters()) {
            contentSize += 4;
            contentSize += kid.length;
        }
        contentSize += 4;
        for (Entry entry : entries) {
            contentSize += entry.getSize();
        }
        return contentSize;
    
public java.util.ListgetEntries()

        return entries;
    
public java.util.ListgetEntrySizes()

        List<Short> entrySizes = new ArrayList<Short>(entries.size());
        for (Entry entry : entries) {
            short size = (short) entry.iv.length;
            if (isSubSampleEncryption()) {
                size += 2; //numPairs
                size += entry.pairs.size() * 6;
            }
            entrySizes.add(size);
        }
        return entrySizes;
    
public intgetIvSize()

        return ivSize;
    
public byte[]getKid()

        return kid;
    
public intgetOffsetToFirstIV()

        int offset = (getSize() > (1l << 32) ? 16 : 8);
        offset += isOverrideTrackEncryptionBoxParameters() ? 20 : 0;
        offset += 4; //num entries
        return offset;
    
public intgetSampleCount()

        return entries.size();
    
public inthashCode()

        int result = algorithmId;
        result = 31 * result + ivSize;
        result = 31 * result + (kid != null ? Arrays.hashCode(kid) : 0);
        result = 31 * result + (entries != null ? entries.hashCode() : 0);
        return result;
    
public booleanisOverrideTrackEncryptionBoxParameters()

        return (getFlags() & 0x1) > 0;
    
public booleanisSubSampleEncryption()

        return (getFlags() & 0x2) > 0;
    
public voidsetAlgorithmId(int algorithmId)

        this.algorithmId = algorithmId;
    
public voidsetEntries(java.util.List entries)

        this.entries = entries;
    
public voidsetIvSize(int ivSize)

        this.ivSize = ivSize;
    
public voidsetKid(byte[] kid)

        this.kid = kid;
    
public voidsetOverrideTrackEncryptionBoxParameters(boolean b)

        if (b) {
            setFlags(getFlags() | 0x1);
        } else {
            setFlags(getFlags() & (0xffffff ^ 0x1));
        }
    
public voidsetSubSampleEncryption(boolean b)

        if (b) {
            setFlags(getFlags() | 0x2);
        } else {
            setFlags(getFlags() & (0xffffff ^ 0x2));
        }