Commit graph

54 commits

Author SHA1 Message Date
Pauli Virtanen
29a6552cc9 bluez5: take fractional resampler delay into account for reference time
Improve sample reference time accuracy by including the fractional part
of the resampler delay.
2025-01-19 17:11:13 +02:00
Pauli Virtanen
b952cfbe38 bluez5: remove resampler delay fudge factor
The resampler delay was off by one sample, so remove the corresponding
fudge factor here.  This matters for BAP output synchronization.

The resampler has also some fractional delay, so there can still be
sub-sample offset between the original and resampled timelines.  This is
not currently taken into account.
2025-01-13 19:05:58 +02:00
Sanchayan Maity
6a5f2bbd1f bluez5: media-sink: Log need_flush in flush_buffer 2025-01-09 14:51:50 +00:00
Pauli Virtanen
2d30ab94c2 bluez5: account for codec internal delay in latency values
Encoders and some decoders have additional internal latency that needs
to be accounted for.

This mostly matters for AAC (~40ms), as the other BT codecs have much
lower delays (~5ms).
2024-12-07 18:28:17 +00:00
Pauli Virtanen
6b6e9c4ea9 bluez5: make node.group valid JSON
The node.group property is parsed as JSON, and as it here contains ":"
it needs to be quoted accordingly. Fixes pw_strv_parse errors in logs.
2024-06-21 13:29:30 +03:00
Wim Taymans
1ae4374ccf Fix compilation with -Werror=float-conversion
Better make the conversions explicit so that we don't get any surprises.

Fixes #4065
2024-06-18 12:17:56 +02:00
Pauli Virtanen
cd166ac899 bluez5: don't use spa_invoke from data loop to main loop
spa_loop_invoke from data loop to main loop is not OK, as Wireplumber
currently runs its main loop with "pw_loop_enter(); pw_loop_iterate();
pw_loop_leave();" which causes the loop to be entered only when it is
processing an event.

In this case, part of the time the loop impl->thread==0, and calling
spa_loop_invoke() at such time causes the callback to be run from the
current thread, ie. in this case data loop which must not happen here.

Fix this by using eventfd instead, which is safe as the callback always
runs from the main loop.

Eventfd is also slightly more natural here, as multiple events will
group to the same mainloop cycle.
2024-04-13 15:54:22 +03:00
Pauli Virtanen
a6dcdfae0c bluez5: iso-io: track and apply corrections to tx latency
Use TX timestamps to get accurate reading of queue length and latency on
kernel + controller side.

This is new kernel BT feature, so requires kernel with the necessary
patches, available currently only in bluetooth-next/master branch.
Enabling Poll Errqueue kernel experimental Bluetooth feature is also
required for this.

Use the latency information to mitigate controller issues where ISO
streams are desynchronized due to tx problems or spontaneously when some
packets that should have been sent are left sitting in the queue, and
transmission is off by a multiple of the ISO interval.  This state is
visible in the latency information, so if we see streams in a group have
persistently different latencies, drop packets to resynchronize them.

Also make corrections if the kernel/controller queues get too long, so
that we don't have too big latency there.

Since BlueZ watches the same socket for errors, and TX timestamps arrive
via the socket error queue, we need to set BT_POLL_ERRQUEUE in addition
to SO_TIMESTAMPING so that BlueZ doesn't think TX timestamps are errors.

Link: https://github.com/bluez/bluez/issues/515
Link: https://lore.kernel.org/linux-bluetooth/cover.1710440392.git.pav@iki.fi/
Link: https://lore.kernel.org/linux-bluetooth/f57e065bb571d633f811610d273711c7047af335.1712499936.git.pav@iki.fi/
2024-04-12 18:50:15 +03:00
Pauli Virtanen
61c585c8e6 bluez5: support Google's Opus A2DP vendor codec 2024-02-04 12:58:40 +02:00
Pauli Virtanen
c0eb634156 bluez5: media-sink: more accurate latency
Don't include the quantum in latency: the latency relative to graph
cycle start doesn't depend on the quantum.  Instead, the audio packet
size determines it.
2024-01-28 18:00:04 +02:00
Pauli Virtanen
95b13391eb bluez5: media-sink: fix wrong clock
Should use position for graph clock rate.
2024-01-27 18:04:57 +02:00
Pauli Virtanen
eaea03c26c spa: export log topic enumerations 2024-01-04 10:02:55 +00:00
Pauli Virtanen
92f0815e4d bluez5: media-sink: keep one more buffer free
We delay the audio a bit to keep packet intervals equal, which keeps
some data in buffers.

In theory the calculation keeps one buffer free, but it doesn't
explicitly keep "extra" buffer space so in theory might flush too late
and next process() might not have free buffers.  However, as we encode
next packet right away this shouldn't really occur...

Try to keep one extra spare buffer free so that the flush time is
certainly early enough.
2023-12-05 18:37:36 +02:00
Pauli Virtanen
a9ff282501 bluez5: drop queued data on node stop
Clear queued buffers when stopping consuming, to ensure that all buffers
are usable when we start again.

Do for A2DP as we already do for SCO.
2023-12-05 18:37:36 +02:00
Pauli Virtanen
81aaf0e204 bluez5: set io status properly when sink is not going to consume
When sink is not ready to consume buffers, set io and status to indicate
it already has data.
2023-12-05 18:37:36 +02:00
Pauli Virtanen
de9086481f bluez5: media-sink: set node.group for ISO group nodes
It's better to schedule nodes in the same ISO group together, since they
are supposed to produce synchronized output.
2023-11-12 18:48:25 +02:00
Wim Taymans
2c2b0f482f bluez: fix port flags
Physical and Terminal should be set for these hardware devices.

Fixes #3418
2023-08-31 13:01:08 +02:00
Pauli Virtanen
b619616c2a bluez5: media-sink: don't fail A2DP duplex sink if remote idles
Don't emit node error for A2DP duplex sink channel, or when BAP server.
These can occur under normal conditions (remote side suspends
transport), and are not errors.
2023-04-18 22:29:48 +03:00
Pauli Virtanen
9d7d3599db bluez5: output silence if no data for some ISO sinks
When a sink contributing to an ISO CIG does not have data, output
silence for it, as long as at least one sink in the CIG is running.
Only if writes to sockets fail, pause all streams to reset
synchronization.

This way we write exactly the same number of packets for each CIS at the
same time, which probably is the best tested configuration in BT
adapters and devices. We also don't then have to pause output if some
sinks are not running or miss their timing, as we generate silence on
the fly.

When using iso-io, have it initialize the codec instance, and have
media-sink uses that instance, so that silence and actual audio are
encoded with the same codec.
2023-04-14 22:29:03 +03:00
Pauli Virtanen
5b55118e7f bluez5: media-sink: fix reference time vs. resampling delay
Determine correctly if we are resampling, and have the associated delay.

Add off-by-one sample adjustment to the resampling delay, which seems to
correctly align the resampled audio with non-resampled.
2023-04-12 10:12:00 +00:00
Pauli Virtanen
5d1782760c bluez5: media-sink: resync ISO streams on playback start
Resynchronize ISO streams on playback (re)start, so the stream positions
are aligned immediately.  This is better than relying on rate matching
to correct any offsets.
2023-04-10 14:50:42 +00:00
Pauli Virtanen
882f9ad2b3 bluez5: emit BAP device set nodes as needed
Emit BAP device set nodes, which the session manager can use to combine
the sinks/sources of a device set to a single sink/source.

Emit the actual sinks/sources with media.class=.../Internal to hide them
from pipewire-pulse.

Add separate device set routes to the set leader device.  Other routes
of the set members will be marked as unavailable when the set is active.

Accordingly, return failure for attempts to set these unavailable
routes, so that volumes etc. of the "internal" nodes are only controlled
via the device set route.
2023-04-10 07:00:44 +00:00
Pauli Virtanen
e4cd99f76f bluez5: add some debug for media-sink 2023-04-10 07:00:44 +00:00
Pauli Virtanen
6db234ad0c bluez5: media-sink: use iso-io for BAP
Use the ISO IO helpers to get synchronized BAP output, and rate match to
the ISO schedule.

The rate matching is necessary, since the driver may be ticking at a
corrected rate, different from the ISO interval rate.
2023-04-03 16:35:22 +00:00
Pauli Virtanen
0ed124f0fb bluez5: add rate matching for BAP to media-sink
Add basics for rate matching.
2023-04-03 16:35:22 +00:00
Wim Taymans
3698593481 spa: reuse code to clear the timers
We have set_timeout and enable_flush_timer functions to disable the
timers so use those.
2023-03-24 17:55:15 +01:00
Wim Taymans
7b6680ba57 plugins: simplify target_ handling
Drivers should only read the target_ values in the timeout, update the
timeout with the new duration and then update the position.

For the position we simply need to add the previous duration to the
position and then set the new duration + rate.

Otherwise, everything else should read the duration/rate and not use
the target_ values.
2023-03-24 11:36:15 +01:00
Wim Taymans
6e8625cf96 node: update the duration/rate from the target
Before scheduling the graph from the driver, update the duration and
rate with the new targets.
2023-03-23 18:39:27 +01:00
Pauli Virtanen
aa06c547d9 bluez5: align audio output of all BAP sinks
Make BAP nodes align the first sample of their packets at multiples of
the ISO interval, counted in the shared graph sample position.  This
skips a few samples (< 10ms) at the start of playback to ensure the
alignment.

Since the sinks align their flush timing to the graph time, this also
results to them sending packets corresponding to the same graph time at
the same real time instants.

Due to packet queues in kernel/controller, the playback may still be off
by multiples of packets. Kernel changes are needed to address that part.

This works towards making BAP left and right channels to be
synchronized in TWS headsets, where the two earpieces currently appear
as different devices.
2023-03-19 19:18:10 +02:00
Pauli Virtanen
13a9964a71 bluez5: don't try to acquire if we are getting errors
If transport goes into error state too often, fail instead of trying to
acquire it again.

This avoids getting into a tight acquire->fail->reacquire loop.
2023-03-18 15:39:56 +02:00
Pauli Virtanen
1d5c693d33 bluez5: change sink/source run state follow transport state
Allow asynchronous changes in transport state in the sinks/sources.

Also allow transport acquire to be actually synchronous, in this case it
must set transport state during acquire call.

Separate driver start/stop from transport start/stop.
2023-03-17 16:56:31 +00:00
Pauli Virtanen
1664df2c85 bluez5: guard against processing when errored or not started
Add some guards against doing processing when there has been an error or
the node is not started. Set error status to IO.  Continue driving on IO
errors.
2023-03-11 11:11:08 +00:00
Pauli Virtanen
04eb709619 bluez5: don't set unnecessary socket options
In media-sink, there's no need to set RCVBUF.

In media-source, we don't need to set NONBLOCK, as reads are done with
DONTWAIT. Don't set SNDBUF as it's not needed there. Don't set RCVBUF,
but use the (big) kernel default value: decode-buffer will handle any
overruns. Small values of RCVBUF might cause problems if kernel is
sending packets in a burst faster than we wake up.
2023-03-05 17:55:24 +00:00
Pauli Virtanen
99d5436f15 bluez5: media-sink: write packets exactly on time
Delay output by one packet, so that we never need to wait for
node_process to supply more data when a packet is due out, and can write
audio packets at exactly equal intervals (up to timer/io accuracy).

In principle, this should not be necessary. However, enable it for now,
in case this improves the various stutter/etc. bug reports.
2023-02-25 01:10:20 +00:00
Pauli Virtanen
f05e68e4d4 bluez5: media-sink: encode next packet as soon as possible
After flushing a packet, encode the next one immediately if we already
have the data.  This makes the flush timing more accurate (std ~4x
smaller) as we don't need to wait for the encode.
2023-02-25 01:10:20 +00: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
6207d98ff1 spa: add debug log context
Make a real debug context with a log function and move it to a new file.
This way we don't need to redefine a macro.
Make a new context for debugging to a log file. Make new functions to
debug to a log file.
Move the stringbuffer to string utils.
Integrate file/line/func and topics into the debug log.
We can remove some more things from the pipewire log_object function and
also add support for topics.
2023-01-18 17:51:16 +01:00
Wim Taymans
97aafe2234 fix use_buffers checks
We can set 0 buffers even if there is no format.
Return -ENOSPC when too many buffers are set.
2023-01-10 12:30:25 +01:00
Pauli Virtanen
b0853ad34a bluez5: media-sink: set initial latency after getting transport
The call to set_latency doesn't do anything, before transport has been
set.
2022-12-21 16:42:04 +02:00
Wim Taymans
f44d55f6c2 handle read from timerfd correctly
When reading the timerfd gives an error, we should return right away
because the timeout did not happen.

If we change the timerfd timeout before reading it, we can get -EAGAIN.
Don't log an error in that case but wait for the new timeout.
2022-12-09 17:30:31 +01:00
Pauli Virtanen
e381a26de9 bluez5: don't put side effects into spa_log_* arguments
spa_log_* are macros such that their arguments don't get executed if the
log topic is not enabled.
2022-11-07 20:22:45 +02:00
Dmitry Sharshakov
1bea58c20b bluez5: media-sink: fix bitpool increase
The free buffer check must happen before writing to check for leftover data in buffers. In case data is left over from previous submission, bitpool mustn't be increased.
Also improved logging by adding bitpool to the log message.
2022-11-07 16:32:53 +00: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
Pauli Virtanen
cc4e5a89b4 bluez5: fix BAP codec sink flag usage
MEDIA_CODEC_FLAG_SINK means the local endpoint is sink. Have this the
same way for BAP, no need to invert it.
2022-10-26 20:14:34 +03:00
Wim Taymans
c0fc29494f bluez5: stop before freeing things
Make sure all timers are stopped before we clear our state.

See #2764
2022-10-20 21:52:49 +02:00
Pauli Virtanen
9cfa66baa2 bluez5: media-sink: flush packets at time of first sample
Send encoded data packets at the time corresponding to their first
sample. This is simpler than what we did previously.

Use this scheme also for BAP.
2022-10-18 14:42:14 +00:00
Pauli Virtanen
d231e2a1b1 bluez5: media-sink: bigger socket buffer
Bigger buffer allows for more fluctuation in transmission rate without
sound glitches.

It doesn't matter much for latency, as under normal conditions we are
not producing data faster than the BT adapter can transmit, so the
buffer generally is almost always empty or full, and in the latter case
we have to reduce the bitrate.
2022-10-18 14:42:14 +00:00
Pauli Virtanen
8de03f5c29 bluez5: support and use old api.bluez5.a2dp.* factory names for A2DP
For backward compatibility with old Wireplumber releases, support the
old api.bluez5.a2dp.sink/source names, and use them in object events
instead of the media.sink/source names.
2022-09-15 23:04:37 +03:00
Pauli Virtanen
101287159e bluez5: fix media-sink for A2DP
It's always Audio/Sink for A2DP, never stream.
2022-09-15 19:46:25 +03:00
Frédéric Danis
d4b639419a bluez5: Init BAP LC3 coder/decoder depending on direction
For LE Audio, sink and source have their own media transport endpoint.
Media sink or source only needs the coder or decoder respectively.
2022-09-15 11:17:20 +00:00