obs-slideshow.c 13 KB

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