Decoding H264 streaming using android low level api

烂漫一生 提交于 2019-12-03 07:39:42

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15756735/decoding-h264-streaming-using-android-low-level-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!