window-basic-vcam-config.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "window-basic-vcam-config.hpp"
  2. #include "window-basic-main.hpp"
  3. #include "qt-wrappers.hpp"
  4. #include <util/util.hpp>
  5. #include <util/platform.h>
  6. enum class VCamOutputType {
  7. Internal,
  8. Scene,
  9. Source,
  10. };
  11. enum class VCamInternalType {
  12. Default,
  13. Preview,
  14. };
  15. struct VCamConfig {
  16. VCamOutputType type = VCamOutputType::Internal;
  17. VCamInternalType internal = VCamInternalType::Default;
  18. std::string scene;
  19. std::string source;
  20. };
  21. static VCamConfig *vCamConfig = nullptr;
  22. OBSBasicVCamConfig::OBSBasicVCamConfig(QWidget *parent)
  23. : QDialog(parent), ui(new Ui::OBSBasicVCamConfig)
  24. {
  25. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  26. ui->setupUi(this);
  27. auto type = (int)vCamConfig->type;
  28. ui->outputType->setCurrentIndex(type);
  29. OutputTypeChanged(type);
  30. connect(ui->outputType,
  31. static_cast<void (QComboBox::*)(int)>(
  32. &QComboBox::currentIndexChanged),
  33. this, &OBSBasicVCamConfig::OutputTypeChanged);
  34. connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
  35. &OBSBasicVCamConfig::Save);
  36. }
  37. void OBSBasicVCamConfig::OutputTypeChanged(int type)
  38. {
  39. auto list = ui->outputSelection;
  40. list->clear();
  41. switch ((VCamOutputType)type) {
  42. case VCamOutputType::Internal:
  43. list->addItem(QTStr("Basic.VCam.InternalDefault"));
  44. list->addItem(QTStr("Basic.VCam.InternalPreview"));
  45. list->setCurrentIndex((int)vCamConfig->internal);
  46. break;
  47. case VCamOutputType::Scene: {
  48. // Scenes in default order
  49. BPtr<char *> scenes = obs_frontend_get_scene_names();
  50. int idx = 0;
  51. for (char **temp = scenes; *temp; temp++) {
  52. list->addItem(*temp);
  53. if (vCamConfig->scene.compare(*temp) == 0)
  54. list->setCurrentIndex(list->count() - 1);
  55. }
  56. break;
  57. }
  58. case VCamOutputType::Source: {
  59. // Sources in alphabetical order
  60. std::vector<std::string> sources;
  61. auto AddSource = [&](obs_source_t *source) {
  62. auto name = obs_source_get_name(source);
  63. auto flags = obs_source_get_output_flags(source);
  64. if (!(obs_source_get_output_flags(source) &
  65. OBS_SOURCE_VIDEO))
  66. return;
  67. sources.push_back(name);
  68. };
  69. using AddSource_t = decltype(AddSource);
  70. obs_enum_sources(
  71. [](void *data, obs_source_t *source) {
  72. auto &AddSource =
  73. *static_cast<AddSource_t *>(data);
  74. if (!obs_source_removed(source))
  75. AddSource(source);
  76. return true;
  77. },
  78. static_cast<void *>(&AddSource));
  79. // Sort and select current item
  80. sort(sources.begin(), sources.end());
  81. for (auto &&source : sources) {
  82. list->addItem(source.c_str());
  83. if (vCamConfig->source == source)
  84. list->setCurrentIndex(list->count() - 1);
  85. }
  86. break;
  87. }
  88. }
  89. }
  90. void OBSBasicVCamConfig::Save()
  91. {
  92. auto type = (VCamOutputType)ui->outputType->currentIndex();
  93. auto out = ui->outputSelection;
  94. switch (type) {
  95. case VCamOutputType::Internal:
  96. vCamConfig->internal = (VCamInternalType)out->currentIndex();
  97. break;
  98. case VCamOutputType::Scene:
  99. vCamConfig->scene = out->currentText().toStdString();
  100. break;
  101. case VCamOutputType::Source:
  102. vCamConfig->source = out->currentText().toStdString();
  103. break;
  104. default:
  105. // unknown value, don't save type
  106. return;
  107. }
  108. vCamConfig->type = type;
  109. // If already running just update the source
  110. if (obs_frontend_virtualcam_active())
  111. UpdateOutputSource();
  112. }
  113. static void SaveCallback(obs_data_t *data, bool saving, void *)
  114. {
  115. if (saving) {
  116. OBSDataAutoRelease obj = obs_data_create();
  117. obs_data_set_int(obj, "type", (int)vCamConfig->type);
  118. obs_data_set_int(obj, "internal", (int)vCamConfig->internal);
  119. obs_data_set_string(obj, "scene", vCamConfig->scene.c_str());
  120. obs_data_set_string(obj, "source", vCamConfig->source.c_str());
  121. obs_data_set_obj(data, "virtual-camera", obj);
  122. } else {
  123. OBSDataAutoRelease obj =
  124. obs_data_get_obj(data, "virtual-camera");
  125. vCamConfig->type =
  126. (VCamOutputType)obs_data_get_int(obj, "type");
  127. vCamConfig->internal =
  128. (VCamInternalType)obs_data_get_int(obj, "internal");
  129. vCamConfig->scene = obs_data_get_string(obj, "scene");
  130. vCamConfig->source = obs_data_get_string(obj, "source");
  131. }
  132. }
  133. static void EventCallback(enum obs_frontend_event event, void *)
  134. {
  135. if (vCamConfig->type != VCamOutputType::Internal)
  136. return;
  137. // Update output source if the preview scene changes
  138. // or if the default transition is changed
  139. switch (event) {
  140. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  141. if (vCamConfig->internal != VCamInternalType::Preview)
  142. return;
  143. break;
  144. case OBS_FRONTEND_EVENT_TRANSITION_CHANGED:
  145. if (vCamConfig->internal != VCamInternalType::Default)
  146. return;
  147. break;
  148. default:
  149. return;
  150. }
  151. OBSBasicVCamConfig::UpdateOutputSource();
  152. }
  153. void OBSBasicVCamConfig::Init()
  154. {
  155. if (vCamConfig)
  156. return;
  157. vCamConfig = new VCamConfig;
  158. obs_frontend_add_save_callback(SaveCallback, nullptr);
  159. obs_frontend_add_event_callback(EventCallback, nullptr);
  160. }
  161. static obs_view_t *view = nullptr;
  162. static video_t *video = nullptr;
  163. video_t *OBSBasicVCamConfig::StartVideo()
  164. {
  165. if (!video) {
  166. view = obs_view_create();
  167. video = obs_view_add(view);
  168. }
  169. UpdateOutputSource();
  170. return video;
  171. }
  172. void OBSBasicVCamConfig::StopVideo()
  173. {
  174. if (view) {
  175. obs_view_remove(view);
  176. obs_view_set_source(view, 0, nullptr);
  177. obs_view_destroy(view);
  178. view = nullptr;
  179. }
  180. video = nullptr;
  181. }
  182. void OBSBasicVCamConfig::UpdateOutputSource()
  183. {
  184. if (!view)
  185. return;
  186. obs_source_t *source = nullptr;
  187. switch ((VCamOutputType)vCamConfig->type) {
  188. case VCamOutputType::Internal:
  189. switch (vCamConfig->internal) {
  190. case VCamInternalType::Default:
  191. source = obs_get_output_source(0);
  192. break;
  193. case VCamInternalType::Preview:
  194. OBSSource s = OBSBasic::Get()->GetCurrentSceneSource();
  195. obs_source_get_ref(s);
  196. source = s;
  197. break;
  198. }
  199. break;
  200. case VCamOutputType::Scene:
  201. source = obs_get_source_by_name(vCamConfig->scene.c_str());
  202. break;
  203. case VCamOutputType::Source:
  204. source = obs_get_source_by_name(vCamConfig->source.c_str());
  205. break;
  206. }
  207. auto current = obs_view_get_source(view, 0);
  208. if (source != current)
  209. obs_view_set_source(view, 0, source);
  210. obs_source_release(source);
  211. obs_source_release(current);
  212. }