obs-slideshow.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. #include <obs-module.h>
  2. #include <util/threading.h>
  3. #include <util/platform.h>
  4. #include <util/darray.h>
  5. #include <util/dstr.h>
  6. #define do_log(level, format, ...) \
  7. blog(level, "[slideshow: '%s'] " format, \
  8. obs_source_get_name(ss->source), ##__VA_ARGS__)
  9. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  10. /* clang-format off */
  11. #define S_TR_SPEED "transition_speed"
  12. #define S_CUSTOM_SIZE "use_custom_size"
  13. #define S_SLIDE_TIME "slide_time"
  14. #define S_TRANSITION "transition"
  15. #define S_RANDOMIZE "randomize"
  16. #define S_LOOP "loop"
  17. #define S_HIDE "hide"
  18. #define S_FILES "files"
  19. #define S_BEHAVIOR "playback_behavior"
  20. #define S_BEHAVIOR_STOP_RESTART "stop_restart"
  21. #define S_BEHAVIOR_PAUSE_UNPAUSE "pause_unpause"
  22. #define S_BEHAVIOR_ALWAYS_PLAY "always_play"
  23. #define S_MODE "slide_mode"
  24. #define S_MODE_AUTO "mode_auto"
  25. #define S_MODE_MANUAL "mode_manual"
  26. #define TR_CUT "cut"
  27. #define TR_FADE "fade"
  28. #define TR_SWIPE "swipe"
  29. #define TR_SLIDE "slide"
  30. #define T_(text) obs_module_text("SlideShow." text)
  31. #define T_TR_SPEED T_("TransitionSpeed")
  32. #define T_CUSTOM_SIZE T_("CustomSize")
  33. #define T_CUSTOM_SIZE_AUTO T_("CustomSize.Auto")
  34. #define T_SLIDE_TIME T_("SlideTime")
  35. #define T_TRANSITION T_("Transition")
  36. #define T_RANDOMIZE T_("Randomize")
  37. #define T_LOOP T_("Loop")
  38. #define T_HIDE T_("HideWhenDone")
  39. #define T_FILES T_("Files")
  40. #define T_BEHAVIOR T_("PlaybackBehavior")
  41. #define T_BEHAVIOR_STOP_RESTART T_("PlaybackBehavior.StopRestart")
  42. #define T_BEHAVIOR_PAUSE_UNPAUSE T_("PlaybackBehavior.PauseUnpause")
  43. #define T_BEHAVIOR_ALWAYS_PLAY T_("PlaybackBehavior.AlwaysPlay")
  44. #define T_MODE T_("SlideMode")
  45. #define T_MODE_AUTO T_("SlideMode.Auto")
  46. #define T_MODE_MANUAL T_("SlideMode.Manual")
  47. #define T_TR_(text) obs_module_text("SlideShow.Transition." text)
  48. #define T_TR_CUT T_TR_("Cut")
  49. #define T_TR_FADE T_TR_("Fade")
  50. #define T_TR_SWIPE T_TR_("Swipe")
  51. #define T_TR_SLIDE T_TR_("Slide")
  52. /* clang-format on */
  53. /* ------------------------------------------------------------------------- */
  54. extern uint64_t image_source_get_memory_usage(void *data);
  55. #define BYTES_TO_MBYTES (1024 * 1024)
  56. #define MAX_MEM_USAGE (400 * BYTES_TO_MBYTES)
  57. struct image_file_data {
  58. char *path;
  59. obs_source_t *source;
  60. };
  61. enum behavior {
  62. BEHAVIOR_STOP_RESTART,
  63. BEHAVIOR_PAUSE_UNPAUSE,
  64. BEHAVIOR_ALWAYS_PLAY,
  65. };
  66. struct slideshow {
  67. obs_source_t *source;
  68. bool randomize;
  69. bool loop;
  70. bool restart_on_activate;
  71. bool pause_on_deactivate;
  72. bool restart;
  73. bool manual;
  74. bool hide;
  75. bool use_cut;
  76. bool paused;
  77. bool stop;
  78. float slide_time;
  79. uint32_t tr_speed;
  80. const char *tr_name;
  81. obs_source_t *transition;
  82. float elapsed;
  83. size_t cur_item;
  84. uint32_t cx;
  85. uint32_t cy;
  86. uint64_t mem_usage;
  87. pthread_mutex_t mutex;
  88. DARRAY(struct image_file_data) files;
  89. enum behavior behavior;
  90. obs_hotkey_id play_pause_hotkey;
  91. obs_hotkey_id restart_hotkey;
  92. obs_hotkey_id stop_hotkey;
  93. obs_hotkey_id next_hotkey;
  94. obs_hotkey_id prev_hotkey;
  95. enum obs_media_state state;
  96. };
  97. static void set_media_state(void *data, enum obs_media_state state)
  98. {
  99. struct slideshow *ss = data;
  100. ss->state = state;
  101. }
  102. static enum obs_media_state ss_get_state(void *data)
  103. {
  104. struct slideshow *ss = data;
  105. return ss->state;
  106. }
  107. static obs_source_t *get_transition(struct slideshow *ss)
  108. {
  109. obs_source_t *tr;
  110. pthread_mutex_lock(&ss->mutex);
  111. tr = obs_source_get_ref(ss->transition);
  112. pthread_mutex_unlock(&ss->mutex);
  113. return tr;
  114. }
  115. static obs_source_t *get_source(struct darray *array, const char *path)
  116. {
  117. DARRAY(struct image_file_data) files;
  118. obs_source_t *source = NULL;
  119. files.da = *array;
  120. for (size_t i = 0; i < files.num; i++) {
  121. const char *cur_path = files.array[i].path;
  122. if (strcmp(path, cur_path) == 0) {
  123. source = obs_source_get_ref(files.array[i].source);
  124. break;
  125. }
  126. }
  127. return source;
  128. }
  129. static obs_source_t *create_source_from_file(const char *file)
  130. {
  131. obs_data_t *settings = obs_data_create();
  132. obs_source_t *source;
  133. obs_data_set_string(settings, "file", file);
  134. obs_data_set_bool(settings, "unload", false);
  135. source = obs_source_create_private("image_source", NULL, settings);
  136. obs_data_release(settings);
  137. return source;
  138. }
  139. static void free_files(struct darray *array)
  140. {
  141. DARRAY(struct image_file_data) files;
  142. files.da = *array;
  143. for (size_t i = 0; i < files.num; i++) {
  144. bfree(files.array[i].path);
  145. obs_source_release(files.array[i].source);
  146. }
  147. da_free(files);
  148. }
  149. static inline size_t random_file(struct slideshow *ss)
  150. {
  151. size_t next = ss->cur_item;
  152. if (ss->files.num > 1) {
  153. while (next == ss->cur_item)
  154. next = (size_t)rand() % ss->files.num;
  155. }
  156. return next;
  157. }
  158. /* ------------------------------------------------------------------------- */
  159. static const char *ss_getname(void *unused)
  160. {
  161. UNUSED_PARAMETER(unused);
  162. return obs_module_text("SlideShow");
  163. }
  164. static void add_file(struct slideshow *ss, struct darray *array,
  165. const char *path, uint32_t *cx, uint32_t *cy)
  166. {
  167. DARRAY(struct image_file_data) new_files;
  168. struct image_file_data data;
  169. obs_source_t *new_source;
  170. new_files.da = *array;
  171. pthread_mutex_lock(&ss->mutex);
  172. new_source = get_source(&ss->files.da, path);
  173. pthread_mutex_unlock(&ss->mutex);
  174. if (!new_source)
  175. new_source = get_source(&new_files.da, path);
  176. if (!new_source)
  177. new_source = create_source_from_file(path);
  178. if (new_source) {
  179. uint32_t new_cx = obs_source_get_width(new_source);
  180. uint32_t new_cy = obs_source_get_height(new_source);
  181. data.path = bstrdup(path);
  182. data.source = new_source;
  183. da_push_back(new_files, &data);
  184. if (new_cx > *cx)
  185. *cx = new_cx;
  186. if (new_cy > *cy)
  187. *cy = new_cy;
  188. void *source_data = obs_obj_get_data(new_source);
  189. ss->mem_usage += image_source_get_memory_usage(source_data);
  190. }
  191. *array = new_files.da;
  192. }
  193. static bool valid_extension(const char *ext)
  194. {
  195. if (!ext)
  196. return false;
  197. return astrcmpi(ext, ".bmp") == 0 || astrcmpi(ext, ".tga") == 0 ||
  198. astrcmpi(ext, ".png") == 0 || astrcmpi(ext, ".jpeg") == 0 ||
  199. astrcmpi(ext, ".jpg") == 0 ||
  200. #ifdef _WIN32
  201. astrcmpi(ext, ".jxr") == 0 ||
  202. #endif
  203. astrcmpi(ext, ".gif") == 0;
  204. }
  205. static inline bool item_valid(struct slideshow *ss)
  206. {
  207. return ss->files.num && ss->cur_item < ss->files.num;
  208. }
  209. static void do_transition(void *data, bool to_null)
  210. {
  211. struct slideshow *ss = data;
  212. bool valid = item_valid(ss);
  213. if (valid && ss->use_cut) {
  214. obs_transition_set(ss->transition,
  215. ss->files.array[ss->cur_item].source);
  216. } else if (valid && !to_null) {
  217. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  218. ss->tr_speed,
  219. ss->files.array[ss->cur_item].source);
  220. } else {
  221. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  222. ss->tr_speed, NULL);
  223. set_media_state(ss, OBS_MEDIA_STATE_ENDED);
  224. obs_source_media_ended(ss->source);
  225. }
  226. }
  227. static void ss_update(void *data, obs_data_t *settings)
  228. {
  229. DARRAY(struct image_file_data) new_files;
  230. DARRAY(struct image_file_data) old_files;
  231. obs_source_t *new_tr = NULL;
  232. obs_source_t *old_tr = NULL;
  233. struct slideshow *ss = data;
  234. obs_data_array_t *array;
  235. const char *tr_name;
  236. uint32_t new_duration;
  237. uint32_t new_speed;
  238. uint32_t cx = 0;
  239. uint32_t cy = 0;
  240. size_t count;
  241. const char *behavior;
  242. const char *mode;
  243. /* ------------------------------------- */
  244. /* get settings data */
  245. da_init(new_files);
  246. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  247. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0)
  248. ss->behavior = BEHAVIOR_PAUSE_UNPAUSE;
  249. else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0)
  250. ss->behavior = BEHAVIOR_ALWAYS_PLAY;
  251. else /* S_BEHAVIOR_STOP_RESTART */
  252. ss->behavior = BEHAVIOR_STOP_RESTART;
  253. mode = obs_data_get_string(settings, S_MODE);
  254. ss->manual = (astrcmpi(mode, S_MODE_MANUAL) == 0);
  255. tr_name = obs_data_get_string(settings, S_TRANSITION);
  256. if (astrcmpi(tr_name, TR_CUT) == 0)
  257. tr_name = "cut_transition";
  258. else if (astrcmpi(tr_name, TR_SWIPE) == 0)
  259. tr_name = "swipe_transition";
  260. else if (astrcmpi(tr_name, TR_SLIDE) == 0)
  261. tr_name = "slide_transition";
  262. else
  263. tr_name = "fade_transition";
  264. ss->randomize = obs_data_get_bool(settings, S_RANDOMIZE);
  265. ss->loop = obs_data_get_bool(settings, S_LOOP);
  266. ss->hide = obs_data_get_bool(settings, S_HIDE);
  267. if (!ss->tr_name || strcmp(tr_name, ss->tr_name) != 0)
  268. new_tr = obs_source_create_private(tr_name, NULL, NULL);
  269. new_duration = (uint32_t)obs_data_get_int(settings, S_SLIDE_TIME);
  270. new_speed = (uint32_t)obs_data_get_int(settings, S_TR_SPEED);
  271. array = obs_data_get_array(settings, S_FILES);
  272. count = obs_data_array_count(array);
  273. /* ------------------------------------- */
  274. /* create new list of sources */
  275. ss->mem_usage = 0;
  276. for (size_t i = 0; i < count; i++) {
  277. obs_data_t *item = obs_data_array_item(array, i);
  278. const char *path = obs_data_get_string(item, "value");
  279. os_dir_t *dir = os_opendir(path);
  280. if (dir) {
  281. struct dstr dir_path = {0};
  282. struct os_dirent *ent;
  283. for (;;) {
  284. const char *ext;
  285. ent = os_readdir(dir);
  286. if (!ent)
  287. break;
  288. if (ent->directory)
  289. continue;
  290. ext = os_get_path_extension(ent->d_name);
  291. if (!valid_extension(ext))
  292. continue;
  293. dstr_copy(&dir_path, path);
  294. dstr_cat_ch(&dir_path, '/');
  295. dstr_cat(&dir_path, ent->d_name);
  296. add_file(ss, &new_files.da, dir_path.array, &cx,
  297. &cy);
  298. if (ss->mem_usage >= MAX_MEM_USAGE)
  299. break;
  300. }
  301. dstr_free(&dir_path);
  302. os_closedir(dir);
  303. } else {
  304. add_file(ss, &new_files.da, path, &cx, &cy);
  305. }
  306. obs_data_release(item);
  307. if (ss->mem_usage >= MAX_MEM_USAGE)
  308. break;
  309. }
  310. /* ------------------------------------- */
  311. /* update settings data */
  312. pthread_mutex_lock(&ss->mutex);
  313. old_files.da = ss->files.da;
  314. ss->files.da = new_files.da;
  315. if (new_tr) {
  316. old_tr = ss->transition;
  317. ss->transition = new_tr;
  318. }
  319. if (strcmp(tr_name, "cut_transition") != 0) {
  320. if (new_duration < 100)
  321. new_duration = 100;
  322. new_duration += new_speed;
  323. } else {
  324. if (new_duration < 50)
  325. new_duration = 50;
  326. }
  327. ss->tr_speed = new_speed;
  328. ss->tr_name = tr_name;
  329. ss->slide_time = (float)new_duration / 1000.0f;
  330. pthread_mutex_unlock(&ss->mutex);
  331. /* ------------------------------------- */
  332. /* clean up and restart transition */
  333. if (old_tr)
  334. obs_source_release(old_tr);
  335. free_files(&old_files.da);
  336. /* ------------------------- */
  337. const char *res_str = obs_data_get_string(settings, S_CUSTOM_SIZE);
  338. bool aspect_only = false, use_auto = true;
  339. int cx_in = 0, cy_in = 0;
  340. if (strcmp(res_str, T_CUSTOM_SIZE_AUTO) != 0) {
  341. int ret = sscanf(res_str, "%dx%d", &cx_in, &cy_in);
  342. if (ret == 2) {
  343. aspect_only = false;
  344. use_auto = false;
  345. } else {
  346. ret = sscanf(res_str, "%d:%d", &cx_in, &cy_in);
  347. if (ret == 2) {
  348. aspect_only = true;
  349. use_auto = false;
  350. }
  351. }
  352. }
  353. if (!use_auto) {
  354. double cx_f = (double)cx;
  355. double cy_f = (double)cy;
  356. double old_aspect = cx_f / cy_f;
  357. double new_aspect = (double)cx_in / (double)cy_in;
  358. if (aspect_only) {
  359. if (fabs(old_aspect - new_aspect) > EPSILON) {
  360. if (new_aspect > old_aspect)
  361. cx = (uint32_t)(cy_f * new_aspect);
  362. else
  363. cy = (uint32_t)(cx_f / new_aspect);
  364. }
  365. } else {
  366. cx = (uint32_t)cx_in;
  367. cy = (uint32_t)cy_in;
  368. }
  369. }
  370. /* ------------------------- */
  371. ss->cx = cx;
  372. ss->cy = cy;
  373. ss->cur_item = 0;
  374. ss->elapsed = 0.0f;
  375. obs_transition_set_size(ss->transition, cx, cy);
  376. obs_transition_set_alignment(ss->transition, OBS_ALIGN_CENTER);
  377. obs_transition_set_scale_type(ss->transition,
  378. OBS_TRANSITION_SCALE_ASPECT);
  379. if (ss->randomize && ss->files.num)
  380. ss->cur_item = random_file(ss);
  381. if (new_tr)
  382. obs_source_add_active_child(ss->source, new_tr);
  383. if (ss->files.num) {
  384. do_transition(ss, false);
  385. if (ss->manual)
  386. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  387. else
  388. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  389. obs_source_media_started(ss->source);
  390. }
  391. obs_data_array_release(array);
  392. }
  393. static void ss_play_pause(void *data, bool pause)
  394. {
  395. struct slideshow *ss = data;
  396. if (ss->stop) {
  397. ss->stop = false;
  398. ss->paused = false;
  399. do_transition(ss, false);
  400. } else {
  401. ss->paused = pause;
  402. ss->manual = pause;
  403. }
  404. if (pause)
  405. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  406. else
  407. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  408. }
  409. static void ss_restart(void *data)
  410. {
  411. struct slideshow *ss = data;
  412. ss->elapsed = 0.0f;
  413. ss->cur_item = 0;
  414. ss->stop = false;
  415. ss->paused = false;
  416. do_transition(ss, false);
  417. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  418. }
  419. static void ss_stop(void *data)
  420. {
  421. struct slideshow *ss = data;
  422. ss->elapsed = 0.0f;
  423. ss->cur_item = 0;
  424. do_transition(ss, true);
  425. ss->stop = true;
  426. ss->paused = false;
  427. set_media_state(ss, OBS_MEDIA_STATE_STOPPED);
  428. }
  429. static void ss_next_slide(void *data)
  430. {
  431. struct slideshow *ss = data;
  432. if (!ss->files.num || obs_transition_get_time(ss->transition) < 1.0f)
  433. return;
  434. if (ss->randomize)
  435. ss->cur_item = random_file(ss);
  436. else if (++ss->cur_item >= ss->files.num)
  437. ss->cur_item = 0;
  438. do_transition(ss, false);
  439. }
  440. static void ss_previous_slide(void *data)
  441. {
  442. struct slideshow *ss = data;
  443. if (!ss->files.num || obs_transition_get_time(ss->transition) < 1.0f)
  444. return;
  445. if (ss->randomize)
  446. ss->cur_item = random_file(ss);
  447. else if (ss->cur_item == 0)
  448. ss->cur_item = ss->files.num - 1;
  449. else
  450. --ss->cur_item;
  451. do_transition(ss, false);
  452. }
  453. static void play_pause_hotkey(void *data, obs_hotkey_id id,
  454. obs_hotkey_t *hotkey, bool pressed)
  455. {
  456. UNUSED_PARAMETER(id);
  457. UNUSED_PARAMETER(hotkey);
  458. struct slideshow *ss = data;
  459. if (pressed && obs_source_showing(ss->source))
  460. obs_source_media_play_pause(ss->source, !ss->paused);
  461. }
  462. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  463. bool pressed)
  464. {
  465. UNUSED_PARAMETER(id);
  466. UNUSED_PARAMETER(hotkey);
  467. struct slideshow *ss = data;
  468. if (pressed && obs_source_showing(ss->source))
  469. obs_source_media_restart(ss->source);
  470. }
  471. static void stop_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  472. bool pressed)
  473. {
  474. UNUSED_PARAMETER(id);
  475. UNUSED_PARAMETER(hotkey);
  476. struct slideshow *ss = data;
  477. if (pressed && obs_source_showing(ss->source))
  478. obs_source_media_stop(ss->source);
  479. }
  480. static void next_slide_hotkey(void *data, obs_hotkey_id id,
  481. obs_hotkey_t *hotkey, bool pressed)
  482. {
  483. UNUSED_PARAMETER(id);
  484. UNUSED_PARAMETER(hotkey);
  485. struct slideshow *ss = data;
  486. if (!ss->manual)
  487. return;
  488. if (pressed && obs_source_showing(ss->source))
  489. obs_source_media_next(ss->source);
  490. }
  491. static void previous_slide_hotkey(void *data, obs_hotkey_id id,
  492. obs_hotkey_t *hotkey, bool pressed)
  493. {
  494. UNUSED_PARAMETER(id);
  495. UNUSED_PARAMETER(hotkey);
  496. struct slideshow *ss = data;
  497. if (!ss->manual)
  498. return;
  499. if (pressed && obs_source_showing(ss->source))
  500. obs_source_media_previous(ss->source);
  501. }
  502. static void current_slide_proc(void *data, calldata_t *cd)
  503. {
  504. struct slideshow *ss = data;
  505. calldata_set_int(cd, "current_index", ss->cur_item);
  506. }
  507. static void total_slides_proc(void *data, calldata_t *cd)
  508. {
  509. struct slideshow *ss = data;
  510. calldata_set_int(cd, "total_files", ss->files.num);
  511. }
  512. static void ss_destroy(void *data)
  513. {
  514. struct slideshow *ss = data;
  515. obs_source_release(ss->transition);
  516. free_files(&ss->files.da);
  517. pthread_mutex_destroy(&ss->mutex);
  518. bfree(ss);
  519. }
  520. static void *ss_create(obs_data_t *settings, obs_source_t *source)
  521. {
  522. struct slideshow *ss = bzalloc(sizeof(*ss));
  523. proc_handler_t *ph = obs_source_get_proc_handler(source);
  524. ss->source = source;
  525. ss->manual = false;
  526. ss->paused = false;
  527. ss->stop = false;
  528. ss->play_pause_hotkey = obs_hotkey_register_source(
  529. source, "SlideShow.PlayPause",
  530. obs_module_text("SlideShow.PlayPause"), play_pause_hotkey, ss);
  531. ss->restart_hotkey = obs_hotkey_register_source(
  532. source, "SlideShow.Restart",
  533. obs_module_text("SlideShow.Restart"), restart_hotkey, ss);
  534. ss->stop_hotkey = obs_hotkey_register_source(
  535. source, "SlideShow.Stop", obs_module_text("SlideShow.Stop"),
  536. stop_hotkey, ss);
  537. ss->next_hotkey = obs_hotkey_register_source(
  538. source, "SlideShow.NextSlide",
  539. obs_module_text("SlideShow.NextSlide"), next_slide_hotkey, ss);
  540. ss->prev_hotkey = obs_hotkey_register_source(
  541. source, "SlideShow.PreviousSlide",
  542. obs_module_text("SlideShow.PreviousSlide"),
  543. previous_slide_hotkey, ss);
  544. proc_handler_add(ph, "int current_index()", current_slide_proc, ss);
  545. proc_handler_add(ph, "int total_files()", total_slides_proc, ss);
  546. pthread_mutex_init_value(&ss->mutex);
  547. if (pthread_mutex_init(&ss->mutex, NULL) != 0)
  548. goto error;
  549. obs_source_update(source, NULL);
  550. UNUSED_PARAMETER(settings);
  551. return ss;
  552. error:
  553. ss_destroy(ss);
  554. return NULL;
  555. }
  556. static void ss_video_render(void *data, gs_effect_t *effect)
  557. {
  558. struct slideshow *ss = data;
  559. obs_source_t *transition = get_transition(ss);
  560. if (transition) {
  561. obs_source_video_render(transition);
  562. obs_source_release(transition);
  563. }
  564. UNUSED_PARAMETER(effect);
  565. }
  566. static void ss_video_tick(void *data, float seconds)
  567. {
  568. struct slideshow *ss = data;
  569. if (!ss->transition || !ss->slide_time)
  570. return;
  571. if (ss->restart_on_activate && ss->use_cut) {
  572. ss->elapsed = 0.0f;
  573. ss->cur_item = ss->randomize ? random_file(ss) : 0;
  574. do_transition(ss, false);
  575. ss->restart_on_activate = false;
  576. ss->use_cut = false;
  577. ss->stop = false;
  578. return;
  579. }
  580. if (ss->pause_on_deactivate || ss->manual || ss->stop || ss->paused)
  581. return;
  582. /* ----------------------------------------------------- */
  583. /* fade to transparency when the file list becomes empty */
  584. if (!ss->files.num) {
  585. obs_source_t *active_transition_source =
  586. obs_transition_get_active_source(ss->transition);
  587. if (active_transition_source) {
  588. obs_source_release(active_transition_source);
  589. do_transition(ss, true);
  590. }
  591. }
  592. /* ----------------------------------------------------- */
  593. /* do transition when slide time reached */
  594. ss->elapsed += seconds;
  595. if (ss->elapsed > ss->slide_time) {
  596. ss->elapsed -= ss->slide_time;
  597. if (!ss->loop && ss->cur_item == ss->files.num - 1) {
  598. if (ss->hide)
  599. do_transition(ss, true);
  600. else
  601. do_transition(ss, false);
  602. return;
  603. }
  604. obs_source_media_next(ss->source);
  605. }
  606. }
  607. static inline bool ss_audio_render_(obs_source_t *transition, uint64_t *ts_out,
  608. struct obs_source_audio_mix *audio_output,
  609. uint32_t mixers, size_t channels,
  610. size_t sample_rate)
  611. {
  612. struct obs_source_audio_mix child_audio;
  613. uint64_t source_ts;
  614. if (obs_source_audio_pending(transition))
  615. return false;
  616. source_ts = obs_source_get_audio_timestamp(transition);
  617. if (!source_ts)
  618. return false;
  619. obs_source_get_audio_mix(transition, &child_audio);
  620. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  621. if ((mixers & (1 << mix)) == 0)
  622. continue;
  623. for (size_t ch = 0; ch < channels; ch++) {
  624. float *out = audio_output->output[mix].data[ch];
  625. float *in = child_audio.output[mix].data[ch];
  626. memcpy(out, in,
  627. AUDIO_OUTPUT_FRAMES * MAX_AUDIO_CHANNELS *
  628. sizeof(float));
  629. }
  630. }
  631. *ts_out = source_ts;
  632. UNUSED_PARAMETER(sample_rate);
  633. return true;
  634. }
  635. static bool ss_audio_render(void *data, uint64_t *ts_out,
  636. struct obs_source_audio_mix *audio_output,
  637. uint32_t mixers, size_t channels,
  638. size_t sample_rate)
  639. {
  640. struct slideshow *ss = data;
  641. obs_source_t *transition = get_transition(ss);
  642. bool success;
  643. if (!transition)
  644. return false;
  645. success = ss_audio_render_(transition, ts_out, audio_output, mixers,
  646. channels, sample_rate);
  647. obs_source_release(transition);
  648. return success;
  649. }
  650. static void ss_enum_sources(void *data, obs_source_enum_proc_t cb, void *param)
  651. {
  652. struct slideshow *ss = data;
  653. pthread_mutex_lock(&ss->mutex);
  654. if (ss->transition)
  655. cb(ss->source, ss->transition, param);
  656. pthread_mutex_unlock(&ss->mutex);
  657. }
  658. static uint32_t ss_width(void *data)
  659. {
  660. struct slideshow *ss = data;
  661. return ss->transition ? ss->cx : 0;
  662. }
  663. static uint32_t ss_height(void *data)
  664. {
  665. struct slideshow *ss = data;
  666. return ss->transition ? ss->cy : 0;
  667. }
  668. static void ss_defaults(obs_data_t *settings)
  669. {
  670. obs_data_set_default_string(settings, S_TRANSITION, "fade");
  671. obs_data_set_default_int(settings, S_SLIDE_TIME, 8000);
  672. obs_data_set_default_int(settings, S_TR_SPEED, 700);
  673. obs_data_set_default_string(settings, S_CUSTOM_SIZE,
  674. T_CUSTOM_SIZE_AUTO);
  675. obs_data_set_default_string(settings, S_BEHAVIOR,
  676. S_BEHAVIOR_ALWAYS_PLAY);
  677. obs_data_set_default_string(settings, S_MODE, S_MODE_AUTO);
  678. obs_data_set_default_bool(settings, S_LOOP, true);
  679. }
  680. static const char *file_filter = "Image files (*.bmp *.tga *.png *.jpeg *.jpg"
  681. #ifdef _WIN32
  682. " *.jxr"
  683. #endif
  684. " *.gif *.webp)";
  685. static const char *aspects[] = {"16:9", "16:10", "4:3", "1:1"};
  686. #define NUM_ASPECTS (sizeof(aspects) / sizeof(const char *))
  687. static obs_properties_t *ss_properties(void *data)
  688. {
  689. obs_properties_t *ppts = obs_properties_create();
  690. struct slideshow *ss = data;
  691. struct obs_video_info ovi;
  692. struct dstr path = {0};
  693. obs_property_t *p;
  694. int cx;
  695. int cy;
  696. /* ----------------- */
  697. obs_get_video_info(&ovi);
  698. cx = (int)ovi.base_width;
  699. cy = (int)ovi.base_height;
  700. /* ----------------- */
  701. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  702. OBS_COMBO_TYPE_LIST,
  703. OBS_COMBO_FORMAT_STRING);
  704. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  705. S_BEHAVIOR_ALWAYS_PLAY);
  706. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  707. S_BEHAVIOR_STOP_RESTART);
  708. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  709. S_BEHAVIOR_PAUSE_UNPAUSE);
  710. p = obs_properties_add_list(ppts, S_MODE, T_MODE, OBS_COMBO_TYPE_LIST,
  711. OBS_COMBO_FORMAT_STRING);
  712. obs_property_list_add_string(p, T_MODE_AUTO, S_MODE_AUTO);
  713. obs_property_list_add_string(p, T_MODE_MANUAL, S_MODE_MANUAL);
  714. p = obs_properties_add_list(ppts, S_TRANSITION, T_TRANSITION,
  715. OBS_COMBO_TYPE_LIST,
  716. OBS_COMBO_FORMAT_STRING);
  717. obs_property_list_add_string(p, T_TR_CUT, TR_CUT);
  718. obs_property_list_add_string(p, T_TR_FADE, TR_FADE);
  719. obs_property_list_add_string(p, T_TR_SWIPE, TR_SWIPE);
  720. obs_property_list_add_string(p, T_TR_SLIDE, TR_SLIDE);
  721. p = obs_properties_add_int(ppts, S_SLIDE_TIME, T_SLIDE_TIME, 50,
  722. 3600000, 50);
  723. obs_property_int_set_suffix(p, " ms");
  724. p = obs_properties_add_int(ppts, S_TR_SPEED, T_TR_SPEED, 0, 3600000,
  725. 50);
  726. obs_property_int_set_suffix(p, " ms");
  727. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  728. obs_properties_add_bool(ppts, S_HIDE, T_HIDE);
  729. obs_properties_add_bool(ppts, S_RANDOMIZE, T_RANDOMIZE);
  730. p = obs_properties_add_list(ppts, S_CUSTOM_SIZE, T_CUSTOM_SIZE,
  731. OBS_COMBO_TYPE_EDITABLE,
  732. OBS_COMBO_FORMAT_STRING);
  733. obs_property_list_add_string(p, T_CUSTOM_SIZE_AUTO, T_CUSTOM_SIZE_AUTO);
  734. for (size_t i = 0; i < NUM_ASPECTS; i++)
  735. obs_property_list_add_string(p, aspects[i], aspects[i]);
  736. char str[32];
  737. snprintf(str, sizeof(str), "%dx%d", cx, cy);
  738. obs_property_list_add_string(p, str, str);
  739. if (ss) {
  740. pthread_mutex_lock(&ss->mutex);
  741. if (ss->files.num) {
  742. struct image_file_data *last = da_end(ss->files);
  743. const char *slash;
  744. dstr_copy(&path, last->path);
  745. dstr_replace(&path, "\\", "/");
  746. slash = strrchr(path.array, '/');
  747. if (slash)
  748. dstr_resize(&path, slash - path.array + 1);
  749. }
  750. pthread_mutex_unlock(&ss->mutex);
  751. }
  752. obs_properties_add_editable_list(ppts, S_FILES, T_FILES,
  753. OBS_EDITABLE_LIST_TYPE_FILES,
  754. file_filter, path.array);
  755. dstr_free(&path);
  756. return ppts;
  757. }
  758. static void ss_activate(void *data)
  759. {
  760. struct slideshow *ss = data;
  761. if (ss->behavior == BEHAVIOR_STOP_RESTART) {
  762. ss->restart_on_activate = true;
  763. ss->use_cut = true;
  764. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  765. } else if (ss->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  766. ss->pause_on_deactivate = false;
  767. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  768. }
  769. }
  770. static void ss_deactivate(void *data)
  771. {
  772. struct slideshow *ss = data;
  773. if (ss->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  774. ss->pause_on_deactivate = true;
  775. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  776. }
  777. }
  778. static void missing_file_callback(void *src, const char *new_path, void *data)
  779. {
  780. struct slideshow *s = src;
  781. const char *orig_path = data;
  782. obs_source_t *source = s->source;
  783. obs_data_t *settings = obs_source_get_settings(source);
  784. obs_data_array_t *files = obs_data_get_array(settings, S_FILES);
  785. size_t l = obs_data_array_count(files);
  786. for (size_t i = 0; i < l; i++) {
  787. obs_data_t *file = obs_data_array_item(files, i);
  788. const char *path = obs_data_get_string(file, "value");
  789. if (strcmp(path, orig_path) == 0) {
  790. obs_data_set_string(file, "value", new_path);
  791. obs_data_release(file);
  792. break;
  793. }
  794. obs_data_release(file);
  795. }
  796. obs_source_update(source, settings);
  797. obs_data_array_release(files);
  798. obs_data_release(settings);
  799. }
  800. static obs_missing_files_t *ss_missingfiles(void *data)
  801. {
  802. struct slideshow *s = data;
  803. obs_missing_files_t *missing_files = obs_missing_files_create();
  804. obs_source_t *source = s->source;
  805. obs_data_t *settings = obs_source_get_settings(source);
  806. obs_data_array_t *files = obs_data_get_array(settings, S_FILES);
  807. size_t l = obs_data_array_count(files);
  808. for (size_t i = 0; i < l; i++) {
  809. obs_data_t *item = obs_data_array_item(files, i);
  810. const char *path = obs_data_get_string(item, "value");
  811. if (strcmp(path, "") != 0) {
  812. if (!os_file_exists(path)) {
  813. obs_missing_file_t *file =
  814. obs_missing_file_create(
  815. path, missing_file_callback,
  816. OBS_MISSING_FILE_SOURCE, source,
  817. (void *)path);
  818. obs_missing_files_add_file(missing_files, file);
  819. }
  820. }
  821. obs_data_release(item);
  822. }
  823. obs_data_array_release(files);
  824. obs_data_release(settings);
  825. return missing_files;
  826. }
  827. static enum gs_color_space
  828. ss_video_get_color_space(void *data, size_t count,
  829. const enum gs_color_space *preferred_spaces)
  830. {
  831. enum gs_color_space space = GS_CS_SRGB;
  832. struct slideshow *ss = data;
  833. obs_source_t *transition = get_transition(ss);
  834. if (transition) {
  835. space = obs_source_get_color_space(transition, count,
  836. preferred_spaces);
  837. obs_source_release(transition);
  838. }
  839. return space;
  840. }
  841. struct obs_source_info slideshow_info = {
  842. .id = "slideshow",
  843. .type = OBS_SOURCE_TYPE_INPUT,
  844. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  845. OBS_SOURCE_COMPOSITE | OBS_SOURCE_CONTROLLABLE_MEDIA,
  846. .get_name = ss_getname,
  847. .create = ss_create,
  848. .destroy = ss_destroy,
  849. .update = ss_update,
  850. .activate = ss_activate,
  851. .deactivate = ss_deactivate,
  852. .video_render = ss_video_render,
  853. .video_tick = ss_video_tick,
  854. .audio_render = ss_audio_render,
  855. .enum_active_sources = ss_enum_sources,
  856. .get_width = ss_width,
  857. .get_height = ss_height,
  858. .get_defaults = ss_defaults,
  859. .get_properties = ss_properties,
  860. .missing_files = ss_missingfiles,
  861. .icon_type = OBS_ICON_TYPE_SLIDESHOW,
  862. .media_play_pause = ss_play_pause,
  863. .media_restart = ss_restart,
  864. .media_stop = ss_stop,
  865. .media_next = ss_next_slide,
  866. .media_previous = ss_previous_slide,
  867. .media_get_state = ss_get_state,
  868. .video_get_color_space = ss_video_get_color_space,
  869. };