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