aboutsummaryrefslogtreecommitdiff
path: root/tuner/src/com/android/tv/tuner/exoplayer/FileSampleExtractor.java
blob: e7224422c66caae7d0f1dda3c1e4fdf5821d8f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.tuner.exoplayer;

import android.os.Handler;
import com.android.tv.tuner.exoplayer.buffer.BufferManager;
import com.android.tv.tuner.exoplayer.buffer.RecordingSampleBuffer;
import com.android.tv.tuner.tvinput.PlaybackBufferListener;
import com.google.android.exoplayer.MediaFormat;
import com.google.android.exoplayer.MediaFormatHolder;
import com.google.android.exoplayer.MediaFormatUtil;
import com.google.android.exoplayer.SampleHolder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A class that plays a recorded stream without using {@link android.media.MediaExtractor}, since
 * all samples are extracted and stored to the permanent storage already.
 */
public class FileSampleExtractor implements SampleExtractor {
    private static final String TAG = "FileSampleExtractor";
    private static final boolean DEBUG = false;

    private int mTrackCount;
    private boolean mReleased;

    private final List<MediaFormat> mTrackFormats = new ArrayList<>();
    private final BufferManager mBufferManager;
    private final PlaybackBufferListener mBufferListener;
    private BufferManager.SampleBuffer mSampleBuffer;

    public FileSampleExtractor(BufferManager bufferManager, PlaybackBufferListener bufferListener) {
        mBufferManager = bufferManager;
        mBufferListener = bufferListener;
        mTrackCount = -1;
    }

    @Override
    public void maybeThrowError() throws IOException {
        // Do nothing.
    }

    @Override
    public boolean prepare() throws IOException {
        List<BufferManager.TrackFormat> trackFormatList = mBufferManager.readTrackInfoFiles();
        if (trackFormatList == null || trackFormatList.isEmpty()) {
            throw new IOException("Cannot find meta files for the recording.");
        }
        mTrackCount = trackFormatList.size();
        List<String> ids = new ArrayList<>();
        mTrackFormats.clear();
        for (int i = 0; i < mTrackCount; ++i) {
            BufferManager.TrackFormat trackFormat = trackFormatList.get(i);
            ids.add(trackFormat.trackId);
            mTrackFormats.add(MediaFormatUtil.createMediaFormat(trackFormat.format));
        }
        mSampleBuffer =
                new RecordingSampleBuffer(
                        mBufferManager,
                        mBufferListener,
                        true,
                        RecordingSampleBuffer.BUFFER_REASON_RECORDED_PLAYBACK);
        mSampleBuffer.init(ids, mTrackFormats);
        return true;
    }

    @Override
    public List<MediaFormat> getTrackFormats() {
        return mTrackFormats;
    }

    @Override
    public void getTrackMediaFormat(int track, MediaFormatHolder outMediaFormatHolder) {
        outMediaFormatHolder.format = mTrackFormats.get(track);
        outMediaFormatHolder.drmInitData = null;
    }

    @Override
    public void release() {
        if (!mReleased) {
            if (mSampleBuffer != null) {
                try {
                    mSampleBuffer.release();
                } catch (IOException e) {
                    // Do nothing. Playback ends now.
                }
            }
        }
        mReleased = true;
    }

    @Override
    public void selectTrack(int index) {
        mSampleBuffer.selectTrack(index);
    }

    @Override
    public void deselectTrack(int index) {
        mSampleBuffer.deselectTrack(index);
    }

    @Override
    public long getBufferedPositionUs() {
        return mSampleBuffer.getBufferedPositionUs();
    }

    @Override
    public void seekTo(long positionUs) {
        mSampleBuffer.seekTo(positionUs);
    }

    @Override
    public int readSample(int track, SampleHolder sampleHolder) {
        return mSampleBuffer.readSample(track, sampleHolder);
    }

    @Override
    public boolean continueBuffering(long positionUs) {
        return mSampleBuffer.continueBuffering(positionUs);
    }

    @Override
    public void setOnCompletionListener(OnCompletionListener listener, Handler handler) {}
}