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.tracks;
17
18import com.coremedia.iso.boxes.*;
19import com.googlecode.mp4parser.authoring.AbstractTrack;
20import com.googlecode.mp4parser.authoring.Track;
21import com.googlecode.mp4parser.authoring.TrackMetaData;
22
23import java.nio.ByteBuffer;
24import java.util.AbstractList;
25import java.util.LinkedList;
26import java.util.List;
27
28/**
29 * Generates a Track where a single sample has been replaced by a given <code>ByteBuffer</code>.
30 */
31
32public class ReplaceSampleTrack extends AbstractTrack {
33    Track origTrack;
34    private long sampleNumber;
35    private ByteBuffer sampleContent;
36    private List<ByteBuffer>  samples;
37
38    public ReplaceSampleTrack(Track origTrack, long sampleNumber, ByteBuffer content) {
39        this.origTrack = origTrack;
40        this.sampleNumber = sampleNumber;
41        this.sampleContent = content;
42        this.samples = new ReplaceASingleEntryList();
43
44    }
45
46    public List<ByteBuffer> getSamples() {
47        return samples;
48    }
49
50    public SampleDescriptionBox getSampleDescriptionBox() {
51        return origTrack.getSampleDescriptionBox();
52    }
53
54    public List<TimeToSampleBox.Entry> getDecodingTimeEntries() {
55        return origTrack.getDecodingTimeEntries();
56
57    }
58
59    public List<CompositionTimeToSample.Entry> getCompositionTimeEntries() {
60        return origTrack.getCompositionTimeEntries();
61
62    }
63
64    synchronized public long[] getSyncSamples() {
65        return origTrack.getSyncSamples();
66    }
67
68    public List<SampleDependencyTypeBox.Entry> getSampleDependencies() {
69        return origTrack.getSampleDependencies();
70    }
71
72    public TrackMetaData getTrackMetaData() {
73        return origTrack.getTrackMetaData();
74    }
75
76    public String getHandler() {
77        return origTrack.getHandler();
78    }
79
80    public Box getMediaHeaderBox() {
81        return origTrack.getMediaHeaderBox();
82    }
83
84    public SubSampleInformationBox getSubsampleInformationBox() {
85        return origTrack.getSubsampleInformationBox();
86    }
87
88    private class ReplaceASingleEntryList extends AbstractList<ByteBuffer> {
89        @Override
90        public ByteBuffer get(int index) {
91            if (ReplaceSampleTrack.this.sampleNumber == index) {
92                return ReplaceSampleTrack.this.sampleContent;
93            } else {
94                return ReplaceSampleTrack.this.origTrack.getSamples().get(index);
95            }
96        }
97
98        @Override
99        public int size() {
100            return ReplaceSampleTrack.this.origTrack.getSamples().size();
101        }
102    }
103
104}
105