package net.ypresto.androidtranscoder.compat; import android.media.MediaCodec; import android.os.Build; import java.nio.ByteBuffer; /** * A Wrapper to MediaCodec that facilitates the use of API-dependent get{Input/Output}Buffer methods, * in order to prevent: http://stackoverflow.com/q/30646885 */ public class MediaCodecBufferCompatWrapper { final MediaCodec mMediaCodec; final ByteBuffer[] mInputBuffers; final ByteBuffer[] mOutputBuffers; public MediaCodecBufferCompatWrapper(MediaCodec mediaCodec) { mMediaCodec = mediaCodec; if (Build.VERSION.SDK_INT < 21) { mInputBuffers = mediaCodec.getInputBuffers(); mOutputBuffers = mediaCodec.getOutputBuffers(); } else { mInputBuffers = mOutputBuffers = null; } } public ByteBuffer getInputBuffer(final int index) { if (Build.VERSION.SDK_INT >= 21) { return mMediaCodec.getInputBuffer(index); } return mInputBuffers[index]; } public ByteBuffer getOutputBuffer(final int index) { if (Build.VERSION.SDK_INT >= 21) { return mMediaCodec.getOutputBuffer(index); } return mOutputBuffers[index]; } }