Commit graph

416 commits

Author SHA1 Message Date
Barnabás Pőcze
15e7a61aa7 treewide: only define feature macros when the feature is available
Most feature checks already use #ifdef, and do not care about
the value of the macro. Convert all feature checks to do that,
and simplify the meson build scripts by replacing

  if cond
    cdata.set('X', 1)
  endif

with

  cdata.set('X', cond)
2022-02-04 00:15:59 +01:00
Barnabás Pőcze
e63dee6075 pulse-server: check reference count of sample when freeing it
When the pulse server context is being freed, only a single reference
should be alive to each sample because the pending samples
are all destroyed beforehand.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
f181210b29 pulse-server: properly unload modules
Instead of simply calling `module_free()`, actually unload
the modules when the pulse server context is being freed.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
a79bb60754 pulse-server: clean up everything when the context is destroyed
Clean everything up when the context is destroyed to avoid
problems stemming from the destruction order of objects
in the context.

Furthermore, free messages after all clients have been freed
because `client_free()` may very well call `message_free()`
which would lead to memory leaks if `impl->free_messages` were
processed first.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
1bf00720e3 pulse-server: do not manage client lifetime during stream cleanup
A stream's lifetime is tied to that of the client, it cannot outlive
the client object. Streams also do not increase the client's reference
count. It is not possible for a stream to exist after the corresponding
client has been destroyed.

Hence it is not necessary to manage the client's reference count
or lifetime in `do_destroy_stream()`. All works related to a particular
stream are cancelled in `stream_free()`, which is called
before the client is destroyed.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
194c0f9c99 pulse-server: stream: remove done flag
When the `done` flag was first added in
9f9be7d7f2, it was
actually needed because cleanup was implemented
using a per-client eventfd which was signalled when
something related to the particular client needed
to be freed. The function that ran, then, checked
each stream's `done` flag, and freed them as necessary.

However, since c70a5de526,
the stream cleanup is done using a work queue, and as
a consequence, the `done` flag is no longer needed.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
4426da6a62 pulse-server: client: remove disconnecting flag
When the `disconnecting` flag was first added in
d44fdabea1, a client's
streams were not explicitly destroyed when it was freed,
instead, only the client's pw_core was disconnected,
which then caused all related streams to be destroyed.

Thus there needed to be a way to determine why the stream's
state changed to PW_STREAM_STATE_UNCONNECTED as it may have
been because the client is disconnecting or because the
stream has been "killed" in some other way.

The `COMMAND_{PLAYBACK,RECORD}_STREAM_KILLED` command
only needed to be sent to the client in the second case.

However, at the moment, all streams of a client are explicitly
destroyed before it's pw_core is disconnected. This makes the
`disconnecting` flag unnecessary, thus it can be removed.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
f5e561c2fe pulse-server: module: remove unused client argument
None of the modules use the `client` argument in their
unload callbacks. Remove it.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
bc2914b3e9 pulse-server: pending-sample: move reference counting
Move the reference counting from `pending_sample_free()`
into `on_sample_done()` so that the client's references
are managed in a single place.

The reason why `pending_sample_free()` cannot simply call
`client_unref()` is that `client_free()` may be called from
`manager_disconnect()` regardless of the reference count,
and, in turn, `pending_sample_free()` may be called,
which could then lead to a recursive call to `client_free()`.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
29a288ebae pulse-server: pending-sample: remove done flag
The `done` member of the `pending_sample` struct is only ever
set, but never read. Remove it.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
9447dbe4f8 pulse-server: remove unnecessary include 2022-02-02 15:40:43 +00:00
Barnabás Pőcze
5db2c6cc32 pulse-server: simplify a condition
`a || (!a && b)` equals `a || b` due to the
short-circuiting nature of `||` and `&&`.
2022-02-02 15:40:43 +00:00
Barnabás Pőcze
6d0075b4e9 pulse-server: client: cancel related work queue jobs 2022-02-02 15:40:43 +00:00
Wim Taymans
6543899fe6 pulse-server: implement stream_buffer_attr 2022-01-28 17:34:36 +01:00
Wim Taymans
1ac1f914e3 pulse-server: allows allocate MAXLENGTH for the ringbuffer
See #2069
2022-01-28 16:21:03 +01:00
Wim Taymans
db2719f1f4 pulse-server: always advance the read pointer in underrun
We should always advance the read pointer when we are underrun and
not corked. This will request more bytes from the client to make
things advance.

Fixes #2041
2022-01-25 15:21:31 +01:00
Wim Taymans
b9c8f0f6b9 pulse-server: don't complete drain when corked
A corked stream should not complete a pending drain operation.
2022-01-25 15:20:27 +01:00
Wim Taymans
4f57f3cdae pulse-server: allow per-application latency quirks
Make it possible to set latency and buffer parameters in the matching
rules.

Add a rule for speech-dispatcher and force it to some larger latency.
2022-01-22 16:17:09 +01:00
Wim Taymans
7f08cadca4 pulse-server: move quirks to config section
Make a pulse.rules section in the config file with match rules. Add
support for setting custom client properties and quirks.
2022-01-22 11:51:11 +01:00
Wim Taymans
9935f921c5 pulse-server: use TARGET_OBJECT with the target serial
Use a new TARGET_OBJECT property to ask to be linked to a target by name
or serial. This should deprecate NODE_TARGET later.
2022-01-19 17:09:55 +01:00
Wim Taymans
1d03923a97 pulse-server: use object serial as index
Use the lower 32 bits of the object serial as the index. When there is
an overflow, use an invalid index, which will probably result in a
protocol error.
2022-01-17 12:48:31 +01:00
Wim Taymans
0904a35ba8 pulse-server: make separate index
Separate the id (the pipewire object id) from the index (what we send to
the client to identify the objects).
2022-01-17 12:01:13 +01:00
Wim Taymans
955e4287ab pulse-server: index flags are only used for modules
one flag for the internal extension modules, another flag for the
modules we load in pulse server that don't really have a matching
pipewire module.
2022-01-17 10:28:09 +01:00
Wim Taymans
6a2aea8e0a pulse-server: remove the MONITOR flag
PulseAudio has separate namespaces for sources and sinks so we can reuse
the same id for the sink and its monitor.
2022-01-17 10:25:26 +01:00
Wim Taymans
5a315d5351 pulse-server: rename some id -> index
It's port_index and profile_index.
2022-01-17 09:49:39 +01:00
Wim Taymans
7c6649b5e5 pulse-server: use quantum_limit
Don't use max_quantum as the upper quantum limit, this is now scaled
with the rate. Use quantum_limit instead. We don't really care about the
max_quantum anymore so get rid of the field.
2022-01-14 16:45:41 +01:00
Wim Taymans
6394537dbd pulse-server: remove unused field
And it's not quite useful to debug the value either.
2022-01-14 10:41:11 +01:00
Wim Taymans
776b52749f Use configured quantum_limit instead of hardcoded value
Parse the quantum_limit parameters and use this to scale the buffers so
that they can contain the maximum allowed samples instead of the
hardcoded 8192 value.

See #1931
2022-01-12 17:50:12 +01:00
Wim Taymans
982758ffed pulse-server: calculate missing bytes differently
Based on patch from Barnabás Pőcze <pobrn@protonmail.com>

Instead of trying to keep track of the missing bytes ourselves, use the
simple tlength - avail - requested formula to request more bytes from
the client.

Fixes #1981
2022-01-10 15:08:52 +01:00
Wim Taymans
ab425d44b8 pulse-server: after uncork, ask for missing data
When we uncork, ask for any missing data immediately so that we don't
underrun in the next process cycle.

See #1981
2022-01-07 10:41:10 +01:00
Pauli Virtanen
9eddd0474f pulse-server: show monitor sources with device.class=monitor
Pulseaudio shows monitors with device.class=monitor, so we follow.
2022-01-06 20:32:53 +00:00
Wim Taymans
c46113faa3 json: spa_json_get_string() writes up to len chars or fail 2022-01-04 10:42:32 +01:00
Wim Taymans
13c0cbb899 pulse-server: remove unused defines 2022-01-04 10:08:54 +01:00
Wim Taymans
35cbe4e939 buffers: make alignment optional
Make the alignment parameter optional when negotiating buffers.
Default to a 16 bytes alignment and adjust for the max cpu
alignment.
Remove the useless align buffer parameter in plugins, we always
set it to 16 anyway.
2022-01-03 12:32:26 +01:00
Barnabás Pőcze
e35b9828d3 pulse-server: make GET_SERVER_INFO accessible without manager
For example, pulseaudio.js[1] immediately sends a
GET_SERVER_INFO request after AUTH, and only later
issues a SET_CLIENT_NAME.

See #1966.

[1]: https://github.com/janakj/pulseaudio.js
2021-12-31 02:12:03 +01:00
Barnabás Pőcze
1c3e45a584 pulse-server: add command access control
By default require that a client is authenticated and
has a manager to be allowed to run a command.

Specially:
 * AUTH requires nothing
 * SET_CLIENT_NAME and STAT only require authentication
2021-12-31 02:11:59 +01:00
Barnabás Pőcze
92abc7b7ad pulse-server: store whether the client has been authenticated
Add a new flag to the client struct which saves whether or not
the client has successfully authenticated itself.
2021-12-30 21:24:06 +01:00
Barnabás Pőcze
705ac662e3 pulse-server: use macro to generate command entries 2021-12-30 21:22:19 +01:00
Wim Taymans
2f742bb56c Revert "pulse-server: use partial data as missing/played for"
This reverts commit 08b18b9da4.

This was likely caused by a bug in mpv

Fixes #1956
2021-12-29 19:49:15 +01:00
Barnabás Pőcze
0753e992b8 pulse-server: improve reference counting of samples
Previously, when a sample is "committed" from an upload stream,
its reference count is set to 1. This is problematic if,
when the sample is committed for a second time, there are
streams playing the sample as the reference count will go out
of sync.

The problem can be easily triggered, especially with longer samples:

   pactl upload-sample a-long-sample.ogg
   pactl play-sample a-long-sample
   pactl play-sample a-long-sample
   pactl upload-sample a-long-sample.ogg  # while playing

When the first stream finishes playing, it will free the sample,
which can cause problems e.g. for the second stream playing the
sample at that very moment.

Fix that by decoupling the buffer from the sample and making
the buffer reference counted. And remove the reference counting
from the samples as it is no longer needed.

Futhermore, previously, the reference counts were ignored
when the removal of a sample was requested. That is fixed
as well.

The previous issue can be triggered easily as well:

  pactl upload-sample a-long-sample.ogg
  pactl play-sample a-long-sample
  pactl remove-sample a-long-sample      # while playing

Fixes #1953
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
0e75b3fa0f pulse-server: handle if the module is destroyed while loading
Create a new event for modules ('destroy') which is emitted from
`module_free()`. It is used by the module loading logic, to handle
when a module is destroyed without properly loading first.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
bd5a715200 pulse-server: rework module loading
Store the modules whose load has been initiated by a particular
client in the `pending_modules` list of the client. When the
client disconnects, "detach" the client from the pending module
objects. This way the reference count need not be increased
for asynchronous module loads.

Furthermore, if the module can load synchronously, do not create
the pending module object at all.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
6f412236d5 pulse-server: stream: only remove from list if pending
Only call `spa_list_remove()` in `stream_free()` if the
stream is pending. `spa_list_remove()` does not reinitialize
the list node, therefore calling `spa_list_remove()` again
after the stream has been removed from the pending list
will corrupt the pending list of the client.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
cc12188763 pulse-server: move parts of stream creation
Move some portions of the client creation logic into stream.c
so that it is easier to keep it in sync with `stream_free()`, etc.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
9c218b2d08 pulse-server: remove unnecessary NULL checks
`free()` and `pw_properties_free()` already include a NULL
check before proceeding.
2021-12-26 19:37:41 +00:00
Wim Taymans
08b18b9da4 pulse-server: use partial data as missing/played for
When we can't fill a complete block, report the amount of data that we
used in missing/played instead of the complete missing part.

Fixes audio breaking up when looping in mpv.

Fixes #1132
2021-12-23 11:38:54 +01:00
Wim Taymans
3c66d46007 pulse-server: recalculate tlength on quantum change
Always reevaluate the tlength or total buffered samples when the
quantum changes, even for the first sample because it is possible that
we completely miscalculated the length when we started, like when the
quantum is force high and the requested latency is low.

Also only increase the calculated tlength, for smaller sizes we don't
need to do anything, we can keep the latency as is it.

See #1930
2021-12-20 17:33:55 +01:00
Wim Taymans
d2f676f9a6 pulser-server: push data instead of silence
When we are draining or underrunning, read whatever we have in the
ringbuffer instead of silence. This places the last samples before
the drain into the sink, padded with 0.

Fixes #1549
2021-12-18 12:32:44 +01:00
Wim Taymans
b223261240 pulse-server: flush after we push the last buffer 2021-12-18 12:32:20 +01:00
Wim Taymans
de12e8dd2c pulse-server: improve fix_* handling
When we have a fix_* flag set, make an extra format description with the
wildcards. This makes it possible for the session manager to fall back
to something when selecting a target and format.

Also only advertize the valid pulseaudio formats for the wildcards.

Fixes #1912
2021-12-16 12:56:19 +01:00