public
Last active

MediaCodec encoder sample

  • Download Gist
AvcEncoder.java
Java
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
package com.opentok.media.avc;
 
import java.io.IOException;
import java.nio.ByteBuffer;
 
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
 
public class AvcEncoder {
 
private EncodedFrameListener frameListener;
private MediaCodec mediaCodec;
private byte[] sps;
private byte[] pps;
private ParameterSetsListener parameterSetsListener;
 
public AvcEncoder() {
mediaCodec = MediaCodec.createEncoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
}
 
@Override
public void close() throws IOException {
mediaCodec.stop();
mediaCodec.release();
}
 
@Override
public void offerEncoder(byte[] input) {
try {
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
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];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
if (sps != null && pps != null) {
ByteBuffer frameBuffer = ByteBuffer.wrap(outData);
frameBuffer.putInt(bufferInfo.size - 4);
frameListener.frameReceived(outData, 0, outData.length);
} else {
ByteBuffer spsPpsBuffer = ByteBuffer.wrap(outData);
if (spsPpsBuffer.getInt() == 0x00000001) {
System.out.println("parsing sps/pps");
} else {
System.out.println("something is amiss?");
}
int ppsIndex = 0;
while(!(spsPpsBuffer.get() == 0x00 && spsPpsBuffer.get() == 0x00 && spsPpsBuffer.get() == 0x00 && spsPpsBuffer.get() == 0x01)) {
 
}
ppsIndex = spsPpsBuffer.position();
sps = new byte[ppsIndex - 8];
System.arraycopy(outData, 4, sps, 0, sps.length);
pps = new byte[outData.length - ppsIndex];
System.arraycopy(outData, ppsIndex, pps, 0, pps.length);
if (null != parameterSetsListener) {
parameterSetsListener.avcParametersSetsEstablished(sps, pps);
}
}
mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
} catch (Throwable t) {
t.printStackTrace();
}
 
}
}

Hello!
what is this:
EncodedFrameListener frameListener;
and te
ParameterSetsListener parameterSetsListener?

I amconfused on EncodedFrameListener and ParameterSetsListener, is it the private Listener which is not provided by the Android SDK?

It seems, author means custom class

public class EncodedFrameListener {
    public void frameReceived(byte[] data, int pos, int length) {

    }
}

http://dev.naver.com/projects/nextcinema/code/MediaCodecSample/src/com/TestGroup/mediacodecsample/view/EncodedFrameListener.java?branch=develop

Hi npakudin,
This page is in Korean. Can you please give more details about EncodedFrameListener. Thanks in advance.

I found this example very useful, thanks wobbals!

EncodedFrameListener is just a listener for when a new frame has been encoded. In my case I would just open the file being written to and write in the new frame information.

ParameterSetsListener is just a listener that updates some parameters.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.