Skip to content

Comments

fix: fix audio resume logic for default sink/source#1026

Merged
robertkill merged 1 commit intolinuxdeepin:masterfrom
robertkill:master
Feb 4, 2026
Merged

fix: fix audio resume logic for default sink/source#1026
robertkill merged 1 commit intolinuxdeepin:masterfrom
robertkill:master

Conversation

@robertkill
Copy link
Contributor

@robertkill robertkill commented Feb 4, 2026

The previous logic had a bug where resumeSinkConfig and resumeSourceConfig would only be called if autoSwitchOutputPort or autoSwitchInputPort returned false. This could cause issues when a default sink or source already exists (non-nil) but auto-switching succeeded. The fix ensures that resume config is also called when a default sink or source is already present, regardless of the auto- switch result. This prevents potential audio configuration issues after initialization.

Influence:

  1. Test audio initialization with existing default sink and source
  2. Verify audio output resumes correctly after system startup
  3. Test audio input configuration persistence
  4. Verify behavior when auto-switching succeeds but default devices exist
  5. Test audio functionality after suspend/resume cycles

fix: 修复默认音频输入输出设备的恢复逻辑

之前的逻辑存在一个缺陷,只有当自动切换输出端口或输入端口返回 false 时,
才会调用 resumeSinkConfig 和 resumeSourceConfig。这可能导致当默认音频输 出或输入设备已经存在(非空)但自动切换成功时出现问题。修复确保当默认音频
输出或输入设备已经存在时,无论自动切换结果如何,都会调用恢复配置。这可以
防止初始化后可能出现的音频配置问题。

Influence:

  1. 测试存在默认音频输出和输入设备时的音频初始化
  2. 验证系统启动后音频输出是否正确恢复
  3. 测试音频输入配置的持久性
  4. 验证当自动切换成功但默认设备存在时的行为
  5. 测试系统休眠唤醒后的音频功能

PMS: BUG-349719

Summary by Sourcery

Bug Fixes:

  • Fix audio resume logic so sink and source configurations are restored whenever default devices exist, regardless of auto-switch success.

The previous logic had a bug where resumeSinkConfig and
resumeSourceConfig would only be called if autoSwitchOutputPort or
autoSwitchInputPort returned false. This could cause issues when a
default sink or source already exists (non-nil) but auto-switching
succeeded. The fix ensures that resume config is also called when a
default sink or source is already present, regardless of the auto-
switch result. This prevents potential audio configuration issues after
initialization.

Influence:
1. Test audio initialization with existing default sink and source
2. Verify audio output resumes correctly after system startup
3. Test audio input configuration persistence
4. Verify behavior when auto-switching succeeds but default devices
exist
5. Test audio functionality after suspend/resume cycles

fix: 修复默认音频输入输出设备的恢复逻辑

之前的逻辑存在一个缺陷,只有当自动切换输出端口或输入端口返回 false 时,
才会调用 resumeSinkConfig 和 resumeSourceConfig。这可能导致当默认音频输
出或输入设备已经存在(非空)但自动切换成功时出现问题。修复确保当默认音频
输出或输入设备已经存在时,无论自动切换结果如何,都会调用恢复配置。这可以
防止初始化后可能出现的音频配置问题。

Influence:
1. 测试存在默认音频输出和输入设备时的音频初始化
2. 验证系统启动后音频输出是否正确恢复
3. 测试音频输入配置的持久性
4. 验证当自动切换成功但默认设备存在时的行为
5. 测试系统休眠唤醒后的音频功能

PMS: BUG-349719
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 4, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts audio initialization so that sink/source resume configuration is applied whenever a default device exists, even if auto-switching to ports succeeds, preventing missed resume of audio settings.

Sequence diagram for updated audio init resume logic

sequenceDiagram
    participant Audio
    participant OutputPort
    participant SinkConfig
    participant InputPort
    participant SourceConfig

    Audio->>Audio: init()
    activate Audio

    Audio->>OutputPort: autoSwitchOutputPort()
    OutputPort-->>Audio: successOrFailure
    alt autoSwitchOutputPort_failed OR defaultSink_not_nil
        Audio->>SinkConfig: resumeSinkConfig(defaultSink)
    end

    Audio->>InputPort: autoSwitchInputPort()
    InputPort-->>Audio: successOrFailure
    alt autoSwitchInputPort_failed OR defaultSource_not_nil
        Audio->>SourceConfig: resumeSourceConfig(defaultSource)
    end

    deactivate Audio
Loading

Class diagram for Audio init and resume behavior

classDiagram
    class Audio {
        +defaultSink
        +defaultSource
        +init() error
        +autoSwitchOutputPort() bool
        +autoSwitchInputPort() bool
        +resumeSinkConfig(sink)
        +resumeSourceConfig(source)
    }
Loading

File-Level Changes

Change Details Files
Ensure sink and source resume configuration runs whenever default devices exist, not only when auto-switching ports fails.
  • Modify output initialization to call resumeSinkConfig if autoSwitchOutputPort fails or a default sink is already set.
  • Modify input initialization to call resumeSourceConfig if autoSwitchInputPort fails or a default source is already set.
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

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 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.

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码修改涉及音频设备初始化逻辑中关于默认设备配置恢复的判断条件。以下是对该代码修改的审查意见,包括语法逻辑、代码质量、代码性能和代码安全四个方面:

1. 语法逻辑

问题:布尔逻辑存在潜在缺陷
原代码逻辑是:如果不需要自动切换端口,则恢复配置。
新代码逻辑是:如果不需要自动切换端口 或者 默认设备不为空,则恢复配置。

这里存在逻辑反转的风险。

  • a.defaultSink != nil 表示"存在默认设备"。
  • a.resumeSinkConfig(a.defaultSink) 的目的是恢复配置。
  • 如果 autoSwitchOutputPort() 返回 false(即不自动切换),原代码会执行恢复。
  • 如果 autoSwitchOutputPort() 返回 true(即自动切换),原代码不会执行恢复,通常这是合理的,因为自动切换逻辑可能已经处理了设备设置。
  • 新代码的问题:如果 autoSwitchOutputPort() 返回 true(自动切换),但 a.defaultSink 不为空,条件 true || true 为真,代码会执行 resumeSinkConfig。这可能导致在自动切换逻辑生效的同时,又强制覆盖了配置,或者造成了冗余操作。

改进意见
请确认业务意图。

  1. 如果意图是"只要存在默认设备就恢复配置",那么条件应该是 if a.defaultSink != nil,与 autoSwitch 无关。
  2. 如果意图是"仅在非自动切换模式下恢复配置",那么原代码 if !a.autoSwitchOutputPort() 是正确的,新代码引入了 || a.defaultSink != nil 可能是错误的。
  3. 如果意图是"在非自动切换模式下恢复配置,必须确保有设备才恢复",那么应该使用 &&(逻辑与)而不是 ||(逻辑或):
    if !a.autoSwitchOutputPort() && a.defaultSink != nil {
        a.resumeSinkConfig(a.defaultSink)
    }

2. 代码质量

问题:可读性与意图表达
当前的修改使得 if 条件变得令人困惑。|| 连接的两个子句看起来像是属于不同维度的判断(一个是模式开关,一个是状态检查),混合在一起降低了代码的可读性。

改进意见
建议根据上述确定的业务逻辑,清晰地重写条件。
如果是为了确保有设备才恢复(防御性编程):

// 只有在非自动切换模式下,且存在默认设备时,才恢复配置
if !a.autoSwitchOutputPort() {
    if a.defaultSink != nil {
        a.resumeSinkConfig(a.defaultSink)
    }
}

或者如果是为了在自动切换模式下也强制恢复(如果这是业务需求):

// 如果存在默认设备,总是尝试恢复配置(无论是否自动切换)
if a.defaultSink != nil {
    a.resumeSinkConfig(a.defaultSink)
}

同时,建议为 resumeSinkConfig 添加注释,说明它是否会处理 nil 指针,以及是否与 autoSwitch 逻辑有冲突。

3. 代码性能

分析

  • 这部分代码位于 init 函数中,通常只执行一次。
  • 增加一个指针判空检查 != nil 的性能开销可以忽略不计。
  • 没有引入循环或复杂计算。

结论
性能方面没有问题,无需优化。

4. 代码安全

问题:潜在的空指针引用风险
虽然代码中增加了 a.defaultSink != nil 的判断,但这取决于逻辑运算符。

  • 如果使用 ||(如当前 diff 所示),当 autoSwitch...false 时,即使 defaultSinknil,条件依然成立,会调用 resumeSinkConfig(nil)
  • 需要检查 resumeSinkConfig 函数内部是否对 nil 指针进行了防御性检查。如果该函数内部直接解引用指针且未判空,则在 defaultSinknil 时会导致 Panic

改进意见

  1. 防御性编程:强烈建议在调用 resumeSinkConfig 之前确保参数非空,或者在 resumeSinkConfig 内部处理 nil 情况。
  2. 如果采用逻辑与 && 的方案(如第1点所述),安全性会更高:
    if !a.autoSwitchOutputPort() && a.defaultSink != nil {
        a.resumeSinkConfig(a.defaultSink)
    }
    这样可以确保只有在设备非空时才调用恢复函数。

总结建议

这段代码修改在语法逻辑代码安全上存在较大风险。目前的 || 逻辑很可能是不正确的,因为它改变了原有的控制流,可能导致在不需要恢复配置的时候进行了恢复,或者在设备为空时调用了恢复函数。

建议修改方案
最可能符合业务逻辑(安全且有保障)的修改是使用逻辑与 &&,确保仅在非自动切换且有设备的情况下才恢复:

// 对于 Sink
if !a.autoSwitchOutputPort() && a.defaultSink != nil {
    a.resumeSinkConfig(a.defaultSink)
}

// 对于 Source
if !a.autoSwitchInputPort() && a.defaultSource != nil {
    a.resumeSourceConfig(a.defaultSource)
}

请务必结合 resumeSinkConfig 的实现和 autoSwitch 的业务逻辑进行最终确认。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@robertkill robertkill merged commit 8c3dd42 into linuxdeepin:master Feb 4, 2026
16 of 17 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