• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 castLabs, Berlin
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.googlecode.mp4parser.boxes.mp4.samplegrouping;
18 
19 import com.coremedia.iso.Hex;
20 import com.coremedia.iso.IsoTypeReader;
21 import com.coremedia.iso.IsoTypeWriter;
22 
23 import java.nio.ByteBuffer;
24 import java.util.Arrays;
25 
26 /**
27  * Each sample in a protected track shall be associated with an IsEncrypted flag, IV_Size, and KID.
28  * This can be accomplished by (a) relying on the default values in the TrackEncryptionBox
29  * (see 8.2), or (b) specifying the parameters by sample group, or (c) using a combination of these two techniques.
30  * <p/>
31  * When specifying the parameters by sample group, the SampleToGroupBox in the sample table or track
32  * fragment specifies which samples use which sample group description from the SampleGroupDescriptionBox.
33  */
34 public class CencSampleEncryptionInformationGroupEntry extends GroupEntry {
35     public static final String TYPE = "seig";
36 
37     private int isEncrypted;
38     private byte ivSize;
39     private byte[] kid = new byte[16];
40 
41     @Override
parse(ByteBuffer byteBuffer)42     public void parse(ByteBuffer byteBuffer) {
43         isEncrypted = IsoTypeReader.readUInt24(byteBuffer);
44         ivSize = (byte) IsoTypeReader.readUInt8(byteBuffer);
45         kid = new byte[16];
46         byteBuffer.get(kid);
47 
48     }
49 
50     @Override
get()51     public ByteBuffer get() {
52         ByteBuffer byteBuffer = ByteBuffer.allocate(20);
53         IsoTypeWriter.writeUInt24(byteBuffer, isEncrypted);
54         IsoTypeWriter.writeUInt8(byteBuffer, ivSize);
55         byteBuffer.put(kid);
56         byteBuffer.rewind();
57         return byteBuffer;
58     }
59 
getEncrypted()60     public int getEncrypted() {
61         return isEncrypted;
62     }
63 
setEncrypted(int encrypted)64     public void setEncrypted(int encrypted) {
65         isEncrypted = encrypted;
66     }
67 
getIvSize()68     public byte getIvSize() {
69         return ivSize;
70     }
71 
setIvSize(byte ivSize)72     public void setIvSize(byte ivSize) {
73         this.ivSize = ivSize;
74     }
75 
getKid()76     public byte[] getKid() {
77         return kid;
78     }
79 
setKid(byte[] kid)80     public void setKid(byte[] kid) {
81         assert kid.length == 16;
82         this.kid = kid;
83     }
84 
85     @Override
toString()86     public String toString() {
87         return "CencSampleEncryptionInformationGroupEntry{" +
88                 "isEncrypted=" + isEncrypted +
89                 ", ivSize=" + ivSize +
90                 ", kid=" + Hex.encodeHex(kid) +
91                 '}';
92     }
93 
94     @Override
equals(Object o)95     public boolean equals(Object o) {
96         if (this == o) {
97             return true;
98         }
99         if (o == null || getClass() != o.getClass()) {
100             return false;
101         }
102 
103         CencSampleEncryptionInformationGroupEntry that = (CencSampleEncryptionInformationGroupEntry) o;
104 
105         if (isEncrypted != that.isEncrypted) {
106             return false;
107         }
108         if (ivSize != that.ivSize) {
109             return false;
110         }
111         if (!Arrays.equals(kid, that.kid)) {
112             return false;
113         }
114 
115         return true;
116     }
117 
118     @Override
hashCode()119     public int hashCode() {
120         int result = isEncrypted;
121         result = 31 * result + (int) ivSize;
122         result = 31 * result + (kid != null ? Arrays.hashCode(kid) : 0);
123         return result;
124     }
125 }
126