Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
MediaCodec encoder sample
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();
}
}
}
@GulaSoft

This comment has been minimized.

Show comment Hide comment
@GulaSoft

GulaSoft Apr 15, 2013

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

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

@XimingCheng

This comment has been minimized.

Show comment Hide comment
@XimingCheng

XimingCheng May 2, 2013

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

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

@npakudin

This comment has been minimized.

Show comment Hide comment
@npakudin

npakudin Jun 17, 2013

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

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

@poojalikop

This comment has been minimized.

Show comment Hide comment
@poojalikop

poojalikop Aug 14, 2013

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

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

@hpp

This comment has been minimized.

Show comment Hide comment
@hpp

hpp Aug 26, 2013

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.

hpp commented Aug 26, 2013

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.

@Bmackenzie-Intel

This comment has been minimized.

Show comment Hide comment
@Bmackenzie-Intel

Bmackenzie-Intel Oct 16, 2015

Super old thread but I am having bit of an issue. when offering the data I get the following error:
Attempt to invoke virtual method 'java.nio.ByteBuffer[] android.media.MediaCodec.getInputBuffers()' on a null object reference

This is the line that produces the error:
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();

However, I have checked and recheck...the MediaCodec object is not null. Any assistance is appreciated.

Super old thread but I am having bit of an issue. when offering the data I get the following error:
Attempt to invoke virtual method 'java.nio.ByteBuffer[] android.media.MediaCodec.getInputBuffers()' on a null object reference

This is the line that produces the error:
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();

However, I have checked and recheck...the MediaCodec object is not null. Any assistance is appreciated.

@leye0

This comment has been minimized.

Show comment Hide comment
@leye0

leye0 Oct 18, 2015

How is your MediaCodec object created? The part where you are doing MediaCodec.Create(encoder/decoder)By(type/codecName), and after that? Do you configure it? Do you see errors in the log when executing it line by line?

leye0 commented Oct 18, 2015

How is your MediaCodec object created? The part where you are doing MediaCodec.Create(encoder/decoder)By(type/codecName), and after that? Do you configure it? Do you see errors in the log when executing it line by line?

@Bmackenzie-Intel

This comment has been minimized.

Show comment Hide comment
@Bmackenzie-Intel

Bmackenzie-Intel Oct 19, 2015

Thanks for the reply, it's verbatim from the above code with a few more try/catch attempts. It seems that the mediaCodec object is not being created, but there are no errors thrown during it's creation, no errors during configuration. Here is the entire file.

http://pastebin.com/raw.php?i=fm2a02M3

I really appreciate the assist.

Thanks for the reply, it's verbatim from the above code with a few more try/catch attempts. It seems that the mediaCodec object is not being created, but there are no errors thrown during it's creation, no errors during configuration. Here is the entire file.

http://pastebin.com/raw.php?i=fm2a02M3

I really appreciate the assist.

@sulaimansust

This comment has been minimized.

Show comment Hide comment
@sulaimansust

sulaimansust Dec 10, 2016

Can anyone tell me the detail use of this encoder? Actually i can't fully understand it. Do i send the byte data received each call on
new Camera.PreviewCallback() {
@override
public void onPreviewFrame(byte[] data, Camera camera) {
Constants.debugLog(TAG, "data ===" + data.length);
}
to the encoder each time? or all the byte at a single time? How do i save the file?

Can anyone tell me the detail use of this encoder? Actually i can't fully understand it. Do i send the byte data received each call on
new Camera.PreviewCallback() {
@override
public void onPreviewFrame(byte[] data, Camera camera) {
Constants.debugLog(TAG, "data ===" + data.length);
}
to the encoder each time? or all the byte at a single time? How do i save the file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment