封装解码器
这部分代码主要功能是对MediaCodec进行封装,实现MediaCodec初始化以及一些配置,并提供解码的h264视频帧的方法:
/**
* Created by ZhangHao on 2016/8/5.
* 用于硬件解码(MediaCodec)H264的工具类
*/
public class MediaCodecUtil {
Logger logger = Logger.getLogger();
private String TAG = "MediaCodecUtil";
private SurfaceHolder holder;
private int width, height;
private MediaCodec mCodec;
private boolean isFirst = true;
private final static String MIME_TYPE = "video/avc";
private final static int TIME_INTERNAL = 5;
/**
* 初始化解码器
*
* @param holder 用于显示视频的surface
* @param width surface宽
* @param height surface高
*/
public MediaCodecUtil(SurfaceHolder holder, int width, int height) {
this.holder = holder;
this.width = width;
this.height = height;
}
public MediaCodecUtil(SurfaceHolder holder) {
this(holder, holder.getSurfaceFrame().width(), holder.getSurfaceFrame().height());
}
public void startCodec() {
if (isFirst) {
initDecoder();
}
}
private void initDecoder() {
try {
mCodec = MediaCodec.createDecoderByType(MIME_TYPE);
} catch (IOException e) {
e.printStackTrace();
}
MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE,
width, height);
mCodec.configure(mediaFormat, holder.getSurface(), null, 0);
mCodec.start();
isFirst = false;
}
int mCount = 0;
public boolean onFrame(byte[] buf, int offset, int length) {
ByteBuffer[] inputBuffers = mCodec.getInputBuffers();
int inputBufferIndex = mCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(buf, offset, length);
mCodec.queueInputBuffer(inputBufferIndex, 0, length, mCount * TIME_INTERNAL, 0);
mCount++;
} else {
return false;
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mCodec.dequeueOutputBuffer(bufferInfo, 100);
while (outputBufferIndex >= 0) {
mCodec.releaseOutputBuffer(outputBufferIndex, true);
outputBufferIndex = mCodec.dequeueOutputBuffer(bufferInfo, 0);
}
if (outputBufferIndex < 0) {
}
return true;
}
/**
*停止解码,释放解码器
*/
public void stopCodec() {
try {
mCodec.stop();
mCodec.release();
mCodec = null;
isFirst = true;
} catch (Exception e) {
e.printStackTrace();
mCodec = null;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
读取文件线程
这部分代码的主要功能是利用线程去读取指定的h264文件,通过判断I帧或者P帧的帧头来读取每一帧的数据送入解码器进行解码,并根据帧率进行休眠。
/**
* Created by ZhangHao on 2017/5/5.
* 读取H264文件送入解码器解码线程
*/
public class MediaCodecThread extends Thread {
Logger logger = Logger.getLogger();
private MediaCodecUtil util;
private String path;
private boolean isFinish = false;
private int FRAME_MIN_LEN = 1024;
private static int FRAME_MAX_LEN = 300 * 1024;
private int PRE_FRAME_TIME = 1000 / 25;
/**
* 初始化解码器
*
* @param util 解码Util
* @param path 文件路径
*/
public MediaCodecThread(MediaCodecUtil util, String path) {
this.util = util;
this.path = path;
}
/**
* 寻找指定buffer中h264头的开始位置
*
* @param data 数据
* @param offset 偏移量
* @param max 需要检测的最大值
* @return h264头的开始位置 ,-1表示未发现
*/
private int findHead(byte[] data, int offset, int max) {
int i;
for (i = offset; i <= max; i++) {
if (isHead(data, i))
break;
}
if (i == max) {
i = -1;
}
return i;
}
/**
* 判断是否是I帧/P帧头:
* 00 00 00 01 65 (I帧)
* 00 00 00 01 61 / 41 (P帧)
*
* @param data
* @param offset
* @return 是否是帧头
*/
private boolean isHead(byte[] data, int offset) {
boolean result = false;
if (data[offset] == 0x00 && data[offset + 1] == 0x00
&& data[offset + 2] == 0x00 && data[3] == 0x01 && isVideoFrameHeadType(data[offset + 4])) {
result = true;
}
if (data[offset] == 0x00 && data[offset + 1] == 0x00
&& data[offset + 2] == 0x01 && isVideoFrameHeadType(data[offset + 3])) {
result = true;
}
return result;
}
/**
* I帧或者P帧
*/
private boolean isVideoFrameHeadType(byte head) {
return head == (byte) 0x65 || head == (byte) 0x61 || head == (byte) 0x41;
}
@Override
public void run() {
super.run();
File file = new File(path);
if (file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
byte[] frame = new byte[FRAME_MAX_LEN];
int frameLen = 0;
byte[] readData = new byte[10 * 1024];
long startTime = System.currentTimeMillis();
while (!isFinish) {
if (fis.available() > 0) {
int readLen = fis.read(readData);
if (frameLen + readLen < FRAME_MAX_LEN) {
System.arraycopy(readData, 0, frame, frameLen, readLen);
frameLen += readLen;
int headFirstIndex = findHead(frame, 0, frameLen);
while (headFirstIndex >= 0 && isHead(frame, headFirstIndex)) {
int headSecondIndex = findHead(frame, headFirstIndex + FRAME_MIN_LEN, frameLen);
if (headSecondIndex > 0 && isHead(frame, headSecondIndex)) {
logger.e("headSecondIndex:" + headSecondIndex);
onFrame(frame, headFirstIndex, headSecondIndex - headFirstIndex);
byte[] temp = Arrays.copyOfRange(frame, headSecondIndex, frameLen);
System.arraycopy(temp, 0, frame, 0, temp.length);
frameLen = temp.length;
sleepThread(startTime, System.currentTimeMillis());
startTime = System.currentTimeMillis();
headFirstIndex = findHead(frame, 0, frameLen);
} else {
headFirstIndex = -1;
}
}
} else {
frameLen = 0;
}
} else {
isFinish = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
logger.e("File not found");
}
}
private void onFrame(byte[] frame, int offset, int length) {
if (util != null) {
try {
util.onFrame(frame, offset, length);
} catch (Exception e) {
e.printStackTrace();
}
} else {
logger.e("mediaCodecUtil is NULL");
}
}
private void sleepThread(long startTime, long endTime) {
long time = PRE_FRAME_TIME - (endTime - startTime);
if (time > 0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stopThread() {
isFinish = true;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
Activity
这里的代码就是在activity中对一个h264文件进行解码播放,布局文件只有一个surfaceView和一个Button,我就不贴出来了,下面的是activity的代码:
public class H264FileDecodeActivity extends AppCompatActivity {
@Bind(R.id.test_surface_view)
SurfaceView testSurfaceView;
private SurfaceHolder holder;
private MediaCodecUtil codecUtil;
private MediaCodecThread thread;
private String path = Environment.getExternalStorageDirectory().toString() + "/SONA/test.h264";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_h264_file_decodec);
ButterKnife.bind(this);
initSurface();
}
private void initSurface() {
holder = testSurfaceView.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (codecUtil == null) {
codecUtil = new MediaCodecUtil(holder);
codecUtil.startCodec();
}
if (thread == null) {
thread = new MediaCodecThread(codecUtil, path);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (codecUtil != null) {
codecUtil.stopCodec();
codecUtil = null;
}
if (thread != null && thread.isAlive()) {
thread.stopThread();
thread = null;
}
}
});
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_h264_file:
if (thread != null) {
thread.start();
}
break;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
结语
因为文字功底太差,所以主要以贴代码为主,有什么大家疑问可以给我留言。接下来有空的话,我会简单介绍如何利用MediaCodec将AAC解码成PCM并利用AudioTrack进行播放。