obs-slideshow.c 22 KB

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