目录
难点1:关于编解码器sps/pps参数的处理
1.未正确设置 SPS/PPS 导致的典型解码器初始化失败
如果编码器没有对关键帧做特殊处理,并且解码器没有设置sps/pps参数,就会报以下错误:
2025-05-30 15:40:17.766 25394-25488 CCodec com.shukun.appmain D Component "c2.mtk.avc.decoder" returned error: 0xe
2025-05-30 15:40:17.767 25394-25525 MediaCodec com.shukun.appmain E Codec reported err 0x80000000/UNKNOWN_ERROR, actionCode 0, while in state 6/STARTED
2025-05-30 15:40:17.767 25394-25525 MediaCodec com.shukun.appmain E Pending dequeue output buffer request cancelled
2025-05-30 15:40:17.767 25394-25525 MediaCodec com.shukun.appmain D flushMediametrics
2025-05-30 15:40:17.769 25394-25525 MediaCodec com.shukun.appmain D [0xb40000727e9b1aa0] setState: 0
如果此时解码器正在解码,则还会报错,最终进程会被干掉:
2025-05-30 15:40:17.777 25394-25516 AndroidRuntime com.shukun.appmain E FATAL EXCEPTION: Thread-5
Process: com.shukun.appmain, PID: 25394
java.lang.IllegalStateException: Pending dequeue output buffer request cancelled
at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:3741)
at com.shukun.screen_recorder.android14plus.ScreenDecoder.run(ScreenDecoder.kt:158)
2025-05-30 15:40:17.781 25394-25517 ScreenEncoder com.shukun.appmain D outPutBufferId: 0
2025-05-30 15:40:17.782 25394-25516 System.err com.shukun.appmain W java.lang.IllegalStateException: Pending dequeue output buffer request cancelled
2025-05-30 15:40:17.783 25394-25525 MediaCodec com.shukun.appmain E Invalid to call at Released state; only valid in executing state
2025-05-30 15:40:17.783 25394-25525 AMessage com.shukun.appmain E trying to post a duplicate reply
2025-05-30 15:40:17.783 25394-25516 System.err com.shukun.appmain W at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
2025-05-30 15:40:17.783 25394-25516 System.err com.shukun.appmain W at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:3741)
2025-05-30 15:40:17.784 25394-25516 System.err com.shukun.appmain W at com.shukun.screen_recorder.android14plus.ScreenDecoder.run(ScreenDecoder.kt:158)
2025-05-30 15:40:17.790 25394-25517 System.err com.shukun.appmain W java.lang.IllegalStateException: Invalid to call at Released state; only valid in executing state
2025-05-30 15:40:17.791 25394-25517 System.err com.shukun.appmain W at android.media.MediaCodec.native_dequeueInputBuffer(Native Method)
2025-05-30 15:40:17.792 25394-25517 System.err com.shukun.appmain W at android.media.MediaCodec.dequeueInputBuffer(MediaCodec.java:3139)
2025-05-30 15:40:17.792 25394-25517 System.err com.shukun.appmain W at com.shukun.screen_recorder.android14plus.ScreenDecoder.decode(ScreenDecoder.kt:118)
2025-05-30 15:40:17.793 25394-25517 System.err com.shukun.appmain W at com.shukun.screen_recorder.android14plus.ScreenEncoder.handleRegularFrame(ScreenEncoder.kt:332)
2025-05-30 15:40:17.794 25394-25517 System.err com.shukun.appmain W at com.shukun.screen_recorder.android14plus.ScreenEncoder.encodeData(ScreenEncoder.kt:299)
2025-05-30 15:40:17.795 25394-25517 System.err com.shukun.appmain W at com.shukun.screen_recorder.android14plus.ScreenEncoder.run(ScreenEncoder.kt:247)
2025-05-30 15:40:17.795 25394-25517 Process com.shukun.appmain I Sending signal. PID: 25394 SIG: 9
2.如何正确设置SPS/PPS
1)获取sps/pps元数据
编码时通过 (bufferInfo.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0
拷贝出sps/pps元数据
2)设置sps/pps元数据
分离出sps/pps数据,设置给解码器:
mediaFormat.setByteBuffer("csd-0", spsBuffer) // 纯SPS
mediaFormat.setByteBuffer("csd-1", ppsBuffer) // 纯PPS
还有另外两种不太规范的设置sps/pps方法:
1)编码时直接将sps/pps合并到关键帧的前面
2)将sps/pps一起设置给解码器的"csd-0"键,"csd-1"就不用设置了。
0 条评论