1 /*
2  * Copyright 2009 castLabs GmbH, Berlin
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.googlecode.mp4parser.AbstractContainerBox;
20 import com.coremedia.iso.boxes.Box;
21 import com.coremedia.iso.boxes.SampleDependencyTypeBox;
22 import com.googlecode.mp4parser.annotations.DoNotParseDetail;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 /**
28  * aligned(8) class MovieFragmentBox extends Box(moof){
29  * }
30  */
31 
32 public class MovieFragmentBox extends AbstractContainerBox {
33     public static final String TYPE = "moof";
34 
MovieFragmentBox()35     public MovieFragmentBox() {
36         super(TYPE);
37     }
38 
39 
getSyncSamples(SampleDependencyTypeBox sdtp)40     public List<Long> getSyncSamples(SampleDependencyTypeBox sdtp) {
41         List<Long> result = new ArrayList<Long>();
42 
43         final List<SampleDependencyTypeBox.Entry> sampleEntries = sdtp.getEntries();
44         long i = 1;
45         for (SampleDependencyTypeBox.Entry sampleEntry : sampleEntries) {
46             if (sampleEntry.getSampleDependsOn() == 2) {
47                 result.add(i);
48             }
49             i++;
50         }
51 
52         return result;
53     }
54 
55     @DoNotParseDetail
getOffset()56     public long getOffset() {
57         Box b = this;
58         long offset = 0;
59         while (b.getParent() != null) {
60             for (Box box : b.getParent().getBoxes()) {
61                 if (b == box) {
62                     break;
63                 }
64                 offset += box.getSize();
65             }
66             b = b.getParent();
67         }
68         return offset;
69     }
70 
71 
getTrackCount()72     public int getTrackCount() {
73         return getBoxes(TrackFragmentBox.class, false).size();
74     }
75 
76     /**
77      * Returns the track numbers associated with this <code>MovieBox</code>.
78      *
79      * @return the tracknumbers (IDs) of the tracks in their order of appearance in the file
80      */
81 
getTrackNumbers()82     public long[] getTrackNumbers() {
83 
84         List<TrackFragmentBox> trackBoxes = this.getBoxes(TrackFragmentBox.class, false);
85         long[] trackNumbers = new long[trackBoxes.size()];
86         for (int trackCounter = 0; trackCounter < trackBoxes.size(); trackCounter++) {
87             TrackFragmentBox trackBoxe = trackBoxes.get(trackCounter);
88             trackNumbers[trackCounter] = trackBoxe.getTrackFragmentHeaderBox().getTrackId();
89         }
90         return trackNumbers;
91     }
92 
getTrackFragmentHeaderBoxes()93     public List<TrackFragmentHeaderBox> getTrackFragmentHeaderBoxes() {
94         return getBoxes(TrackFragmentHeaderBox.class, true);
95     }
96 
getTrackRunBoxes()97     public List<TrackRunBox> getTrackRunBoxes() {
98         return getBoxes(TrackRunBox.class, true);
99     }
100 }
101