obs-slideshow.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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_FILES "files"
  16. #define TR_CUT "cut"
  17. #define TR_FADE "fade"
  18. #define TR_SWIPE "swipe"
  19. #define TR_SLIDE "slide"
  20. #define T_(text) obs_module_text("SlideShow." text)
  21. #define T_TR_SPEED T_("TransitionSpeed")
  22. #define T_CUSTOM_SIZE T_("CustomSize")
  23. #define T_CUSTOM_SIZE_AUTO T_("CustomSize.Auto")
  24. #define T_SLIDE_TIME T_("SlideTime")
  25. #define T_TRANSITION T_("Transition")
  26. #define T_RANDOMIZE T_("Randomize")
  27. #define T_FILES T_("Files")
  28. #define T_TR_(text) obs_module_text("SlideShow.Transition." text)
  29. #define T_TR_CUT T_TR_("Cut")
  30. #define T_TR_FADE T_TR_("Fade")
  31. #define T_TR_SWIPE T_TR_("Swipe")
  32. #define T_TR_SLIDE T_TR_("Slide")
  33. /* ------------------------------------------------------------------------- */
  34. struct image_file_data {
  35. char *path;
  36. obs_source_t *source;
  37. };
  38. struct slideshow {
  39. obs_source_t *source;
  40. bool randomize;
  41. float slide_time;
  42. uint32_t tr_speed;
  43. const char *tr_name;
  44. obs_source_t *transition;
  45. float elapsed;
  46. size_t cur_item;
  47. uint32_t cx;
  48. uint32_t cy;
  49. pthread_mutex_t mutex;
  50. DARRAY(struct image_file_data) files;
  51. };
  52. static obs_source_t *get_transition(struct slideshow *ss)
  53. {
  54. obs_source_t *tr;
  55. pthread_mutex_lock(&ss->mutex);
  56. tr = ss->transition;
  57. obs_source_addref(tr);
  58. pthread_mutex_unlock(&ss->mutex);
  59. return tr;
  60. }
  61. static obs_source_t *get_source(struct darray *array, const char *path)
  62. {
  63. DARRAY(struct image_file_data) files;
  64. obs_source_t *source = NULL;
  65. files.da = *array;
  66. for (size_t i = 0; i < files.num; i++) {
  67. const char *cur_path = files.array[i].path;
  68. if (strcmp(path, cur_path) == 0) {
  69. source = files.array[i].source;
  70. obs_source_addref(source);
  71. break;
  72. }
  73. }
  74. return source;
  75. }
  76. static obs_source_t *create_source_from_file(const char *file)
  77. {
  78. obs_data_t *settings = obs_data_create();
  79. obs_source_t *source;
  80. obs_data_set_string(settings, "file", file);
  81. obs_data_set_bool(settings, "unload", false);
  82. source = obs_source_create_private("image_source", NULL, settings);
  83. obs_data_release(settings);
  84. return source;
  85. }
  86. static void free_files(struct darray *array)
  87. {
  88. DARRAY(struct image_file_data) files;
  89. files.da = *array;
  90. for (size_t i = 0; i < files.num; i++) {
  91. bfree(files.array[i].path);
  92. obs_source_release(files.array[i].source);
  93. }
  94. da_free(files);
  95. }
  96. static inline size_t random_file(struct slideshow *ss)
  97. {
  98. return (size_t)rand() % ss->files.num;
  99. }
  100. /* ------------------------------------------------------------------------- */
  101. static const char *ss_getname(void *unused)
  102. {
  103. UNUSED_PARAMETER(unused);
  104. return obs_module_text("SlideShow");
  105. }
  106. static void add_file(struct slideshow *ss, struct darray *array,
  107. const char *path, uint32_t *cx, uint32_t *cy)
  108. {
  109. DARRAY(struct image_file_data) new_files;
  110. struct image_file_data data;
  111. obs_source_t *new_source;
  112. new_files.da = *array;
  113. pthread_mutex_lock(&ss->mutex);
  114. new_source = get_source(&ss->files.da, path);
  115. pthread_mutex_unlock(&ss->mutex);
  116. if (!new_source)
  117. new_source = get_source(&new_files.da, path);
  118. if (!new_source)
  119. new_source = create_source_from_file(path);
  120. if (new_source) {
  121. uint32_t new_cx = obs_source_get_width(new_source);
  122. uint32_t new_cy = obs_source_get_height(new_source);
  123. data.path = bstrdup(path);
  124. data.source = new_source;
  125. da_push_back(new_files, &data);
  126. if (new_cx > *cx) *cx = new_cx;
  127. if (new_cy > *cy) *cy = new_cy;
  128. }
  129. *array = new_files.da;
  130. }
  131. static bool valid_extension(const char *ext)
  132. {
  133. if (!ext)
  134. return false;
  135. return astrcmpi(ext, ".bmp") == 0 ||
  136. astrcmpi(ext, ".tga") == 0 ||
  137. astrcmpi(ext, ".png") == 0 ||
  138. astrcmpi(ext, ".jpeg") == 0 ||
  139. astrcmpi(ext, ".jpg") == 0 ||
  140. astrcmpi(ext, ".gif") == 0;
  141. }
  142. static void ss_update(void *data, obs_data_t *settings)
  143. {
  144. DARRAY(struct image_file_data) new_files;
  145. DARRAY(struct image_file_data) old_files;
  146. obs_source_t *new_tr = NULL;
  147. obs_source_t *old_tr = NULL;
  148. struct slideshow *ss = data;
  149. obs_data_array_t *array;
  150. const char *tr_name;
  151. uint32_t new_duration;
  152. uint32_t new_speed;
  153. uint32_t cx = 0;
  154. uint32_t cy = 0;
  155. size_t count;
  156. /* ------------------------------------- */
  157. /* get settings data */
  158. da_init(new_files);
  159. tr_name = obs_data_get_string(settings, S_TRANSITION);
  160. if (astrcmpi(tr_name, TR_CUT) == 0)
  161. tr_name = "cut_transition";
  162. else if (astrcmpi(tr_name, TR_SWIPE) == 0)
  163. tr_name = "swipe_transition";
  164. else if (astrcmpi(tr_name, TR_SLIDE) == 0)
  165. tr_name = "slide_transition";
  166. else
  167. tr_name = "fade_transition";
  168. ss->randomize = obs_data_get_bool(settings, S_RANDOMIZE);
  169. if (!ss->tr_name || strcmp(tr_name, ss->tr_name) != 0)
  170. new_tr = obs_source_create_private(tr_name, NULL, NULL);
  171. new_duration = (uint32_t)obs_data_get_int(settings, S_SLIDE_TIME);
  172. new_speed = (uint32_t)obs_data_get_int(settings, S_TR_SPEED);
  173. array = obs_data_get_array(settings, S_FILES);
  174. count = obs_data_array_count(array);
  175. /* ------------------------------------- */
  176. /* create new list of sources */
  177. for (size_t i = 0; i < count; i++) {
  178. obs_data_t *item = obs_data_array_item(array, i);
  179. const char *path = obs_data_get_string(item, "value");
  180. os_dir_t *dir = os_opendir(path);
  181. if (dir) {
  182. struct dstr dir_path = {0};
  183. struct os_dirent *ent;
  184. for (;;) {
  185. const char *ext;
  186. ent = os_readdir(dir);
  187. if (!ent)
  188. break;
  189. if (ent->directory)
  190. continue;
  191. ext = os_get_path_extension(ent->d_name);
  192. if (!valid_extension(ext))
  193. continue;
  194. dstr_copy(&dir_path, path);
  195. dstr_cat_ch(&dir_path, '/');
  196. dstr_cat(&dir_path, ent->d_name);
  197. add_file(ss, &new_files.da, dir_path.array,
  198. &cx, &cy);
  199. }
  200. dstr_free(&dir_path);
  201. os_closedir(dir);
  202. } else {
  203. add_file(ss, &new_files.da, path, &cx, &cy);
  204. }
  205. obs_data_release(item);
  206. }
  207. /* ------------------------------------- */
  208. /* update settings data */
  209. pthread_mutex_lock(&ss->mutex);
  210. old_files.da = ss->files.da;
  211. ss->files.da = new_files.da;
  212. if (new_tr) {
  213. old_tr = ss->transition;
  214. ss->transition = new_tr;
  215. }
  216. if (new_duration < 50)
  217. new_duration = 50;
  218. if (new_speed > (new_duration - 50))
  219. new_speed = new_duration - 50;
  220. ss->tr_speed = new_speed;
  221. ss->tr_name = tr_name;
  222. ss->slide_time = (float)new_duration / 1000.0f;
  223. pthread_mutex_unlock(&ss->mutex);
  224. /* ------------------------------------- */
  225. /* clean up and restart transition */
  226. if (old_tr)
  227. obs_source_release(old_tr);
  228. free_files(&old_files.da);
  229. /* ------------------------- */
  230. const char *res_str = obs_data_get_string(settings, S_CUSTOM_SIZE);
  231. bool aspect_only = false, use_auto = true;
  232. int cx_in = 0, cy_in = 0;
  233. if (strcmp(res_str, T_CUSTOM_SIZE_AUTO) != 0) {
  234. int ret = sscanf(res_str, "%dx%d", &cx_in, &cy_in);
  235. if (ret == 2) {
  236. aspect_only = false;
  237. use_auto = false;
  238. } else {
  239. ret = sscanf(res_str, "%d:%d", &cx_in, &cy_in);
  240. if (ret == 2) {
  241. aspect_only = true;
  242. use_auto = false;
  243. }
  244. }
  245. }
  246. if (!use_auto) {
  247. double cx_f = (double)cx;
  248. double cy_f = (double)cy;
  249. double old_aspect = cx_f / cy_f;
  250. double new_aspect = (double)cx_in / (double)cy_in;
  251. if (aspect_only) {
  252. if (fabs(old_aspect - new_aspect) > EPSILON) {
  253. if (new_aspect > old_aspect)
  254. cx = (uint32_t)(cy_f * new_aspect);
  255. else
  256. cy = (uint32_t)(cx_f / new_aspect);
  257. }
  258. } else {
  259. cx = (uint32_t)cx_in;
  260. cy = (uint32_t)cy_in;
  261. }
  262. }
  263. /* ------------------------- */
  264. ss->cx = cx;
  265. ss->cy = cy;
  266. ss->cur_item = 0;
  267. ss->elapsed = 0.0f;
  268. obs_transition_set_size(ss->transition, cx, cy);
  269. obs_transition_set_alignment(ss->transition, OBS_ALIGN_CENTER);
  270. obs_transition_set_scale_type(ss->transition,
  271. OBS_TRANSITION_SCALE_ASPECT);
  272. if (ss->randomize && ss->files.num)
  273. ss->cur_item = random_file(ss);
  274. if (new_tr)
  275. obs_source_add_active_child(ss->source, new_tr);
  276. if (ss->files.num)
  277. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  278. ss->tr_speed,
  279. ss->files.array[ss->cur_item].source);
  280. obs_data_array_release(array);
  281. }
  282. static void ss_destroy(void *data)
  283. {
  284. struct slideshow *ss = data;
  285. obs_source_release(ss->transition);
  286. free_files(&ss->files.da);
  287. pthread_mutex_destroy(&ss->mutex);
  288. bfree(ss);
  289. }
  290. static void *ss_create(obs_data_t *settings, obs_source_t *source)
  291. {
  292. struct slideshow *ss = bzalloc(sizeof(*ss));
  293. ss->source = source;
  294. pthread_mutex_init_value(&ss->mutex);
  295. if (pthread_mutex_init(&ss->mutex, NULL) != 0)
  296. goto error;
  297. obs_source_update(source, NULL);
  298. UNUSED_PARAMETER(settings);
  299. return ss;
  300. error:
  301. ss_destroy(ss);
  302. return NULL;
  303. }
  304. static void ss_video_render(void *data, gs_effect_t *effect)
  305. {
  306. struct slideshow *ss = data;
  307. obs_source_t *transition = get_transition(ss);
  308. if (transition) {
  309. obs_source_video_render(transition);
  310. obs_source_release(transition);
  311. }
  312. UNUSED_PARAMETER(effect);
  313. }
  314. static void ss_video_tick(void *data, float seconds)
  315. {
  316. struct slideshow *ss = data;
  317. if (!ss->transition || !ss->slide_time)
  318. return;
  319. ss->elapsed += seconds;
  320. if (ss->elapsed > ss->slide_time) {
  321. ss->elapsed -= ss->slide_time;
  322. if (ss->randomize) {
  323. size_t next = ss->cur_item;
  324. if (ss->files.num > 1) {
  325. while (next == ss->cur_item)
  326. next = random_file(ss);
  327. }
  328. ss->cur_item = next;
  329. } else if (++ss->cur_item >= ss->files.num) {
  330. ss->cur_item = 0;
  331. }
  332. if (ss->files.num)
  333. obs_transition_start(ss->transition,
  334. OBS_TRANSITION_MODE_AUTO, ss->tr_speed,
  335. ss->files.array[ss->cur_item].source);
  336. }
  337. }
  338. static inline bool ss_audio_render_(obs_source_t *transition, uint64_t *ts_out,
  339. struct obs_source_audio_mix *audio_output,
  340. uint32_t mixers, size_t channels, size_t sample_rate)
  341. {
  342. struct obs_source_audio_mix child_audio;
  343. uint64_t source_ts;
  344. if (obs_source_audio_pending(transition))
  345. return false;
  346. source_ts = obs_source_get_audio_timestamp(transition);
  347. if (!source_ts)
  348. return false;
  349. obs_source_get_audio_mix(transition, &child_audio);
  350. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  351. if ((mixers & (1 << mix)) == 0)
  352. continue;
  353. for (size_t ch = 0; ch < channels; ch++) {
  354. float *out = audio_output->output[mix].data[ch];
  355. float *in = child_audio.output[mix].data[ch];
  356. memcpy(out, in, AUDIO_OUTPUT_FRAMES *
  357. MAX_AUDIO_CHANNELS * sizeof(float));
  358. }
  359. }
  360. *ts_out = source_ts;
  361. UNUSED_PARAMETER(sample_rate);
  362. return true;
  363. }
  364. static bool ss_audio_render(void *data, uint64_t *ts_out,
  365. struct obs_source_audio_mix *audio_output,
  366. uint32_t mixers, size_t channels, size_t sample_rate)
  367. {
  368. struct slideshow *ss = data;
  369. obs_source_t *transition = get_transition(ss);
  370. bool success;
  371. if (!transition)
  372. return false;
  373. success = ss_audio_render_(transition, ts_out, audio_output, mixers,
  374. channels, sample_rate);
  375. obs_source_release(transition);
  376. return success;
  377. }
  378. static void ss_enum_sources(void *data, obs_source_enum_proc_t cb, void *param)
  379. {
  380. struct slideshow *ss = data;
  381. pthread_mutex_lock(&ss->mutex);
  382. if (ss->transition)
  383. cb(ss->source, ss->transition, param);
  384. pthread_mutex_unlock(&ss->mutex);
  385. }
  386. static uint32_t ss_width(void *data)
  387. {
  388. struct slideshow *ss = data;
  389. return ss->transition ? ss->cx : 0;
  390. }
  391. static uint32_t ss_height(void *data)
  392. {
  393. struct slideshow *ss = data;
  394. return ss->transition ? ss->cy : 0;
  395. }
  396. static void ss_defaults(obs_data_t *settings)
  397. {
  398. obs_data_set_default_string(settings, S_TRANSITION, "fade");
  399. obs_data_set_default_int(settings, S_SLIDE_TIME, 8000);
  400. obs_data_set_default_int(settings, S_TR_SPEED, 700);
  401. obs_data_set_default_string(settings, S_CUSTOM_SIZE, T_CUSTOM_SIZE_AUTO);
  402. }
  403. static const char *file_filter =
  404. "Image files (*.bmp *.tga *.png *.jpeg *.jpg *.gif)";
  405. static const char *aspects[] = {
  406. "16:9",
  407. "16:10",
  408. "4:3",
  409. "1:1"
  410. };
  411. #define NUM_ASPECTS (sizeof(aspects) / sizeof(const char *))
  412. static obs_properties_t *ss_properties(void *data)
  413. {
  414. obs_properties_t *ppts = obs_properties_create();
  415. struct slideshow *ss = data;
  416. struct obs_video_info ovi;
  417. struct dstr path = {0};
  418. obs_property_t *p;
  419. int cx;
  420. int cy;
  421. /* ----------------- */
  422. obs_get_video_info(&ovi);
  423. cx = (int)ovi.base_width;
  424. cy = (int)ovi.base_height;
  425. /* ----------------- */
  426. p = obs_properties_add_list(ppts, S_TRANSITION, T_TRANSITION,
  427. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  428. obs_property_list_add_string(p, T_TR_CUT, TR_CUT);
  429. obs_property_list_add_string(p, T_TR_FADE, TR_FADE);
  430. obs_property_list_add_string(p, T_TR_SWIPE, TR_SWIPE);
  431. obs_property_list_add_string(p, T_TR_SLIDE, TR_SLIDE);
  432. obs_properties_add_int(ppts, S_SLIDE_TIME, T_SLIDE_TIME,
  433. 50, 3600000, 50);
  434. obs_properties_add_int(ppts, S_TR_SPEED, T_TR_SPEED,
  435. 0, 3600000, 50);
  436. obs_properties_add_bool(ppts, S_RANDOMIZE, T_RANDOMIZE);
  437. p = obs_properties_add_list(ppts, S_CUSTOM_SIZE, T_CUSTOM_SIZE,
  438. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  439. obs_property_list_add_string(p, T_CUSTOM_SIZE_AUTO, T_CUSTOM_SIZE_AUTO);
  440. for (size_t i = 0; i < NUM_ASPECTS; i++)
  441. obs_property_list_add_string(p, aspects[i], aspects[i]);
  442. char str[32];
  443. snprintf(str, 32, "%dx%d", cx, cy);
  444. obs_property_list_add_string(p, str, str);
  445. if (ss) {
  446. pthread_mutex_lock(&ss->mutex);
  447. if (ss->files.num) {
  448. struct image_file_data *last = da_end(ss->files);
  449. const char *slash;
  450. dstr_copy(&path, last->path);
  451. dstr_replace(&path, "\\", "/");
  452. slash = strrchr(path.array, '/');
  453. if (slash)
  454. dstr_resize(&path, slash - path.array + 1);
  455. }
  456. pthread_mutex_unlock(&ss->mutex);
  457. }
  458. obs_properties_add_editable_list(ppts, S_FILES, T_FILES,
  459. OBS_EDITABLE_LIST_TYPE_FILES, file_filter, path.array);
  460. dstr_free(&path);
  461. return ppts;
  462. }
  463. struct obs_source_info slideshow_info = {
  464. .id = "slideshow",
  465. .type = OBS_SOURCE_TYPE_INPUT,
  466. .output_flags = OBS_SOURCE_VIDEO |
  467. OBS_SOURCE_CUSTOM_DRAW |
  468. OBS_SOURCE_COMPOSITE,
  469. .get_name = ss_getname,
  470. .create = ss_create,
  471. .destroy = ss_destroy,
  472. .update = ss_update,
  473. .video_render = ss_video_render,
  474. .video_tick = ss_video_tick,
  475. .audio_render = ss_audio_render,
  476. .enum_active_sources = ss_enum_sources,
  477. .get_width = ss_width,
  478. .get_height = ss_height,
  479. .get_defaults = ss_defaults,
  480. .get_properties = ss_properties
  481. };