记录利用MediaCodec解码h264遇到的一些问题
Java层的主要两段代码
/**
* 初始化MediaCodec
* @param codecName
* @param width
* @param height
* @param csd_0
* @param csd_1
*/
public void initMediaCodec(String codecName, int width, int height, byte[] csd_0, byte[] csd_1)
{
if(surface != null)
{
try {
Log.e("MyMusic",Thread.currentThread().toString());
wlGLSurfaceView.getWlRender().setRenderType(WlRender.RENDER_MEDIACODEC);
String mime = WlVideoSupportUitl.findVideoCodecName(codecName);
mediaFormat = MediaFormat.createVideoFormat(mime, width, height);
mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, width * height);
mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(csd_0));
mediaFormat.setByteBuffer("csd-1", ByteBuffer.wrap(csd_1));
MyLog.d(mediaFormat.toString());
mediaFormat=initExtractor(Environment.getExternalStorageDirectory()+"/bbb.mp4");
mediaCodec = MediaCodec.createDecoderByType(mime);
info = new MediaCodec.BufferInfo();
//flag有两种模式,CONFIGURE_FLAG_ENCODE(值等于1)为编码模式,其他的值为解码模式。
mediaCodec.configure(mediaFormat, surface, null, 0);
mediaCodec.start();
Log.e("MyMusic","start success");
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
if(wlOnErrorListener != null)
{
wlOnErrorListener.onError(2001, "surface is null");
}
}
}
/**
* 从ffmpeg层回调,data参数就是AVPacket中的data属性
* @param datasize
* @param data
*/
public void decodeAVPacket(int datasize, byte[] data)
{
try {
if(surface != null && datasize > 0 && data != null && mediaCodec != null)
{
int intputBufferIndex = mediaCodec.dequeueInputBuffer(10);
if(intputBufferIndex >= 0)
{
ByteBuffer byteBuffer = mediaCodec.getInputBuffers()[intputBufferIndex];
byteBuffer.clear();
byteBuffer.put(data);
mediaCodec.queueInputBuffer(intputBufferIndex, 0, datasize, 0, 0);
}
int outputBufferIndex = mediaCodec.dequeueOutputBuffer(info, 10);
while(outputBufferIndex >= 0)
{
mediaCodec.releaseOutputBuffer(outputBufferIndex, true);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(info, 10);
}
}
}
catch (Exception e)
{
Log.e("VoicePlayer","异常"+e.toString());
e.printStackTrace();
}
}
测试手机是 Galaxy S6和坚果Pro
坚果Pro表现正常,Galaxy S6在执行到int intputBufferIndex = mediaCodec.dequeueInputBuffer(10);这行代码的时候抛出了上面的异常。
后来看了下系统sample中关于MediaCodec的使用(android-BasicMeidaDecoder-master项目),在Galaxy S6是正常的,项目中对于MediaFormat的初始化不是自己新建的,而是直接获取的
/**
* 初始化MediaCodec
* @param codecName
* @param width
* @param height
* @param csd_0
* @param csd_1
*/
public void initMediaCodec(String codecName, int width, int height, byte[] csd_0, byte[] csd_1)
{
if(surface != null)
{
try {
Log.e("MyMusic",Thread.currentThread().toString());
wlGLSurfaceView.getWlRender().setRenderType(WlRender.RENDER_MEDIACODEC);
String mime = WlVideoSupportUitl.findVideoCodecName(codecName);
mediaFormat=initExtractor(Environment.getExternalStorageDirectory()+"/bbb.mp4");
mediaCodec = MediaCodec.createDecoderByType(mime);
info = new MediaCodec.BufferInfo();
//flag有两种模式,CONFIGURE_FLAG_ENCODE(值等于1)为编码模式,其他的值为解码模式。
mediaCodec.configure(mediaFormat, surface, null, 0);
mediaCodec.start();
Log.e("MyMusic","start success");
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
if(wlOnErrorListener != null)
{
wlOnErrorListener.onError(2001, "surface is null");
}
}
}
private MediaExtractor mExtractor = new MediaExtractor();
public MediaFormat initExtractor(String path)
{
try {
Uri videoUri = Uri.parse(path);
mExtractor.setDataSource(context, videoUri, null);
int nTracks = mExtractor.getTrackCount();
// Begin by unselecting all of the tracks in the extractor, so we won't see
// any tracks that we haven't explicitly selected.
for (int i = 0; i < nTracks; ++i) {
mExtractor.unselectTrack(i);
}
// Find the first video track in the stream. In a real-world application
// it's possible that the stream would contain multiple tracks, but this
// sample assumes that we just want to play the first one.
for (int i = 0; i < nTracks; ++i) {
// Try to create a video codec for this track. This call will return null if the
// track is not a video track, or not a recognized video format. Once it returns
// a valid MediaCodecWrapper, we can break out of the loop.
MediaFormat trackFormat=mExtractor.getTrackFormat(i);
final String mimeType = trackFormat.getString(MediaFormat.KEY_MIME);
// Check to see if this is actually a video mime type. If it is, then create
// a codec that can decode this mime type.
if (mimeType.contains("video/")) {
return trackFormat;
}
}
}
catch (Exception e)
{
}
return null;
}
测试的时候是正常的,那出现异常的原因就是对MediaFormat的初始化了,对比之后发现出现问题的地方应该是csd-0和csd-1的值的获取
这个轨道数有时候得到的是0
int nTracks = mExtractor.getTrackCount();
比如这个地址:http://pl3.live.panda.tv/live_panda/dd9f182bcec99d04099113e618cfc5b3_mid.flv?sign=ce033518ab3f12dca896e414c42fd4a6&time=&ts=5bea339e&rid=-56292951