【问题标题】:Manipulate a Custom GLSurfaceView in Android在 Android 中操作自定义 GLSurfaceView
【发布时间】:2015-07-10 12:07:14
【问题描述】:

我有一个由 D.J.I. 创建的自定义 GLSurfaceView。视频渲染由他们自己编译的类完成,它只允许一个 GLSurfaceView 接收他们编码的视频。

我想将屏幕上的视频输出倍增以使用 V.R 查看器查看,但不确定是否可行。我知道通过处理他们的自定义 GLSurfaceView 我可以进行一些操作,例如控制视频输出的大小。 (如果我将这一行:“SplitDjiSurfaceView.w = width;”更改为“width/2”) 我会让左侧工作正常。

问:如何将视频相乘并在一个 GLSurfaceView 上并排播放,然后将其提供给 VideoCallBack?

自定义 GLSurfaceView:

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import android.opengl.GLSurfaceView.EGLContextFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;
import android.util.Log;
import dji.midware.natives.FPVController;
import dji.sdk.api.DJIDrone;
import dji.sdk.api.Camera.DJICameraSettingsTypeDef.CameraPreviewResolustionType;
import dji.sdk.api.DJIDroneTypeDef.DJIDroneType;
import dji.sdk.natives.CamShow;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

public class DjiGLSurfaceView extends GLSurfaceView {
    private static String TAG = "MyGLSurfaceView.java";
    private static final boolean DEBUG = false;
    private static int w = 0;
    private static int h = 0;
    private boolean isPause = false;

    public DjiGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(false, 0, 0);
    }

    public DjiGLSurfaceView(Context context) {
        super(context);
        this.init(false, 0, 0);
    }

    public DjiGLSurfaceView(Context context, boolean translucent, int depth, int stencil) {
        super(context);
        this.init(translucent, depth, stencil);
    }

    private void init(boolean translucent, int depth, int stencil) {
        if(translucent) {
            this.getHolder().setFormat(-3);
        }

        this.setEGLContextFactory(new DjiGLSurfaceView.ContextFactory((DjiGLSurfaceView.ContextFactory)null));
        this.setEGLConfigChooser(translucent?new DjiGLSurfaceView.ConfigChooser(8, 8, 8, 8, depth, stencil):new DjiGLSurfaceView.ConfigChooser(5, 6, 5, 0, depth, stencil));
        this.setRenderer(new DjiGLSurfaceView.MyRenderer((DjiGLSurfaceView.MyRenderer)null));
        this.setRenderMode(0);
    }

    private static void checkEglError(String prompt, EGL10 egl) {
        int error;
        while((error = egl.eglGetError()) != 12288) {
            Log.e(TAG, String.format("%s: EGL error: 0x%x", new Object[]{prompt, Integer.valueOf(error)}));
        }

    }

    public int fcb() {
        this.requestRender();
        return 0;
    }

    private int getType(int type) {
        byte result = 1;
        if(type == 0) {
            result = 1;
        } else if(type == 1) {
            result = 2;
        } else if(type == 2) {
            result = 4;
        } else if(type == 3) {
            result = 8;
        }

        return result;
    }

    public boolean start() {
        (new Thread() {
            public void run() {
                if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
                    CamShow.native_setOnStreamCB(DjiGLSurfaceView.this, "fcb");
                } else {
                    FPVController.native_setOnStreamCB(DjiGLSurfaceView.this, "fcb");
                }

            }
        }).start();
        return true;
    }

    public boolean setStreamType(final CameraPreviewResolustionType type) {
        boolean result = false;
        if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
            (new Thread() {
                public void run() {
                    CamShow.native_pauseStream(true);
                    CamShow.native_setType(DjiGLSurfaceView.this.getType(type.value()));
                    CamShow.native_pauseStream(false);

                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException var2) {
                        var2.printStackTrace();
                    }

                }
            }).start();
            result = true;
        } else {
            result = false;
        }

        return result;
    }

    public boolean setDataToDecoder(byte[] videoBuffer, int size) {
        boolean result = false;
        boolean returnVal = true;
        int returnVal1;
        if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
            returnVal1 = CamShow.native_setDataToDecoder(videoBuffer, size);
        } else {
            returnVal1 = FPVController.native_setDataToDecoder(videoBuffer, size);
        }

        if(returnVal1 == 0) {
            result = true;
        }

        return result;
    }

    public boolean pause() {
        this.isPause = true;
        if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
            CamShow.native_pauseStream(true);
        }

        return true;
    }

    public boolean resume() {
        if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
            CamShow.native_pauseStream(false);
        }

        this.isPause = false;
        return true;
    }

    public boolean destroy() {
        this.isPause = false;
        return true;
    }

    public boolean getIsPause() {
        return this.isPause;
    }

    private void setIsPause(boolean isPause) {
        this.isPause = isPause;
    }

    private static class ConfigChooser implements EGLConfigChooser {
        private static int EGL_OPENGL_ES2_BIT = 4;
        private static int[] s_configAttribs2;
        protected int mRedSize;
        protected int mGreenSize;
        protected int mBlueSize;
        protected int mAlphaSize;
        protected int mDepthSize;
        protected int mStencilSize;
        private int[] mValue = new int[1];

        static {
            s_configAttribs2 = new int[]{12324, EGL_OPENGL_ES2_BIT, 12323, EGL_OPENGL_ES2_BIT, 12322, EGL_OPENGL_ES2_BIT, 12352, EGL_OPENGL_ES2_BIT, 12344};
        }

        public ConfigChooser(int r, int g, int b, int a, int depth, int stencil) {
            this.mRedSize = r;
            this.mGreenSize = g;
            this.mBlueSize = b;
            this.mAlphaSize = a;
            this.mDepthSize = depth;
            this.mStencilSize = stencil;
        }

        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) {
            int[] num_config = new int[1];
            egl.eglChooseConfig(display, s_configAttribs2, (EGLConfig[])null, 0, num_config);
            int numConfigs = num_config[0];
            if(numConfigs <= 0) {
                throw new IllegalArgumentException("No configs match configSpec");
            } else {
                EGLConfig[] configs = new EGLConfig[numConfigs];
                egl.eglChooseConfig(display, s_configAttribs2, configs, numConfigs, num_config);
                return this.chooseConfig(egl, display, configs);
            }
        }

        public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display, EGLConfig[] configs) {
            EGLConfig[] var7 = configs;
            int var6 = configs.length;

            for(int var5 = 0; var5 < var6; ++var5) {
                EGLConfig config = var7[var5];
                int d = this.findConfigAttrib(egl, display, config, 12325, 0);
                int s = this.findConfigAttrib(egl, display, config, 12326, 0);
                if(d >= this.mDepthSize && s >= this.mStencilSize) {
                    int r = this.findConfigAttrib(egl, display, config, 12324, 0);
                    int g = this.findConfigAttrib(egl, display, config, 12323, 0);
                    int b = this.findConfigAttrib(egl, display, config, 12322, 0);
                    int a = this.findConfigAttrib(egl, display, config, 12321, 0);
                    if(r == this.mRedSize && g == this.mGreenSize && b == this.mBlueSize && a == this.mAlphaSize) {
                        return config;
                    }
                }
            }

            return null;
        }

        private int findConfigAttrib(EGL10 egl, EGLDisplay display, EGLConfig config, int attribute, int defaultValue) {
            return egl.eglGetConfigAttrib(display, config, attribute, this.mValue)?this.mValue[0]:defaultValue;
        }

        private void printConfigs(EGL10 egl, EGLDisplay display, EGLConfig[] configs) {
            int numConfigs = configs.length;
            Log.w(DjiGLSurfaceView.TAG, String.format("%d configurations", new Object[]{Integer.valueOf(numConfigs)}));

            for(int i = 0; i < numConfigs; ++i) {
                Log.w(DjiGLSurfaceView.TAG, String.format("Configuration %d:\n", new Object[]{Integer.valueOf(i)}));
                this.printConfig(egl, display, configs[i]);
            }

        }

        private void printConfig(EGL10 egl, EGLDisplay display, EGLConfig config) {
            int[] attributes = new int[]{12320, 12321, 12322, 12323, 12324, 12325, 12326, 12327, 12328, 12329, 12330, 12331, 12332, 12333, 12334, 12335, 12336, 12337, 12338, 12339, 12340, 12343, 12342, 12341, 12345, 12346, 12347, 12348, 12349, 12350, 12351, 12352, 12354};
            String[] names = new String[]{"EGL_BUFFER_SIZE", "EGL_ALPHA_SIZE", "EGL_BLUE_SIZE", "EGL_GREEN_SIZE", "EGL_RED_SIZE", "EGL_DEPTH_SIZE", "EGL_STENCIL_SIZE", "EGL_CONFIG_CAVEAT", "EGL_CONFIG_ID", "EGL_LEVEL", "EGL_MAX_PBUFFER_HEIGHT", "EGL_MAX_PBUFFER_PIXELS", "EGL_MAX_PBUFFER_WIDTH", "EGL_NATIVE_RENDERABLE", "EGL_NATIVE_VISUAL_ID", "EGL_NATIVE_VISUAL_TYPE", "EGL_PRESERVED_RESOURCES", "EGL_SAMPLES", "EGL_SAMPLE_BUFFERS", "EGL_SURFACE_TYPE", "EGL_TRANSPARENT_TYPE", "EGL_TRANSPARENT_RED_VALUE", "EGL_TRANSPARENT_GREEN_VALUE", "EGL_TRANSPARENT_BLUE_VALUE", "EGL_BIND_TO_TEXTURE_RGB", "EGL_BIND_TO_TEXTURE_RGBA", "EGL_MIN_SWAP_INTERVAL", "EGL_MAX_SWAP_INTERVAL", "EGL_LUMINANCE_SIZE", "EGL_ALPHA_MASK_SIZE", "EGL_COLOR_BUFFER_TYPE", "EGL_RENDERABLE_TYPE", "EGL_CONFORMANT"};
            int[] value = new int[1];

            for(int i = 0; i < attributes.length; ++i) {
                int attribute = attributes[i];
                String name = names[i];
                if(egl.eglGetConfigAttrib(display, config, attribute, value)) {
                    Log.w(DjiGLSurfaceView.TAG, String.format("  %s: %d\n", new Object[]{name, Integer.valueOf(value[0])}));
                }
            }

        }
    }

    private static class ContextFactory implements EGLContextFactory {
        private static int EGL_CONTEXT_CLIENT_VERSION = 12440;

        private ContextFactory() {
        }

        public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig eglConfig) {
            Log.w(DjiGLSurfaceView.TAG, "creating OpenGL ES 2.0 context");
            DjiGLSurfaceView.checkEglError("Before eglCreateContext", egl);
            int[] attrib_list = new int[]{EGL_CONTEXT_CLIENT_VERSION, 2, 12344};
            EGLContext context = egl.eglCreateContext(display, eglConfig, EGL10.EGL_NO_CONTEXT, attrib_list);
            DjiGLSurfaceView.checkEglError("After eglCreateContext", egl);
            return context;
        }

        public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) {
            egl.eglDestroyContext(display, context);
        }
    }

    private class MyRenderer implements Renderer {
        private MyRenderer() {
        }

        public void onDrawFrame(GL10 gl) {
            if(!DjiGLSurfaceView.this.isPause) {
                if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
                    CamShow.native_GLDrawFrame();
                } else {
                    FPVController.native_GLDrawFrame();
                }
            }

        }

        public void onSurfaceChanged(GL10 gl, int width, int height) {
            DjiGLSurfaceView.w = width;
            DjiGLSurfaceView.h = height;
            if(DJIDrone.getDroneType() == DJIDroneType.DJIDrone_Vision) {
                CamShow.native_GLInit(DjiGLSurfaceView.w, DjiGLSurfaceView.h);
            } else {
                FPVController.native_GLInit(DjiGLSurfaceView.w, DjiGLSurfaceView.h);
            }

        }

        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        }
    }
}

使用 VideoCallBack 实现 GLSurfaceView:

        mReceivedVideoDataCallBack = new DJIReceivedVideoDataCallBack(){

        @Override
        public void onResult(byte[] videoBuffer, int size)
        {
            djiGLSurfaceView.setDataToDecoder(videoBuffer,size);
        }
    };

    DJIDrone.getDjiCamera().setReceivedVideoDataCallBack(mReceivedVideoDataCallBack);

并排放置两个表面视图不起作用,一个失败而另一个工作正常。我猜渲染器只能使用一个。它必须是一个 GLSurfaceView 才能工作。

我一定遗漏了一些东西,请随时提出任何要求...

【问题讨论】:

    标签: android video opengl-es glsurfaceview


    【解决方案1】:

    我不熟悉这个库,但我猜它是通过使用当前的 EGL 上下文和表面来工作的,可能在软件中解码视频并将其作为 GLES 纹理上传。

    如果是这种情况,您可以使用不同的表面配置 EGL,当您调用 native_GLDrawFrame() 时,它将改为使用该表面。如果当前表面是 FBO,它将使用它而不是 SurfaceView。然后,您可以根据需要使用 GLES 多次渲染附加的纹理。 (我假设您正在尝试某种立体声效果。)

    可以在Grafika's“记录 GL 应用程序”活动中找到此类事情的一个示例。 doFrame() 方法,当处于RECMETHOD_FBO 模式时,将渲染到 FBO,然后从它传输两次(一次到屏幕,一次到视频编码器)。像这样的东西应该适合你。

    如果您没有等效代码,则需要从 Grafika 提取 EGL/GLES 代码。

    (您无法从 SurfaceView 读取像素,因此您需要将它们渲染两次。我假设,如果库为您这样做,您就不会问这个问题,因此捕获渲染并对其进行 blitting是必要的。)

    我对图书馆运作方式的猜测也完全有可能是错误的,因此可能需要进行一些实验。

    编辑:我应该注意,虽然您无法从 SurfaceView 获取像素,但您可以在提交帧之前读取它们。因此,如果该库按照我认为的方式工作,它会在不调用 eglSwapBuffers() 的情况下呈现给 GLES(当 onDrawFrame() 返回时由 GLSurfaceView 调用)。因此,在onDrawFrame() 中,您可以使用glReadPixels() 读取像素,使用glTexImage2D() 将它们上传到第二个GLSurfaceView 上下文中的纹理,然后将它们绘制在不同的表面上。这比 FBO 方法慢,因为必须将像素复制到 CPU 并重新输入,但它可能会起作用。

    我还应该指出,无论您采用何种解决方案,最好将视频的两面都放在一个 Surface 上,而不是两个单独的 SurfaceView。如果您使用两个 Surface,则无法保证它们都会在相同的显示刷新时更新,因此您可能会有一侧稍微落后于另一侧的帧。捕获输出然后将其两次渲染到单个 SurfaceView 会更好。 (GLSurfaceView 只是一个 SurfaceView,带有一些额外的代码来处理 EGL 设置和线程管理。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 2012-11-08
      • 1970-01-01
      相关资源
      最近更新 更多