1262 Commits

Author SHA1 Message Date
Mengshi Wu
bc53b6b343 bluez5: Remove unused bt_features variable in sco_create_socket. 2026-01-25 17:26:15 +00:00
Mengshi Wu
254620676f bluez5: Use named constants for Bluetooth codec IDs
Replace magic numbers (0x02, 0x05) with named constants BT_CODEC_CVSD
and BT_CODEC_MSBC for better code readability. Also remove redundant
zero initialization of num_caps field since the buffer is already
memset to zero.
2026-01-25 17:26:15 +00:00
Mengshi Wu
78f16bc04b bluez5: Remove hw-offload feature flag check and associated quirks
The sco_offload_btcodec() function now returns void and only skips
offload setup when using the default datapath, simplifying the logic
and removing the need for explicit feature flag checks.
2026-01-25 17:26:15 +00:00
Mengshi Wu
2b5d21da5b bluez5: simplify SCO datapath parsing with spa_atou32 2026-01-25 17:26:15 +00:00
Mengshi Wu
2d6a7d2186 bluez5: fix format string in sco_offload_btcodec log message 2026-01-25 17:26:15 +00:00
Mengshi Wu
db7c74a042 bluez5/backend-native: Add HFP/HSP hardware offload datapath configuration
Add support for configuring the SCO hardware offload data path for
HFP/HSP profiles using the Bluetooth SIG-specified procedure. This
enables vendor-specific SCO offload integrations.

Changes:
- Add `bluez5.hw-offload-datapath` configuration property (default: 0)
- Implement `sco_offload_btcodec()` to set BT_CODEC socket option
- Add `SPA_BT_FEATURE_HW_OFFLOAD` quirk feature flag
- Apply offload configuration when creating SCO sockets if quirk enabled
- Document new property in pipewire-props.7.md

The datapath ID is configurable via device parameters and only applied
when the hardware offload feature flag is set in quirks, allowing
platform-specific SCO offload implementations.
2026-01-25 17:26:15 +00:00
Pauli Virtanen
c1f7963c2a bluez5: fix crash due to debug_mono
Enabling bluez5.debug-iso-mono was in non-ISO branch and crashes. Fix
that.
2026-01-12 19:57:56 +02:00
Pauli Virtanen
f21c44751e bluez5: iso-io: resync if controller packet completion is out of sync
Add heuristic to resync streams if controller packet completion times
for different streams differ by too much.  This likely indicates
controller has lost sync between the streams, and we have to reset
playback.

There's no way to do this properly. The ISO-over-HCI transport is badly
specified in Bluetooth Core Specification.  Many controllers have broken
implementation of the current send timestamp read command, so packets
have no identifiers which ISO interval they belong to.

Controllers try to reconstruct the right interval based on
manufacturer-specific heuristics probably based on packet arrival times.
Kernel + USB introduce timing jitter, and playback sometimes desyncs and
packet from some streams are persistently sent some multiple of the SDU
interval off from the intended timing.

Try to determine this from packet completion latencies.  This is
somewhat manufacturer specific, tested on Intel & Realtek, hopefully
works on others too.
2026-01-11 17:55:26 +02:00
Pauli Virtanen
0003d7a2d0 bluez5: iso-io: add debug option for forcing same data in all streams
When debugging desync issues, it's useful to force same data be sent on
all ISO streams. Add option for that.
2026-01-11 17:55:26 +02:00
Pauli Virtanen
d0309b4e1e bluez5: deal with missing TX timestamps
There are known controller firmware bugs that cause packet completion
reports, mainly for ISO packets, to be missing.

To avoid getting stuck e.g. in ISO queue flushing, we should consider a
packet completed if sufficient time has passed even if controller (and
kernel) don't report it completed. Take 1 s as conservative timeout, the
expected values are some ms.

These firmware bugs also cause kernel to stop sending packets if too
many are left uncompleted, but we cannot detect that.
2026-01-11 17:55:26 +02:00
Pauli Virtanen
c96e58af01 bluez5: handle BAP device set volume change notifications
Update volume state on device set volume notifications.

When one device sends volume notification, CAP specifies volume on other
devices shall be synchronized too.
2026-01-06 16:14:22 +00:00
Pauli Virtanen
260e8261d5 bluez5: add bluez5.autoswitch-routes option to indicate loopback nodes
When session manager emits loopback nodes for profile autoswitch, we
need to indicate them in the Routes.

Otherwise, the port information in Pulseaudio API doesn't account for
them, and some apps (eg GNOME) misbehave, as the loopback node sometimes
doesn't have valid ports.
2026-01-05 20:43:13 +02:00
Pauli Virtanen
f754741d58 bluez5: handle BAP volume change events
Handle volume changes initiated by remote also for the BAP profiles.
2026-01-05 15:35:17 +00:00
Pauli Virtanen
8a7c71694d bluez5: handle BAP initial HW volumes
Setup initial HW volumes for BAP profiles similarly as done for A2DP.
As Client, retain the remote volumes as initial values, and as Server
use our own default volumes.

Also as A2DP Source, use the remote HW volume as initial value, if
available.

In the Client / A2DP Source modes session manager usually restores its
own volumes overriding what we set here.
2026-01-05 15:35:17 +00:00
Pauli Virtanen
11389d101a bluez5: iso-io: more accurate resync after overrun
Take active rate correction properly into account when dropping data on
overrun resync.

Drop data only for the currently processed stream, after data has been
consumed from it. Make sure the rate correction factor is updated after
this for the next cycle of the stream.

Also fix buffer fill level calculation: the fill level interpolation
should use node rate corr, not clock rate diff, since the calculations
are done in system clock domain. Fix same issue in fractional delay
calculation, and take no resampler prefill into account.

Later, we maybe need some more resampler APIs to avoid such details
leaking in.

Previously, stream could have its old rate correction locked in, and its
fill level would then end up off the target on the next cycle.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
b535534611 bluez5: media-source: do buffering on connection start also with PLC
Also when we are capable of PLC, it's better to buffer audio at start,
to get a buffer level close to the target initially.

Delay ISO overrun handling one cycle after buffering is complete, so
that any resamplers are filled at that point.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
1d7e89e069 bluez5: media-source: improve handling of underrun with PLC
When PLC data was produced due to underrun and decode buffer has reached
target level, drop received audio if we already did PLC for that packet.
It's better to lose some packets than having to resync latency.

When about to underrun when PLC is active, update rate matching before
filling up buffer, so that rate slows down and we do not get stuck in a
continuous underrun where PLC fills data and we drop received packets.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
de34ce606f bluez5: media-source: fix off-by-one cycle in rate matching
The rate matching filter assumes buffer level for cycle j+1 is

    buffer(j+1) = buffer(j) + recv(j) - corr(j+1) * duration

but what we are actually doing is instead

    buffer(j+1) = buffer(j) + recv(j) - corr(j-1) * duration

because the correction factor that is computed is not used for the next
cycle, but the one following that. Although the filter is still stable
in theory the extra lag causes oscillations to be damped less.

Fix by using the computed correction factor for the next cycle, as
there's no reason why we'd like to have more lag in rate matching.

This then changes c(j-1) -> c(j) in the assumptions, which turns out to
fix the situation. Fix the filter derivation to match.  The filter
coefficients stay as they were, and they are actually exactly correct
also for short averaging times.

In practice, it is observed that ISO RX with quantum 4096 converges to
stable rate, whereas previously the matching retained small
oscillations.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
c4812af436 bluez5: decode-buffer: fix buffer level after recovery
The buffer level number includes the current quantum, so it should not
be subtracted. We do this after recovery from glitch, and this throws
rate matching off.

The level after recovery should also include the resampler delay.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
11faea9dbc bluez5: media-source: don't break audio if BAP presentation delay too small
As BAP server, when we can't satisfy BAP presentation delay, just accept
a bigger latency and emit a warning, instead of having broken audio.
Also make sure it works if quantum is forced to a larger value than our
wanted node.latency.

Take other latencies into account when selecting the wanted node.latency
for BAP Server.

Fix up port.params usage vs. user flag, which is not used here.

Reset decode buffer rate matching if we need to PLC due to underrun so
it doesn't get stuck at playback at increased rate if target is too
small.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
ac3ac3382b bluez5: iso-io: delay streaming start until all acquires are complete
For better start synchronization, we should wait until all ISO nodes
that are going to be started finish creating ISO io.

Add a separate ready flag for startup that is set when all Acquire
requests are complete.
2026-01-05 15:34:06 +00:00
Pauli Virtanen
8b36e2d9b7 bluez5: support specifying preferred delays as BAP Server
Add options to control advertised delays supported.

Smaller delay needs smaller node.latency be used, so use 40ms as a
reasonable minimum preferred delay.
2026-01-05 15:34:06 +00:00
Frédéric Danis
e15e50c5ee spa: bluez: backend-native: Prevent HSP/HFP connection in both directions
The HSP and HFP profiles expect that a device function only as an audio
gateway or as an headset, which is the normal behavior for a headset,
a hands-free car unit or a phone.

In case of a desktop, it can perform both functionalities, but there's
no interest to get them at the same time as the bidirectional audio
is already supported.
2025-12-19 07:12:46 +00:00
Frédéric Danis
f468529084 spa: bluez: backend-native: Fix audio connection policy for HSP/HFP 2025-12-19 07:12:46 +00:00
Frédéric Danis
d89d1668dc spa: bluez: backend-native: Add support for AT+BLDN for PTS tests
This allows to fake Last Dial Number call by calling the first memory.

This allows to pass PTS tests HFP/AG/OCL/BV-01-C and HFP/AG/OCL/BV-02-C.
2025-12-15 08:56:03 +00:00
Frédéric Danis
9a48bbaa36 spa: bluez: modemmanager: Add support for memory dialing for PTS tests
This add a new property to to allow to fake memory dialing for PTS tests
HFP/AG/OCM/BV-01-C and HFP/AG/OCM/BV-02-C.
2025-12-15 08:56:03 +00:00
Frédéric Danis
25a6fdcdb1 spa: bluez: device: Add SPA_PROP_params to disable dummy call state
The current implementation only send the +CIEV:<call>,<active> event
if there's an active modem in ModemManager. This may lead to headset
disconnection as in (1) if the profile is by another application than
telephony one, e.g. a conference application/website.

This commit improves dummy call status update by adding a new
"bluez5.disable-dummy-call" props param in bluez5 device, allowing
external application like WirePlumber to set it dynamically.

(1) https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1744

Fixes: https://gitlab.freedesktop.org/pipewire/pipewire/-/merge_requests/2606
2025-12-09 16:08:31 +01:00
Pauli Virtanen
2942bae034 bluez5: parse and enable configuration of TMAP / GMAP features
Parse TMAP / GMAP features from MediaEndpoint:SupportedFeatures and pass
them onto the codec in SelectProperties, so it can determine which
mandatory features the device supports.

Add configuration option for specifying which TMAP / GMAP feature bits
we advertise to remote side.

Although some of these could be determined automatically, for production
systems it's better to have explicit option to specify which ones should
be advertised as this may depend on HW capabilities.
2025-12-06 11:23:48 +00:00
Pauli Virtanen
c623886625 bluez5: fix parsing of 0-element dbus arrays
Type checking by recurse + get_arg_type is wrong for 0-element arrays.
Just check from iterator signature.

Also avoid relying on malloc(0) != NULL
2025-12-06 11:21:56 +00:00
Frédéric Danis
6ced56e11d spa: bluez: backend-native: Fix BIEV HF indicators support for HFP AG
The "+BIND: <a>,<state>" reply to AT+BIND? should be sent for every
supported indicator.

"AT+BIEV= <assigned number>,<value>" should only be provided for
enabled indicators. The AG shall respond with an ERROR response code
if it receives updates for disabled or unknown HF indicators or values
that are out of bounds.

This allows to pass PTs tests:
- HFP/AG/HFI/BV-02-C
  AG receives an updated HF Indicator value
- HFP/AG/HFI/BI-03-C
  AG receives invalid updated HF Indicator values
2025-11-25 16:20:02 +01:00
Pauli Virtanen
3b6609f13a bluez5: release transports in CIG/BIG simultaneously
When releasing multiple transports, call Release() simultaneously
instead of serializing the calls.

This operations still needs to be blocking currently, as all releases
have to finish before we do other state-modifying ops.

This works around broken firmware on Creative Zen Hybrid Pro with BAP,
whose Disable command misbehaves when shutting down sink + source CIS
otherwise. It's also anyway better to shut down everything at once.
2025-11-25 09:23:13 +00:00
Pauli Virtanen
8d59ad2713 bluez5: set some BAP Context metadata value on streams
Some devices refuse to enable microphone if Streaming Context metadata
is just Unspecified.

Set some reasonable values for the stream context we create along TMAP,
and try follow CAP rules for selecting the PAC.
2025-11-21 08:33:14 +00:00
Pauli Virtanen
2010a525d3 bluez5: simplify ltv parsing 2025-11-21 08:33:14 +00:00
Pauli Virtanen
ff6db3e08e bluez5: add codec_data for codec-private configuration data
With BAP codec configuration selection goes via multiple functions,
which will need to maintain some private state.

Adjust media_codec to allow for that.

Use it for get_qos().
2025-11-21 08:33:14 +00:00
Pauli Virtanen
914e8c6c7a bluez5: parse BAP endpoint Metadata field 2025-11-21 08:33:14 +00:00
Pauli Virtanen
bf801f4f7f bluez5: clean up LTV writing
Add ltv_writer that checks bounds, and use it. Simplify code a bit.
2025-11-21 08:33:14 +00:00
Frédéric Danis
f9f08f7f5c spa: bluez: backend-native: Fix CIEV call status support for HFP AG
Based on HFP specs, the audio connection is independent of the active
call status, which should be managed by the ModemManager part of the
plugin.
But when using HFP AG without modem attached, e.g. during zoom meeting,
the connection will be closed after a while unless call status has been
forced to active,
cf. https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1744.

Currently and for HFP AG PTS tests requesting to get an audio connection
in 3 seconds after a call activates, this prevent to start audio
connection before starting a call.

This commit prevents to force the call status during audio (dis)connection
if a modem is available.
2025-11-21 08:27:44 +00:00
Frédéric Danis
b940d9f3a1 spa: bluez: modemmanager: Fix NameOwnerChanged
Get the ModemManager interfaces when the ModemManager starts after native
HFP has started.

Also add log topic to be able to select log level for modemmanager plugin
part.
2025-11-21 08:26:40 +00:00
Frédéric Danis
5d39e1357e spa: bluez: backend-native: Fix +CNUM reply
PTS test HFP/AG/NUM/BV-01-C expects to receive 5 items, the last one
representing service type which could be 4 for voice or 5 for fax.
2025-11-21 08:22:55 +00:00
Pauli Virtanen
8df58db415 bluez5: read errqueue also from media-source handler
Flush errqueue for iso-io also from the media-source handler, to avoid
dropping packet tx reports.

This applies to bidirectional streams. The sink/source handlers poll on
different fd, one being dup'd, and epoll does not trigger them in any
specific interleaved order.
2025-11-09 02:34:54 +02:00
Pauli Virtanen
878dd7a0c9 bluez5: default to FL,FR channels for BAP server
As server, it's not possible to expose all locations as supported
because BlueZ only exposes two ASE.

Default to just FL,FR
2025-11-09 01:47:17 +02:00
Pauli Virtanen
567d5181ca bluez5: iso-io: force resync after underrun
If stream underruns we should resync playback position on next cycle as
there is anyway a sound glitch.
2025-11-09 01:45:21 +02:00
Barnabás Pőcze
3413ca9617 spa: bluez: backend-native: fix libusb device leak
`libusb_free_device_list()` should be passed `true` as its second
argument to unreference every single device in the list.

Fixes: 5e0b63b1495591 ("bluez5: backend-native: use quirks + usb adapter caps for checking msbc")
2025-11-07 12:28:16 +00:00
Barnabás Pőcze
78b6df769b spa: bluez: telephony: reject double registration 2025-11-07 12:28:16 +00:00
Barnabás Pőcze
9a0053a501 spa: bluez: telephony: fix string leaks
`dbus_connection_register_object_path()` does not take ownership of the
path passed to it, so currently the callers `telephony_{ag,call}_register()`
leak a copy of the path string. Fix that by not making an unnecessary copy.

Fixes: b28399ac578357 ("bluez5: add telephony D-Bus service implementation")
2025-11-07 12:28:16 +00:00
Barnabás Pőcze
963d10f1ac spa: bluez: mark dbus vtables static 2025-11-07 12:28:16 +00:00
Pauli Virtanen
296abbf7ca bluez5: fix wrong use of SPA_POD_CHOICE_ENUM_Int
SPA_POD_CHOICE_ENUM_Int must always take at least 2 values as first one
is default. (Just 1 value no longer works on current master, and it's
anyway incorrect.)

Replace with just SPA_POD_Int, as there's just one choice.
2025-11-03 22:11:04 +00:00
Arun Raghavan
49a803eaa0 spa: bluez5: Make HFP description slightly more user-friendly
In the unknown form-factor case (which I saw on a set of headphones
here, Creative Zen Hybrid Pro), let's avoid an apocryphal acronym in
favour of a term that might be more familiar to the user.
2025-11-03 13:57:24 -08:00
Wim Taymans
aa0272f6f3 treewide: remove some obsolete channel checks
The spa_audio_info can not be parsed with too many channels so there
is always enough space for the positions.
2025-10-24 10:31:45 +02:00
Wim Taymans
78219471ff spa: remove some obsolete functions
The spa_audio_info array now always holds enough positions for all
channels and we don't need to wrap around.
2025-10-24 09:35:59 +02:00