I am using MediaCodec low level Api in android to decode h264 raw stream received from IP CAMERA. Raw stream from IP camera , receiving on TCP/IP connection.
To decode stream , My code is :
@Override
protected void onCreate(Bundle savedInstanceState) {
MediaCodec mCodecc;
MediaFormat mFormat;
BufferInfo mInfo;
ByteBuffer[] inputBuffers ;
ByteBuffer[] outputBuffers ;
}
protected void Init_Codec()
{
mCodecc = MediaCodec.createDecoderByType("video/avc");
mFormat = MediaFormat.createVideoFormat("video/avc", width, height);
mInfo = new BufferInfo();
mFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
mCodecc.configure(mFormat, holder.getSurface(), null,0);
}
protected void Start_Codec()
{
mCodecc.start();
inputBuffers = mCodecc.getInputBuffers();
outputBuffers = mCodecc.getOutputBuffers();
}
private void OnRawStreamReceived(final ByteBuffer buffer)
{
mHandler.postAtFrontOfQueue(new Runnable() {
@Override
public void run()
{
int inIndex = mCodecc.dequeueInputBuffer(10000);
if(inIndex>=0)
{
inputBuffers[inIndex] = buffer;
mCodecc.queueInputBuffer(inIndex, 0,buffer.limit(),System.currentTimeMillis(), 0);
}
int outIndex = mCodecc.dequeueOutputBuffer(mInfo, 10000);
switch (outIndex) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
Log.d("DecodeActivity", "INFO_OUTPUT_BUFFERS_CHANGED");
outputBuffers = mCodecc.getOutputBuffers();
break;
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
Log.d("DecodeActivity", "New format " + mCodecc.getOutputFormat());
break;
case MediaCodec.INFO_TRY_AGAIN_LATER:
Log.d("DecodeActivity", "dequeueOutputBuffer timed out! --- size : " + mInfo.size );
break;
default:
ByteBuffer buffer = outputBuffers[outIndex];
mCodecc.releaseOutputBuffer(outIndex, true);
break;
}
}
int outIndex = mCodecc.dequeueOutputBuffer(mInfo, 10000);
But at this line of code , I'm always receiving "-1". and mInfo.size() is also I'm getting "0". and It is not displaying anything on given surface.
Which step I'm missing. please guide me. thanx
I'm assuming you're passing in individual "access units", i.e. one frame of video per buffer.
What you seem to be missing is the codec setup block, which is expected to be in the first buffer submitted (could also be tucked into the MediaFormat
via format.setByteBuffer("csd-0", ...)
). Assuming that data is coming out of your particular encoder, all you have to do is queue the first buffer with the BUFFER_FLAG_CODEC_CONFIG flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With