obs-slideshow-mk2.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. #include <obs-module.h>
  2. #include <util/deque.h>
  3. #include <util/threading.h>
  4. #include <util/platform.h>
  5. #include <util/darray.h>
  6. #include <util/dstr.h>
  7. #include <util/task.h>
  8. #include <inttypes.h>
  9. #define do_log(level, format, ...) \
  10. blog(level, "[slideshow: '%s'] " format, \
  11. obs_source_get_name(ss->source), ##__VA_ARGS__)
  12. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  13. /* clang-format off */
  14. static const char *S_TR_SPEED = "transition_speed";
  15. static const char *S_CUSTOM_SIZE = "use_custom_size";
  16. static const char *S_SLIDE_TIME = "slide_time";
  17. static const char *S_TRANSITION = "transition";
  18. static const char *S_RANDOMIZE = "randomize";
  19. static const char *S_LOOP = "loop";
  20. static const char *S_HIDE = "hide";
  21. static const char *S_FILES = "files";
  22. static const char *S_BEHAVIOR = "playback_behavior";
  23. static const char *S_BEHAVIOR_STOP_RESTART = "stop_restart";
  24. static const char *S_BEHAVIOR_PAUSE_UNPAUSE = "pause_unpause";
  25. static const char *S_BEHAVIOR_ALWAYS_PLAY = "always_play";
  26. static const char *S_MODE = "slide_mode";
  27. static const char *S_MODE_AUTO = "mode_auto";
  28. static const char *S_MODE_MANUAL = "mode_manual";
  29. static const char *TR_CUT = "cut";
  30. static const char *TR_FADE = "fade";
  31. static const char *TR_SWIPE = "swipe";
  32. static const char *TR_SLIDE = "slide";
  33. #define T_(text) obs_module_text("SlideShow." text)
  34. #define T_TR_SPEED T_("TransitionSpeed")
  35. #define T_CUSTOM_SIZE T_("CustomSize")
  36. #define T_SLIDE_TIME T_("SlideTime")
  37. #define T_TRANSITION T_("Transition")
  38. #define T_RANDOMIZE T_("Randomize")
  39. #define T_LOOP T_("Loop")
  40. #define T_HIDE T_("HideWhenDone")
  41. #define T_FILES T_("Files")
  42. #define T_BEHAVIOR T_("PlaybackBehavior")
  43. #define T_BEHAVIOR_STOP_RESTART T_("PlaybackBehavior.StopRestart")
  44. #define T_BEHAVIOR_PAUSE_UNPAUSE T_("PlaybackBehavior.PauseUnpause")
  45. #define T_BEHAVIOR_ALWAYS_PLAY T_("PlaybackBehavior.AlwaysPlay")
  46. #define T_MODE T_("SlideMode")
  47. #define T_MODE_AUTO T_("SlideMode.Auto")
  48. #define T_MODE_MANUAL T_("SlideMode.Manual")
  49. #define T_TR_(text) obs_module_text("SlideShow.Transition." text)
  50. #define T_TR_CUT T_TR_("Cut")
  51. #define T_TR_FADE T_TR_("Fade")
  52. #define T_TR_SWIPE T_TR_("Swipe")
  53. #define T_TR_SLIDE T_TR_("Slide")
  54. /* clang-format on */
  55. extern void image_source_preload_image(void *data);
  56. /* ------------------------------------------------------------------------- */
  57. struct image_file_data {
  58. char *path;
  59. };
  60. struct source_data {
  61. size_t slide_idx;
  62. const char *path;
  63. obs_source_t *source;
  64. };
  65. typedef DARRAY(struct image_file_data) image_file_array_t;
  66. enum behavior {
  67. BEHAVIOR_STOP_RESTART,
  68. BEHAVIOR_PAUSE_UNPAUSE,
  69. BEHAVIOR_ALWAYS_PLAY,
  70. };
  71. #define SLIDE_BUFFER_COUNT 5
  72. struct active_slides {
  73. struct deque prev;
  74. struct deque next;
  75. struct source_data cur;
  76. };
  77. struct slideshow_data {
  78. struct active_slides slides;
  79. image_file_array_t files;
  80. float slide_time;
  81. uint32_t tr_speed;
  82. const char *tr_name;
  83. bool manual;
  84. bool randomize;
  85. bool loop;
  86. bool restart_on_activate;
  87. bool pause_on_deactivate;
  88. bool restart;
  89. bool hide;
  90. bool use_cut;
  91. bool paused;
  92. bool stop;
  93. calldata_t cd;
  94. float elapsed;
  95. enum behavior behavior;
  96. enum obs_media_state state;
  97. };
  98. struct slideshow {
  99. obs_source_t *source;
  100. struct slideshow_data data;
  101. os_task_queue_t *queue;
  102. obs_source_t *transition;
  103. uint32_t cx;
  104. uint32_t cy;
  105. obs_hotkey_id play_pause_hotkey;
  106. obs_hotkey_id restart_hotkey;
  107. obs_hotkey_id stop_hotkey;
  108. obs_hotkey_id next_hotkey;
  109. obs_hotkey_id prev_hotkey;
  110. };
  111. static void set_media_state(void *data, enum obs_media_state state)
  112. {
  113. struct slideshow *ss = data;
  114. ss->data.state = state;
  115. }
  116. static enum obs_media_state ss_get_state(void *data)
  117. {
  118. struct slideshow *ss = data;
  119. return ss->data.state;
  120. }
  121. static inline obs_source_t *get_transition(struct slideshow *ss)
  122. {
  123. return obs_source_get_ref(ss->transition);
  124. }
  125. static inline void free_source_data(struct source_data *sd)
  126. {
  127. obs_source_release(sd->source);
  128. }
  129. static void free_active_slides(struct active_slides *slides)
  130. {
  131. while (slides->prev.size) {
  132. struct source_data file;
  133. deque_pop_front(&slides->prev, &file, sizeof(file));
  134. free_source_data(&file);
  135. }
  136. while (slides->next.size) {
  137. struct source_data file;
  138. deque_pop_front(&slides->next, &file, sizeof(file));
  139. free_source_data(&file);
  140. }
  141. free_source_data(&slides->cur);
  142. deque_free(&slides->prev);
  143. deque_free(&slides->next);
  144. }
  145. static inline void free_slideshow_data(struct slideshow_data *ssd)
  146. {
  147. free_active_slides(&ssd->slides);
  148. for (size_t i = 0; i < ssd->files.num; i++)
  149. bfree(ssd->files.array[i].path);
  150. calldata_free(&ssd->cd);
  151. da_free(ssd->files);
  152. }
  153. static size_t random_file(struct slideshow_data *ssd, size_t slide_idx)
  154. {
  155. const size_t files = ssd->files.num;
  156. size_t next = slide_idx;
  157. if (ssd->files.num > 1) {
  158. while (next == slide_idx) {
  159. size_t threshold = (~files + 1) % files;
  160. size_t r;
  161. do {
  162. r = rand();
  163. } while (r < threshold);
  164. next = r % files;
  165. }
  166. }
  167. return next;
  168. }
  169. /* gets a new file idx based upon a prior index */
  170. static inline size_t get_new_file(struct slideshow_data *ssd, size_t slide_idx,
  171. bool next)
  172. {
  173. if (ssd->randomize) {
  174. return random_file(ssd, slide_idx);
  175. } else if (next) {
  176. return (++slide_idx >= ssd->files.num) ? 0 : slide_idx;
  177. } else {
  178. return (slide_idx == 0) ? ssd->files.num - 1 : slide_idx - 1;
  179. }
  180. }
  181. /* ------------------------------------------------------------------------- */
  182. static const char *ss_getname(void *unused)
  183. {
  184. UNUSED_PARAMETER(unused);
  185. return obs_module_text("SlideShow");
  186. }
  187. /* adds a new file to the files list. used only in update */
  188. static inline void add_file(image_file_array_t *new_files, const char *path)
  189. {
  190. struct image_file_data data;
  191. data.path = bstrdup(path);
  192. da_push_back(*new_files, &data);
  193. }
  194. extern bool valid_extension(const char *ext);
  195. /* transition to new source. assumes cur has already been set to new file */
  196. static void do_transition(void *data, bool to_null)
  197. {
  198. struct slideshow *ss = data;
  199. struct slideshow_data *ssd = &ss->data;
  200. bool valid = !!ss->data.files.num;
  201. if (valid && ssd->use_cut) {
  202. obs_transition_set(ss->transition, ssd->slides.cur.source);
  203. } else if (valid && !to_null) {
  204. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  205. ssd->tr_speed, ssd->slides.cur.source);
  206. } else {
  207. obs_transition_start(ss->transition, OBS_TRANSITION_MODE_AUTO,
  208. ssd->tr_speed, NULL);
  209. set_media_state(ss, OBS_MEDIA_STATE_ENDED);
  210. obs_source_media_ended(ss->source);
  211. }
  212. if (valid && !to_null) {
  213. calldata_set_int(&ssd->cd, "index", ssd->slides.cur.slide_idx);
  214. calldata_set_string(&ssd->cd, "path", ssd->slides.cur.path);
  215. signal_handler_t *sh =
  216. obs_source_get_signal_handler(ss->source);
  217. signal_handler_signal(sh, "slide_changed", &ssd->cd);
  218. }
  219. }
  220. /* get a source via its slide idx in one of slideshow_data's deques. *
  221. * only used in get_new_source(). */
  222. static inline struct source_data *deque_get_source(struct deque *buf,
  223. size_t slide_idx)
  224. {
  225. struct source_data *cur;
  226. size_t count = buf->size / sizeof(*cur);
  227. for (size_t i = 0; i < count; i++) {
  228. cur = deque_data(buf, i * sizeof(struct source_data));
  229. if (cur->slide_idx == slide_idx)
  230. return cur;
  231. }
  232. return NULL;
  233. }
  234. static void decode_image(void *data)
  235. {
  236. obs_weak_source_t *weak = data;
  237. obs_source_t *source = obs_weak_source_get_source(weak);
  238. if (source) {
  239. image_source_preload_image(obs_obj_get_data(source));
  240. obs_source_release(source);
  241. }
  242. obs_weak_source_release(weak);
  243. }
  244. /* creates source from a file path. only used in get_new_source(). */
  245. static inline obs_source_t *create_source_from_file(struct slideshow *ss,
  246. const char *file, bool now)
  247. {
  248. obs_data_t *settings = obs_data_create();
  249. obs_source_t *source;
  250. obs_data_set_string(settings, "file", file);
  251. obs_data_set_bool(settings, "unload", false);
  252. obs_data_set_bool(settings, "is_slide", !now);
  253. source = obs_source_create_private("image_source", NULL, settings);
  254. obs_data_release(settings);
  255. os_task_queue_queue_task(ss->queue, decode_image,
  256. obs_source_get_weak_source(source));
  257. return source;
  258. }
  259. /* searches the active slides for the same slide so we can reuse existing *
  260. * sources */
  261. static struct source_data *find_existing_source(struct active_slides *as,
  262. size_t slide_idx)
  263. {
  264. struct source_data *psd;
  265. if (as->cur.source && as->cur.slide_idx == slide_idx)
  266. return &as->cur;
  267. psd = deque_get_source(&as->prev, slide_idx);
  268. if (psd)
  269. return psd;
  270. psd = deque_get_source(&as->next, slide_idx);
  271. return psd;
  272. }
  273. /* get a new source_data structure and reuse existing sources if possible. *
  274. * use the 'new' parameter if you want to create a brand new list of *
  275. * active sources while still reusing the old list as well. */
  276. static struct source_data get_new_source(struct slideshow *ss,
  277. struct active_slides *new,
  278. size_t slide_idx)
  279. {
  280. struct slideshow_data *ssd = &ss->data;
  281. struct source_data *psd;
  282. struct source_data sd;
  283. /* ----------------------------------------------- */
  284. /* use existing source if we already have it */
  285. psd = find_existing_source(&ssd->slides, slide_idx);
  286. if (psd) {
  287. sd = *psd;
  288. sd.source = obs_source_get_ref(sd.source);
  289. if (sd.source) {
  290. return sd;
  291. }
  292. }
  293. if (new) {
  294. psd = find_existing_source(new, slide_idx);
  295. if (psd) {
  296. sd = *psd;
  297. sd.source = obs_source_get_ref(sd.source);
  298. if (sd.source) {
  299. return sd;
  300. }
  301. }
  302. }
  303. /* ----------------------------------------------- */
  304. /* and then create a new one if we don't */
  305. sd.path = ssd->files.array[slide_idx].path;
  306. sd.slide_idx = slide_idx;
  307. sd.source = create_source_from_file(ss, sd.path, false);
  308. return sd;
  309. }
  310. static void restart_slides(struct slideshow *ss)
  311. {
  312. struct slideshow_data *ssd = &ss->data;
  313. size_t start_idx = ssd->randomize ? (size_t)rand() % ssd->files.num : 0;
  314. struct active_slides new_slides = {0};
  315. if (ssd->files.num) {
  316. struct source_data sd;
  317. size_t idx;
  318. new_slides.cur = get_new_source(ss, &new_slides, start_idx);
  319. idx = start_idx;
  320. for (size_t i = 0; i < SLIDE_BUFFER_COUNT; i++) {
  321. idx = get_new_file(ssd, idx, true);
  322. sd = get_new_source(ss, &new_slides, idx);
  323. deque_push_back(&new_slides.next, &sd, sizeof(sd));
  324. }
  325. idx = start_idx;
  326. for (size_t i = 0; i < SLIDE_BUFFER_COUNT; i++) {
  327. idx = get_new_file(ssd, idx, false);
  328. sd = get_new_source(ss, &new_slides, idx);
  329. deque_push_front(&new_slides.prev, &sd, sizeof(sd));
  330. }
  331. }
  332. free_active_slides(&ssd->slides);
  333. ssd->slides = new_slides;
  334. }
  335. static void ss_update(void *data, obs_data_t *settings)
  336. {
  337. struct slideshow *ss = data;
  338. struct slideshow_data new_data = {0};
  339. struct slideshow_data old_data = ss->data;
  340. obs_data_array_t *array;
  341. obs_source_t *new_tr = NULL;
  342. obs_source_t *old_tr = NULL;
  343. uint32_t new_duration;
  344. uint32_t cx = 0;
  345. uint32_t cy = 0;
  346. size_t count;
  347. const char *behavior;
  348. const char *tr_name;
  349. const char *mode;
  350. /* ------------------------------------- */
  351. /* get settings data */
  352. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  353. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0)
  354. new_data.behavior = BEHAVIOR_PAUSE_UNPAUSE;
  355. else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0)
  356. new_data.behavior = BEHAVIOR_ALWAYS_PLAY;
  357. else /* S_BEHAVIOR_STOP_RESTART */
  358. new_data.behavior = BEHAVIOR_STOP_RESTART;
  359. mode = obs_data_get_string(settings, S_MODE);
  360. new_data.manual = (astrcmpi(mode, S_MODE_MANUAL) == 0);
  361. tr_name = obs_data_get_string(settings, S_TRANSITION);
  362. if (astrcmpi(tr_name, TR_CUT) == 0)
  363. tr_name = "cut_transition";
  364. else if (astrcmpi(tr_name, TR_SWIPE) == 0)
  365. tr_name = "swipe_transition";
  366. else if (astrcmpi(tr_name, TR_SLIDE) == 0)
  367. tr_name = "slide_transition";
  368. else
  369. tr_name = "fade_transition";
  370. new_data.randomize = obs_data_get_bool(settings, S_RANDOMIZE);
  371. new_data.loop = obs_data_get_bool(settings, S_LOOP);
  372. new_data.hide = obs_data_get_bool(settings, S_HIDE);
  373. if (!old_data.tr_name || strcmp(tr_name, old_data.tr_name) != 0)
  374. new_tr = obs_source_create_private(tr_name, NULL, NULL);
  375. new_duration = (uint32_t)obs_data_get_int(settings, S_SLIDE_TIME);
  376. new_data.tr_speed = (uint32_t)obs_data_get_int(settings, S_TR_SPEED);
  377. array = obs_data_get_array(settings, S_FILES);
  378. count = obs_data_array_count(array);
  379. if (strcmp(tr_name, "cut_transition") != 0) {
  380. if (new_duration < 100)
  381. new_duration = 100;
  382. new_duration += new_data.tr_speed;
  383. } else {
  384. if (new_duration < 50)
  385. new_duration = 50;
  386. }
  387. new_data.slide_time = (float)new_duration / 1000.0f;
  388. /* ------------------------------------- */
  389. /* create new list of sources */
  390. for (size_t i = 0; i < count; i++) {
  391. obs_data_t *item = obs_data_array_item(array, i);
  392. const char *path = obs_data_get_string(item, "value");
  393. os_dir_t *dir = os_opendir(path);
  394. if (!path || !*path) {
  395. obs_data_release(item);
  396. continue;
  397. }
  398. if (dir) {
  399. struct dstr dir_path = {0};
  400. struct os_dirent *ent;
  401. for (;;) {
  402. const char *ext;
  403. ent = os_readdir(dir);
  404. if (!ent)
  405. break;
  406. if (ent->directory)
  407. continue;
  408. ext = os_get_path_extension(ent->d_name);
  409. if (!valid_extension(ext))
  410. continue;
  411. dstr_copy(&dir_path, path);
  412. dstr_cat_ch(&dir_path, '/');
  413. dstr_cat(&dir_path, ent->d_name);
  414. add_file(&new_data.files, dir_path.array);
  415. }
  416. dstr_free(&dir_path);
  417. os_closedir(dir);
  418. } else {
  419. add_file(&new_data.files, path);
  420. }
  421. obs_data_release(item);
  422. }
  423. /* ------------------------------------- */
  424. /* get new size */
  425. const char *res_str = obs_data_get_string(settings, S_CUSTOM_SIZE);
  426. bool valid = false;
  427. int ret = sscanf(res_str, "%" PRIu32 "x%" PRIu32, &cx, &cy);
  428. if (ret == 2) {
  429. valid = true;
  430. } else {
  431. cx = 0;
  432. cy = 0;
  433. }
  434. /* ------------------------------------- */
  435. /* update settings data */
  436. ss->data = new_data;
  437. if (new_tr) {
  438. old_tr = ss->transition;
  439. ss->transition = new_tr;
  440. }
  441. /* ------------------------------------- */
  442. /* clean up */
  443. if (old_tr)
  444. obs_source_release(old_tr);
  445. free_slideshow_data(&old_data);
  446. /* ------------------------------------- */
  447. /* update files */
  448. restart_slides(ss);
  449. /* ------------------------------------- */
  450. /* restart transition */
  451. ss->cx = cx;
  452. ss->cy = cy;
  453. obs_transition_set_size(ss->transition, cx, cy);
  454. obs_transition_set_alignment(ss->transition, OBS_ALIGN_CENTER);
  455. obs_transition_set_scale_type(ss->transition,
  456. OBS_TRANSITION_SCALE_ASPECT);
  457. if (new_tr)
  458. obs_source_add_active_child(ss->source, new_tr);
  459. if (ss->data.files.num) {
  460. do_transition(ss, false);
  461. if (ss->data.manual)
  462. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  463. else
  464. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  465. obs_source_media_started(ss->source);
  466. }
  467. obs_data_array_release(array);
  468. }
  469. static void ss_play_pause(void *data, bool pause)
  470. {
  471. struct slideshow *ss = data;
  472. if (ss->data.stop) {
  473. ss->data.stop = false;
  474. ss->data.paused = false;
  475. do_transition(ss, false);
  476. } else {
  477. ss->data.paused = pause;
  478. ss->data.manual = pause;
  479. }
  480. if (pause)
  481. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  482. else
  483. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  484. }
  485. static void ss_restart(void *data)
  486. {
  487. struct slideshow *ss = data;
  488. restart_slides(ss);
  489. ss->data.elapsed = 0.0f;
  490. ss->data.stop = false;
  491. ss->data.paused = false;
  492. do_transition(ss, false);
  493. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  494. }
  495. static void ss_stop(void *data)
  496. {
  497. struct slideshow *ss = data;
  498. restart_slides(ss);
  499. ss->data.elapsed = 0.0f;
  500. ss->data.stop = true;
  501. ss->data.paused = false;
  502. do_transition(ss, true);
  503. set_media_state(ss, OBS_MEDIA_STATE_STOPPED);
  504. }
  505. static void ss_next_slide(void *data)
  506. {
  507. struct slideshow *ss = data;
  508. struct slideshow_data *ssd = &ss->data;
  509. struct active_slides *slides = &ssd->slides;
  510. struct source_data sd;
  511. if (!ssd->files.num || obs_transition_get_time(ss->transition) < 1.0f)
  512. return;
  513. struct source_data *last = deque_data(
  514. &slides->next, (SLIDE_BUFFER_COUNT - 1) * sizeof(sd));
  515. size_t slide_idx = last->slide_idx;
  516. if (ss->data.randomize)
  517. slide_idx = random_file(&ss->data, slide_idx);
  518. else if (++slide_idx >= ssd->files.num)
  519. slide_idx = 0;
  520. sd = get_new_source(ss, NULL, slide_idx);
  521. deque_push_back(&slides->next, &sd, sizeof(sd));
  522. deque_push_back(&slides->prev, &slides->cur, sizeof(sd));
  523. deque_pop_front(&slides->next, &slides->cur, sizeof(sd));
  524. deque_pop_front(&slides->prev, &sd, sizeof(sd));
  525. free_source_data(&sd);
  526. do_transition(ss, false);
  527. }
  528. static void ss_previous_slide(void *data)
  529. {
  530. struct slideshow *ss = data;
  531. struct slideshow_data *ssd = &ss->data;
  532. struct active_slides *slides = &ssd->slides;
  533. struct source_data sd;
  534. if (!ssd->files.num || obs_transition_get_time(ss->transition) < 1.0f)
  535. return;
  536. struct source_data *first = deque_data(&slides->prev, 0);
  537. size_t slide_idx = first->slide_idx;
  538. if (ss->data.randomize)
  539. slide_idx = random_file(&ss->data, slide_idx);
  540. else if (slide_idx == 0)
  541. slide_idx = ss->data.files.num - 1;
  542. else
  543. --slide_idx;
  544. sd = get_new_source(ss, NULL, slide_idx);
  545. deque_push_front(&slides->prev, &sd, sizeof(sd));
  546. deque_push_front(&slides->next, &slides->cur, sizeof(sd));
  547. deque_pop_back(&slides->prev, &slides->cur, sizeof(sd));
  548. deque_pop_back(&slides->next, &sd, sizeof(sd));
  549. free_source_data(&sd);
  550. do_transition(ss, false);
  551. }
  552. static void play_pause_hotkey(void *data, obs_hotkey_id id,
  553. obs_hotkey_t *hotkey, bool pressed)
  554. {
  555. UNUSED_PARAMETER(id);
  556. UNUSED_PARAMETER(hotkey);
  557. struct slideshow *ss = data;
  558. if (pressed && obs_source_showing(ss->source))
  559. obs_source_media_play_pause(ss->source, !ss->data.paused);
  560. }
  561. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  562. bool pressed)
  563. {
  564. UNUSED_PARAMETER(id);
  565. UNUSED_PARAMETER(hotkey);
  566. struct slideshow *ss = data;
  567. if (pressed && obs_source_showing(ss->source))
  568. obs_source_media_restart(ss->source);
  569. }
  570. static void stop_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  571. bool pressed)
  572. {
  573. UNUSED_PARAMETER(id);
  574. UNUSED_PARAMETER(hotkey);
  575. struct slideshow *ss = data;
  576. if (pressed && obs_source_showing(ss->source))
  577. obs_source_media_stop(ss->source);
  578. }
  579. static void next_slide_hotkey(void *data, obs_hotkey_id id,
  580. obs_hotkey_t *hotkey, bool pressed)
  581. {
  582. UNUSED_PARAMETER(id);
  583. UNUSED_PARAMETER(hotkey);
  584. struct slideshow *ss = data;
  585. if (!ss->data.manual)
  586. return;
  587. if (pressed && obs_source_showing(ss->source))
  588. obs_source_media_next(ss->source);
  589. }
  590. static void previous_slide_hotkey(void *data, obs_hotkey_id id,
  591. obs_hotkey_t *hotkey, bool pressed)
  592. {
  593. UNUSED_PARAMETER(id);
  594. UNUSED_PARAMETER(hotkey);
  595. struct slideshow *ss = data;
  596. if (!ss->data.manual)
  597. return;
  598. if (pressed && obs_source_showing(ss->source))
  599. obs_source_media_previous(ss->source);
  600. }
  601. static void current_slide_proc(void *data, calldata_t *cd)
  602. {
  603. struct slideshow *ss = data;
  604. calldata_set_int(cd, "current_index", ss->data.slides.cur.slide_idx);
  605. }
  606. static void total_slides_proc(void *data, calldata_t *cd)
  607. {
  608. struct slideshow *ss = data;
  609. calldata_set_int(cd, "total_files", ss->data.files.num);
  610. }
  611. static void ss_destroy(void *data)
  612. {
  613. struct slideshow *ss = data;
  614. os_task_queue_destroy(ss->queue);
  615. obs_source_release(ss->transition);
  616. free_slideshow_data(&ss->data);
  617. bfree(ss);
  618. }
  619. static void *ss_create(obs_data_t *settings, obs_source_t *source)
  620. {
  621. struct slideshow *ss = bzalloc(sizeof(*ss));
  622. proc_handler_t *ph = obs_source_get_proc_handler(source);
  623. ss->source = source;
  624. ss->data.manual = false;
  625. ss->data.paused = false;
  626. ss->data.stop = false;
  627. ss->queue = os_task_queue_create();
  628. ss->play_pause_hotkey = obs_hotkey_register_source(
  629. source, "SlideShow.PlayPause",
  630. obs_module_text("SlideShow.PlayPause"), play_pause_hotkey, ss);
  631. ss->restart_hotkey = obs_hotkey_register_source(
  632. source, "SlideShow.Restart",
  633. obs_module_text("SlideShow.Restart"), restart_hotkey, ss);
  634. ss->stop_hotkey = obs_hotkey_register_source(
  635. source, "SlideShow.Stop", obs_module_text("SlideShow.Stop"),
  636. stop_hotkey, ss);
  637. ss->next_hotkey = obs_hotkey_register_source(
  638. source, "SlideShow.NextSlide",
  639. obs_module_text("SlideShow.NextSlide"), next_slide_hotkey, ss);
  640. ss->prev_hotkey = obs_hotkey_register_source(
  641. source, "SlideShow.PreviousSlide",
  642. obs_module_text("SlideShow.PreviousSlide"),
  643. previous_slide_hotkey, ss);
  644. proc_handler_add(ph, "void current_index(out int current_index)",
  645. current_slide_proc, ss);
  646. proc_handler_add(ph, "void total_files(out int total_files)",
  647. total_slides_proc, ss);
  648. signal_handler_t *sh = obs_source_get_signal_handler(ss->source);
  649. signal_handler_add(sh, "void slide_changed(int index, string path)");
  650. obs_source_update(source, NULL);
  651. UNUSED_PARAMETER(settings);
  652. return ss;
  653. }
  654. static void ss_video_render(void *data, gs_effect_t *effect)
  655. {
  656. struct slideshow *ss = data;
  657. obs_source_t *transition = get_transition(ss);
  658. if (transition) {
  659. obs_source_video_render(transition);
  660. obs_source_release(transition);
  661. }
  662. UNUSED_PARAMETER(effect);
  663. }
  664. static void ss_video_tick(void *data, float seconds)
  665. {
  666. struct slideshow *ss = data;
  667. struct slideshow_data *ssd = &ss->data;
  668. if (!ss->transition || !ssd->slide_time)
  669. return;
  670. if (ssd->restart_on_activate && ssd->use_cut) {
  671. ssd->elapsed = 0.0f;
  672. restart_slides(ss);
  673. do_transition(ss, false);
  674. ssd->restart_on_activate = false;
  675. ssd->use_cut = false;
  676. ssd->stop = false;
  677. return;
  678. }
  679. if (ssd->pause_on_deactivate || ssd->manual || ssd->stop || ssd->paused)
  680. return;
  681. /* ----------------------------------------------------- */
  682. /* fade to transparency when the file list becomes empty */
  683. if (!ssd->files.num) {
  684. obs_source_t *active_transition_source =
  685. obs_transition_get_active_source(ss->transition);
  686. if (active_transition_source) {
  687. obs_source_release(active_transition_source);
  688. do_transition(ss, true);
  689. }
  690. }
  691. /* ----------------------------------------------------- */
  692. /* do transition when slide time reached */
  693. ssd->elapsed += seconds;
  694. if (ssd->elapsed > ssd->slide_time) {
  695. ssd->elapsed -= ssd->slide_time;
  696. if (!ssd->loop &&
  697. ssd->slides.cur.slide_idx == ssd->files.num - 1) {
  698. if (ssd->hide)
  699. do_transition(ss, true);
  700. else
  701. do_transition(ss, false);
  702. return;
  703. }
  704. obs_source_media_next(ss->source);
  705. }
  706. }
  707. static inline bool ss_audio_render_(obs_source_t *transition, uint64_t *ts_out,
  708. struct obs_source_audio_mix *audio_output,
  709. uint32_t mixers, size_t channels,
  710. size_t sample_rate)
  711. {
  712. struct obs_source_audio_mix child_audio;
  713. uint64_t source_ts;
  714. if (obs_source_audio_pending(transition))
  715. return false;
  716. source_ts = obs_source_get_audio_timestamp(transition);
  717. if (!source_ts)
  718. return false;
  719. obs_source_get_audio_mix(transition, &child_audio);
  720. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  721. if ((mixers & (1 << mix)) == 0)
  722. continue;
  723. for (size_t ch = 0; ch < channels; ch++) {
  724. float *out = audio_output->output[mix].data[ch];
  725. float *in = child_audio.output[mix].data[ch];
  726. memcpy(out, in, AUDIO_OUTPUT_FRAMES * sizeof(float));
  727. }
  728. }
  729. *ts_out = source_ts;
  730. UNUSED_PARAMETER(sample_rate);
  731. return true;
  732. }
  733. static bool ss_audio_render(void *data, uint64_t *ts_out,
  734. struct obs_source_audio_mix *audio_output,
  735. uint32_t mixers, size_t channels,
  736. size_t sample_rate)
  737. {
  738. struct slideshow *ss = data;
  739. obs_source_t *transition = get_transition(ss);
  740. bool success;
  741. if (!transition)
  742. return false;
  743. success = ss_audio_render_(transition, ts_out, audio_output, mixers,
  744. channels, sample_rate);
  745. obs_source_release(transition);
  746. return success;
  747. }
  748. static void ss_enum_sources(void *data, obs_source_enum_proc_t cb, void *param)
  749. {
  750. struct slideshow *ss = data;
  751. if (ss->transition)
  752. cb(ss->source, ss->transition, param);
  753. }
  754. static uint32_t ss_width(void *data)
  755. {
  756. struct slideshow *ss = data;
  757. return ss->transition ? ss->cx : 0;
  758. }
  759. static uint32_t ss_height(void *data)
  760. {
  761. struct slideshow *ss = data;
  762. return ss->transition ? ss->cy : 0;
  763. }
  764. static void ss_defaults(obs_data_t *settings)
  765. {
  766. obs_data_set_default_string(settings, S_TRANSITION, "fade");
  767. obs_data_set_default_int(settings, S_SLIDE_TIME, 2000); //8000);
  768. obs_data_set_default_int(settings, S_TR_SPEED, 700);
  769. obs_data_set_default_string(settings, S_CUSTOM_SIZE, "1920x1080");
  770. obs_data_set_default_string(settings, S_BEHAVIOR,
  771. S_BEHAVIOR_ALWAYS_PLAY);
  772. obs_data_set_default_string(settings, S_MODE, S_MODE_AUTO);
  773. obs_data_set_default_bool(settings, S_LOOP, true);
  774. }
  775. static const char *file_filter = "Image files (*.bmp *.tga *.png *.jpeg *.jpg"
  776. #ifdef _WIN32
  777. " *.jxr"
  778. #endif
  779. " *.gif *.webp)";
  780. static obs_properties_t *ss_properties(void *data)
  781. {
  782. obs_properties_t *ppts = obs_properties_create();
  783. struct slideshow *ss = data;
  784. struct obs_video_info ovi;
  785. struct dstr path = {0};
  786. obs_property_t *p;
  787. int cx;
  788. int cy;
  789. /* ----------------- */
  790. obs_get_video_info(&ovi);
  791. cx = (int)ovi.base_width;
  792. cy = (int)ovi.base_height;
  793. /* ----------------- */
  794. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  795. OBS_COMBO_TYPE_LIST,
  796. OBS_COMBO_FORMAT_STRING);
  797. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  798. S_BEHAVIOR_ALWAYS_PLAY);
  799. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  800. S_BEHAVIOR_STOP_RESTART);
  801. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  802. S_BEHAVIOR_PAUSE_UNPAUSE);
  803. p = obs_properties_add_list(ppts, S_MODE, T_MODE, OBS_COMBO_TYPE_LIST,
  804. OBS_COMBO_FORMAT_STRING);
  805. obs_property_list_add_string(p, T_MODE_AUTO, S_MODE_AUTO);
  806. obs_property_list_add_string(p, T_MODE_MANUAL, S_MODE_MANUAL);
  807. p = obs_properties_add_list(ppts, S_TRANSITION, T_TRANSITION,
  808. OBS_COMBO_TYPE_LIST,
  809. OBS_COMBO_FORMAT_STRING);
  810. obs_property_list_add_string(p, T_TR_CUT, TR_CUT);
  811. obs_property_list_add_string(p, T_TR_FADE, TR_FADE);
  812. obs_property_list_add_string(p, T_TR_SWIPE, TR_SWIPE);
  813. obs_property_list_add_string(p, T_TR_SLIDE, TR_SLIDE);
  814. p = obs_properties_add_int(ppts, S_SLIDE_TIME, T_SLIDE_TIME, 50,
  815. 3600000, 50);
  816. obs_property_int_set_suffix(p, " ms");
  817. p = obs_properties_add_int(ppts, S_TR_SPEED, T_TR_SPEED, 0, 3600000,
  818. 50);
  819. obs_property_int_set_suffix(p, " ms");
  820. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  821. obs_properties_add_bool(ppts, S_HIDE, T_HIDE);
  822. obs_properties_add_bool(ppts, S_RANDOMIZE, T_RANDOMIZE);
  823. p = obs_properties_add_list(ppts, S_CUSTOM_SIZE, T_CUSTOM_SIZE,
  824. OBS_COMBO_TYPE_EDITABLE,
  825. OBS_COMBO_FORMAT_STRING);
  826. char str[32];
  827. snprintf(str, sizeof(str), "%dx%d", cx, cy);
  828. obs_property_list_add_string(p, str, str);
  829. if (ss) {
  830. if (ss->data.files.num) {
  831. struct image_file_data *last = da_end(ss->data.files);
  832. const char *slash;
  833. dstr_copy(&path, last->path);
  834. dstr_replace(&path, "\\", "/");
  835. slash = strrchr(path.array, '/');
  836. if (slash)
  837. dstr_resize(&path, slash - path.array + 1);
  838. }
  839. }
  840. obs_properties_add_editable_list(ppts, S_FILES, T_FILES,
  841. OBS_EDITABLE_LIST_TYPE_FILES,
  842. file_filter, path.array);
  843. dstr_free(&path);
  844. return ppts;
  845. }
  846. static void ss_activate(void *data)
  847. {
  848. struct slideshow *ss = data;
  849. if (ss->data.behavior == BEHAVIOR_STOP_RESTART) {
  850. ss->data.restart_on_activate = true;
  851. ss->data.use_cut = true;
  852. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  853. } else if (ss->data.behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  854. ss->data.pause_on_deactivate = false;
  855. set_media_state(ss, OBS_MEDIA_STATE_PLAYING);
  856. }
  857. }
  858. static void ss_deactivate(void *data)
  859. {
  860. struct slideshow *ss = data;
  861. if (ss->data.behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  862. ss->data.pause_on_deactivate = true;
  863. set_media_state(ss, OBS_MEDIA_STATE_PAUSED);
  864. }
  865. }
  866. static void missing_file_callback(void *src, const char *new_path, void *data)
  867. {
  868. struct slideshow *s = src;
  869. const char *orig_path = data;
  870. obs_source_t *source = s->source;
  871. obs_data_t *settings = obs_source_get_settings(source);
  872. obs_data_array_t *files = obs_data_get_array(settings, S_FILES);
  873. size_t l = obs_data_array_count(files);
  874. for (size_t i = 0; i < l; i++) {
  875. obs_data_t *file = obs_data_array_item(files, i);
  876. const char *path = obs_data_get_string(file, "value");
  877. if (strcmp(path, orig_path) == 0) {
  878. if (new_path && *new_path)
  879. obs_data_set_string(file, "value", new_path);
  880. else
  881. obs_data_array_erase(files, i);
  882. obs_data_release(file);
  883. break;
  884. }
  885. obs_data_release(file);
  886. }
  887. obs_source_update(source, settings);
  888. obs_data_array_release(files);
  889. obs_data_release(settings);
  890. }
  891. static obs_missing_files_t *ss_missingfiles(void *data)
  892. {
  893. struct slideshow *s = data;
  894. obs_missing_files_t *missing_files = obs_missing_files_create();
  895. obs_source_t *source = s->source;
  896. obs_data_t *settings = obs_source_get_settings(source);
  897. obs_data_array_t *files = obs_data_get_array(settings, S_FILES);
  898. size_t l = obs_data_array_count(files);
  899. for (size_t i = 0; i < l; i++) {
  900. obs_data_t *item = obs_data_array_item(files, i);
  901. const char *path = obs_data_get_string(item, "value");
  902. if (*path != 0) {
  903. if (!os_file_exists(path)) {
  904. obs_missing_file_t *file =
  905. obs_missing_file_create(
  906. path, missing_file_callback,
  907. OBS_MISSING_FILE_SOURCE, source,
  908. (void *)path);
  909. obs_missing_files_add_file(missing_files, file);
  910. }
  911. }
  912. obs_data_release(item);
  913. }
  914. obs_data_array_release(files);
  915. obs_data_release(settings);
  916. return missing_files;
  917. }
  918. static enum gs_color_space
  919. ss_video_get_color_space(void *data, size_t count,
  920. const enum gs_color_space *preferred_spaces)
  921. {
  922. enum gs_color_space space = GS_CS_SRGB;
  923. struct slideshow *ss = data;
  924. obs_source_t *transition = get_transition(ss);
  925. if (transition) {
  926. space = obs_source_get_color_space(transition, count,
  927. preferred_spaces);
  928. obs_source_release(transition);
  929. }
  930. return space;
  931. }
  932. struct obs_source_info slideshow_info_mk2 = {
  933. .id = "slideshow",
  934. .version = 2,
  935. .type = OBS_SOURCE_TYPE_INPUT,
  936. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  937. OBS_SOURCE_COMPOSITE | OBS_SOURCE_CONTROLLABLE_MEDIA,
  938. .get_name = ss_getname,
  939. .create = ss_create,
  940. .destroy = ss_destroy,
  941. .update = ss_update,
  942. .activate = ss_activate,
  943. .deactivate = ss_deactivate,
  944. .video_render = ss_video_render,
  945. .video_tick = ss_video_tick,
  946. .audio_render = ss_audio_render,
  947. .enum_active_sources = ss_enum_sources,
  948. .get_width = ss_width,
  949. .get_height = ss_height,
  950. .get_defaults = ss_defaults,
  951. .get_properties = ss_properties,
  952. .missing_files = ss_missingfiles,
  953. .icon_type = OBS_ICON_TYPE_SLIDESHOW,
  954. .media_play_pause = ss_play_pause,
  955. .media_restart = ss_restart,
  956. .media_stop = ss_stop,
  957. .media_next = ss_next_slide,
  958. .media_previous = ss_previous_slide,
  959. .media_get_state = ss_get_state,
  960. .video_get_color_space = ss_video_get_color_space,
  961. };