aboutsummaryrefslogtreecommitdiff
path: root/usbtuner/src/com/android/usbtuner/UsbTunerDataSource.java
blob: 0d139311631f598fdce047e7e2a0f818cc76bc31 (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
/*
 * 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.usbtuner;

import android.media.MediaDataSource;
import android.util.Log;

import com.android.usbtuner.ChannelScanFileParser.ScanChannel;
import com.android.usbtuner.data.Channel;
import com.android.usbtuner.data.TunerChannel;
import com.android.usbtuner.tvinput.EventDetector;
import com.android.usbtuner.tvinput.EventDetector.EventListener;
import com.android.usbtuner.tvinput.UsbTunerDebug;

import java.io.IOException;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicLong;

/**
 * A {@link MediaDataSource} implementation which provides the mpeg2ts stream from the tuner device
 * to {@link MediaExtractor}.
 */
public class UsbTunerDataSource extends MediaDataSource implements InputStreamSource {
    private static final String TAG = "UsbTunerDataSource";

    private static final int MIN_READ_UNIT = 1500;
    private static final int READ_BUFFER_SIZE = MIN_READ_UNIT * 10; // ~15KB
    private static final int CIRCULAR_BUFFER_SIZE = MIN_READ_UNIT * 20000;  // ~ 30MB

    private static final int READ_TIMEOUT_MS = 5000; // 5 secs.
    private static final int BUFFER_UNDERRUN_SLEEP_MS = 10;

    private static final int CACHE_KEY_VERSION = 1;

    // UTCK stands for USB Tuner Cache Key.
    private static final String CACHE_KEY_PREFIX = "UTCK";

    private final Object mCircularBufferMonitor = new Object();
    private final byte[] mCircularBuffer = new byte[CIRCULAR_BUFFER_SIZE];
    private long mBytesFetched;
    private final AtomicLong mLastReadPosition = new AtomicLong();
    private boolean mEndOfStreamSent;
    private boolean mStreaming;

    private final TunerHal mTunerHal;
    private Thread mStreamingThread;
    private boolean mDeviceConfigured;
    private EventDetector mEventDetector;

    public UsbTunerDataSource(TunerHal tunerHal, EventListener eventListener) {
        mTunerHal = tunerHal;
        mEventDetector = new EventDetector(mTunerHal, eventListener);
    }

    /**
     * Starts the streaming of a configured program. Throws a runtime exception if no channel and
     * program have successfully been configured yet.
     */
    @Override
    public void startStream() {
        if (!mDeviceConfigured) {
            throw new RuntimeException("Channel and program not configured!");
        }

        synchronized (mCircularBufferMonitor) {
            if (mStreaming) {
                Log.w(TAG, "Streaming should be stopped before start streaming");
                return;
            }
            mStreaming = true;
            mBytesFetched = 0;
            mLastReadPosition.set(0L);
            mEndOfStreamSent = false;
        }

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

    /**
     * Sets the channel required to start streaming from this device. Afterwards, prepares the tuner
     * device for streaming. Package retrieval can be made at any time after invoking this method
     * and before stopping the stream.
     *
     * @param channel a {@link TunerChannel} instance tune to
     * @return {@code true} if the entire operation was successful; {@code false} otherwise
     */
    @Override
    public boolean tuneToChannel(TunerChannel channel) {
        if (mTunerHal.tune(channel.getFrequency(), channel.getModulation())) {
            if (channel.hasVideo()) {
                mTunerHal.addPidFilter(channel.getVideoPid(),
                        TunerHal.FILTER_TYPE_VIDEO);
            }
            if (channel.hasAudio()) {
                mTunerHal.addPidFilter(channel.getAudioPid(),
                        TunerHal.FILTER_TYPE_AUDIO);
            }
            mTunerHal.addPidFilter(channel.getPcrPid(),
                    TunerHal.FILTER_TYPE_PCR);
            if (mEventDetector != null) {
                mEventDetector.startDetecting(channel.getFrequency(), channel.getModulation());
            }
            mDeviceConfigured = true;
            return true;
        }
        return false;
    }

    /**
     * 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();
        } finally {
            mTunerHal.stopTune();
        }
    }

    @Override
    public long getLimit() {
        synchronized (mCircularBufferMonitor) {
            return mBytesFetched;
        }
    }

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

    private class StreamingThread extends Thread {
        @Override
        public void run() {
            // Buffers for streaming data from the tuner and the internal buffer.
            byte[] dataBuffer = new byte[READ_BUFFER_SIZE];

            while (true) {
                synchronized (mCircularBufferMonitor) {
                    if (!mStreaming) {
                        break;
                    }
                }

                int bytesWritten = mTunerHal.readTsStream(dataBuffer, dataBuffer.length);
                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;
                }

                if (mEventDetector != null) {
                    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");
        }
    }

    @Override
    public int readAt(long pos, byte[] buffer, int offset, int amount) throws IOException {
        synchronized (mCircularBufferMonitor) {
            if (mEndOfStreamSent) {
                // Nothing was received during READ_TIMEOUT_MS before.
                return -1;
            }
            if (mBytesFetched - CIRCULAR_BUFFER_SIZE > pos) {
                // Not available at circular buffer.
                Log.w(TAG, "Not available at circular buffer");
                return -1;
            }
            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.
                    mEndOfStreamSent = true;
                    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.set(pos + amount);
            mCircularBufferMonitor.notify();

            if (UsbTunerDebug.ENABLED) {
                UsbTunerDebug.setBytesInQueue((int) (mBytesFetched - mLastReadPosition.get()));
            }

            return amount;
        }
    }

    @Override
    public long getSize() throws IOException {
        return -1;
    }

    @Override
    public void close() {
        // Called from system MediaExtractor. All the resource should be closed
        // in stopStream() already.
    }

    @Override
    public int getType() {
        return Channel.TYPE_TUNER;
    }

    @Override
    public boolean setScanChannel(ScanChannel channel) {
        return false;
    }

    public static String generateCacheKey(TunerChannel channel, long timestampMs) {
        return String.format(Locale.ENGLISH, "%s-%x-%x-%x-%x", CACHE_KEY_PREFIX, CACHE_KEY_VERSION,
                channel.getFrequency(), channel.getProgramNumber(), timestampMs);
    }

    /**
     * Parses the timestamp from a cache key generated by {@link #generateCacheKey}.
     *
     * @param cacheKey a cache key generated by {@link #generateCacheKey}
     * @return the timestamp parsed from the given cache key. {@code -1} if unable to parse.
     */
    public static long parseTimestampFromCacheKey(String cacheKey) {
        String[] tokens = cacheKey.split("-");
        if (tokens.length < 2 || !tokens[0].equals(CACHE_KEY_PREFIX)) {
            return -1;
        }
        int version = Integer.parseInt(tokens[1], 16);
        if (version == 1) {
            return Long.parseLong(tokens[4], 16);
        } else {
            return -1;
        }
    }
}