视频编解码器在 val outindex =decoder.dequeueoutputbuffer(bufferinfo, 10000) 处引发非法状态异常

问题描述

i have been trying to stream through rtsp and sometimes the video decode thread throws illegal state exception and it gets stuck in the loop and eventually the app crashes,I have been thinking of the reason but I Could not find why this happens,any idea what is happening
I am new to Media Codecs library and don't understand it 

共享的代码是alexvas-rtsp-demo-decode-videodecodethread 我添加了一些 try 和 catch 块 只是为了了解应用程序似乎在哪里崩溃 使用此应用进行流式传输 - https://github.com/alexeyvasilyev/rtsp-client-android

Sharing The Code Now 
package com.alexvas.rtsp.demo.decode

import android.media.MediaCodec
import android.media.MediaFormat
import android.util.Log
import android.view.Surface
import java.nio.ByteBuffer

class VideoDecodeThread (
        private val surface: Surface,private val mimeType: String,private val width: Int,private val height: Int,private val videoFrameQueue: FrameQueue) : Thread() {

    private val TAG: String = VideoDecodeThread::class.java.simpleName
    private val DEBUG = true

    override fun run() {
        if (DEBUG) Log.d(TAG,"VideoDecodeThread started")

        val decoder = MediaCodec.createDecoderByType(mimeType)
        val format = MediaFormat.createVideoFormat(mimeType,width,height)

        decoder.configure(format,surface,null,0)
        decoder.start()

        val bufferInfo = MediaCodec.BufferInfo()
        while (!interrupted()) {
            val inIndex: Int = decoder.dequeueInputBuffer(10000L)
            if (inIndex >= 0) {
                // fill inputBuffers[inputBufferIndex] with valid data
                val byteBuffer: ByteBuffer? = decoder.getInputBuffer(inIndex)
                byteBuffer?.rewind()
//                 byteBuffer?.limit(3000)

                // Preventing BufferOverflowException
//              if (length > byteBuffer.limit()) throw DecoderFatalException("Error")

                val frame: FrameQueue.Frame?
                try {
                    frame = videoFrameQueue.pop()
                    if (frame == null) {
                        Log.d(TAG,"Empty frame")
                    } else {
                        byteBuffer!!.put(frame.data,frame.offset,frame.length)
                        decoder.queueInputBuffer(inIndex,frame.length,frame.timestamp,0)
                    }
                } catch (e: Exception) {
                    e.printstacktrace()
                }
            }

            try {
           --(Keep Getting Illegal error on this Line) -->    val outIndex = decoder.dequeueOutputBuffer(bufferInfo,10000)
                when (outIndex) {
                    MediaCodec.INFO_OUTPUT_FORMAT_CHANGED -> Log.d(TAG,"Decoder format changed: " + decoder.outputFormat)
                    MediaCodec.INFO_TRY_AGAIN_LATER -> if (DEBUG) Log.d(TAG,"No output from decoder available")
                    else -> {
                        if (outIndex >= 0) {
                            decoder.releaSEOutputBuffer(outIndex,bufferInfo.size != 0)
                        }
                    }
                }
            } catch (e: java.lang.Exception) {
                e.printstacktrace()
            }

            // All decoded frames have been rendered,we can stop playing Now
            if (bufferInfo.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0) {
                Log.d(TAG,"OutputBuffer BUFFER_FLAG_END_OF_STREAM")
                break
            }
        }

        decoder.stop()
        decoder.release()
        videoFrameQueue.clear()

        if (DEBUG) Log.d(TAG,"VideoDecodeThread stopped")
    }
}

我希望这会有所帮助 如果有人知道可以完美地传输 RTSP 流而没有延迟的应用程序 请告诉我

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)