带有用于 H264 流的 MediaCodec 解码器的 TextureView

TextureView with MediaCodec decoder for H264 streams

这是 的后续问题。

这是我的 TextureView 代码:

public class VideoTextureView extends TextureView implements SurfaceTextureListener{

    private static final String LOG_TAG = VideoTextureView.class.getSimpleName();
    private MediaCodecDecoder mMediaDecoder;
    private MediaCodecAsyncDecoder mMediaAsyncDecoder;

    public VideoTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setSurfaceTextureListener(this);
        Log.d(LOG_TAG, "Video texture created.");
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Log.d(LOG_TAG, "Surface Available: " + width + " x " + height);
        mMediaDecoder = new MediaCodecDecoder();
        mMediaDecoder.Start();
        mMediaDecoder.SetSurface(new Surface(getSurfaceTexture()));
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mMediaDecoder.Stop();
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // TODO Auto-generated method stub

    }

}

我的问题 - 我的 TextureView 实现是否可以渲染由 MediaCodec 解码的 H264 流?或者我是否需要进行 EGL 设置或其他任何操作?

提前致谢!

我目前正在使用 TextureView 在 android 上使用集合视图单元格在一个 activity 中渲染多个流(抱歉那里的 ios 术语)。

它工作正常,但问题是当您旋转设备时会出现 surface_destroyed,然后是 surface_available。如我所见,您正确地停止和启动了解码器。

我在解码器中做的一件事是:

List<NaluSegment> segments = NaluParser.parseNaluSegments(buffer);
        for (NaluSegment segment : segments) {
            // ignore unspecified NAL units.
            if (segment.getType() != NaluType.UNSPECIFIED) {

                // Hold the parameter set for stop/start initialization speed
                if (segment.getType() == NaluType.PPS) {
                    lastParameterSet[0] = segment;
                } else if (segment.getType() == NaluType.SPS) {
                    lastParameterSet[1] = segment;
                } else if (segment.getType() == NaluType.CODED_SLICE_IDR) {
                    lastParameterSet[2] = segment;
                }

                // add to input queue
                naluSegmentQueue.add(segment);
            }
        }

我保留最后一个参数集和最后一个关键帧,并在开始时先用这些填充 naluSegmentQueue 以减少视频渲染的延迟。

我的 TextureView 实现没问题,因为我也尝试使用 SurfaceView 并发现相同的结果。正如@fadden 所说 -

EGL setup is only required if you're rendering with GLES. TextureView combines a SurfaceTexture with a custom View, and does the GLES rendering for you. Which is why the View must be hardware-accelerated for TextureView to work.

感谢@fadden。