Skip to content

fix: 解决连接蓝牙耳机时模式跟配置不匹配的问题#860

Closed
fly602 wants to merge 0 commit intolinuxdeepin:masterfrom
fly602:master
Closed

fix: 解决连接蓝牙耳机时模式跟配置不匹配的问题#860
fly602 wants to merge 0 commit intolinuxdeepin:masterfrom
fly602:master

Conversation

@fly602
Copy link
Contributor

@fly602 fly602 commented Aug 13, 2025

连接蓝牙耳机时,首选的输入输出端口的配置文件可能不一样,导致自动切换输入时,修改了声卡的配置文件,导致蓝牙耳机的模式发生了变化。

Log: 解决连接蓝牙耳机时模式跟配置不匹配的问题
PMS: TASK-369021

Summary by Sourcery

Restrict auto-switch to ports that support the preferred profile, refactor profile preference persistence to card level, and refresh Bluetooth audio mode options dynamically

Bug Fixes:

  • Fix mismatched audio profile and mode when connecting Bluetooth headsets

Enhancements:

  • Check port’s available profiles before auto-switching input or output ports
  • Default to and persist the core’s selected profile when no preference is set
  • Refactor ConfigKeeper.SetProfile to drop the port parameter and store preferred profile per card
  • Replace hardcoded Bluetooth mode options with a dynamic refresh method

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @fly602 - I've reviewed your changes and they look great!


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.

@sourcery-ai
Copy link

sourcery-ai bot commented Aug 13, 2025

Reviewer's Guide

This PR fixes mismatches during Bluetooth headset connections by enhancing auto-switch validation to ensure port–profile compatibility, unifying the profile persistence API in ConfigKeeper, and updating audio initialization and profile-setting calls accordingly.

File-Level Changes

Change Details Files
Enhance auto-switch input/output port logic with profile availability checks
  • Removed nil defaultSource/defaultSink early returns
  • Retrieved current port and iterated its Profiles
  • Checked availability and name match before auto-switching
  • Blocked auto-switch when desired profile isn’t supported by the port
audio1/audio_events.go
Refine autoSwitchProfile to default profile and persist changes
  • Default to SelectProfile when no preferred profile is set
  • Skip switching if profile is empty or already active
  • Persist applied profile via ConfigKeeper.SetProfile
audio1/audio_events.go
Simplify ConfigKeeper.SetProfile API and logic
  • Dropped portName parameter from SetProfile signature
  • Set only card.PreferProfile and removed port.PreferProfile/card.PreferPort
  • Added guard to ignore unknown card names
audio1/config_keeper.go
Refactor Bluetooth audio mode options initialization
  • Replaced setPropBluetoothAudioModeOpts call with refreshBluetoothOpts
audio1/audio.go
Update SetProfile calls to match new API
  • Removed portName argument from SetProfile invocations
  • Updated calls in setPortWithProfileSwitch and SetBluetoothAudioMode
audio1/audio.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

mhduiy
mhduiy previously approved these changes Aug 14, 2025
@deepin-bot
Copy link
Contributor

deepin-bot bot commented Aug 15, 2025

TAG Bot

New tag: 6.1.50
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #862

@fly602 fly602 force-pushed the master branch 3 times, most recently from ca8ceaf to 2cd6d26 Compare August 19, 2025 08:58
@deepin-ci-robot
Copy link

deepin pr auto review

代码审查报告

总体评价

这段代码是一个音频管理系统,主要处理音频设备的管理、切换和配置。代码结构清晰,但存在一些可以改进的地方。

具体改进建议

1. 代码质量

  1. 错误处理

    • 在多个地方使用了logger.Warning(err)但没有处理错误,应该考虑是否需要向上层传递错误或进行适当处理。
    • 例如在tryGetPreferOutPut()函数中,多处使用了logger.Warning(err)但没有后续处理。
  2. 代码重复

    • autoSwitchOutputPort()autoSwitchInputPort()函数有相似的逻辑,可以考虑提取公共部分。
    • 多处使用了相似的端口切换逻辑,可以考虑封装成公共函数。
  3. 注释质量

    • 部分注释过于简单或已过时,例如"TODO: 这个逻辑有问题,下次来修改"应该被修复或提供更详细的解释。
    • 建议为复杂的业务逻辑添加更详细的注释。

2. 性能优化

  1. 锁的使用

    • 移除了callbackLocker,这是一个好的改进,减少了锁的使用。
    • 但在autoSwitchPort()等函数中仍然存在潜在的并发问题,需要确保操作的原子性。
  2. 循环优化

    • tryGetPreferOutPut()函数中有多个嵌套循环,可以考虑优化算法减少循环次数。
    • 例如,在查找端口时可以考虑使用map来存储端口信息,提高查找效率。

3. 代码安全

  1. 输入验证

    • SetPort()等函数中,应该对输入参数进行更严格的验证。
    • 例如,检查cardIdportName是否有效。
  2. 资源管理

    • 确保在错误情况下正确释放资源,特别是在处理音频设备时。

4. 具体问题

  1. tryGetPreferOutPut()函数

    • 逻辑过于复杂,嵌套层次太深,难以维护。
    • 建议拆分成多个小函数,每个函数负责一个具体的任务。
  2. setPort()函数

    • 参数auto的命名不够清晰,建议改为isAutoSwitch以更清楚地表达其含义。
    • 函数内部逻辑可以进一步优化,减少重复代码。
  3. autoSwitchPort()函数

    • 错误处理不够完善,应该根据不同的错误类型采取不同的处理策略。
    • 日志记录可以更加详细,便于问题排查。

5. 建议改进

  1. 重构tryGetPreferOutPut()函数

    func (a *Audio) tryGetPreferOutPut() PriorityPort {
        // 1. 获取优先级最高的端口
        // 2. 检查端口是否启用
        // 3. 检查期望配置文件
        // 4. 返回符合条件的端口
    }
  2. 优化端口切换逻辑

    func (a *Audio) switchPort(cardId uint32, portName string, direction int, isAuto bool) error {
        // 统一的端口切换逻辑
    }
  3. 增加单元测试

    • 为关键函数编写单元测试,特别是端口切换和配置管理相关的函数。
    • 使用mock对象来模拟音频设备,提高测试覆盖率。

总结

代码整体结构良好,但存在一些可以改进的地方。主要关注点包括:

  1. 简化复杂的函数逻辑
  2. 改进错误处理
  3. 优化性能
  4. 增加代码注释
  5. 添加单元测试

这些改进将有助于提高代码的可维护性、可靠性和性能。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602

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

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