1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tv.tuner.exoplayer.buffer; 18 19 import com.google.android.exoplayer.SampleHolder; 20 import com.google.android.exoplayer.SampleSource; 21 22 import java.util.LinkedList; 23 24 /** 25 * A sample queue which reads from the buffer and passes to player pipeline. 26 */ 27 public class SampleQueue { 28 private final LinkedList<SampleHolder> mQueue = new LinkedList<>(); 29 private final SamplePool mSamplePool; 30 private Long mLastQueuedPositionUs = null; 31 32 public SampleQueue(SamplePool samplePool) { 33 mSamplePool = samplePool; 34 } 35 36 public void queueSample(SampleHolder sample) { 37 mQueue.offer(sample); 38 mLastQueuedPositionUs = sample.timeUs; 39 } 40 41 public int dequeueSample(SampleHolder sample) { 42 SampleHolder sampleFromQueue = mQueue.poll(); 43 if (sampleFromQueue == null) { 44 return SampleSource.NOTHING_READ; 45 } 46 sample.ensureSpaceForWrite(sampleFromQueue.size); 47 sample.size = sampleFromQueue.size; 48 sample.flags = sampleFromQueue.flags; 49 sample.timeUs = sampleFromQueue.timeUs; 50 sample.clearData(); 51 sampleFromQueue.data.position(0).limit(sample.size); 52 sample.data.put(sampleFromQueue.data); 53 mSamplePool.releaseSample(sampleFromQueue); 54 return SampleSource.SAMPLE_READ; 55 } 56 57 public void clear() { 58 while (!mQueue.isEmpty()) { 59 mSamplePool.releaseSample(mQueue.poll()); 60 } 61 mLastQueuedPositionUs = null; 62 } 63 64 public Long getLastQueuedPositionUs() { 65 return mLastQueuedPositionUs; 66 } 67 68 public boolean isDurationGreaterThan(long durationUs) { 69 return !mQueue.isEmpty() && mQueue.getLast().timeUs - mQueue.getFirst().timeUs > durationUs; 70 } 71 72 public boolean isEmpty() { 73 return mQueue.isEmpty(); 74 } 75 } 76