简体   繁体   English

具有MediaCodec解码器的TextureView,用于H264流

[英]TextureView with MediaCodec decoder for H264 streams

This is a follow-up question of this question . 这是该问题的后续问题

This is my TextureView code: 这是我的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

    }

}

My question - Is my TextureView implementation okay for rendering H264 streams decoded by MediaCodec ? 我的问题-我的TextureView实现可以通过MediaCodec解码H264流吗? Or do I need to do EGL setup or anything else? 还是我需要进行EGL设置或其他?

Thanks in advance! 提前致谢!

I am currently using a TextureView for rendering multiple streams in one activity using collection view cells on android (Sorry for ios terminology there). 我目前正在使用TextureView在android上使用集合视图单元在一个活动中呈现多个流(抱歉,那里的ios术语)。

It works fine but the issue is when you for example rotate the device there will be surface_destroyed followed by surface_available. 它工作正常,但问题是,例如当您旋转设备时,将出现surface_destroyed,然后是surface_available。 As i see you are correctly stopping and starting your decoder. 如我所见,您正在正确地停止和启动解码器。

One thing i do in my Decoder is: 我在解码器中所做的一件事是:

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);
            }
        }

I hold onto the last parameter sets and last keyframe and on start i fill the naluSegmentQueue with these first off to reduce the delay in video rendering. 我保留了最后一个参数集和最后一个关键帧,并从一开始就填充了naluSegmentQueue,以减少视频渲染的延迟。

My TextureView implementation is okay as I tried with SurfaceView too and found same result. 我的TextureView实现也可以,因为我也尝试过SurfaceView并找到了相同的结果。 And as @fadden said - 正如@fadden所说-

EGL setup is only required if you're rendering with GLES. 仅当使用GLES进行渲染时才需要EGL设置。 TextureView combines a SurfaceTexture with a custom View, and does the GLES rendering for you. TextureView将SurfaceTexture与自定义View结合在一起,并为您执行GLES渲染。 Which is why the View must be hardware-accelerated for TextureView to work. 这就是为什么必须对硬件进行硬件加速以使TextureView起作用的原因。

Thanks to @fadden. 感谢@fadden。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM