window-basic-main-screenshot.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. bool success = false;
  82. image = QImage(cx, cy, QImage::Format::Format_RGBX8888);
  83. if (gs_stagesurface_map(stagesurf, &videoData, &videoLinesize)) {
  84. int linesize = image.bytesPerLine();
  85. for (int y = 0; y < (int)cy; y++)
  86. memcpy(image.scanLine(y),
  87. videoData + (y * videoLinesize), linesize);
  88. gs_stagesurface_unmap(stagesurf);
  89. success = true;
  90. }
  91. }
  92. void ScreenshotObj::Save()
  93. {
  94. OBSBasic *main = OBSBasic::Get();
  95. config_t *config = main->Config();
  96. const char *mode = config_get_string(config, "Output", "Mode");
  97. const char *type = config_get_string(config, "AdvOut", "RecType");
  98. const char *adv_path =
  99. strcmp(type, "Standard")
  100. ? config_get_string(config, "AdvOut", "FFFilePath")
  101. : config_get_string(config, "AdvOut", "RecFilePath");
  102. const char *rec_path =
  103. strcmp(mode, "Advanced")
  104. ? config_get_string(config, "SimpleOutput", "FilePath")
  105. : adv_path;
  106. const char *filenameFormat =
  107. config_get_string(config, "Output", "FilenameFormatting");
  108. bool overwriteIfExists =
  109. config_get_bool(config, "Output", "OverwriteIfExists");
  110. path = GetOutputFilename(
  111. rec_path, "png", false, overwriteIfExists,
  112. GetFormatString(filenameFormat, "Screenshot", nullptr).c_str());
  113. th = std::thread([this] { MuxAndFinish(); });
  114. }
  115. void ScreenshotObj::MuxAndFinish()
  116. {
  117. image.save(QT_UTF8(path.c_str()));
  118. blog(LOG_INFO, "Saved screenshot to '%s'", path.c_str());
  119. deleteLater();
  120. }
  121. /* ========================================================================= */
  122. #define STAGE_SCREENSHOT 0
  123. #define STAGE_DOWNLOAD 1
  124. #define STAGE_COPY_AND_SAVE 2
  125. #define STAGE_FINISH 3
  126. static void ScreenshotTick(void *param, float)
  127. {
  128. ScreenshotObj *data = reinterpret_cast<ScreenshotObj *>(param);
  129. if (data->stage == STAGE_FINISH) {
  130. return;
  131. }
  132. obs_enter_graphics();
  133. switch (data->stage) {
  134. case STAGE_SCREENSHOT:
  135. data->Screenshot();
  136. break;
  137. case STAGE_DOWNLOAD:
  138. data->Download();
  139. break;
  140. case STAGE_COPY_AND_SAVE:
  141. data->Copy();
  142. QMetaObject::invokeMethod(data, "Save");
  143. obs_remove_tick_callback(ScreenshotTick, data);
  144. break;
  145. }
  146. obs_leave_graphics();
  147. data->stage++;
  148. }
  149. void OBSBasic::Screenshot(OBSSource source)
  150. {
  151. if (!!screenshotData) {
  152. blog(LOG_WARNING, "Cannot take new screenshot, "
  153. "screenshot currently in progress");
  154. return;
  155. }
  156. screenshotData = new ScreenshotObj(source);
  157. }
  158. void OBSBasic::ScreenshotSelectedSource()
  159. {
  160. OBSSceneItem item = GetCurrentSceneItem();
  161. if (item) {
  162. Screenshot(obs_sceneitem_get_source(item));
  163. } else {
  164. blog(LOG_INFO, "Could not take a source screenshot: "
  165. "no source selected");
  166. }
  167. }
  168. void OBSBasic::ScreenshotProgram()
  169. {
  170. Screenshot(GetProgramSource());
  171. }
  172. void OBSBasic::ScreenshotScene()
  173. {
  174. Screenshot(GetCurrentSceneSource());
  175. }