greenjim301 / Titan

Android手机采集音视频流推到服务器
https://blog.csdn.net/greenjim301/article/details/80560283
12 stars 4 forks source link

MediaCodec BufferOverflowException #1

Open renruigang opened 5 years ago

renruigang commented 5 years ago

你好,进行编码推流报错: java.nio.BufferOverflowException at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:264) at java.nio.ByteBuffer.put(ByteBuffer.java:642) at com.lisi.titan.TitanVideoEncoder.encodeByteBuffer(TitanVideoEncoder.java:276) at com.lisi.titan.TitanVideoEncoder.encode(TitanVideoEncoder.java:226) at com.lisi.titan.TitanCameraSession$3.onImageAvailable(TitanCameraSession.java:619) at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:658) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:163) at android.os.HandlerThread.run(HandlerThread.java:61)

ByteBuffer buffer; try { buffer = codec.getInputBuffer(index); } catch (IllegalStateException e) { Log.e(TAG, "getInputBuffers failed", e); return -1; }

    buffer.clear();
    buffer.put(videoFrame);

是不是缓存太小的原因,要怎么修改呢?

greenjim301 commented 5 years ago

这里是先clear再put,而且调用前有加锁,所以肯定是videoFrame超出了Capacity。主要考虑videoFrame的宽高与初始化MediaCoedec宽高是否一致,或者YUV格式设置是否匹配。