1/*
2 * Copyright 2008 CoreMedia AG, Hamburg
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
17package com.coremedia.iso.boxes;
18
19import com.coremedia.iso.IsoFile;
20import com.coremedia.iso.IsoTypeReader;
21import com.coremedia.iso.IsoTypeWriter;
22import com.coremedia.iso.Utf8;
23import com.googlecode.mp4parser.AbstractFullBox;
24
25import java.nio.ByteBuffer;
26
27/**
28 * The Scheme Type Box identifies the protection scheme. Resides in  a Protection Scheme Information Box or
29 * an SRTP Process Box.
30 *
31 * @see com.coremedia.iso.boxes.SchemeInformationBox
32 */
33public class SchemeTypeBox extends AbstractFullBox {
34    public static final String TYPE = "schm";
35    String schemeType = "    ";
36    long schemeVersion;
37    String schemeUri = null;
38
39    public SchemeTypeBox() {
40        super(TYPE);
41    }
42
43    public String getSchemeType() {
44        return schemeType;
45    }
46
47    public long getSchemeVersion() {
48        return schemeVersion;
49    }
50
51    public String getSchemeUri() {
52        return schemeUri;
53    }
54
55    public void setSchemeType(String schemeType) {
56        assert schemeType != null && schemeType.length() == 4 : "SchemeType may not be null or not 4 bytes long";
57        this.schemeType = schemeType;
58    }
59
60    public void setSchemeVersion(int schemeVersion) {
61        this.schemeVersion = schemeVersion;
62    }
63
64    public void setSchemeUri(String schemeUri) {
65        this.schemeUri = schemeUri;
66    }
67
68    protected long getContentSize() {
69        return 12 + (((getFlags() & 1) == 1) ? Utf8.utf8StringLengthInBytes(schemeUri) + 1 : 0);
70    }
71
72    @Override
73    public void _parseDetails(ByteBuffer content) {
74        parseVersionAndFlags(content);
75        schemeType = IsoTypeReader.read4cc(content);
76        schemeVersion = IsoTypeReader.readUInt32(content);
77        if ((getFlags() & 1) == 1) {
78            schemeUri = IsoTypeReader.readString(content);
79        }
80    }
81
82    @Override
83    protected void getContent(ByteBuffer byteBuffer) {
84        writeVersionAndFlags(byteBuffer);
85        byteBuffer.put(IsoFile.fourCCtoBytes(schemeType));
86        IsoTypeWriter.writeUInt32(byteBuffer, schemeVersion);
87        if ((getFlags() & 1) == 1) {
88            byteBuffer.put(Utf8.convert(schemeUri));
89        }
90    }
91
92    public String toString() {
93        StringBuilder buffer = new StringBuilder();
94        buffer.append("Schema Type Box[");
95        buffer.append("schemeUri=").append(schemeUri).append("; ");
96        buffer.append("schemeType=").append(schemeType).append("; ");
97        buffer.append("schemeVersion=").append(schemeUri).append("; ");
98        buffer.append("]");
99        return buffer.toString();
100    }
101}
102