obs-slideshow-mk2.c 32 KB

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