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