obs-slideshow-mk2.c 31 KB

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