Android中多USB摄像头解决方案——UVCCamera源码分析(五)

本章我们来分析一下之前我们提过的相机采集的数据究竟是如何绘制到屏幕上的,这里需要几个必要的知识点:OpenGL、Android的SurfaceTexture、TextureView。网上可以搜到比较全面的有关于这些知识的文章,因此本文将不会花大篇幅介绍这些知识。

既然要将相机的采集,那么我们还是得从开启预览说起,之前文章(https://www.jianshu.com/p/225734c143ba
)有详细分析过startPreview的具体细节,但是对startPreview这个方法的参数没有做太多的分析,这里我们将从底层倒过来分析这个startPreview参数的来龙去脉。我们先看com.serenegiant.usb.UVCCamera的代码片段:

/**
     * set preview surface with SurfaceHolder
* you can use SurfaceHolder came from SurfaceView/GLSurfaceView * @param holder */ public synchronized void setPreviewDisplay(final SurfaceHolder holder) { nativeSetPreviewDisplay(mNativePtr, holder.getSurface()); } /** * set preview surface with SurfaceTexture. * this method require API >= 14 * @param texture */ public synchronized void setPreviewTexture(final SurfaceTexture texture) { // API >= 11 final Surface surface = new Surface(texture); // XXX API >= 14 nativeSetPreviewDisplay(mNativePtr, surface); } /** * set preview surface with Surface * @param surface */ public synchronized void setPreviewDisplay(final Surface surface) { nativeSetPreviewDisplay(mNativePtr, surface); }

直接说结论,相机相当于一个生产者,它会不断地产生图像数据,与其对应的自然会需要有一个消费者来接收相机送过来的数据,而这个消费者的代表就是Surface。最终这个Surface将会通过Android封装好的SurfaceView或者TextureView绘制到屏幕上。接下来我们再看这里传过来的Surface(或者是Surface的包装如SurfaceTexture等)是怎么来的。

public void handleStartPreview(final Object surface) {
        ...
            if (surface instanceof SurfaceHolder) {
                mUVCCamera.setPreviewDisplay((SurfaceHolder) surface);
            }
            if (surface instanceof Surface) {
                mUVCCamera.setPreviewDisplay((Surface) surface);
            } else {
                mUVCCamera.setPreviewTexture((SurfaceTexture) surface);
            }
            mUVCCamera.startPreview();
        ...
        }

我们继续跟踪代码发现UVCCamera的这几个方法调用者是AbstractUVCCameraHandler中CameraThread的handleStartPreview方法。而这个方法又是AbstractUVCCameraHandler本身的startPreview方法所调用的,重点来了,这个startPreview中的Object surface究竟是从何而来呢。答案就是这个surface正是由最终渲染承载的TextureView所创建出来的。在UVCCamera中已经帮我们封装好了一个TextureView——UVCCameraTextureView,UVCCameraTextureView实现了一个自定义的方便调用的接口——CameraViewInterface。

public interface CameraViewInterface extends IAspectRatioView {
    interface Callback {
        void onSurfaceCreated(CameraViewInterface view, Surface surface);

        void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height);

        void onSurfaceDestroy(CameraViewInterface view, Surface surface);
    }

    void onPause();

    void onResume();

    void setCallback(Callback callback);

    SurfaceTexture getSurfaceTexture();

    Surface getSurface();

    boolean hasSurface();

    void setVideoEncoder(final IVideoEncoder encoder);

    Bitmap captureStillImage(int width, int height);
}

而这个接口中正有一个获取Surface的方法——getSurfaceTexture。我们继续看UVCCameraTextureView中对这个方法的具体实现:

@Override
public SurfaceTexture getSurfaceTexture() {
  return mRenderHandler != null ? mRenderHandler.getPreviewTexture() : null;
}

在UVCCameraTextureView中定义了一个渲染线程——RenderThread,RenderHandler则是这个线程中创建的Handler。我们再看这个RenderHandler的getPreviewTexture方法:

public final SurfaceTexture getPreviewTexture() {
    if (DEBUG) Log.v(TAG, "getPreviewTexture:");
        if (mIsActive) {
            synchronized (mThread.mSync) {
            sendEmptyMessage(MSG_CREATE_SURFACE);
            try {
            mThread.mSync.wait();
            } catch (final InterruptedException e) {
            }
        return mThread.mPreviewSurface;
        }
    } else {
        return null;
    }
}

可以看到最终这个SurfaceTexture是在RenderThread中的,我们发现在return之前这个Handler还发了一个create_surface的消息。

@Override
public final void handleMessage(final Message msg) {
    if (mThread == null) return;
    switch (msg.what) {
    ···
    case MSG_CREATE_SURFACE:
    mThread.updatePreviewSurface();
    break;
    ···
    }
}
public final void updatePreviewSurface() {
    if (DEBUG) Log.i(TAG, "RenderThread#updatePreviewSurface:");
    synchronized (mSync) {
        if (mPreviewSurface != null) {
            if (DEBUG) Log.d(TAG, "updatePreviewSurface:release mPreviewSurface");
            mPreviewSurface.setOnFrameAvailableListener(null);
            mPreviewSurface.release();
            mPreviewSurface = null;
        }
        mEglSurface.makeCurrent();
        if (mTexId >= 0) {
            mDrawer.deleteTex(mTexId);
        }
       // create texture and SurfaceTexture for input from camera
        mTexId = mDrawer.initTex();
        if (DEBUG) Log.v(TAG, "updatePreviewSurface:tex_id=" + mTexId);
        mPreviewSurface = new SurfaceTexture(mTexId);
        mPreviewSurface.setDefaultBufferSize(mViewWidth, mViewHeight);
        mPreviewSurface.setOnFrameAvailableListener(mHandler);
        // notify to caller thread that previewSurface is ready
        mSync.notifyAll();
    }
}

可以看到最终这个SurefaceTexture是在这里创建的。我们大概分析一下这段代码,首先当mPreviewSurface不为空时候先将其release。然后调用EGLBase.IEglSurface的makeCurrent方法,这个EGLBase.IEglSurface也是UVCCamera中定义的,它主要封装了EGL相关的东西,并且它是根据UVCCameraTextureView的Surface所创建的:

private final void init() {
    if (DEBUG) Log.v(TAG, "RenderThread#init:");
    // create EGLContext for this thread
    mEgl = EGLBase.createFrom(null, false, false);
    mEglSurface = mEgl.createFromSurface(mSurface);
    mEglSurface.makeCurrent();
    // create drawing object
    mDrawer = new GLDrawer2D(true);
}

这里的mSurface就是UVCCameraTextureView中onSurfaceTextureAvailable回调所拿到的。

我们继续回到mEglSurface.makeCurrent(),Android里(好像是Api 17以后)用到的EGL是1.4版本,而makeCurrent最终调用的也是EGL14的makeCurrent。这个方法是用来切换EGL的上下文,只有在该方法调用之后,我们才可以调用OpenGL的方法。
接下来是初始化纹理——mDrawer.initTex(),这个mDrawer也是UVCCamera中定义的类—— GLDrawer2D,它是一个对着色器、纹理操作相关的封装,有兴趣的同学可以仔细阅读一下这个类。最终这个方法会返回一个纹理id。接下来的代码就是根据这个纹理id创建一块SurfaceTexture并设置默认框以及对纹理变化的监听。

你可能感兴趣的:(Android中多USB摄像头解决方案——UVCCamera源码分析(五))