obs-slideshow.c 28 KB

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