我在 android 中使用 MediaCodec 低级 Api 来解码从 IP CAMERA 接收的 h264 原始流。来自 IP 摄像机的原始流,通过 TCP/IP 连接接收。
要解码流,我的代码是:
@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);
但是在这行代码中,我总是收到“-1”。并且 mInfo.size() 也是我得到“0”。并且它没有在给定的表面上显示任何东西。
我错过了哪一步。请指导我。谢谢