record_desktop_wgc.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "record_desktop_wgc.h"
  2. #include "utils_string.h"
  3. #include "system_error.h"
  4. #include "error_define.h"
  5. #include "log_helper.h"
  6. BOOL WINAPI EnumMonitorProc(HMONITOR hmonitor, HDC hdc, LPRECT lprc,
  7. LPARAM data) {
  8. MONITORINFOEX info_ex;
  9. info_ex.cbSize = sizeof(MONITORINFOEX);
  10. GetMonitorInfo(hmonitor, &info_ex);
  11. if (info_ex.dwFlags == DISPLAY_DEVICE_MIRRORING_DRIVER)
  12. return true;
  13. if (info_ex.dwFlags & MONITORINFOF_PRIMARY) {
  14. *(HMONITOR *)data = hmonitor;
  15. }
  16. return true;
  17. }
  18. HMONITOR GetPrimaryMonitor() {
  19. HMONITOR hmonitor = nullptr;
  20. ::EnumDisplayMonitors(NULL, NULL, EnumMonitorProc, (LPARAM)&hmonitor);
  21. return hmonitor;
  22. }
  23. namespace am {
  24. record_desktop_wgc::record_desktop_wgc() {}
  25. record_desktop_wgc::~record_desktop_wgc() {
  26. stop();
  27. clean_up();
  28. }
  29. int record_desktop_wgc::init(const RECORD_DESKTOP_RECT &rect, const int fps) {
  30. int error = AE_NO;
  31. if (_inited == true)
  32. return error;
  33. _fps = fps;
  34. _rect = rect;
  35. _start_time = av_gettime_relative();
  36. _time_base = {1, AV_TIME_BASE};
  37. _pixel_fmt = AV_PIX_FMT_BGRA;
  38. do {
  39. if (!module_.is_supported()) {
  40. error = AE_UNSUPPORT;
  41. break;
  42. }
  43. session_ = module_.create_session();
  44. if (!session_) {
  45. error = AE_WGC_CREATE_CAPTURER_FAILED;
  46. break;
  47. }
  48. session_->register_observer(this);
  49. error = session_->initialize(GetPrimaryMonitor());
  50. _inited = true;
  51. } while (0);
  52. if (error != AE_NO) {
  53. al_debug("%s,last error:%s", err2str(error),
  54. system_error::error2str(GetLastError()).c_str());
  55. }
  56. return error;
  57. }
  58. int record_desktop_wgc::start() {
  59. if (_running == true) {
  60. al_warn("record desktop duplication is already running");
  61. return AE_NO;
  62. }
  63. if (_inited == false) {
  64. return AE_NEED_INIT;
  65. }
  66. _running = true;
  67. session_->start();
  68. return AE_NO;
  69. }
  70. int record_desktop_wgc::pause() {
  71. _paused = true;
  72. if (session_)
  73. session_->pause();
  74. return AE_NO;
  75. }
  76. int record_desktop_wgc::resume() {
  77. _paused = false;
  78. if (session_)
  79. session_->resume();
  80. return AE_NO;
  81. }
  82. int record_desktop_wgc::stop() {
  83. _running = false;
  84. if (session_)
  85. session_->stop();
  86. return AE_NO;
  87. }
  88. void record_desktop_wgc::on_frame(const wgc_session::wgc_session_frame &frame) {
  89. al_debug("wgc on frame");
  90. AVFrame *av_frame = av_frame_alloc();
  91. av_frame->pts = av_gettime_relative();
  92. av_frame->pkt_dts = av_frame->pts;
  93. av_frame->pkt_pts = av_frame->pts;
  94. av_frame->width = frame.width;
  95. av_frame->height = frame.height;
  96. av_frame->format = AV_PIX_FMT_BGRA;
  97. av_frame->pict_type = AV_PICTURE_TYPE_NONE;
  98. av_frame->pkt_size = frame.width * frame.height * 4;
  99. av_image_fill_arrays(av_frame->data, av_frame->linesize, frame.data,
  100. AV_PIX_FMT_BGRA, frame.width, frame.height, 1);
  101. if (_on_data)
  102. _on_data(av_frame);
  103. av_frame_free(&av_frame);
  104. }
  105. void record_desktop_wgc::clean_up() {
  106. _inited = false;
  107. if (session_)
  108. session_->release();
  109. session_ = nullptr;
  110. }
  111. } // namespace am