I am doing RTSP streaming from Android. I have configured my MediaCodec as follows:
MediaCodecInfo codecInfo = selectCodec(MIME_TYPE);
int mBitrate = (int) ((mHeight * mWidth * frameRate)* 2 * 0.07);
MediaFormat mediaFormat = MediaFormat.createVideoFormat(MIME_TYPE, mWidth,mHeight);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, mBitrate);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,colorFormat);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL,1);
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
try{
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
}catch(IllegalArgumentException e)
{
e.printStackTrace();
}catch (IllegalStateException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
Then I am encoding the image bytes that I got from the Camera2api. Code as below:
public byte[] 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, (System.currentTimeMillis()) * 1000, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
//codecOutputBuffers = codec.getOutputBuffers();
Log.i(TAG, "encoder output buffer changed: ");
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
}
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
if (spsPpsInfo == null) {
ByteBuffer spsPpsBuffer = ByteBuffer.wrap(outData);
if (spsPpsBuffer.getInt() == 0x00000001) {
spsPpsInfo = new byte[outData.length];
System.arraycopy(outData, 0, spsPpsInfo, 0, outData.length);
mVideoPacketHeader.sendVideoSPP(spsPpsInfo);
firstFrame = true;
} else {
return null;
}
} else {
outputStreamData.write(outData);
}
if (Camera2Service.startStream && firstFrame) {
mRtspStreamer.h264Stream(outData, outData.length);
}
}
mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
I am getting the SPS and PPS info from mediacodec. I am not getting the fps displayed on VLC. The sps info I am getting is of 29 bytes. I can analyze the vui parameter in which the timing_info_present_flag is 0. How can I configure the MediaCodec such that it sets the flag to 1 with sps pps?
User contributions licensed under CC BY-SA 3.0