@@ -63,6 +63,10 @@ private void initVideo(int width, int height, Surface surface) throws IOExceptio
6363 mFormat .setInteger (MediaFormat .KEY_ROTATION , 0 );
6464 mFormat .setInteger (MediaFormat .KEY_PROFILE , MediaCodecInfo .CodecProfileLevel .AVCProfileBaseline );
6565 mFormat .setInteger (MediaFormat .KEY_LEVEL , MediaCodecInfo .CodecProfileLevel .AVCLevel31 );
66+ mFormat .setInteger (MediaFormat .KEY_PRIORITY , 0 ); // 设置低优先级
67+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
68+ mFormat .setInteger (MediaFormat .KEY_LOW_LATENCY , 1 ); // 启用低延迟模式
69+ }
6670 String model = Build .MODEL ;
6771 if (model .contains ("KONKA" ) && model .contains ("9652" ) || model .contains ("KONKA" ) && model .contains ("9653" ) || model .contains ("XY01" )) { // 康佳 MTK的一个SoC 型号
6872 mFormat .setInteger ("low-latency" , 1 );
@@ -88,22 +92,22 @@ public int decoderH264(byte[] data, int len, long pts) {
8892 try {
8993 ByteBuffer [] inputBuffers = mVideoCodec .getInputBuffers ();
9094 // queue and decode
91- int inputBufferIndex = mVideoCodec .dequeueInputBuffer (- 1 );
95+ int inputBufferIndex = mVideoCodec .dequeueInputBuffer (10000 );
9296 if (inputBufferIndex >= 0 ) {
9397 ByteBuffer inputBuffer = inputBuffers [inputBufferIndex ];
9498 inputBuffer .clear ();
9599 inputBuffer .put (data , 0 , len );
96- mVideoCodec .queueInputBuffer (inputBufferIndex , 0 , len , 0 , 0 );
100+ mVideoCodec .queueInputBuffer (inputBufferIndex , 0 , len , pts * 1000 , 0 );
97101 } else {
98102 Log .e (TAG , "video inputBufferIndex invalid: " + inputBufferIndex );
99103 }
100104
101105 // dequeue and render
102106 MediaCodec .BufferInfo bufferInfo = new MediaCodec .BufferInfo ();
103- int outputBufferIndex = mVideoCodec .dequeueOutputBuffer (bufferInfo , 0 );
107+ int outputBufferIndex = mVideoCodec .dequeueOutputBuffer (bufferInfo , 10000 );
104108 while (outputBufferIndex >= 0 ) {
105109 mVideoCodec .releaseOutputBuffer (outputBufferIndex , true );
106- outputBufferIndex = mVideoCodec .dequeueOutputBuffer (bufferInfo , 0 );
110+ outputBufferIndex = mVideoCodec .dequeueOutputBuffer (bufferInfo , 10000 );
107111 }
108112 } catch (Throwable t ) {
109113 t .printStackTrace ();
0 commit comments