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.IsoTypeReader;
20import com.coremedia.iso.IsoTypeWriter;
21import com.googlecode.mp4parser.AbstractFullBox;
22
23import java.nio.ByteBuffer;
24
25/**
26 * This box defines overall information which is media-independent, and relevant to the entire presentation
27 * considered as a whole.
28 */
29public class MediaHeaderBox extends AbstractFullBox {
30    public static final String TYPE = "mdhd";
31
32
33    private long creationTime;
34    private long modificationTime;
35    private long timescale;
36    private long duration;
37    private String language;
38
39    public MediaHeaderBox() {
40        super(TYPE);
41    }
42
43    public long getCreationTime() {
44        return creationTime;
45    }
46
47    public long getModificationTime() {
48        return modificationTime;
49    }
50
51    public long getTimescale() {
52        return timescale;
53    }
54
55    public long getDuration() {
56        return duration;
57    }
58
59    public String getLanguage() {
60        return language;
61    }
62
63    protected long getContentSize() {
64        long contentSize = 4;
65        if (getVersion() == 1) {
66            contentSize += 8 + 8 + 4 + 8;
67        } else {
68            contentSize += 4 + 4 + 4 + 4;
69        }
70        contentSize += 2;
71        contentSize += 2;
72        return contentSize;
73
74    }
75
76    public void setCreationTime(long creationTime) {
77        this.creationTime = creationTime;
78    }
79
80    public void setModificationTime(long modificationTime) {
81        this.modificationTime = modificationTime;
82    }
83
84    public void setTimescale(long timescale) {
85        this.timescale = timescale;
86    }
87
88    public void setDuration(long duration) {
89        this.duration = duration;
90    }
91
92    public void setLanguage(String language) {
93        this.language = language;
94    }
95
96    @Override
97    public void _parseDetails(ByteBuffer content) {
98        parseVersionAndFlags(content);
99        if (getVersion() == 1) {
100            creationTime = IsoTypeReader.readUInt64(content);
101            modificationTime = IsoTypeReader.readUInt64(content);
102            timescale = IsoTypeReader.readUInt32(content);
103            duration = IsoTypeReader.readUInt64(content);
104        } else {
105            creationTime = IsoTypeReader.readUInt32(content);
106            modificationTime = IsoTypeReader.readUInt32(content);
107            timescale = IsoTypeReader.readUInt32(content);
108            duration = IsoTypeReader.readUInt32(content);
109        }
110        language = IsoTypeReader.readIso639(content);
111        IsoTypeReader.readUInt16(content);
112    }
113
114
115    public String toString() {
116        StringBuilder result = new StringBuilder();
117        result.append("MediaHeaderBox[");
118        result.append("creationTime=").append(getCreationTime());
119        result.append(";");
120        result.append("modificationTime=").append(getModificationTime());
121        result.append(";");
122        result.append("timescale=").append(getTimescale());
123        result.append(";");
124        result.append("duration=").append(getDuration());
125        result.append(";");
126        result.append("language=").append(getLanguage());
127        result.append("]");
128        return result.toString();
129    }
130
131    protected void getContent(ByteBuffer byteBuffer) {
132        writeVersionAndFlags(byteBuffer);
133        if (getVersion() == 1) {
134            IsoTypeWriter.writeUInt64(byteBuffer, creationTime);
135            IsoTypeWriter.writeUInt64(byteBuffer, modificationTime);
136            IsoTypeWriter.writeUInt32(byteBuffer, timescale);
137            IsoTypeWriter.writeUInt64(byteBuffer, duration);
138        } else {
139            IsoTypeWriter.writeUInt32(byteBuffer, creationTime);
140            IsoTypeWriter.writeUInt32(byteBuffer, modificationTime);
141            IsoTypeWriter.writeUInt32(byteBuffer, timescale);
142            IsoTypeWriter.writeUInt32(byteBuffer, duration);
143        }
144        IsoTypeWriter.writeIso639(byteBuffer, language);
145        IsoTypeWriter.writeUInt16(byteBuffer, 0);
146    }
147}
148