用于SPS和PPS H264编码器的MediaCodec配置

时间:2018-02-28 07:16:27

标签: android rtsp frame-rate mediacodec

我正在从Android进行RTSP流媒体播放。我已经按如下方式配置了我的MediaCodec:

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();
    }

然后我编码从Camera2api获得的图像字节。代码如下:

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);

        }

我从mediacodec获得SPS和PPS信息。我没有在VLC上显示fps。我得到的sps信息是29个字节。我可以分析timer_info_present_flag为0的vui参数。如何配置MediaCodec,使其用sps pps将标志设置为1?

0 个答案:

没有答案