Commit graph

1209 commits

Author SHA1 Message Date
Wim Taymans
13b8c23767 Don't use SPA_AUDIO_MAX_CHANNELS directly
Make a MAX_CHANNELS define and use that one in code. This makes it
easier to change the constant later.
2025-10-21 09:43:06 +02:00
Wim Taymans
f453b1545d audio: don't use SPA_AUDIO_MAX_CHANNELS in some places
When we know the max size of the array, just use this instead of the
SPA_AUDIO_MAX_CHANNELS constant.
2025-10-20 18:31:17 +02:00
Pauli Virtanen
91702975f7 bluez5: media-sink: cleanup ISO rate matching
Move accounting for pending ISO packet to the reference time.  Make sure
rate matching is reset on start, and reset matching on resync properly.

Allow resync on first cycle, ok since iso_io->now is valid immediately.
2025-10-14 07:59:55 +00:00
Pauli Virtanen
4e3a5d9e6f bluez5: iso-io: initialize stream->size, now when setting cb
Ensure size and now have valid values after exiting
spa_bt_iso_io_set_cb(), so data may be provided already on first cycle.
2025-10-14 07:59:55 +00:00
Pauli Virtanen
4ca1d70979 bluez5: media-sink: fix silence padding for ISO stream resync
Silence padding larger than ISO packet may be needed for resync when
quantum is large. We can't insert silence by adding data to encoding
buffer, as the encoding buffer may be then too small and it may also be
partially filled.

Fix by inserting silence from flush_data() just before buffers would be
consumed.

Fixes ISO stream alignment at playback start.
2025-10-14 07:59:55 +00:00
Pauli Virtanen
8bf8600e59 bluez5: if Acquire results to NoReply, try to clean up with Release
If BlueZ doesn't reply, it may consider the operation still active.
Try to Release the transport to get to a known state.

This can happen if device doesn't respond to operations in reasonable
time and BlueZ doesn't have its own timeout which is the case for BAP
currently (which is a bug there).
2025-10-11 20:52:59 +03:00
Pauli Virtanen
6755f24a3d bluez5: reduce log level for unhandled RFCOMM commands
Don't log warnings about not passing on RFCOMM commands to modem if
there is no modem configured, as this is normal occurrence.
2025-10-02 01:33:23 +03:00
Pauli Virtanen
8277bf6b36 bluez5: improve error messages when connection drops
Log something less confusing when connection to remote device drops
unexpectedly.

Silence logging transport Release() error in cases where the transport
was simultaneously deleted.
2025-10-02 01:25:56 +03:00
Wim Taymans
532140ca90 bluez5: Don't assume channels fit in uint8_t
There is no reason to believe the number of channels can fit in a
uint8_t. Limit the number of channels in some places where it can not
be avoided.
2025-10-01 09:13:42 +02:00
Pauli Virtanen
2248807835 bluez5: sco-io: send keepalive TX data if sink is not feeding it
When using LC3-24kHz, remote device drops connection after a few seconds
if there is no sink playback.  Avoid this by sending silence, one TX
packet for each RX packet, if sink hasn't been feeding data within a
timeout.
2025-09-29 14:15:46 +00:00
Pauli Virtanen
589bc4b6f4 bluez5: iso-io: sync to ISO RX clock, align stream RX in group
Align RX of streams in same ISO group:

- Ensure all streams in ISO group have same target latency also for BAP
  Client

- Determine rate matching to ISO group clock from RX times of all
  streams in the group

- Based on this, compute nominal packet RX times, and feed them to
  decode-buffer instead of the real RX time. This is enough for
  sub-sample level sync.

- Customise buffer overrun handling for ISO so that it drops data to
  arrive exactly at the target, for faster convergence at RX start

The ISO clock matching is done based on kernel-provided packet RX times,
so it has unknown offset from the actual ISO clock, probably a few ms.

Current kernels (6.17) do not provide anything better to use for the
clock matching, and doing it properly appears to be controller
vendor-defined (if possible at all).
2025-09-07 18:26:03 +00:00
Pauli Virtanen
94c354c290 bluez5: decode-buffer: sub-sample accurate fill level tracking
Take resampler delay into account when computing the buffer fill level,
including the fractional part.

If decode-buffer is now fed nominal packet reference times in
write_packet(), it converges the total buffer + resampler latency to the
target at sub-sample accuracy.

This is needed for aligning RX of ISO streams in the same group, so that
e.g. stereo pair alignment is achieved even though the streams have
separate resamplers. Resampler phases get aligned via independent rate
matching.
2025-09-07 18:26:03 +00:00
Pauli Virtanen
e446e3aac5 bluez5: media-source: account for driver clock rate difference in rate match
The rate matching calculations are done in the system clock domain.  If
the driver ticks at a different rate, the correction factor needs to be
adjusted by the rate_diff.

setup_matching() also needs to be before spa_bt_decode_buffer_process():
as follower we should use rate matching value calculated on the
*previous* cycle, because this is what driver is doing when it adjusts
it tick rate.
2025-09-07 18:26:03 +00:00
Pauli Virtanen
f48a72a504 bluez5: smaller max latency for BAP client capture
4 packets should be enough jitter buffer. This matters only for BAP
client, server is controlled by presentation delay.
2025-09-07 18:26:03 +00:00
Pauli Virtanen
7e04f8fe44 bluez5: ensure capture target latency is uniform for an ISO group
All BAP client sources in the ISO group should use same target latency,
so that capture streams stay in sync.
2025-09-07 18:26:03 +00:00
Pauli Virtanen
396d37594c bluez5: media-source: drop all errqueue data when ignoring 2025-09-07 18:26:03 +00:00
Pauli Virtanen
47780884e1 bluez5: media-source: pass through node.rate and node.latency
Allow user to specify custom values for node.rate and note.latency.

Also restore the 512 default latency.
2025-09-07 18:04:28 +00:00
George Kiagiadakis
e9b78f1c31 bluez5: media-source: add option to control the target latency of the decode-buffer
On production systems, having a constant high latency is favored over
dynamically adjusting it in order to optimize for low latency,
because every time a dynamic adjustment happens, there's a glitch.

This adds an option to let the user specify the exact amount of latency
they want.
2025-09-07 18:04:28 +00:00
George Kiagiadakis
5af8340183 bluez5: media-source: don't set node.latency by default
The hardcoded latency of 512/<rate> is quite low on some ALSA devices.
Instead of forcing that latency onto the graph, just don't set it at all
unless it originates from the BAP presentation delay. That means that
the functionality remains the same for BAP but changes for A2DP to favor
the preferred quantum of the ALSA sink (or whatever is the driver).

Also, avoid setting an empty string ("") latency and rate in the cases
where it's not defined. This allows users to override those properties
through the wireplumber monitor rules if they need to.
2025-09-07 18:04:28 +00:00
Pauli Virtanen
984c44b044 bluez5: fix BIS source presentation delay
The value comes from QoS preset, not configurable otherwise right now.

Patch from @michael-kong754
2025-08-27 15:55:50 +00:00
Wim Taymans
e35a8554f8 control: improve UMP to Midi conversiom
Improve the spa_ump_to_midi function so that it can consume multiple UMP
messages and produce multiple midi messages.

Some UMP messages (like program changes) need to be translated into up
to 3 midi messages. Do this byt adding a state to the function and by
making it consume the input bytes, just like the spa_ump_from_midi
function.

Adapt code to this new world. This is a little API break..
2025-08-19 18:33:59 +02:00
Frédéric Danis
a50b66651e bluez5: backend-native: Add log for call state changes
This also to track each call state changes.
2025-08-12 18:22:40 +03:00
Frédéric Danis
1ad3fdff8a bluez5: backend-native: Free command list queue on RFComm free
When RFComm conection is closed or lost, the pending commands should
be freed, and if a DBus message is attached an error reply should be
sent.
2025-08-12 11:35:02 +02:00
George Kiagiadakis
22e80e228c bluez5: backend-native: reset hfp_hf_in_progress state on error reply
If a AT+CHLD command fails, we may get stuck in the "in progress" state
forever unless we clear the state here.
2025-08-09 09:24:02 +00:00
Pauli Virtanen
a39462a6c0 bluez5: support 44.1kHz rate for BAP LC3
liblc3 doesn't support 44.1kHz directly, but its authors write one
should just use some nearby rate instead. Do just that.
2025-08-09 11:30:40 +03:00
Pauli Virtanen
e8fa7929b7 bluez5: use BT_PKT_SEQNUM for ISO packet sequence numbers
Use kernel BT_PKT_SEQNUM (likely in Linux v6.17) to provide the ISO
packets sequence numbers. Fall back to counting packets if kernel is too
old to support the feature.
2025-08-01 15:39:45 +00:00
George Kiagiadakis
79a069c886 bluez5: backend-native: add debug log in hfp_hf_remove_disconnected_calls() 2025-08-01 15:39:06 +00:00
George Kiagiadakis
5ccd1c5619 bluez5: backend-native: update hfp_hf_in_progress at the end of the CLCC update
When we skip the CIEV event in order to update the call list with CLCC,
we may receive multiple +CLCC events or even none, if the calls are
disconnected. To avoid any mistakes, update the hfp_hf_in_progress flag
after the CLCC update is entirely done.
2025-08-01 15:39:06 +00:00
George Kiagiadakis
e4b0f68e0b bluez5: telephony: implement asynchronous D-Bus calls
This removes the need to call poll() on the rfcomm socket in order
to wait for replies from the AG.

Use a queue to buffer all the commands that are to be sent to the AG
and match them to replies when they are received. Optionally associate
each command with a DBusMessage that is assumed to be a method call
from the telephony interface, which is then replied to when the rfcomm
command reply is received. Also associate each command with a state,
so that it is always deterministic what gets executed after the reply
is received.

On the telephony module, pass on the DBusMessage on the callbacks and
add a method to allow the receiver to send a reply. Only send FAILED
directly when the callback is not handled. Also, remove the return value
from the Dial() command (it was not advertised on the introspection
anyway) to make things easier.
2025-08-01 15:39:06 +00:00
Pauli Virtanen
121608f040 bluez5: allow framing for BAP
There's actually no reason to disable framed qos presets.
2025-07-31 10:58:00 +00:00
Wim Taymans
28510f3117 bluez: safely parse the control data from buffers 2025-07-31 11:24:29 +02:00
Pauli Virtanen
cee0c39b00 bluez5: fix decode-buffer buffering threshold
The minimum is the number of requested samples, not duration, which can
be different when resampling.
2025-07-12 19:59:33 +00:00
Pauli Virtanen
ff81fc9f7b bluez5: fix ISO sequence numbering
Pass zero-length packets to the codec. BAP/ISO may use these to indicate
missing data.

Fix A2DP codecs to not parse input with spa_return_val_if_fail, that's
meant for assertions. Just return -EINVAL directly, it's normal that
input data may contain garbage.
2025-07-12 19:59:33 +00:00
Pauli Virtanen
02d5d9bc1f bluez5: sco-io: remove unnecessary variable 2025-07-12 19:59:33 +00:00
Pauli Virtanen
87843366ce bluez5: add PLC for MSBC using spandsp
Use spandsp as optional dependency for MSBC codec, for providing PLC.
2025-07-12 19:59:33 +00:00
Pauli Virtanen
90a1b35017 bluez5: media-source: support codec-provided packet loss concealment
If packet sequence number jumps ahead, or we would underflow, use
codec-provided packet loss concealment to produce some audio data.

When we produce it during underflow, skip the corresponding number of
sequence numbers of future packets.

If codec doesn't have PLC, keep the previous behavior (pad with zeros,
buffering pauses to wait for data).
2025-07-12 19:59:33 +00:00
Pauli Virtanen
d0680a2b3d bluez5: support packet loss concealment in codecs
LC3 and Opus have built-in support for packet loss concealment.

Add codec interface for that, and implement for LC3.

Extend media_codec interface so that packets not aligned with socket
reads can be handled, as in HFP. This is required for correct sequence
number counting, and for being able to run codec PLC *before* decoding
the next correctly received packet.
2025-07-12 19:59:33 +00:00
Pauli Virtanen
a2ede93479 bluez5: report ISO latency correctly and refresh when transport starts 2025-07-12 19:57:22 +00:00
Pauli Virtanen
5e79d0fb01 bluez5: fix compilation and warnings 2025-07-12 19:57:22 +00:00
Pauli Virtanen
d10249d0ce bluez5: allow faster rate matching
Bump up DLL maximum rate difference and reduce averaging time.
2025-07-12 19:57:22 +00:00
Pauli Virtanen
2c70c13cc3 bluez5: rate match ISO only from process()
Update rate matching only once per process(). This ensures all nodes in
the group update their rate matching in the same way.

Also account for audio data in ISO output buffer in the reference time.
2025-07-12 19:57:22 +00:00
Pauli Virtanen
ad90a2d0ac bluez5: take clock rate difference into account in get_reference_time()
The calculations is in system clock domain, so when converting from
samples/duration to time rate difference should be accounted.

This does not have much effect in practice.
2025-07-12 19:57:22 +00:00
Pauli Virtanen
30047f232b bluez5: account for driver clock rate difference in rate matching
The rate matching calculations are done in the system clock domain.  If
the driver ticks at a different rate, the correction factor needs to be
adjusted by the rate_diff.

This fixes ISO streams getting out of sync with each other when target
delay changes. This happens because typically one of them is the driver
and the other follower. Driver adjust clock rate, and follower does its
own adjustment *on top of that* so it rate matches more or less at
double speed.  (The DLL of the follower to some degree corrects for
this, but can't do that when hitting RATE_CTL_DIFF_MAX and moreover it
acts with a delay.)
2025-07-12 19:57:22 +00:00
Pauli Virtanen
ddc023b883 bluez5: media-sink: make ISO target latency scale with quantum
The ISO target latency should scale with graph quantum, as jitter in the
graph processing time probably is proportional to the quantum.
2025-07-12 19:57:22 +00:00
Frédéric Danis
067e29543a bluez5: backend-native: Fix call held hangup
Currently it's not possible to hangup a call place on hold, and
request user to swap calls before been able to hangup.
2025-07-11 10:42:03 +02:00
Pauli Virtanen
ae7a893ce9 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.
2025-07-10 14:12:15 +00:00
Frédéric Danis
80d44e8f39 bluez5: backend-native: Fix incorrect dial number management
When dialing an incorrect phone number some phones (e.g. iOS 18.5)
replies with OK but never send +CIEV updates, so there's no way to
know that the dial is not in progress and the call object should be
removed.

This change waits for +CIEV event to create the call object.
2025-07-08 11:37:45 +00:00
Pauli Virtanen
7fd05e7eaa bluez5: drop old SCO fragment data when sink starts
Any pending SCO fragment data should be cleared when sink starts, so
that we don't send out any old data.
2025-06-21 16:13:57 +03:00
Pauli Virtanen
665a27f281 bluez5: replace sco-source with media-source
Change media-source to use sco-io for HFP codecs.

Replace sco-source with media-source.

sco-source is mostly copypaste from media-source, only differed in the
IO handling.
2025-06-21 16:08:30 +03:00
Pauli Virtanen
5b4e9dc33e bluez5: replace sco-sink with media-sink
Change media-sink to use sco-io for HFP codecs.

Move SCO fragmentation to sco-io side.

Replace sco-sink with media-sink.

sco-sink is mostly copypaste from media-sink, and only differed in the
fragmentation detail, which can as well be handled on sco-io side.
2025-06-21 16:08:30 +03:00