Skip to content

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Aug 21, 2025

Refactor Wayland recording module to use AVFrame cache queue instead of RGB buffer queue, enabling early RGB→YUV conversion for improved memory efficiency and performance.

Key changes:

  • Add AVFrame field to waylandFrame struct for pre-converted YUV data
  • Implement convertARGB2YUV420ToAVFrame() for early RGB→YUV conversion
  • Modify appendBuffer() to perform conversion at queue entry time
  • Optimize getFrame() to return pre-converted AVFrame data
  • Simplify writeVideoFrame() to use AVFrame directly when available
  • Add proper AVFrame memory management and cleanup

Performance improvements:

  • Reduce memory usage by ~62.5% (RGB32 4bytes/pixel → YUV420p 1.5bytes/pixel)
  • Enable parallel conversion and encoding operations
  • Decrease encoding latency through pre-conversion
  • Maintain full backward compatibility with RGB fallback

Log: Optimize Wayland recording memory usage with AVFrame pre-conversion
Bug: @https://pms.uniontech.com/bug-view-313701.html

Refactor Wayland recording module to use AVFrame cache queue instead of
RGB buffer queue, enabling early RGB→YUV conversion for improved memory
efficiency and performance.

Key changes:
- Add AVFrame field to waylandFrame struct for pre-converted YUV data
- Implement convertARGB2YUV420ToAVFrame() for early RGB→YUV conversion
- Modify appendBuffer() to perform conversion at queue entry time
- Optimize getFrame() to return pre-converted AVFrame data
- Simplify writeVideoFrame() to use AVFrame directly when available
- Add proper AVFrame memory management and cleanup

Performance improvements:
- Reduce memory usage by ~62.5% (RGB32 4bytes/pixel → YUV420p
1.5bytes/pixel)
- Enable parallel conversion and encoding operations
- Decrease encoding latency through pre-conversion
- Maintain full backward compatibility with RGB fallback

Log: Optimize Wayland recording memory usage with AVFrame pre-conversion
Bug: @https://pms.uniontech.com/bug-view-313701.html
@deepin-ci-robot
Copy link

deepin pr auto review

我对这个代码审查如下:

1. 语法逻辑

优点:

  • 代码结构清晰,逻辑分层合理
  • 使用了适当的注释说明关键步骤
  • 错误处理较为完善

改进建议:

  • 在writeVideoFrame函数中,当使用预转换的AVFrame时,直接修改了frame._avframe的pts,但没有检查frame._avframe是否为空,应该添加空指针检查
  • 在gstWriteVideoFrame函数中,虽然检查了frame._frame,但没有检查frame._avframe是否为空,可能导致访问空指针

2. 代码质量

优点:

  • 代码模块化程度高,职责划分清晰
  • 使用了RAII机制管理资源
  • 代码可读性好,变量命名规范

改进建议:

  • convertARGB2YUV420ToAVFrame函数中的魔法数字(如offset_y、offset_uv)应该定义为常量
  • 在waylandFrame结构中同时保存RGB和YUV数据可能会增加内存使用,考虑是否需要同时保存两种格式
  • 一些调试代码(如qDebug语句)应该在发布版本中移除或使用条件编译

3. 代码性能

优点:

  • 优化了视频帧处理流程,在appendBuffer中提前进行RGB到YUV的转换
  • 使用了内存池技术重用内存,减少内存分配开销

改进建议:

  • 在convertARGB2YUV420ToAVFrame函数中,可以考虑使用SIMD指令优化像素转换过程
  • 对于高频调用的函数,如getFrame,可以考虑使用更高效的数据结构
  • 在处理视频帧时,可以考虑使用多线程处理,将转换和编码分离到不同线程

4. 代码安全

优点:

  • 使用了QMutexLocker确保线程安全
  • 资源释放较为完善,避免内存泄漏

改进建议:

  • 在appendBuffer中,如果内存分配失败,应该确保所有已分配的资源都被正确释放
  • 在处理AVFrame时,应该添加更多的错误检查,特别是对av_frame_get_buffer等可能失败的操作
  • 在使用外部传入的数据时,应该进行有效性检查,防止空指针或无效数据导致的问题

5. 其他建议

  1. 建议添加单元测试,特别是对视频帧转换和编码功能的测试
  2. 考虑添加性能监控,记录关键操作的耗时,便于后续优化
  3. 可以考虑使用更现代的C++特性,如智能指针,来简化资源管理
  4. 建议添加日志系统,替代直接使用qDebug,便于问题追踪和调试

总体来说,这段代码质量较高,优化思路合理,但仍有改进空间,特别是在性能优化和错误处理方面。

@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 Aug 21, 2025

/merge

@deepin-bot deepin-bot bot merged commit 5a46bdf into linuxdeepin:develop/wayland-recorde-accumulate Aug 21, 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