obs-slideshow.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. #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. /* ------------------------------------------------------------------------- */
  52. struct image_file_data {
  53. char *path;
  54. obs_source_t *source;
  55. };
  56. enum behavior {
  57. BEHAVIOR_STOP_RESTART,
  58. BEHAVIOR_PAUSE_UNPAUSE,
  59. BEHAVIOR_ALWAYS_PLAY,
  60. };
  61. struct slideshow {
  62. obs_source_t *source;
  63. bool randomize;
  64. bool loop;
  65. bool restart_on_activate;
  66. bool pause_on_deactivate;
  67. bool restart;
  68. bool manual;
  69. bool hide;
  70. bool use_cut;
  71. bool paused;
  72. bool stop;
  73. float slide_time;
  74. uint32_t tr_speed;
  75. const char *tr_name;
  76. obs_source_t *transition;
  77. float elapsed;
  78. size_t cur_item;
  79. uint32_t cx;
  80. uint32_t cy;
  81. pthread_mutex_t mutex;
  82. DARRAY(struct image_file_data) files;
  83. enum behavior behavior;
  84. obs_hotkey_id play_pause_hotkey;
  85. obs_hotkey_id restart_hotkey;
  86. obs_hotkey_id stop_hotkey;
  87. obs_hotkey_id next_hotkey;
  88. obs_hotkey_id prev_hotkey;
  89. };
  90. static obs_source_t *get_transition(struct slideshow *ss)
  91. {
  92. obs_source_t *tr;
  93. pthread_mutex_lock(&ss->mutex);
  94. tr = ss->transition;
  95. obs_source_addref(tr);
  96. pthread_mutex_unlock(&ss->mutex);
  97. return tr;
  98. }
  99. static obs_source_t *get_source(struct darray *array, const char *path)
  100. {
  101. DARRAY(struct image_file_data) files;
  102. obs_source_t *source = NULL;
  103. files.da = *array;
  104. for (size_t i = 0; i < files.num; i++) {
  105. const char *cur_path = files.array[i].path;
  106. if (strcmp(path, cur_path) == 0) {
  107. source = files.array[i].source;
  108. obs_source_addref(source);
  109. break;
  110. }
  111. }
  112. return source;
  113. }
  114. static obs_source_t *create_source_from_file(const char *file)
  115. {
  116. obs_data_t *settings = obs_data_create();
  117. obs_source_t *source;
  118. obs_data_set_string(settings, "file", file);
  119. obs_data_set_bool(settings, "unload", false);
  120. source = obs_source_create_private("image_source", NULL, settings);
  121. obs_data_release(settings);
  122. return source;
  123. }
  124. static void free_files(struct darray *array)
  125. {
  126. DARRAY(struct image_file_data) files;
  127. files.da = *array;
  128. for (size_t i = 0; i < files.num; i++) {
  129. bfree(files.array[i].path);
  130. obs_source_release(files.array[i].source);
  131. }
  132. da_free(files);
  133. }
  134. static inline size_t random_file(struct slideshow *ss)
  135. {
  136. return (size_t)rand() % ss->files.num;
  137. }
  138. /* ------------------------------------------------------------------------- */
  139. static const char *ss_getname(void *unused)
  140. {
  141. UNUSED_PARAMETER(unused);
  142. return obs_module_text("SlideShow");
  143. }
  144. static void add_file(struct slideshow *ss, struct darray *array,
  145. const char *path, uint32_t *cx, uint32_t *cy)
  146. {
  147. DARRAY(struct image_file_data) new_files;
  148. struct image_file_data data;
  149. obs_source_t *new_source;
  150. new_files.da = *array;
  151. pthread_mutex_lock(&ss->mutex);
  152. new_source = get_source(&ss->files.da, path);
  153. pthread_mutex_unlock(&ss->mutex);
  154. if (!new_source)
  155. new_source = get_source(&new_files.da, path);
  156. if (!new_source)
  157. new_source = create_source_from_file(path);
  158. if (new_source) {
  159. uint32_t new_cx = obs_source_get_width(new_source);
  160. uint32_t new_cy = obs_source_get_height(new_source);
  161. data.path = bstrdup(path);
  162. data.source = new_source;
  163. da_push_back(new_files, &data);
  164. if (new_cx > *cx) *cx = new_cx;
  165. if (new_cy > *cy) *cy = new_cy;
  166. }
  167. *array = new_files.da;
  168. }
  169. static bool valid_extension(const char *ext)
  170. {
  171. if (!ext)
  172. return false;
  173. return astrcmpi(ext, ".bmp") == 0 ||
  174. astrcmpi(ext, ".tga") == 0 ||
  175. astrcmpi(ext, ".png") == 0 ||
  176. astrcmpi(ext, ".jpeg") == 0 ||
  177. astrcmpi(ext, ".jpg") == 0 ||
  178. astrcmpi(ext, ".gif") == 0;
  179. }
  180. static inline bool item_valid(struct slideshow *ss)
  181. {
  182. return ss->files.num && ss->cur_item < ss->files.num;
  183. }
  184. static void do_transition(void *data, bool to_null)
  185. {
  186. struct slideshow *ss = data;
  187. bool valid = item_valid(ss);
  188. if (valid && ss->use_cut)
  189. obs_transition_set(ss->transition,
  190. ss->files.array[ss->cur_item].source);
  191. else if (valid && !to_null)
  192. obs_transition_start(ss->transition,
  193. OBS_TRANSITION_MODE_AUTO,
  194. ss->tr_speed,
  195. ss->files.array[ss->cur_item].source);
  196. else
  197. obs_transition_start(ss->transition,
  198. OBS_TRANSITION_MODE_AUTO,
  199. ss->tr_speed,
  200. NULL);
  201. }
  202. static void ss_update(void *data, obs_data_t *settings)
  203. {
  204. DARRAY(struct image_file_data) new_files;
  205. DARRAY(struct image_file_data) old_files;
  206. obs_source_t *new_tr = NULL;
  207. obs_source_t *old_tr = NULL;
  208. struct slideshow *ss = data;
  209. obs_data_array_t *array;
  210. const char *tr_name;
  211. uint32_t new_duration;
  212. uint32_t new_speed;
  213. uint32_t cx = 0;
  214. uint32_t cy = 0;
  215. size_t count;
  216. const char *behavior;
  217. const char *mode;
  218. /* ------------------------------------- */
  219. /* get settings data */
  220. da_init(new_files);
  221. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  222. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0)
  223. ss->behavior = BEHAVIOR_PAUSE_UNPAUSE;
  224. else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0)
  225. ss->behavior = BEHAVIOR_ALWAYS_PLAY;
  226. else /* S_BEHAVIOR_STOP_RESTART */
  227. ss->behavior = BEHAVIOR_STOP_RESTART;
  228. mode = obs_data_get_string(settings, S_MODE);
  229. ss->manual = (astrcmpi(mode, S_MODE_MANUAL) == 0);
  230. tr_name = obs_data_get_string(settings, S_TRANSITION);
  231. if (astrcmpi(tr_name, TR_CUT) == 0)
  232. tr_name = "cut_transition";
  233. else if (astrcmpi(tr_name, TR_SWIPE) == 0)
  234. tr_name = "swipe_transition";
  235. else if (astrcmpi(tr_name, TR_SLIDE) == 0)
  236. tr_name = "slide_transition";
  237. else
  238. tr_name = "fade_transition";
  239. ss->randomize = obs_data_get_bool(settings, S_RANDOMIZE);
  240. ss->loop = obs_data_get_bool(settings, S_LOOP);
  241. ss->hide = obs_data_get_bool(settings, S_HIDE);
  242. if (!ss->tr_name || strcmp(tr_name, ss->tr_name) != 0)
  243. new_tr = obs_source_create_private(tr_name, NULL, NULL);
  244. new_duration = (uint32_t)obs_data_get_int(settings, S_SLIDE_TIME);
  245. new_speed = (uint32_t)obs_data_get_int(settings, S_TR_SPEED);
  246. array = obs_data_get_array(settings, S_FILES);
  247. count = obs_data_array_count(array);
  248. /* ------------------------------------- */
  249. /* create new list of sources */
  250. for (size_t i = 0; i < count; i++) {
  251. obs_data_t *item = obs_data_array_item(array, i);
  252. const char *path = obs_data_get_string(item, "value");
  253. os_dir_t *dir = os_opendir(path);
  254. if (dir) {
  255. struct dstr dir_path = {0};
  256. struct os_dirent *ent;
  257. for (;;) {
  258. const char *ext;
  259. ent = os_readdir(dir);
  260. if (!ent)
  261. break;
  262. if (ent->directory)
  263. continue;
  264. ext = os_get_path_extension(ent->d_name);
  265. if (!valid_extension(ext))
  266. continue;
  267. dstr_copy(&dir_path, path);
  268. dstr_cat_ch(&dir_path, '/');
  269. dstr_cat(&dir_path, ent->d_name);
  270. add_file(ss, &new_files.da, dir_path.array,
  271. &cx, &cy);
  272. }
  273. dstr_free(&dir_path);
  274. os_closedir(dir);
  275. } else {
  276. add_file(ss, &new_files.da, path, &cx, &cy);
  277. }
  278. obs_data_release(item);
  279. }
  280. /* ------------------------------------- */
  281. /* update settings data */
  282. pthread_mutex_lock(&ss->mutex);
  283. old_files.da = ss->files.da;
  284. ss->files.da = new_files.da;
  285. if (new_tr) {
  286. old_tr = ss->transition;
  287. ss->transition = new_tr;
  288. }
  289. if (new_duration < 50)
  290. new_duration = 50;
  291. if (new_speed > (new_duration - 50))
  292. new_speed = new_duration - 50;
  293. ss->tr_speed = new_speed;
  294. ss->tr_name = tr_name;
  295. ss->slide_time = (float)new_duration / 1000.0f;
  296. pthread_mutex_unlock(&ss->mutex);
  297. /* ------------------------------------- */
  298. /* clean up and restart transition */
  299. if (old_tr)
  300. obs_source_release(old_tr);
  301. free_files(&old_files.da);
  302. /* ------------------------- */
  303. const char *res_str = obs_data_get_string(settings, S_CUSTOM_SIZE);
  304. bool aspect_only = false, use_auto = true;
  305. int cx_in = 0, cy_in = 0;
  306. if (strcmp(res_str, T_CUSTOM_SIZE_AUTO) != 0) {
  307. int ret = sscanf(res_str, "%dx%d", &cx_in, &cy_in);
  308. if (ret == 2) {
  309. aspect_only = false;
  310. use_auto = false;
  311. } else {
  312. ret = sscanf(res_str, "%d:%d", &cx_in, &cy_in);
  313. if (ret == 2) {
  314. aspect_only = true;
  315. use_auto = false;
  316. }
  317. }
  318. }
  319. if (!use_auto) {
  320. double cx_f = (double)cx;
  321. double cy_f = (double)cy;
  322. double old_aspect = cx_f / cy_f;
  323. double new_aspect = (double)cx_in / (double)cy_in;
  324. if (aspect_only) {
  325. if (fabs(old_aspect - new_aspect) > EPSILON) {
  326. if (new_aspect > old_aspect)
  327. cx = (uint32_t)(cy_f * new_aspect);
  328. else
  329. cy = (uint32_t)(cx_f / new_aspect);
  330. }
  331. } else {
  332. cx = (uint32_t)cx_in;
  333. cy = (uint32_t)cy_in;
  334. }
  335. }
  336. /* ------------------------- */
  337. ss->cx = cx;
  338. ss->cy = cy;
  339. ss->cur_item = 0;
  340. ss->elapsed = 0.0f;
  341. obs_transition_set_size(ss->transition, cx, cy);
  342. obs_transition_set_alignment(ss->transition, OBS_ALIGN_CENTER);
  343. obs_transition_set_scale_type(ss->transition,
  344. OBS_TRANSITION_SCALE_ASPECT);
  345. if (ss->randomize && ss->files.num)
  346. ss->cur_item = random_file(ss);
  347. if (new_tr)
  348. obs_source_add_active_child(ss->source, new_tr);
  349. if (ss->files.num)
  350. do_transition(ss, false);
  351. obs_data_array_release(array);
  352. }
  353. static void ss_play_pause(void *data)
  354. {
  355. struct slideshow *ss = data;
  356. ss->paused = !ss->paused;
  357. ss->manual = ss->paused;
  358. }
  359. static void ss_restart(void *data)
  360. {
  361. struct slideshow *ss = data;
  362. ss->elapsed = 0.0f;
  363. ss->cur_item = 0;
  364. obs_transition_set(ss->transition,
  365. ss->files.array[ss->cur_item].source);
  366. ss->stop = false;
  367. ss->paused = false;
  368. }
  369. static void ss_stop(void *data)
  370. {
  371. struct slideshow *ss = data;
  372. ss->elapsed = 0.0f;
  373. ss->cur_item = 0;
  374. do_transition(ss, true);
  375. ss->stop = true;
  376. ss->paused = false;
  377. }
  378. static void ss_next_slide(void *data)
  379. {
  380. struct slideshow *ss = data;
  381. if (!ss->files.num)
  382. return;
  383. if (++ss->cur_item >= ss->files.num)
  384. ss->cur_item = 0;
  385. do_transition(ss, false);
  386. }
  387. static void ss_previous_slide(void *data)
  388. {
  389. struct slideshow *ss = data;
  390. if (!ss->files.num)
  391. return;
  392. if (ss->cur_item == 0)
  393. ss->cur_item = ss->files.num - 1;
  394. else
  395. --ss->cur_item;
  396. do_transition(ss, false);
  397. }
  398. static void play_pause_hotkey(void *data, obs_hotkey_id id,
  399. obs_hotkey_t *hotkey, bool pressed)
  400. {
  401. UNUSED_PARAMETER(id);
  402. UNUSED_PARAMETER(hotkey);
  403. struct slideshow *ss = data;
  404. if (pressed && obs_source_active(ss->source))
  405. ss_play_pause(ss);
  406. }
  407. static void restart_hotkey(void *data, obs_hotkey_id id,
  408. obs_hotkey_t *hotkey, bool pressed)
  409. {
  410. UNUSED_PARAMETER(id);
  411. UNUSED_PARAMETER(hotkey);
  412. struct slideshow *ss = data;
  413. if (pressed && obs_source_active(ss->source))
  414. ss_restart(ss);
  415. }
  416. static void stop_hotkey(void *data, obs_hotkey_id id,
  417. obs_hotkey_t *hotkey, bool pressed)
  418. {
  419. UNUSED_PARAMETER(id);
  420. UNUSED_PARAMETER(hotkey);
  421. struct slideshow *ss = data;
  422. if (pressed && obs_source_active(ss->source))
  423. ss_stop(ss);
  424. }
  425. static void next_slide_hotkey(void *data, obs_hotkey_id id,
  426. obs_hotkey_t *hotkey, bool pressed)
  427. {
  428. UNUSED_PARAMETER(id);
  429. UNUSED_PARAMETER(hotkey);
  430. struct slideshow *ss = data;
  431. if (!ss->manual)
  432. return;
  433. if (pressed && obs_source_active(ss->source))
  434. ss_next_slide(ss);
  435. }
  436. static void previous_slide_hotkey(void *data, obs_hotkey_id id,
  437. obs_hotkey_t *hotkey, bool pressed)
  438. {
  439. UNUSED_PARAMETER(id);
  440. UNUSED_PARAMETER(hotkey);
  441. struct slideshow *ss = data;
  442. if (!ss->manual)
  443. return;
  444. if (pressed && obs_source_active(ss->source))
  445. ss_previous_slide(ss);
  446. }
  447. static void ss_destroy(void *data)
  448. {
  449. struct slideshow *ss = data;
  450. obs_source_release(ss->transition);
  451. free_files(&ss->files.da);
  452. pthread_mutex_destroy(&ss->mutex);
  453. bfree(ss);
  454. }
  455. static void *ss_create(obs_data_t *settings, obs_source_t *source)
  456. {
  457. struct slideshow *ss = bzalloc(sizeof(*ss));
  458. ss->source = source;
  459. ss->manual = false;
  460. ss->paused = false;
  461. ss->stop = false;
  462. ss->play_pause_hotkey = obs_hotkey_register_source(source,
  463. "SlideShow.PlayPause",
  464. obs_module_text("SlideShow.PlayPause"),
  465. play_pause_hotkey, ss);
  466. ss->restart_hotkey = obs_hotkey_register_source(source,
  467. "SlideShow.Restart",
  468. obs_module_text("SlideShow.Restart"),
  469. restart_hotkey, ss);
  470. ss->stop_hotkey = obs_hotkey_register_source(source,
  471. "SlideShow.Stop",
  472. obs_module_text("SlideShow.Stop"),
  473. stop_hotkey, ss);
  474. ss->prev_hotkey = obs_hotkey_register_source(source,
  475. "SlideShow.NextSlide",
  476. obs_module_text("SlideShow.NextSlide"),
  477. next_slide_hotkey, ss);
  478. ss->prev_hotkey = obs_hotkey_register_source(source,
  479. "SlideShow.PreviousSlide",
  480. obs_module_text("SlideShow.PreviousSlide"),
  481. previous_slide_hotkey, ss);
  482. pthread_mutex_init_value(&ss->mutex);
  483. if (pthread_mutex_init(&ss->mutex, NULL) != 0)
  484. goto error;
  485. obs_source_update(source, NULL);
  486. UNUSED_PARAMETER(settings);
  487. return ss;
  488. error:
  489. ss_destroy(ss);
  490. return NULL;
  491. }
  492. static void ss_video_render(void *data, gs_effect_t *effect)
  493. {
  494. struct slideshow *ss = data;
  495. obs_source_t *transition = get_transition(ss);
  496. if (transition) {
  497. obs_source_video_render(transition);
  498. obs_source_release(transition);
  499. }
  500. UNUSED_PARAMETER(effect);
  501. }
  502. static void ss_video_tick(void *data, float seconds)
  503. {
  504. struct slideshow *ss = data;
  505. if (!ss->transition || !ss->slide_time)
  506. return;
  507. if (ss->restart_on_activate && !ss->randomize && ss->use_cut) {
  508. ss->elapsed = 0.0f;
  509. ss->cur_item = 0;
  510. do_transition(ss, false);
  511. ss->restart_on_activate = false;
  512. ss->use_cut = false;
  513. ss->stop = false;
  514. return;
  515. }
  516. if (ss->pause_on_deactivate || ss->manual || ss->stop || ss->paused)
  517. return;
  518. ss->elapsed += seconds;
  519. if (ss->elapsed > ss->slide_time) {
  520. ss->elapsed -= ss->slide_time;
  521. if (!ss->loop && ss->cur_item == ss->files.num - 1) {
  522. if (ss->hide)
  523. do_transition(ss, true);
  524. else
  525. do_transition(ss, false);
  526. return;
  527. }
  528. if (ss->randomize) {
  529. size_t next = ss->cur_item;
  530. if (ss->files.num > 1) {
  531. while (next == ss->cur_item)
  532. next = random_file(ss);
  533. }
  534. ss->cur_item = next;
  535. } else if (++ss->cur_item >= ss->files.num) {
  536. ss->cur_item = 0;
  537. }
  538. if (ss->files.num)
  539. do_transition(ss, false);
  540. }
  541. }
  542. static inline bool ss_audio_render_(obs_source_t *transition, uint64_t *ts_out,
  543. struct obs_source_audio_mix *audio_output,
  544. uint32_t mixers, size_t channels, size_t sample_rate)
  545. {
  546. struct obs_source_audio_mix child_audio;
  547. uint64_t source_ts;
  548. if (obs_source_audio_pending(transition))
  549. return false;
  550. source_ts = obs_source_get_audio_timestamp(transition);
  551. if (!source_ts)
  552. return false;
  553. obs_source_get_audio_mix(transition, &child_audio);
  554. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  555. if ((mixers & (1 << mix)) == 0)
  556. continue;
  557. for (size_t ch = 0; ch < channels; ch++) {
  558. float *out = audio_output->output[mix].data[ch];
  559. float *in = child_audio.output[mix].data[ch];
  560. memcpy(out, in, AUDIO_OUTPUT_FRAMES *
  561. MAX_AUDIO_CHANNELS * sizeof(float));
  562. }
  563. }
  564. *ts_out = source_ts;
  565. UNUSED_PARAMETER(sample_rate);
  566. return true;
  567. }
  568. static bool ss_audio_render(void *data, uint64_t *ts_out,
  569. struct obs_source_audio_mix *audio_output,
  570. uint32_t mixers, size_t channels, size_t sample_rate)
  571. {
  572. struct slideshow *ss = data;
  573. obs_source_t *transition = get_transition(ss);
  574. bool success;
  575. if (!transition)
  576. return false;
  577. success = ss_audio_render_(transition, ts_out, audio_output, mixers,
  578. channels, sample_rate);
  579. obs_source_release(transition);
  580. return success;
  581. }
  582. static void ss_enum_sources(void *data, obs_source_enum_proc_t cb, void *param)
  583. {
  584. struct slideshow *ss = data;
  585. pthread_mutex_lock(&ss->mutex);
  586. if (ss->transition)
  587. cb(ss->source, ss->transition, param);
  588. pthread_mutex_unlock(&ss->mutex);
  589. }
  590. static uint32_t ss_width(void *data)
  591. {
  592. struct slideshow *ss = data;
  593. return ss->transition ? ss->cx : 0;
  594. }
  595. static uint32_t ss_height(void *data)
  596. {
  597. struct slideshow *ss = data;
  598. return ss->transition ? ss->cy : 0;
  599. }
  600. static void ss_defaults(obs_data_t *settings)
  601. {
  602. obs_data_set_default_string(settings, S_TRANSITION, "fade");
  603. obs_data_set_default_int(settings, S_SLIDE_TIME, 8000);
  604. obs_data_set_default_int(settings, S_TR_SPEED, 700);
  605. obs_data_set_default_string(settings, S_CUSTOM_SIZE, T_CUSTOM_SIZE_AUTO);
  606. obs_data_set_default_string(settings, S_BEHAVIOR,
  607. S_BEHAVIOR_ALWAYS_PLAY);
  608. obs_data_set_default_string(settings, S_MODE, S_MODE_AUTO);
  609. obs_data_set_default_bool(settings, S_LOOP, true);
  610. }
  611. static const char *file_filter =
  612. "Image files (*.bmp *.tga *.png *.jpeg *.jpg *.gif)";
  613. static const char *aspects[] = {
  614. "16:9",
  615. "16:10",
  616. "4:3",
  617. "1:1"
  618. };
  619. #define NUM_ASPECTS (sizeof(aspects) / sizeof(const char *))
  620. static obs_properties_t *ss_properties(void *data)
  621. {
  622. obs_properties_t *ppts = obs_properties_create();
  623. struct slideshow *ss = data;
  624. struct obs_video_info ovi;
  625. struct dstr path = {0};
  626. obs_property_t *p;
  627. int cx;
  628. int cy;
  629. /* ----------------- */
  630. obs_get_video_info(&ovi);
  631. cx = (int)ovi.base_width;
  632. cy = (int)ovi.base_height;
  633. /* ----------------- */
  634. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  635. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  636. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  637. S_BEHAVIOR_ALWAYS_PLAY);
  638. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  639. S_BEHAVIOR_STOP_RESTART);
  640. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  641. S_BEHAVIOR_PAUSE_UNPAUSE);
  642. p = obs_properties_add_list(ppts, S_MODE, T_MODE,
  643. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  644. obs_property_list_add_string(p, T_MODE_AUTO, S_MODE_AUTO);
  645. obs_property_list_add_string(p, T_MODE_MANUAL, S_MODE_MANUAL);
  646. p = obs_properties_add_list(ppts, S_TRANSITION, T_TRANSITION,
  647. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  648. obs_property_list_add_string(p, T_TR_CUT, TR_CUT);
  649. obs_property_list_add_string(p, T_TR_FADE, TR_FADE);
  650. obs_property_list_add_string(p, T_TR_SWIPE, TR_SWIPE);
  651. obs_property_list_add_string(p, T_TR_SLIDE, TR_SLIDE);
  652. obs_properties_add_int(ppts, S_SLIDE_TIME, T_SLIDE_TIME,
  653. 50, 3600000, 50);
  654. obs_properties_add_int(ppts, S_TR_SPEED, T_TR_SPEED,
  655. 0, 3600000, 50);
  656. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  657. obs_properties_add_bool(ppts, S_HIDE, T_HIDE);
  658. obs_properties_add_bool(ppts, S_RANDOMIZE, T_RANDOMIZE);
  659. p = obs_properties_add_list(ppts, S_CUSTOM_SIZE, T_CUSTOM_SIZE,
  660. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  661. obs_property_list_add_string(p, T_CUSTOM_SIZE_AUTO, T_CUSTOM_SIZE_AUTO);
  662. for (size_t i = 0; i < NUM_ASPECTS; i++)
  663. obs_property_list_add_string(p, aspects[i], aspects[i]);
  664. char str[32];
  665. snprintf(str, 32, "%dx%d", cx, cy);
  666. obs_property_list_add_string(p, str, str);
  667. if (ss) {
  668. pthread_mutex_lock(&ss->mutex);
  669. if (ss->files.num) {
  670. struct image_file_data *last = da_end(ss->files);
  671. const char *slash;
  672. dstr_copy(&path, last->path);
  673. dstr_replace(&path, "\\", "/");
  674. slash = strrchr(path.array, '/');
  675. if (slash)
  676. dstr_resize(&path, slash - path.array + 1);
  677. }
  678. pthread_mutex_unlock(&ss->mutex);
  679. }
  680. obs_properties_add_editable_list(ppts, S_FILES, T_FILES,
  681. OBS_EDITABLE_LIST_TYPE_FILES, file_filter, path.array);
  682. dstr_free(&path);
  683. return ppts;
  684. }
  685. static void ss_activate(void *data)
  686. {
  687. struct slideshow *ss = data;
  688. if (ss->behavior == BEHAVIOR_STOP_RESTART) {
  689. ss->restart_on_activate = true;
  690. ss->use_cut = true;
  691. } else if (ss->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  692. ss->pause_on_deactivate = false;
  693. }
  694. }
  695. static void ss_deactivate(void *data)
  696. {
  697. struct slideshow *ss = data;
  698. if (ss->behavior == BEHAVIOR_PAUSE_UNPAUSE)
  699. ss->pause_on_deactivate = true;
  700. }
  701. struct obs_source_info slideshow_info = {
  702. .id = "slideshow",
  703. .type = OBS_SOURCE_TYPE_INPUT,
  704. .output_flags = OBS_SOURCE_VIDEO |
  705. OBS_SOURCE_CUSTOM_DRAW |
  706. OBS_SOURCE_COMPOSITE,
  707. .get_name = ss_getname,
  708. .create = ss_create,
  709. .destroy = ss_destroy,
  710. .update = ss_update,
  711. .activate = ss_activate,
  712. .deactivate = ss_deactivate,
  713. .video_render = ss_video_render,
  714. .video_tick = ss_video_tick,
  715. .audio_render = ss_audio_render,
  716. .enum_active_sources = ss_enum_sources,
  717. .get_width = ss_width,
  718. .get_height = ss_height,
  719. .get_defaults = ss_defaults,
  720. .get_properties = ss_properties
  721. };