Skip to content

feat: Enhance ExtCaptureSession with QPointer for safer frame handling#766

Merged
deepin-bot[bot] merged 8 commits into
linuxdeepin:develop/snipefrom
dengzhongyuan365-dev:treelandcommit1
Nov 27, 2025
Merged

feat: Enhance ExtCaptureSession with QPointer for safer frame handling#766
deepin-bot[bot] merged 8 commits into
linuxdeepin:develop/snipefrom
dengzhongyuan365-dev:treelandcommit1

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member

feat(treeland): Refactor capture management and UI transitions for TreeLand mode

  • Update cancelCapture method to avoid direct destruction of Wayland proxy, preventing crashes during cleanup.
  • Modify exitApp to handle TreeLand mode exit more gracefully, ensuring proper resource management without immediate destruction.
  • Implement new UI transition methods onTreelandSwitchToRecordUI and onTreelandSwitchToShotUI for better handling of mode switches, including toolbar adjustments and region management.
  • Enhance logging for debugging and tracking state changes during TreeLand operations.

This refactor improves stability and user experience when switching between recording and screenshot modes in the TreeLand environment.

feat: Enhance ExtCaptureSession with QPointer for safer frame handling

  • Introduced QPointer to manage ExtCaptureFrame instances, preventing dangling pointers during signal emissions.
  • Cached DMA-related data before emitting signals to ensure safe access and avoid dereferencing invalid pointers.
  • Updated frame cleanup process to use deleteLater for asynchronous deletion, enhancing stability during frame processing.

This update improves the robustness of frame handling in screen recording, particularly when dealing with DMA buffers.

…eeLand mode

- Update `cancelCapture` method to avoid direct destruction of Wayland proxy, preventing crashes during cleanup.
- Modify `exitApp` to handle TreeLand mode exit more gracefully, ensuring proper resource management without immediate destruction.
- Implement new UI transition methods `onTreelandSwitchToRecordUI` and `onTreelandSwitchToShotUI` for better handling of mode switches, including toolbar adjustments and region management.
- Enhance logging for debugging and tracking state changes during TreeLand operations.

This refactor improves stability and user experience when switching between recording and screenshot modes in the TreeLand environment.
- Introduced QPointer to manage ExtCaptureFrame instances, preventing dangling pointers during signal emissions.
- Cached DMA-related data before emitting signals to ensure safe access and avoid dereferencing invalid pointers.
- Updated frame cleanup process to use deleteLater for asynchronous deletion, enhancing stability during frame processing.

This update improves the robustness of frame handling in screen recording, particularly when dealing with DMA buffers.
- Enhanced the cancelCapture method to safely destroy the current capture session without crashing due to invalid proxies.
- Added null checks for voice volume and camera watchers to prevent crashes from uninitialized objects.
- Implemented asynchronous stopping of recording in ExtCaptureRecorder to ensure complete stop before cleanup, avoiding residual frames.
- Updated finalizeRecording to improve error handling and logging for video file creation, ensuring robust resource management.

This update enhances the stability and reliability of the screen recording features, particularly in TreeLand and Wayland environments.
- Commented out various debug logging statements in ExtCaptureIntegration and ExtCaptureRecorder to reduce console clutter during screen recording operations.
- This change aims to streamline the logging output while maintaining the ability to re-enable logging for debugging purposes if needed.

This update enhances the clarity of log outputs during screen recording processes.
- Updated MainWindow to hide the main window during recording in Treeland mode to prevent obstruction of events.
- Modified RecorderRegionShow to ensure it stays on top and is transparent for mouse events in Treeland mode.
- Improved event handling for the RecorderRegionShow to manage visibility and focus correctly.

This update enhances the user experience in Treeland mode by ensuring the recording interface is unobstructed and properly managed.
- Removed unused VAAPI initialization and encoding methods from ExtCaptureRecorder to streamline the codebase.
- Updated the header file to reflect the removal of VAAPI-related member variables and methods, enhancing clarity and maintainability.

This refactor simplifies the ExtCaptureRecorder class by eliminating unnecessary complexity related to VAAPI, improving overall code readability.
- Deleted outdated technical documents related to ext-image-copy-capture implementation, including the main implementation guide, technical summary, and multi-screen recording design.
- This cleanup helps streamline the project by removing unnecessary files that are no longer relevant to the current development efforts.

This update ensures that the repository remains focused and organized, facilitating easier navigation and maintenance.
- Added null checks and active connection validations in the TreelandCaptureManager and related classes to ensure safe destruction of objects only when the Wayland connection is active.
- Enhanced the destroy methods in various capture classes to skip destruction if the objects are not initialized, preventing potential crashes during cleanup.
- Updated logging to provide clearer insights into the state of the capture manager and context during destruction processes.

This update enhances the stability and reliability of the capture management system, particularly in Wayland environments.
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

我将对代码进行详细审查,主要关注语法逻辑、代码质量、性能和安全等方面。

  1. 语法逻辑:
  • 代码整体语法正确,使用了Qt框架的标准写法
  • 对Wayland协议的处理使用了QtWaylandClient库,接口使用正确
  • 信号槽连接使用了现代的lambda表达式,符合Qt5/6的编程规范
  1. 代码质量:
  • 添加了充分的日志记录,便于调试和问题追踪
  • 使用了QPointer来防止悬空指针问题
  • 采用了RAII原则管理资源,如智能指针的使用
  • 错误处理完善,特别是对Wayland连接状态和对象初始化的检查
  1. 性能优化:
  • 使用QTimer进行异步操作,避免阻塞主线程
  • 对DMA Buffer的处理优化了内存映射和拷贝流程
  • 减少了不必要的日志输出,将调试日志注释掉
  1. 安全性改进:
  • 在Treeland模式下添加了事件穿透处理
  • 对文件操作添加了错误检查
  • 使用QScopedPointer等智能指针管理资源
  • 对Wayland对象的destroy操作增加了初始化状态检查

具体改进建议:

  1. 在TreelandCaptureManager的析构函数中:
if (m_context) {
    delete m_context;
    m_context = nullptr;  // 确保指针置空
}
if (isActive()) {
    destroy();
}

建议改进为:

if (m_context) {
    delete m_context;
    m_context = nullptr;
}
if (isActive()) {
    destroy();
} else {
    qCDebug(dsrApp) << "Wayland connection not active, skipping destroy";
}

这样可以更安全地处理Wayland连接状态。

  1. 在ExtCaptureRecorder的stopRecording方法中:
if (m_state != Recording) {
    return;
}

建议改进为:

if (m_state != Recording && m_state != Starting && m_state != Stopping) {
    return;
}

这样可以处理更多的状态转换情况。

  1. 在ExtCaptureRecorder的onFrameReady方法中:
    建议添加对timestamp的有效性检查:
if (timestamp <= 0) {
    qCWarning(dsrApp) << "Invalid timestamp received";
    return;
}
  1. 在processDmaBufferFrame方法中:
    建议添加对DMA buffer大小的验证:
if (frame_size <= 0 || frame_size > MAX_FRAME_SIZE) {
    qCWarning(dsrApp) << "Invalid frame size:" << frame_size;
    return false;
}
  1. 在onTreelandSwitchToRecordUI方法中:
    建议添加对manager有效性的检查:
if (!manager) {
    qCWarning(dsrApp) << "TreelandCaptureManager instance is null";
    return;
}
  1. 在exitRecord方法中:
    建议使用更安全的退出方式:
QTimer::singleShot(100, []() {
    _Exit(0);
});

而不是直接调用_Exit(0),这样可以给Qt事件循环一些处理时间。

  1. 在ExtCaptureRecorder的构造函数中:
    建议初始化所有成员变量:
ExtCaptureRecorder::ExtCaptureRecorder(QObject *parent)
    : QObject(parent)
    , m_extCapture(nullptr)
    , m_frameBuffer(nullptr)
    , m_captureTimer(new QTimer(this))
    , m_state(Uninitialized)
    , m_frameCount(0)
    , m_recording(false)
    , m_streamingMode(false)
    , m_ffmpegProcess(nullptr)
    , m_ffmpegStarted(false)
    , m_firstFrameWidth(0)
    , m_firstFrameHeight(0)
    , m_firstFrameStride(0)
    , m_firstFrameTimestampNs(-1)
    , m_lastFrameTimestampNs(-1)
{
    // 初始化代码
}
  1. 在处理DMA Buffer时:
    建议添加错误恢复机制:
if (mapped_data == MAP_FAILED) {
    int error_code = errno;
    qCWarning(dsrApp) << "DMA-BUF mmap failed:" << strerror(error_code);
    // 尝试备用方案
    return tryAlternativeMapping();
}

这些改进可以提高代码的健壮性、安全性和可维护性。同时,建议在代码中添加更多的注释,特别是对于复杂的业务逻辑和关键的安全检查点。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, 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

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 54cdbde into linuxdeepin:develop/snipe Nov 27, 2025
7 of 10 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