Movie.java revision dd9eb897ee7c7b507cbdcf80263bb4b5de6966bf
1/*
2 * Copyright 2012 Sebastian Annies, 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 */
16package com.googlecode.mp4parser.authoring;
17
18import java.util.LinkedList;
19import java.util.List;
20
21/**
22 *
23 */
24public class Movie {
25    List<Track> tracks = new LinkedList<Track>();
26
27    public List<Track> getTracks() {
28        return tracks;
29    }
30
31    public void setTracks(List<Track> tracks) {
32        this.tracks = tracks;
33    }
34
35    public void addTrack(Track nuTrack) {
36        // do some checking
37        // perhaps the movie needs to get longer!
38        if (getTrackByTrackId(nuTrack.getTrackMetaData().getTrackId()) != null) {
39            // We already have a track with that trackId. Create a new one
40            nuTrack.getTrackMetaData().setTrackId(getNextTrackId());
41        }
42        tracks.add(nuTrack);
43    }
44
45
46    @Override
47    public String toString() {
48        String s = "Movie{ ";
49        for (Track track : tracks) {
50            s += "track_" + track.getTrackMetaData().getTrackId() + " (" + track.getHandler() + ") ";
51        }
52
53        s += '}';
54        return s;
55    }
56
57    public long getNextTrackId() {
58        long nextTrackId = 0;
59        for (Track track : tracks) {
60            nextTrackId = nextTrackId < track.getTrackMetaData().getTrackId() ? track.getTrackMetaData().getTrackId() : nextTrackId;
61        }
62        return ++nextTrackId;
63    }
64
65
66    public Track getTrackByTrackId(long trackId) {
67        for (Track track : tracks) {
68            if (track.getTrackMetaData().getTrackId() == trackId) {
69                return track;
70            }
71        }
72        return null;
73    }
74
75
76    public long getTimescale() {
77        long timescale = this.getTracks().iterator().next().getTrackMetaData().getTimescale();
78        for (Track track : this.getTracks()) {
79            timescale = gcd(track.getTrackMetaData().getTimescale(), timescale);
80        }
81        return timescale;
82    }
83
84    public static long gcd(long a, long b) {
85        if (b == 0) {
86            return a;
87        }
88        return gcd(b, a % b);
89    }
90
91}
92