1
0

multiview.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #include "multiview.hpp"
  2. #include "window-basic-main.hpp"
  3. #include "obs-app.hpp"
  4. #include "platform.hpp"
  5. #include "display-helpers.hpp"
  6. Multiview::Multiview()
  7. {
  8. InitSafeAreas(&actionSafeMargin, &graphicsSafeMargin,
  9. &fourByThreeSafeMargin, &leftLine, &topLine, &rightLine);
  10. }
  11. Multiview::~Multiview()
  12. {
  13. for (OBSWeakSource &weakSrc : multiviewScenes) {
  14. OBSSource src = OBSGetStrongRef(weakSrc);
  15. if (src)
  16. obs_source_dec_showing(src);
  17. }
  18. obs_enter_graphics();
  19. gs_vertexbuffer_destroy(actionSafeMargin);
  20. gs_vertexbuffer_destroy(graphicsSafeMargin);
  21. gs_vertexbuffer_destroy(fourByThreeSafeMargin);
  22. gs_vertexbuffer_destroy(leftLine);
  23. gs_vertexbuffer_destroy(topLine);
  24. gs_vertexbuffer_destroy(rightLine);
  25. obs_leave_graphics();
  26. }
  27. static OBSSource CreateLabel(const char *name, size_t h)
  28. {
  29. OBSDataAutoRelease settings = obs_data_create();
  30. OBSDataAutoRelease font = obs_data_create();
  31. std::string text;
  32. text += " ";
  33. text += name;
  34. text += " ";
  35. #if defined(_WIN32)
  36. obs_data_set_string(font, "face", "Arial");
  37. #elif defined(__APPLE__)
  38. obs_data_set_string(font, "face", "Helvetica");
  39. #else
  40. obs_data_set_string(font, "face", "Monospace");
  41. #endif
  42. obs_data_set_int(font, "flags", 1); // Bold text
  43. obs_data_set_int(font, "size", int(h / 9.81));
  44. obs_data_set_obj(settings, "font", font);
  45. obs_data_set_string(settings, "text", text.c_str());
  46. obs_data_set_bool(settings, "outline", false);
  47. #ifdef _WIN32
  48. const char *text_source_id = "text_gdiplus";
  49. #else
  50. const char *text_source_id = "text_ft2_source";
  51. #endif
  52. OBSSourceAutoRelease txtSource =
  53. obs_source_create_private(text_source_id, name, settings);
  54. return txtSource.Get();
  55. }
  56. void Multiview::Update(MultiviewLayout multiviewLayout, bool drawLabel,
  57. bool drawSafeArea)
  58. {
  59. this->multiviewLayout = multiviewLayout;
  60. this->drawLabel = drawLabel;
  61. this->drawSafeArea = drawSafeArea;
  62. multiviewScenes.clear();
  63. multiviewLabels.clear();
  64. struct obs_video_info ovi;
  65. obs_get_video_info(&ovi);
  66. uint32_t w = ovi.base_width;
  67. uint32_t h = ovi.base_height;
  68. fw = float(w);
  69. fh = float(h);
  70. ratio = fw / fh;
  71. struct obs_frontend_source_list scenes = {};
  72. obs_frontend_get_scenes(&scenes);
  73. multiviewLabels.emplace_back(
  74. CreateLabel(Str("StudioMode.Preview"), h / 2));
  75. multiviewLabels.emplace_back(
  76. CreateLabel(Str("StudioMode.Program"), h / 2));
  77. switch (multiviewLayout) {
  78. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  79. pvwprgCX = fw / 2;
  80. pvwprgCY = fh / 2;
  81. maxSrcs = 18;
  82. break;
  83. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  84. pvwprgCX = fw / 3;
  85. pvwprgCY = fh / 3;
  86. maxSrcs = 24;
  87. break;
  88. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  89. pvwprgCX = fw / 2;
  90. pvwprgCY = fh / 2;
  91. maxSrcs = 4;
  92. break;
  93. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  94. pvwprgCX = fw / 3;
  95. pvwprgCY = fh / 3;
  96. maxSrcs = 9;
  97. break;
  98. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  99. pvwprgCX = fw / 4;
  100. pvwprgCY = fh / 4;
  101. maxSrcs = 16;
  102. break;
  103. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  104. pvwprgCX = fw / 5;
  105. pvwprgCY = fh / 5;
  106. maxSrcs = 25;
  107. break;
  108. default:
  109. pvwprgCX = fw / 2;
  110. pvwprgCY = fh / 2;
  111. maxSrcs = 8;
  112. }
  113. ppiCX = pvwprgCX - thicknessx2;
  114. ppiCY = pvwprgCY - thicknessx2;
  115. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  116. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  117. switch (multiviewLayout) {
  118. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  119. scenesCX = pvwprgCX / 3;
  120. scenesCY = pvwprgCY / 3;
  121. break;
  122. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  123. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  124. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  125. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  126. scenesCX = pvwprgCX;
  127. scenesCY = pvwprgCY;
  128. break;
  129. default:
  130. scenesCX = pvwprgCX / 2;
  131. scenesCY = pvwprgCY / 2;
  132. }
  133. siCX = scenesCX - thicknessx2;
  134. siCY = scenesCY - thicknessx2;
  135. siScaleX = (scenesCX - thicknessx2) / fw;
  136. siScaleY = (scenesCY - thicknessx2) / fh;
  137. numSrcs = 0;
  138. size_t i = 0;
  139. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  140. obs_source_t *src = scenes.sources.array[i++];
  141. OBSDataAutoRelease data = obs_source_get_private_settings(src);
  142. obs_data_set_default_bool(data, "show_in_multiview", true);
  143. if (!obs_data_get_bool(data, "show_in_multiview"))
  144. continue;
  145. // We have a displayable source.
  146. numSrcs++;
  147. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  148. obs_source_inc_showing(src);
  149. multiviewLabels.emplace_back(
  150. CreateLabel(obs_source_get_name(src), h / 3));
  151. }
  152. obs_frontend_source_list_free(&scenes);
  153. }
  154. static inline uint32_t labelOffset(MultiviewLayout multiviewLayout,
  155. obs_source_t *label, uint32_t cx)
  156. {
  157. uint32_t w = obs_source_get_width(label);
  158. int n; // Twice of scale factor of preview and program scenes
  159. switch (multiviewLayout) {
  160. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  161. n = 6;
  162. break;
  163. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  164. n = 5;
  165. break;
  166. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  167. n = 3;
  168. break;
  169. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  170. n = 2;
  171. break;
  172. default:
  173. n = 4;
  174. break;
  175. }
  176. w = uint32_t(w * ((1.0f) / n));
  177. return (cx / 2) - w;
  178. }
  179. void Multiview::Render(uint32_t cx, uint32_t cy)
  180. {
  181. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  182. uint32_t targetCX, targetCY;
  183. int x, y;
  184. float scale;
  185. targetCX = (uint32_t)fw;
  186. targetCY = (uint32_t)fh;
  187. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  188. OBSSource previewSrc = main->GetCurrentSceneSource();
  189. OBSSource programSrc = main->GetProgramSource();
  190. bool studioMode = main->IsPreviewProgramMode();
  191. auto drawBox = [&](float cx, float cy, uint32_t colorVal) {
  192. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  193. gs_eparam_t *color =
  194. gs_effect_get_param_by_name(solid, "color");
  195. gs_effect_set_color(color, colorVal);
  196. while (gs_effect_loop(solid, "Solid"))
  197. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  198. };
  199. auto setRegion = [&](float bx, float by, float cx, float cy) {
  200. float vX = int(x + bx * scale);
  201. float vY = int(y + by * scale);
  202. float vCX = int(cx * scale);
  203. float vCY = int(cy * scale);
  204. float oL = bx;
  205. float oT = by;
  206. float oR = (bx + cx);
  207. float oB = (by + cy);
  208. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  209. };
  210. auto calcBaseSource = [&](size_t i) {
  211. switch (multiviewLayout) {
  212. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  213. sourceX = (i % 6) * scenesCX;
  214. sourceY = pvwprgCY + (i / 6) * scenesCY;
  215. break;
  216. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  217. sourceX = (i % 6) * scenesCX;
  218. sourceY = pvwprgCY + (i / 6) * scenesCY;
  219. break;
  220. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  221. sourceX = pvwprgCX;
  222. sourceY = (i / 2) * scenesCY;
  223. if (i % 2 != 0)
  224. sourceX += scenesCX;
  225. break;
  226. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  227. sourceX = 0;
  228. sourceY = (i / 2) * scenesCY;
  229. if (i % 2 != 0)
  230. sourceX = scenesCX;
  231. break;
  232. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  233. if (i < 4) {
  234. sourceX = (float(i) * scenesCX);
  235. sourceY = 0;
  236. } else {
  237. sourceX = (float(i - 4) * scenesCX);
  238. sourceY = scenesCY;
  239. }
  240. break;
  241. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  242. sourceX = (i % 2) * scenesCX;
  243. sourceY = (i / 2) * scenesCY;
  244. break;
  245. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  246. sourceX = (i % 3) * scenesCX;
  247. sourceY = (i / 3) * scenesCY;
  248. break;
  249. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  250. sourceX = (i % 4) * scenesCX;
  251. sourceY = (i / 4) * scenesCY;
  252. break;
  253. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  254. sourceX = (i % 5) * scenesCX;
  255. sourceY = (i / 5) * scenesCY;
  256. break;
  257. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  258. if (i < 4) {
  259. sourceX = (float(i) * scenesCX);
  260. sourceY = pvwprgCY;
  261. } else {
  262. sourceX = (float(i - 4) * scenesCX);
  263. sourceY = pvwprgCY + scenesCY;
  264. }
  265. }
  266. siX = sourceX + thickness;
  267. siY = sourceY + thickness;
  268. };
  269. auto calcPreviewProgram = [&](bool program) {
  270. switch (multiviewLayout) {
  271. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  272. sourceX = thickness + pvwprgCX / 2;
  273. sourceY = thickness;
  274. labelX = offset + pvwprgCX / 2;
  275. labelY = pvwprgCY * 0.85f;
  276. if (program) {
  277. sourceX += pvwprgCX;
  278. labelX += pvwprgCX;
  279. }
  280. break;
  281. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  282. sourceX = thickness;
  283. sourceY = pvwprgCY + thickness;
  284. labelX = offset;
  285. labelY = pvwprgCY * 1.85f;
  286. if (program) {
  287. sourceY = thickness;
  288. labelY = pvwprgCY * 0.85f;
  289. }
  290. break;
  291. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  292. sourceX = pvwprgCX + thickness;
  293. sourceY = pvwprgCY + thickness;
  294. labelX = pvwprgCX + offset;
  295. labelY = pvwprgCY * 1.85f;
  296. if (program) {
  297. sourceY = thickness;
  298. labelY = pvwprgCY * 0.85f;
  299. }
  300. break;
  301. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  302. sourceX = thickness;
  303. sourceY = pvwprgCY + thickness;
  304. labelX = offset;
  305. labelY = pvwprgCY * 1.85f;
  306. if (program) {
  307. sourceX += pvwprgCX;
  308. labelX += pvwprgCX;
  309. }
  310. break;
  311. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  312. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  313. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  314. sourceX = thickness;
  315. sourceY = thickness;
  316. labelX = offset;
  317. break;
  318. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES and 18_SCENES
  319. sourceX = thickness;
  320. sourceY = thickness;
  321. labelX = offset;
  322. labelY = pvwprgCY * 0.85f;
  323. if (program) {
  324. sourceX += pvwprgCX;
  325. labelX += pvwprgCX;
  326. }
  327. }
  328. };
  329. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  330. uint32_t color) {
  331. gs_matrix_push();
  332. gs_matrix_translate3f(tx, ty, 0.0f);
  333. drawBox(cx, cy, color);
  334. gs_matrix_pop();
  335. };
  336. // Define the whole usable region for the multiview
  337. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, fw, 0.0f,
  338. fh);
  339. // Change the background color to highlight all sources
  340. drawBox(fw, fh, outerColor);
  341. /* ----------------------------- */
  342. /* draw sources */
  343. for (size_t i = 0; i < maxSrcs; i++) {
  344. // Handle all the offsets
  345. calcBaseSource(i);
  346. if (i >= numSrcs) {
  347. // Just paint the background and continue
  348. paintAreaWithColor(sourceX, sourceY, scenesCX, scenesCY,
  349. outerColor);
  350. paintAreaWithColor(siX, siY, siCX, siCY,
  351. backgroundColor);
  352. continue;
  353. }
  354. OBSSource src = OBSGetStrongRef(multiviewScenes[i]);
  355. // We have a source. Now chose the proper highlight color
  356. uint32_t colorVal = outerColor;
  357. if (src == programSrc)
  358. colorVal = programColor;
  359. else if (src == previewSrc)
  360. colorVal = studioMode ? previewColor : programColor;
  361. // Paint the background
  362. paintAreaWithColor(sourceX, sourceY, scenesCX, scenesCY,
  363. colorVal);
  364. paintAreaWithColor(siX, siY, siCX, siCY, backgroundColor);
  365. /* ----------- */
  366. // Render the source
  367. gs_matrix_push();
  368. gs_matrix_translate3f(siX, siY, 0.0f);
  369. gs_matrix_scale3f(siScaleX, siScaleY, 1.0f);
  370. setRegion(siX, siY, siCX, siCY);
  371. obs_source_video_render(src);
  372. endRegion();
  373. gs_matrix_pop();
  374. /* ----------- */
  375. // Render the label
  376. if (!drawLabel)
  377. continue;
  378. obs_source *label = multiviewLabels[i + 2];
  379. if (!label)
  380. continue;
  381. offset = labelOffset(multiviewLayout, label, scenesCX);
  382. gs_matrix_push();
  383. gs_matrix_translate3f(sourceX + offset,
  384. (scenesCY * 0.85f) + sourceY, 0.0f);
  385. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  386. drawBox(obs_source_get_width(label),
  387. obs_source_get_height(label) + int(sourceY * 0.015f),
  388. labelColor);
  389. obs_source_video_render(label);
  390. gs_matrix_pop();
  391. }
  392. if (multiviewLayout == MultiviewLayout::SCENES_ONLY_4_SCENES ||
  393. multiviewLayout == MultiviewLayout::SCENES_ONLY_9_SCENES ||
  394. multiviewLayout == MultiviewLayout::SCENES_ONLY_16_SCENES ||
  395. multiviewLayout == MultiviewLayout::SCENES_ONLY_25_SCENES) {
  396. endRegion();
  397. return;
  398. }
  399. /* ----------------------------- */
  400. /* draw preview */
  401. obs_source_t *previewLabel = multiviewLabels[0];
  402. offset = labelOffset(multiviewLayout, previewLabel, pvwprgCX);
  403. calcPreviewProgram(false);
  404. // Paint the background
  405. paintAreaWithColor(sourceX, sourceY, ppiCX, ppiCY, backgroundColor);
  406. // Scale and Draw the preview
  407. gs_matrix_push();
  408. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  409. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  410. setRegion(sourceX, sourceY, ppiCX, ppiCY);
  411. if (studioMode)
  412. obs_source_video_render(previewSrc);
  413. else
  414. obs_render_main_texture();
  415. if (drawSafeArea) {
  416. RenderSafeAreas(actionSafeMargin, targetCX, targetCY);
  417. RenderSafeAreas(graphicsSafeMargin, targetCX, targetCY);
  418. RenderSafeAreas(fourByThreeSafeMargin, targetCX, targetCY);
  419. RenderSafeAreas(leftLine, targetCX, targetCY);
  420. RenderSafeAreas(topLine, targetCX, targetCY);
  421. RenderSafeAreas(rightLine, targetCX, targetCY);
  422. }
  423. endRegion();
  424. gs_matrix_pop();
  425. /* ----------- */
  426. // Draw the Label
  427. if (drawLabel) {
  428. gs_matrix_push();
  429. gs_matrix_translate3f(labelX, labelY, 0.0f);
  430. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  431. drawBox(obs_source_get_width(previewLabel),
  432. obs_source_get_height(previewLabel) +
  433. int(pvwprgCX * 0.015f),
  434. labelColor);
  435. obs_source_video_render(previewLabel);
  436. gs_matrix_pop();
  437. }
  438. /* ----------------------------- */
  439. /* draw program */
  440. obs_source_t *programLabel = multiviewLabels[1];
  441. offset = labelOffset(multiviewLayout, programLabel, pvwprgCX);
  442. calcPreviewProgram(true);
  443. paintAreaWithColor(sourceX, sourceY, ppiCX, ppiCY, backgroundColor);
  444. // Scale and Draw the program
  445. gs_matrix_push();
  446. gs_matrix_translate3f(sourceX, sourceY, 0.0f);
  447. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  448. setRegion(sourceX, sourceY, ppiCX, ppiCY);
  449. obs_render_main_texture();
  450. endRegion();
  451. gs_matrix_pop();
  452. /* ----------- */
  453. // Draw the Label
  454. if (drawLabel) {
  455. gs_matrix_push();
  456. gs_matrix_translate3f(labelX, labelY, 0.0f);
  457. gs_matrix_scale3f(ppiScaleX, ppiScaleY, 1.0f);
  458. drawBox(obs_source_get_width(programLabel),
  459. obs_source_get_height(programLabel) +
  460. int(pvwprgCX * 0.015f),
  461. labelColor);
  462. obs_source_video_render(programLabel);
  463. gs_matrix_pop();
  464. }
  465. // Region for future usage with additional info.
  466. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  467. // Just paint the background for now
  468. paintAreaWithColor(thickness, thickness, siCX,
  469. siCY * 2 + thicknessx2, backgroundColor);
  470. paintAreaWithColor(thickness + 2.5 * (thicknessx2 + ppiCX),
  471. thickness, siCX, siCY * 2 + thicknessx2,
  472. backgroundColor);
  473. }
  474. endRegion();
  475. }
  476. OBSSource Multiview::GetSourceByPosition(int x, int y)
  477. {
  478. int pos = -1;
  479. QWidget *rec = QApplication::activeWindow();
  480. if (!rec)
  481. return nullptr;
  482. int cx = rec->width();
  483. int cy = rec->height();
  484. int minX = 0;
  485. int minY = 0;
  486. int maxX = cx;
  487. int maxY = cy;
  488. switch (multiviewLayout) {
  489. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  490. if (float(cx) / float(cy) > ratio) {
  491. int validX = cy * ratio;
  492. minX = (cx / 2) - (validX / 2);
  493. maxX = (cx / 2) + (validX / 2);
  494. } else {
  495. int validY = cx / ratio;
  496. maxY = (cy / 2) + (validY / 2);
  497. }
  498. minY = cy / 2;
  499. if (x < minX || x > maxX || y < minY || y > maxY)
  500. break;
  501. pos = (x - minX) / ((maxX - minX) / 6);
  502. pos += ((y - minY) / ((maxY - minY) / 3)) * 6;
  503. break;
  504. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  505. if (float(cx) / float(cy) > ratio) {
  506. int validX = cy * ratio;
  507. minX = (cx / 2) - (validX / 2);
  508. maxX = (cx / 2) + (validX / 2);
  509. minY = cy / 3;
  510. } else {
  511. int validY = cx / ratio;
  512. maxY = (cy / 2) + (validY / 2);
  513. minY = (cy / 2) - (validY / 6);
  514. }
  515. if (x < minX || x > maxX || y < minY || y > maxY)
  516. break;
  517. pos = (x - minX) / ((maxX - minX) / 6);
  518. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  519. break;
  520. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  521. if (float(cx) / float(cy) > ratio) {
  522. int validX = cy * ratio;
  523. maxX = (cx / 2) + (validX / 2);
  524. } else {
  525. int validY = cx / ratio;
  526. minY = (cy / 2) - (validY / 2);
  527. maxY = (cy / 2) + (validY / 2);
  528. }
  529. minX = cx / 2;
  530. if (x < minX || x > maxX || y < minY || y > maxY)
  531. break;
  532. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  533. if (x > minX + ((maxX - minX) / 2))
  534. pos++;
  535. break;
  536. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  537. if (float(cx) / float(cy) > ratio) {
  538. int validX = cy * ratio;
  539. minX = (cx / 2) - (validX / 2);
  540. } else {
  541. int validY = cx / ratio;
  542. minY = (cy / 2) - (validY / 2);
  543. maxY = (cy / 2) + (validY / 2);
  544. }
  545. maxX = (cx / 2);
  546. if (x < minX || x > maxX || y < minY || y > maxY)
  547. break;
  548. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  549. if (x > minX + ((maxX - minX) / 2))
  550. pos++;
  551. break;
  552. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  553. if (float(cx) / float(cy) > ratio) {
  554. int validX = cy * ratio;
  555. minX = (cx / 2) - (validX / 2);
  556. maxX = (cx / 2) + (validX / 2);
  557. } else {
  558. int validY = cx / ratio;
  559. minY = (cy / 2) - (validY / 2);
  560. }
  561. maxY = (cy / 2);
  562. if (x < minX || x > maxX || y < minY || y > maxY)
  563. break;
  564. pos = (x - minX) / ((maxX - minX) / 4);
  565. if (y > minY + ((maxY - minY) / 2))
  566. pos += 4;
  567. break;
  568. case MultiviewLayout::SCENES_ONLY_4_SCENES:
  569. if (float(cx) / float(cy) > ratio) {
  570. int validX = cy * ratio;
  571. minX = (cx / 2) - (validX / 2);
  572. maxX = (cx / 2) + (validX / 2);
  573. } else {
  574. int validY = cx / ratio;
  575. maxY = (cy / 2) + (validY / 2);
  576. minY = (cy / 2) - (validY / 2);
  577. }
  578. if (x < minX || x > maxX || y < minY || y > maxY)
  579. break;
  580. pos = (x - minX) / ((maxX - minX) / 2);
  581. pos += ((y - minY) / ((maxY - minY) / 2)) * 2;
  582. break;
  583. case MultiviewLayout::SCENES_ONLY_9_SCENES:
  584. if (float(cx) / float(cy) > ratio) {
  585. int validX = cy * ratio;
  586. minX = (cx / 2) - (validX / 2);
  587. maxX = (cx / 2) + (validX / 2);
  588. } else {
  589. int validY = cx / ratio;
  590. maxY = (cy / 2) + (validY / 2);
  591. minY = (cy / 2) - (validY / 2);
  592. }
  593. if (x < minX || x > maxX || y < minY || y > maxY)
  594. break;
  595. pos = (x - minX) / ((maxX - minX) / 3);
  596. pos += ((y - minY) / ((maxY - minY) / 3)) * 3;
  597. break;
  598. case MultiviewLayout::SCENES_ONLY_16_SCENES:
  599. if (float(cx) / float(cy) > ratio) {
  600. int validX = cy * ratio;
  601. minX = (cx / 2) - (validX / 2);
  602. maxX = (cx / 2) + (validX / 2);
  603. } else {
  604. int validY = cx / ratio;
  605. maxY = (cy / 2) + (validY / 2);
  606. minY = (cy / 2) - (validY / 2);
  607. }
  608. if (x < minX || x > maxX || y < minY || y > maxY)
  609. break;
  610. pos = (x - minX) / ((maxX - minX) / 4);
  611. pos += ((y - minY) / ((maxY - minY) / 4)) * 4;
  612. break;
  613. case MultiviewLayout::SCENES_ONLY_25_SCENES:
  614. if (float(cx) / float(cy) > ratio) {
  615. int validX = cy * ratio;
  616. minX = (cx / 2) - (validX / 2);
  617. maxX = (cx / 2) + (validX / 2);
  618. } else {
  619. int validY = cx / ratio;
  620. maxY = (cy / 2) + (validY / 2);
  621. minY = (cy / 2) - (validY / 2);
  622. }
  623. if (x < minX || x > maxX || y < minY || y > maxY)
  624. break;
  625. pos = (x - minX) / ((maxX - minX) / 5);
  626. pos += ((y - minY) / ((maxY - minY) / 5)) * 5;
  627. break;
  628. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  629. if (float(cx) / float(cy) > ratio) {
  630. int validX = cy * ratio;
  631. minX = (cx / 2) - (validX / 2);
  632. maxX = (cx / 2) + (validX / 2);
  633. } else {
  634. int validY = cx / ratio;
  635. maxY = (cy / 2) + (validY / 2);
  636. }
  637. minY = (cy / 2);
  638. if (x < minX || x > maxX || y < minY || y > maxY)
  639. break;
  640. pos = (x - minX) / ((maxX - minX) / 4);
  641. if (y > minY + ((maxY - minY) / 2))
  642. pos += 4;
  643. }
  644. if (pos < 0 || pos >= (int)numSrcs)
  645. return nullptr;
  646. return OBSGetStrongRef(multiviewScenes[pos]);
  647. }