Commit graph

4056 commits

Author SHA1 Message Date
Barnabás Pőcze
19d9bb7219 spa: libcamera: source: create eventfd before starting camera
An eventfd is used to signal the data loop from the libcamera request
completion event handler. Previously, this eventfd was created and
installed after the camera has been started and requests were queued.

This is problematic because it creates a small time frame where the
libcamera request completion handler will run in a state where the
eventfd is not fully set up.

Fix that by settup up the eventfd before the camera is started.

(cherry picked from commit e0e8bf083d)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
21a61b2194 spa: libcamera: source: generate camera config right away
Currently the plugin uses a single configuration during its entire lifetime.
So generate that configuration during initialization and hold on to it.

This makes the code a bit simpler, and also fixes issues stemming from missed
calls to `spa_libcamera_get_config()` as well as no error checking of
`libcamera::Camera::generateConfiguration()`.

(cherry picked from commit 49be2a1c52)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
1774249bdb spa: libcamera: source: remove SPA_PROP_device{,Name}
`SPA_PROP_deviceName` is an empty string and setting it does nothing.

`SPA_PROP_device` is always the libcamera identifier of the camera,
and setting it has no effect whatsoever in contrast to other plugins
such as v4l2.

So remove them both for now.

(cherry picked from commit 8c4f60af48)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
47aacb2a84 spa: libcamera: source: do not close fd
Currently the plugin does not support importing memory and uses
`libcamera::FrameBufferAllocator` to allocate memory. Every file
descriptor is managed by that object, so they must not be closed
manually.

(cherry picked from commit 429c0e03a3)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
192eab1fea spa: libcamera: source: remove unused enum_fmt member
This was introduced by b2c38a2b3b ("libcamera: work on rewrite"),
but it has never been used.

(cherry picked from commit c167b98ff2)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
a080a4a6f2 spa: libcamera: source: prop_id_to_control(): do range check first
If it is a custom spa property, it will not be found in the lookup
table, so we can return immediately.

(cherry picked from commit 30ce210c2a)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
b775acca60 spa: libcamera: source: fix mapping of libcamera::ColorSpace::TransferFunction::Linear
`SPA_VIDEO_TRANSFER_GAMMA10` should be used to represent a
linear transfer function.

Fixes: 7e202a3844 ("spa: libcamera: add colorimetry support")
(cherry picked from commit 07a4e593bb)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
db23d9fa04 spa: libcamera: source: simplify color space conversion
Instead of using an out parameter, just return the `spa_video_colorimetry`
object; and do the libcamera -> spa conversion in a single place, where
the data is actually added to the pod.

(cherry picked from commit 938195b19f)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
e770ed42c3 spa: libcamera: source: avoid iterator overrun when enumerating controls
Do not overrun the iterator when skipping the initial couple items.
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
c3fec9769c spa: libcamera: manager: fix id allocation
There is an issue in the id allocation mechanism which can result
in the different devices having the same id. Specifically, consider
the scenario where there are only two cameras, which have just been
added. In this case `impl::devices` looks like this:

   (0, camA) | (1, camB) | (?, nullptr) | ...

Now assume that `camA` is removed, after which the array appears
as follows:

  (1, camB) | (1, nullptr) | (?, nullptr) | ...

Then assume that a new camera appears. When `get_free_id()` runs,
when `i == 1`, it will observe that `devices[i].camera == nullptr`,
so it selects `1` as the id. Leading to the following:

  (1, camB) | (1, camC) | (?, nullptr) | ...

This is of course incorrect. The set of ids must be unique. When
wireplumber is faced with this situation it destroys the device
object for `camB` when `camC` is emitted.

Fix this by simply not moving elements in the `devices` array,
leaving everything where it is. In which case the array looks
like this:

  (nullptr) | (camB) | (nullptr) | ... // after `camA` removal
  (camC) | (camB) | (nullptr) | ... // after `camC` appearance

Note that `device::id` is removed, and the id is now derived from
the position in `impl::devices`.

(cherry picked from commit 2c2808fab1)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
0749851cfe spa: libcamera: use nullptr instead of NULL
(cherry picked from commit db3d91ebeb)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
a5ba04a8ba spa: libcamera: use C++ style casts
(cherry picked from commit 4fa11619a2)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
ea67bf1662 spa: libcamera: use anon ns instead of static
Move most things into anonymous namespaces for internal linkage
instead of using `static`. This shortes declarations and makes it
hard to forget.

(cherry picked from commit bb8223bff1)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
843dfe48b0 spa: libcamera: device: remove empty line
(cherry picked from commit cb71071d93)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
c22d9127e1 spa: libcamera: source: inline mmap_init()
The function has a single caller is essentially just a wrapper only
calling `mmap_init()`. So inline it into `spa_libcamera_alloc_buffers()`.

(cherry picked from commit e19a8bb5cd)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
5f77c729c2 spa: libcamera: source: set "corrupted" flag if applicable
If the libcamera `FrameMetadata` reports anything other than `FrameSuccess`,
then set `SPA_META_HEADER_FLAG_CORRUPTED`, notifying the application that
the frame may be unusable.

(cherry picked from commit 561a9d6ebb)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
7414d948ad spa: libcamera: source: use union for transferring control value
Use a union since only one member is active at a time, and use the
proper `libcamera::ControlType` enum to store the type instead of a
bare number. Also remove an unnecessary cast.

(cherry picked from commit 0022fc90b7)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
8181e2e051 spa: libcamera: source: simplify control mapping
Remove the `impl` parameter as it is not used, and use C++ range
based for loops.

(cherry picked from commit f94f4de6ff)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
bf1c57928b spa: libcamera: source: do not make expensive queries multiple times
`StreamFormats::pixelformats()` and `StreamFormats::sizes()` both
return newly created `std::vector`s, so do not call them multiple
times.

(cherry picked from commit 311b3cc37f)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
bdf904ebad spa: libcamera: source: simplify format lookup
Use range based for loops instead of indices.

(cherry picked from commit 489cc49937)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
d1eb5f6d20 spa: libcamera: source: use enum types
Use the appropriate enum types instead of bare `uint32_t`,
this provides better type safety in C++.

(cherry picked from commit 0ea7dc9f19)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
3dd413e131 spa: libcamera: source: handle camera acquire failure
Check the return value of `Camera::acquire()` and return the error
if that fails.

(cherry picked from commit f53ac8d57c)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
a233905f75 spa: libcamera: inline libcamera-utils.cpp
The file is not useful without `libcamera-source.cpp` because it
uses symbols only defined there. And being a non-self-contained
source file, it also breaks clangd. So move its contents directly
to `libcamera-source.cpp`. This makes the file about 2200 lines long,
but I feel that is still manageable (and it is by far not the longest).

(cherry picked from commit 1a1cf55efb)
2025-09-26 09:58:10 +02:00
Barnabás Pőcze
60d95e447a spa: libcamera: clean up includes
Remove some unnecessarily includes.

(cherry picked from commit 5a9cdd724f)
2025-09-19 05:35:47 +02:00
Barnabás Pőcze
de97a682f2 spa: libcamera: use lock when acquiring CameraManager
Make `libcamera_manager_acquire()` thread safe by locking a mutex
when the `CameraManager` instance is created and started.

(cherry picked from commit 5f4f4b5dd3)
2025-09-19 05:35:39 +02:00
Robert Mader
74bd520224 spa: libcamera: add colorimetry support
Libcamera equivalent to 41b831d0f ("spa: v4l2: add colorimetry support")

(cherry picked from commit 7e202a3844)
2025-09-19 05:35:23 +02:00
David Turner
8094cdf846 libcamera: Default to auto-focus & auto-exposure
libcamera says that cameras should default to manual focus mode.  This
means that unless pipewire clients specifically change this control,
users with an autofocus-capable camera are left with an out-of-focus
image.  This patch sets the autofocus mode to continuous and enables
auto-exposure (as the default for this is unspecified).

Testing with an imx708 on Raspberry Pi OS on a Raspberry Pi 4, before
this patch the image was generally out of focus in Firefox/webrtc, after
this patch autofocus works correctly.

(cherry picked from commit 3a0ffe21e6)
2025-09-19 05:34:37 +02:00
Wim Taymans
5212649ee1 control: fix event compare function
We can only compare UMP when both types are 2 or 4, so it must be
different from 2 *and* 4 to be rejected.

Fixes #4899
2025-09-17 10:29:15 +02:00
Pauli Virtanen
16b3c8a6f6 audioconvert: add log topic for resampler 2025-09-10 10:28:57 +02:00
Pauli Virtanen
e8d739fdd7 alsa: report extra latency for FireWire drivers
Based on testing, ALSA FireWire drivers introduce additional latency
determined by the buffer size.

Report that latency.

Pass device.bus to the node, so it can recognize firewire.
2025-09-10 10:28:42 +02:00
Pauli Virtanen
3cb2c93c48 alsa: force IRQ scheduling for firewire in pro-audio profile
FireWire ALSA driver latency is determined by the buffer size and not the
period. Timer-based scheduling is then not really useful on these devices as
the latency is fixed.

In pro-audio profile, enable IRQ scheduling unconditionally for these
devices, so that controlling the latency works properly.

See #4785
2025-09-10 10:28:34 +02:00
Pauli Virtanen
8077dabd3a alsa: set minimum period count before automatic period size
Some devices (FireWire) fail to produce audio if period count is < 3,
and also have small buffer size. When quantum is too large, we might
then get too few periods and broken sound.

Set minimum for the period count in ALSA, to determine the maximum
period size we can use. If smaller than what we were going to use, round
down to power-of-2.

See #4785
2025-09-10 10:28:27 +02:00
Barnabás Pőcze
2735baa4ce 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-10 10:28:08 +02:00
Wim Taymans
35dbea3c6c audiomixer: set change mask correctly 2025-09-10 10:20:34 +02:00
Wim Taymans
42eb28c0d2 audiomixer: format is Id 2025-09-10 10:20:18 +02:00
Wim Taymans
881d0e44d0 alsa: don't add MAX_LATENCY when using IRQ scheduling
The Max latency property only works for timer based scheduling so that
we don't select a quantum larger than we can handle in our buffer.

With IRQ based scheduling this does not make sense because we will
reconfigure the buffer completely when we change quantums and so the
currently selected buffer size does not limit the latency in any way.

Fixes #4877
2025-09-10 10:20:07 +02:00
Wim Taymans
ec1f3437e5 alsa: use 3 periods in IRQ mode by default
3 seems to work better as a default for Firewire. It does not actually
add latency because we only keep 1 period filled with data at all times.
2025-09-10 10:19:51 +02:00
Wim Taymans
124aa40f64 alsa: use 2 (or 3 for batch) periods in IRQ mode
Some drivers (Firewire) have a latency depending on the ALSA buffer size
instead of the period size.

In IRQ mode, we can safely use 2 (or 3 for batch devices) periods
because we always need to reconfigure the hardware when we want to
change the period and so we don't need to keep some headroom like we do
for timer based scheduling.

See #4785
2025-09-10 10:18:55 +02:00
Wim Taymans
0da4afca76 audiommixer: only clear mix_ops when initialized
It's possible that the mix_ops was not initialized and then the free
pointer is NULL, so check this instead of segfaulting.
2025-09-10 10:17:29 +02:00
alexdlm
ee0809ead9 Map Razer BlackShark v3 ACP 2025-09-10 10:12:37 +02:00
Pauli Virtanen
165bd9965b alsa: show correct value in api.alsa.period-num 2025-09-10 10:04:30 +02:00
Wim Taymans
fefea32d9f resample: optimize phase scaling
Precalculate the constant factor to avoid a division for each sample.
2025-07-23 14:51:25 +02:00
Wim Taymans
140ae1bb62 resample: fix compilation
Also fix a compiler warning in clang
2025-07-23 14:51:11 +02:00
Wim Taymans
f04e8164a6 resample: avoid calculating GCD in rate updates
We don't actually need to calculate the GCD for each resampler rate
update. The GCD is only used to scale the in/out rates when using the
full resampler and this we can cache and reuse when we did the setup.

The interpolating resampler can work perfectly fine with a GCD of 1 and
so we can just assume that.
2025-07-23 14:50:15 +02:00
Wim Taymans
4a22b53b74 resample: reorder resample function setup
We also don't need to copy the resampler function name with each dynamic
function update, this is just for debugging.
2025-07-23 14:50:12 +02:00
Wim Taymans
a95d6dd553 alsa: update resampler requested size before reading
spa_alsa_read is called from the source process function when we are a
follower and no buffer is ready yet.

Part of the rate correction was performed by the ALSA driver when it
woke up but now, the resampler has updated the requested size and we
need to requery it before we can start reading samples.

Otherwise, we end up with requested samples from before the rate update
and we might not give enough samples to the resampler. In that case, the
adapter will call us again and we will again try to produce a buffer
worth of the requested samples, which will xrun.
2025-07-23 14:50:05 +02:00
Niklas Carlsson
5aa46bbddd filter-graph: fix index off by one in dsp_delay_c
Checking w + 1 > n_buffer means that w will go to n_buffer, which
in turn leads to reading buffer[2 * n_buffer].
2025-07-22 15:54:48 +02:00
Wim Taymans
74ad61f094 alsa: update resample state in all cases
We need to manually recheck the resample state when the matching state
got updated.
2025-07-22 14:43:37 +02:00
Wim Taymans
83fa53ba6e alsa: don't try to activate resampling with unknown rates
If the driver or our rate is not known yet, don't assume we will need to
resample.
2025-07-17 10:21:25 +02:00
Pauli Virtanen
7cd256d14c bluez5: aac: fix for A2DP v1.4 using rfa bits for more channels
A2DP v1.4 uses the rfa bits for adding 5.1 and 7.1 configurations.
Clear those bits properly when sending configuration, in case remote
device sets them.

(cherry picked from commit ae7a893ce9)
2025-07-10 17:17:22 +03:00