obs-slideshow.c 27 KB

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