Anroid camera + mediacodec

本文介绍如何在Android中结合相机和mediacodec进行视频采集和编码。通过生成OES纹理,设置SurfaceTexture回调,将SurfaceTexture应用到相机并监听帧更新,然后利用OpenGL ES 2.0绘制并编码视频流。详细代码参考链接提供。
摘要由CSDN通过智能技术生成

具体代码参考:https://github.com/google/grafika/tree/master/src/com/android/grafika

主要流程:

1、生成一个oes textrure

2、通过上面生成的oes  texture,new一个surfacetexture,设置回调surfacetexture.setOnFrameAvailableListener(CB);

3、将surfacetexture设置给 camera,开始采集

4、camera数据有更新会通过第二步会调通知

5、有数据回调是通过第一步的texture绘制

6、如果需要编码,则创建编码器,注意format

format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
                MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);

7、得到编码器的surface

mInputSurface = mEncoder.createInputSurface();

8、在7步的surface上建立EGL环境

9、camera画面更新时,通过OpenGL es2.0绘制一下,绘制代码如下:画完编码。

package com.android.grafika.gles;

import android.opengl.GLES11Ext;
import android.opengl.GLES20;
import android.util.Log;

import java.nio.FloatBuffer;

/**
 * GL program and supporting functions for textured 2D shapes.
 */
public class Texture2dProgram {
    private static final String TAG = GlUtil.TAG;

    public enum ProgramType {
        TEXTURE_2D, TEXTURE_EXT, TEXTURE_EXT_BW, TEXTURE_EXT_FILT
    }

    // Simple vertex shader, used for all programs.
    private static final String VERTEX_SHADER =
            "uniform mat4 uMVPMatrix;\n" +
            "uniform mat4 uTexMatrix;\n" +
            "attribute vec4 aPosition;\n" +
            "attribute vec4 aTextureCoord;\n" +
            "varying vec2 vTextureCoord;\n" +
            "void main() {\n" +
            "    gl_Position = uMVPMatrix * aPosition;\n" +
            "    vTextureCoord = (uTexMatrix * aTextureCoord).xy;\n" +
            "}\n";

    // Simple fragment shader for use with "normal" 2D textures.
    private static final String FRAGMENT_SHADER_2D =
            "precision mediump float;\n" +
            "varying vec2 vTextureCoord;\n" +
            "uniform sampler2D sTexture;\n" +
            "void main() {\n" +
            "    gl_FragColor = texture2D(sText
android glsurfaceview +camera+mediaCodec编码中,可以通过设置MediaCodec的参数来自定义编码帧率,具体步骤如下: 1. 创建MediaCodec编码器 ```java MediaCodec mediaCodec = MediaCodec.createEncoderByType(mimeType); ``` 2. 配置MediaCodec参数 ```java MediaFormat mediaFormat = MediaFormat.createVideoFormat(mimeType, width, height); mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitRate); mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, frameRate); // 设置帧率 mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, colorFormat); mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, iFrameInterval); mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); ``` 其中,frameRate参数即为要设置的帧率值,可以在0-30之间自定义。 3. 启动MediaCodec编码器 ```java mediaCodec.start(); ``` 4. 循环获取相机预览数据并进行编码 ```java while (mIsRecording) { // 获取相机预览数据 byte[] input = getCameraData(); // 将获取到的数据传入MediaCodec进行编码 int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1); if (inputBufferIndex >= 0) { ByteBuffer inputBuffer = inputBuffers[inputBufferIndex]; inputBuffer.clear(); inputBuffer.put(input); mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, System.nanoTime() / 1000, 0); } // 获取编码后的数据 int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US); if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { // 获取到编码后的格式信息,可进行相关初始化操作 MediaFormat newFormat = mediaCodec.getOutputFormat(); // ... } else if (outputBufferIndex >= 0) { ByteBuffer outputBuffer = outputBuffers[outputBufferIndex]; byte[] output = new byte[bufferInfo.size]; outputBuffer.get(output); // 处理编码后的数据 // ... mediaCodec.releaseOutputBuffer(outputBufferIndex, false); } } ``` 通过以上步骤,即可在不影响预览的情况下自定义编码帧率。需要注意的是,帧率的设置只是建议值,并不一定能够达到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值