Axoverlay 2 API
Introduction

Axoverlay can be used to draw arbitrary overlays onto video streams in a camera or other video device. The library is intended to be flexible and there are few restrictions on what you can draw.

Main loop

The event loop of a simple Axoverlay program is illustrated by the 'axoverlay2.c' example program. The elements of the loop are:

  1. Wait for a new video stream to connect
  2. Create one or more overlays on that stream (at your option, depending on stream settings)
  3. Request a new buffer to draw into
  4. Draw a frame of animation into the buffer
  5. Submit the buffer to be shown
  6. Wait for a stream to disconnect
  7. Destroy any overlays which have been created on that stream

Steps 3–5 are done using Axoverlay. These steps may be looped to show an animated overlay, or may be performed just once to show a static overlay.

Steps 1–2 and 6–7 are performed using the Vdo API. These steps are always executed in a loop. Most axoverlay programs create an overlay on each new stream which fits its internal criteria. It is mandatory to destroy overlays on all disconnected streams to avoid exhausting system resources.

Important concepts

Video streams

Consult the Vdo API documentation for more information about video streams. The application decides whether to create overlays on any given stream, and how many to create. Any necessary information about the streams is available in the Vdo API.

All applications should use the 'overlay' stream filter in Vdo (as shown in the example program). This ensures that you don't waste system resources drawing overlays on streams which will not display them.

It is also common to filter out streams too low resolution to keep any text and icons readable.

Overlays

An overlay is placed on a specific stream. The overlay has a unique ID number, a set of frame buffers, and other properties.

Due to memory safety concerns, the overlay is not automatically destroyed when the stream disconnects. Each overlay application must actively clean up overlays as streams disconnect.

The two main operations supported by an overlay:

  1. Switch the frame buffer currently shown to the next frame
  2. Modify other properties of the overlay

Some properties are fixed at overlay creating, so changing them requires you to destroy and re-create the overlay.

Frame buffers

A frame buffer can hold one frame of animation. Each overlay has a set of frame buffers which it cycles through continuously. The frames are managed by the overlay system in firmware, to make sure that frames are safely accessed by software and hardware.

The last submitted frame buffer automatically remains shown until the application submits a new buffer to replace it.

A frame buffer physically represents a chunk of device memory and is normally represented by a Linux dma_buf. It is possible to map the buffer for CPU access in the application. However, the underlying memory may not use normal CPU cache settings, making direct access a bit more complicated. Please refer to the available Linux dma_buf documentation.

An alternative strategy is to draw into a normal (malloc'ed) memory buffer first and then copy the result into the final buffer using memcpy. This approach is used in the example program.

Each buffer is identified by an ID number, returned by axo_buffer_get_id(). The buffer ID may be used (in combination with the overlay ID) to tell when you are drawing into a particular buffer out of the set allocated for your overlay.

Common problems and their solutions

Race condition between creating overlay and stream disconnecting

Symptom: Overlay creation fails with code AXO_ERR_NO_STREAM

Solution: Handle the error code by simply skipping overlay creation and return to the stream monitoring loop. Soon, a stream disconnect event will appear.

Incorrectly aligned overlay size

Symptom: Overlay creation fails for certain odd overlay sizes

Solution: Always call axo_get_aligned_size to find a suitable size for your overlay.

Too large overlays

Sympton: Overlay takes up to much memory on high-resolution products

Solution: Enable upscaling of overlays using axo_propes_set_upscale_x2 when the stream resolution is large. The suggested maximum (non upscaled) overlay size is 4 megapixels.