aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/tuner/source/FileTsStreamer.java
blob: 14997ee4d8e0c1c4445dd486a8bd76af03dba3ed (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * 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.source;

import android.os.Environment;
import android.util.Log;
import android.util.SparseBooleanArray;

import com.google.android.exoplayer.C;
import com.google.android.exoplayer.upstream.DataSpec;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.tuner.ChannelScanFileParser.ScanChannel;
import com.android.tv.tuner.data.TunerChannel;
import com.android.tv.tuner.ts.TsParser;
import com.android.tv.tuner.tvinput.EventDetector;
import com.android.tv.tuner.tvinput.FileSourceEventDetector;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Provides MPEG-2 TS stream sources for both channel scanning and channel playing from a local file
 * generated by capturing TV signal.
 */
public class FileTsStreamer implements TsStreamer {
    private static final String TAG = "FileTsStreamer";

    private static final int TS_PACKET_SIZE = 188;
    private static final int TS_SYNC_BYTE = 0x47;
    private static final int MIN_READ_UNIT = TS_PACKET_SIZE * 10;
    private static final int READ_BUFFER_SIZE = MIN_READ_UNIT * 10; // ~20KB
    private static final int CIRCULAR_BUFFER_SIZE = MIN_READ_UNIT * 4000; // ~ 8MB
    private static final int PADDING_SIZE = MIN_READ_UNIT * 1000; // ~2MB
    private static final int READ_TIMEOUT_MS = 10000; // 10 secs.
    private static final int BUFFER_UNDERRUN_SLEEP_MS = 10;
    private static final String FILE_DIR =
            new File(Environment.getExternalStorageDirectory(), "Streams").getAbsolutePath();

    // Virtual frequency base used for file-based source
    public static final int FREQ_BASE = 100;

    private final Object mCircularBufferMonitor = new Object();
    private final byte[] mCircularBuffer = new byte[CIRCULAR_BUFFER_SIZE];
    private final FileSourceEventDetector mEventDetector;

    private long mBytesFetched;
    private long mLastReadPosition;
    private boolean mStreaming;

    private Thread mStreamingThread;
    private StreamProvider mSource;

    public static class FileDataSource extends TsDataSource {
        private final FileTsStreamer mTsStreamer;
        private final AtomicLong mLastReadPosition = new AtomicLong(0);
        private long mStartBufferedPosition;

        private FileDataSource(FileTsStreamer tsStreamer) {
            mTsStreamer = tsStreamer;
            mStartBufferedPosition = tsStreamer.getBufferedPosition();
        }

        @Override
        public long getBufferedPosition() {
            return mTsStreamer.getBufferedPosition() - mStartBufferedPosition;
        }

        @Override
        public long getLastReadPosition() {
            return mLastReadPosition.get();
        }

        @Override
        public void shiftStartPosition(long offset) {
            SoftPreconditions.checkState(mLastReadPosition.get() == 0);
            SoftPreconditions.checkArgument(0 <= offset && offset <= getBufferedPosition());
            mStartBufferedPosition += offset;
        }

        @Override
        public long open(DataSpec dataSpec) throws IOException {
            mLastReadPosition.set(0);
            return C.LENGTH_UNBOUNDED;
        }

        @Override
        public void close() {
        }

        @Override
        public int read(byte[] buffer, int offset, int readLength) throws IOException {
            int ret = mTsStreamer.readAt(mStartBufferedPosition + mLastReadPosition.get(), buffer,
                    offset, readLength);
            if (ret > 0) {
                mLastReadPosition.addAndGet(ret);
            }
            return ret;
        }
    }

    /**
     * Creates {@link TsStreamer} for scanning & playing MPEG-2 TS file.
     * @param eventListener the listener for channel & program information
     */
    public FileTsStreamer(EventDetector.EventListener eventListener) {
        mEventDetector = new FileSourceEventDetector(eventListener);
    }

    @Override
    public boolean startStream(ScanChannel channel) {
        String filepath = new File(FILE_DIR, channel.filename).getAbsolutePath();
        mSource = new StreamProvider(filepath);
        if (!mSource.isReady()) {
            return false;
        }
        mEventDetector.start(mSource, FileSourceEventDetector.ALL_PROGRAM_NUMBERS);
        mSource.addPidFilter(TsParser.ATSC_SI_BASE_PID);
        mSource.addPidFilter(TsParser.PAT_PID);
        synchronized (mCircularBufferMonitor) {
            if (mStreaming) {
                return true;
            }
            mStreaming = true;
        }

        mStreamingThread = new StreamingThread();
        mStreamingThread.start();
        Log.i(TAG, "Streaming started");
        return true;
    }

    @Override
    public boolean startStream(TunerChannel channel) {
        Log.i(TAG, "tuneToChannel with: " + channel.getFilepath());
        mSource = new StreamProvider(channel.getFilepath());
        if (!mSource.isReady()) {
            return false;
        }
        mEventDetector.start(mSource, channel.getProgramNumber());
        mSource.addPidFilter(channel.getVideoPid());
        for (Integer i : channel.getAudioPids()) {
            mSource.addPidFilter(i);
        }
        mSource.addPidFilter(channel.getPcrPid());
        mSource.addPidFilter(TsParser.ATSC_SI_BASE_PID);
        mSource.addPidFilter(TsParser.PAT_PID);
        synchronized (mCircularBufferMonitor) {
            if (mStreaming) {
                return true;
            }
            mStreaming = true;
        }

        mStreamingThread = new StreamingThread();
        mStreamingThread.start();
        Log.i(TAG, "Streaming started");
        return true;
    }

    /**
     * Blocks the current thread until the streaming thread stops. In rare cases when the tuner
     * device is overloaded this can take a while, but usually it returns pretty quickly.
     */
    @Override
    public void stopStream() {
        synchronized (mCircularBufferMonitor) {
            mStreaming = false;
            mCircularBufferMonitor.notify();
        }

        try {
            if (mStreamingThread != null) {
                mStreamingThread.join();
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    @Override
    public TsDataSource createDataSource() {
        return new FileDataSource(this);
    }

    /**
     * Returns the current buffered position from the file.
     * @return the current buffered position
     */
    public long getBufferedPosition() {
        synchronized (mCircularBufferMonitor) {
            return mBytesFetched;
        }
    }

    /**
     * Provides MPEG-2 transport stream from a local file. Stream can be filtered by PID.
     */
    public static class StreamProvider {
        private final String mFilepath;
        private final SparseBooleanArray mPids = new SparseBooleanArray();
        private final byte[] mPreBuffer = new byte[READ_BUFFER_SIZE];

        private BufferedInputStream mInputStream;

        private StreamProvider(String filepath) {
            mFilepath = filepath;
            open(filepath);
        }

        private void open(String filepath) {
            try {
                mInputStream = new BufferedInputStream(new FileInputStream(filepath));
            } catch (IOException e) {
                Log.e(TAG, "Error opening input stream", e);
                mInputStream = null;
            }
        }

        private boolean isReady() {
            return mInputStream != null;
        }

        /**
         * Returns the file path of the MPEG-2 TS file.
         */
        public String getFilepath() {
            return mFilepath;
        }

        /**
         * Adds a pid for filtering from the MPEG-2 TS file.
         */
        public void addPidFilter(int pid) {
            mPids.put(pid, true);
        }

        /**
         * Returns whether the current pid filter is empty or not.
         */
        public boolean isFilterEmpty() {
            return mPids.size() > 0;
        }

        /**
         * Clears the current pid filter.
         */
        public void clearPidFilter() {
            mPids.clear();
        }

        /**
         * Returns whether a pid is in the pid filter or not.
         * @param pid the pid to check
         */
        public boolean isInFilter(int pid) {
            return mPids.get(pid);
        }

        /**
         * Reads from the MPEG-2 TS file to buffer.
         *
         * @param inputBuffer to read
         * @return the number of read bytes
         */
        private int read(byte[] inputBuffer) {
            int readSize = readInternal();
            if (readSize <= 0) {
                // Reached the end of stream. Restart from the beginning.
                close();
                open(mFilepath);
                if (mInputStream == null) {
                    return -1;
                }
                readSize = readInternal();
            }

            if (mPreBuffer[0] != TS_SYNC_BYTE) {
                Log.e(TAG, "Error reading input stream - no TS sync found");
                return -1;
            }
            int filteredSize = 0;
            for (int i = 0, destPos = 0; i < readSize; i += TS_PACKET_SIZE) {
                if (mPreBuffer[i] == TS_SYNC_BYTE) {
                    int pid = ((mPreBuffer[i + 1] & 0x1f) << 8) + (mPreBuffer[i + 2] & 0xff);
                    if (mPids.get(pid)) {
                        System.arraycopy(mPreBuffer, i, inputBuffer, destPos, TS_PACKET_SIZE);
                        destPos += TS_PACKET_SIZE;
                        filteredSize += TS_PACKET_SIZE;
                    }
                }
            }
            return filteredSize;
        }

        private int readInternal() {
            int readSize;
            try {
                readSize = mInputStream.read(mPreBuffer, 0, mPreBuffer.length);
            } catch (IOException e) {
                Log.e(TAG, "Error reading input stream", e);
                return -1;
            }
            return readSize;
        }

        private void close() {
            try {
                mInputStream.close();
            } catch (IOException e) {
                Log.e(TAG, "Error closing input stream:", e);
            }
            mInputStream = null;
        }
    }

    /**
     * Reads data from internal buffer.
     * @param pos the position to read from
     * @param buffer to read
     * @param offset start position of the read buffer
     * @param amount number of bytes to read
     * @return number of read bytes when successful, {@code -1} otherwise
     * @throws IOException
     */
    public int readAt(long pos, byte[] buffer, int offset, int amount) throws IOException {
        synchronized (mCircularBufferMonitor) {
            long initialBytesFetched = mBytesFetched;
            while (mBytesFetched < pos + amount && mStreaming) {
                try {
                    mCircularBufferMonitor.wait(READ_TIMEOUT_MS);
                } catch (InterruptedException e) {
                    // Wait again.
                    Thread.currentThread().interrupt();
                }
                if (initialBytesFetched == mBytesFetched) {
                    Log.w(TAG, "No data update for " + READ_TIMEOUT_MS + "ms. returning -1.");

                    // Returning -1 will make demux report EOS so that the input service can retry
                    // the playback.
                    return -1;
                }
            }
            if (!mStreaming) {
                Log.w(TAG, "Stream is already stopped.");
                return -1;
            }
            if (mBytesFetched - CIRCULAR_BUFFER_SIZE > pos) {
                Log.e(TAG, "Demux is requesting the data which is already overwritten.");
                return -1;
            }
            int posInBuffer = (int) (pos % CIRCULAR_BUFFER_SIZE);
            int bytesToCopyInFirstPass = amount;
            if (posInBuffer + bytesToCopyInFirstPass > mCircularBuffer.length) {
                bytesToCopyInFirstPass = mCircularBuffer.length - posInBuffer;
            }
            System.arraycopy(mCircularBuffer, posInBuffer, buffer, offset, bytesToCopyInFirstPass);
            if (bytesToCopyInFirstPass < amount) {
                System.arraycopy(mCircularBuffer, 0, buffer, offset + bytesToCopyInFirstPass,
                        amount - bytesToCopyInFirstPass);
            }
            mLastReadPosition = pos + amount;
            mCircularBufferMonitor.notify();
            return amount;
        }
    }

    /**
     * Adds {@link ScanChannel} instance for local files.
     *
     * @param output a list of channels where the results will be placed in
     */
    public static void addLocalStreamFiles(List<ScanChannel> output) {
        File dir = new File(FILE_DIR);
        if (!dir.exists()) return;

        File[] tsFiles = dir.listFiles();
        if (tsFiles == null) return;
        int freq = FileTsStreamer.FREQ_BASE;
        for (File file : tsFiles) {
            if (!file.isFile()) continue;
            output.add(ScanChannel.forFile(freq, file.getName()));
            freq += 100;
        }
    }

    /**
     * A thread managing a circular buffer that holds stream data to be consumed by player.
     * Keeps reading data in from a {@link StreamProvider} to hold enough amount for buffering.
     * Started and stopped by {@link #startStream()} and {@link #stopStream()}, respectively.
     */
    private class StreamingThread extends Thread {
        @Override
        public void run() {
            byte[] dataBuffer = new byte[READ_BUFFER_SIZE];

            synchronized (mCircularBufferMonitor) {
                mBytesFetched = 0;
                mLastReadPosition = 0;
            }

            while (true) {
                synchronized (mCircularBufferMonitor) {
                    while ((mBytesFetched - mLastReadPosition + PADDING_SIZE) > CIRCULAR_BUFFER_SIZE
                            && mStreaming) {
                        try {
                            mCircularBufferMonitor.wait();
                        } catch (InterruptedException e) {
                            // Wait again.
                            Thread.currentThread().interrupt();
                        }
                    }
                    if (!mStreaming) {
                        break;
                    }
                }

                int bytesWritten = mSource.read(dataBuffer);
                if (bytesWritten <= 0) {
                    try {
                        // When buffer is underrun, we sleep for short time to prevent
                        // unnecessary CPU draining.
                        sleep(BUFFER_UNDERRUN_SLEEP_MS);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    continue;
                }

                mEventDetector.feedTSStream(dataBuffer, 0, bytesWritten);

                synchronized (mCircularBufferMonitor) {
                    int posInBuffer = (int) (mBytesFetched % CIRCULAR_BUFFER_SIZE);
                    int bytesToCopyInFirstPass = bytesWritten;
                    if (posInBuffer + bytesToCopyInFirstPass > mCircularBuffer.length) {
                        bytesToCopyInFirstPass = mCircularBuffer.length - posInBuffer;
                    }
                    System.arraycopy(dataBuffer, 0, mCircularBuffer, posInBuffer,
                            bytesToCopyInFirstPass);
                    if (bytesToCopyInFirstPass < bytesWritten) {
                        System.arraycopy(dataBuffer, bytesToCopyInFirstPass, mCircularBuffer, 0,
                                bytesWritten - bytesToCopyInFirstPass);
                    }
                    mBytesFetched += bytesWritten;
                    mCircularBufferMonitor.notify();
                }
            }

            Log.i(TAG, "Streaming stopped");
            mSource.close();
        }
    }
}