obs-slideshow-mk2.c 32 KB

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