obs-slideshow.c 23 KB

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