Commit graph

269 commits

Author SHA1 Message Date
Demi Marie Obenour
babcf6d118 *: mark code that cannot support oversized POD values
This should be supported, but lots of code doesn't support it yet.
2025-09-08 15:49:31 -04:00
Barnabás Pőcze
3b33f60d2f treewide: map SPA_PROP_exposure to V4L2_CID_EXPOSURE_ABSOLUTE
Currently the v4l2 and libcamera plugins map `SPA_PROP_exposure` in incompatible
ways. So change the v4l2 mapping to `V4L2_CID_EXPOSURE_ABSOLUTE` because at least
that is in units of time (a step closer to addressing #4697), and because that
is more relevant for UVC cameras.

Also change the pipewire-v4l2 translation layer.
2025-09-05 17:26:44 +02:00
Wim Taymans
f4ab704948 spa: use safe IO_Control parsing
The IO_Control areas are in shaed memory and need to use the parser to
safely extract the info.
2025-07-31 11:23:30 +02:00
Demi Marie Obenour
5bfc3e6b03 spa: v4l2: avoid integer wraparound to out of bounds read
I'm not sure if untrusted input can reach this point.
2025-07-15 10:13:17 +02:00
Demi Marie Obenour
b3bf5be1f6 *: Avoid macros that use casts where possible
Use direct field access when the type is known, instead of a macro that
includes a cast.

These were missed in e4fcbef89a.
2025-07-10 14:02:55 +00:00
Arun Raghavan
70aaec0ac4 spa: v4l2: Drop unused variable
Fixes warning:

[186/359] Compiling C object spa/plugins/v4l2/libspa-v4l2.so.p/v4l2-source.c.o
In file included from ../spa/plugins/v4l2/v4l2-source.c:164:
../spa/plugins/v4l2/v4l2-utils.c: In function ‘spa_v4l2_enum_format’:
../spa/plugins/v4l2/v4l2-utils.c:1103:22: warning: unused variable ‘drop_next’ [-Wunused-variable]
 1103 |                 bool drop_next = false;
      |                      ^~~~~~~~~
2025-07-03 10:07:54 -04:00
Michael Olbrich
41b831d0f8 spa: v4l2: add colorimetry support 2025-06-27 12:57:01 +00:00
Michael Olbrich
17c755714d v4l2: allow negotiation with modifier
This assumes that the modifier is always 'linear'. That is not quite
correct for all V4L2 formats. But PipeWire only uses input devices and
other modifiers are very unlikely.

This makes it possible to use DMABUFs with the GStreamer pipewiresrc.
2025-06-26 09:21:00 +00:00
Wim Taymans
f2452a6af7 spa: some more invoke -> locked calls 2025-05-29 10:17:16 +02:00
Robert Mader
e387772dc2 v4l2: Re-enable first buffer skip for jpeg types
The workaround is typically needed with usb-cameras using jpeg streams.
Re-enabling the skip shouldn't affect what the commit was trying to do,
i.e. fix encoded streams like h264 etc.

Fixes: bcf0c0cf8 (v4l2: only skip buffer for raw formats)
2025-05-17 10:53:08 +00:00
Wim Taymans
bcf0c0cf89 v4l2: only skip buffer for raw formats
We don't want to skip the first buffer for encoded formats because it
can contain the encoding parameters, in the h264 case, for example.
2025-04-24 10:57:49 +02:00
Barnabás Pőcze
a71cd05401 spa: v4l2: query_ext_ctrl_ioctl(): only check errno on failure
`errno` is not guaranteed to be cleared, so only check it if
the previous call actually failed.
2024-12-03 20:54:20 +01:00
Michael Olbrich
e76e057038 v4l2: ensure that the default frame rate is within the min/max bounds
Without this for continuous frame intervals, the default is set to 25
fps even if that is outside the min/max bounds (e.g. defined by the
peer).
Clip the default with the min/max bound to avoid this.
2024-11-27 11:46:26 +00:00
Michael Olbrich
669f53946e v4l2: handle min/max for continuous frame interval correctly
v4l2 frame intervals instead of frame rates, which is basically the
inverse. For the most part, this is already handled correctly and
numerator and denominator are swapped accordingly.

However, the minimim frame interval is the maximum frame rate so those
need to be swapped as well.

Without this, the minimum frame rate is larger than the maximum frame
rate for v4l2 devices that define a continuous frame interval.
2024-11-27 11:46:26 +00:00
Wim Taymans
41e35c7b17 v4l2: use dll to track rate difference
Track the difference between the configured and real framerate and use
that as the rate correction to adjust the next_nsec clock field.
2024-11-26 17:14:26 +01:00
Wim Taymans
b2dd733520 v4l2: Add mmap fallback when USERPTR is not supported
When we don't support EXPBUF according to the probe, the host will
allocate memory for us. We will then try to use USERPTR to import the
memory into v4l2.

If this is not supported, try to fall back to MMAP support, mmap the
buffers and memcpy the MMAP buffer to the host allocated buffers.
2024-11-07 15:55:38 +01:00
Wim Taymans
c4df4a0371 v4l2: improve debug a little 2024-11-07 15:55:38 +01:00
Wim Taymans
b9df297e2a v4l2: use a separate flag to skip the first buffer
The buf.sequence value might stay to 0 (v4l2loopback)
2024-11-07 15:55:38 +01:00
Wim Taymans
877c262e78 v4l2: probe number of buffers as well
Instead of just probing with 2 buffers, probe with the MAX_BUFFERS and
remember how many buffers we received.

Some drivers (v4l2loopback) work with less than MAX_BUFFERS (8) and we
need to set the max number of buffers in the Buffers param.
2024-11-07 15:55:33 +01:00
Wim Taymans
71f6d269db v4l2: first probe and then update port flags
The probe is to decide it we can use EXPBUF or not and this is used to
decide on the flags.

This got broken with 842ac9e110
2024-11-07 11:17:33 +01:00
Hans de Goede
0e63bd902f spa: v4l2: Fix v4l2 SPA not working on camera with relative controls
Some V4L2 controls like focus, pan or tilt can be relative (change
the lens / angle relative to previous positions) these return EACCES
when calling VIDIOC_G_CTRL on them.

Fix the v4l2 SPA not working on cameras with relative controls,
such as e.g. the Logitech QuickCam Orbit MP.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2024-09-16 19:31:32 +02:00
Wim Taymans
37eef2cf23 v4l2: queue dropped first buffer again
When we drop the first buffer to avoid timestamp problems, queue it
again in the driver or else we will not be able to dequeue is again
later and we will be running with a buffer less.
2024-09-04 16:57:07 +02:00
Sven Püschel
5932963506 spa: match camera property types with libcamera
The Pipewire libcamera spa plugin exposes multiple camera properties.
Unlike v4l2, libcamera usually exposes these as normalized floating
point values. But as the SPA_PROP types are based on v4l2, they are
currently set to integers.

This causes a problem when using pw-cli to change the properties,
as the spa_json_to_pod_part function casts the properties according
to their spa_type_info. Other software that doesn't depend on the
spa_type_info can correctly set the properties, as the values are
encoded in the spa_pod type and therefore also carry a type.

As the limited range from switching integers to floats is likely not a
problem, the affected spa properties were changed to the Float type.
This will cause pw-cli to also generate spa_pod values of type float
when setting v4l2 properties. Therefore the v4l2 spa plugin is also
adapted to allow floating point properties and cast these to integers.

Signed-off-by: Sven Püschel <s.pueschel@pengutronix.de>
2024-07-17 15:56:53 +00:00
Wim Taymans
853a46120e v4l2: Improve format and control enumeration
Use dynamic pod builder so that we can also build complex formats.

Make sure we zero the format before we parse it or else we end up with
potentially uninitialized values.

When ENUM_FRAMESIZES or VIDIOC_ENUM_FRAMEINTERVALS return EINVAL for the
first index, make a dummy result and continue with that. This will
trigger an intersect withe filter so that we end up with something valid
instead of nothing.

Handle 0 framerates without crashing.

See #4063
2024-07-09 14:14:42 +02:00
Wim Taymans
2c1ec7fa43 v4l2: use a dynamic pod builder to handle larger PropInfo
The labels from the vivid driver need more space so use a dynamic
builder to make sure we can handle them.

See #4063
2024-07-09 09:23:49 +02:00
Wim Taymans
fe5a29ac2a v4l2: improve logging a little
Add the device to the error log lines.
2024-06-11 12:15:05 +02:00
Wim Taymans
4d7c81bd90 v4l2: support meta_videotransform on buffers
Advertise support for the videotransform metadata.
Make a new meta.videotransform.transform property to configure the
desired video transformation in the metadata.

This makes it possible for a session manager or other rules to set
a custom transformation on the source.

See #4034
2024-06-07 10:54:46 +02:00
Wim Taymans
61af61a4b3 v4l2: handle empty properties gracefully
When a property is not found or is empty, go on to the next format
instead of failing.

See #3959
2024-04-10 11:06:44 +02:00
Robert Mader
1a2fee260b v4l2: Drop the first frame after camera startup
A quite big number of UVC cameras - due to firmware or kernel driver
issues - have bad timestamps of the first frame, confusing clients
like pipewiresrc.
Drop the first frame, as this seems to be the most reliable workaround
for the time being.

Closes https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/3910
2024-03-30 14:14:44 +01:00
Wim Taymans
842ac9e110 v4l2: set latency on port
Use the duration of one frame as the latency.

See #3910
2024-03-22 11:15:10 +01:00
Wim Taymans
858d4e9e1c buffer: add MAPPABLE data flag
Add a MAPPABLE data flag that hints that the fd in the data is mappable
with a simple mmap/munmap. Normally, DmaBuf is not mappable like that
unless explicitly indicated with this flag.

Set the MAPPABLE flag on the DmaBuf from v4l2 and libcamera fd.

When asked, mmap the buffer memory in all cases when the MAPPABLE
flag is set.

This solves the case where v4l2 has exported DmaBuf and is streaming to
node A and then node B links but doesn't get automatically mmaped
memory.

Fixes #3840
2024-02-08 18:09:08 +01:00
Wim Taymans
bbb83b97c5 v4l2: probe EXPBUF and disable alloc_buffer flag
After we set the format, probe if we can do EXPBUF and enable/disable
the ALLOC_BUFFERS flag on the port.

This should gracefully handle the case where EXPBUF is not available.

Fixes #3821
2024-01-29 16:38:52 +01:00
Wim Taymans
24f510b9b3 v4l2: clear support for alloc_buffers on failure
When we try to alloc buffers but EXPBUF is not supported, make sure to
clear the alloc_buffers flag so that the caller can try again with
allocated buffers instead.

See #3821
2024-01-29 16:10:36 +01:00
Wim Taymans
490558f3de v4l2: improve filter with missing format
When the filter has no format property, just enumerate all possible
framerates. Handle error case where the filter has the wrong type.

Makes gst-launch gstpipewiresrc ! video/x-raw ! fakesink work.

See #1793
2024-01-17 10:11:29 +01:00
Wim Taymans
14b265f646 v4l2: fix format enum with unsupported framerate
Actually count the number of frame fractions we add. If we added 0, we
don't have any supported framerate that intersects with the filter and we
try the next frame size.

See #1793
2024-01-17 09:55:47 +01:00
Wim Taymans
42a099381b v4l2: handle ENOTTY
Instead of erroring out, go to the next format when enumerating frame
size or frame interval.

See #3325
2023-07-04 11:36:37 +02:00
Wim Taymans
b59766c3d1 v4l2: add support fot G_CTRL
Get the current values of the controls and add those to the Props.

Emit a Props param changed event when we update properties.
2023-04-18 16:27:55 +02:00
Wim Taymans
a0457a3bdf camera: work around target_ mishandling
A driver node should use the target_duration and target_rate to adjust
the quantum and rate when the graph starts.

The camera nodes don't currently support any of this and simply enforce
a specific rate and duration for the graph clock. Mark this with a
FIXME. Otherwise, pipewire will complain that the node is ignoring the
configured graph rate.

We should really look at the graph target rate/quantum and only produce
a buffer when it is inside the current graph cycle. This would make it
possible to join audio and camera nodes and have them be in sync.
2023-03-27 09:59:21 +02:00
Barnabás Pőcze
934ab3036e treewide: use SPDX tags to specify copyright information
SPDX tags make the licensing information easy to understand and clear,
and they are machine parseable.

See https://spdx.dev for more information.
2023-02-16 10:54:48 +00:00
Wim Taymans
cfb226b0fb v4l2: improve debug
Add device path to debug lines.
2022-11-14 11:22:53 +01:00
Wim Taymans
ccf2891070 v4l2: fix buffer amount check
When we get buffers from the driver, check if we have at least as many
as we requested. If we have more, that's ok, we will simply not queue
them.

Previously we would ignore the input number of buffers and use the
allocated amount to fill up the buffer array, which might be too small
and then we crash.
2022-11-04 13:10:55 +01:00
Wim Taymans
74447acedb Add some more format checks
The format parse functions don't really check if the parsed values
make any sense so we need to to this ourselves.
2022-11-03 13:10:32 +01:00
Wim Taymans
71d58e6445 v4l2: implement setting controls 2022-10-31 11:33:45 +01:00
Wim Taymans
df094b3649 v4l2: recycle buffers when no io area
When there is no io area to place our buffers, recycle them immediately.
Otherwise we would run out of buffers and stall.
2022-10-24 13:16:08 +02:00
Wim Taymans
a57602cc4d v4l2: remove unused forward declaration 2022-10-24 13:15:42 +02:00
Wim Taymans
9b6e504c19 clean up some more array iterations 2022-10-03 09:20:42 +02:00
Michael Tretter
a745374232 v4l2: fix port param enumeration for devices without controls
It is valid for V4L2 devices to not implement any controls. QUERYCTRL
returns ENOTTY in these cases. Enumerating the controls must not fail in
these cases but return no controls.
2022-06-24 12:16:05 +00:00
Michael Tretter
edd41d8259 v4l2: close v4l2 device on error
The device needs to be closed when the enum_controls function is exited,
but is not closed if the VIDIOC_QUERYCTRL fails. Fix it.
2022-06-24 12:16:05 +00:00
Wim Taymans
7e4f99512f v4l2: cast varargs to the right type
The controls have 64 bits but the int varargs expect an int value, which
is not always 64 bits.

See #2400
2022-05-27 13:30:07 +02:00
Wim Taymans
2f5bba112f spa: Improve PropInfo fields
The PropInfo either has a registered id (and then also a name from the
type-info) or a custom name as a string.
In all cases, the description contains a free form text that clarifies
the property.

Use the description in the stream controls name.
2022-03-30 17:09:08 +02:00