在Android Lollipop 5.0.2上慢速H264 1080P @ 60fps解码

时间:2015-10-07 18:20:57

标签: android-5.0-lollipop h.264 mediacodec

我正在为公司项目开发一个JAVA RTP流应用程序,它应该能够加入多播服务器并接收RTP数据包。后来我使用H264 Depacketizer从NAL FU重新创建一个完整的帧(Keep将数据追加到End Bit& Marker Bit设置)

我想在Android中解码并显示原始h264视频字节流,因此我目前正在使用配置了硬件解码器的MediaCodec类。

应用程序已启动并运行Jeallybean(API 17)。我需要解码的各种解决方案是:
480P,30/60 FPS
720P / I,30/60 FPS
1080P / I,30/60 FPS

最近,由于系统升级,我们将应用程序移植到Android L版本5.0.2。我的应用程序无法播放720p @ 60fps和1080p @ 60fps等高分辨率视频。

出于调试目的,我开始从转储文件向MediaCodec提供尺寸为小学的H264帧,并发现视频是滞后的。
我使用的示例视频有时间戳,似乎在渲染视频中进行1秒所需的实际时间更多 以下是我的示例代码和示例视频的链接
h264视频https://www.dropbox.com/s/cocjhhovihm8q25/dump60fps.h264?dl=0
h264 framesize https://www.dropbox.com/s/r146d5zederrne1/dump60fps.size?dl=0

另外,由于这是我在stackoverflow上的问题,请关注错误的代码格式和直接引用。

public class MainActivity extends Activity {

    static final String TAG = "MainActivity";
    private PlayerThread mPlayer = null;
    private static final String MIME_TYPE = "video/avc";


    private byte[] mSPSPPSFrame = new byte [3000];
    private byte[] sps = new byte[37];
    File videoFile = null;
    File videoFile1 = null;
    TextView tv ;

    FileInputStream videoFileStream = null;
    FileInputStream videoFileStream1 = null;
    int[] tall = null ;
    SpeedControlCallback mspeed = new SpeedControlCallback();

    int mStreamLen = 0;
    FrameLayout game;
    RelativeLayout rl ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        //mVideoSurfaceView = (SurfaceView)findViewById(R.id.videoSurfaceView);
        setContentView(R.layout.activity_main);

        SurfaceView first = (SurfaceView) findViewById(R.id.firstSurface);
        first.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                Log.d(TAG, "First surface created!");
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
                Log.d(TAG, "surfaceChanged()");
                surfaceHolder.getSurface();

                if (mPlayer == null) {
                    mPlayer = new PlayerThread(surfaceHolder.getSurface());
                    mPlayer.start();

                }

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                Log.d(TAG, "First surface destroyed!");
            }
        });

        tv = (TextView) findViewById(R.id.textview);

        videoFile = new File("/data/local/tmp/dump60fps.h264");
        videoFile1 = new File("/data/local/tmp/dump60fps.size");

    }

    private class PlayerThread extends Thread {
        private Surface surface;

        public PlayerThread(Surface surface) {
            this.surface = surface;
        }

        @Override
        public void run() {
            try {
                decodeVideo(0, 1920,1080, 50, surface);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }

        }
    }




    private void decodeVideo(int testinput, int width, int height,
            int threshold, Surface surface) throws Throwable {

        MediaCodec codec = null;
        MediaFormat mFormat;
        final long kTimeOutUs = 10000;
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
        boolean sawInputEOS = false;
        boolean sawOutputEOS = false;
        MediaFormat oformat = null;
        int errors = -1;
        long presentationTimeUs = 0L;
        boolean mVideoStart = false;
        byte[] byteArray = new byte[65525*5*3];
        int i; 
        int sizeInBytes = 0, index, sampleSize = 0;


        try {

            byte[] bytes = new byte[(int) videoFile1.length()];
            FileInputStream fis = new FileInputStream(videoFile1);
            fis.read(bytes);
            fis.close();
            String[] valueStr = new String(bytes).trim().split("\\s+");
            tall = new int[valueStr.length];
            mStreamLen = valueStr.length;
            Log.e(TAG, "++++++ Total Frames ++++++"+mStreamLen);
            for ( i = 0; i < valueStr.length; i++) {
                tall[i] = Integer.parseInt(valueStr[i]);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        index =1;
        try {
            videoFileStream = new FileInputStream(videoFile);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }


        System.currentTimeMillis();

        if (mVideoStart == false) {
            try {
                sizeInBytes = videoFileStream.read(mSPSPPSFrame, 0,37);
                Log.e(TAG, "VideoEngine configure ."+sizeInBytes);
                //for (i = 0 ; i < sizeInBytes; i++){
                //  Log.e(TAG, "VideoEngine  ."+mSPSPPSFrame[i]);}



            } catch (IOException e1) {
                e1.printStackTrace();
            }
            sampleSize = sizeInBytes;
            index++;
            index++;

            mFormat = MediaFormat.createVideoFormat(MIME_TYPE, 1920,1080);
            mFormat.setByteBuffer("csd-0", ByteBuffer.wrap( mSPSPPSFrame,0, sizeInBytes));
            codec = MediaCodec.createDecoderByType(MIME_TYPE);

            codec.configure(mFormat, surface /*surface*/ , null /* crypto */, 0 /* flags */);
            codec.start();
            codec.getInputBuffers();
            codec.getOutputBuffers();

        }

        //  index = 0;
        while (!sawOutputEOS && errors < 0) {


            if (!sawInputEOS) {
                int inputBufIndex = codec.dequeueInputBuffer(kTimeOutUs);
                //Log.d(TAG, String.format("Archana Dqing the input buffer with BufIndex #: %d",inputBufIndex));


                if (inputBufIndex >= 0) {
                    ByteBuffer dstBuf = codec.getInputBuffers()[inputBufIndex];


                    /*
                     * Read data from file and copy to the input ByteBuffer
                     */
                    try {
                        sizeInBytes = videoFileStream.read(byteArray, 0,
                                tall[index] /*+ 4*/);
                        sampleSize = tall[index]/*+ 4*/;
                        index++;

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (sizeInBytes <= 0) {
                        codec.queueInputBuffer(
                                inputBufIndex,
                                0 /* offset */,
                                0,
                                presentationTimeUs,
                                MediaCodec.BUFFER_FLAG_END_OF_STREAM );
                        sawInputEOS = true;
                    }
                    else {
                        dstBuf.put(byteArray, 0, sizeInBytes);

                        if (mVideoStart == false) mVideoStart = true;

                        codec.queueInputBuffer(
                                inputBufIndex,
                                0 /* offset */,
                                sampleSize,
                                presentationTimeUs,
                                mVideoStart ? 0:MediaCodec.BUFFER_FLAG_CODEC_CONFIG );
                        //Log.d(TAG, String.format(" After queueing the buffer to decoder with inputbufindex and samplesize #: %d ,%d ind %d",inputBufIndex,sampleSize,index));
                    }
                }
            }

            int res = codec.dequeueOutputBuffer(info, kTimeOutUs);
            //Log.d(TAG, String.format(" Getting the information about decoded output buffer flags,offset,PT,size #: %d %d %d %d",info.flags,info.offset,info.presentationTimeUs,info.size));
            //Log.d(TAG, String.format(" Getting the output of decoder in res #: %d",res));

            if (res >= 0) {
                int outputBufIndex = res;

                //Log.d(TAG, "Output PTS "+info.presentationTimeUs);


                //mspeed.preRender(info.presentationTimeUs);
                //mspeed.setFixedPlaybackRate(25);

                codec.releaseOutputBuffer(outputBufIndex, true /* render */);
                //Log.d(TAG, String.format(" releaseoutputbuffer index= #: %d",outputBufIndex));


                if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
                    Log.d(TAG, "saw output EOS.");
                    sawOutputEOS = true;
                }

            } else if (res == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                codec.getOutputBuffers();
                Log.d(TAG, "output buffers have changed.");

            } else if (res == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                oformat = codec.getOutputFormat();
                Log.d(TAG, "output format has changed to " + oformat);
            }

        }
        codec.stop();
        codec.release();
        this.finish();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

上述样本测试有几种解决方法。

  • 我没有将一个Full Frame送到解码器Inout,而是一次喂一个NAL单元。但播放速度仍然很慢,无法与60FPS相匹配
  • Google已将Surface BufferQueue的实现从Asynchronous更改为Synchronous.Hence当我们调用MediaCodec.dequeueBuffer来获取解码数据时,服务器端(SurfaceTexture :: dequeueBuffer)将等待缓冲区排队,并且客户端等待,所以SurfaceTextureClient :: dequeueBuffer将不会返回,直到缓冲区实际上已在服务器端排队。在异步模式中,分配了一个新的GraphicBuffer。