1 /*
2  * Copyright (C) 2020 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.exoplayer2.buffer;
18 
19 import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
20 
21 import java.util.LinkedList;
22 
23 /** Pool of samples to recycle ByteBuffers as much as possible. */
24 public class InputBufferPool {
25     private final LinkedList<DecoderInputBuffer> mInputBufferPool = new LinkedList<>();
26 
27     /**
28      * Acquires a sample with a buffer larger than size from the pool. Allocate new one or resize an
29      * existing buffer if necessary.
30      */
acquireSample(int size)31     public synchronized DecoderInputBuffer acquireSample(int size) {
32         if (mInputBufferPool.isEmpty()) {
33             DecoderInputBuffer sample =
34                     new DecoderInputBuffer(DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_NORMAL);
35             sample.ensureSpaceForWrite(size);
36             return sample;
37         }
38         DecoderInputBuffer smallestSufficientSample = null;
39         DecoderInputBuffer maxSample = mInputBufferPool.getFirst();
40         for (DecoderInputBuffer sample : mInputBufferPool) {
41             // Grab the smallest sufficient sample.
42             if (sample.data.capacity() >= size
43                     && (smallestSufficientSample == null
44                             || smallestSufficientSample.data.capacity() > sample.data.capacity())) {
45                 smallestSufficientSample = sample;
46             }
47 
48             // Grab the max size sample.
49             if (maxSample.data.capacity() < sample.data.capacity()) {
50                 maxSample = sample;
51             }
52         }
53         DecoderInputBuffer sampleFromPool = smallestSufficientSample;
54 
55         // If there's no sufficient sample, grab the maximum sample and resize it to size.
56         if (sampleFromPool == null) {
57             sampleFromPool = maxSample;
58             sampleFromPool.ensureSpaceForWrite(size);
59         }
60         mInputBufferPool.remove(sampleFromPool);
61         return sampleFromPool;
62     }
63 
64     /** Releases the sample back to the pool. */
releaseSample(DecoderInputBuffer sample)65     public synchronized void releaseSample(DecoderInputBuffer sample) {
66         sample.clear();
67         mInputBufferPool.offerLast(sample);
68     }
69 }
70