window-basic-main-screenshot.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /******************************************************************************
  2. Copyright (C) 2020 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "window-basic-main.hpp"
  15. #include "screenshot-obj.hpp"
  16. #include "qt-wrappers.hpp"
  17. static void ScreenshotTick(void *param, float);
  18. /* ========================================================================= */
  19. ScreenshotObj::ScreenshotObj(obs_source_t *source)
  20. : weakSource(OBSGetWeakRef(source))
  21. {
  22. obs_add_tick_callback(ScreenshotTick, this);
  23. }
  24. ScreenshotObj::~ScreenshotObj()
  25. {
  26. obs_enter_graphics();
  27. gs_stagesurface_destroy(stagesurf);
  28. gs_texrender_destroy(texrender);
  29. obs_leave_graphics();
  30. obs_remove_tick_callback(ScreenshotTick, this);
  31. if (th.joinable())
  32. th.join();
  33. }
  34. void ScreenshotObj::Screenshot()
  35. {
  36. OBSSource source = OBSGetStrongRef(weakSource);
  37. if (source) {
  38. cx = obs_source_get_base_width(source);
  39. cy = obs_source_get_base_height(source);
  40. } else {
  41. obs_video_info ovi;
  42. obs_get_video_info(&ovi);
  43. cx = ovi.base_width;
  44. cy = ovi.base_height;
  45. }
  46. if (!cx || !cy) {
  47. blog(LOG_WARNING, "Cannot screenshot, invalid target size");
  48. obs_remove_tick_callback(ScreenshotTick, this);
  49. deleteLater();
  50. return;
  51. }
  52. texrender = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  53. stagesurf = gs_stagesurface_create(cx, cy, GS_RGBA);
  54. gs_texrender_reset(texrender);
  55. if (gs_texrender_begin(texrender, cx, cy)) {
  56. vec4 zero;
  57. vec4_zero(&zero);
  58. gs_clear(GS_CLEAR_COLOR, &zero, 0.0f, 0);
  59. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  60. gs_blend_state_push();
  61. gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
  62. if (source) {
  63. obs_source_inc_showing(source);
  64. obs_source_video_render(source);
  65. obs_source_dec_showing(source);
  66. } else {
  67. obs_render_main_texture();
  68. }
  69. gs_blend_state_pop();
  70. gs_texrender_end(texrender);
  71. }
  72. }
  73. void ScreenshotObj::Download()
  74. {
  75. gs_stage_texture(stagesurf, gs_texrender_get_texture(texrender));
  76. }
  77. void ScreenshotObj::Copy()
  78. {
  79. uint8_t *videoData = nullptr;
  80. uint32_t videoLinesize = 0;
  81. image = QImage(cx, cy, QImage::Format::Format_RGBX8888);
  82. if (gs_stagesurface_map(stagesurf, &videoData, &videoLinesize)) {
  83. int linesize = image.bytesPerLine();
  84. for (int y = 0; y < (int)cy; y++)
  85. memcpy(image.scanLine(y),
  86. videoData + (y * videoLinesize), linesize);
  87. gs_stagesurface_unmap(stagesurf);
  88. }
  89. }
  90. void ScreenshotObj::Save()
  91. {
  92. OBSBasic *main = OBSBasic::Get();
  93. config_t *config = main->Config();
  94. const char *mode = config_get_string(config, "Output", "Mode");
  95. const char *type = config_get_string(config, "AdvOut", "RecType");
  96. const char *adv_path =
  97. strcmp(type, "Standard")
  98. ? config_get_string(config, "AdvOut", "FFFilePath")
  99. : config_get_string(config, "AdvOut", "RecFilePath");
  100. const char *rec_path =
  101. strcmp(mode, "Advanced")
  102. ? config_get_string(config, "SimpleOutput", "FilePath")
  103. : adv_path;
  104. const char *filenameFormat =
  105. config_get_string(config, "Output", "FilenameFormatting");
  106. bool overwriteIfExists =
  107. config_get_bool(config, "Output", "OverwriteIfExists");
  108. path = GetOutputFilename(
  109. rec_path, "png", false, overwriteIfExists,
  110. GetFormatString(filenameFormat, "Screenshot", nullptr).c_str());
  111. th = std::thread([this] { MuxAndFinish(); });
  112. }
  113. void ScreenshotObj::MuxAndFinish()
  114. {
  115. image.save(QT_UTF8(path.c_str()));
  116. blog(LOG_INFO, "Saved screenshot to '%s'", path.c_str());
  117. deleteLater();
  118. }
  119. /* ========================================================================= */
  120. #define STAGE_SCREENSHOT 0
  121. #define STAGE_DOWNLOAD 1
  122. #define STAGE_COPY_AND_SAVE 2
  123. #define STAGE_FINISH 3
  124. static void ScreenshotTick(void *param, float)
  125. {
  126. ScreenshotObj *data = reinterpret_cast<ScreenshotObj *>(param);
  127. if (data->stage == STAGE_FINISH) {
  128. return;
  129. }
  130. obs_enter_graphics();
  131. switch (data->stage) {
  132. case STAGE_SCREENSHOT:
  133. data->Screenshot();
  134. break;
  135. case STAGE_DOWNLOAD:
  136. data->Download();
  137. break;
  138. case STAGE_COPY_AND_SAVE:
  139. data->Copy();
  140. QMetaObject::invokeMethod(data, "Save");
  141. obs_remove_tick_callback(ScreenshotTick, data);
  142. break;
  143. }
  144. obs_leave_graphics();
  145. data->stage++;
  146. }
  147. void OBSBasic::Screenshot(OBSSource source)
  148. {
  149. if (!!screenshotData) {
  150. blog(LOG_WARNING, "Cannot take new screenshot, "
  151. "screenshot currently in progress");
  152. return;
  153. }
  154. screenshotData = new ScreenshotObj(source);
  155. }
  156. void OBSBasic::ScreenshotSelectedSource()
  157. {
  158. OBSSceneItem item = GetCurrentSceneItem();
  159. if (item) {
  160. Screenshot(obs_sceneitem_get_source(item));
  161. } else {
  162. blog(LOG_INFO, "Could not take a source screenshot: "
  163. "no source selected");
  164. }
  165. }
  166. void OBSBasic::ScreenshotProgram()
  167. {
  168. Screenshot(GetProgramSource());
  169. }
  170. void OBSBasic::ScreenshotScene()
  171. {
  172. Screenshot(GetCurrentSceneSource());
  173. }