Skip to content

fix(audio): fix BluetoothAudioMode empty after bluez5 support#1136

Merged
mhduiy merged 1 commit into
linuxdeepin:masterfrom
mhduiy:bluez
Jun 4, 2026
Merged

fix(audio): fix BluetoothAudioMode empty after bluez5 support#1136
mhduiy merged 1 commit into
linuxdeepin:masterfrom
mhduiy:bluez

Conversation

@mhduiy

@mhduiy mhduiy commented Jun 3, 2026

Copy link
Copy Markdown
Contributor
  1. Add bluez5 device API recognition in getCardName to support PipeWire/BlueZ5 bluetooth audio devices
  2. Fix refreshBluetoothOpts using friendly card name instead of pulse raw name for isBluezAudio check, causing BluetoothAudioMode to remain empty after bluetooth profile switch

Log: fix BluetoothAudioMode empty caused by bluez5 card name change

fix(audio): 修复 bluez5 支持后 BluetoothAudioMode 为空的问题

  1. getCardName 中增加 bluez5 设备 API 的识别,支持 PipeWire/BlueZ5 蓝牙音频设备
  2. 修复 refreshBluetoothOpts 中 isBluezAudio 使用友好名称而非 pulse 原始名称判断, 导致蓝牙 profile 切换后 BluetoothAudioMode 属性未被赋值

Log: 修复 bluez5 声卡名称变更导致 BluetoothAudioMode 为空
PMS: BUG-364299

Summary by Sourcery

Fix Bluetooth audio mode handling for BlueZ5/PipeWire devices by correctly identifying BlueZ5 cards and using the raw card name for BlueZ detection.

Bug Fixes:

  • Recognize BlueZ5 device API when deriving card names so PipeWire/BlueZ5 Bluetooth audio devices are treated as Bluetooth cards.
  • Ensure BluetoothAudioMode is updated after Bluetooth profile switches by checking BlueZ audio status using the underlying PulseAudio card name instead of the friendly name.

1. Add bluez5 device API recognition in getCardName to support
   PipeWire/BlueZ5 bluetooth audio devices
2. Fix refreshBluetoothOpts using friendly card name instead of
   pulse raw name for isBluezAudio check, causing BluetoothAudioMode
   to remain empty after bluetooth profile switch

Log: fix BluetoothAudioMode empty caused by bluez5 card name change

fix(audio): 修复 bluez5 支持后 BluetoothAudioMode 为空的问题

1. getCardName 中增加 bluez5 设备 API 的识别,支持 PipeWire/BlueZ5 蓝牙音频设备
2. 修复 refreshBluetoothOpts 中 isBluezAudio 使用友好名称而非 pulse 原始名称判断,
   导致蓝牙 profile 切换后 BluetoothAudioMode 属性未被赋值

Log: 修复 bluez5 声卡名称变更导致 BluetoothAudioMode 为空
PMS: BUG-364299
@sourcery-ai

sourcery-ai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates Bluetooth audio handling to correctly recognize BlueZ5-based devices and ensure BluetoothAudioMode is populated by using the core PulseAudio card name for BlueZ detection and supporting the bluez5 device.api in card naming.

Flow diagram for updated Bluetooth audio card handling

flowchart TD
  PA_Card[pulse.Card] --> GetName[getCardName]

  GetName --> CheckAPI{device.api<br/>bluez/bluez5?}
  CheckAPI -->|yes and device.description not empty| UseDesc[Set name = device.description]
  CheckAPI -->|no| CheckAlsa{alsa.card_name<br/>not empty?}
  CheckAlsa -->|yes| UseAlsa[Set name = alsa.card_name]
  CheckAlsa -->|no| UseRaw[Set name = raw card name]

  subgraph BluetoothOpts[refreshBluetoothOpts]
    CardWrapper[card] --> CoreName[card.core.Name]
    CoreName --> IsBluez[isBluezAudio]
    IsBluez -->|false| Exit[Return without<br/>BluetoothAudioMode]
    IsBluez -->|true| SetMode[Set BluetoothAudioMode]
  end
Loading

File-Level Changes

Change Details Files
Ensure BluetoothAudioMode is updated based on the underlying PulseAudio card name instead of the friendly card name.
  • Change refreshBluetoothOpts to call isBluezAudio with card.core.Name instead of card.Name so detection uses the raw PulseAudio card identifier
  • Prevent early return in refreshBluetoothOpts when the friendly card name no longer matches BlueZ naming after profile switches
audio1/audio.go
Support BlueZ5/PipeWire bluetooth devices when computing user-visible card names.
  • Extend getCardName to treat device.api="bluez5" the same as "bluez" when choosing device.description as the card name
  • Maintain existing fallback to alsa.card_name when no BlueZ/BlueZ5 device description is present
audio1/card.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff代码。这次修改主要涉及音频卡名称获取逻辑的优化以及蓝牙设备判断逻辑的调整。

以下是我对这次代码变更的详细审查意见,分为语法逻辑、代码质量、代码性能和代码安全四个方面:

1. 语法与逻辑

  • audio1/audio.go 修改:card.Name -> card.core.Name

    • 逻辑分析:将获取卡名称的途径从直接读取 card.Name 改为读取底层 PulseAudio 对象的 card.core.Name。结合 card.go 的修改来看,这是合理的。因为 getCardName 函数的逻辑表明,对于蓝牙设备,应该优先使用 device.description 属性,而不是 PulseAudio 默认的 card.Name。如果 card.Name 返回的是底层原始名称,而 card.core.Name 是经过上层封装/过滤的名称,那么这种修改是正确的。
    • 潜在风险:需要确保 card.core 不为 nil,且 card.core.Name 在所有生命周期内都是安全可读的。如果 card.core 可能为 nil,直接访问 card.core.Name 会引发 panic。建议确认 card 对象初始化的逻辑,确保 core 字段必定被赋值。
  • audio1/card.go 修改:增加 bluez5 的判断

    • 逻辑分析:PulseAudio 在处理蓝牙音频时,旧版使用 bluez API,而较新的版本使用 bluez5 API。增加对 bluez5 的支持是非常必要且正确的修复,否则使用较新蓝牙协议的设备可能无法正确获取 device.description,导致回退到 alsa.card_name 或显示不友好的名称。
    • 改进建议:当前使用 || 进行判断是可行的,但随着 API 版本演进,如果未来出现 bluez6 等,这种硬编码会变得冗长。可以考虑提取为辅助函数或使用 strings.HasPrefix

2. 代码质量

  • 魔法字符串:代码中直接使用了 "bluez5" 和 `"bluez"``。这属于魔法字符串,降低了代码的可读性和可维护性。
    • 建议:建议将其定义为常量,集中管理。
  • 逻辑简化(propDeviceApi == "bluez5" || propDeviceApi == "bluez") 可以使用更简洁的方式表达。

代码质量改进示例:

// 在 card.go 顶部或相关的常量定义区域添加
const (
    ApiBluez  = "bluez"
    ApiBluez5 = "bluez5"
)

// 修改 getCardName 函数中的判断逻辑
func getCardName(card *pulse.Card) (name string) {
	propAlsaCardName := card.PropList["alsa.card_name"]
	propDeviceApi := card.PropList["device.api"]
	propDeviceDesc := card.PropList["device.description"]
	
	// 使用常量替换魔法字符串,提高可读性
	if (propDeviceApi == ApiBluez5 || propDeviceApi == ApiBluez) && propDeviceDesc != "" {
		name = propDeviceDesc
	} else if propAlsaCardName != "" {
		name = propAlsaCardName
    }
    // ... 其他逻辑
}

3. 代码性能

  • 评估:此次修改不涉及循环、高并发读写、大量内存分配或复杂的算法操作。字符串的等值比较(==)在 Go 中是非常高效的(编译器会优化长度不同的字符串比较)。因此,性能方面没有任何负面影响,无需进行性能优化。

4. 代码安全

  • 空指针解引用风险
    如前文所述,card.core.Name 的引入增加了潜在的空指针风险。虽然这在编译期不会报错,但在运行时如果 card.corenil,会导致程序崩溃。在系统级服务(如音频服务)中,崩溃是绝对不可接受的。
  • 输入校验card.PropList 读取的值理论上都应该是字符串,但如果 PulseAudio 绑定库出现异常,或者传入的 card 指针本身为 nil,当前代码缺乏防御性编程。

安全改进建议:

针对 audio.go 中的修改,建议增加防御性检查:

// audio1/audio.go
func (a *Audio) refreshBluetoothOpts() {
    // ... 前置逻辑
    
    // 防御性编程:确保 card 和 card.core 都不为空
    if card == nil || card.core == nil {
        return
    }

    if !isBluezAudio(card.core.Name) {
        return
    }
    
    // ... 后续逻辑
}

总结

这次 Diff 的修改方向是正确的,修复了 BlueZ5 协议设备名称获取不准确的问题,并统一了名称获取的入口。主要的改进点在于增加防御性编程以防空指针崩溃,以及消除魔法字符串提升代码可维护性。建议采纳上述安全和质量方面的改进意见后合入主分支。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider extracting the BlueZ/BlueZ5 device.api check into a small helper (e.g., isBluezApi(propDeviceApi string)) so that future BlueZ variants or related APIs can be handled in a single place and reused if needed.
  • When switching refreshBluetoothOpts to use card.core.Name, ensure card.core is always non-nil in all code paths or add a defensive nil check to avoid potential panics if a card is partially initialized.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the BlueZ/BlueZ5 device.api check into a small helper (e.g., `isBluezApi(propDeviceApi string)`) so that future BlueZ variants or related APIs can be handled in a single place and reused if needed.
- When switching `refreshBluetoothOpts` to use `card.core.Name`, ensure `card.core` is always non-nil in all code paths or add a defensive nil check to avoid potential panics if a card is partially initialized.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602, mhduiy

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mhduiy mhduiy merged commit ec17fd3 into linuxdeepin:master Jun 4, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants