【发布时间】:2019-08-17 04:55:02
【问题描述】:
我正在尝试使用 MediaCodec 和 Surfaces 对电影进行编码(像素缓冲模式有效,但性能不够好)。但是,每次我尝试调用eglSwapBuffers() 时,它都会以EGL_BAD_SURFACE 失败,因此dequeueOutputBuffer() 总是返回-1 (INFO_TRY_AGAIN_LATER)
我已经看过 Bigflake 和 Grafika 上的示例,并且我有另一个工作项目,一切正常,但我需要在另一个稍微不同的设置中让它工作。
我目前有一个 GLSurfaceView,它进行屏幕渲染,并提供了一个自定义 EGLContextFactory/EGLConfigChooser。这允许我创建共享上下文以用于在本机库中进行单独的 OpenGL 渲染。这些是使用 EGL10 创建的,但这应该不是问题,因为据我所知,底层上下文只关心客户端版本。
我已使用以下配置确保上下文是可记录的:
private android.opengl.EGLConfig chooseConfig14(android.opengl.EGLDisplay display) {
// Configure EGL for recording and OpenGL ES 3.x
int[] attribList = {
EGL14.EGL_RED_SIZE, 8,
EGL14.EGL_GREEN_SIZE, 8,
EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_ALPHA_SIZE, 8,
EGL14.EGL_RENDERABLE_TYPE, EGLExt.EGL_OPENGL_ES3_BIT_KHR,
EGLExt.EGL_RECORDABLE_ANDROID, 1,
EGL14.EGL_NONE
};
android.opengl.EGLConfig[] configs = new android.opengl.EGLConfig[1];
int[] numConfigs = new int[1];
if (!EGL14.eglChooseConfig(display, attribList, 0, configs, 0,
configs.length, numConfigs, 0)) {
return null;
}
return configs[0];
}
现在,我尝试简化场景,因此在开始录制时,我将 MediaCodec 的一个实例初始化为编码器,并在 GLSurfaceView 的线程上调用 createInputSurface()。有了surface之后,我把它变成了EGLSurface(EGL14),如下:
EGLSurface createEGLSurface(Surface surface) {
if (surface == null) return null;
int[] surfaceAttribs = { EGL14.EGL_NONE };
android.opengl.EGLDisplay display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
android.opengl.EGLConfig config = chooseConfig14(display);
EGLSurface eglSurface = EGL14.eglCreateWindowSurface(display, config, surface, surfaceAttribs, 0);
return eglSurface;
}
当一个新的帧从相机到达时,我将它发送到屏幕和另一个处理记录的类。该类只是将其渲染到由 MediaCodec 的输入表面构建的EGLSurface,如下所示:
public void drawToSurface(EGLSurface targetSurface, int width, int height, long timestampNano, boolean ignoreOrientation) {
if (mTextures[0] == null) {
Log.w(TAG, "Attempting to draw without a source texture");
return;
}
EGLContext currentContext = EGL14.eglGetCurrentContext();
EGLDisplay currentDisplay = EGL14.eglGetCurrentDisplay();
EGL14.eglMakeCurrent(currentDisplay, targetSurface, targetSurface, currentContext);
int error = EGL14.eglGetError();
ShaderProgram program = getProgramForTextureType(mTextures[0].getTextureType());
program.draw(width, height, TextureRendererView.LayoutType.LINEAR_HORIZONTAL, 0, 1, mTextures[0]);
error = EGL14.eglGetError();
EGLExt.eglPresentationTimeANDROID(currentDisplay, targetSurface, timestampNano);
error = EGL14.eglGetError();
EGL14.eglSwapBuffers(currentDisplay, targetSurface);
error = EGL14.eglGetError();
Log.d(TAG, "drawToSurface");
}
由于某种原因,eglSwapBuffers() 失败并报告EGL_BAD_SURFACE,我还没有找到进一步调试的方法。
更新
我尝试在调用后查询当前表面,使其成为当前表面,它总是返回一个格式错误的表面(往里面看,我可以看到句柄是0,并且在查询时它总是失败)。看起来eglMakeCurrent() 默默地未能将表面绑定到上下文。
此外,我已经确定这个问题出现在高通芯片 (Adreno) 上,而不是 Kirin,所以它肯定与原生 OpenGL 实现有关(这有点有趣,因为我一直注意到 Adreno 在涉及到“糟糕”的 OpenGL 配置)
【问题讨论】:
标签: android opengl-es android-mediacodec video-encoding egl