obs-slideshow.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. return (size_t)rand() % ss->files.num;
  152. }
  153. /* ------------------------------------------------------------------------- */
  154. static const char *ss_getname(void *unused)
  155. {
  156. UNUSED_PARAMETER(unused);
  157. return obs_module_text("SlideShow");
  158. }
  159. static void add_file(struct slideshow *ss, struct darray *array,
  160. const char *path, uint32_t *cx, uint32_t *cy)
  161. {
  162. DARRAY(struct image_file_data) new_files;
  163. struct image_file_data data;
  164. obs_source_t *new_source;
  165. new_files.da = *array;
  166. pthread_mutex_lock(&ss->mutex);
  167. new_source = get_source(&ss->files.da, path);
  168. pthread_mutex_unlock(&ss->mutex);
  169. if (!new_source)
  170. new_source = get_source(&new_files.da, path);
  171. if (!new_source)
  172. new_source = create_source_from_file(path);
  173. if (new_source) {
  174. uint32_t new_cx = obs_source_get_width(new_source);
  175. uint32_t new_cy = obs_source_get_height(new_source);
  176. data.path = bstrdup(path);
  177. data.source = new_source;
  178. da_push_back(new_files, &data);
  179. if (new_cx > *cx)
  180. *cx = new_cx;
  181. if (new_cy > *cy)
  182. *cy = new_cy;
  183. void *source_data = obs_obj_get_data(new_source);
  184. ss->mem_usage += image_source_get_memory_usage(source_data);
  185. }
  186. *array = new_files.da;
  187. }
  188. static bool valid_extension(const char *ext)
  189. {
  190. if (!ext)
  191. return false;
  192. return astrcmpi(ext, ".bmp") == 0 || astrcmpi(ext, ".tga") == 0 ||
  193. astrcmpi(ext, ".png") == 0 || astrcmpi(ext, ".jpeg") == 0 ||
  194. astrcmpi(ext, ".jpg") == 0 || astrcmpi(ext, ".gif") == 0;
  195. }
  196. static inline bool item_valid(struct slideshow *ss)
  197. {
  198. return ss->files.num && ss->cur_item < ss->files.num;
  199. }
  200. static void do_transition(void *data, bool to_null)
  201. {
  202. struct slideshow *ss = data;
  203. bool valid = item_valid(ss);
  204. if (valid && ss->use_cut) {
  205. obs_transition_set(ss->transition,
  206. ss->files.array[ss->cur_item].source);
  207. } else if (valid && !to_null) {
  208. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  209. ss->tr_speed,
  210. ss->files.array[ss->cur_item].source);
  211. } else {
  212. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  213. ss->tr_speed, NULL);
  214. set_media_state(ss, OBS_MEDIA_STATE_ENDED);
  215. obs_source_media_ended(ss->source);
  216. }
  217. }
  218. static void ss_update(void *data, obs_data_t *settings)
  219. {
  220. DARRAY(struct image_file_data) new_files;
  221. DARRAY(struct image_file_data) old_files;
  222. obs_source_t *new_tr = NULL;
  223. obs_source_t *old_tr = NULL;
  224. struct slideshow *ss = data;
  225. obs_data_array_t *array;
  226. const char *tr_name;
  227. uint32_t new_duration;
  228. uint32_t new_speed;
  229. uint32_t cx = 0;
  230. uint32_t cy = 0;
  231. size_t count;
  232. const char *behavior;
  233. const char *mode;
  234. /* ------------------------------------- */
  235. /* get settings data */
  236. da_init(new_files);
  237. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  238. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0)
  239. ss->behavior = BEHAVIOR_PAUSE_UNPAUSE;
  240. else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0)
  241. ss->behavior = BEHAVIOR_ALWAYS_PLAY;
  242. else /* S_BEHAVIOR_STOP_RESTART */
  243. ss->behavior = BEHAVIOR_STOP_RESTART;
  244. mode = obs_data_get_string(settings, S_MODE);
  245. ss->manual = (astrcmpi(mode, S_MODE_MANUAL) == 0);
  246. tr_name = obs_data_get_string(settings, S_TRANSITION);
  247. if (astrcmpi(tr_name, TR_CUT) == 0)
  248. tr_name = "cut_transition";
  249. else if (astrcmpi(tr_name, TR_SWIPE) == 0)
  250. tr_name = "swipe_transition";
  251. else if (astrcmpi(tr_name, TR_SLIDE) == 0)
  252. tr_name = "slide_transition";
  253. else
  254. tr_name = "fade_transition";
  255. ss->randomize = obs_data_get_bool(settings, S_RANDOMIZE);
  256. ss->loop = obs_data_get_bool(settings, S_LOOP);
  257. ss->hide = obs_data_get_bool(settings, S_HIDE);
  258. if (!ss->tr_name || strcmp(tr_name, ss->tr_name) != 0)
  259. new_tr = obs_source_create_private(tr_name, NULL, NULL);
  260. new_duration = (uint32_t)obs_data_get_int(settings, S_SLIDE_TIME);
  261. new_speed = (uint32_t)obs_data_get_int(settings, S_TR_SPEED);
  262. array = obs_data_get_array(settings, S_FILES);
  263. count = obs_data_array_count(array);
  264. /* ------------------------------------- */
  265. /* create new list of sources */
  266. ss->mem_usage = 0;
  267. for (size_t i = 0; i < count; i++) {
  268. obs_data_t *item = obs_data_array_item(array, i);
  269. const char *path = obs_data_get_string(item, "value");
  270. os_dir_t *dir = os_opendir(path);
  271. if (dir) {
  272. struct dstr dir_path = {0};
  273. struct os_dirent *ent;
  274. for (;;) {
  275. const char *ext;
  276. ent = os_readdir(dir);
  277. if (!ent)
  278. break;
  279. if (ent->directory)
  280. continue;
  281. ext = os_get_path_extension(ent->d_name);
  282. if (!valid_extension(ext))
  283. continue;
  284. dstr_copy(&dir_path, path);
  285. dstr_cat_ch(&dir_path, '/');
  286. dstr_cat(&dir_path, ent->d_name);
  287. add_file(ss, &new_files.da, dir_path.array, &cx,
  288. &cy);
  289. if (ss->mem_usage >= MAX_MEM_USAGE)
  290. break;
  291. }
  292. dstr_free(&dir_path);
  293. os_closedir(dir);
  294. } else {
  295. add_file(ss, &new_files.da, path, &cx, &cy);
  296. }
  297. obs_data_release(item);
  298. if (ss->mem_usage >= MAX_MEM_USAGE)
  299. break;
  300. }
  301. /* ------------------------------------- */
  302. /* update settings data */
  303. pthread_mutex_lock(&ss->mutex);
  304. old_files.da = ss->files.da;
  305. ss->files.da = new_files.da;
  306. if (new_tr) {
  307. old_tr = ss->transition;
  308. ss->transition = new_tr;
  309. }
  310. if (strcmp(tr_name, "cut_transition") != 0) {
  311. if (new_duration < 100)
  312. new_duration = 100;
  313. new_duration += new_speed;
  314. } else {
  315. if (new_duration < 50)
  316. new_duration = 50;
  317. }
  318. ss->tr_speed = new_speed;
  319. ss->tr_name = tr_name;
  320. ss->slide_time = (float)new_duration / 1000.0f;
  321. pthread_mutex_unlock(&ss->mutex);
  322. /* ------------------------------------- */
  323. /* clean up and restart transition */
  324. if (old_tr)
  325. obs_source_release(old_tr);
  326. free_files(&old_files.da);
  327. /* ------------------------- */
  328. const char *res_str = obs_data_get_string(settings, S_CUSTOM_SIZE);
  329. bool aspect_only = false, use_auto = true;
  330. int cx_in = 0, cy_in = 0;
  331. if (strcmp(res_str, T_CUSTOM_SIZE_AUTO) != 0) {
  332. int ret = sscanf(res_str, "%dx%d", &cx_in, &cy_in);
  333. if (ret == 2) {
  334. aspect_only = false;
  335. use_auto = false;
  336. } else {
  337. ret = sscanf(res_str, "%d:%d", &cx_in, &cy_in);
  338. if (ret == 2) {
  339. aspect_only = true;
  340. use_auto = false;
  341. }
  342. }
  343. }
  344. if (!use_auto) {
  345. double cx_f = (double)cx;
  346. double cy_f = (double)cy;
  347. double old_aspect = cx_f / cy_f;
  348. double new_aspect = (double)cx_in / (double)cy_in;
  349. if (aspect_only) {
  350. if (fabs(old_aspect - new_aspect) > EPSILON) {
  351. if (new_aspect > old_aspect)
  352. cx = (uint32_t)(cy_f * new_aspect);
  353. else
  354. cy = (uint32_t)(cx_f / new_aspect);
  355. }
  356. } else {
  357. cx = (uint32_t)cx_in;
  358. cy = (uint32_t)cy_in;
  359. }
  360. }
  361. /* ------------------------- */
  362. ss->cx = cx;
  363. ss->cy = cy;
  364. ss->cur_item = 0;
  365. ss->elapsed = 0.0f;
  366. obs_transition_set_size(ss->transition, cx, cy);
  367. obs_transition_set_alignment(ss->transition, OBS_ALIGN_CENTER);
  368. obs_transition_set_scale_type(ss->transition,
  369. OBS_TRANSITION_SCALE_ASPECT);
  370. if (ss->randomize && ss->files.num)
  371. ss->cur_item = random_file(ss);
  372. if (new_tr)
  373. obs_source_add_active_child(ss->source, new_tr);
  374. if (ss->files.num) {
  375. do_transition(ss, false);
  376. if (ss->manual)
  377. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  378. else
  379. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  380. obs_source_media_started(ss->source);
  381. }
  382. obs_data_array_release(array);
  383. }
  384. static void ss_play_pause(void *data, bool pause)
  385. {
  386. struct slideshow *ss = data;
  387. if (ss->stop) {
  388. ss->stop = false;
  389. ss->paused = false;
  390. do_transition(ss, false);
  391. } else {
  392. ss->paused = pause;
  393. ss->manual = pause;
  394. }
  395. if (pause)
  396. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  397. else
  398. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  399. }
  400. static void ss_restart(void *data)
  401. {
  402. struct slideshow *ss = data;
  403. ss->elapsed = 0.0f;
  404. ss->cur_item = 0;
  405. ss->stop = false;
  406. ss->paused = false;
  407. do_transition(ss, false);
  408. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  409. }
  410. static void ss_stop(void *data)
  411. {
  412. struct slideshow *ss = data;
  413. ss->elapsed = 0.0f;
  414. ss->cur_item = 0;
  415. do_transition(ss, true);
  416. ss->stop = true;
  417. ss->paused = false;
  418. set_media_state(ss, OBS_MEDIA_STATE_STOPPED);
  419. }
  420. static void ss_next_slide(void *data)
  421. {
  422. struct slideshow *ss = data;
  423. if (!ss->files.num || obs_transition_get_time(ss->transition) < 1.0f)
  424. return;
  425. if (++ss->cur_item >= ss->files.num)
  426. ss->cur_item = 0;
  427. do_transition(ss, false);
  428. }
  429. static void ss_previous_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->cur_item == 0)
  435. ss->cur_item = ss->files.num - 1;
  436. else
  437. --ss->cur_item;
  438. do_transition(ss, false);
  439. }
  440. static void play_pause_hotkey(void *data, obs_hotkey_id id,
  441. obs_hotkey_t *hotkey, bool pressed)
  442. {
  443. UNUSED_PARAMETER(id);
  444. UNUSED_PARAMETER(hotkey);
  445. struct slideshow *ss = data;
  446. if (pressed && obs_source_showing(ss->source))
  447. obs_source_media_play_pause(ss->source, !ss->paused);
  448. }
  449. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  450. bool pressed)
  451. {
  452. UNUSED_PARAMETER(id);
  453. UNUSED_PARAMETER(hotkey);
  454. struct slideshow *ss = data;
  455. if (pressed && obs_source_showing(ss->source))
  456. obs_source_media_restart(ss->source);
  457. }
  458. static void stop_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  459. bool pressed)
  460. {
  461. UNUSED_PARAMETER(id);
  462. UNUSED_PARAMETER(hotkey);
  463. struct slideshow *ss = data;
  464. if (pressed && obs_source_showing(ss->source))
  465. obs_source_media_stop(ss->source);
  466. }
  467. static void next_slide_hotkey(void *data, obs_hotkey_id id,
  468. obs_hotkey_t *hotkey, bool pressed)
  469. {
  470. UNUSED_PARAMETER(id);
  471. UNUSED_PARAMETER(hotkey);
  472. struct slideshow *ss = data;
  473. if (!ss->manual)
  474. return;
  475. if (pressed && obs_source_showing(ss->source))
  476. obs_source_media_next(ss->source);
  477. }
  478. static void previous_slide_hotkey(void *data, obs_hotkey_id id,
  479. obs_hotkey_t *hotkey, bool pressed)
  480. {
  481. UNUSED_PARAMETER(id);
  482. UNUSED_PARAMETER(hotkey);
  483. struct slideshow *ss = data;
  484. if (!ss->manual)
  485. return;
  486. if (pressed && obs_source_showing(ss->source))
  487. obs_source_media_previous(ss->source);
  488. }
  489. static void ss_destroy(void *data)
  490. {
  491. struct slideshow *ss = data;
  492. obs_source_release(ss->transition);
  493. free_files(&ss->files.da);
  494. pthread_mutex_destroy(&ss->mutex);
  495. bfree(ss);
  496. }
  497. static void *ss_create(obs_data_t *settings, obs_source_t *source)
  498. {
  499. struct slideshow *ss = bzalloc(sizeof(*ss));
  500. ss->source = source;
  501. ss->manual = false;
  502. ss->paused = false;
  503. ss->stop = false;
  504. ss->play_pause_hotkey = obs_hotkey_register_source(
  505. source, "SlideShow.PlayPause",
  506. obs_module_text("SlideShow.PlayPause"), play_pause_hotkey, ss);
  507. ss->restart_hotkey = obs_hotkey_register_source(
  508. source, "SlideShow.Restart",
  509. obs_module_text("SlideShow.Restart"), restart_hotkey, ss);
  510. ss->stop_hotkey = obs_hotkey_register_source(
  511. source, "SlideShow.Stop", obs_module_text("SlideShow.Stop"),
  512. stop_hotkey, ss);
  513. ss->prev_hotkey = obs_hotkey_register_source(
  514. source, "SlideShow.NextSlide",
  515. obs_module_text("SlideShow.NextSlide"), next_slide_hotkey, ss);
  516. ss->prev_hotkey = obs_hotkey_register_source(
  517. source, "SlideShow.PreviousSlide",
  518. obs_module_text("SlideShow.PreviousSlide"),
  519. previous_slide_hotkey, ss);
  520. pthread_mutex_init_value(&ss->mutex);
  521. if (pthread_mutex_init(&ss->mutex, NULL) != 0)
  522. goto error;
  523. obs_source_update(source, NULL);
  524. UNUSED_PARAMETER(settings);
  525. return ss;
  526. error:
  527. ss_destroy(ss);
  528. return NULL;
  529. }
  530. static void ss_video_render(void *data, gs_effect_t *effect)
  531. {
  532. struct slideshow *ss = data;
  533. obs_source_t *transition = get_transition(ss);
  534. if (transition) {
  535. obs_source_video_render(transition);
  536. obs_source_release(transition);
  537. }
  538. UNUSED_PARAMETER(effect);
  539. }
  540. static void ss_video_tick(void *data, float seconds)
  541. {
  542. struct slideshow *ss = data;
  543. if (!ss->transition || !ss->slide_time)
  544. return;
  545. if (ss->restart_on_activate && ss->use_cut) {
  546. ss->elapsed = 0.0f;
  547. ss->cur_item = ss->randomize ? random_file(ss) : 0;
  548. do_transition(ss, false);
  549. ss->restart_on_activate = false;
  550. ss->use_cut = false;
  551. ss->stop = false;
  552. return;
  553. }
  554. if (ss->pause_on_deactivate || ss->manual || ss->stop || ss->paused)
  555. return;
  556. /* ----------------------------------------------------- */
  557. /* fade to transparency when the file list becomes empty */
  558. if (!ss->files.num) {
  559. obs_source_t *active_transition_source =
  560. obs_transition_get_active_source(ss->transition);
  561. if (active_transition_source) {
  562. obs_source_release(active_transition_source);
  563. do_transition(ss, true);
  564. }
  565. }
  566. /* ----------------------------------------------------- */
  567. /* do transition when slide time reached */
  568. ss->elapsed += seconds;
  569. if (ss->elapsed > ss->slide_time) {
  570. ss->elapsed -= ss->slide_time;
  571. if (!ss->loop && ss->cur_item == ss->files.num - 1) {
  572. if (ss->hide)
  573. do_transition(ss, true);
  574. else
  575. do_transition(ss, false);
  576. return;
  577. }
  578. if (ss->randomize) {
  579. size_t next = ss->cur_item;
  580. if (ss->files.num > 1) {
  581. while (next == ss->cur_item)
  582. next = random_file(ss);
  583. }
  584. ss->cur_item = next;
  585. } else if (++ss->cur_item >= ss->files.num) {
  586. ss->cur_item = 0;
  587. }
  588. if (ss->files.num)
  589. do_transition(ss, false);
  590. }
  591. }
  592. static inline bool ss_audio_render_(obs_source_t *transition, uint64_t *ts_out,
  593. struct obs_source_audio_mix *audio_output,
  594. uint32_t mixers, size_t channels,
  595. size_t sample_rate)
  596. {
  597. struct obs_source_audio_mix child_audio;
  598. uint64_t source_ts;
  599. if (obs_source_audio_pending(transition))
  600. return false;
  601. source_ts = obs_source_get_audio_timestamp(transition);
  602. if (!source_ts)
  603. return false;
  604. obs_source_get_audio_mix(transition, &child_audio);
  605. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  606. if ((mixers & (1 << mix)) == 0)
  607. continue;
  608. for (size_t ch = 0; ch < channels; ch++) {
  609. float *out = audio_output->output[mix].data[ch];
  610. float *in = child_audio.output[mix].data[ch];
  611. memcpy(out, in,
  612. AUDIO_OUTPUT_FRAMES * MAX_AUDIO_CHANNELS *
  613. sizeof(float));
  614. }
  615. }
  616. *ts_out = source_ts;
  617. UNUSED_PARAMETER(sample_rate);
  618. return true;
  619. }
  620. static bool ss_audio_render(void *data, uint64_t *ts_out,
  621. struct obs_source_audio_mix *audio_output,
  622. uint32_t mixers, size_t channels,
  623. size_t sample_rate)
  624. {
  625. struct slideshow *ss = data;
  626. obs_source_t *transition = get_transition(ss);
  627. bool success;
  628. if (!transition)
  629. return false;
  630. success = ss_audio_render_(transition, ts_out, audio_output, mixers,
  631. channels, sample_rate);
  632. obs_source_release(transition);
  633. return success;
  634. }
  635. static void ss_enum_sources(void *data, obs_source_enum_proc_t cb, void *param)
  636. {
  637. struct slideshow *ss = data;
  638. pthread_mutex_lock(&ss->mutex);
  639. if (ss->transition)
  640. cb(ss->source, ss->transition, param);
  641. pthread_mutex_unlock(&ss->mutex);
  642. }
  643. static uint32_t ss_width(void *data)
  644. {
  645. struct slideshow *ss = data;
  646. return ss->transition ? ss->cx : 0;
  647. }
  648. static uint32_t ss_height(void *data)
  649. {
  650. struct slideshow *ss = data;
  651. return ss->transition ? ss->cy : 0;
  652. }
  653. static void ss_defaults(obs_data_t *settings)
  654. {
  655. obs_data_set_default_string(settings, S_TRANSITION, "fade");
  656. obs_data_set_default_int(settings, S_SLIDE_TIME, 8000);
  657. obs_data_set_default_int(settings, S_TR_SPEED, 700);
  658. obs_data_set_default_string(settings, S_CUSTOM_SIZE,
  659. T_CUSTOM_SIZE_AUTO);
  660. obs_data_set_default_string(settings, S_BEHAVIOR,
  661. S_BEHAVIOR_ALWAYS_PLAY);
  662. obs_data_set_default_string(settings, S_MODE, S_MODE_AUTO);
  663. obs_data_set_default_bool(settings, S_LOOP, true);
  664. }
  665. static const char *file_filter =
  666. "Image files (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.webp)";
  667. static const char *aspects[] = {"16:9", "16:10", "4:3", "1:1"};
  668. #define NUM_ASPECTS (sizeof(aspects) / sizeof(const char *))
  669. static obs_properties_t *ss_properties(void *data)
  670. {
  671. obs_properties_t *ppts = obs_properties_create();
  672. struct slideshow *ss = data;
  673. struct obs_video_info ovi;
  674. struct dstr path = {0};
  675. obs_property_t *p;
  676. int cx;
  677. int cy;
  678. /* ----------------- */
  679. obs_get_video_info(&ovi);
  680. cx = (int)ovi.base_width;
  681. cy = (int)ovi.base_height;
  682. /* ----------------- */
  683. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  684. OBS_COMBO_TYPE_LIST,
  685. OBS_COMBO_FORMAT_STRING);
  686. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  687. S_BEHAVIOR_ALWAYS_PLAY);
  688. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  689. S_BEHAVIOR_STOP_RESTART);
  690. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  691. S_BEHAVIOR_PAUSE_UNPAUSE);
  692. p = obs_properties_add_list(ppts, S_MODE, T_MODE, OBS_COMBO_TYPE_LIST,
  693. OBS_COMBO_FORMAT_STRING);
  694. obs_property_list_add_string(p, T_MODE_AUTO, S_MODE_AUTO);
  695. obs_property_list_add_string(p, T_MODE_MANUAL, S_MODE_MANUAL);
  696. p = obs_properties_add_list(ppts, S_TRANSITION, T_TRANSITION,
  697. OBS_COMBO_TYPE_LIST,
  698. OBS_COMBO_FORMAT_STRING);
  699. obs_property_list_add_string(p, T_TR_CUT, TR_CUT);
  700. obs_property_list_add_string(p, T_TR_FADE, TR_FADE);
  701. obs_property_list_add_string(p, T_TR_SWIPE, TR_SWIPE);
  702. obs_property_list_add_string(p, T_TR_SLIDE, TR_SLIDE);
  703. obs_properties_add_int(ppts, S_SLIDE_TIME, T_SLIDE_TIME, 50, 3600000,
  704. 50);
  705. obs_properties_add_int(ppts, S_TR_SPEED, T_TR_SPEED, 0, 3600000, 50);
  706. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  707. obs_properties_add_bool(ppts, S_HIDE, T_HIDE);
  708. obs_properties_add_bool(ppts, S_RANDOMIZE, T_RANDOMIZE);
  709. p = obs_properties_add_list(ppts, S_CUSTOM_SIZE, T_CUSTOM_SIZE,
  710. OBS_COMBO_TYPE_EDITABLE,
  711. OBS_COMBO_FORMAT_STRING);
  712. obs_property_list_add_string(p, T_CUSTOM_SIZE_AUTO, T_CUSTOM_SIZE_AUTO);
  713. for (size_t i = 0; i < NUM_ASPECTS; i++)
  714. obs_property_list_add_string(p, aspects[i], aspects[i]);
  715. char str[32];
  716. snprintf(str, 32, "%dx%d", cx, cy);
  717. obs_property_list_add_string(p, str, str);
  718. if (ss) {
  719. pthread_mutex_lock(&ss->mutex);
  720. if (ss->files.num) {
  721. struct image_file_data *last = da_end(ss->files);
  722. const char *slash;
  723. dstr_copy(&path, last->path);
  724. dstr_replace(&path, "\\", "/");
  725. slash = strrchr(path.array, '/');
  726. if (slash)
  727. dstr_resize(&path, slash - path.array + 1);
  728. }
  729. pthread_mutex_unlock(&ss->mutex);
  730. }
  731. obs_properties_add_editable_list(ppts, S_FILES, T_FILES,
  732. OBS_EDITABLE_LIST_TYPE_FILES,
  733. file_filter, path.array);
  734. dstr_free(&path);
  735. return ppts;
  736. }
  737. static void ss_activate(void *data)
  738. {
  739. struct slideshow *ss = data;
  740. if (ss->behavior == BEHAVIOR_STOP_RESTART) {
  741. ss->restart_on_activate = true;
  742. ss->use_cut = true;
  743. } else if (ss->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  744. ss->pause_on_deactivate = false;
  745. }
  746. }
  747. static void ss_deactivate(void *data)
  748. {
  749. struct slideshow *ss = data;
  750. if (ss->behavior == BEHAVIOR_PAUSE_UNPAUSE)
  751. ss->pause_on_deactivate = true;
  752. }
  753. static void missing_file_callback(void *src, const char *new_path, void *data)
  754. {
  755. struct slideshow *s = src;
  756. const char *orig_path = data;
  757. obs_source_t *source = s->source;
  758. obs_data_t *settings = obs_source_get_settings(source);
  759. obs_data_array_t *files = obs_data_get_array(settings, S_FILES);
  760. size_t l = obs_data_array_count(files);
  761. for (size_t i = 0; i < l; i++) {
  762. obs_data_t *file = obs_data_array_item(files, i);
  763. const char *path = obs_data_get_string(file, "value");
  764. if (strcmp(path, orig_path) == 0) {
  765. obs_data_set_string(file, "value", new_path);
  766. obs_data_release(file);
  767. break;
  768. }
  769. obs_data_release(file);
  770. }
  771. obs_source_update(source, settings);
  772. obs_data_array_release(files);
  773. obs_data_release(settings);
  774. }
  775. static obs_missing_files_t *ss_missingfiles(void *data)
  776. {
  777. struct slideshow *s = data;
  778. obs_missing_files_t *missing_files = obs_missing_files_create();
  779. obs_source_t *source = s->source;
  780. obs_data_t *settings = obs_source_get_settings(source);
  781. obs_data_array_t *files = obs_data_get_array(settings, S_FILES);
  782. size_t l = obs_data_array_count(files);
  783. for (size_t i = 0; i < l; i++) {
  784. obs_data_t *item = obs_data_array_item(files, i);
  785. const char *path = obs_data_get_string(item, "value");
  786. if (strcmp(path, "") != 0) {
  787. if (!os_file_exists(path)) {
  788. obs_missing_file_t *file =
  789. obs_missing_file_create(
  790. path, missing_file_callback,
  791. OBS_MISSING_FILE_SOURCE, source,
  792. (void *)path);
  793. obs_missing_files_add_file(missing_files, file);
  794. }
  795. }
  796. obs_data_release(item);
  797. }
  798. obs_data_array_release(files);
  799. obs_data_release(settings);
  800. return missing_files;
  801. }
  802. struct obs_source_info slideshow_info = {
  803. .id = "slideshow",
  804. .type = OBS_SOURCE_TYPE_INPUT,
  805. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  806. OBS_SOURCE_COMPOSITE | OBS_SOURCE_CONTROLLABLE_MEDIA,
  807. .get_name = ss_getname,
  808. .create = ss_create,
  809. .destroy = ss_destroy,
  810. .update = ss_update,
  811. .activate = ss_activate,
  812. .deactivate = ss_deactivate,
  813. .video_render = ss_video_render,
  814. .video_tick = ss_video_tick,
  815. .audio_render = ss_audio_render,
  816. .enum_active_sources = ss_enum_sources,
  817. .get_width = ss_width,
  818. .get_height = ss_height,
  819. .get_defaults = ss_defaults,
  820. .get_properties = ss_properties,
  821. .missing_files = ss_missingfiles,
  822. .icon_type = OBS_ICON_TYPE_SLIDESHOW,
  823. .media_play_pause = ss_play_pause,
  824. .media_restart = ss_restart,
  825. .media_stop = ss_stop,
  826. .media_next = ss_next_slide,
  827. .media_previous = ss_previous_slide,
  828. .media_get_state = ss_get_state,
  829. };