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.size = sampleFromQueue.size; 47 sample.flags = sampleFromQueue.flags; 48 sample.timeUs = sampleFromQueue.timeUs; 49 sample.clearData(); 50 sampleFromQueue.data.position(0).limit(sample.size); 51 sample.data.put(sampleFromQueue.data); 52 mSamplePool.releaseSample(sampleFromQueue); 53 return SampleSource.SAMPLE_READ; 54 } 55 56 public void clear() { 57 while (!mQueue.isEmpty()) { 58 mSamplePool.releaseSample(mQueue.poll()); 59 } 60 mLastQueuedPositionUs = null; 61 } 62 63 public Long getLastQueuedPositionUs() { 64 return mLastQueuedPositionUs; 65 } 66 67 public boolean isDurationGreaterThan(long durationUs) { 68 return !mQueue.isEmpty() && mQueue.getLast().timeUs - mQueue.getFirst().timeUs > durationUs; 69 } 70 71 public boolean isEmpty() { 72 return mQueue.isEmpty(); 73 } 74 } 75