Commit graph

582 commits

Author SHA1 Message Date
Wim Taymans
121c9cd3ab jack: use the CPU max_alignment
Instead of aligning the buffers to 16 bytes, use the CPU max_align
value (32 on intel).

Move the mix function from a static global variable to a per client
member because this could change per client.
2024-09-27 15:09:41 +02:00
Wim Taymans
0f08f54ef3 thread: make it possible to set a custom create function
Make a property to pass a custom function pointer to create threads
instead of pthread_create.

Use this in jack instead of bypassing the thread utils create function,
which gives the wrong thread rt priority with rtkit.

Fixes #4099
2024-08-07 10:46:52 +02:00
Wim Taymans
8d06e19100 jack: queue free of old mem in node_set_io as well
Avoid freeing the old io Position area before the data loop has managed
to get a pointer to it. Queue a free operation that will be executed
from the main loop after the data loop has the io area.

Fixes a crash when stressing jack clients to switch between drivers.
2024-06-18 12:45:09 +02:00
Wim Taymans
22d6065cda jack: don't call free_link from the data thread
We are not allowed to call free_link from the data thread because it
does free() and some pw_mem calls which should only be called from the
main thread.

To solve this, pause the core, queue a free_link operation on the data
thread, which will be scheduled after the previous remove_link operation
completes, free the link and then resume the core. Blocking and resuming
the core is necessary because we can't block for completion of the
invoke calls (the jack method is not allowed to block) and we must
ensure that nothing can happen with the memory (like reuse the mem_id)
before we have cleaned it up.

Fixes a crash in jack with create/destroy link stress.
2024-06-18 12:44:46 +02:00
Wim Taymans
b5cfbf058a jack: rename a function
Add/remove_link is more what this function does.
2024-06-18 12:39:11 +02:00
Wim Taymans
aa5625a470 jack: update the eventfd from the data loop
Also update the variables we use from the data loop.
2024-06-18 12:36:38 +02:00
Wim Taymans
412b379138 jack: copy events to right offset
The larger events need to be copied into the target buffer at the same
offset as the source buffer or else we overwrite the header and make
a corrupt buffer.
2024-05-16 09:30:04 +02:00
Barnabás Pőcze
8a4ff447d9 treewide: fix some format string issues
Use the proper specifier, and cast to a known type where the type
is not guaranteed by any standard.

See #3975
2024-04-29 16:19:15 +02:00
Wim Taymans
6e710ea39b jack: set the output buffer size correctly
The output buffer size is always the current cycle buffer_size.
The size that is give by the JACK API is only used to restrict the
number of mixdown samples or midi offsets.

Fixes #3892
2024-03-12 17:44:46 +01:00
Wim Taymans
b3c7dda96a jack: fix multiple midi input ports
On the midi input ports, do the same trick as on the output ports:
first convert the midi to JACK and then copy the whole buffer to the
port specific storage.

This makes it possible to have a different midi buffer per port and
allow multiple threads to get the buffer concurrently.

Fixes #3901
2024-03-11 09:16:33 +01:00
Wim Taymans
0b75a3bed7 jack: FORCE_RATE should just contain the rate 2024-03-01 13:53:15 +01:00
Wim Taymans
6242dab47c jack: set global_mix safely
Keep track of the active number of mixer ports and update the global mix
io in sync with the data thread because that is where we will check the
state of the global mix io.

This is mostly important for output ports. When removing all links from
an output port, we first will clear all the mixer io and then remove the
global mixer with client_node_port_set_mix_info(). If we don't clear the
io before that, the data thread will be using that buffers as they are
cleared.

See !1915
2024-03-01 13:52:31 +01:00
Wim Taymans
5d965e1d68 jack: sync threads by pausing the core
When we clear the port io, pause the core until the invoke call
completed. This way we don't start processing other messages until we
have safely removed the port io.

Normally, when clearing a link on a port, first the mix io will be set
to NULL and then the format will be cleared, which clears the buffers
as well. By delaying the processing of the format clear until the io
is removed from the data thread we avoid taking away the buffer memory
from the processing thread prematurely.

When creating a link, first the format and buffers are configured and
then the io is set, which should be safe in all cases.

See !1915
2024-03-01 13:52:23 +01:00
Wim Taymans
f5c85478a7 meson: add options to set server and client RT priority
Make a rtprio-server and rtprio-client option. Leave the server
priority by default to 88 but lower client priority to 83. JACK
does something similar by setting clients to rtprio-server - 5.

Make module-rt use the client priority by default and bump the server
priority explicitly in the config file.

Leave the pulse-server to the default rtprio-client, there is no reason
to lower this any further because it is really just a regular client.

Bump the ffado packetizer thread to rtprio-server + 5 because that is
also what JACK does.

88 is still much higher than the value of 60 that JACK uses in
Fedora but now this is at least configurable.
2024-02-16 10:00:45 +01:00
Dimitrios Katsaros
3d5b9ce3e8 Jack: Added missing lock to jack API call 2024-02-12 10:48:58 +01:00
Dimitrios Katsaros
9cb234dda9 Jack: Fix jack_remove_property(ies) to use proxy id
These functions were using the object serial as the object id
2024-02-12 10:48:08 +01:00
Wim Taymans
1c9c3e3553 jack: fix version check 2024-01-31 10:37:09 +01:00
Wim Taymans
13511e9295 jack: improve running check
When we get a node info about our own state, we can use the active state
of the node to decide if we are running or not.

Otherwise, we will try to hide ports from JACK clients that suspend
(while still active).

See #3794
2024-01-25 12:02:01 +01:00
Wim Taymans
8d5e7749c2 jack: handle -ENOENT from the core
This means that we tried to do something on a proxy that was destroyed
on the server and we should just ignore the error.
2023-12-14 13:12:44 +01:00
Wim Taymans
2a29581b2a jack: improve current_usec calculation
We should not use nsec from the clock as the current_usec because
current_usec is supposed to be an idealized time when the wakeup would
have happened and nsec is when it actually happened.

Instead use next_nsec and subtract the rate corrected period from it to
get the idealized current_usec.

We can still use nsec to calculate the elapsed time since the wakeup and
be sure that it is always in the past.
2023-12-14 13:09:10 +01:00
Wim Taymans
3376b96f2a jack: handle concurrent jack_port_get_buffer()
Ardour calls jack_port_get_buffer() from multiple threads. For audio
buffers this will result in mixing the input samples into the target
buffer and there is no window for having a corrupt buffer.

For MIDI, there is a problem because we need to convert from and to
PipeWire MIDI and while we do that from multiple threads, the midi
buffer can be incomplete or corrupt.

Fix this by building the intermediate POD to a thread-local scratch area
and then copy it to the target buffer. If this is done from multiple
threads there is no moment where incomplete data can be seen in the
target buffer.

Make the complete midi-scratch buffer thread_local so that we also avoid
a race when converting midi data from a foreign port.

When we write into the scratch buffer when mixing and converting input
midi data, we also avoid a race there.

Fixes #3632
2023-11-22 16:16:30 +01:00
Wim Taymans
f324f0e8e1 jack: optimize one buffer case
The normal case is using 1 buffer port port so we can avoid a
queue/dequeue pair of operations.
2023-11-22 13:20:52 +01:00
Wim Taymans
e8c6c78982 jack: fix crash with fastpath debug
Don't deref p in the trace log because it can be NULL.
Don't try to dequeue a buffer when there are none. Improve some debug.

See #3632
2023-11-21 17:09:08 +01:00
Wim Taymans
b3ee9942f6 jack: enunerate Latency only for DSP ports
We don't really include the other ports in the API.
2023-11-20 18:17:31 +01:00
Wim Taymans
0fe7bd2780 jack: improve transport and times handling
Avoid reading from the activation directly to get cycle times but copy
the relevant fields of the clock when the cycle starts. Use the unique id
to get a consistent copy of the data.

This avoids some stay frames and values in jack_showtime.
2023-11-20 17:20:39 +01:00
Wim Taymans
935093e4a2 jack: fix compilation with -UFASTPATH 2023-11-15 09:28:08 +01:00
Wim Taymans
e2598b3242 jack: Add jack_port_t* <-> object helpers
Improve get_buffer debug.
Add some extra checks for the type before we try to use the object as a
port.
Set latency range to 0 when called with wrong port. Ardour seems to call
into us with ports from a previous jack_client..
2023-11-10 12:07:52 +01:00
Wim Taymans
ee811307cd jack: make 0 an invalid object type
So that we don't accidentally think uninitialized objects are ports.
2023-11-10 11:26:06 +01:00
Wim Taymans
ea412a5628 jack: fix midi events from peer port
When we get a midi buffer directly from one of our peer ports, we need
to convert it to a jack midi buffer.

Note that this previously returned NULL because of the size check of the
midi buffer, which was likely much smaller than the frames argument.

This fixes midi event recording preview in ardour.
2023-11-08 12:59:33 +01:00
Théo Lebrun
0e44f5ef97 jack: fix PW_VERSION_* constant used with pw_*_events structs
PW_VERSION_NODE is 3 while PW_VERSION_NODE_EVENTS is 0, I am unsure if
it could have caused issues. Same thing with PW_VERSION_PORT &
PW_VERSION_PORT_EVENTS.

I have not seen code checking this version number.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
2023-11-02 12:42:30 +01:00
Wim Taymans
b8a8e00a80 jack: do sync when starting client
do a sync when starting up client and sync when creating a new proxy so
that we get all the proxy updates before the client is opened.

This ensures all ports are enumerated correctly.

Fixes #3618
2023-11-01 18:42:12 +01:00
Wim Taymans
733cf7835e jack: add jack_set_sample_rate() extension 2023-10-24 11:08:49 +02:00
Wim Taymans
b92b66cf5b jack: refactor function to get buffer data
Split out the function to get the buffer data.
Don't process mix with invalid id.
Check global_mix and io before processing the mix.
2023-10-19 16:06:14 +02:00
Wim Taymans
6ef8d31f55 jack: only set io in NEED_DATA after complete
Don't set the port io to NEED_DATA in prepare_output because this
might be called when we get the data from an input port that is linked
to out output port.

See #3514
2023-10-19 12:29:00 +02:00
Wim Taymans
13f52f4297 jack: don't require global-mix in prepare_output
We can directly use the port io, which is always available. This ensures
the mix->io status is set to NEED_DATA even when there is no global
mix.

This reverts part of 56786aedc4

See #3514
2023-10-19 12:20:58 +02:00
Wim Taymans
69d0f586b2 jack: use a separate thread for notify
Some jack clients will take a lock before doing IPC and then will
take the same lock in a notify callback. This prevents the IPC from
progressing and causes a deadlock.

Make a separate thread to dispatch the notify so that we don't block the
IPC in any case.

Fixes #3585
2023-10-17 13:05:19 +02:00
Wim Taymans
40cc2f7068 jack: don't wait for mix_set_io
Some jack clients like to lock the process function and so we can't be
sure we will deadlock while we try to wait for the data-loop.

Instead, don't sync with the data-loop when setting the mix-io, it will
happen later when it is possible. Queue a free operation of the memmap
after the mix_set_io if we need to clean up.

See #3585
2023-10-17 13:01:53 +02:00
Wim Taymans
7d5b809b19 jack: make option to make input writable 2023-10-16 11:40:03 +02:00
Wim Taymans
8f2ee0a29c Revert "jack: use a private writable mapping on input"
This reverts commit 6fefd49a8a.

We can't use PRIVATE because mmap docs say that we then might not see
changes in the data anymore from other processes.

Fixes #3575
2023-10-16 09:36:16 +02:00
Wim Taymans
6fefd49a8a jack: use a private writable mapping on input
See #3571
2023-10-14 12:23:39 +02:00
Wim Taymans
11320cf203 tweak number of buffers
In most cases we can use just 1 buffer.

The alsa-pcm-source needs at least 2 buffers so increment the min
limit.
2023-10-13 14:00:29 +02:00
Wim Taymans
6eb17393bf jack: remove fixed buffer size limit
Use the quantum_limit as the max buffer frames and allocate the ports
with the right samples at the end.
2023-10-13 13:04:28 +02:00
Wim Taymans
bafa890aef jack: schedule all jack clients in the same group
Always group the jack clients together to avoid them using different
drivers (and transport/timing).

See #3562
2023-10-13 09:43:00 +02:00
Wim Taymans
45bab72abc jack: fix midi events in destination buffer
Don't fix up the midi events in the (read only) input buffer but
after we have converted it into the JACK midi buffer.

See #3560
2023-10-11 10:56:19 +02:00
Wim Taymans
fcde479a82 env: PIPEWIRE_QUANTUM now uses FORCE_RATE and FORCE_QUANTUM
Make it force a RATE and QUANTUM on the graph. The non-force options
can already be done with PIPEWIRE_RATE and PIPEWIRE_LATENCY.
2023-10-10 15:10:19 +02:00
Wim Taymans
4568d90565 jack: sync after setting metadata
Do a do_sync after setting the metadata to ensure the messages are
flushed to the server and processed. Fixes an issue where jack_property
would exit before the messages are flushed and so nothing happens.
2023-09-19 12:44:59 +02:00
Wim Taymans
bb10a7cb98 jack: setting the Latency to NULL clears the latency 2023-09-15 11:37:58 +02:00
Jan Alexander Steffens (heftig)
fa7cd96b6e pipewire-jack: Disable LD_LIBRARY_PATH when libjack is in libdir
This avoids prepending /usr/lib to the LD_LIBRARY_PATH, which supersedes
DT_RUNPATH and can cause library confusion.
2023-09-15 08:49:36 +00:00
Jan Alexander Steffens (heftig)
43db03909b ci: ShellCheck preprocessed scripts separately
Change the shellcheck job so that we configure the build and check the
preprocessed versions of the scripts, not the bare ones, which might not
be syntactically valid yet.
2023-09-15 08:49:36 +00:00
Wim Taymans
6abc6e6693 jack: mark client active a little earlier
So that we get all the notifications queued and emitted when we complete
the activation.
2023-09-13 17:48:28 +02:00