obs-slideshow.c 25 KB

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