obs-slideshow.c 28 KB

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