微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决视频编解码器在 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 举报,一经查实,本站将立刻删除。