obs-slideshow.c 27 KB

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