obs-slideshow.c 23 KB

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