window-basic-main-screenshot.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if (cx && cy) {
  34. OBSBasic *main = OBSBasic::Get();
  35. main->ShowStatusBarMessage(
  36. QTStr("Basic.StatusBar.ScreenshotSavedTo")
  37. .arg(QT_UTF8(path.c_str())));
  38. }
  39. }
  40. }
  41. void ScreenshotObj::Screenshot()
  42. {
  43. OBSSource source = OBSGetStrongRef(weakSource);
  44. if (source) {
  45. cx = obs_source_get_base_width(source);
  46. cy = obs_source_get_base_height(source);
  47. } else {
  48. obs_video_info ovi;
  49. obs_get_video_info(&ovi);
  50. cx = ovi.base_width;
  51. cy = ovi.base_height;
  52. }
  53. if (!cx || !cy) {
  54. blog(LOG_WARNING, "Cannot screenshot, invalid target size");
  55. obs_remove_tick_callback(ScreenshotTick, this);
  56. deleteLater();
  57. return;
  58. }
  59. texrender = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  60. stagesurf = gs_stagesurface_create(cx, cy, GS_RGBA);
  61. gs_texrender_reset(texrender);
  62. if (gs_texrender_begin(texrender, cx, cy)) {
  63. vec4 zero;
  64. vec4_zero(&zero);
  65. gs_clear(GS_CLEAR_COLOR, &zero, 0.0f, 0);
  66. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  67. gs_blend_state_push();
  68. gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
  69. if (source) {
  70. obs_source_inc_showing(source);
  71. obs_source_video_render(source);
  72. obs_source_dec_showing(source);
  73. } else {
  74. obs_render_main_texture();
  75. }
  76. gs_blend_state_pop();
  77. gs_texrender_end(texrender);
  78. }
  79. }
  80. void ScreenshotObj::Download()
  81. {
  82. gs_stage_texture(stagesurf, gs_texrender_get_texture(texrender));
  83. }
  84. void ScreenshotObj::Copy()
  85. {
  86. uint8_t *videoData = nullptr;
  87. uint32_t videoLinesize = 0;
  88. image = QImage(cx, cy, QImage::Format::Format_RGBX8888);
  89. if (gs_stagesurface_map(stagesurf, &videoData, &videoLinesize)) {
  90. int linesize = image.bytesPerLine();
  91. for (int y = 0; y < (int)cy; y++)
  92. memcpy(image.scanLine(y),
  93. videoData + (y * videoLinesize), linesize);
  94. gs_stagesurface_unmap(stagesurf);
  95. }
  96. }
  97. void ScreenshotObj::Save()
  98. {
  99. OBSBasic *main = OBSBasic::Get();
  100. config_t *config = main->Config();
  101. const char *mode = config_get_string(config, "Output", "Mode");
  102. const char *type = config_get_string(config, "AdvOut", "RecType");
  103. const char *adv_path =
  104. strcmp(type, "Standard")
  105. ? config_get_string(config, "AdvOut", "FFFilePath")
  106. : config_get_string(config, "AdvOut", "RecFilePath");
  107. const char *rec_path =
  108. strcmp(mode, "Advanced")
  109. ? config_get_string(config, "SimpleOutput", "FilePath")
  110. : adv_path;
  111. bool noSpace =
  112. config_get_bool(config, "SimpleOutput", "FileNameWithoutSpace");
  113. const char *filenameFormat =
  114. config_get_string(config, "Output", "FilenameFormatting");
  115. bool overwriteIfExists =
  116. config_get_bool(config, "Output", "OverwriteIfExists");
  117. path = GetOutputFilename(
  118. rec_path, "png", noSpace, overwriteIfExists,
  119. GetFormatString(filenameFormat, "Screenshot", nullptr).c_str());
  120. th = std::thread([this] { MuxAndFinish(); });
  121. }
  122. void ScreenshotObj::MuxAndFinish()
  123. {
  124. image.save(QT_UTF8(path.c_str()));
  125. blog(LOG_INFO, "Saved screenshot to '%s'", path.c_str());
  126. deleteLater();
  127. }
  128. /* ========================================================================= */
  129. #define STAGE_SCREENSHOT 0
  130. #define STAGE_DOWNLOAD 1
  131. #define STAGE_COPY_AND_SAVE 2
  132. #define STAGE_FINISH 3
  133. static void ScreenshotTick(void *param, float)
  134. {
  135. ScreenshotObj *data = reinterpret_cast<ScreenshotObj *>(param);
  136. if (data->stage == STAGE_FINISH) {
  137. return;
  138. }
  139. obs_enter_graphics();
  140. switch (data->stage) {
  141. case STAGE_SCREENSHOT:
  142. data->Screenshot();
  143. break;
  144. case STAGE_DOWNLOAD:
  145. data->Download();
  146. break;
  147. case STAGE_COPY_AND_SAVE:
  148. data->Copy();
  149. QMetaObject::invokeMethod(data, "Save");
  150. obs_remove_tick_callback(ScreenshotTick, data);
  151. break;
  152. }
  153. obs_leave_graphics();
  154. data->stage++;
  155. }
  156. void OBSBasic::Screenshot(OBSSource source)
  157. {
  158. if (!!screenshotData) {
  159. blog(LOG_WARNING, "Cannot take new screenshot, "
  160. "screenshot currently in progress");
  161. return;
  162. }
  163. screenshotData = new ScreenshotObj(source);
  164. }
  165. void OBSBasic::ScreenshotSelectedSource()
  166. {
  167. OBSSceneItem item = GetCurrentSceneItem();
  168. if (item) {
  169. Screenshot(obs_sceneitem_get_source(item));
  170. } else {
  171. blog(LOG_INFO, "Could not take a source screenshot: "
  172. "no source selected");
  173. }
  174. }
  175. void OBSBasic::ScreenshotProgram()
  176. {
  177. Screenshot(GetProgramSource());
  178. }
  179. void OBSBasic::ScreenshotScene()
  180. {
  181. Screenshot(GetCurrentSceneSource());
  182. }