-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
replay-source.c
3177 lines (2842 loc) · 110 KB
/
replay-source.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 <graphics/image-file.h>
#include <util/platform.h>
#include <util/dstr.h>
#include <util/threading.h>
#include <media-io/video-io.h>
#include <media-io/video-frame.h>
#include <media-io/video-scaler.h>
#include <obs-frontend-api.h>
#include "replay.h"
#include <inttypes.h>
#include <string.h>
#define VISIBILITY_ACTION_RESTART 0
#define VISIBILITY_ACTION_PAUSE 1
#define VISIBILITY_ACTION_CONTINUE 2
#define VISIBILITY_ACTION_NONE 3
#define END_ACTION_HIDE 0
#define END_ACTION_PAUSE 1
#define END_ACTION_LOOP 2
#define END_ACTION_REVERSE 3
#define END_ACTION_HIDE_ALL 4
#define END_ACTION_PAUSE_ALL 5
#define END_ACTION_LOOP_ALL 6
#define END_ACTION_REVERSE_ALL 7
enum saving_status {
SAVING_STATUS_NONE = 0,
SAVING_STATUS_STARTING = 1,
SAVING_STATUS_STARTING2 = 2,
SAVING_STATUS_SAVING = 3,
SAVING_STATUS_STOPPING = 4
};
struct replay {
struct obs_source_frame **video_frames;
uint64_t video_frame_count;
struct obs_audio_data *audio_frames;
struct audio_convert_info oai;
uint64_t audio_frame_count;
uint64_t first_frame_timestamp;
uint64_t last_frame_timestamp;
uint64_t duration;
int64_t trim_front;
int64_t trim_end;
};
struct replay_source {
obs_source_t *source;
obs_source_t *source_filter;
obs_source_t *source_audio_filter;
char *source_name;
char *source_audio_name;
float speed_percent;
bool backward;
bool backward_start;
int visibility_action;
int end_action;
char *next_scene_name;
bool next_scene_disabled;
char *load_switch_scene_name;
obs_hotkey_id replay_hotkey;
obs_hotkey_id next_hotkey;
obs_hotkey_id previous_hotkey;
obs_hotkey_id first_hotkey;
obs_hotkey_id last_hotkey;
obs_hotkey_id remove_hotkey;
obs_hotkey_id clear_hotkey;
obs_hotkey_id restart_hotkey;
obs_hotkey_id pause_hotkey;
obs_hotkey_id faster_hotkey;
obs_hotkey_id slower_hotkey;
obs_hotkey_id faster_by_5_hotkey;
obs_hotkey_id slower_by_5_hotkey;
obs_hotkey_id normal_or_faster_hotkey;
obs_hotkey_id normal_or_slower_hotkey;
obs_hotkey_id normal_speed_hotkey;
obs_hotkey_id half_speed_hotkey;
obs_hotkey_id double_speed_hotkey;
obs_hotkey_id trim_front_hotkey;
obs_hotkey_id trim_end_hotkey;
obs_hotkey_id trim_reset_hotkey;
obs_hotkey_id reverse_hotkey;
obs_hotkey_id forward_hotkey;
obs_hotkey_id backward_hotkey;
obs_hotkey_id forward_or_faster_hotkey;
obs_hotkey_id backward_or_faster_hotkey;
obs_hotkey_id save_hotkey;
obs_hotkey_id enable_hotkey;
obs_hotkey_id disable_hotkey;
obs_hotkey_id enable_next_scene_hotkey;
obs_hotkey_id disable_next_scene_hotkey;
obs_hotkey_id next_scene_current_hotkey;
obs_hotkey_id next_scene_hotkey;
obs_hotkey_id next_frame_hotkey;
obs_hotkey_id prev_frame_hotkey;
obs_hotkey_id next_n_frames_hotkey;
obs_hotkey_id prev_n_frames_hotkey;
uint64_t start_timestamp;
uint64_t previous_frame_timestamp;
uint64_t pause_timestamp;
int64_t start_delay;
int64_t retrieve_delay;
int64_t frame_step_count;
uint64_t retrieve_timestamp;
uint64_t threshold_timestamp;
struct obs_source_audio audio;
bool disabled;
bool play;
bool restart;
bool active;
bool end;
bool stepped;
enum saving_status saving_status;
int replay_position;
int replay_max;
#if LIBOBS_API_VER >= MAKE_SEMANTIC_VERSION(30, 1, 0)
struct deque replays;
#else
struct circlebuf replays;
#endif
struct replay current_replay;
struct replay saving_replay;
uint64_t video_frame_position;
uint64_t video_save_position;
/* stores the audio data */
uint64_t audio_frame_position;
struct obs_audio_data audio_output;
pthread_mutex_t video_mutex;
pthread_mutex_t audio_mutex;
pthread_mutex_t replay_mutex;
uint32_t known_width;
uint32_t known_height;
video_t *video_output;
obs_output_t *fileOutput;
obs_encoder_t *h264Recording;
audio_t *audio_t;
video_scaler_t *scaler;
bool lossless;
char *file_format;
char *directory;
uint64_t start_save_timestamp;
obs_encoder_t *aac;
char *progress_source_name;
char *text_source_name;
char *text_format;
bool sound_trigger;
bool filter_loaded;
bool free_after_save;
};
static void replace_text(struct dstr *str, size_t pos, size_t len, const char *new_text)
{
struct dstr front = {0};
struct dstr back = {0};
dstr_left(&front, str, pos);
dstr_right(&back, str, pos + len);
dstr_copy_dstr(str, &front);
dstr_cat(str, new_text);
dstr_cat_dstr(str, &back);
dstr_free(&front);
dstr_free(&back);
}
static void replay_update_text(struct replay_source *c)
{
if (!c->text_source_name || !c->text_format)
return;
obs_source_t *s = obs_get_source_by_name(c->text_source_name);
if (!s)
return;
struct dstr sf;
size_t pos = 0;
struct dstr buffer;
dstr_init(&buffer);
dstr_init_copy(&sf, c->text_format);
while (pos < sf.len) {
//duration, speed, index, count
const char *cmp = sf.array + pos;
if (astrcmp_n(cmp, "%SPEED%", 7) == 0) {
dstr_printf(&buffer, "%.1f", c->speed_percent * (c->backward ? -1.0f : 1.0f));
dstr_cat_ch(&buffer, '%');
replace_text(&sf, pos, 7, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%PROGRESS%", 10) == 0) {
if (c->current_replay.video_frame_count && c->video_frame_position < c->current_replay.video_frame_count) {
dstr_printf(&buffer, "%.1f", c->video_frame_position * 100.0 / c->current_replay.video_frame_count);
dstr_cat_ch(&buffer, '%');
} else {
dstr_copy(&buffer, "");
}
replace_text(&sf, pos, 10, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%COUNT%", 7) == 0) {
dstr_printf(&buffer, "%d", (int)(c->replays.size / sizeof c->current_replay));
replace_text(&sf, pos, 7, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%INDEX%", 7) == 0) {
if (c->replays.size) {
dstr_printf(&buffer, "%d", c->replay_position + 1);
} else {
dstr_copy(&buffer, "0");
}
replace_text(&sf, pos, 7, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%DURATION%", 10) == 0) {
if (c->replays.size) {
dstr_printf(&buffer, "%.2f", (double)c->current_replay.duration / (double)1000000000.0);
} else {
dstr_copy(&buffer, "");
}
replace_text(&sf, pos, 10, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%TIME%", 6) == 0) {
if (c->replays.size && c->start_timestamp) {
int64_t time = 0;
if (c->pause_timestamp > c->start_timestamp) {
time = c->pause_timestamp - c->start_timestamp;
} else {
time = obs_get_video_frame_time() - c->start_timestamp;
}
if (c->speed_percent != 100.0f) {
time = (int64_t)((float)time * c->speed_percent / 100.0f);
}
dstr_printf(&buffer, "%.2f", (double)time / (double)1000000000.0);
} else {
dstr_copy(&buffer, "");
}
replace_text(&sf, pos, 6, buffer.array);
pos += buffer.len;
} else if (astrcmp_n(cmp, "%FPS%", 5) == 0) {
if (c->current_replay.video_frame_count && c->current_replay.duration) {
dstr_printf(&buffer, "%d",
(int)(c->current_replay.video_frame_count * 1000000000U / c->current_replay.duration));
} else {
dstr_copy(&buffer, "0");
}
replace_text(&sf, pos, 5, buffer.array);
pos += buffer.len;
} else {
pos++;
}
}
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "text", sf.array);
obs_source_update(s, settings);
obs_data_release(settings);
dstr_free(&sf);
dstr_free(&buffer);
obs_source_release(s);
}
struct siu {
uint32_t crop_width;
obs_source_t *source;
};
static bool EnumSceneItem(obs_scene_t *scene, obs_sceneitem_t *item, void *data)
{
UNUSED_PARAMETER(scene);
struct siu *siu = data;
if (obs_sceneitem_get_source(item) == siu->source) {
struct obs_sceneitem_crop crop;
obs_sceneitem_get_crop(item, &crop);
crop.left = 0;
crop.right = siu->crop_width;
obs_sceneitem_set_crop(item, &crop);
} else if (obs_sceneitem_is_group(item)) {
obs_scene_enum_items(obs_sceneitem_group_get_scene(item), EnumSceneItem, data);
}
return true;
}
static bool EnumScenesItems(void *data, obs_source_t *source)
{
obs_scene_t *scene = obs_scene_from_source(source);
obs_scene_enum_items(scene, EnumSceneItem, data);
return true;
}
static void replay_update_progress_crop(struct replay_source *context, uint64_t t)
{
if (!context->progress_source_name)
return;
obs_source_t *s = obs_get_source_by_name(context->progress_source_name);
if (!s)
return;
const uint32_t width = obs_source_get_base_width(s);
if (width) {
struct siu siu;
siu.source = s;
if (t && context->current_replay.last_frame_timestamp && context->current_replay.duration) {
siu.crop_width = (uint32_t)((context->current_replay.last_frame_timestamp - t) * width /
context->current_replay.duration);
} else {
siu.crop_width = width;
}
obs_enum_scenes(EnumScenesItems, &siu);
}
obs_source_release(s);
}
static const char *replay_source_get_name(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("ReplaySource");
}
static void EnumFilter(obs_source_t *source, obs_source_t *filter, void *data)
{
UNUSED_PARAMETER(source);
struct replay_source *c = data;
const char *filterName = obs_source_get_name(filter);
const char *sourceName = obs_source_get_name(c->source);
const char *id = obs_source_get_unversioned_id(filter);
if ((strcmp(REPLAY_FILTER_ASYNC_ID, id) == 0 || strcmp(REPLAY_FILTER_ID, id) == 0) && strcmp(filterName, sourceName) == 0)
c->source_filter = filter;
}
static void EnumAudioFilter(obs_source_t *source, obs_source_t *filter, void *data)
{
UNUSED_PARAMETER(source);
struct replay_source *c = data;
const char *filterName = obs_source_get_name(filter);
const char *sourceName = obs_source_get_name(c->source);
const char *id = obs_source_get_unversioned_id(filter);
if (strcmp(REPLAY_FILTER_AUDIO_ID, id) == 0 && strcmp(filterName, sourceName) == 0)
c->source_audio_filter = filter;
}
static void EnumAudioVideoFilter(obs_source_t *source, obs_source_t *filter, void *data)
{
UNUSED_PARAMETER(source);
struct replay_source *c = data;
const char *filterName = obs_source_get_name(filter);
const char *sourceName = obs_source_get_name(c->source);
const char *id = obs_source_get_unversioned_id(filter);
if ((strcmp(REPLAY_FILTER_AUDIO_ID, id) == 0 || strcmp(REPLAY_FILTER_ASYNC_ID, id) == 0 ||
strcmp(REPLAY_FILTER_ID, id) == 0) &&
strcmp(filterName, sourceName) == 0)
c->source_audio_filter = filter;
}
static inline void obs_source_signal(struct obs_source *source, const char *signal_source)
{
struct calldata data;
uint8_t stack[128];
calldata_init_fixed(&data, stack, sizeof(stack));
calldata_set_ptr(&data, "source", source);
if (signal_source)
signal_handler_signal(obs_source_get_signal_handler(source), signal_source, &data);
}
static void replay_reverse(struct replay_source *c, uint64_t os_timestamp)
{
c->backward = !c->backward;
if (c->end) {
c->end = false;
if (c->backward && c->current_replay.video_frame_count) {
c->video_frame_position = c->current_replay.video_frame_count - 1;
} else {
c->video_frame_position = 0;
}
}
const int64_t duration =
(int64_t)(((int64_t)c->current_replay.last_frame_timestamp - (int64_t)c->current_replay.first_frame_timestamp) *
100.0 / c->speed_percent);
int64_t play_duration = os_timestamp - c->start_timestamp;
if (play_duration > duration) {
play_duration = duration;
}
c->start_timestamp = os_timestamp - duration + play_duration;
}
static void replay_reverse_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
struct replay_source *c = data;
if (pressed) {
const int64_t time = obs_get_video_frame_time();
if (c->pause_timestamp && !c->stepped) {
c->start_timestamp += time - c->pause_timestamp;
c->pause_timestamp = 0;
}
replay_reverse(c, time);
if (!c->play && !c->stepped) {
c->play = true;
obs_source_signal(c->source, "media_play");
}
}
}
static void replay_forward_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
struct replay_source *c = data;
if (!pressed)
return;
const int64_t time = obs_get_video_frame_time();
if (c->pause_timestamp) {
c->start_timestamp += time - c->pause_timestamp;
c->pause_timestamp = 0;
}
if (!c->play) {
c->play = true;
obs_source_signal(c->source, "media_play");
}
if (c->end) {
c->end = false;
c->video_frame_position = 0;
c->start_timestamp = obs_get_video_frame_time();
c->backward = false;
} else if (c->backward) {
c->backward = false;
const int64_t duration = (int64_t)(((int64_t)c->current_replay.last_frame_timestamp -
(int64_t)c->current_replay.first_frame_timestamp) *
100.0 / c->speed_percent);
int64_t play_duration = time - c->start_timestamp;
if (play_duration > duration) {
play_duration = duration;
}
c->start_timestamp = time - duration + play_duration;
}
}
static void replay_backward_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
struct replay_source *c = data;
if (!pressed)
return;
const int64_t time = obs_get_video_frame_time();
if (c->pause_timestamp) {
c->start_timestamp += time - c->pause_timestamp;
c->pause_timestamp = 0;
}
if (!c->play) {
c->play = true;
obs_source_signal(c->source, "media_play");
}
if (c->end || c->video_frame_position == 0) {
c->end = false;
if (c->current_replay.video_frame_count)
c->video_frame_position = c->current_replay.video_frame_count - 1;
c->start_timestamp = obs_get_video_frame_time();
c->backward = true;
} else if (!c->backward) {
c->backward = true;
const int64_t duration = (int64_t)(((int64_t)c->current_replay.last_frame_timestamp -
(int64_t)c->current_replay.first_frame_timestamp) *
100.0 / c->speed_percent);
int64_t play_duration = time - c->start_timestamp;
if (play_duration > duration) {
play_duration = duration;
}
c->start_timestamp = time - duration + play_duration;
}
}
static void replay_update_position(struct replay_source *context, bool lock)
{
if (lock)
pthread_mutex_lock(&context->video_mutex);
pthread_mutex_lock(&context->audio_mutex);
const int replay_count = (int)(context->replays.size / sizeof context->current_replay);
if (replay_count == 0) {
context->current_replay.video_frame_count = 0;
context->current_replay.video_frames = NULL;
context->current_replay.audio_frame_count = 0;
context->current_replay.audio_frames = NULL;
context->replay_position = 0;
struct obs_source_frame *f = obs_source_frame_create(VIDEO_FORMAT_NONE, 0, 0);
obs_source_output_video(context->source, f);
obs_source_frame_destroy(f);
pthread_mutex_unlock(&context->audio_mutex);
if (lock)
pthread_mutex_unlock(&context->video_mutex);
blog(LOG_INFO, "[replay_source: '%s'] No replay active", obs_source_get_name(context->source));
return;
}
if (context->replay_position >= replay_count) {
context->replay_position = replay_count - 1;
} else if (context->replay_position < 0) {
context->replay_position = 0;
}
memcpy(&context->current_replay,
circlebuf_data(&context->replays, context->replay_position * sizeof context->current_replay),
sizeof context->current_replay);
context->video_frame_position = 0;
context->audio_frame_position = 0;
context->start_timestamp = obs_get_video_frame_time();
context->backward = context->backward_start;
if (!context->backward && context->current_replay.trim_front != 0) {
context->start_timestamp -= (uint64_t)(context->current_replay.trim_front * 100.0 / context->speed_percent);
} else if (context->backward && context->current_replay.trim_end != 0) {
context->start_timestamp -= (uint64_t)(context->current_replay.trim_end * 100.0 / context->speed_percent);
}
context->pause_timestamp = 0;
if (context->backward && context->current_replay.video_frame_count) {
context->video_frame_position = context->current_replay.video_frame_count - 1;
}
if (context->active || context->visibility_action == VISIBILITY_ACTION_CONTINUE) {
if (!context->play && !context->stepped) {
context->play = true;
obs_source_signal(context->source, "media_play");
}
} else {
if (context->play) {
context->play = false;
obs_source_signal(context->source, "media_pause");
}
context->pause_timestamp = obs_get_video_frame_time();
}
pthread_mutex_unlock(&context->audio_mutex);
if (lock)
pthread_mutex_unlock(&context->video_mutex);
replay_update_text(context);
}
static void replay_free_replay(struct replay *replay, struct replay_source *context)
{
if (replay == &context->saving_replay) {
context->free_after_save = false;
} else if (replay->video_frames == context->saving_replay.video_frames && context->saving_status != SAVING_STATUS_NONE) {
context->free_after_save = true;
return;
}
for (uint64_t i = 0; i < replay->video_frame_count; i++) {
struct obs_source_frame *frame = replay->video_frames[i];
if (frame && os_atomic_dec_long(&frame->refs) <= 0) {
obs_source_frame_destroy(frame);
replay->video_frames[i] = NULL;
}
}
replay->video_frame_count = 0;
if (replay->video_frames) {
bfree(replay->video_frames);
replay->video_frames = NULL;
}
for (uint64_t i = 0; i < replay->audio_frame_count; i++) {
free_audio_packet(&replay->audio_frames[i]);
}
replay->audio_frame_count = 0;
if (replay->audio_frames) {
bfree(replay->audio_frames);
replay->audio_frames = NULL;
}
}
static void replay_purge_replays(struct replay_source *context)
{
if ((int)(context->replays.size / sizeof context->current_replay) > context->replay_max) {
pthread_mutex_lock(&context->replay_mutex);
const int replays_to_delete = (int)(context->replays.size / sizeof context->current_replay - context->replay_max);
if (replays_to_delete > context->replay_position) {
context->replay_position = replays_to_delete;
replay_update_position(context, true);
}
while ((int)(context->replays.size / sizeof context->current_replay) > context->replay_max) {
struct replay old_replay;
circlebuf_pop_front(&context->replays, &old_replay, sizeof context->current_replay);
replay_free_replay(&old_replay, context);
context->replay_position--;
}
if (context->replay_max > 1)
blog(LOG_INFO, "[replay_source: '%s'] switched to replay %i/%i", obs_source_get_name(context->source),
context->replay_position + 1, (int)(context->replays.size / sizeof context->current_replay));
pthread_mutex_unlock(&context->replay_mutex);
}
}
static void replay_retrieve(struct replay_source *context);
void replay_trigger_threshold(void *data)
{
struct replay_source *context = data;
const uint64_t os_time = obs_get_video_frame_time();
uint64_t duration = context->current_replay.duration;
if (context->speed_percent < 100.f)
duration = (uint64_t)(duration * 100.0 / context->speed_percent);
if (context->threshold_timestamp && context->threshold_timestamp + context->retrieve_delay + duration > os_time)
return;
context->threshold_timestamp = os_time;
blog(LOG_INFO, "[replay_source: '%s'] audio triggered", obs_source_get_name(context->source));
if (context->retrieve_delay > 0) {
context->retrieve_timestamp = obs_get_video_frame_time() + context->retrieve_delay;
} else {
replay_retrieve(context);
}
}
static void replay_source_defaults(obs_data_t *settings)
{
obs_data_set_default_int(settings, SETTING_DURATION, 5000);
obs_data_set_default_int(settings, SETTING_REPLAYS, 1);
obs_data_set_default_int(settings, SETTING_SPEED, 100);
obs_data_set_default_int(settings, SETTING_VISIBILITY_ACTION, VISIBILITY_ACTION_CONTINUE);
obs_data_set_default_int(settings, SETTING_START_DELAY, 0);
obs_data_set_default_int(settings, SETTING_FRAME_STEP_COUNT, 5);
obs_data_set_default_int(settings, SETTING_END_ACTION, END_ACTION_LOOP);
obs_data_set_default_bool(settings, SETTING_BACKWARD, false);
obs_data_set_default_string(settings, SETTING_FILE_FORMAT, "%CCYY-%MM-%DD %hh.%mm.%ss");
obs_data_set_default_bool(settings, SETTING_LOSSLESS, false);
}
static void replay_source_show(void *data)
{
struct replay_source *context = data;
UNUSED_PARAMETER(context);
}
static void replay_source_hide(void *data)
{
struct replay_source *context = data;
UNUSED_PARAMETER(context);
}
static void replay_source_active(void *data)
{
struct replay_source *context = data;
if (context->visibility_action == VISIBILITY_ACTION_PAUSE || context->visibility_action == VISIBILITY_ACTION_CONTINUE) {
if (!context->play && !context->end) {
context->play = true;
if (context->pause_timestamp) {
context->start_timestamp += obs_get_video_frame_time() - context->pause_timestamp;
context->pause_timestamp = 0;
}
obs_source_signal(context->source, "media_play");
}
} else if (context->visibility_action == VISIBILITY_ACTION_RESTART) {
context->play = true;
context->restart = true;
obs_source_signal(context->source, "media_restart");
}
context->active = true;
}
static void replay_source_deactive(void *data)
{
struct replay_source *context = data;
if (context->visibility_action == VISIBILITY_ACTION_PAUSE) {
if (context->play) {
context->play = false;
context->pause_timestamp = obs_get_video_frame_time();
obs_source_signal(context->source, "media_pause");
}
} else if (context->visibility_action == VISIBILITY_ACTION_RESTART) {
if (context->play) {
context->play = false;
obs_source_signal(context->source, "media_pause");
}
context->restart = true;
}
context->active = false;
}
void replay_restart(void *data)
{
struct replay_source *c = data;
c->restart = true;
c->play = true;
obs_source_signal(c->source, "media_restart");
}
void replay_stop(void *data)
{
struct replay_source *c = data;
c->play = false;
c->restart = true;
obs_source_media_ended(c->source);
}
void replay_next(void *data)
{
struct replay_source *context = data;
const int replay_count = (int)(context->replays.size / sizeof context->current_replay);
if (replay_count == 0)
return;
if (context->replay_position + 1 >= replay_count) {
context->replay_position = replay_count - 1;
} else {
context->replay_position++;
}
replay_update_position(context, true);
blog(LOG_INFO, "[replay_source: '%s'] next switched to replay %i/%i", obs_source_get_name(context->source),
context->replay_position + 1, replay_count);
}
void replay_previous(void *data)
{
struct replay_source *context = data;
if (context->replay_position <= 0) {
context->replay_position = 0;
} else {
context->replay_position--;
}
replay_update_position(context, true);
blog(LOG_INFO, "[replay_source: '%s'] previous hotkey switched to replay %i/%i", obs_source_get_name(context->source),
context->replay_position + 1, (int)(context->replays.size / sizeof context->current_replay));
}
static void replay_play_pause(void *data, bool pause)
{
struct replay_source *c = data;
if (pause) {
if (c->play) {
c->play = false;
c->pause_timestamp = obs_get_video_frame_time();
obs_source_signal(c->source, "media_pause");
} else {
c->play = true;
if (c->pause_timestamp) {
c->start_timestamp += obs_get_video_frame_time() - c->pause_timestamp;
c->pause_timestamp = 0;
}
obs_source_signal(c->source, "media_play");
}
} else {
const int64_t time = obs_get_video_frame_time();
if (c->pause_timestamp) {
c->start_timestamp += time - c->pause_timestamp;
c->pause_timestamp = 0;
}
c->play = true;
if (c->end || (c->video_frame_position == 0 && c->backward)) {
c->end = false;
if (c->backward) {
if (c->current_replay.video_frame_count)
c->video_frame_position = c->current_replay.video_frame_count - 1;
} else {
c->video_frame_position = 0;
}
c->start_timestamp = obs_get_video_frame_time();
}
obs_source_signal(c->source, "media_play");
}
}
static void replay_restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (pressed)
replay_restart(data);
}
static void replay_pause_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey, bool pressed)
{
UNUSED_PARAMETER(id);
UNUSED_PARAMETER(hotkey);
if (pressed)
replay_play_pause(data, true);
}
static void InitFileOutputLossless(struct replay_source *context)
{
context->fileOutput = obs_output_create("ffmpeg_output", "replay_ffmpeg_output", NULL, NULL);
obs_data_t *foSettings = obs_data_create();
obs_data_set_string(foSettings, "format_name", "avi");
obs_data_set_string(foSettings, "video_encoder", "utvideo");
obs_data_set_string(foSettings, "audio_encoder", "pcm_s16le");
obs_output_update(context->fileOutput, foSettings);
obs_data_release(foSettings);
obs_output_set_media(context->fileOutput, context->video_output, context->audio_t);
}
static void InitFileOutput(struct replay_source *context)
{
context->fileOutput = obs_output_create("ffmpeg_muxer", "replay_ffmpeg_output", NULL, NULL);
context->h264Recording = obs_video_encoder_create("obs_x264", "replay_h264_recording", NULL, NULL);
obs_data_t *xsettings = obs_data_create();
obs_data_set_int(xsettings, "crf", 23);
obs_data_set_bool(xsettings, "use_bufsize", true);
obs_data_set_string(xsettings, "rate_control", "CRF");
obs_data_set_string(xsettings, "profile", "high");
obs_data_set_string(xsettings, "preset", "veryfast");
obs_encoder_update(context->h264Recording, xsettings);
obs_data_release(xsettings);
obs_encoder_set_video(context->h264Recording, context->video_output);
obs_output_set_video_encoder(context->fileOutput, context->h264Recording);
context->aac = obs_audio_encoder_create("ffmpeg_aac", "aac", NULL, 0, NULL);
obs_encoder_set_audio(context->aac, context->audio_t);
obs_output_set_audio_encoder(context->fileOutput, context->aac, 0);
}
static inline size_t convert_time_to_frames(size_t sample_rate, uint64_t t)
{
return (size_t)(t * (uint64_t)sample_rate / 1000000000ULL);
}
bool audio_input_callback(void *param, uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts, uint32_t mixers,
struct audio_output_data *mixes)
{
UNUSED_PARAMETER(mixers);
struct replay_source *context = param;
*out_ts = start_ts_in;
if (!context->audio_t)
return true;
if (!context->start_save_timestamp || start_ts_in < context->start_save_timestamp)
return true;
uint64_t end_timestamp = context->start_save_timestamp + context->saving_replay.last_frame_timestamp -
context->saving_replay.first_frame_timestamp;
if (start_ts_in > end_timestamp)
return true;
size_t channels = audio_output_get_channels(context->audio_t);
size_t sample_rate = audio_output_get_sample_rate(context->audio_t);
pthread_mutex_lock(&context->audio_mutex);
uint64_t i = 0;
uint64_t duration_start = start_ts_in - context->start_save_timestamp;
uint64_t duration_end = end_ts_in - context->start_save_timestamp;
while (i < context->saving_replay.audio_frame_count &&
context->saving_replay.audio_frames[i].timestamp < context->saving_replay.first_frame_timestamp) {
i++;
}
if (i == context->saving_replay.audio_frame_count) {
pthread_mutex_unlock(&context->audio_mutex);
return true;
}
while (i < context->saving_replay.audio_frame_count &&
context->saving_replay.audio_frames[i].timestamp - context->saving_replay.first_frame_timestamp < duration_start) {
i++;
}
if (i == context->saving_replay.audio_frame_count) {
pthread_mutex_unlock(&context->audio_mutex);
return true;
}
if (i)
i--;
while (i < context->saving_replay.audio_frame_count &&
duration_end >= context->saving_replay.audio_frames[i].timestamp - context->saving_replay.first_frame_timestamp) {
size_t total_floats = AUDIO_OUTPUT_FRAMES;
size_t start_point = 0;
size_t start_point2 = 0;
if (context->saving_replay.audio_frames[i].timestamp - context->saving_replay.first_frame_timestamp >
duration_start) {
start_point = convert_time_to_frames(sample_rate, context->saving_replay.audio_frames[i].timestamp -
context->saving_replay.first_frame_timestamp -
duration_start);
if (start_point >= AUDIO_OUTPUT_FRAMES) {
pthread_mutex_unlock(&context->audio_mutex);
return true;
}
total_floats -= start_point;
} else if (context->saving_replay.audio_frames[i].timestamp - context->saving_replay.first_frame_timestamp <
duration_start) {
start_point2 = convert_time_to_frames(sample_rate,
duration_start - (context->saving_replay.audio_frames[i].timestamp -
context->saving_replay.first_frame_timestamp));
if (start_point2 >= context->saving_replay.audio_frames[i].frames) {
i++;
continue;
}
}
if (context->saving_replay.audio_frames[i].frames - start_point2 < total_floats) {
total_floats = context->saving_replay.audio_frames[i].frames - start_point2;
}
for (size_t mix_idx = 0; mix_idx < MAX_AUDIO_MIXES; mix_idx++) {
for (size_t ch = 0; ch < channels; ch++) {
register float *mix = mixes[mix_idx].data[ch];
register float *aud = (float *)context->saving_replay.audio_frames[i].data[ch];
register float *end;
if (!aud)
continue;
aud += start_point2;
mix += start_point;
end = aud + total_floats;
while (aud < end)
*(mix++) += *(aud++);
}
}
i++;
}
pthread_mutex_unlock(&context->audio_mutex);
return true;
}
void replay_save(struct replay_source *context)
{
if (context->current_replay.video_frame_count == 0) {
context->saving_status = SAVING_STATUS_NONE;
return;
}
if (context->saving_status != SAVING_STATUS_NONE && context->saving_status != SAVING_STATUS_STARTING)
return;
pthread_mutex_lock(&context->video_mutex);
memcpy(&context->saving_replay, &context->current_replay, sizeof context->current_replay);
const uint32_t width = context->saving_replay.video_frames[0]->width;
const uint32_t height = context->saving_replay.video_frames[0]->height;
if (context->known_width != width || context->known_height != height) {
video_t *t = obs_get_video();
const struct video_output_info *ovi = video_output_get_info(t);
struct video_output_info vi = {0};
vi.format = ovi->format;
vi.width = width;
vi.height = height;
vi.fps_den = ovi->fps_den;
vi.fps_num = ovi->fps_num;
vi.cache_size = 16;
vi.colorspace = ovi->colorspace;
vi.range = ovi->range;
vi.name = "ReplayOutput";
video_output_close(context->video_output);
const int r = video_output_open(&context->video_output, &vi);
if (r != VIDEO_OUTPUT_SUCCESS) {
context->saving_status = SAVING_STATUS_NONE;
pthread_mutex_unlock(&context->video_mutex);
if (context->free_after_save) {
replay_free_replay(&context->saving_replay, context);
}
return;
}
context->known_width = width;
context->known_height = height;
if (context->scaler) {
video_scaler_destroy(context->scaler);
context->scaler = NULL;
}
struct video_scale_info ssi;
ssi.format = context->saving_replay.video_frames[0]->format;
ssi.colorspace = ovi->colorspace;
ssi.range = ovi->range;
ssi.width = context->saving_replay.video_frames[0]->width;
ssi.height = context->saving_replay.video_frames[0]->height;
struct video_scale_info dsi;
dsi.format = vi.format;
dsi.colorspace = ovi->colorspace;
dsi.range = ovi->range;
dsi.height = height;
dsi.width = width;
video_scaler_create(&context->scaler, &dsi, &ssi, VIDEO_SCALE_DEFAULT);
}
if (!context->audio_t) {
struct audio_output_info oi;
oi.name = "ReplayAudio";
oi.speakers = context->saving_replay.oai.speakers;
oi.samples_per_sec = context->saving_replay.oai.samples_per_sec;
oi.format = context->saving_replay.oai.format;
oi.input_param = context;
oi.input_callback = audio_input_callback;
const int r = audio_output_open(&context->audio_t, &oi);
if (r != AUDIO_OUTPUT_SUCCESS) {
context->saving_status = SAVING_STATUS_NONE;
pthread_mutex_unlock(&context->video_mutex);
if (context->free_after_save) {
replay_free_replay(&context->saving_replay, context);
}
return;
}
}
if (!context->fileOutput) {
if (context->lossless) {
InitFileOutputLossless(context);
} else {