Reputation: 31
I configure the MediaCodec with this
mediaCodec = MediaCodec.createEncoderByType(MIME_TYPE);
MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE,
width, height);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate); //
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, framerate); // frame rate
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, iFramerate);
mediaCodec.configure(mediaFormat, null, null,
MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
and send the data with Called from Camera.onPreviewFrame(byte[] data, Camera camera)
try {
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
if (inputBuffer.capacity() < input.length) {
byte[] temp = new byte[input.length];
System.arraycopy(input, 0, temp, 0, temp.length);
inputBuffer.put(temp);
} else {
inputBuffer.put(input);
}
mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length,
0, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo,
0);
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
The input(byte[]) comes from the camera preview. And I follow the code in API-DOC . But when i running this code, i can only get one frame succeed. Which means outputBufferIndex get >=0 only one time whatever how long it's running. where is the problem?
Upvotes: 2
Views: 1484
Reputation: 41
for some encoders,when you encode video frames you should add timestamp.
mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length,0, 0);
for some encoders this func should change to:
mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length,(long)ptsUsec, 0);
note:"ptsUsec" should be different every time.you can creat is like this: long ptsUsec = (long) generateIndex * 1000000 / VideoConfig.VIDEO_FPS; (English is poor...)
Upvotes: 4