|
|
|
@ -10,7 +10,7 @@ import android.view.Surface; |
|
|
|
|
import java.nio.ByteBuffer; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 使用MediaExtractor抽帧,MediaCodec解码,然后渲染到Surface |
|
|
|
|
* Extract by MediaExtractor, decode by MediaCodec, and render to Surface |
|
|
|
|
* Created by frank on 2019/11/16. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
@ -105,7 +105,8 @@ public class HardwareDecode { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据原分辨率大小动态设置预览分辨率 |
|
|
|
|
* setting the preview resolution according to video aspect ratio |
|
|
|
|
* |
|
|
|
|
* @param mediaFormat mediaFormat |
|
|
|
|
*/ |
|
|
|
|
private void setPreviewRatio(MediaFormat mediaFormat) { |
|
|
|
@ -160,11 +161,11 @@ public class HardwareDecode { |
|
|
|
|
} |
|
|
|
|
Log.i(TAG, "width=" + width + "--height=" + height + "--duration==" + duration); |
|
|
|
|
|
|
|
|
|
//重新设置预览分辨率
|
|
|
|
|
//setting preview resolution
|
|
|
|
|
setPreviewRatio(mediaFormat); |
|
|
|
|
Log.i(TAG, "mediaFormat=" + mediaFormat.toString()); |
|
|
|
|
|
|
|
|
|
//配置MediaCodec,并且start
|
|
|
|
|
//config MediaCodec, and start
|
|
|
|
|
mediaCodec = MediaCodec.createDecoderByType(mimeType); |
|
|
|
|
mediaCodec.configure(mediaFormat, mSurface, null, 0); |
|
|
|
|
mediaCodec.start(); |
|
|
|
@ -176,12 +177,12 @@ public class HardwareDecode { |
|
|
|
|
SystemClock.sleep(SLEEP_TIME); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
//从缓冲区取出一个缓冲块,如果当前无可用缓冲块,返回inputIndex<0
|
|
|
|
|
//dequeue from input buffer
|
|
|
|
|
int inputIndex = mediaCodec.dequeueInputBuffer(DEQUEUE_TIME); |
|
|
|
|
if (inputIndex >= 0) { |
|
|
|
|
ByteBuffer inputBuffer = inputBuffers[inputIndex]; |
|
|
|
|
int sampleSize = mediaExtractor.readSampleData(inputBuffer, 0); |
|
|
|
|
//入队列
|
|
|
|
|
//enqueue to input buffer
|
|
|
|
|
if (sampleSize < 0) { |
|
|
|
|
mediaCodec.queueInputBuffer(inputIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM); |
|
|
|
|
} else { |
|
|
|
@ -190,7 +191,7 @@ public class HardwareDecode { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//出队列
|
|
|
|
|
//dequeue from output buffer
|
|
|
|
|
int outputIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, DEQUEUE_TIME); |
|
|
|
|
switch (outputIndex) { |
|
|
|
|
case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED: |
|
|
|
@ -203,7 +204,7 @@ public class HardwareDecode { |
|
|
|
|
Log.i(TAG, "output buffer changed..."); |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
//渲染到surface
|
|
|
|
|
//render to surface
|
|
|
|
|
mediaCodec.releaseOutputBuffer(outputIndex, true); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|