aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/tuner/exoplayer/MpegTsSampleExtractor.java
blob: 7bf116c838f4b23de46aad7a02f731c5f0e3d44d (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * 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.net.Uri;
import android.os.Handler;

import com.google.android.exoplayer.MediaFormat;
import com.google.android.exoplayer.MediaFormatHolder;
import com.google.android.exoplayer.SampleHolder;
import com.google.android.exoplayer.SampleSource;
import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.util.MimeTypes;
import com.android.tv.tuner.exoplayer.buffer.BufferManager;
import com.android.tv.tuner.exoplayer.buffer.SamplePool;
import com.android.tv.tuner.tvinput.PlaybackBufferListener;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Extracts samples from {@link DataSource} for MPEG-TS streams.
 */
public final class MpegTsSampleExtractor implements SampleExtractor {
    public static final String MIMETYPE_TEXT_CEA_708 = "text/cea-708";

    private static final int CC_BUFFER_SIZE_IN_BYTES = 9600 / 8;

    private final SampleExtractor mSampleExtractor;
    private final List<MediaFormat> mTrackFormats = new ArrayList<>();
    private final List<Boolean> mReachedEos = new ArrayList<>();
    private int mVideoTrackIndex;
    private final SamplePool mCcSamplePool = new SamplePool();
    private final List<SampleHolder> mPendingCcSamples = new LinkedList<>();

    private int mCea708TextTrackIndex;
    private boolean mCea708TextTrackSelected;

    private CcParser mCcParser;

    private void init() {
        mVideoTrackIndex = -1;
        mCea708TextTrackIndex = -1;
        mCea708TextTrackSelected = false;
    }

    /**
     * Creates MpegTsSampleExtractor for {@link DataSource}.
     *
     * @param source the {@link DataSource} to extract from
     * @param bufferManager the manager for reading & writing samples backed by physical storage
     * @param bufferListener the {@link PlaybackBufferListener}
     *                      to notify buffer storage status change
     */
    public MpegTsSampleExtractor(DataSource source, BufferManager bufferManager,
            PlaybackBufferListener bufferListener) {
        mSampleExtractor = new ExoPlayerSampleExtractor(Uri.EMPTY, source, bufferManager,
                bufferListener, false);
        init();
    }

    /**
     * Creates MpegTsSampleExtractor for a recorded program.
     *
     * @param bufferManager the samples provider which is stored in physical storage
     * @param bufferListener the {@link PlaybackBufferListener}
     *                      to notify buffer storage status change
     */
    public MpegTsSampleExtractor(BufferManager bufferManager,
            PlaybackBufferListener bufferListener) {
        mSampleExtractor = new FileSampleExtractor(bufferManager, bufferListener);
        init();
    }

    @Override
    public void maybeThrowError() throws IOException {
        if (mSampleExtractor != null) {
            mSampleExtractor.maybeThrowError();
        }
    }

    @Override
    public boolean prepare() throws IOException {
        if(!mSampleExtractor.prepare()) {
            return false;
        }
        List<MediaFormat> formats = mSampleExtractor.getTrackFormats();
        int trackCount = formats.size();
        mTrackFormats.clear();
        mReachedEos.clear();

        for (int i = 0; i < trackCount; ++i) {
            mTrackFormats.add(formats.get(i));
            mReachedEos.add(false);
            String mime = formats.get(i).mimeType;
            if (MimeTypes.isVideo(mime) && mVideoTrackIndex == -1) {
                mVideoTrackIndex = i;
                if (android.media.MediaFormat.MIMETYPE_VIDEO_MPEG2.equals(mime)) {
                    mCcParser = new Mpeg2CcParser();
                } else if (android.media.MediaFormat.MIMETYPE_VIDEO_AVC.equals(mime)) {
                    mCcParser = new H264CcParser();
                }
            }
        }

        if (mVideoTrackIndex != -1) {
            mCea708TextTrackIndex = trackCount;
        }
        if (mCea708TextTrackIndex >= 0) {
            mTrackFormats.add(MediaFormat.createTextFormat(null, MIMETYPE_TEXT_CEA_708, 0,
                    mTrackFormats.get(0).durationUs, ""));
        }
        return true;
    }

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

    @Override
    public void selectTrack(int index) {
        if (index == mCea708TextTrackIndex) {
            mCea708TextTrackSelected = true;
            return;
        }
        mSampleExtractor.selectTrack(index);
    }

    @Override
    public void deselectTrack(int index) {
        if (index == mCea708TextTrackIndex) {
            mCea708TextTrackSelected = false;
            return;
        }
        mSampleExtractor.deselectTrack(index);
    }

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

    @Override
    public void seekTo(long positionUs) {
        mSampleExtractor.seekTo(positionUs);
        for (SampleHolder holder : mPendingCcSamples) {
            mCcSamplePool.releaseSample(holder);
        }
        mPendingCcSamples.clear();
    }

    @Override
    public void getTrackMediaFormat(int track, MediaFormatHolder outMediaFormatHolder) {
        if (track != mCea708TextTrackIndex) {
            mSampleExtractor.getTrackMediaFormat(track, outMediaFormatHolder);
        }
    }

    @Override
    public int readSample(int track, SampleHolder sampleHolder) {
        if (track == mCea708TextTrackIndex) {
            if (mCea708TextTrackSelected && !mPendingCcSamples.isEmpty()) {
                SampleHolder holder = mPendingCcSamples.remove(0);
                holder.data.flip();
                sampleHolder.timeUs = holder.timeUs;
                sampleHolder.data.put(holder.data);
                mCcSamplePool.releaseSample(holder);
                return SampleSource.SAMPLE_READ;
            } else {
                return mVideoTrackIndex < 0 || mReachedEos.get(mVideoTrackIndex)
                        ? SampleSource.END_OF_STREAM : SampleSource.NOTHING_READ;
            }
        }

        int result = mSampleExtractor.readSample(track, sampleHolder);
        switch (result) {
            case SampleSource.END_OF_STREAM: {
                mReachedEos.set(track, true);
                break;
            }
            case SampleSource.SAMPLE_READ: {
                if (mCea708TextTrackSelected && track == mVideoTrackIndex
                        && sampleHolder.data != null) {
                    mCcParser.mayParseClosedCaption(sampleHolder.data, sampleHolder.timeUs);
                }
                break;
            }
        }
        return result;
    }

    @Override
    public void release() {
        mSampleExtractor.release();
        mVideoTrackIndex = -1;
        mCea708TextTrackIndex = -1;
        mCea708TextTrackSelected = false;
    }

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

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

    private abstract class CcParser {
        // Interim buffer for reduce direct access to ByteBuffer which is expensive. Using
        // relatively small buffer size in order to minimize memory footprint increase.
        protected final byte[] mBuffer = new byte[1024];

        abstract void mayParseClosedCaption(ByteBuffer buffer, long presentationTimeUs);

        protected int parseClosedCaption(ByteBuffer buffer, int offset, long presentationTimeUs) {
            // For the details of user_data_type_structure, see ATSC A/53 Part 4 - Table 6.9.
            int pos = offset;
            if (pos + 2 >= buffer.position()) {
                return offset;
            }
            boolean processCcDataFlag = (buffer.get(pos) & 64) != 0;
            int ccCount = buffer.get(pos) & 0x1f;
            pos += 2;
            if (!processCcDataFlag || pos + 3 * ccCount >= buffer.position() || ccCount == 0) {
                return offset;
            }
            SampleHolder holder = mCcSamplePool.acquireSample(CC_BUFFER_SIZE_IN_BYTES);
            for (int i = 0; i < 3 * ccCount; i++) {
                holder.data.put(buffer.get(pos++));
            }
            holder.timeUs = presentationTimeUs;
            mPendingCcSamples.add(holder);
            return pos;
        }
    }

    private class Mpeg2CcParser extends CcParser {
        private static final int PATTERN_LENGTH = 9;

        @Override
        public void mayParseClosedCaption(ByteBuffer buffer, long presentationTimeUs) {
            int totalSize = buffer.position();
            // Reading the frame in bulk to reduce the overhead from ByteBuffer.get() with
            // overlapping to handle the case that the pattern exists in the boundary.
            for (int i = 0; i < totalSize; i += mBuffer.length - PATTERN_LENGTH) {
                buffer.position(i);
                int size = Math.min(totalSize - i, mBuffer.length);
                buffer.get(mBuffer, 0, size);
                int j = 0;
                while (j < size - PATTERN_LENGTH) {
                    // Find the start prefix code of private user data.
                    if (mBuffer[j] == 0
                            && mBuffer[j + 1] == 0
                            && mBuffer[j + 2] == 1
                            && (mBuffer[j + 3] & 0xff) == 0xb2) {
                        // ATSC closed caption data embedded in MPEG2VIDEO stream has 'GA94' user
                        // identifier and user data type code 3.
                        if (mBuffer[j + 4] == 'G'
                                && mBuffer[j + 5] == 'A'
                                && mBuffer[j + 6] == '9'
                                && mBuffer[j + 7] == '4'
                                && mBuffer[j + 8] == 3) {
                            j = parseClosedCaption(buffer, i + j + PATTERN_LENGTH,
                                    presentationTimeUs) - i;
                        } else {
                            j += PATTERN_LENGTH;
                        }
                    } else {
                        ++j;
                    }
                }
            }
            buffer.position(totalSize);
        }
    }

    private class H264CcParser extends CcParser {
        private static final int PATTERN_LENGTH = 14;

        @Override
        public void mayParseClosedCaption(ByteBuffer buffer, long presentationTimeUs) {
            int totalSize = buffer.position();
            // Reading the frame in bulk to reduce the overhead from ByteBuffer.get() with
            // overlapping to handle the case that the pattern exists in the boundary.
            for (int i = 0; i < totalSize; i += mBuffer.length - PATTERN_LENGTH) {
                buffer.position(i);
                int size = Math.min(totalSize - i, mBuffer.length);
                buffer.get(mBuffer, 0, size);
                int j = 0;
                while (j < size - PATTERN_LENGTH) {
                    // Find the start prefix code of a NAL Unit.
                    if (mBuffer[j] == 0
                            && mBuffer[j + 1] == 0
                            && mBuffer[j + 2] == 1) {
                        int nalType = mBuffer[j + 3] & 0x1f;
                        int payloadType = mBuffer[j + 4] & 0xff;

                        // ATSC closed caption data embedded in H264 private user data has NAL type
                        // 6, payload type 4, and 'GA94' user identifier for ATSC.
                        if (nalType == 6 && payloadType == 4 && mBuffer[j + 9] == 'G'
                                && mBuffer[j + 10] == 'A'
                                && mBuffer[j + 11] == '9'
                                && mBuffer[j + 12] == '4') {
                            j = parseClosedCaption(buffer, i + j + PATTERN_LENGTH,
                                    presentationTimeUs) - i;
                        } else {
                            j += 7;
                        }
                    } else {
                        ++j;
                    }
                }
            }
            buffer.position(totalSize);
        }
    }
}