PipeWire
0.3.33
|
Tutorial - Part 4: Playing a tone | Index | Tutorial - Part 6: Binding objects
In this tutorial we show how to use a stream to capture a stream of video frames.
Even though we are now working with a different media type and we are capturing instead of playback, you will see that this example is very similar to Tutorial - Part 4: Playing a tone.
Let's take a look at the code before we break it down:
Save as tutorial5.c and compile with:
gcc -Wall tutorial5.c -o tutorial5 -lm $(pkg-config --cflags --libs libpipewire-0.3)
Most of the application is structured like Tutorial - Part 4: Playing a tone.
We create a stream object with different properties to make it a Camera Video Capture stream.
In addition to the process
event, we are also going to listen to a new event, param_changed
:
Because we capture a stream of a wide range of different video formats and resolutions, we have to describe our accepted formats in a different way:
This is using a struct spa_pod_builder
to make a struct spa_pod *
object in the buffer array on the stack. The parameter is of type SPA_PARAM_EnumFormat
which means that it enumerates the possible formats for this stream.
In this example we use the builder to create some CHOICE
entries for the format properties.
We have an enumeration of formats, we need to first give the amount of enumerations that follow, then the default (preferred) value, followed by alternatives in order of preference:
We also have a RANGE
of values for the size. We need to give a default (preferred) size and then a min and max value:
We have something similar for the framerate.
Note that there are other video parameters that we don't specify here. This means that we don't have any restrictions for their values.
See SPA POD for more information about how to make these POD objects.
Now we're ready to connect the stream and run the main loop:
To connect we specify that we have a PW_DIRECTION_INPUT
stream. PW_ID_ANY
means that we are ok with connecting to any producer. We also allow the user to pass an optional target id.
We're setting the PW_STREAM_FLAG_AUTOCONNECT
flag to make an automatic connection to a suitable camera and PW_STREAM_FLAG_MAP_BUFFERS
to let the stream mmap the data for us.
And last we pass the extra parameters for our stream. Here we only have the allowed formats (SPA_PARAM_EnumFormat
).
Running the mainloop will start the connection and negotiation process. First our param_changed
event will be called with the format that was negotiated between our stream and the camera. This is always something that is compatible with what we enumerated in the EnumFormat param when we connected.
Let's take a look at how we can parse the format in the param_changed
event:
First check if there is a param. A NULL param means that it is cleared. The id of the param tells you what param it is. We are only interested in Format param (SPA_PARAM_Format
).
We can parse the media type and subtype as below and ensure that it is of the right type. In our example this will always be true but when your EnumFormat contains different media types or subtypes, this is how you can parse them:
For the video/raw
media type/subtype there is a utility function to parse out the values into a struct spa_video_info
. This makes it easier to deal with.
In this example we dump the video size and parameters but in a real playback or capture application you might want to set up the screen or encoder to deal with the format.
After negotiation, the process function is called for each new frame. Check out Tutorial - Part 4: Playing a tone for another example.
In a real playback application, one would do something with the data, like copy it to the screen or encode it into a file.
Tutorial - Part 4: Playing a tone | Index | Tutorial - Part 6: Binding objects