1 /* 2 * Copyright (C) 2016 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 package com.google.android.exoplayer2.decoder; 17 18 import androidx.annotation.Nullable; 19 import java.nio.ByteBuffer; 20 import java.nio.ByteOrder; 21 22 /** 23 * Buffer for {@link SimpleDecoder} output. 24 */ 25 public class SimpleOutputBuffer extends OutputBuffer { 26 27 private final Owner<SimpleOutputBuffer> owner; 28 29 @Nullable public ByteBuffer data; 30 SimpleOutputBuffer(Owner<SimpleOutputBuffer> owner)31 public SimpleOutputBuffer(Owner<SimpleOutputBuffer> owner) { 32 this.owner = owner; 33 } 34 35 /** 36 * Initializes the buffer. 37 * 38 * @param timeUs The presentation timestamp for the buffer, in microseconds. 39 * @param size An upper bound on the size of the data that will be written to the buffer. 40 * @return The {@link #data} buffer, for convenience. 41 */ init(long timeUs, int size)42 public ByteBuffer init(long timeUs, int size) { 43 this.timeUs = timeUs; 44 if (data == null || data.capacity() < size) { 45 data = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()); 46 } 47 data.position(0); 48 data.limit(size); 49 return data; 50 } 51 52 @Override clear()53 public void clear() { 54 super.clear(); 55 if (data != null) { 56 data.clear(); 57 } 58 } 59 60 @Override release()61 public void release() { 62 owner.releaseOutputBuffer(this); 63 } 64 65 } 66