aboutsummaryrefslogtreecommitdiff
path: root/tuner/src/com/android/tv/tuner/exoplayer/buffer/SampleChunk.java
blob: bf77a6eba37411c50d5a52e74cff11e600f019fd (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
/*
 * Copyright (C) 2016 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.buffer;

import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import com.google.android.exoplayer.SampleHolder;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * {@link SampleChunk} stores samples into file and makes them available for read. Stored file = {
 * Header, Sample } * N Header = sample size : int, sample flag : int, sample PTS in micro second :
 * long
 */
public class SampleChunk {
    private static final String TAG = "SampleChunk";
    private static final boolean DEBUG = false;

    private final long mCreatedTimeMs;
    private final long mStartPositionUs;
    private SampleChunk mNextChunk;

    // Header = sample size : int, sample flag : int, sample PTS in micro second : long
    private static final int SAMPLE_HEADER_LENGTH = 16;

    private final File mFile;
    private final ChunkCallback mChunkCallback;
    private final SamplePool mSamplePool;
    private RandomAccessFile mAccessFile;
    private long mWriteOffset;
    private boolean mWriteFinished;
    private boolean mIsReading;
    private boolean mIsWriting;

    /** A callback for chunks being committed to permanent storage. */
    public abstract static class ChunkCallback {

        /**
         * Notifies when writing a SampleChunk is completed.
         *
         * @param chunk SampleChunk which is written completely
         */
        public void onChunkWrite(SampleChunk chunk) {}

        /**
         * Notifies when a SampleChunk is deleted.
         *
         * @param chunk SampleChunk which is deleted from storage
         */
        public void onChunkDelete(SampleChunk chunk) {}
    }

    /** A class for SampleChunk creation. */
    public static class SampleChunkCreator {

        /**
         * Returns a newly created SampleChunk to read & write samples.
         *
         * @param samplePool sample allocator
         * @param file filename which will be created newly
         * @param startPositionUs the start position of the earliest sample to be stored
         * @param chunkCallback for total storage usage change notification
         */
        @VisibleForTesting
        public SampleChunk createSampleChunk(
                SamplePool samplePool,
                File file,
                long startPositionUs,
                ChunkCallback chunkCallback) {
            return new SampleChunk(
                    samplePool, file, startPositionUs, System.currentTimeMillis(), chunkCallback);
        }

        /**
         * Returns a newly created SampleChunk which is backed by an existing file. Created
         * SampleChunk is read-only.
         *
         * @param samplePool sample allocator
         * @param bufferDir the directory where the file to read is located
         * @param filename the filename which will be read afterwards
         * @param startPositionUs the start position of the earliest sample in the file
         * @param chunkCallback for total storage usage change notification
         * @param prev the previous SampleChunk just before the newly created SampleChunk
         * @throws IOException
         */
        SampleChunk loadSampleChunkFromFile(
                SamplePool samplePool,
                File bufferDir,
                String filename,
                long startPositionUs,
                ChunkCallback chunkCallback,
                SampleChunk prev)
                throws IOException {
            File file = new File(bufferDir, filename);
            SampleChunk chunk = new SampleChunk(samplePool, file, startPositionUs, chunkCallback);
            if (prev != null) {
                prev.mNextChunk = chunk;
            }
            return chunk;
        }
    }

    /**
     * Handles I/O for SampleChunk. Maintains current SampleChunk and the current offset for next
     * I/O operation.
     */
    @VisibleForTesting
    public static class IoState {
        private SampleChunk mChunk;
        private long mCurrentOffset;

        private boolean equals(SampleChunk chunk, long offset) {
            return chunk == mChunk && mCurrentOffset == offset;
        }

        /** Returns whether read I/O operation is finished. */
        boolean isReadFinished() {
            return mChunk == null;
        }

        /** Returns the start position of the current SampleChunk */
        long getStartPositionUs() {
            return mChunk == null ? 0 : mChunk.getStartPositionUs();
        }

        private void reset(@Nullable SampleChunk chunk) {
            mChunk = chunk;
            mCurrentOffset = 0;
        }

        private void reset(SampleChunk chunk, long offset) {
            mChunk = chunk;
            mCurrentOffset = offset;
        }

        /**
         * Prepares for read I/O operation from a new SampleChunk.
         *
         * @param chunk the new SampleChunk to read from
         * @throws IOException
         */
        void openRead(SampleChunk chunk, long offset) throws IOException {
            if (mChunk != null) {
                mChunk.closeRead();
            }
            chunk.openRead();
            reset(chunk, offset);
        }

        /**
         * Prepares for write I/O operation to a new SampleChunk.
         *
         * @param chunk the new SampleChunk to write samples afterwards
         * @throws IOException
         */
        void openWrite(SampleChunk chunk) throws IOException {
            if (mChunk != null) {
                mChunk.closeWrite(chunk);
            }
            chunk.openWrite();
            reset(chunk);
        }

        /**
         * Reads a sample if it is available.
         *
         * @return Returns a sample if it is available, null otherwise.
         * @throws IOException
         */
        SampleHolder read() throws IOException {
            if (mChunk != null && mChunk.isReadFinished(this)) {
                SampleChunk next = mChunk.mNextChunk;
                mChunk.closeRead();
                if (next != null) {
                    next.openRead();
                }
                reset(next);
            }
            if (mChunk != null) {
                try {
                    return mChunk.read(this);
                } catch (IllegalStateException e) {
                    // Write is finished and there is no additional buffer to read.
                    Log.w(TAG, "Tried to read sample over EOS.");
                    return null;
                }
            } else {
                return null;
            }
        }

        /**
         * Writes a sample.
         *
         * @param sample to write
         * @param nextChunk if this is {@code null} writes at the current SampleChunk, otherwise
         *     close current SampleChunk and writes at this
         * @throws IOException
         */
        void write(SampleHolder sample, SampleChunk nextChunk) throws IOException {
            if (mChunk == null) {
                throw new IOException("mChunk should not be null");
            }
            if (nextChunk != null) {
                if (mChunk.mNextChunk != null) {
                    throw new IllegalStateException("Requested write for wrong SampleChunk");
                }
                mChunk.closeWrite(nextChunk);
                mChunk.mChunkCallback.onChunkWrite(mChunk);
                nextChunk.openWrite();
                reset(nextChunk);
            }
            mChunk.write(sample, this);
        }

        /**
         * Finishes write I/O operation.
         *
         * @throws IOException
         */
        void closeWrite() throws IOException {
            if (mChunk != null) {
                mChunk.closeWrite(null);
            }
        }

        /** Returns the current SampleChunk for subsequent I/O operation. */
        SampleChunk getChunk() {
            return mChunk;
        }

        /** Returns the current offset of the current SampleChunk for subsequent I/O operation. */
        long getOffset() {
            return mCurrentOffset;
        }

        /**
         * Releases SampleChunk. the SampleChunk will not be used anymore.
         *
         * @param chunk to release
         * @param delete {@code true} when the backed file needs to be deleted, {@code false}
         *     otherwise.
         */
        static void release(SampleChunk chunk, boolean delete) {
            chunk.release(delete);
        }
    }

    @VisibleForTesting
    protected SampleChunk(
            SamplePool samplePool,
            File file,
            long startPositionUs,
            long createdTimeMs,
            ChunkCallback chunkCallback) {
        mStartPositionUs = startPositionUs;
        mCreatedTimeMs = createdTimeMs;
        mSamplePool = samplePool;
        mFile = file;
        mChunkCallback = chunkCallback;
    }

    // Constructor of SampleChunk which is backed by the given existing file.
    private SampleChunk(
            SamplePool samplePool, File file, long startPositionUs, ChunkCallback chunkCallback)
            throws IOException {
        mStartPositionUs = startPositionUs;
        mCreatedTimeMs = mStartPositionUs / 1000;
        mSamplePool = samplePool;
        mFile = file;
        mChunkCallback = chunkCallback;
        mWriteFinished = true;
    }

    private void openRead() throws IOException {
        if (!mIsReading) {
            if (mAccessFile == null) {
                mAccessFile = new RandomAccessFile(mFile, "r");
            }
            if (mWriteFinished && mWriteOffset == 0) {
                // Lazy loading of write offset, in order not to load
                // all SampleChunk's write offset at start time of recorded playback.
                mWriteOffset = mAccessFile.length();
            }
            mIsReading = true;
        }
    }

    private void openWrite() throws IOException {
        if (mWriteFinished) {
            throw new IllegalStateException("Opened for write though write is already finished");
        }
        if (!mIsWriting) {
            if (mIsReading) {
                throw new IllegalStateException(
                        "Write is requested for " + "an already opened SampleChunk");
            }
            mAccessFile = new RandomAccessFile(mFile, "rw");
            mIsWriting = true;
        }
    }

    private void CloseAccessFileIfNeeded() throws IOException {
        if (!mIsReading && !mIsWriting) {
            try {
                if (mAccessFile != null) {
                    mAccessFile.close();
                }
            } finally {
                mAccessFile = null;
            }
        }
    }

    private void closeRead() throws IOException {
        if (mIsReading) {
            mIsReading = false;
            CloseAccessFileIfNeeded();
        }
    }

    private void closeWrite(SampleChunk nextChunk) throws IOException {
        if (mIsWriting) {
            mNextChunk = nextChunk;
            mIsWriting = false;
            mWriteFinished = true;
            CloseAccessFileIfNeeded();
        }
    }

    private boolean isReadFinished(IoState state) {
        return mWriteFinished && state.equals(this, mWriteOffset);
    }

    private SampleHolder read(IoState state) throws IOException {
        if (mAccessFile == null || state.mChunk != this) {
            throw new IllegalStateException("Requested read for wrong SampleChunk");
        }
        long offset = state.mCurrentOffset;
        if (offset >= mWriteOffset) {
            if (mWriteFinished) {
                throw new IllegalStateException("Requested read for wrong range");
            } else {
                if (offset != mWriteOffset) {
                    Log.e(TAG, "This should not happen!");
                }
                return null;
            }
        }
        mAccessFile.seek(offset);
        int size = mAccessFile.readInt();
        SampleHolder sample = mSamplePool.acquireSample(size);
        sample.size = size;
        sample.flags = mAccessFile.readInt();
        sample.timeUs = mAccessFile.readLong();
        sample.clearData();
        sample.data.put(
                mAccessFile
                        .getChannel()
                        .map(
                                FileChannel.MapMode.READ_ONLY,
                                offset + SAMPLE_HEADER_LENGTH,
                                sample.size));
        offset += sample.size + SAMPLE_HEADER_LENGTH;
        state.mCurrentOffset = offset;
        return sample;
    }

    @VisibleForTesting
    protected void write(SampleHolder sample, IoState state) throws IOException {
        if (mAccessFile == null || mNextChunk != null || !state.equals(this, mWriteOffset)) {
            throw new IllegalStateException("Requested write for wrong SampleChunk");
        }

        mAccessFile.seek(mWriteOffset);
        mAccessFile.writeInt(sample.size);
        mAccessFile.writeInt(sample.flags);
        mAccessFile.writeLong(sample.timeUs);
        sample.data.position(0).limit(sample.size);
        mAccessFile.getChannel().position(mWriteOffset + SAMPLE_HEADER_LENGTH).write(sample.data);
        mWriteOffset += sample.size + SAMPLE_HEADER_LENGTH;
        state.mCurrentOffset = mWriteOffset;
    }

    private void release(boolean delete) {
        mWriteFinished = true;
        mIsReading = mIsWriting = false;
        try {
            if (mAccessFile != null) {
                mAccessFile.close();
            }
        } catch (IOException e) {
            // Since the SampleChunk will not be reused, ignore exception.
        }
        if (delete) {
            mFile.delete();
            mChunkCallback.onChunkDelete(this);
        }
    }

    /** Returns the start position. */
    public long getStartPositionUs() {
        return mStartPositionUs;
    }

    /** Returns the creation time. */
    public long getCreatedTimeMs() {
        return mCreatedTimeMs;
    }

    /** Returns the current size. */
    public long getSize() {
        return mWriteOffset;
    }
}