Skip to content

fix: Add DConfig for preview with no delay.#460

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
lichaofan2008:master
Mar 12, 2026
Merged

fix: Add DConfig for preview with no delay.#460
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
lichaofan2008:master

Conversation

@lichaofan2008
Copy link
Copy Markdown
Contributor

@lichaofan2008 lichaofan2008 commented Mar 12, 2026

Add DConfig for preview with no delay.
DConfig中增加关于预览无延迟的配置。

Bug: https://pms.uniontech.com/bug-view-351345.html

v20 BUG 分支合一到v25主线
Task: https://pms.uniontech.com/task-view-383481.html

Summary by Sourcery

Add configuration-driven support to toggle preview no-delay behavior and wire it into the main data manager and image processing thread.

New Features:

  • Introduce a DataManager flag to represent whether preview should run with no delay, with getter and setter accessors.
  • Read the new previewNoDelay option from DConfig at startup to control preview behavior dynamically.

Enhancements:

  • Conditionally skip frame sleep in the main image processing loop based on the preview no-delay configuration flag to allow latency-free preview when enabled.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 12, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a new DataManager flag controlled by DConfig to optionally disable the preview throttling sleep, enabling a no-delay preview mode when configured.

Sequence diagram for DConfig controlled preview no delay behavior

sequenceDiagram
    participant Main
    participant DConfig
    participant DataManager
    participant MajorImageProcessingThread

    Main->>DConfig: load()
    alt previewNoDelay key exists and config is valid
        Main->>DConfig: value(previewNoDelay)
        DConfig-->>Main: bool previewNoDelay
        Main->>DataManager: setPreviewNoDelay(previewNoDelay)
    end

    MajorImageProcessingThread->>DataManager: isPreviewNoDelay()
    DataManager-->>MajorImageProcessingThread: bool previewNoDelay
    alt previewNoDelay is false and m_nVdWidth <= 1920
        MajorImageProcessingThread->>MajorImageProcessingThread: msleep(33)
    else previewNoDelay is true or m_nVdWidth > 1920
        MajorImageProcessingThread->>MajorImageProcessingThread: process frame without sleep
    end
Loading

Class diagram for updated DataManager preview no delay flag

classDiagram
    class DataManager {
        - static DataManager* m_dataManager
        - volatile DeviceStatus m_devStatus
        - bool m_H264EncoderExists
        - bool m_isSupportCameraSwitch
        - bool m_isPreviewNoDelay
        + DataManager* instance()
        + bool isSupportCameraSwitch()
        + void setIsSupportCameraSwitch(bool isTrue)
        + void setPreviewNoDelay(bool isTrue)
        + bool isPreviewNoDelay()
    }
Loading

File-Level Changes

Change Details Files
Introduce a preview-no-delay configuration flag in DataManager and wire it to DConfig.
  • Add m_isPreviewNoDelay member with default false to DataManager and expose getter/setter methods.
  • Read previewNoDelay key from DConfig in main and set DataManager::m_isPreviewNoDelay accordingly when present.
src/src/basepub/datamanager.h
src/main.cpp
Use the new preview-no-delay flag to conditionally skip the frame-rate limiting sleep in the image processing thread.
  • Gate the existing msleep(33) call on !DataManager::instance()->isPreviewNoDelay() in addition to the width check so that enabling previewNoDelay removes the artificial delay for qualifying resolutions.
src/src/majorimageprocessingthread.cpp

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
Copy Markdown

@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 found 1 issue, and left some high level feedback:

  • Consider marking isPreviewNoDelay() (and isSupportCameraSwitch() while you're here) as const to better express intent and allow use on const DataManager instances.
  • Access to m_isPreviewNoDelay from the processing thread goes through DataManager::instance(); if DataManager is touched from multiple threads, consider making this flag atomic or otherwise thread-safe to avoid data races.
  • The DConfig read for previewNoDelay can be simplified by skipping the keyList().contains() check and just using a default value with dconfig->value("previewNoDelay", false).toBool() to reduce lookups and keep the logic consistent with other keys.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider marking `isPreviewNoDelay()` (and `isSupportCameraSwitch()` while you're here) as `const` to better express intent and allow use on const `DataManager` instances.
- Access to `m_isPreviewNoDelay` from the processing thread goes through `DataManager::instance()`; if `DataManager` is touched from multiple threads, consider making this flag atomic or otherwise thread-safe to avoid data races.
- The DConfig read for `previewNoDelay` can be simplified by skipping the `keyList().contains()` check and just using a default value with `dconfig->value("previewNoDelay", false).toBool()` to reduce lookups and keep the logic consistent with other keys.

## Individual Comments

### Comment 1
<location path="src/src/basepub/datamanager.h" line_range="178" />
<code_context>
     volatile enum DeviceStatus m_devStatus;
     bool m_H264EncoderExists;
     bool m_isSupportCameraSwitch = false; // 是否带有摄像头开关
+    bool m_isPreviewNoDelay = false; // 是否预览无延迟
 };
 #endif // DATAMANAGER_H
</code_context>
<issue_to_address>
**issue (bug_risk):** Access to `m_isPreviewNoDelay` from multiple threads may need synchronization or an atomic type.

This flag is written from the main thread (`setPreviewNoDelay`) and read from `MajorImageProcessingThread::run()`. If those can overlap, this is a data race in standard C++. Please make it `std::atomic<bool>` or protect it with the same synchronization used for other shared `DataManager` state.
</issue_to_address>

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.

Comment thread src/src/basepub/datamanager.h
Add DConfig for preview with no delay.
DConfig中增加关于预览无延迟的配置。

Bug: https://pms.uniontech.com/bug-view-351345.html

v20 BUG 分支合一到v25主线
Task: https://pms.uniontech.com/task-view-383481.html
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

Git Diff 代码审查报告

我已仔细审查了提供的 Git diff,以下是我的分析和改进建议:

整体评价

这段代码添加了一个"预览无延迟"功能,通过配置项控制是否在视频预览时降低刷新率。代码实现了基本功能,但在一些细节上可以进一步优化。

详细审查

1. 语法逻辑

优点:

  • 配置项添加完整,包含了中英文名称、描述和权限设置
  • DataManager 中正确添加了设置和获取方法
  • 修改了视频处理逻辑,根据配置决定是否延迟

问题:

  1. previewNoDelay 配置项的 serial 字段值为 0,应该使用一个唯一且递增的值
  2. previewNoDelaydescription 描述不够清晰,建议改为 "Whether to disable preview delay for real-time effect"

2. 代码质量

优点:

  • 添加了必要的注释说明功能
  • 使用了 const 修饰符在 getter 方法上

问题:

  1. main.cpp 中对 dconfig 的检查可以简化,因为前面已经检查过 dconfig 是否有效
  2. datamanager.h 中的注释格式不一致,有些有 @brief 有些没有
  3. datamanager.h 中的成员变量命名风格不一致,m_isSupportCameraSwitch 有注释,但 m_H264EncoderExists 没有

3. 代码性能

优点:

  • 使用静态变量 noDelay 避免了每次循环都调用 isPreviewNoDelay()

问题:

  1. MajorImageProcessingThread::run() 中使用静态变量 noDelay 意味着配置更改后不会立即生效,需要重启应用才能生效
  2. 考虑到性能影响,可以在配置更改时通知相关线程,而不是使用静态变量

4. 代码安全

优点:

  • 使用了默认值 false 确保即使配置项不存在也有合理的默认行为
  • 检查了配置键是否存在

问题:

  1. 没有对配置值进行验证,虽然布尔值相对安全,但未来扩展时可能需要验证
  2. 在多线程环境下,m_isPreviewNoDelay 的读写没有加锁保护,可能导致数据竞争

改进建议

  1. 修改 previewNoDelay 配置项的 serial 字段为一个唯一值
  2. 改进 previewNoDelay 的描述文本
  3. 统一 datamanager.h 中的注释格式
  4. 考虑使用信号/槽机制或原子操作来处理配置更改,而不是使用静态变量
  5. m_isPreviewNoDelay 添加线程安全保护
  6. main.cpp 中简化 dconfig 检查

改进后的代码示例

// org.deepin.camera.encode.json
"previewNoDelay": {
    "value": false,
    "serial": 1,  // 使用唯一值
    "flags": ["global"],
    "name": "preview no delay",
    "name[zh_CN]": "是否预览无延迟",
    "description": "Whether to disable preview delay for real-time effect",
    "permissions": "readwrite",
    "visibility": "private"
}
// datamanager.h
class DataManager: public QObject
{
    Q_OBJECT
public:
    /**
     * @brief 设置是否预览无延迟
     * @param enable 是否启用无延迟模式
     */
    void setPreviewNoDelay(bool enable) { 
        QMutexLocker locker(&m_mutex);
        m_isPreviewNoDelay = enable; 
    };
    
    /**
     * @brief 获取是否预览无延迟
     * @return 是否启用无延迟模式
     */
    bool isPreviewNoDelay() const { 
        QMutexLocker locker(&m_mutex);
        return m_isPreviewNoDelay; 
    };

private:
    mutable QMutex m_mutex;
    bool m_isPreviewNoDelay = false; // 是否预览无延迟
};
// majorimageprocessingthread.cpp
void MajorImageProcessingThread::run()
{
    // ...
    // 保证画面流畅的前提下降低刷新率
    // 无延迟模式下可能增加CPU/GPU负载,某些客户要求更加实时的效果会使用此配置。
    if (!DataManager::instance()->isPreviewNoDelay() && m_nVdWidth <= 1920) {
        msleep(33);
    }
    // ...
}
// main.cpp
int main(int argc, char *argv[])
{
    // ...
    if (dconfig && dconfig->isValid()) {
        if (dconfig->keyList().contains("supportCameraSwitch")) {
            DataManager::instance()->setIsSupportCameraSwitch(dconfig->value("supportCameraSwitch").toBool());
        }
        
        if (dconfig->keyList().contains("previewNoDelay")) {
            DataManager::instance()->setPreviewNoDelay(dconfig->value("previewNoDelay", false).toBool());
        }
    }
    // ...
}

这些改进将使代码更加健壮、安全和可维护,同时保持原有功能不变。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lichaofan2008, lzwind

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

@lichaofan2008
Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot deepin-bot Bot merged commit 3f8df41 into linuxdeepin:master Mar 12, 2026
19 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