Skip to content

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Sep 1, 2025

  • Convert buffer queue to store YUV420P AVFrame instead of RGB data in FFmpeg mode
  • Move ARGB to YUV conversion from encoding to appendBuffer stage
  • Implement smart cropping for partial screen recording with even dimension validation
  • Reduce memory usage by ~50% in FFmpeg mode
  • Maintain backward compatibility for GStreamer mode

Log: Optimize Wayland recording buffer queue and cropping
Bug: https://pms.uniontech.com/bug-view-313701.html

- Convert buffer queue to store YUV420P AVFrame instead of RGB data in FFmpeg mode
- Move ARGB to YUV conversion from encoding to appendBuffer stage
- Implement smart cropping for partial screen recording with even dimension validation
- Reduce memory usage by ~50% in FFmpeg mode
- Maintain backward compatibility for GStreamer mode

Log: Optimize Wayland recording buffer queue and cropping
Bug: https://pms.uniontech.com/bug-view-313701.html
@deepin-ci-robot
Copy link

deepin pr auto review

代码审查报告

总体评价

这次代码修改主要针对Wayland录制功能进行了优化,特别是针对FFmpeg环境下的视频帧处理流程进行了改进。代码整体结构清晰,但存在一些可以改进的地方。

具体分析

1. avoutputstream.cpp 改进

优点:

  • 增加了更详细的日志输出,便于调试
  • 添加了尺寸检查机制,防止编码器配置与帧尺寸不匹配

改进建议:

// 当前代码
if (sourceFrame->width != pCodecCtx->width || sourceFrame->height != pCodecCtx->height) {
    qDebug() << "AVFrame size mismatch: frame" << sourceFrame->width << "x" << sourceFrame->height 
             << "vs codec" << pCodecCtx->width << "x" << pCodecCtx->height;
}

// 建议改进
if (sourceFrame->width != pCodecCtx->width || sourceFrame->height != pCodecCtx->height) {
    qWarning() << "AVFrame size mismatch: frame" << sourceFrame->width << "x" << sourceFrame->height 
              << "vs codec" << pCodecCtx->width << "x" << pCodecCtx->height;
    // 考虑添加尺寸调整逻辑或错误处理
    return -1; // 或其他适当的错误码
}

2. recordadmin.cpp 改进

优点:

  • 增加了尺寸对齐处理,确保编码尺寸为偶数,符合YUV编码要求

改进建议:

// 当前代码
int encodeWidth = m_selectWidth;
int encodeHeight = m_selectHeight;
if (encodeWidth % 2 == 1) encodeWidth--;
if (encodeHeight % 2 == 1) encodeHeight--;

// 建议改进
int encodeWidth = (m_selectWidth / 2) * 2;  // 确保是偶数
int encodeHeight = (m_selectHeight / 2) * 2;
if (encodeWidth == 0) encodeWidth = 2;  // 确保最小尺寸
if (encodeHeight == 0) encodeHeight = 2;

3. waylandintegration.cpp 改进

优点:

  • 优化了内存管理,减少了不必要的内存分配
  • 增加了裁剪功能,支持选择性录制区域
  • 区分了FFmpeg和非FFmpeg环境的处理逻辑

改进建议:

  1. 内存管理优化:
// 当前代码
if (m_waylandList.size() >= m_bufferSize) {
    waylandFrame old = m_waylandList.takeFirst();
    if (old._avframe) {
        avlibInterface::m_av_frame_free(&old._avframe);
    }
}

// 建议改进
if (m_waylandList.size() >= m_bufferSize) {
    waylandFrame old = m_waylandList.takeFirst();
    if (old._avframe) {
        avlibInterface::m_av_frame_free(&old._avframe);
        old._avframe = nullptr;  // 避免悬空指针
    }
    // 添加错误处理
    if (!m_waylandList.isEmpty()) {
        m_waylandList.last()._index = old._index + 1;
    }
}
  1. 裁剪参数验证:
// 当前代码
if (cropWidth <= 0 || cropHeight <= 0) {
    qWarning() << "Invalid crop dimensions:" << cropWidth << "x" << cropHeight;
    return;
}

// 建议改进
if (cropWidth <= 0 || cropHeight <= 0) {
    qWarning() << "Invalid crop dimensions:" << cropWidth << "x" << cropHeight;
    qWarning() << "Original dimensions:" << width << "x" << height;
    qWarning() << "Crop parameters:" << cropLeft << cropTop << cropRight << cropBottom;
    return -1;  // 返回错误码
}
  1. 线程安全改进:
// 当前代码
QMutexLocker locker(&m_mutex);
if (m_waylandList.size() >= m_bufferSize) {
    // ... 处理逻辑
}

// 建议改进
QMutexLocker locker(&m_mutex);
try {
    if (m_waylandList.size() >= m_bufferSize) {
        // ... 处理逻辑
    }
} catch (const std::exception& e) {
    qWarning() << "Exception in appendBuffer:" << e.what();
    return;
}

性能优化建议

  1. 内存预分配:

    • 考虑在初始化时预分配所有需要的内存,避免频繁的内存分配和释放
    • 使用内存池管理AVFrame对象
  2. 批处理优化:

    • 考虑实现帧批处理机制,减少单帧处理的开销
  3. 异步处理:

    • 考虑将YUV转换和编码操作放到独立线程,提高响应速度

代码安全性建议

  1. 边界检查:

    • 在访问数组或缓冲区前始终进行边界检查
    • 使用安全的内存操作函数(如memcpy_s替代memcpy)
  2. 错误处理:

    • 为所有可能失败的操作添加错误处理
    • 使用适当的错误码和错误信息
  3. 资源管理:

    • 使用RAII(资源获取即初始化)模式管理资源
    • 确保在所有退出路径上正确释放资源

总体而言,这次代码改进在性能和功能上都有提升,但还需要在错误处理、边界检查和资源管理方面进一步加强。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, max-lvs

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

@add-uos
Copy link
Contributor Author

add-uos commented Sep 2, 2025

/merge

@deepin-bot deepin-bot bot merged commit ebfdeda into linuxdeepin:develop/wayland-recorde-accumulate Sep 2, 2025
7 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