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
25import static com.googlecode.mp4parser.util.CastUtils.l2i;
26
27/**
28 * This box provides a compact marking of the random access points withinthe stream. The table is arranged in
29 * strictly decreasinf order of sample number. Defined in ISO/IEC 14496-12.
30 */
31public class SyncSampleBox extends AbstractFullBox {
32    public static final String TYPE = "stss";
33
34    private long[] sampleNumber;
35
36    public SyncSampleBox() {
37        super(TYPE);
38    }
39
40    /**
41     * Gives the numbers of the samples that are random access points in the stream.
42     *
43     * @return random access sample numbers.
44     */
45    public long[] getSampleNumber() {
46        return sampleNumber;
47    }
48
49    protected long getContentSize() {
50        return sampleNumber.length * 4 + 8;
51    }
52
53    @Override
54    public void _parseDetails(ByteBuffer content) {
55        parseVersionAndFlags(content);
56        int entryCount = l2i(IsoTypeReader.readUInt32(content));
57
58        sampleNumber = new long[entryCount];
59        for (int i = 0; i < entryCount; i++) {
60            sampleNumber[i] = IsoTypeReader.readUInt32(content);
61        }
62    }
63
64    @Override
65    protected void getContent(ByteBuffer byteBuffer) {
66        writeVersionAndFlags(byteBuffer);
67
68        IsoTypeWriter.writeUInt32(byteBuffer, sampleNumber.length);
69
70        for (long aSampleNumber : sampleNumber) {
71            IsoTypeWriter.writeUInt32(byteBuffer, aSampleNumber);
72        }
73
74    }
75
76    public String toString() {
77        return "SyncSampleBox[entryCount=" + sampleNumber.length + "]";
78    }
79
80    public void setSampleNumber(long[] sampleNumber) {
81        this.sampleNumber = sampleNumber;
82    }
83}
84