IllegalStateException

image.png

記錄利用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層回調(diào),data參數(shù)就是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表現(xiàn)正常,Galaxy S6在執(zhí)行到int intputBufferIndex = mediaCodec.dequeueInputBuffer(10);這行代碼的時候拋出了上面的異常。

后來看了下系統(tǒng)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;
    }

測試的時候是正常的,那出現(xiàn)異常的原因就是對MediaFormat的初始化了,對比之后發(fā)現(xiàn)出現(xiàn)問題的地方應該是csd-0和csd-1的值的獲取

這個軌道數(shù)有時候得到的是0

int nTracks = mExtractor.getTrackCount();
比如這個地址:http://pl3.live.panda.tv/live_panda/dd9f182bcec99d04099113e618cfc5b3_mid.flv?sign=ce033518ab3f12dca896e414c42fd4a6&time=&ts=5bea339e&rid=-56292951

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容