Skip to content

fix: fix audio input volume initialization issue#1058

Open
fly602 wants to merge 1 commit intolinuxdeepin:masterfrom
fly602:master
Open

fix: fix audio input volume initialization issue#1058
fly602 wants to merge 1 commit intolinuxdeepin:masterfrom
fly602:master

Conversation

@fly602
Copy link
Contributor

@fly602 fly602 commented Mar 14, 2026

The refresh() function now calls GetPriorityManager().Init(a.cards) to ensure audio configuration is properly initialized during refresh operations. This addresses the issue where audio input volume was not initializing to 50% by ensuring the priority manager configuration is loaded during refresh cycles.

Previously, the priority manager initialization was only called during the initial audio system setup in the init() function. However, when the system refreshes audio devices or configurations, the priority settings were not being reapplied, leading to incorrect volume initialization for audio inputs.

Log: Fixed audio input volume initialization to correctly set to 50% on startup

Influence:

  1. Test audio input volume initialization after system boot
  2. Verify audio input volume is correctly set to 50% when new audio devices are connected
  3. Check that audio configuration persists correctly during refresh operations
  4. Test with multiple audio input devices to ensure consistent behavior
  5. Verify volume settings are maintained after audio system restarts

fix: 修复音频输入音量初始化问题

refresh() 函数现在会调用 GetPriorityManager().Init(a.cards) 来确保在刷新 操作期间正确初始化音频配置。这解决了音频输入音量未初始化为50%的问题,通
过在刷新周期中加载优先级管理器配置。

之前优先级管理器初始化仅在 init() 函数的初始音频系统设置期间调用。然而,
当系统刷新音频设备或配置时,优先级设置没有被重新应用,导致音频输入音量初
始化不正确。

Log: 修复音频输入音量初始化,确保启动时正确设置为50%
PMS: BUG-347769
Influence:

  1. 测试系统启动后的音频输入音量初始化
  2. 验证连接新音频设备时音频输入音量正确设置为50%
  3. 检查音频配置在刷新操作期间是否正确保持
  4. 使用多个音频输入设备测试以确保一致行为
  5. 验证音频系统重启后音量设置是否保持

Summary by Sourcery

Bug Fixes:

  • Fix audio input volume not initializing to the expected default when audio devices or configurations are refreshed.

The refresh() function now calls GetPriorityManager().Init(a.cards)
to ensure audio configuration is properly initialized during refresh
operations. This addresses the issue where audio input volume was not
initializing to 50% by ensuring the priority manager configuration is
loaded during refresh cycles.

Previously, the priority manager initialization was only called during
the initial audio system setup in the init() function. However, when the
system refreshes audio devices or configurations, the priority settings
were not being reapplied, leading to incorrect volume initialization for
audio inputs.

Log: Fixed audio input volume initialization to correctly set to 50%
on startup

Influence:
1. Test audio input volume initialization after system boot
2. Verify audio input volume is correctly set to 50% when new audio
devices are connected
3. Check that audio configuration persists correctly during refresh
operations
4. Test with multiple audio input devices to ensure consistent behavior
5. Verify volume settings are maintained after audio system restarts

fix: 修复音频输入音量初始化问题

refresh() 函数现在会调用 GetPriorityManager().Init(a.cards) 来确保在刷新
操作期间正确初始化音频配置。这解决了音频输入音量未初始化为50%的问题,通
过在刷新周期中加载优先级管理器配置。

之前优先级管理器初始化仅在 init() 函数的初始音频系统设置期间调用。然而,
当系统刷新音频设备或配置时,优先级设置没有被重新应用,导致音频输入音量初
始化不正确。

Log: 修复音频输入音量初始化,确保启动时正确设置为50%
PMS: BUG-347769
Influence:
1. 测试系统启动后的音频输入音量初始化
2. 验证连接新音频设备时音频输入音量正确设置为50%
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

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 14, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR ensures the audio priority manager is re‑initialized during audio refresh cycles instead of only at initial setup, so that audio input volumes consistently initialize to 50% when devices or configurations change.

Sequence diagram for audio refresh with priority manager reinitialization

sequenceDiagram
  actor User
  participant Audio
  participant PriorityManager

  User ->> Audio: triggerRefresh()
  Audio ->> Audio: refreshSources()
  Audio ->> Audio: refershSinkInputs()
  Audio ->> PriorityManager: Init(cards)
  Audio ->> Audio: refreshDefaultSinkSource()
  Audio ->> Audio: refreshBluetoothModeOpts()
Loading

Updated class diagram for Audio and PriorityManager interaction

classDiagram
  class Audio {
    +refresh()
    +init() error
    +refreshSources()
    +refershSinkInputs()
    +refreshDefaultSinkSource()
    +refreshBluetoothModeOpts()
    ctx
    eventChan
    stateChan
    cards
  }

  class PriorityManager {
    +Init(cards)
  }

  Audio --> PriorityManager : uses
Loading

File-Level Changes

Change Details Files
Reinitialize audio priority configuration during refresh instead of only during initial init so volume defaults are consistently applied.
  • Invoke the priority manager initialization with current audio cards at the end of the refresh sequence, after sources and sink inputs are refreshed but before defaults and Bluetooth options are handled.
  • Remove priority manager initialization from the init() function so that configuration loading is tied to refresh operations rather than one-time startup.
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

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的 git diff 显示了对音频管理模块中初始化逻辑的调整。以下是从语法逻辑、代码质量、代码性能和代码安全四个方面进行的详细审查和改进意见:

1. 语法逻辑

  • 现状:代码将 GetPriorityManager().Init(a.cards)init() 函数(初始化阶段)移动到了 refresh() 函数(运行时刷新阶段)。
  • 潜在问题
    • 逻辑依赖风险refresh() 函数通常用于响应系统状态变化(如热插拔)。将核心配置初始化逻辑移至此处,意味着每次系统音频设备状态变更时,都会重新初始化优先级管理器。
    • 调用时机init() 通常在模块加载时仅执行一次,而 refresh() 可能会被频繁调用。如果 Init 函数内部包含较重的逻辑(如文件读取、复杂计算),频繁调用是不合理的。
  • 改进意见
    • 确认 PriorityManager 的设计意图。如果它只需要根据当前的 a.cards 更新权重,建议将 Init 重命名为更语义化的名字(如 UpdatePriorities),并确保它是幂等的(多次调用结果一致且副作用可控)。
    • 如果 Init 包含加载全局静态配置的逻辑,它不应该被放在 refresh 中反复执行。

2. 代码质量

  • 现状:代码中添加了 logger.Debug 日志,方便追踪执行流程。
  • 潜在问题
    • 拼写错误:在 refresh() 函数中,调用 sinkinputs 刷新的代码写成了 a.refershSinkInputs()refersh 拼写错误)。虽然这不在本次 diff 的修改行内,但既然审查代码,建议一并修正。
    • 命名规范:如上所述,如果 Initrefresh 中调用,命名容易产生误导。
  • 改进意见
    • 修正拼写错误:将 refershSinkInputs 改为 refreshSinkInputs
    • 优化日志:日志内容 "refresh config" 比较笼统,建议改为 "refresh priority config" 或 "update priority manager",以准确反映操作内容。

3. 代码性能

  • 现状:将 Init 移动到 refresh 中,意味着每次音频设备变动都会触发。
  • 潜在问题
    • 如果 GetPriorityManager().Init 内部涉及文件 I/O 或复杂的锁竞争,在 refresh 这种高频回调路径中调用会导致性能瓶颈或界面卡顿。
  • 改进意见
    • 性能剖析:检查 Init 的具体实现。如果它仅仅是基于内存中的 a.cards 重新计算优先级,那么性能影响可能不大。如果涉及文件读取,建议将文件读取保留在 init() 或单独的加载函数中,而将基于 cards 的更新逻辑剥离出来放在 refresh 中。

4. 代码安全

  • 现状GetPriorityManager() 返回一个单例或全局对象,并调用其方法。
  • 潜在问题
    • 并发安全refresh() 函数通常在事件处理的 goroutine 中执行(如 handleEvent)。如果 PriorityManager 的内部状态没有做好并发控制(如使用 sync.Mutex),在 Init 执行期间读取其状态可能会导致数据竞争。
    • 空指针风险:虽然 GetPriorityManager() 暗示了单例模式,但仍需确保该对象在调用 Init 之前已经完全初始化,且 a.cards 不为 nil。
  • 改进意见
    • 检查 PriorityManagerInit 方法是否是线程安全的。如果不是,请确保在调用前加锁,或者在 refresh 的调用链中保证串行化。
    • 增加 nil 检查:在调用 GetPriorityManager().Init(a.cards) 之前,确保 a.cards 有效。

总结建议

这段代码的修改意图可能是为了在设备变动时动态更新优先级配置。为了避免逻辑混淆和潜在的性能问题,建议:

  1. 重构 Init 方法:将其拆分为 LoadConfig(仅在启动时调用一次)和 UpdateByCards(cards)(在 refresh 时调用)。
  2. 修正拼写:修复 refershSinkInputs 的拼写错误。
  3. 增强并发控制:确保 PriorityManager 的操作是线程安全的。

修改后的代码示例建议:

func (a *Audio) refresh() {
    a.refreshSources()
    logger.Debug("refresh sinkinputs")
    a.refreshSinkInputs() // 修正拼写
    
    // 使用更明确的语义,并确保线程安全
    logger.Debug("update priority config based on cards")
    if pm := GetPriorityManager(); pm != nil {
        pm.UpdatePriorities(a.cards) 
    }
    
    logger.Debug("refresh default")
    a.refreshDefaultSinkSource()
    // ...
}

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 - I've left some high level feedback:

  • By moving GetPriorityManager().Init(a.cards) from init() to refresh(), ensure that refresh() is always invoked during startup so that priority configuration is initialized before any audio operations that depend on it.
  • Since GetPriorityManager().Init(a.cards) is now called on every refresh, confirm that Init is cheap and idempotent, or consider a guard (e.g. only re-init when a.cards changes) to avoid unnecessary repeated initialization work during frequent refreshes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- By moving `GetPriorityManager().Init(a.cards)` from `init()` to `refresh()`, ensure that `refresh()` is always invoked during startup so that priority configuration is initialized before any audio operations that depend on it.
- Since `GetPriorityManager().Init(a.cards)` is now called on every refresh, confirm that `Init` is cheap and idempotent, or consider a guard (e.g. only re-init when `a.cards` changes) to avoid unnecessary repeated initialization work during frequent refreshes.

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.

@fly602 fly602 requested review from 18202781743 and deepin-ci-robot and removed request for 18202781743 March 14, 2026 07:43
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.

2 participants