Commit graph

854 commits

Author SHA1 Message Date
Barnabás Pőcze
5421f2689e pulse-server: module-zeroconf-publish: cache service TXT records
Keep the TXT records for each service,
and reuse them when publishing, if possible.
2021-12-28 23:37:53 +01:00
Barnabás Pőcze
422d69b471 pulse-server: module-zeroconf-publish: handle disconnection
Keep track of the created services in two lists: published, pending.
Move services between the lists as the avahi client's state changes:
keep services in the pending list until the avahi daemon appears on dbus,
move them to the pending list if connection is lost,
and re-publish them after reconnection.
2021-12-28 23:35:22 +01:00
Barnabás Pőcze
987c7fc1e4 pulse-server: manager: add mechanism to query object data
Add `pw_manager_object_get_data()` which can fetch linked
data by its id. And use it in the zeroconf-publish module.
2021-12-28 21:23:21 +01:00
Barnabás Pőcze
fd111da507 pulse-server: module-zeroconf-publish: do not allocate service name dynamically
The service name cannot exceed AVAHI_LABEL_MAX-1 characters in length,
so store it in the service struct instead of dynamically allocating it.
2021-12-28 21:23:21 +01:00
Barnabás Pőcze
d240d7a5ef pulse-server: module-zeroconf-publish: use fixed object data id
Use a fixed object data id for storing the service of each
pw_manager_object instead of always generating the
service name and using that.
2021-12-28 21:23:21 +01:00
Barnabás Pőcze
9769dee5e7 pulse-server: module-zeroconf-publish: avoid double-free
When a module's `load()` fails, its `unload()` will unconditionally
be called. Freeing resource in `load()` while not marking those
resources freed (e.g. setting the pointers to NULL) will result
in double-free when the module's `unload()` method is called.
2021-12-28 21:23:20 +01:00
Barnabás Pőcze
9a9fce66e0 pulse-server: module-zeroconf-publish: remove unnecessary member
The `key` member in `struct service` is not used. Remove it.
2021-12-28 21:23:20 +01:00
Barnabás Pőcze
748fe111f9 pulse-server: module-zeroconf-publish: remove unnecessary NULL check 2021-12-28 21:23:20 +01:00
Barnabás Pőcze
1c74079994 pulse-server: module-zeroconf-publish: rename functions
Rename `get_service()` to `create_service()`,
and `get_service_data()` to `fill_service_data()`.
2021-12-28 21:23:20 +01:00
Barnabás Pőcze
ce687d6518 pulse-server: module-zeroconf-publish: do not query twice
Whether the object is a sink or source is already queried at the
beginning of the function, and is kept in local variables.
Use those instead of calling `pw_manager_object_is_{sink,source}()` again.
2021-12-28 21:23:20 +01:00
Barnabás Pőcze
7b0af5940e pulse-server: module-null-sink: do not use client's core
Do not use the client's connection to create the adapter object,
instead, create a new connection. This avoids the need for setting
object.linger=true, which guarantees that when the pulse server goes
down, the null sink is cleaned up.
2021-12-28 19:53:27 +00:00
Barnabás Pőcze
b08da23715 pulse-server: module: do not schedule unloading multiple times
While it is not a problem since `module_free()` calls
`pw_work_queue_cancel()`, it is completely unnecessary
to do it more than once.

Introduce a new flag on the module which stores whether or
not an unloading has been scheduled.
2021-12-28 19:53:27 +00:00
Barnabás Pőcze
4fbe3cbfc6 pulse-server: module: use SPA_FOR_EACH_ELEMENT()
Since `module_list` is a fixed-sized array, `SPA_FOR_EACH_ELEMENT()`
can be used. So use that. This way there is no need for explicit
indexing nor a sentinel at the end.
2021-12-28 19:53:27 +00:00
Barnabás Pőcze
6aba315b82 pulse-server: always compile ROC modules
The module-roc-{sink,source} modules simply load the corresponding
native pipewire modules, they have no dependency on ROC.
So always compile them. This way these modules are
compile tested, and if the corresponding pipewire
modules are added to the system later, they will work
with no changes to the protocol-pulse module.
2021-12-28 19:53:27 +00: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
043934655a pulse-server: client: add 'disconnect' event
Have the clients emit a 'disconnect' event when they are
being disconnected.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
2d4febaba1 pulse-server: client: do not drop partially sent messages
If the first message has already been partially sent, do not
drop it when looking for redundant messages.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
4678ea06a0 pulse-server: client: restructure message handling
Move all I/O event source modifications into client.c.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
5ef9deae83 pulse-server: client: do not queue messages after disconnect
Do not queue messages to be delivered to a client if
that client has already disconnected.
2021-12-26 19:37:41 +00:00
Barnabás Pőcze
d939fa5b1c pulse-server: server: use spa_strstartswith() 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
6d2e6fde75 pulse-server: move parts of client creation
Move some portions of the client creation logic into client.c
so that it is easier to keep it in sync with `client_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
3de9d3df3b pulse-server: use safer spa_scnprintf
This clamps to the max size of the input buffer so that we don't write
the next item past the allocated space.
2021-12-22 21:34:29 +01:00
Wim Taymans
2905635de3 pulse-server: increase channel name length
, and AUX11 and the \0 need at least 8 chars, 6 chars will truncate
after AUX1 and leave an invalid channel map.
2021-12-22 21:15:46 +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
512a52d9c6 pulse-server: fix the number of enum values
Set the right number of enum values or else we might crash.

Fixes #1928
2021-12-20 09:13:37 +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
c8fc79bad2 Revert "pulse-server: only try to increase the tlength"
This reverts commit 7f0255f4ce.

Doesn't work with small quant for some reason.
2021-12-16 17:10:38 +01:00
Wim Taymans
7f0255f4ce pulse-server: only try to increase the tlength
PulseAudio only tries to increase tlength, never decrease so let's
do the same.
2021-12-16 17:08:23 +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
Wim Taymans
12cc3b34b1 pulse-server: implement fix_* fields
Use wildcards for the format/rate/channels when the fix flags are
set in the client.

See #1912
2021-12-16 12:24:59 +01:00
Wim Taymans
1f4254b344 pulse-server: track quantum and update tlength
Keep track of the current quantum and recalculate the tlength in the
same way that pulseaudio does.

Send a bufferattr changed message to a client when we change the
parameters.

This fixes the case where the quantum is increased and there needs to be
more buffering to keep the stream going.
2021-12-14 15:48:54 +01:00
Wim Taymans
6b81be5ac3 pulse-server: update the in_prebuf state
Move out of prebuf when we reach the prebuf amount of data. Go back to
prebuf when we run empty.
2021-12-14 15:47:19 +01:00
Wim Taymans
fd4ea442d9 pulse-server: increase buffer size
Because we keep everything in a ringbuffer and provide exactly the
required amount of data, we can use 1/4 buffers.
Also increase the buffer size. We don't want to limit the buffer size
to the negotiated tlength because it can be increased later. Instead
scale it to the max quantum size (8192) with a max resample rate of 32.
2021-12-14 15:42:19 +01:00
Wim Taymans
8caab44782 Revert "pulse-server: always ask for more data when underrun"
This reverts commit e1576c53d4.

This is not quite right, it seems to break orca and qemu audio.

See #1900
2021-12-14 10:27:01 +01:00
Wim Taymans
e1576c53d4 pulse-server: always ask for more data when underrun
Ask for more data when we are in underrun and not corked.

Fixes paplay and changing the quantum to something large.
2021-12-09 23:10:08 +01:00
Wim Taymans
7e5a0849f6 Revert "pulse-server: always asks for more data when underrun"
This reverts commit 1b94b66924.

It causes problems with qemu.

Without this patch, paplay --latency-msec=1 /some.wav hangs when
forcing the quantum to 8192. A different fix will be needed.
2021-12-09 16:54:47 +01:00
Wim Taymans
a8139b7acf pulse-server: handle prebuf state better
When we have a prebuf value, we start in the prebuf state.
We go back to prebuf when we have a prebuf value and an empty
ringbuffer.
2021-12-03 17:49:59 +01:00
Wim Taymans
16665d74d2 pulse-server: don't clamp missing bytes to tlength
When we increase the quantum past the tlength, we need to be able to
ask for more bytes than the tlength or else we will never exit this
underrun state. Look at the last required bytes and use that if it's
larger then tlength.
2021-12-03 16:43:16 +01:00
Wim Taymans
1b94b66924 pulse-server: always asks for more data when underrun
Even when prebuffer is active, we need to ask for more data to
resolve the underrun.
2021-12-03 16:41:41 +01:00
Wim Taymans
cc9fbe4375 pulse-server: add some debug 2021-11-30 09:22:58 +01:00
Wim Taymans
700a22b7f7 pulse-server: recover for underrun better
If we are underrun, don't update the read pointer and let the write
pointer catch up.
If we don't have enough data to fill a buffer, skip all the available
data and wait for the new request to arrive.

Fixes #1857
2021-11-29 20:22:05 +01:00
Wim Taymans
a0d7fb01ba pulse-server: add size check when reading strings 2021-11-25 10:14:30 +01:00
Wim Taymans
5d20e3028a pulse-server: avoid reading past the message size
We can't really get into this situation but it is a good idea to check
that we don't try to read past the message length.
2021-11-25 09:48:34 +01:00