1*e01b6f76SAndroid Build Coastguard Worker# V4L2 Camera HALv3 2*e01b6f76SAndroid Build Coastguard Worker 3*e01b6f76SAndroid Build Coastguard WorkerThe camera.v4l2 library implements a Camera HALv3 using the 4*e01b6f76SAndroid Build Coastguard WorkerVideo For Linux 2 (V4L2) interface. This allows it to theoretically 5*e01b6f76SAndroid Build Coastguard Workerwork with a wide variety of devices, though the limitations of V4L2 6*e01b6f76SAndroid Build Coastguard Workerintroduce some [caveats](#V4L2-Deficiencies), causing this HAL to 7*e01b6f76SAndroid Build Coastguard Workernot be fully spec-compliant. 8*e01b6f76SAndroid Build Coastguard Worker 9*e01b6f76SAndroid Build Coastguard Worker## Current status 10*e01b6f76SAndroid Build Coastguard Worker 11*e01b6f76SAndroid Build Coastguard WorkerPeople are free to use that library if that works for their purpose, 12*e01b6f76SAndroid Build Coastguard Workerbut it's not maintained by Android Camera team. There is another V4L2 13*e01b6f76SAndroid Build Coastguard Workercamera HAL implementation which is maintained by Android Camera team 14*e01b6f76SAndroid Build Coastguard Workerstarting in Android P. See more information 15*e01b6f76SAndroid Build Coastguard Worker[here](https://source.android.com/devices/camera/external-usb-cameras). 16*e01b6f76SAndroid Build Coastguard Worker 17*e01b6f76SAndroid Build Coastguard Worker## Building a Device with the HAL 18*e01b6f76SAndroid Build Coastguard Worker 19*e01b6f76SAndroid Build Coastguard WorkerTo ensure the HAL is built for a device, include the following in your 20*e01b6f76SAndroid Build Coastguard Worker`<device>.mk`: 21*e01b6f76SAndroid Build Coastguard Worker 22*e01b6f76SAndroid Build Coastguard Worker``` 23*e01b6f76SAndroid Build Coastguard WorkerUSE_CAMERA_V4L2_HAL := true 24*e01b6f76SAndroid Build Coastguard WorkerPRODUCT_PACKAGES += camera.v4l2 25*e01b6f76SAndroid Build Coastguard WorkerPRODUCT_PROPERTY_OVERRIDES += ro.hardware.camera=v4l2 26*e01b6f76SAndroid Build Coastguard Worker``` 27*e01b6f76SAndroid Build Coastguard Worker 28*e01b6f76SAndroid Build Coastguard WorkerThe first line ensures the V4L2 HAL module is visible to the build system. 29*e01b6f76SAndroid Build Coastguard WorkerThis prevents checkbuilds on devices that don't have the necessary support 30*e01b6f76SAndroid Build Coastguard Workerfrom failing. The product packages tells the build system to include the V4L2 31*e01b6f76SAndroid Build Coastguard WorkerHALv3 library in the system image. The final line tells the hardware manager 32*e01b6f76SAndroid Build Coastguard Workerto load the V4L2 HAL instead of a default Camera HAL. 33*e01b6f76SAndroid Build Coastguard Worker 34*e01b6f76SAndroid Build Coastguard Worker## Requirements for Using the HAL 35*e01b6f76SAndroid Build Coastguard Worker 36*e01b6f76SAndroid Build Coastguard WorkerDevices and cameras wishing to use this HAL must meet 37*e01b6f76SAndroid Build Coastguard Workerthe following requirements: 38*e01b6f76SAndroid Build Coastguard Worker 39*e01b6f76SAndroid Build Coastguard Worker* The camera must support BGR32, YUV420, and JPEG formats. 40*e01b6f76SAndroid Build Coastguard Worker* The gralloc and other graphics modules used by the device must use 41*e01b6f76SAndroid Build Coastguard Worker`HAL_PIXEL_FORMAT_RGBA_8888` as the `HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED` 42*e01b6f76SAndroid Build Coastguard Worker 43*e01b6f76SAndroid Build Coastguard Worker## Understanding the HAL Code 44*e01b6f76SAndroid Build Coastguard Worker 45*e01b6f76SAndroid Build Coastguard WorkerThere are three large pieces to the V4L2 Camera HAL: the general HALv3 46*e01b6f76SAndroid Build Coastguard WorkerCamera & HAL code, the specific implementation using V4L2, 47*e01b6f76SAndroid Build Coastguard Workerand the Metadata system. 48*e01b6f76SAndroid Build Coastguard Worker 49*e01b6f76SAndroid Build Coastguard WorkerFor context, you may also wish to read some of the documentation in 50*e01b6f76SAndroid Build Coastguard Workerlibhardware/include/camera3.h about how the framework interacts with the HAL. 51*e01b6f76SAndroid Build Coastguard Worker 52*e01b6f76SAndroid Build Coastguard Worker### Camera & HAL Interface 53*e01b6f76SAndroid Build Coastguard Worker 54*e01b6f76SAndroid Build Coastguard WorkerThe camera and HAL interfaces are implemented by the Camera and 55*e01b6f76SAndroid Build Coastguard WorkerV4L2CameraHAL classes. 56*e01b6f76SAndroid Build Coastguard Worker 57*e01b6f76SAndroid Build Coastguard WorkerThe V4L2CameraHAL class deals primarily with initialization of the system. 58*e01b6f76SAndroid Build Coastguard WorkerOn creation, it searches /dev/video* nodes for ones with the necessary 59*e01b6f76SAndroid Build Coastguard Workercapabilities. These are then all presented to the framework as available 60*e01b6f76SAndroid Build Coastguard Workerfor use. Further operations are passed to the individual Cameras as appropriate. 61*e01b6f76SAndroid Build Coastguard Worker 62*e01b6f76SAndroid Build Coastguard WorkerThe Camera class implements the general logic for handling the camera - 63*e01b6f76SAndroid Build Coastguard Workeropening and closing, configuring streams, preparing and tracking requests, etc. 64*e01b6f76SAndroid Build Coastguard WorkerWhile it handles the logistics surrounding the camera, actual image 65*e01b6f76SAndroid Build Coastguard Workercapture and settings logic are implemented by calling down into the 66*e01b6f76SAndroid Build Coastguard Worker[V4L2 Camera](#V4L2-Camera). The Camera (using helper classes) enforces 67*e01b6f76SAndroid Build Coastguard Workerrestrictions given in the [Metadata](#Metadata) initialized by the V4L2Camera, 68*e01b6f76SAndroid Build Coastguard Workersuch as limits on the number of in-flight requests per stream. 69*e01b6f76SAndroid Build Coastguard WorkerNotably, this means you should be able to replace the V4L2 implementation 70*e01b6f76SAndroid Build Coastguard Workerwith something else, and as long as you fill in the metadata correctly the 71*e01b6f76SAndroid Build Coastguard WorkerCamera class should "just work". 72*e01b6f76SAndroid Build Coastguard Worker 73*e01b6f76SAndroid Build Coastguard Worker### V4L2 Specific Implementation 74*e01b6f76SAndroid Build Coastguard Worker 75*e01b6f76SAndroid Build Coastguard WorkerThe V4L2Camera class is the implementation of all the capture functionality. 76*e01b6f76SAndroid Build Coastguard WorkerIt includes some methods for the Camera class to verify the setup, but the 77*e01b6f76SAndroid Build Coastguard Workerbulk of the class is the request queue. The Camera class submits CaptureRequests 78*e01b6f76SAndroid Build Coastguard Workeras they come in and are verified. The V4L2Camera runs these through a three 79*e01b6f76SAndroid Build Coastguard Workerstage asynchronous pipeline: 80*e01b6f76SAndroid Build Coastguard Worker 81*e01b6f76SAndroid Build Coastguard Worker* Acceptance: the V4L2Camera accepts the request, and puts it into waiting to be 82*e01b6f76SAndroid Build Coastguard Workerpicked up by the enqueuer. 83*e01b6f76SAndroid Build Coastguard Worker* Enqueuing: the V4L2Camera reads the request settings, applies them to the 84*e01b6f76SAndroid Build Coastguard Workerdevice, takes a snapshot of the settings, and hands the buffer over to the 85*e01b6f76SAndroid Build Coastguard WorkerV4L2 driver. 86*e01b6f76SAndroid Build Coastguard Worker* Dequeueing: A completed frame is reclaimed from the driver, and sent 87*e01b6f76SAndroid Build Coastguard Workerback to the Camera class for final processing (validation, filling in the 88*e01b6f76SAndroid Build Coastguard Workerresult object, and sending the data back to the framework). 89*e01b6f76SAndroid Build Coastguard Worker 90*e01b6f76SAndroid Build Coastguard WorkerMuch of this work is aided by the V4L2Wrapper helper class, 91*e01b6f76SAndroid Build Coastguard Workerwhich provides simpler inputs and outputs around the V4L2 ioctls 92*e01b6f76SAndroid Build Coastguard Workerbased on their known use by the HAL; filling in common values automatically 93*e01b6f76SAndroid Build Coastguard Workerand extracting the information useful to the HAL from the results. 94*e01b6f76SAndroid Build Coastguard WorkerThis wrapper is also used to expose V4L2 controls to their corresponding 95*e01b6f76SAndroid Build Coastguard WorkerMetadata components. 96*e01b6f76SAndroid Build Coastguard Worker 97*e01b6f76SAndroid Build Coastguard Worker### Metadata 98*e01b6f76SAndroid Build Coastguard Worker 99*e01b6f76SAndroid Build Coastguard WorkerThe Metadata subsystem attempts to organize and simplify handling of 100*e01b6f76SAndroid Build Coastguard Workercamera metadata (system/media/camera/docs/docs.html). At the top level 101*e01b6f76SAndroid Build Coastguard Workeris the Metadata class and the PartialMetadataInterface. The Metadata 102*e01b6f76SAndroid Build Coastguard Workerclass provides high level interaction with the individual components - 103*e01b6f76SAndroid Build Coastguard Workerfilling the static metadata, validating, getting, and setting settings, 104*e01b6f76SAndroid Build Coastguard Workeretc. The Metadata class passes all of these things on to the component 105*e01b6f76SAndroid Build Coastguard WorkerPartialMetadataInterfaces, each of which filter for their specific 106*e01b6f76SAndroid Build Coastguard Workermetadata components and perform the requested task. 107*e01b6f76SAndroid Build Coastguard Worker 108*e01b6f76SAndroid Build Coastguard WorkerSome generalized metadata classes are provided to simplify common logic 109*e01b6f76SAndroid Build Coastguard Workerfor this filtering and application. At a high level, there are three 110*e01b6f76SAndroid Build Coastguard Workertypes: 111*e01b6f76SAndroid Build Coastguard Worker 112*e01b6f76SAndroid Build Coastguard Worker* Properties: a static value. 113*e01b6f76SAndroid Build Coastguard Worker* Controls: dynamically adjustable values, and optionally an 114*e01b6f76SAndroid Build Coastguard Workerassociated static property indicating what allowable values are. 115*e01b6f76SAndroid Build Coastguard Worker* States: a dynamic read-only value. 116*e01b6f76SAndroid Build Coastguard Worker 117*e01b6f76SAndroid Build Coastguard WorkerThe Metadata system uses further interfaces and subclasses to distinguish 118*e01b6f76SAndroid Build Coastguard Workerthe variety of different functionalities necessary for different metadata 119*e01b6f76SAndroid Build Coastguard Workertags. 120*e01b6f76SAndroid Build Coastguard Worker 121*e01b6f76SAndroid Build Coastguard Worker#### Metadata Factory 122*e01b6f76SAndroid Build Coastguard Worker 123*e01b6f76SAndroid Build Coastguard WorkerThis V4L2 Camera HAL implementation utilizes a metadata factory method. 124*e01b6f76SAndroid Build Coastguard WorkerThis method initializes all the 100+ required metadata components for 125*e01b6f76SAndroid Build Coastguard Workerbasic HAL spec compliance. Most do nothing/report fixed values, 126*e01b6f76SAndroid Build Coastguard Workerbut a few are hooked up to the V4L2 driver. 127*e01b6f76SAndroid Build Coastguard Worker 128*e01b6f76SAndroid Build Coastguard WorkerThis HAL was initially designed for use with the Raspberry Pi camera module 129*e01b6f76SAndroid Build Coastguard Workerv2.1, so the fixed defaults are usually assigned based on that camera. 130*e01b6f76SAndroid Build Coastguard Worker 131*e01b6f76SAndroid Build Coastguard Worker## V4L2 Deficiencies 132*e01b6f76SAndroid Build Coastguard Worker 133*e01b6f76SAndroid Build Coastguard Worker* One stream at a time is supported. Notably, this means you must re-configure 134*e01b6f76SAndroid Build Coastguard Workerthe stream between preview and capture if they're not the same format. 135*e01b6f76SAndroid Build Coastguard WorkerThis makes this HAL not backwards compatible with the Android Camera (v1) API 136*e01b6f76SAndroid Build Coastguard Workeras many of its methods attempt to do just that; Camera2 must be used instead. 137*e01b6f76SAndroid Build Coastguard Worker* A variety of metadata properties can't be filled in from V4L2, 138*e01b6f76SAndroid Build Coastguard Workersuch as physical properties of the camera. Thus this HAL will never be capable 139*e01b6f76SAndroid Build Coastguard Workerof providing perfectly accurate information for all cameras it can theoretically 140*e01b6f76SAndroid Build Coastguard Workersupport. 141*e01b6f76SAndroid Build Coastguard Worker* Android requires HALs support YUV420, JPEG, and a format of the graphics 142*e01b6f76SAndroid Build Coastguard Workerstack's choice ("implementation defined"). Very few cameras actually support 143*e01b6f76SAndroid Build Coastguard Workerall of these formats (so far the Raspberry Pi cameras are the only known ones), 144*e01b6f76SAndroid Build Coastguard Workerso some form of format conversion built in to the HAL would be a useful feature 145*e01b6f76SAndroid Build Coastguard Workerto expand the reach/usefulness of this HAL. 146*e01b6f76SAndroid Build Coastguard Worker* V4L2 doesn't make promises about how fast settings will apply, and there's no 147*e01b6f76SAndroid Build Coastguard Workergood way to determine what settings were in effect for a given frame. Thus, 148*e01b6f76SAndroid Build Coastguard Workerthe settings passed into requests and out with results are applied/read as 149*e01b6f76SAndroid Build Coastguard Workera best effort and may be incorrect. 150*e01b6f76SAndroid Build Coastguard Worker* Many features V4L2 is capable of are not hooked up to the HAL, so the HAL 151*e01b6f76SAndroid Build Coastguard Workeris underfeatured compared to the ideal/what is possible. 152*e01b6f76SAndroid Build Coastguard Worker 153*e01b6f76SAndroid Build Coastguard Worker## Other Known Issues 154*e01b6f76SAndroid Build Coastguard Worker 155*e01b6f76SAndroid Build Coastguard Worker* A variety of features are unimplemented: High speed capture, 156*e01b6f76SAndroid Build Coastguard Workerflash torch mode, hotplugging/unplugging. 157