目录

虚拟设备音视频通话

代码结构

模块

文件名

文件用途

CaaSEngine Init

CaasKitHelper.java

用于提供初始化CaaS服务和注册。

DeviceDiscoverReceiver.java

注册广播并进行初始化。

VirtualCameraListener.java

虚拟化摄像机服务。

ExtSurfaceRender.java

外部EGL渲染。

模块

文件名

文件用途

OpenGL Init

EglManager.java

EGL控制管理器。

EglProgram.java

Opengl Program。

EglUtil.java

Opengl Util。

EglCore.java

配置EGL环境。

模块

文件名

文件用途

CaaSEngineDemo

CaasKitApplication.java

全局的Application。

CaasKitDemoView.java

自定义的GLSurface View文件。

CaasKitDemoActivity.java

全局Activity布局文件。

Calls on virtualized devices Demo

[h2]CaasKitHelper.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo.caaskit;
 
import android.content.Context;
import android.content.IntentFilter;
import android.opengl.EGLContext;
import android.util.Log;
 
import com.huawei.caas.caasservice.HwCaasHandler;
import com.huawei.caas.caasservice.HwCaasServiceCallBack;
import com.huawei.caas.caasservice.HwCaasServiceManager;
import com.huawei.caas.caasservice.HwCaasUtils;
import com.huawei.caaskitdemo.CaasKitApplication;
 
/**
 * CaasKit Helper.
 *
 * @since 2019-10-21
 */
public class CaasKitHelper {
    private static final String TAG = "CaasKitHelper";
    private static final String DMSDP_STARTDISCOVERY = "com.huawei.dmsdp.DMSDP_STARTDISCOVERY";
    private static final int VIEWHEIGHT = 248;
    private static final int VIEWWIDTH = 256;
    private static final int LOCATION_X = 102;
    private static final int LOCATION_Y = 40;
    private static final int LOCATION_STARTY = 24;
    private static CaasKitHelper sCaasKitHelper;
    private DeviceDiscoverReceiver mDiscoverReceiver;
    private HwCaasServiceManager mHwCaasServiceManager;
    private HwCaasHandler mHwCaasHandler;
    private Context mContext;
    private boolean mIsSendShowFail;
    private boolean mIsCaasKitInit;
    private boolean mIsHasCaaSContacts;
 
    private HwCaasServiceCallBack mCallBack = new HwCaasServiceCallBack() {
        @Override
        public void initSuccess(HwCaasHandler handler) {
            // Callback after successful initialization of HwCaasHandler.
            mHwCaasHandler = handler;
            if (mHwCaasHandler != null) {
                boolean isSetSuccess = false;
                // query if there are contacts to call.
                mIsHasCaaSContacts = mHwCaasHandler.hasCaaSContacts(HwCaasUtils.ContactsType.NORMAL_CONTACTS);
                isSetSuccess = mHwCaasHandler.setContactViewSize(VIEWWIDTH, VIEWHEIGHT);
                Log.i(TAG, " isSetSuccess: " + isSetSuccess);
                isSetSuccess = mHwCaasHandler.setAppMode(HwCaasUtils.LANDSCAPE);
                Log.i(TAG, " isSetSuccess: " + isSetSuccess);
                isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.STARTVIEW,
                        HwCaasUtils.POINT_RIGHTANDDOWN, LOCATION_X, LOCATION_STARTY);
                Log.i(TAG, "viewType: " + HwCaasUtils.STARTVIEW + " isSetSuccess: " + isSetSuccess);
                isSetSuccess =
                        mHwCaasHandler.setFloatViewLocation(HwCaasUtils.CONTACTVIEW, HwCaasUtils.POINT_RIGHTANDUP, LOCATION_X, LOCATION_Y);
                Log.i(TAG, "viewType: " + HwCaasUtils.CONTACTVIEW + " isSetSuccess: " + isSetSuccess);
                isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.CALLVIEW, HwCaasUtils.POINT_RIGHTANDUP,
                        LOCATION_X, LOCATION_Y);
                Log.i(TAG, "viewType: " + HwCaasUtils.CALLVIEW + " isSetSuccess: " + isSetSuccess);
                isSetSuccess = mHwCaasHandler.setFloatViewLocation(HwCaasUtils.VIDEOVIEW, HwCaasUtils.POINT_RIGHTANDUP,
                        LOCATION_X, LOCATION_Y);
                Log.i(TAG, "viewType: " + HwCaasUtils.VIDEOVIEW + " isSetSuccess: " + isSetSuccess);
                if (mIsSendShowFail) {
                    sendShow();
                    mIsSendShowFail = false;
                }
            }
        }
 
        @Override
        public void initFail(int retCode) {
            // Callback if init Handler fail.
            Log.i(TAG, "retCode: " + retCode);
            if (retCode == HwCaasUtils.SERVICE_EXCEPTION) {
                stopRendering();
            }
        }
 
        @Override
        public void releaseSuccess() {
            // Callback after successful release of mHwCaasServiceManager.
            mHwCaasHandler = null;
            mIsSendShowFail = false;
        }
    };
 
    private CaasKitHelper() {
        mContext = CaasKitApplication.getContext();
    }
 
    public static CaasKitHelper getInstance() {
        if (sCaasKitHelper == null) {
            synchronized (CaasKitHelper.class) {
                if (sCaasKitHelper == null) {
                    sCaasKitHelper = new CaasKitHelper();
                }
            }
        }
        return sCaasKitHelper;
    }
 
    private void registerDiscoverReceiver() {
        Log.d(TAG, "registerDiscoverReceiver.");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DMSDP_STARTDISCOVERY);
        if (mDiscoverReceiver == null) {
            Log.d(TAG, "mDiscoverReceiver.");
            mDiscoverReceiver = new DeviceDiscoverReceiver();
            mContext.registerReceiver(mDiscoverReceiver, intentFilter);
        }
    }
 
    private void unregisterDiscoverReceiver() {
        Log.d(TAG, "unregisterDiscoverReceiver.");
        if (mDiscoverReceiver != null) {
            mContext.unregisterReceiver(mDiscoverReceiver);
            mDiscoverReceiver = null;
        }
    }
 
    /**
     * identifies whether the rendering thread is working.
     *
     * @return Returns true if recording has been started.
     */
    public boolean isRendering() {
        return ExtSurfaceRender.getInstance().isRendering();
    }
 
    /**
     * tells the surface render to stop rendering.
     */
    public void stopRendering() {
        Log.d(TAG, "stopRendering.");
        if (isRendering()) {
            ExtSurfaceRender.getInstance().stopRendering();
        }
    }
 
    /**
     * Handles an available frame.
     *
     * @param textureId textureId available for rendering.
     */
    public void frameAvailable(int textureId) {
        ExtSurfaceRender.getInstance().frameAvailable(textureId);
    }
 
    /**
     * set the shared EGLContext.
     *
     * @param sharedContext The context to share.
     */
    public void setSharedContext(EGLContext sharedContext) {
        ExtSurfaceRender.getInstance().setSharedContext(sharedContext);
    }
 
    /**
     * Initialization before using CaaSKit.
     */
    public void caasKitInit() {
        Log.d(TAG, "caasKitInit." + mIsCaasKitInit);
        if (!mIsCaasKitInit) {
            registerDiscoverReceiver();
            // Initialize mHwCaasServiceManager instance.
            mHwCaasServiceManager = HwCaasServiceManager.init();
            // Initialize HwCaasHandler instance through handlerType.
            mHwCaasServiceManager.initHandler(mContext, HwCaasUtils.VIRTUAL_CAMERA_TYPE, mCallBack);
            mIsCaasKitInit = true;
        }
    }
 
    /**
     * Virtualize Camera and show float ball.
     */
    public void sendShow() {
        Log.d(TAG, "sendShow.");
        if (mHwCaasHandler != null && mIsHasCaaSContacts) {
            // show float ball.
            int ret = mHwCaasHandler.initVirtualCamera(VirtualCameraListener.DEVICE_ID, VirtualCameraListener.VIRCAMERA_ID);
            Log.d(TAG, "ret: " + ret);
        } else {
            // Prevent first call, mHwCaasHandler hasn't returned yet.
            mIsSendShowFail = true;
            Log.e(TAG, "sendShow fail.");
        }
    }
 
    /**
     * application back to background, release virtual Camera.
     */
    public void releaseVirtualCamera() {
        Log.d(TAG, "releaseVirtualCamera.");
        if (mHwCaasHandler != null) {
            // release virtual camera.
            int ret = mHwCaasHandler.releaseVirtualCamera(VirtualCameraListener.DEVICE_ID, VirtualCameraListener.VIRCAMERA_ID);
            Log.d(TAG, "ret: " + ret);
        }
    }
 
    /**
     * Called when the application is in the foreground and some scenes do not want to display the float ball.
     *
     * @return returns true if sent successfully.
     */
    public boolean sendHide() {
        Log.d(TAG, "sendHide.");
        if (mHwCaasHandler != null) {
            // Send HIDE event to hide float ball.
            boolean isSendoK = mHwCaasHandler.sendEventToCaasService(HwCaasUtils.HIDE);
            Log.d(TAG, "isSendoK: " + isSendoK);
            return isSendoK;
        }
        Log.e(TAG, "sendHide fail.");
        return false;
    }
 
    /**
     * called when the app exits.
     */
    public void caasKitRelease() {
        Log.d(TAG, "caasKitRelease." + mIsCaasKitInit);
        if (mIsCaasKitInit) {
            if (mHwCaasServiceManager != null) {
                // Source release.
                mHwCaasServiceManager.release();
                mHwCaasServiceManager = null;
            }
            unregisterDiscoverReceiver();
            mIsCaasKitInit = false;
        }
    }
}

[h2]DeviceDiscoverReceiver.java

package com.huawei.caaskitdemo.caaskit;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
 
import com.huawei.caaskitdemo.CaasKitApplication;
import com.huawei.dmsdpsdk.localapp.HwDmsdpService;
 
public class DeviceDiscoverReceiver extends BroadcastReceiver {
    private static final String TAG = "DmsdpStartDiscoverReceiver";
    private Context mApplicationContext;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive.");
        mApplicationContext = CaasKitApplication.getContext();
        HwDmsdpService.init(mApplicationContext, new VirtualCameraListener());
    }
}

[h2]VirtualCameraListener.java

package com.huawei.caaskitdemo.caaskit;
 
import android.util.Log;
import android.view.Surface;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
import com.huawei.dmsdpsdk.localapp.CameraInfo;
import com.huawei.dmsdpsdk.localapp.CameraListener;
import com.huawei.dmsdpsdk.localapp.CameraParameters;
import com.huawei.dmsdpsdk.localapp.CommonUtils;
import com.huawei.dmsdpsdk.localapp.DeviceInfo;
import com.huawei.dmsdpsdk.localapp.HwDmsdpService;
 
public class VirtualCameraListener extends CameraListener {
    // Use java.util.UUID.randomUUID().toString() to generate, please do not directly use the ID provided in the demo
    public static final String DEVICE_ID = "ff66bf58-6413-48e7-8dcd-c084254b199e";
 
    // this ID is customized by the developer.
    public static final String VIRCAMERA_ID = "virtualcamera0";
 
    private static final String TAG = "VirtualCameraListener";
 
    private static final int VIDEO_BUFFER_MODE_YUV = 1;
 
    private static final int DEVICE_CAMERA = 5;
 
    private static final String DEVICE_NAME = "CaasKitCamera";
 
    public VirtualCameraListener() {
    }
 
    @Override
    public List<DeviceInfo> getDeviceInfo() {
        Log.d(TAG, "getDeviceInfo.");
        List<DeviceInfo> listDeviceInfo = new ArrayList<>();
 
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setDeviceID(DEVICE_ID);
        deviceInfo.setDeviceName(DEVICE_NAME);
        deviceInfo.setDeviceType(DEVICE_CAMERA);
        deviceInfo.properties.put(CommonUtils.DEVICE_SUPPORTCAMERA_BOOLEAN, "true");
 
        listDeviceInfo.add(deviceInfo);
 
        return listDeviceInfo;
    }
 
    @Override
    public HashMap<String, CameraInfo> getCameraInfo(String deviceId) {
            Log.d(TAG, "getCameraInfo.");
            HashMap<String, CameraInfo> mapCameraInfo = new HashMap<>();
 
            CameraInfo cameraInfo = new CameraInfo();
            cameraInfo.setCameraId(VIRCAMERA_ID);
            cameraInfo.setVideoType(VIDEO_BUFFER_MODE_YUV);
            cameraInfo.setSupportedFpsRange("25000,25000");
            cameraInfo.setSupportedResolutionRange("1080,1920");
 
            mapCameraInfo.put(VIRCAMERA_ID, cameraInfo);
 
            return mapCameraInfo;
    }
 
    @Override
    public CameraParameters getCameraParameters(String cameraId) {
        Log.d(TAG, "getCameraParameters.");
        CameraParameters cameraParams = new CameraParameters();
 
        cameraParams.cameraId = VIRCAMERA_ID;
        cameraParams.properties.put(CommonUtils.CURRENT_RESOLUTION, "1080,1920");
        cameraParams.properties.put(CommonUtils.CURRENT_FRAMERATES, "25000,30000");
        cameraParams.properties.put(CommonUtils.CURRENT_IMAGEFORMAT, CommonUtils.IMAGE_FORMAT_NV21);
        cameraParams.properties.put(CommonUtils.CURRENT_DECODEFORMAT, CommonUtils.DECODE_FORMAT_RGBA);
 
        return cameraParams;
    }
 
    @Override
    public int startCaptureVideo(String cameraId, Surface surface) {
        Log.d(TAG, "startCaptureVideo." + cameraId);
        if (ExtSurfaceRender.getInstance().isEGLContextReady()) {
            Log.d(TAG, "startCaptureVideo.");
            ExtSurfaceRender.getInstance().startRendering(surface);
        }
        return 0;
    }
 
    @Override
    public int stopCaptureVideo(String cameraId) {
        Log.d(TAG, "stopCaptureVideo.");
        ExtSurfaceRender.getInstance().stopRendering();
        return 0;
    }
 
    @Override
    public int notifyUnbindMSDPService() {
       Log.d(TAG, "notifyUnbindMSDPService.");
        HwDmsdpService.release();
        return 0;
    }
 
    @Override
    public void checkPermissionOnCallback(int result) {
        Log.d(TAG, "checkPermissionOnCallback.result: " + result);
    }
}

[h2]ExtSurfaceRender.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 *
 */
 
package com.huawei.caaskitdemo.caaskit;
 
import android.graphics.Point;
import android.opengl.EGLContext;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Surface;
 
import com.huawei.caaskitdemo.opengl.EglManager;
import com.huawei.caaskitdemo.opengl.EglUtil;
 
import java.lang.ref.WeakReference;
 
/**
 * Create an thread to render the specified surface.
 *
 * @since 2019-10-21
 */
public class ExtSurfaceRender implements Runnable {
    private static final String TAG = "ExtSurfaceRender";
    private static final int MSG_START_RENDERING = 0;
    private static final int MSG_STOP_RENDERING = 1;
    private static final int MSG_FRAME_AVAILABLE = 2;
    private static final int MSG_QUIT_RENDERING = 3;
    private static final int VIDEO_WEIGHT = 1920;
    private static final int VIDEO_HEIGHT = 1080;
    private static ExtSurfaceRender sExtSurfaceRender;
 
    private Object mObject = new Object();
    private EglManager mEglManager;
    private Surface mExtSurface;
    private boolean mIsContextReady;
    private EGLContext mSharedContext;
    private volatile RenderHandler mHandler;
    private boolean mIsThreadReady;
    private boolean mIsRunning;
    private Point VideoResolution;
 
    private ExtSurfaceRender() {
    }
 
    public static ExtSurfaceRender getInstance() {
        if (sExtSurfaceRender == null) {
            synchronized (ExtSurfaceRender.class) {
                if (sExtSurfaceRender == null) {
                    sExtSurfaceRender = new ExtSurfaceRender();
                }
            }
        }
        return sExtSurfaceRender;
    }
 
    /**
     * Handles an available frame.
     *
     * @param textureId textureId available for rendering.
     */
    public void frameAvailable(int textureId) {
        synchronized (mObject) {
            if (!mIsThreadReady) {
                return;
            }
        }
        mHandler.sendMessage(mHandler.obtainMessage(MSG_FRAME_AVAILABLE, textureId, 0, null));
    }
 
    /**
     * set the shared EGLContext.
     *
     * @param sharedContext The context to share.
     */
    public void setSharedContext(EGLContext sharedContext) {
        Log.d(TAG, "setSharedContext: " + sharedContext);
        mSharedContext = sharedContext;
        mIsContextReady = true;
    }
 
    /**
     * get the shared EGLContext.
     *
     * @return sharedContext The context to share.
     */
    public EGLContext getSharedContext() {
        return mSharedContext;
    }
 
    /**
     * set the video resolution.
     *
     * @param width video width.
     * @param height video height.
     */
    public void setVideoResolution(int width, int height) {
        Log.d(TAG, "width: " + width + "height: " + height);
        VideoResolution = new Point(width, height);
    }
 
    /**
     * get the video resolution.
     *
     * @return video resolution.
     */
    public Point getVideoResolution() {
        if (VideoResolution == null) {
            return new Point(VIDEO_WEIGHT, VIDEO_HEIGHT);
        }
        return VideoResolution;
    }
 
    /**
     * Judge if EGLContext is ready.
     *
     * @return mIsContextReady.
     */
    public boolean isEGLContextReady() {
        return mIsContextReady;
    }
 
    /**
     * Tells the surface render to start rendering.
     *
     * @param surface the Surface we want to render.
     */
    public void startRendering(Surface surface) {
        Log.d(TAG, "startRendering.");
        synchronized (mObject) {
            if (mIsRunning) {
                Log.w(TAG, "Render thread already running.");
                return;
            }
            if (!mIsContextReady) {
                Log.w(TAG, "EGLContext is not ready.");
                return;
            }
            mIsRunning = true;
            mExtSurface = surface;
            new Thread(this, "ExtSurfaceRender").start();
            while (!mIsThreadReady) {
                try {
                    mObject.wait();
                } catch (InterruptedException e) {
                    Log.e(TAG, "InterruptedException.");
                }
            }
        }
        mHandler.sendMessage(mHandler.obtainMessage(MSG_START_RENDERING));
    }
 
    /**
     * Tells the surface render to stop rendering.
     */
    public void stopRendering() {
        Log.d(TAG, "stopRendering.");
        if (isRendering()) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_STOP_RENDERING));
            mHandler.sendMessage(mHandler.obtainMessage(MSG_QUIT_RENDERING));
        }
    }
 
    /**
     * Identifies whether the rendering thread is working.
     *
     * @return Returns true if recording has been started.
     */
    public boolean isRendering() {
        synchronized (mObject) {
            return mIsRunning;
        }
    }
 
    /**
     * Render thread entry point.
     * Establishes Looper/Handler and waits for messages.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        Looper.prepare();
        synchronized (mObject) {
            mHandler = new RenderHandler(this);
            mIsThreadReady = true;
            mObject.notify();
        }
        Looper.loop();
        Log.d(TAG, "Render thread exiting.");
        synchronized (mObject) {
            mIsThreadReady = false;
            mIsRunning = false;
            mHandler = null;
        }
    }
 
    /**
     * Handles notification of an available frame.
     *
     * @param textureId textureId available for rendering.
     */
    private void handleFrameAvailable(int textureId) {
        if (mEglManager != null) {
            mEglManager.draw(EglUtil.IDENTITY_MATRIX, EglUtil.IDENTITY_MATRIX, textureId);
            mEglManager.swapBuffers();
        }
    }
 
    /**
     * Handles render state change requests. The handler is created on the render thread.
     *
     * @since 2019-10-21
     */
    private static class RenderHandler extends Handler {
        private WeakReference<ExtSurfaceRender> mWeakRender;
 
        public RenderHandler(ExtSurfaceRender render) {
            mWeakRender = new WeakReference<ExtSurfaceRender>(render);
        }
 
        @Override
        public void handleMessage(Message inputMessage) {
            int what = inputMessage.what;
            ExtSurfaceRender render = mWeakRender.get();
            if (render == null) {
                Log.e(TAG, "render is null.");
                return;
            }
            switch (what) {
                case MSG_START_RENDERING:
                    render.handleStartRendering();
                    break;
                case MSG_STOP_RENDERING:
                    render.handleStopRendering();
                    break;
                case MSG_FRAME_AVAILABLE:
                    render.handleFrameAvailable(inputMessage.arg1);
                    break;
                case MSG_QUIT_RENDERING:
                    Looper.myLooper().quit();
                    break;
                default:
                    throw new RuntimeException("invalid value: " + what);
            }
        }
    }
 
    /**
     * Start rendering.
     *
     */
    private void handleStartRendering() {
        Log.d(TAG, "StartRendering.");
        mEglManager = new EglManager(mSharedContext, mExtSurface);
    }
 
    /**
     * Stop rendering.
     */
    private void handleStopRendering() {
        Log.d(TAG, "handleStopRendering.");
        if (mEglManager != null) {
            mEglManager.release();
            mEglManager = null;
        }
        mExtSurface = null;
    }
}

OpenGL Init

[h2]EglManager.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo.opengl;
 
import android.opengl.EGLContext;
import android.opengl.GLES20;
import android.util.Log;
import android.view.Surface;
 
/**
 * Control the open gl to process the video data.
 *
 * @since 2019-10-20
 */
public class EglManager {
    private static final String TAG = "EglManager";
    private EglCore mEglCore;
    private EglProgram mEglProgram2D;
 
    /**
     * Create EGL environment and Associates an EGL surface with the native window surface.
     *
     * @param sharedContext The context to share, or null if sharing is not desired.
     * @param surface the Surface we want to render.
     */
    public EglManager(EGLContext sharedContext, Surface surface) {
        mEglCore = new EglCore(sharedContext);
        if (surface != null) {
            mEglCore.createWindowSurface(surface);
            mEglCore.makeCurrent();
            mEglProgram2D = new EglProgram(GLES20.GL_TEXTURE_2D);
        }
    }
 
    /**
     * Releases any resources associated with the EGL surface.
     */
    public void release() {
        if (mEglCore != null) {
            mEglCore.release();
        }
        if (mEglProgram2D != null) {
            mEglProgram2D.release();
        }
    }
 
    /**
     * draw only use textureId
     *
     * @param mvpMatrix The mvp matrix for rotation and cut off
     * @param texMatrix The texture matrix
     * @param textureId the texture id
     */
    public void draw(float[] mvpMatrix,
                     float[] texMatrix, int textureId) {
        if (mEglProgram2D != null) {
            mEglProgram2D.draw(mvpMatrix, texMatrix, textureId);
        }
    }
 
    /**
     * call eglSwapBuffers.
     */
    public void swapBuffers() {
        if (mEglCore != null) {
            boolean isSuccess = mEglCore.swapBuffers();
            if (!isSuccess) {
                Log.e(TAG, "swapBuffers() Error.");
            }
        }
    }
}

[h2]EglProgram.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo.opengl;
 
import android.opengl.GLES20;
import android.util.Log;
 
import java.nio.FloatBuffer;
 
import static android.opengl.GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
 
/**
 * GL program and supporting functions for textured 2D and EXTERNAL_OES shapes.
 *
 * @since 2019-10-21
 */
public class EglProgram {
    private static final String TAG = "EglProgram";
 
    private static final String SEPARATOR = System.lineSeparator();
 
    private final float[] SQUARE_COORDS = {
            -1.0f, 1.0f,
            -1.0f, -1.0f,
            1.0f, 1.0f,
            1.0f, -1.0f
    };
 
    private final float[] TEXTURE_COORDS_OES = {
            0.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 0.0f,
            1.0f, 1.0f,
    };
 
    private final float[] TEXTURE_COORDS_2D = {
            0.0f, 1 - 0.0f,
            0.0f, 1 - 1.0f,
            1.0f, 1 - 0.0f,
            1.0f, 1 - 1.0f,
    };
 
    // for example: botton left hava 2 coords
    private static final int COORDS_PER_VERTEX = 2;
 
    private static final int VERTEX_SIZE = 2;
 
    private static final String SIMPLE_VERTEX_SHADER =
            "uniform mat4 uMVPMatrix;" + SEPARATOR
                    + "uniform mat4 uTexMatrix;" + SEPARATOR
                    + "attribute vec4 aPosition;" + SEPARATOR
                    + "attribute vec4 aTextureCoord;" + SEPARATOR
                    + "varying vec2 vTextureCoord;" + SEPARATOR
                    + "void main() {" + SEPARATOR
                    + "    gl_Position = uMVPMatrix * aPosition;" + SEPARATOR
                    + "    vTextureCoord = (uTexMatrix * aTextureCoord).xy;" + SEPARATOR
                    + "}" + SEPARATOR;
 
    private static final String SIMPLE_FRAGMENT_SHADER =
            "precision mediump float;" + SEPARATOR
                    + "varying vec2 vTextureCoord;" + SEPARATOR
                    + "uniform sampler2D sTexture;" + SEPARATOR
                    + "void main() {" + SEPARATOR
                    + "    gl_FragColor = texture2D(sTexture, vTextureCoord);" + SEPARATOR
                    + "}" + SEPARATOR;
 
    private static final String OES_FRAGMENT_SHADER =
            "#extension GL_OES_EGL_image_external : require" + SEPARATOR
                    + "precision mediump float;" + SEPARATOR
                    + "varying vec2 vTextureCoord;" + SEPARATOR
                    + "uniform samplerExternalOES sTexture;" + SEPARATOR
                    + "void main() {" + SEPARATOR
                    + "  gl_FragColor = texture2D(sTexture, vTextureCoord);" + SEPARATOR
                    + "}" + SEPARATOR;
 
    private int mStride = COORDS_PER_VERTEX * EglUtil.SIZEOF_FLOAT;
 
    private int mVertexCount = SQUARE_COORDS.length / COORDS_PER_VERTEX;
 
    private FloatBuffer mVertexBuf;
 
    private FloatBuffer mTexureBuf;
 
    private int mProgramId;
 
    private int muMVPMatrixLocation;
 
    private int muTexMatrixLocation;
 
    private int maPositionLocation;
 
    private int maTextureCoordLocation;
 
    private int mTextureTarget;
 
    /**
     * Prepares the program in the current EGL context.
     *
     * @param textureTarget GL_TEXTURE_2D or GL_TEXTURE_EXTERNAL_OES
     */
    public EglProgram(int textureTarget) {
        Log.d(TAG, "EglProgram.");
        mTextureTarget = textureTarget;
        initVertexAndShaderBuffer();
        if (mTextureTarget == GLES20.GL_TEXTURE_2D) {
            mProgramId = EglUtil.makeProgram(SIMPLE_VERTEX_SHADER, SIMPLE_FRAGMENT_SHADER);
        } else if (mTextureTarget == GL_TEXTURE_EXTERNAL_OES) {
            mProgramId = EglUtil.makeProgram(SIMPLE_VERTEX_SHADER, OES_FRAGMENT_SHADER);
        } else {
            throw new RuntimeException("textureTarget is illegal.");
        }
        if (mProgramId == 0) {
            throw new RuntimeException("Unable to create program.");
        }
        getLocationAttr();
    }
 
    /**
     * Releases the program.
     * <p>
     * The appropriate EGL context must be current (i.e. the one that was used to create
     * the program).
     */
    public void release() {
        Log.d(TAG, "deleting program: " + mProgramId);
        GLES20.glDeleteProgram(mProgramId);
        mProgramId = -1;
    }
 
    /**
     * draw only use textureId
     *
     * @param mvpMatrix The mvp matrix for rotation and cut off
     * @param texMatrix The texture matrix
     * @param textureId the texture id
     */
    public void draw(float[] mvpMatrix,
                     float[] texMatrix, int textureId) {
        // clear
        GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
 
        // Select the program.
        GLES20.glUseProgram(mProgramId);
        EglUtil.checkEglError("glUseProgram");
 
        // Set the texture.
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(mTextureTarget, textureId);
        EglUtil.checkEglError("glBindTexture");
 
        // Copy the model / view / projection matrix over.
        GLES20.glUniformMatrix4fv(muMVPMatrixLocation, 1, false, mvpMatrix, 0);
        EglUtil.checkEglError("muMVPMatrixLocation");
 
        // Copy the texture transformation matrix over.
        GLES20.glUniformMatrix4fv(muTexMatrixLocation, 1, false, texMatrix, 0);
        EglUtil.checkEglError("muTexMatrixLocation");
 
        // Enable the "aPosition" vertex attribute.
        GLES20.glEnableVertexAttribArray(maPositionLocation);
        EglUtil.checkEglError("glEnableVertexAttribArray");
 
        // Connect mVertexBuf to "aPosition".
        GLES20.glVertexAttribPointer(maPositionLocation, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false, mStride, mVertexBuf);
        EglUtil.checkEglError("glVertexAttribPointer");
 
        // Enable the "aTextureCoord" vertex attribute.
        GLES20.glEnableVertexAttribArray(maTextureCoordLocation);
        EglUtil.checkEglError("glEnableVertexAttribArray");
 
        // Connect mTexureBuf to "aTextureCoord".
        GLES20.glVertexAttribPointer(maTextureCoordLocation, VERTEX_SIZE,
                GLES20.GL_FLOAT, false, mStride, mTexureBuf);
        EglUtil.checkEglError("glVertexAttribPointer");
 
        // Draw the rect.
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mVertexCount);
        EglUtil.checkEglError("glDrawArrays");
 
        // Done -- disable vertex array, texture, and program.
        GLES20.glDisableVertexAttribArray(maPositionLocation);
        GLES20.glDisableVertexAttribArray(maTextureCoordLocation);
        GLES20.glBindTexture(mTextureTarget, 0);
        GLES20.glUseProgram(0);
    }
 
    private void initVertexAndShaderBuffer() {
        if (mVertexBuf == null) {
            mVertexBuf = EglUtil.createFloatBuffer(SQUARE_COORDS);
        }
        if (mTexureBuf == null) {
            if (mTextureTarget == GLES20.GL_TEXTURE_2D) {
                mTexureBuf = EglUtil.createFloatBuffer(TEXTURE_COORDS_2D);
            } else {
                mTexureBuf = EglUtil.createFloatBuffer(TEXTURE_COORDS_OES);
            }
        }
    }
 
    private void getLocationAttr() {
        maPositionLocation = GLES20.glGetAttribLocation(mProgramId, "aPosition");
        EglUtil.checkLocationError(maPositionLocation, "aPosition");
        muMVPMatrixLocation = GLES20.glGetUniformLocation(mProgramId, "uMVPMatrix");
        EglUtil.checkLocationError(muMVPMatrixLocation, "uMVPMatrix");
        maTextureCoordLocation = GLES20.glGetAttribLocation(mProgramId, "aTextureCoord");
        EglUtil.checkLocationError(maTextureCoordLocation, "aTextureCoord");
        muTexMatrixLocation = GLES20.glGetUniformLocation(mProgramId, "uTexMatrix");
        EglUtil.checkLocationError(muTexMatrixLocation, "uTexMatrix");
    }
}

[h2]EglUtil.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo.opengl;
 
import android.opengl.EGL14;
import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
 
import javax.microedition.khronos.opengles.GL10;
 
import static android.opengl.GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
 
/**
 * Some OpenGL utility functions.
 *
 * @since 2019-10-21
 */
public class EglUtil {
    public static final String TAG = "EglUtil";
 
    /**
     * The size of egl float
     */
    public static final int SIZEOF_FLOAT = 4;
 
    /**
     * The identity of matrix
     */
    public static final float[] IDENTITY_MATRIX;
 
    /**
     * The matrix size
     */
    public static final int MATRIX_SIZE = 16;
 
    static {
        IDENTITY_MATRIX = new float[MATRIX_SIZE];
        Matrix.setIdentityM(IDENTITY_MATRIX, 0);
    }
 
    private EglUtil() {}
 
    /**
     * Use the supplied vertex and fragment shaders to creates a new program.
     *
     * @param vertexProgramSource the vertex source
     * @param fragmentProgramSource the fragment source
     * @return The handle of the program, or 0 on failure.
     */
    public static int makeProgram(String vertexProgramSource, String fragmentProgramSource) {
        int vertexProgramShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexProgramSource);
        if (vertexProgramShader == 0) {
            return 0;
        }
        int pixelProgramShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentProgramSource);
        if (pixelProgramShader == 0) {
            return 0;
        }
 
        int programId = GLES20.glCreateProgram();
        checkEglError("glCreateProgram");
        if (programId == 0) {
            Log.e(TAG, "Could not make program");
        }
        GLES20.glAttachShader(programId, vertexProgramShader);
        checkEglError("vertexProgramShader");
        GLES20.glAttachShader(programId, pixelProgramShader);
        checkEglError("pixelProgramShader");
        GLES20.glLinkProgram(programId);
        int[] linkProgramStatus = new int[1];
        GLES20.glGetProgramiv(programId, GLES20.GL_LINK_STATUS, linkProgramStatus, 0);
        if (linkProgramStatus[0] != GLES20.GL_TRUE) {
            Log.e(TAG, "Could not link program.");
            Log.e(TAG, GLES20.glGetProgramInfoLog(programId));
            GLES20.glDeleteProgram(programId);
            programId = 0;
        }
        return programId;
    }
 
    /**
     * Load and compile the provided shader shaderSource.
     *
     * @param shaderType the shader type
     * @param shaderSource The shaderSource
     * @return The handle of the shader, or 0 on failure.
     */
    public static int loadShader(int shaderType, String shaderSource) {
        int shaderId = GLES20.glCreateShader(shaderType);
        checkEglError("glCreateShader type: " + shaderType);
        GLES20.glShaderSource(shaderId, shaderSource);
        GLES20.glCompileShader(shaderId);
        int[] compiledShaders = new int[1];
        GLES20.glGetShaderiv(shaderId, GLES20.GL_COMPILE_STATUS, compiledShaders, 0);
        if (compiledShaders[0] == 0) {
            Log.e(TAG, "Could not compile shaderId: " + shaderType + " : ");
            Log.e(TAG, " " + GLES20.glGetShaderInfoLog(shaderId));
            GLES20.glDeleteShader(shaderId);
            shaderId = 0;
        }
        return shaderId;
    }
 
    /**
     * Check the egl error.
     *
     * @param msg msg on error.
     */
    public static void checkEglError(String msg) {
        int error;
        if ((error = EGL14.eglGetError()) != EGL14.EGL_SUCCESS) {
            throw new RuntimeException(msg + "-EGL error: 0x" + Integer.toHexString(error));
        }
    }
 
    /**
     * Check location error.
     *
     * @param location The location
     * @param label The label
     */
    public static void checkLocationError(int location, String label) {
        if (location < 0) {
            throw new RuntimeException("location error: " + label);
        }
    }
 
    /**
     * Allocates a direct float buffer, and populates it with the float array data.
     *
     * @param coords Coordinate array.
     */
    public static FloatBuffer createFloatBuffer(float[] coords) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(coords.length * SIZEOF_FLOAT);
        byteBuffer.order(ByteOrder.nativeOrder());
        FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
        floatBuffer.put(coords);
        floatBuffer.position(0);
        return floatBuffer;
    }
 
    /**
     * Rotate matrix by angle.
     *
     * @param matrix Matrix before rotation.
     * @param angle Rotation angle.
     * @return Matrix after rotation.
     */
    public static float[] rotateMatrix(float[] matrix, float angle){
        Matrix.rotateM(matrix,0, angle,0,0,1);
        return matrix;
    }
 
    /**
     * Generate textureId and bind GL_TEXTURE_EXTERNAL_OES.
     *
     * @return textureId.
     */
    public static int genOesTextureId() {
        int[] textureObjectId = new int[1];
        GLES20.glGenTextures(1, textureObjectId, 0);
        GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureObjectId[0]);
        GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES,
                GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
        GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES,
                GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
        GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES,
                GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES,
                GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
        return textureObjectId[0];
    }
}

[h2]EglCore.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo.opengl;
 
import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import android.util.Log;
import android.view.Surface;
 
import static android.opengl.EGLExt.EGL_RECORDABLE_ANDROID;
 
/**
 * Prepares EGL display and context.
 *
 * @since 2019-10-21
 */
public final class EglCore {
    private static final String TAG = "EglCore";
    private static final int VERSION_SIZE = 2;
    private static final int EGL_SIZE = 8;
    private EGLDisplay mEGLDisplay = EGL14.EGL_NO_DISPLAY;
    private EGLContext mEGLContext = EGL14.EGL_NO_CONTEXT;
    private EGLConfig mEGLConfig = null;
    private EGLSurface mEglSurface;
 
    /**
     * Prepares EGL display and context.
     *
     * @param sharedContext The context to share, or null if sharing is not desired.
     */
    public EglCore(EGLContext sharedContext) {
        if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) {
            throw new RuntimeException("EGL already set up.");
        }
        mEGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
        if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) {
            throw new RuntimeException("unable to get EGL14 display.");
        }
        int[] version = new int[VERSION_SIZE];
        if (!EGL14.eglInitialize(mEGLDisplay, version, 0, version, 1)) {
            mEGLDisplay = EGL14.EGL_NO_DISPLAY;
            throw new RuntimeException("eglInitialize failed.");
        }
        if (sharedContext == null) {
            sharedContext = EGL14.EGL_NO_CONTEXT;
        }
        if (mEGLContext == EGL14.EGL_NO_CONTEXT) {
            int[] attributes = {
                    EGL14.EGL_RED_SIZE, EGL_SIZE,
                    EGL14.EGL_GREEN_SIZE, EGL_SIZE,
                    EGL14.EGL_BLUE_SIZE, EGL_SIZE,
                    EGL14.EGL_ALPHA_SIZE, EGL_SIZE,
                    EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
                    EGL_RECORDABLE_ANDROID, 1,
                    EGL14.EGL_NONE
            };
            EGLConfig[] configs = new EGLConfig[1];
            int[] numConfigs = new int[1];
            if (!EGL14.eglChooseConfig(mEGLDisplay, attributes, 0, configs, 0, configs.length,
                    numConfigs, 0)) {
                throw new RuntimeException("eglChooseConfig fail.");
            }
            int[] attribList = {
                    EGL14.EGL_CONTEXT_CLIENT_VERSION, VERSION_SIZE,
                    EGL14.EGL_NONE
            };
            EGLContext context = EGL14.eglCreateContext(mEGLDisplay, configs[0], sharedContext,
                    attribList, 0);
            EglUtil.checkEglError("eglCreateContext");
            mEGLConfig = configs[0];
            mEGLContext = context;
        }
    }
 
    /**
     * Discards all resources held by this class, notably the EGL context.  This must be
     * called from the thread where the context was created.
     * <p>
     * On completion, no context will be current.
     */
    public void release() {
        EGL14.eglDestroySurface(mEGLDisplay, mEglSurface);
        EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE,
                EGL14.EGL_NO_CONTEXT);
        EGL14.eglDestroyContext(mEGLDisplay, mEGLContext);
        EGL14.eglReleaseThread();
        EGL14.eglTerminate(mEGLDisplay);
        mEGLDisplay = EGL14.EGL_NO_DISPLAY;
        mEGLContext = EGL14.EGL_NO_CONTEXT;
        mEglSurface = EGL14.EGL_NO_SURFACE;
        mEGLConfig = null;
    }
 
    @Override
    protected void finalize() throws Throwable {
        try {
            if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) {
                Log.e(TAG, "EglCore was not explicitly released.");
                release();
            }
        } finally {
            super.finalize();
        }
    }
 
    /**
     * Create a window surface, and attach it to the Surface we received.
     *
     * @param surface the Surface we want to render.
     */
    public void createWindowSurface(Object surface) {
        if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture)) {
            throw new RuntimeException("invalid surface: " + surface);
        }
        int[] surfaceAttribs = {
                EGL14.EGL_NONE
        };
        EGLSurface eglSurface = EGL14.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, surface,
                surfaceAttribs, 0);
        EglUtil.checkEglError("eglCreateWindowSurface");
        mEglSurface = eglSurface;
    }
 
    /**
     * Makes our EGL context current, using the supplied surface for both "draw" and "read".
     */
    public void makeCurrent() {
        if (mEglSurface == null) {
            Log.e(TAG, "makeCurrent eglSurface is null.");
            return;
        }
        if (!EGL14.eglMakeCurrent(mEGLDisplay, mEglSurface, mEglSurface, mEGLContext)) {
            throw new RuntimeException("eglMakeCurrent failed");
        }
    }
 
    /**
     * Calls eglSwapBuffers. Use this to "publish" the current frame.
     *
     * @return false on failure
     */
    public boolean swapBuffers() {
        return EGL14.eglSwapBuffers(mEGLDisplay, mEglSurface);
    }
}

CaaSEngine Demo

[h2]CaasKitApplication.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo;
 
import android.app.Application;
import android.content.Context;
import android.util.Log;
 
/**
 * Main application.
 *
 * @since 2019-10-21
 */
public class CaasKitApplication extends Application {
    private static final String TAG = "CaasKitApplication";
 
    private static CaasKitApplication sApplication;
 
    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate.");
        super.onCreate();
        setApp(this);
    }
 
    public static CaasKitApplication getInstance() {
        return sApplication;
    }
 
    public static Context getContext() {
        return sApplication.getApplicationContext();
    }
 
    private static synchronized void setApp(CaasKitApplication app) {
        sApplication = app;
    }
}

[h2]CaasKitDemoView.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */
 
package com.huawei.caaskitdemo;
 
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.opengl.EGL14;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.ViewGroup;
 
import com.huawei.caaskitdemo.caaskit.CaasKitHelper;
import com.huawei.caaskitdemo.opengl.EglProgram;
import com.huawei.caaskitdemo.opengl.EglUtil;
 
import java.io.File;
import java.io.IOException;
 
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
 
import static android.opengl.GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
 
/**
 * Create an OpenGL environment. Complete interaction with mediaplayer.
 *
 * @since 2019-10-23
 */
public class CaasKitDemoView extends GLSurfaceView implements GLSurfaceView.Renderer {
    private static final String TAG = "CaasKitDemoView";
    private static final int EGL_VERSION = 2;
    private MediaPlayer mMediaPlayer;
    private int mPlayingPosition;
    private File mVideoFilePath;
    private EglProgram mEglProgramOes;
    private EglProgram mEglProgram2D;
    private SurfaceTexture mSurfaceTexture;
    private int mOesTextureId;
    private float[] mModelMatrix = new float[EglUtil.MATRIX_SIZE];
    private int mOffscreenTextureId;
    private int mFrameBuffer;
    private Surface mOesSurface;
 
    public CaasKitDemoView(Context context) {
        super(context);
        Log.d(TAG, "CaasKitDemoView.");
        setEGLContextClientVersion(EGL_VERSION);
        setRenderer(this);
        setRenderMode(RENDERMODE_WHEN_DIRTY);
    }
 
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.d(TAG, "onSurfaceCreated.");
        destroyFrameBuffer();
        mOesTextureId = EglUtil.genOesTextureId();
        mSurfaceTexture = new SurfaceTexture(mOesTextureId);
        mOesSurface = new Surface(mSurfaceTexture);
        mSurfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
            @Override
            public void onFrameAvailable(SurfaceTexture surfaceTexture) {
                requestRender();
            }
        });
        mEglProgramOes = new EglProgram(GL_TEXTURE_EXTERNAL_OES);
        mEglProgram2D = new EglProgram(GLES20.GL_TEXTURE_2D);
        Matrix.setIdentityM(mModelMatrix, 0);
        CaasKitHelper.getInstance().setSharedContext(EGL14.eglGetCurrentContext());
        if (mPlayingPosition > 0) {
            startPlaying(mPlayingPosition);
            mPlayingPosition = 0;
        }
    }
 
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.d(TAG, "onSurfaceChanged.");
        destroyFrameBuffer();
        prepareFramebuffer(width, height);
        GLES20.glViewport(0, 0, width, height);
        Log.d(TAG, "width: " + width + "height: " + height);
    }
 
    @Override
    public void onDrawFrame(GL10 gl) {
        if (mSurfaceTexture != null) {
            mSurfaceTexture.updateTexImage();
        }
 
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer);
        mEglProgramOes.draw(mModelMatrix, EglUtil.IDENTITY_MATRIX, mOesTextureId);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
 
        if (CaasKitHelper.getInstance().isRendering()) {
            CaasKitHelper.getInstance().frameAvailable(mOffscreenTextureId);
        }
 
        mEglProgram2D.draw(EglUtil.IDENTITY_MATRIX, EglUtil.IDENTITY_MATRIX, mOffscreenTextureId);
    }
 
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        super.surfaceDestroyed(holder);
        Log.e(TAG, "surfaceDestroyed.");
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mPlayingPosition = mMediaPlayer.getCurrentPosition();
            Log.e(TAG, "mPlayingPosition: " + mPlayingPosition);
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
 
    @Override
    public void onPause() {
        Log.d(TAG, "onPause.");;
        super.onPause();
        if (mSurfaceTexture != null) {
            Log.d(TAG, "renderer pausing.");
            if (CaasKitHelper.getInstance().isRendering()) {
                CaasKitHelper.getInstance().stopRendering();
            }
            destroyFrameBuffer();
            mSurfaceTexture.release();
            mEglProgramOes.release();
            mEglProgram2D.release();
            mSurfaceTexture = null;
            mEglProgramOes = null;
            mEglProgram2D = null;
        }
    }
 
    public boolean startPlaying(File file) {
        if (file == null || !file.exists()
           || !file.canRead() || mOesSurface == null) {
            return false;
        }
        mVideoFilePath = file;
        startPlaying(0);
        return true;
    }
 
    public void onDestroy() {
        Log.d(TAG, "onDestroy.");
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
 
    private void startPlaying(int position) {
        if (mVideoFilePath == null) {
            return;
        }
        if (mMediaPlayer != null) {
            Log.e(TAG, "MediaPlayer release.");
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(mVideoFilePath.toString());
            mMediaPlayer.prepare();
        } catch (IllegalStateException e) {
            Log.e(TAG, "IllegalStateException.");
        } catch (IOException e) {
            Log.e(TAG, "IOException.");
        }
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                Log.d(TAG, "mMediaPlayer onCompletion.");
                mMediaPlayer.seekTo(0);
                mMediaPlayer.start();
            }
        });
        mMediaPlayer.setSurface(mOesSurface);
        mMediaPlayer.setOnPreparedListener(new PrepareListener(position));
    }
 
    private void adjustAspectRatio(int videoWidth, int videoHeight) {
        int viewWidth = getWidth();
        int viewHeight = getHeight();
        double aspectRatio = (double) videoHeight / videoWidth;
        int newWidth;
        int newHeight;
        if (viewHeight > (int) (viewWidth * aspectRatio)) {
            newWidth = viewWidth;
            newHeight = (int) (viewWidth * aspectRatio);
        } else {
            newWidth = (int) (viewHeight / aspectRatio);
            newHeight = viewHeight;
        }
        post(() -> {
            ViewGroup.LayoutParams layoutParams = getLayoutParams();
            layoutParams.height = newHeight;
            layoutParams.width = newWidth;
            setLayoutParams(layoutParams);
        });
    }
 
    private void prepareFramebuffer(int width, int height) {
        int[] values = new int[1];
        GLES20.glGenTextures(1, values, 0);
        mOffscreenTextureId = values[0];
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mOffscreenTextureId);
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0,
                GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
                GLES20.GL_NEAREST);
        GLES20.glTexParameterf(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);
        GLES20.glGenFramebuffers(1, values, 0);
        mFrameBuffer = values[0];
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffer);
        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mOffscreenTextureId, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
    }
 
    private void destroyFrameBuffer() {
        GLES20.glDeleteTextures(1, new int[] {mOffscreenTextureId}, 0);
        GLES20.glDeleteFramebuffers(1, new int[] {mFrameBuffer}, 0);
    }
 
    private final class PrepareListener implements MediaPlayer.OnPreparedListener {
        private int mPosition;
 
        public PrepareListener(int position) {
            mPosition = position;
        }
 
        public void onPrepared(MediaPlayer mediaPlayer) {
            adjustAspectRatio(mMediaPlayer.getVideoWidth(), mMediaPlayer.getVideoHeight());
            if (mPosition > 0) {
                mMediaPlayer.seekTo(mPosition);
            }
            Log.d(TAG, "mediaPlayer start.");
            mMediaPlayer.start();
            CaasKitHelper.getInstance().sendShow();
        }
    }
}

[h2]CaasKitDemoActivity.java

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 *
 */
 
package com.huawei.caaskitdemo;
 
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import com.huawei.caaskitdemo.caaskit.CaasKitHelper;
 
import java.io.File;
 
public class CaasKitDemoActivity extends AppCompatActivity {
    private static final String TAG = "CaaSKitDemoActivity";
    private final int REQUEST_EXTERNAL_STORAGE = 1;
    private final int REQUEST_MEDIA_DATA = 1;
    public CaasKitDemoView mVideoView;
    private RelativeLayout mRootView;
    private Button mVideoControl;
    private Button mVideoPick;
    private Button mCallShow;
    private Button mCallHide;
    private View mCallView;
    private File mVideoFilePath;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_caaskitdemo);
        int permission = ActivityCompat.checkSelfPermission(getApplication(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_EXTERNAL_STORAGE);
        } else {
            setButtonClickEvent();
        }
    }
 
    private void setButtonClickEvent() {
        Log.d(TAG, "setButtonClickEvent.");
        mRootView = (RelativeLayout) findViewById(R.id.root_view);
        mVideoControl = findViewById(R.id.video_control);
        mVideoControl.setOnClickListener(v -> {
            if (mVideoView != null) {
                startPlaying();
            }
        });
        mVideoPick = findViewById(R.id.video_pick);
        mVideoPick.setOnClickListener(v ->{
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, REQUEST_MEDIA_DATA);
        });
        mCallView = findViewById(R.id.call_view);
        mCallShow = findViewById(R.id.call_show);
        mCallHide = findViewById(R.id.call_hide);
        mCallShow.setOnClickListener(v ->{
            CaasKitHelper.getInstance().sendShow();
        });
        mCallHide.setOnClickListener(v ->{
            CaasKitHelper.getInstance().sendHide();
        });
    }
 
    private void startPlaying() {
        if (mVideoFilePath != null) {
            boolean isVideoPlaying = mVideoView.startPlaying(mVideoFilePath);
            if (isVideoPlaying) {
                mVideoPick.setVisibility(View.GONE);
                mVideoControl.setVisibility(View.GONE);
                mCallView.setVisibility(View.VISIBLE);
            }
        }
    }
 
    @Override
    protected void onPause() {
        Log.d(TAG, "onPause.");
        super.onPause();
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop.");
        if (mVideoView != null) {
            mVideoView.onPause();
        }
        CaasKitHelper.getInstance().releaseVirtualCamera();
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume.");
        if (mVideoView != null) {
            mVideoView.onResume();
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy.");
        if (mVideoView != null) {
            mVideoView.onDestroy();
            mVideoView = null;
        }
        CaasKitHelper.getInstance().caasKitRelease();
    }
 
    private void addVideoView() {
        if (mVideoView == null) {
            Log.d(TAG, "addVideoView.");
            mVideoView = new CaasKitDemoView(this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.gravity = Gravity.CENTER;
            mRootView.addView(mVideoView, params);
            mVideoControl.setVisibility(View.VISIBLE);
        }
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_EXTERNAL_STORAGE:
                if (grantResults[0] == 0) {
                    setButtonClickEvent();
                } else {
                    finish();
                }
                break;
            default:
                break;
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_MEDIA_DATA) {
            if (resultCode == RESULT_OK && data != null && data.getData() != null) {
                setVideoFilePath(data);
                addVideoView();
                CaasKitHelper.getInstance().caasKitInit();
            }
        }
    }
 
    private void setVideoFilePath(Intent intent) {
        Uri uri = intent.getData();
        Cursor cursor = null;
        try {
            String[] filePathColumn = {MediaStore.Video.Media.DATA};
            cursor = getContentResolver().query(uri,
                    filePathColumn, null, null, null);
            if (cursor == null) {
                return;
            }
            cursor.moveToFirst();
            String filePath = cursor.getString(0);
            Toast.makeText(this, "FILE PATH: " + filePath, Toast.LENGTH_SHORT).show();
            mVideoFilePath = new File(filePath);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
}

示例代码下载

huawei-CaaS-CallsOnVirtualizedDevices-android-demo.zip