zhantong/Android-VideoToImages

getOutputImage(index); 报异常

Samcasa opened this issue · 0 comments

感谢您的开源。

我现在是从rtsp流中解码了h264的数据出来,所以没有mediaextractor , 模仿您的写法,为什么getOutputImage一直是空的呢?

感谢
`
do {
if (frameInfo != null) {
byte[] pBuf = frameInfo.buffer;
index = mCodec.dequeueInputBuffer(10);
if (index >= 0) {
ByteBuffer buffer = mCodec.getInputBuffers()[index];
buffer.clear();
if (pBuf.length > buffer.remaining()) {
mCodec.queueInputBuffer(index, 0, 0, frameInfo.stamp, 0);
} else {
buffer.put(pBuf, frameInfo.offset, frameInfo.length);
mCodec.queueInputBuffer(index, 0, buffer.position(), frameInfo.stamp + differ, 0);
}
frameInfo = null;
}
}
index = mCodec.dequeueOutputBuffer(info, 10); //
//此处回调数据出去
if (index > 0) {
//这里的image一直是空的
Image image = mCodec.getOutputImage(index);
if (image != null) {
byte[] dataFromImage = getDataFromImage(image, COLOR_FormatNV21);
if (i420callback != null) {
i420callback.onI420Data(dataFromImage, frameInfo);
}
}

                            }


                            switch (index) {
                                case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
                                    Log.i(TAG, "INFO_OUTPUT_BUFFERS_CHANGED");
                                    break;
                                case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:
                                    MediaFormat mf = mCodec.getOutputFormat();
                                    Log.i(TAG, "INFO_OUTPUT_FORMAT_CHANGED :" + mf);
                                    break;
                                case MediaCodec.INFO_TRY_AGAIN_LATER:
                                    // 输出为空
                                    break;
                                default:
                                    // 输出队列不为空
                                    // -1表示为第一帧数据
                                    long newSleepUs = -1;
                                    boolean firstTime = previewStampUs == 0l;
                                    if (!firstTime) {
                                        long sleepUs = (info.presentationTimeUs - previewStampUs);
                                        if (sleepUs > 50000) {
                                            // 时间戳异常,可能服务器丢帧了。
                                            Log.w(TAG, "sleep time.too long:" + sleepUs);
                                            sleepUs = 50000;
                                        }
                                        {
                                            long cache = mNewestStample - previewStampUs;
                                            newSleepUs = fixSleepTime(sleepUs, cache, 0);
                                        }
                                    }
                                    previewStampUs = info.presentationTimeUs;

                                    if (false && Build.VERSION.SDK_INT >= 21) {

                                        Log.d(TAG, String.format("releaseoutputbuffer:%d,stampUs:%d", index, previewStampUs));
                                        mCodec.releaseOutputBuffer(index, previewStampUs);
                                    } else {
                                        if (newSleepUs < 0) {
                                            newSleepUs = 0;
                                        }

// Log.i(TAG,String.format("sleep:%d", newSleepUs/1000));
Thread.sleep(newSleepUs / 1000);
mCodec.releaseOutputBuffer(index, true);
}
if (firstTime) {
Log.i(TAG, String.format("POST VIDEO_DISPLAYED!!!"));
ResultReceiver rr = mRR;
if (rr != null) rr.send(RESULT_VIDEO_DISPLAYED, null);
}
}
} while (frameInfo != null || index < MediaCodec.INFO_TRY_AGAIN_LATER);

`