PipeWire
0.3.33
|
POD (plain old data) is a sort of data container.
It is comparable to DBus Variant or LV2 Atom.
A POD can express nested structures of Objects (with properties), Vectors, Arrays, sequences and various primitives types. All information in the POD is laid out sequentially in memory and can be written directly to storage or exchanged between processes or threads without additional marshalling.
Each POD is made of a 32 bits size followed by a 32 bits type field, followed by the pod contents. This makes it possible to skip over unknown POD types. The POD start is always aligned to 8 bytes.
PODs can be efficiently constructed and parsed in real-time threads without requiring memory allocations.
PODs use the SPA type system for the basic types and containers. See the SPA types for more info.
PODs can contain a number of basic SPA types:
SPA_TYPE_None
: no value or a NULL pointer.SPA_TYPE_Bool
: a boolean valueSPA_TYPE_Id
: an enumerated valueSPA_TYPE_Int
, SPA_TYPE_Long
, SPA_TYPE_Float
, SPA_TYPE_Double
:SPA_TYPE_String
: a stringSPA_TYPE_Bytes
: a byte arraySPA_TYPE_Rectangle
: a rectangle with width and heightSPA_TYPE_Fraction
: a fraction with numerator and denominatorSPA_TYPE_Bitmap
: an array of bitsPODs can be grouped together in these container types:
SPA_TYPE_Array
: an array of equal sized objectsSPA_TYPE_Struct
: a collection of types and objectsSPA_TYPE_Object
: an object with propertiesSPA_TYPE_Sequence
: a timed sequence of PODsPODs can also contain some extra types:
SPA_TYPE_Pointer
: a typed pointer in memorySPA_TYPE_Fd
: a file descriptorSPA_TYPE_Choice
: a choice of valuesSPA_TYPE_Pod
: a generic type for the POD itselfA POD is usually constructed with a struct spa_pod_builder
. The builder needs to be initialized with a memory region to write into. It is also possible to dynamically grow the memory as needed.
The most common way to construct a POD is on the stack. This does not require any memory allocations. The size of the POD can be estimated pretty easily and if the buffer is not large enough, an appropriate error will be generated.
The code fragment below initializes a pod builder to write into the stack allocated buffer.
Next we need to write some object into the builder. Let's write a simple struct with an Int and Float in it. Structs are comparable to JSON arrays.
First we open the struct container, the struct spa_pod_frame
keeps track of the container context. Next we add some values to the container like this:
Then we close the container by popping the frame again:
spa_pod_builder_pop()
returns a reference to the object we completed on the stack.
We can also use the following construct to make POD objects:
Or even shorter:
It's not possible to use the varargs builder to make a Sequence or Array, use the normal builder methods for that.
POD objects are containers for properties and are comparable to JSON objects.
Start by pushing an object:
An object requires an object type (SPA_TYPE_OBJECT_Props
) and a context id (SPA_PARAM_Props
). The object type defines the properties that can be added to the object and their meaning. The SPA type system allows you to make this connection (See the type system).
Next we can push some properties in the object:
As can be seen, we always need to push a prop (with key and flags) and then the associated value. For performance reasons it is a good idea to always push (and parse) the object keys in ascending order.
Don't forget to pop the result when the object is finished:
There is a shortcut for making objects:
It is possible to express ranges or enumerations of possible values for properties (and to some extend structs). This is achieved with Choice values.
Choice values are really just a choice type and an array of choice values (of the same type). Depending on the choice type, the array values are interpreted in different ways:
SPA_CHOICE_None
: no choice, first value is current SPA_CHOICE_Range
: range: default, min, max SPA_CHOICE_Step
: range with step: default, min, max, step SPA_CHOICE_Enum
: enum: default, alternative,... SPA_CHOICE_Flags
: bitmask of flags
Let's illustrate this with a Props object that specifies a range of possible values for the frequency:
As you can see, first push the choice as a Range, then the values. A Range choice expects at least 3 values, the default value, minimum and maximum values. There is a shortcut for this as well using varargs:
This is a description of a possible SPA_TYPE_OBJECT_Format
as used when enumerating allowed formats (SPA_PARAM_EnumFormat
) in SPA objects:
We can remove all choice values from the object with the spa_pod_object_fixate()
method. This modifies the pod in-place and sets all choice properties to SPA_CHOICE_None
, forcing the default value as the only available value in the choice.
Running fixate on our previous example would result in an object equivalent to:
Parsing a POD usually consists of
validating if raw bytes + size can contain a valid pod inspecting the type of a pod looping over the items in an object or struct getting data out of PODs.
Use spa_pod_from_data()
to check if maxsize of bytes in data contain a POD at the size bytes starting at offset. This function checks that the POD size will fit and not overflow.
Use one of spa_pod_is_bool()
, spa_pod_is_int()
, etc to check for the type of the pod. For simple (non-container) types, spa_pod_get_bool()
, spa_pod_get_int()
etc can be used to extract the value of the pod.
spa_pod_is_object_type()
can be used to check if the POD contains an object of the expected type.
To iterate over the fields of a Struct use:
For parsing Structs it is usually much easier to use the parser below.
To iterate over the properties in an object you can do:
There is a function to retrieve the property for a certain key in the object. If the properties of the object are in ascending order, you can start searching from the previous key.
Similar to the builder, there is a parser object as well.
If the fields in a struct are known, it is much easier to use the parser. Similarly, if the object type (and thus its keys) are known, the parser is easier.
First initialize a struct spa_pod_parser
:
You can then enter containers such as objects or structs with a push operation:
You need to store the context in a struct spa_pod_frame
to be able to exit the container again later.
You can then parse each field. The parser takes care of moving to the next field.
And finally exit the container again:
In most cases, parsing objects is easier with the variable argument functions. The parse function look like the mirror image of the builder functions.
To parse a struct:
To parse properties in an object:
When parsing objects it is possible to have optional fields. You can make a field optional be parsing it with the SPA_POD_OPT_
prefix for the type.
In the next example, the rate and channels fields are optional and when they are not present, the variables will not be changed.
It is not possible to parse a Sequence or Array with the parser. Use the iterator for this.
The parser will handle Choice values as long as they are of type None. It will then parse the single value from the choice. When dealing with other choice values, it's possible to parse the property values into a struct spa_pod
and then inspect the Choice manually, if needed.
Here is an example of parsing the format values as a POD:
spa_pod_get_values()
is a useful function. It returns a struct spa_pod*
with and array of values. For normal PODs and Choice None values, it simply returns the POD and 1 value. For other Choice values it returns the Choice type and an array of values:
Given 2 pod objects of the same type (Object, Struct, ..) one can run a filter and generate a new pod that only contains values that are compatible with both input pods.
This is, for example, used to find a compatible format between two ports.
As an example we can run a filter on two simple PODs:
Filter will contain a POD equivalent to:
Each POD has a 32 bits size field, followed by a 32 bits type field. The size field specifies the size following the type field.
Each POD is aligned to an 8 byte boundary.