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.googlecode.mp4parser.AbstractBox;
23import com.googlecode.mp4parser.annotations.DoNotParseDetail;
24
25import java.nio.ByteBuffer;
26import java.util.Collection;
27import java.util.Collections;
28import java.util.LinkedList;
29import java.util.List;
30
31/**
32 * This box identifies the specifications to which this file complies. <br>
33 * Each brand is a printable four-character code, registered with ISO, that
34 * identifies a precise specification.
35 */
36public class FileTypeBox extends AbstractBox {
37    public static final String TYPE = "ftyp";
38
39    private String majorBrand;
40    private long minorVersion;
41    private List<String> compatibleBrands = Collections.emptyList();
42
43    public FileTypeBox() {
44        super(TYPE);
45    }
46
47    public FileTypeBox(String majorBrand, long minorVersion, List<String> compatibleBrands) {
48        super(TYPE);
49        this.majorBrand = majorBrand;
50        this.minorVersion = minorVersion;
51        this.compatibleBrands = compatibleBrands;
52    }
53
54    protected long getContentSize() {
55        return 8 + compatibleBrands.size() * 4;
56
57    }
58
59    @Override
60    public void _parseDetails(ByteBuffer content) {
61        majorBrand = IsoTypeReader.read4cc(content);
62        minorVersion = IsoTypeReader.readUInt32(content);
63        int compatibleBrandsCount = content.remaining() / 4;
64        compatibleBrands = new LinkedList<String>();
65        for (int i = 0; i < compatibleBrandsCount; i++) {
66            compatibleBrands.add(IsoTypeReader.read4cc(content));
67        }
68    }
69
70    @Override
71    protected void getContent(ByteBuffer byteBuffer) {
72        byteBuffer.put(IsoFile.fourCCtoBytes(majorBrand));
73        IsoTypeWriter.writeUInt32(byteBuffer, minorVersion);
74        for (String compatibleBrand : compatibleBrands) {
75            byteBuffer.put(IsoFile.fourCCtoBytes(compatibleBrand));
76        }
77
78    }
79
80    /**
81     * Gets the brand identifier.
82     *
83     * @return the brand identifier
84     */
85    public String getMajorBrand() {
86        return majorBrand;
87    }
88
89    /**
90     * Sets the major brand of the file used to determine an appropriate reader.
91     *
92     * @param majorBrand the new major brand
93     */
94    public void setMajorBrand(String majorBrand) {
95        this.majorBrand = majorBrand;
96    }
97
98    /**
99     * Sets the "informative integer for the minor version of the major brand".
100     *
101     * @param minorVersion the version number of the major brand
102     */
103    public void setMinorVersion(int minorVersion) {
104        this.minorVersion = minorVersion;
105    }
106
107    /**
108     * Gets an informative integer for the minor version of the major brand.
109     *
110     * @return an informative integer
111     * @see FileTypeBox#getMajorBrand()
112     */
113    public long getMinorVersion() {
114        return minorVersion;
115    }
116
117    /**
118     * Gets an array of 4-cc brands.
119     *
120     * @return the compatible brands
121     */
122    public List<String> getCompatibleBrands() {
123        return compatibleBrands;
124    }
125
126    public void setCompatibleBrands(List<String> compatibleBrands) {
127        this.compatibleBrands = compatibleBrands;
128    }
129
130    @DoNotParseDetail
131    public String toString() {
132        StringBuilder result = new StringBuilder();
133        result.append("FileTypeBox[");
134        result.append("majorBrand=").append(getMajorBrand());
135        result.append(";");
136        result.append("minorVersion=").append(getMinorVersion());
137        for (String compatibleBrand : compatibleBrands) {
138            result.append(";");
139            result.append("compatibleBrand=").append(compatibleBrand);
140        }
141        result.append("]");
142        return result.toString();
143    }
144}
145