window-basic-vcam-config.cpp 6.2 KB

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