fix(camera): warm up shutter audio on demand - #510
Merged
deepin-bot[bot] merged 1 commit intoJul 30, 2026
Merged
Conversation
Replace QSound with QSoundEffect and silently warm the audio stream before playing each shutter sound.\n\n使用 QSoundEffect 替换 QSound,并在播放快门音前静音预热音频流。\n\nLog: 修复快门音首声音量偏小的问题\nPMS: https://pms.uniontech.com/bug-view-369937.html\nInfluence: 空闲后的快门声音量正常,且不维持持续音频流。
There was a problem hiding this comment.
Sorry @Resurgamz, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's GuideThis PR replaces QSound with QSoundEffect for the camera shutter and adds an on‑demand warmup mechanism that silently primes the audio stream before playback while ensuring it is stopped and cleaned up correctly. Sequence diagram for shutter sound warmup and playbacksequenceDiagram
actor User
participant videowidget
participant QSoundEffect as m_takePicSound
participant QTimer
User->>videowidget: flash()
videowidget->>videowidget: get_sound_of_takeing_photo()
alt [sound enabled]
videowidget->>videowidget: m_shutterSoundPending = true
videowidget->>videowidget: startShutterSoundWarmup()
opt [!m_shutterSoundWarming && status == Ready]
videowidget->>m_takePicSound: setVolume(0.0)
videowidget->>m_takePicSound: play()
end
end
m_takePicSound-->>videowidget: playingChanged
videowidget->>videowidget: onShutterSoundPlayingChanged()
alt [m_shutterSoundWarming]
alt [isPlaying]
videowidget->>m_takePicSound: stop()
else [stopped]
videowidget->>videowidget: m_shutterSoundWarming = false
videowidget->>m_takePicSound: setVolume(1.0)
alt [m_shutterSoundPending && get_sound_of_takeing_photo()]
videowidget->>videowidget: m_shutterSoundPending = false
videowidget->>QTimer: singleShot(0)
QTimer-->>videowidget: timeout lambda
videowidget->>videowidget: get_sound_of_takeing_photo()
alt [sound still enabled]
videowidget->>m_takePicSound: play()
end
else
videowidget->>videowidget: m_shutterSoundPending = false
end
end
end
m_takePicSound-->>videowidget: statusChanged
videowidget->>videowidget: onShutterSoundStatusChanged()
alt [status == Error]
videowidget->>videowidget: m_shutterSoundWarming = false
videowidget->>videowidget: m_shutterSoundPending = false
videowidget->>m_takePicSound: setVolume(1.0)
else [m_shutterSoundPending]
videowidget->>videowidget: startShutterSoundWarmup()
end
User->>videowidget: ~videowidget()
videowidget->>m_takePicSound: stop()
videowidget->>m_takePicSound: delete
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review★ 总体评分:85分 - ■ 【总体评价】 > 代码通过引入预热机制解决了QSoundEffect首次播放延迟问题,状态管理逻辑清晰且修复了析构时的重复删除问题。 > 逻辑正确且修复了内存释放隐患,但因预热机制中存在轻微的竞态条件和状态机复杂度扣15分。 - ■ 【详细分析】 > > - 1.语法逻辑(基本正确)✓ > > 代码将QSound替换为QSoundEffect,并引入了预热机制。在`startShutterSoundWarmup`中检查`status() != QSoundEffect::Ready`避免无效播放,在`onShutterSoundPlayingChanged`中正确处理了播放状态的切换。析构函数中修复了原代码对`m_takePicSound`的重复`delete`问题,改为先`stop()`再`delete`。 > > 潜在问题:在`onShutterSoundPlayingChanged`中,当`isPlaying()`为true时调用`stop()`并直接`return`,此时`m_shutterSoundWarming`仍为`true`,依赖下一次`playingChanged`信号触发来重置状态。如果由于底层音频框架异常导致未能及时发出状态变更信号,可能导致`m_shutterSoundWarming`卡在`true`状态,后续拍照将无声。 > > 建议:在调用`stop()`后,可以结合`QTimer::singleShot`增加超时保护机制,确保在一定时间后强制重置`m_shutterSoundWarming`状态;或在调用`stop()`时直接重置部分状态标志。 - > - 2.代码质量(良好)✓ > > 代码结构清晰,新增的三个函数职责单一,命名符合规范。头文件中新增的成员变量使用了默认初始化(`= false`),提高了代码的健壮性。注释保持了一致性。 > > 潜在问题:`onShutterSoundPlayingChanged`和`onShutterSoundStatusChanged`中使用了魔法数字`0.0`和`1.0`表示音量,虽然在此上下文中容易理解,但不如使用常量或枚举清晰。 > > 建议:将音量值提取为命名常量,如`const qreal kShutterSoundVolumeMuted = 0.0;`和`const qreal kShutterSoundVolumeNormal = 1.0;`。 - > - 3.代码性能(无性能问题)✓ > > 预热机制通过静音播放解决了首次播放延迟,避免了在拍照瞬间因初始化导致的卡顿。使用`QTimer::singleShot(0, ...)`将实际播放操作放入事件循环,避免了在槽函数中直接执行可能导致的重入问题。 > > 建议:保持当前实现,无需额外优化。 - > - 4.代码安全(存在0个安全漏洞)✓ > > 漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个 > > 本次代码变更未引入任何安全漏洞,不存在命令注入、内存越界等风险。资源管理通过Qt的父子对象机制和显式`delete`结合,无内存泄漏风险。 > > - 建议:无需针对安全进行额外修复。 - ■ 【改进建议代码示例】// videowidget.h
private:
static constexpr qreal kShutterSoundVolumeMuted = 0.0;
static constexpr qreal kShutterSoundVolumeNormal = 1.0;
// videowidget.cpp
void videowidget::onShutterSoundPlayingChanged()
{
if (!m_shutterSoundWarming) {
return;
}
if (m_takePicSound->isPlaying()) {
m_takePicSound->stop();
// 增加超时保护,防止状态卡死
QTimer::singleShot(100, this, [this]() {
if (m_shutterSoundWarming) {
m_shutterSoundWarming = false;
m_takePicSound->setVolume(kShutterSoundVolumeNormal);
}
});
return;
}
m_shutterSoundWarming = false;
m_takePicSound->setVolume(kShutterSoundVolumeNormal);
if (m_shutterSoundPending && get_sound_of_takeing_photo()) {
m_shutterSoundPending = false;
QTimer::singleShot(0, this, [this] {
if (get_sound_of_takeing_photo()) {
m_takePicSound->play();
}
});
return;
}
m_shutterSoundPending = false;
}
void videowidget::startShutterSoundWarmup()
{
if (m_shutterSoundWarming || m_takePicSound->status() != QSoundEffect::Ready) {
return;
}
m_shutterSoundWarming = true;
m_takePicSound->setVolume(kShutterSoundVolumeMuted);
m_takePicSound->play();
} |
max-lvs
approved these changes
Jul 30, 2026
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: max-lvs, Resurgamz The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Author
|
/merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Verification
PMS: https://pms.uniontech.com/bug-view-369937.html
Summary by Sourcery
Replace the photo shutter sound implementation with a warmed-up QSoundEffect-based playback to reduce latency while avoiding continuous audio power usage.
New Features:
Bug Fixes:
Enhancements: