obs-slideshow.c 26 KB

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