EncodeAndMuxTest.java:绘制纹理时带黑屏的视频输出

时间:2014-09-29 17:32:51

标签: android opengl-es-2.0 mediacodec

我正在使用此示例EncodeAndMuxTest.java对视频进行编码。

而不是通过GLES20.glScissor()生成表面帧,而是绘制纹理(通过GLES20.glActiveTexture( ),GLES20.glBindTexture(),GLES20.glDrawElements())。但输出视频总是有黑屏 我认为输出视频只在GLES20.glClear()中显示颜色,并且它不显示图像纹理。我想在使用EGL14.eglSwapBuffers时纹理有些问题,但我无法弄明白。

请帮助我解决我的问题。非常感谢!

更新代码使用例如

public void testEncodeVideoToMp4() {
    // QVGA at 2Mbps
    mWidth = 320;
    mHeight = 240;
    mBitRate = 2000000;

    try {
        prepareEncoder();
        setupTexture();     // insert new function to setup opengles
        mInputSurface.makeCurrent();
        for (int i = 0; i < NUM_FRAMES; i++) {
        drainEncoder(false);
        // Generate a new frame of input.
        // generateSurfaceFrame(i);
        drawTexture();  // insert new function to draw texture
        mInputSurface.setPresentationTime(computePresentationTimeNsec(i));
        if (VERBOSE)
            Log.d(TAG, "sending frame " + i + " to encoder");
        mInputSurface.swapBuffers();
        }
    drainEncoder(true);
    } finally {
        releaseEncoder();
    }
}

setupTexture()函数:

    public void setupTexture() {

    mVertices = ByteBuffer.allocateDirect(mVerticesData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    mVertices.put(mVerticesData).position(0);
    mIndices = ByteBuffer.allocateDirect(mIndicesData.length * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
    mIndices.put(mIndicesData).position(0);

    String vShaderStr = "attribute vec4 a_position;   \n"
            + "attribute vec2 a_texCoord;   \n"
            + "varying vec2 v_texCoord;     \n"
            + "void main()                  \n"
            + "{                            \n"
            + "   gl_Position = a_position; \n"
            + "   v_texCoord = a_texCoord;  \n"
            + "}                            \n";

    String fShaderStr = "precision mediump float;                            \n"
            + "varying vec2 v_texCoord;                            \n"
            + "uniform sampler2D s_baseMap;                        \n"
            + "void main()                                         \n"
            + "{                                                   \n"
            + "  gl_FragColor = texture2D( s_baseMap, v_texCoord );   \n"
            + "}                                                   \n";

    // Load the shaders and get a linked program object
    mProgramObject = ESShader.loadProgram(vShaderStr, fShaderStr);

    // Get the attribute locations
    mPositionLoc = GLES20.glGetAttribLocation(mProgramObject, "a_position");
    mTexCoordLoc = GLES20.glGetAttribLocation(mProgramObject, "a_texCoord");

    // Get the sampler locations
    mBaseMapLoc = GLES20.glGetUniformLocation(mProgramObject, "s_baseMap");

    // Load the texture
    mBaseMapTexId = loadTexture();

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GLES20.glViewport(0, 0, 720, 1280);
}

loadTexture()函数

private int loadTexture() {
int[] textureId = new int[1];
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(mFilePath);   // please change mFilePath to image file in sdcard
if (bitmap == null) {
    return -1;
}

GLES20.glGenTextures(1, textureId, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
bitmap.recycle();
return textureId[0];

}

drawTexture()函数:

    public void drawTexture() {

    GLES20.glClearColor(TEST_R0 / 255.0f, TEST_G0 / 255.0f, TEST_B0 / 255.0f, 1.0f); // green color
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Use the program object
    GLES20.glUseProgram(mProgramObject);

    // Load the vertex position
    mVertices.position(0);
    GLES20.glVertexAttribPointer(mPositionLoc, 3, GLES20.GL_FLOAT, false, 5 * 4, mVertices);
    // Load the texture coordinate
    mVertices.position(3);
    GLES20.glVertexAttribPointer(mTexCoordLoc, 2, GLES20.GL_FLOAT, false, 5 * 4, mVertices);

    GLES20.glEnableVertexAttribArray(mPositionLoc);
    GLES20.glEnableVertexAttribArray(mTexCoordLoc);

    // Bind the base map
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mBaseMapTexId);

    // Set the base map sampler to texture unit to 0
    GLES20.glUniform1i(mBaseMapLoc, 0);

    GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mIndices);
}

用于openGL ES的变量

    // Handle to a program object
private int mProgramObject;

// Attribute locations
private int mPositionLoc;
private int mTexCoordLoc;

// Sampler location
private int mBaseMapLoc;
// Texture handle
private int mBaseMapTexId;

// Additional member variables
private FloatBuffer mVertices;
private ShortBuffer mIndices;

private final float[] mVerticesData = { -1f, 1f, 0.0f, // Position 0
        0.0f, 0.0f, // TexCoord 0
        -1f, -1f, 0.0f, // Position 1
        0.0f, 1.0f, // TexCoord 1
        1f, -1f, 0.0f, // Position 2
        1.0f, 1.0f, // TexCoord 2
        1f, 1f, 0.0f, // Position 3
        1.0f, 0.0f // TexCoord 3
};

private final short[] mIndicesData = { 0, 1, 2, 0, 2, 3 };

视频输出仅显示green screen

0 个答案:

没有答案