1*ec779b8eSAndroid Build Coastguard Worker# Virtual Camera 2*ec779b8eSAndroid Build Coastguard Worker 3*ec779b8eSAndroid Build Coastguard WorkerThe virtual camera feature allows 3rd party application to expose a remote or 4*ec779b8eSAndroid Build Coastguard Workervirtual camera to the standard Android camera frameworks (Camera2/CameraX, NDK, 5*ec779b8eSAndroid Build Coastguard Workercamera1). 6*ec779b8eSAndroid Build Coastguard Worker 7*ec779b8eSAndroid Build Coastguard WorkerThe stack is composed into 4 different parts: 8*ec779b8eSAndroid Build Coastguard Worker 9*ec779b8eSAndroid Build Coastguard Worker1. The **Virtual Camera Service** (this directory), implementing the Camera HAL 10*ec779b8eSAndroid Build Coastguard Worker and acts as an interface between the Android Camera Server and the *Virtual 11*ec779b8eSAndroid Build Coastguard Worker Camera Owner* (via the VirtualDeviceManager APIs). 12*ec779b8eSAndroid Build Coastguard Worker 13*ec779b8eSAndroid Build Coastguard Worker2. The **VirtualDeviceManager** running in the system process and handling the 14*ec779b8eSAndroid Build Coastguard Worker communication between the Virtual Camera service and the Virtual Camera 15*ec779b8eSAndroid Build Coastguard Worker owner 16*ec779b8eSAndroid Build Coastguard Worker 17*ec779b8eSAndroid Build Coastguard Worker3. The **Virtual Camera Owner**, the client application declaring the Virtual 18*ec779b8eSAndroid Build Coastguard Worker Camera and handling the production of image data. We will also refer to this 19*ec779b8eSAndroid Build Coastguard Worker part as the **producer** 20*ec779b8eSAndroid Build Coastguard Worker 21*ec779b8eSAndroid Build Coastguard Worker4. The **Consumer Application**, the client application consuming camera data, 22*ec779b8eSAndroid Build Coastguard Worker which can be any application using the camera APIs 23*ec779b8eSAndroid Build Coastguard Worker 24*ec779b8eSAndroid Build Coastguard WorkerThis document describes the functionalities of the *Virtual Camera Service* 25*ec779b8eSAndroid Build Coastguard Worker 26*ec779b8eSAndroid Build Coastguard Worker## Before reading 27*ec779b8eSAndroid Build Coastguard Worker 28*ec779b8eSAndroid Build Coastguard WorkerThe service implements the Camera HAL. It's best to have a bit of an 29*ec779b8eSAndroid Build Coastguard Workerunderstanding of how it works by reading the 30*ec779b8eSAndroid Build Coastguard Worker[HAL documentation first](https://source.android.com/docs/core/camera) 31*ec779b8eSAndroid Build Coastguard Worker 32*ec779b8eSAndroid Build Coastguard Worker 33*ec779b8eSAndroid Build Coastguard Worker 34*ec779b8eSAndroid Build Coastguard WorkerThe HAL implementations are declared in: - 35*ec779b8eSAndroid Build Coastguard Worker[VirtualCameraDevice](./VirtualCameraDevice.h) - 36*ec779b8eSAndroid Build Coastguard Worker[VirtualCameraProvider](./VirtualCameraProvider.h) - 37*ec779b8eSAndroid Build Coastguard Worker[VirtualCameraSession](./VirtualCameraSession.h) 38*ec779b8eSAndroid Build Coastguard Worker 39*ec779b8eSAndroid Build Coastguard Worker## Current supported features 40*ec779b8eSAndroid Build Coastguard Worker 41*ec779b8eSAndroid Build Coastguard WorkerVirtual Cameras report `EXTERNAL` 42*ec779b8eSAndroid Build Coastguard Worker[hardware level](https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#INFO_SUPPORTED_HARDWARE_LEVEL) 43*ec779b8eSAndroid Build Coastguard Workerbut some 44*ec779b8eSAndroid Build Coastguard Worker[functionalities of `EXTERNAL`](https://developer.android.com/reference/android/hardware/camera2/CameraMetadata#INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL) 45*ec779b8eSAndroid Build Coastguard Workerhardware level are not fully supported. 46*ec779b8eSAndroid Build Coastguard Worker 47*ec779b8eSAndroid Build Coastguard WorkerHere is a list of supported features - Single input multiple output stream and 48*ec779b8eSAndroid Build Coastguard Workercapture: 49*ec779b8eSAndroid Build Coastguard Worker 50*ec779b8eSAndroid Build Coastguard Worker- Support for YUV and JPEG 51*ec779b8eSAndroid Build Coastguard Worker 52*ec779b8eSAndroid Build Coastguard WorkerNotable missing features: 53*ec779b8eSAndroid Build Coastguard Worker 54*ec779b8eSAndroid Build Coastguard Worker- Support for auto 3A (AWB, AE, AF): virtual camera will announce convergence 55*ec779b8eSAndroid Build Coastguard Worker of 3A algorithm even though it can't receive any information about this from 56*ec779b8eSAndroid Build Coastguard Worker the owner. 57*ec779b8eSAndroid Build Coastguard Worker 58*ec779b8eSAndroid Build Coastguard Worker- No flash/torch support 59*ec779b8eSAndroid Build Coastguard Worker 60*ec779b8eSAndroid Build Coastguard Worker## Overview 61*ec779b8eSAndroid Build Coastguard Worker 62*ec779b8eSAndroid Build Coastguard WorkerGraphic data are exchanged using the Surface infrastructure. Like any other 63*ec779b8eSAndroid Build Coastguard WorkerCamera HAL, the Surfaces to write data into are received from the client. 64*ec779b8eSAndroid Build Coastguard WorkerVirtual Camera exposes a **different** Surface onto which the owner can write 65*ec779b8eSAndroid Build Coastguard Workerdata. In the middle, we use an EGL Texture which adapts (if needed) the producer 66*ec779b8eSAndroid Build Coastguard Workerdata to the required consumer format (scaling only for now, but we might also 67*ec779b8eSAndroid Build Coastguard Workeradd support for rotation and cropping in the future). 68*ec779b8eSAndroid Build Coastguard Worker 69*ec779b8eSAndroid Build Coastguard WorkerWhen the client application requires multiple resolutions, the closest one among 70*ec779b8eSAndroid Build Coastguard Workersupported resolutions is used for the input data and the image data is down 71*ec779b8eSAndroid Build Coastguard Workerscaled for the lower resolutions. 72*ec779b8eSAndroid Build Coastguard Worker 73*ec779b8eSAndroid Build Coastguard WorkerDepending on the type of output, the rendering pipelines change. Here is an 74*ec779b8eSAndroid Build Coastguard Workeroverview of the YUV and JPEG pipelines. 75*ec779b8eSAndroid Build Coastguard Worker 76*ec779b8eSAndroid Build Coastguard Worker**YUV Rendering:** 77*ec779b8eSAndroid Build Coastguard Worker 78*ec779b8eSAndroid Build Coastguard Worker``` 79*ec779b8eSAndroid Build Coastguard WorkerVirtual Device Owner Surface[1] (Producer) --{binds to}--> EGL 80*ec779b8eSAndroid Build Coastguard WorkerTexture[1] --{renders into}--> Client Surface[1-n] (Consumer) 81*ec779b8eSAndroid Build Coastguard Worker``` 82*ec779b8eSAndroid Build Coastguard Worker 83*ec779b8eSAndroid Build Coastguard Worker**JPEG Rendering:** 84*ec779b8eSAndroid Build Coastguard Worker 85*ec779b8eSAndroid Build Coastguard Worker``` 86*ec779b8eSAndroid Build Coastguard WorkerVirtual Device Owner Surface[1] (Producer) --{binds to}--> EGL 87*ec779b8eSAndroid Build Coastguard WorkerTexture[1] --{compress data into}--> temporary buffer --{renders into}--> 88*ec779b8eSAndroid Build Coastguard WorkerClient Surface[1-n] (Consumer) 89*ec779b8eSAndroid Build Coastguard Worker``` 90*ec779b8eSAndroid Build Coastguard Worker 91*ec779b8eSAndroid Build Coastguard Worker## Life of a capture request 92*ec779b8eSAndroid Build Coastguard Worker 93*ec779b8eSAndroid Build Coastguard Worker> Before reading the following, you must understand the concepts of 94*ec779b8eSAndroid Build Coastguard Worker> [CaptureRequest](https://developer.android.com/reference/android/hardware/camera2/CaptureRequest) 95*ec779b8eSAndroid Build Coastguard Worker> and 96*ec779b8eSAndroid Build Coastguard Worker> [OutputConfiguration](https://developer.android.com/reference/android/hardware/camera2/OutputConfiguration). 97*ec779b8eSAndroid Build Coastguard Worker 98*ec779b8eSAndroid Build Coastguard Worker1. The consumer creates a session with one or more `Surfaces` 99*ec779b8eSAndroid Build Coastguard Worker 100*ec779b8eSAndroid Build Coastguard Worker2. The VirtualCamera owner will receive a call to 101*ec779b8eSAndroid Build Coastguard Worker `VirtualCameraCallback#onStreamConfigured` with a reference to another 102*ec779b8eSAndroid Build Coastguard Worker `Suface` where it can write into. 103*ec779b8eSAndroid Build Coastguard Worker 104*ec779b8eSAndroid Build Coastguard Worker3. The consumer will then start sending `CaptureRequests`. The owner will 105*ec779b8eSAndroid Build Coastguard Worker receive a call to `VirtualCameraCallback#onProcessCaptureRequest`, at which 106*ec779b8eSAndroid Build Coastguard Worker points it should write the required data into the surface it previously 107*ec779b8eSAndroid Build Coastguard Worker received. At the same time, a new task will be enqueued in the render thread 108*ec779b8eSAndroid Build Coastguard Worker 109*ec779b8eSAndroid Build Coastguard Worker4. The [VirtualCameraRenderThread](./VirtualCameraRenderThread.cc) will consume 110*ec779b8eSAndroid Build Coastguard Worker the enqueued tasks as they come. It will wait for the producer to write into 111*ec779b8eSAndroid Build Coastguard Worker the input Surface (using `Surface::waitForNextFrame`). 112*ec779b8eSAndroid Build Coastguard Worker 113*ec779b8eSAndroid Build Coastguard Worker > **Note:** Since the Surface API allows us to wait for the next frame, 114*ec779b8eSAndroid Build Coastguard Worker > there is no need for the producer to notify when the frame is ready by 115*ec779b8eSAndroid Build Coastguard Worker > calling a `processCaptureResult()` equivalent. 116*ec779b8eSAndroid Build Coastguard Worker 117*ec779b8eSAndroid Build Coastguard Worker5. The EGL Texture is updated with the content of the Surface. 118*ec779b8eSAndroid Build Coastguard Worker 119*ec779b8eSAndroid Build Coastguard Worker6. The EGL Texture renders into the output Surfaces. 120*ec779b8eSAndroid Build Coastguard Worker 121*ec779b8eSAndroid Build Coastguard Worker7. The Camera client is notified of the "shutter" event and the `CaptureResult` 122*ec779b8eSAndroid Build Coastguard Worker is sent to the consumer. 123*ec779b8eSAndroid Build Coastguard Worker 124*ec779b8eSAndroid Build Coastguard Worker## EGL Rendering 125*ec779b8eSAndroid Build Coastguard Worker 126*ec779b8eSAndroid Build Coastguard Worker### The render thread 127*ec779b8eSAndroid Build Coastguard Worker 128*ec779b8eSAndroid Build Coastguard WorkerThe [VirtualCameraRenderThread](./VirtualCameraRenderThread.h) module takes care 129*ec779b8eSAndroid Build Coastguard Workerof rendering the input from the owner to the output via the EGL Texture. The 130*ec779b8eSAndroid Build Coastguard Workerrendering is done either to a JPEG buffer, which is the BLOB rendering for 131*ec779b8eSAndroid Build Coastguard Workercreating a JPEG or to a YUV buffer used mainly for preview Surfaces or video. 132*ec779b8eSAndroid Build Coastguard WorkerTwo EGLPrograms (shaders) defined in [EglProgram](./util/EglProgram.cc) handle 133*ec779b8eSAndroid Build Coastguard Workerthe rendering of the data. 134*ec779b8eSAndroid Build Coastguard Worker 135*ec779b8eSAndroid Build Coastguard Worker### Initialization 136*ec779b8eSAndroid Build Coastguard Worker 137*ec779b8eSAndroid Build Coastguard Worker[EGlDisplayContext](./util/EglDisplayContext.h) initializes the whole EGL 138*ec779b8eSAndroid Build Coastguard Workerenvironment (Display, Surface, Context, and Config). 139*ec779b8eSAndroid Build Coastguard Worker 140*ec779b8eSAndroid Build Coastguard WorkerThe EGL Rendering is backed by a 141*ec779b8eSAndroid Build Coastguard Worker[ANativeWindow](https://developer.android.com/ndk/reference/group/a-native-window) 142*ec779b8eSAndroid Build Coastguard Workerwhich is just the native counterpart of the 143*ec779b8eSAndroid Build Coastguard Worker[Surface](https://developer.android.com/reference/android/view/Surface), which 144*ec779b8eSAndroid Build Coastguard Workeritself is the producer side of buffer queue, the consumer being either the 145*ec779b8eSAndroid Build Coastguard Workerdisplay (Camera preview) or some encoder (to save the data or send it across the 146*ec779b8eSAndroid Build Coastguard Workernetwork). 147*ec779b8eSAndroid Build Coastguard Worker 148*ec779b8eSAndroid Build Coastguard Worker### More about OpenGL 149*ec779b8eSAndroid Build Coastguard Worker 150*ec779b8eSAndroid Build Coastguard WorkerTo better understand how the EGL rendering works the following resources can be 151*ec779b8eSAndroid Build Coastguard Workerused: 152*ec779b8eSAndroid Build Coastguard Worker 153*ec779b8eSAndroid Build Coastguard WorkerIntroduction to OpenGL: https://learnopengl.com/ 154*ec779b8eSAndroid Build Coastguard Worker 155*ec779b8eSAndroid Build Coastguard WorkerThe official documentation of EGL API can be queried at: 156*ec779b8eSAndroid Build Coastguard Workerhttps://www.khronos.org/registry/egl/sdk/docs/man/xhtml/ 157*ec779b8eSAndroid Build Coastguard Worker 158*ec779b8eSAndroid Build Coastguard WorkerAnd using Google search with the following query: 159*ec779b8eSAndroid Build Coastguard Worker 160*ec779b8eSAndroid Build Coastguard Worker``` 161*ec779b8eSAndroid Build Coastguard Worker[function name] site:https://registry.khronos.org/EGL/sdk/docs/man/html/ 162*ec779b8eSAndroid Build Coastguard Worker 163*ec779b8eSAndroid Build Coastguard Worker// example: eglSwapBuffers site:https://registry.khronos.org/EGL/sdk/docs/man/html/ 164*ec779b8eSAndroid Build Coastguard Worker``` 165