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 */
16
17package com.googlecode.mp4parser.authoring.tracks;
18
19import com.coremedia.iso.boxes.*;
20import com.googlecode.mp4parser.authoring.AbstractTrack;
21import com.googlecode.mp4parser.authoring.TrackMetaData;
22import com.googlecode.mp4parser.boxes.adobe.ActionMessageFormat0SampleEntryBox;
23
24import java.nio.ByteBuffer;
25import java.util.Collections;
26import java.util.Date;
27import java.util.LinkedList;
28import java.util.List;
29import java.util.Map;
30import java.util.SortedMap;
31import java.util.TreeMap;
32
33public class Amf0Track extends AbstractTrack {
34    SortedMap<Long, byte[]> rawSamples = new TreeMap<Long, byte[]>() {
35    };
36    private TrackMetaData trackMetaData = new TrackMetaData();
37
38
39    /**
40     * Creates a new AMF0 track from
41     *
42     * @param rawSamples
43     */
44    public Amf0Track(Map<Long, byte[]> rawSamples) {
45        this.rawSamples = new TreeMap<Long, byte[]>(rawSamples);
46        trackMetaData.setCreationTime(new Date());
47        trackMetaData.setModificationTime(new Date());
48        trackMetaData.setTimescale(1000); // Text tracks use millieseconds
49        trackMetaData.setLanguage("eng");
50    }
51
52    public List<ByteBuffer> getSamples() {
53        LinkedList<ByteBuffer> samples = new LinkedList<ByteBuffer>();
54        for (byte[] bytes : rawSamples.values()) {
55            samples.add(ByteBuffer.wrap(bytes));
56        }
57        return samples;
58    }
59
60    public SampleDescriptionBox getSampleDescriptionBox() {
61        SampleDescriptionBox stsd = new SampleDescriptionBox();
62        ActionMessageFormat0SampleEntryBox amf0 = new ActionMessageFormat0SampleEntryBox();
63        amf0.setDataReferenceIndex(1);
64        stsd.addBox(amf0);
65        return stsd;
66    }
67
68    public List<TimeToSampleBox.Entry> getDecodingTimeEntries() {
69        LinkedList<TimeToSampleBox.Entry> timesToSample = new LinkedList<TimeToSampleBox.Entry>();
70        LinkedList<Long> keys = new LinkedList<Long>(rawSamples.keySet());
71        Collections.sort(keys);
72        long lastTimeStamp = 0;
73        for (Long key : keys) {
74            long delta = key - lastTimeStamp;
75            if (timesToSample.size() > 0 && timesToSample.peek().getDelta() == delta) {
76                timesToSample.peek().setCount(timesToSample.peek().getCount() + 1);
77            } else {
78                timesToSample.add(new TimeToSampleBox.Entry(1, delta));
79            }
80            lastTimeStamp = key;
81        }
82        return timesToSample;
83    }
84
85    public List<CompositionTimeToSample.Entry> getCompositionTimeEntries() {
86        // AMF0 tracks do not have Composition Time
87        return null;
88    }
89
90    public long[] getSyncSamples() {
91        // AMF0 tracks do not have Sync Samples
92        return null;
93    }
94
95    public List<SampleDependencyTypeBox.Entry> getSampleDependencies() {
96        // AMF0 tracks do not have Sample Dependencies
97        return null;
98    }
99
100    public TrackMetaData getTrackMetaData() {
101        return trackMetaData;  //To change body of implemented methods use File | Settings | File Templates.
102    }
103
104    public String getHandler() {
105        return "data";
106    }
107
108    public Box getMediaHeaderBox() {
109        return new NullMediaHeaderBox();
110    }
111
112    public SubSampleInformationBox getSubsampleInformationBox() {
113        return null;
114    }
115
116}
117