/
filter_session.c
4623 lines (4067 loc) · 141 KB
/
filter_session.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2017-2023
* All rights reserved
*
* This file is part of GPAC / filters sub-project
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terfsess of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include "filter_session.h"
#include <gpac/network.h>
#ifndef GPAC_DISABLE_3D
#include <gpac/modules/video_out.h>
#endif
//#define CHECK_TASK_LIST_INTEGRITY
#ifndef GPAC_DISABLE_FONTS
struct _gf_ft_mgr *gf_font_manager_new();
void gf_font_manager_del(struct _gf_ft_mgr *fm);
#endif
static GFINLINE void gf_fs_sema_io(GF_FilterSession *fsess, Bool notify, Bool main)
{
//we don't use sema on emscripten, we always give control back to main caller or pthread
#ifndef GPAC_CONFIG_EMSCRIPTEN
GF_Semaphore *sem = main ? fsess->semaphore_main : fsess->semaphore_other;
if (sem) {
if (notify) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_SCHEDULER, ("Thread %u notify scheduler %s semaphore\n", gf_th_id(), main ? "main" : "secondary"));
if ( ! gf_sema_notify(sem, 1)) {
GF_LOG(GF_LOG_ERROR, GF_LOG_SCHEDULER, ("Cannot notify scheduler of new task, semaphore failure\n"));
}
} else {
u32 nb_tasks;
//if not main and no tasks in main list, this could be the last task to process.
//if main thread is sleeping force a wake to take further actions (only the main thread decides the exit)
//this also ensures that the main thread will process tasks from secondary task lists if no
//dedicated main thread tasks are present (eg no GL filters)
if (!main && fsess->in_main_sem_wait && !gf_fq_count(fsess->main_thread_tasks)) {
gf_fs_sema_io(fsess, GF_TRUE, GF_TRUE);
}
nb_tasks = 1;
//no active threads, count number of tasks. If no posted tasks we are likely at the end of the session, don't block, rather use a sem_wait
if (!fsess->active_threads)
nb_tasks = gf_fq_count(fsess->main_thread_tasks) + gf_fq_count(fsess->tasks);
//if main semaphore, keep track that we are going to sleep
if (main) {
fsess->in_main_sem_wait = GF_TRUE;
if (!nb_tasks) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_SCHEDULER, ("No tasks scheduled, waiting on main semaphore for at most 100 ms\n"));
if (gf_sema_wait_for(sem, 100)) {
}
} else {
if (gf_sema_wait(sem)) {
}
}
fsess->in_main_sem_wait = GF_FALSE;
} else {
if (!nb_tasks) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_SCHEDULER, ("No tasks scheduled, waiting on secondary semaphore for at most 100 ms\n"));
if (gf_sema_wait_for(sem, 100)) {
}
} else {
if (gf_sema_wait(sem)) {
}
}
}
}
}
#endif // GPAC_CONFIG_EMSCRIPTEN
}
GF_EXPORT
void gf_fs_add_filter_register(GF_FilterSession *fsess, const GF_FilterRegister *freg)
{
if (!freg) return;
if (!fsess) {
if (freg->register_free) freg->register_free(fsess, (GF_FilterRegister *)freg);
return;
}
if (!freg->name) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Filter missing name - ignoring\n"));
if (freg->register_free) freg->register_free(fsess, (GF_FilterRegister *)freg);
return;
}
#if 0
if (strchr(freg->name, fsess->sep_args) ) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Filter name cannot contain argument separator %c - ignoring\n", fsess->sep_args));
if (freg->register_free) freg->register_free(fsess, (GF_FilterRegister *)freg);
return;
}
if (strchr(freg->name, fsess->sep_name)) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Filter name cannot contain argument value separator %c - ignoring\n", fsess->sep_name));
if (freg->register_free) freg->register_free(fsess, (GF_FilterRegister *)freg);
return;
}
#endif
if (!freg->process) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Filter %s missing process function - ignoring\n", freg->name));
if (freg->register_free) freg->register_free(fsess, (GF_FilterRegister *)freg);
return;
}
//except for meta filters, don't accept a filter with input caps but no output caps
//meta filters do follow the same rule, however when expanding them for help we may have weird cases
//where no config is set but inputs are listed to expose mime types or extensions (cf ffdmx)
if (!(freg->flags & GF_FS_REG_META)
&& !freg->configure_pid
&& gf_filter_has_in_caps(freg->caps, freg->nb_caps)
) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Filter %s missing configure_pid function but has input capabilities - ignoring\n", freg->name));
if (freg->register_free) freg->register_free(fsess, (GF_FilterRegister *)freg);
return;
}
gf_mx_p(fsess->filters_mx);
gf_list_add(fsess->registry, (void *) freg);
gf_mx_v(fsess->filters_mx);
if (fsess->init_done && fsess->links && gf_list_count( fsess->links)) {
gf_filter_sess_build_graph(fsess, freg);
}
}
static Bool fs_default_event_proc(void *ptr, GF_Event *evt)
{
GF_FilterSession *fs = (GF_FilterSession *)ptr;
if (evt->type==GF_EVENT_QUIT) {
gf_fs_abort(fs, (evt->message.error) ? GF_FS_FLUSH_NONE : GF_FS_FLUSH_FAST);
}
if (evt->type==GF_EVENT_MESSAGE) {
if (evt->message.error) {
if (evt->message.service) {
GF_LOG(GF_LOG_ERROR, GF_LOG_MEDIA, ("Service %s %s: %s\n", evt->message.service, evt->message.message, gf_error_to_string(evt->message.error) ))
} else {
GF_LOG(GF_LOG_ERROR, GF_LOG_MEDIA, ("%s: %s\n", evt->message.message, gf_error_to_string(evt->message.error) ))
}
}
}
#ifdef GPAC_HAS_QJS
if (fs->on_evt_task && jsfs_on_event(fs, evt))
return GF_TRUE;
#endif
if (evt->type==GF_EVENT_AUTHORIZATION) {
#ifdef GPAC_HAS_QJS
if (fs->on_auth_task && jsfs_on_auth(fs, evt))
return GF_TRUE;
#endif
if (gf_sys_is_test_mode()) {
return GF_FALSE;
}
assert( evt->type == GF_EVENT_AUTHORIZATION);
assert( evt->auth.user);
assert( evt->auth.password);
assert( evt->auth.site_url);
fprintf(stderr, "**** Authorization required for site %s %s ****\n", evt->auth.site_url, evt->auth.secure ? "(secure)" : "- NOT SECURE");
fprintf(stderr, "login : ");
if (!gf_read_line_input(evt->auth.user, 50, 1))
return GF_FALSE;
fprintf(stderr, "\npassword: ");
if (!gf_read_line_input(evt->auth.password, 50, 0))
return GF_FALSE;
fprintf(stderr, "*********\n");
if (evt->auth.on_usr_pass) {
evt->auth.on_usr_pass(evt->auth.async_usr_data, evt->auth.user, evt->auth.password, GF_FALSE);
evt->auth.password[0] = 0;
}
return GF_TRUE;
}
return GF_FALSE;
}
#ifdef GF_FS_ENABLE_LOCALES
static Bool fs_check_locales(void *__self, const char *locales_parent_path, const char *rel_path, char *relocated_path, char *localized_rel_path);
#endif
#ifdef __EMSCRIPTEN_PTHREADS__
#include <emscripten/threading.h>
#endif
GF_EXPORT
GF_FilterSession *gf_fs_new(s32 nb_threads, GF_FilterSchedulerType sched_type, u32 flags, const char *blacklist)
{
const char *opt;
Bool gf_sys_has_filter_global_args();
Bool gf_sys_has_filter_global_meta_args();
u32 i;
GF_FilterSession *fsess, *a_sess;
//safety check: all built-in properties shall have unique 4CCs
if (gf_sys_is_test_mode() && ! gf_props_4cc_check_props())
return NULL;
GF_SAFEALLOC(fsess, GF_FilterSession);
if (!fsess) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Failed to alloc media session\n"));
return NULL;
}
//reverse implicit mode flag, we run by default in this mode
if (flags & GF_FS_FLAG_NO_IMPLICIT)
flags &= ~GF_FS_FLAG_IMPLICIT_MODE;
else
flags |= GF_FS_FLAG_IMPLICIT_MODE;
fsess->flags = flags;
if (gf_opts_get_bool("core", "no-mx"))
nb_threads=0;
#ifdef __EMSCRIPTEN_PTHREADS__
//detect if we run as a worker
if (!emscripten_is_main_runtime_thread()) {
fsess->is_worker = GF_TRUE;
GF_LOG(GF_LOG_INFO, GF_LOG_FILTER, ("Session running in worker mode\n"));
}
#endif
fsess->filters = gf_list_new();
fsess->main_th.fsess = fsess;
if ((s32) nb_threads == -1) {
GF_SystemRTInfo rti;
memset(&rti, 0, sizeof(GF_SystemRTInfo));
if (gf_sys_get_rti(0, &rti, 0)) {
nb_threads = rti.nb_cores-1;
}
if ((s32)nb_threads<0) {
GF_LOG(GF_LOG_WARNING, GF_LOG_FILTER, ("Failed to query number of cores, disabling extra threads for session\n"));
nb_threads=0;
}
}
if (sched_type==GF_FS_SCHEDULER_DIRECT) {
fsess->direct_mode = GF_TRUE;
nb_threads=0;
}
if (nb_threads && (sched_type != GF_FS_SCHEDULER_LOCK_FREE_X)) {
fsess->tasks_mx = gf_mx_new("TasksList");
}
//regardless of scheduler type, we don't use lock on the main task list
fsess->tasks = gf_fq_new(fsess->tasks_mx);
if (nb_threads>0) {
fsess->main_thread_tasks = gf_fq_new(fsess->tasks_mx);
fsess->filters_mx = gf_mx_new("Filters");
} else {
//otherwise use the same as the global task list
fsess->main_thread_tasks = fsess->tasks;
}
if (!(flags & GF_FS_FLAG_NO_RESERVOIR)) {
fsess->tasks_reservoir = gf_fq_new(fsess->tasks_mx);
}
if (nb_threads || (sched_type==GF_FS_SCHEDULER_LOCK_FORCE) ) {
fsess->semaphore_main = fsess->semaphore_other = gf_sema_new(GF_INT_MAX, 0);
if (nb_threads>0)
fsess->semaphore_other = gf_sema_new(GF_INT_MAX, 0);
//force testing of mutex queues
//fsess->use_locks = GF_TRUE;
}
fsess->ui_event_proc = fs_default_event_proc;
fsess->ui_opaque = fsess;
if (flags & GF_FS_FLAG_NON_BLOCKING)
fsess->non_blocking = 1;
if (!fsess->semaphore_main)
nb_threads=0;
#ifndef GPAC_DISABLE_THREADS
if (nb_threads) {
fsess->threads = gf_list_new();
if (!fsess->threads) {
gf_sema_del(fsess->semaphore_main);
fsess->semaphore_main=NULL;
gf_sema_del(fsess->semaphore_other);
fsess->semaphore_other=NULL;
nb_threads=0;
}
fsess->use_locks = (sched_type==GF_FS_SCHEDULER_LOCK) ? GF_TRUE : GF_FALSE;
} else
#endif
{
#ifdef GPAC_MEMORY_TRACKING
extern int gf_mem_track_enabled;
fsess->check_allocs = gf_mem_track_enabled;
#endif
}
if (fsess->use_locks)
fsess->props_mx = gf_mx_new("FilterSessionProps");
if (!(flags & GF_FS_FLAG_NO_RESERVOIR)) {
#if GF_PROPS_HASHTABLE_SIZE
fsess->prop_maps_list_reservoir = gf_fq_new(fsess->props_mx);
#endif
fsess->prop_maps_reservoir = gf_fq_new(fsess->props_mx);
fsess->prop_maps_entry_reservoir = gf_fq_new(fsess->props_mx);
fsess->prop_maps_entry_data_alloc_reservoir = gf_fq_new(fsess->props_mx);
//we also use the props mutex for the this one
fsess->pcks_refprops_reservoir = gf_fq_new(fsess->props_mx);
}
#ifndef GPAC_DISABLE_REMOTERY
sprintf(fsess->main_th.rmt_name, "FSThread0");
#endif
if (!fsess->filters || !fsess->tasks) {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Failed to alloc media session\n"));
fsess->run_status = GF_OUT_OF_MEM;
gf_fs_del(fsess);
return NULL;
}
if (nb_threads) {
fsess->info_mx = gf_mx_new("FilterSessionInfo");
fsess->ui_mx = gf_mx_new("FilterSessionUIProc");
}
#ifndef GPAC_DISABLE_THREADS
for (i=0; i<(u32) nb_threads; i++) {
char szName[30];
GF_SessionThread *sess_thread;
GF_SAFEALLOC(sess_thread, GF_SessionThread);
if (!sess_thread) continue;
#ifndef GPAC_DISABLE_REMOTERY
sprintf(sess_thread->rmt_name, "FSThread%d", i+1);
#endif
sprintf(szName, "gf_fs_th_%d", i+1);
sess_thread->th = gf_th_new(szName);
if (!sess_thread->th) {
gf_free(sess_thread);
continue;
}
sess_thread->fsess = fsess;
gf_list_add(fsess->threads, sess_thread);
}
#endif
gf_fs_set_separators(fsess, NULL);
fsess->registry = gf_list_new();
#ifdef GPAC_HAS_QJS
//keep copy of blacklist for JS
fsess->blacklist = blacklist ? gf_strdup(blacklist) : NULL;
#else
fsess->blacklist = (char*)blacklist;
#endif
a_sess = (flags & GF_FS_FLAG_LOAD_META) ? fsess : NULL;
gf_fs_reg_all(fsess, a_sess);
#ifndef GPAC_HAS_QJS
fsess->blacklist = NULL;
#endif
//todo - find a way to handle events without mutex ...
fsess->evt_mx = gf_mx_new("Event mutex");
fsess->blocking_mode = GF_FS_BLOCK_ALL;
opt = gf_opts_get_key("core", "no-block");
if (opt) {
if (!strcmp(opt, "fanout")) {
fsess->blocking_mode = GF_FS_NOBLOCK_FANOUT;
}
else if (!strcmp(opt, "all")) {
fsess->blocking_mode = GF_FS_NOBLOCK;
}
}
fsess->run_status = GF_EOS;
fsess->nb_threads_stopped = 1+nb_threads;
fsess->default_pid_buffer_max_us = gf_opts_get_int("core", "buffer-gen");
fsess->decoder_pid_buffer_max_us = gf_opts_get_int("core", "buffer-dec");
fsess->default_pid_buffer_max_units = gf_opts_get_int("core", "buffer-units");
fsess->max_resolve_chain_len = 6;
fsess->auto_inc_nums = gf_list_new();
if (nb_threads)
fsess->links_mx = gf_mx_new("FilterRegistryGraph");
fsess->links = gf_list_new();
#ifndef GPAC_DISABLE_3D
fsess->gl_providers = gf_list_new();
#endif
if (! (fsess->flags & GF_FS_FLAG_NO_GRAPH_CACHE))
gf_filter_sess_build_graph(fsess, NULL);
fsess->init_done = GF_TRUE;
//parse all global filter options for argument tracking
if (gf_sys_has_filter_global_args() || gf_sys_has_filter_global_meta_args()) {
u32 nb_args = gf_sys_get_argc();
for (i=0; i<nb_args; i++) {
char *arg = (char *)gf_sys_get_arg(i);
if (arg[0]!='-') continue;
if ((arg[1]!='-') && (arg[1]!='+')) continue;
char *sep = strchr(arg, '=');
if (sep) sep[0] = 0;
gf_fs_push_arg(fsess, arg+2, GF_FALSE, (arg[1]=='-') ? GF_ARGTYPE_GLOBAL : GF_ARGTYPE_META, NULL, NULL);
//force indexing in reframers when dash template with bandwidth is used
if (sep && !strcmp(arg+2, "template") && strstr(sep+1, "$Bandwidth$")) {
gf_opts_set_key("temp", "force_indexing", "true");
}
if (sep) sep[0] = '=';
}
}
#ifdef GF_FS_ENABLE_LOCALES
fsess->uri_relocators = gf_list_new();
fsess->locales.relocate_uri = fs_check_locales;
fsess->locales.sess = fsess;
gf_list_add(fsess->uri_relocators, &fsess->locales);
#endif
return fsess;
}
void gf_fs_push_arg(GF_FilterSession *session, const char *szArg, Bool was_found, GF_FSArgItemType type, GF_Filter *meta_filter, const char *sub_opt_name)
{
u32 meta_len = meta_filter ? (u32) strlen(meta_filter->freg->name) : 0;
Bool create_if_not_found = GF_TRUE;
if (session->flags & GF_FS_FLAG_NO_ARG_CHECK)
return;
//ignore any meta argument reported (found or not) that is not already present
//if sub_opt_name, we must create an entry
if (!sub_opt_name && (type==GF_ARGTYPE_META_REPORTING)) {
create_if_not_found = GF_FALSE;
}
if (!session->parsed_args) session->parsed_args = gf_list_new();
u32 k, acount = gf_list_count(session->parsed_args);
GF_FSArgItem *ai=NULL;
for (k=0; k<acount; k++) {
ai = gf_list_get(session->parsed_args, k);
if (!strcmp(ai->argname, szArg))
break;
if (meta_len
&& !strncmp(ai->argname, meta_filter->freg->name, meta_len)
&& ((ai->argname[meta_len]==':') || (ai->argname[meta_len]=='@'))
&& !strcmp(ai->argname+meta_len+1, szArg)
) {
break;
}
ai = NULL;
}
if (!ai && create_if_not_found) {
GF_SAFEALLOC(ai, GF_FSArgItem);
if (ai) {
ai->argname = gf_strdup(szArg);
ai->type = type;
if ((type==GF_ARGTYPE_META_REPORTING) && meta_filter) {
ai->meta_filter = meta_filter->freg->name;
ai->meta_opt = sub_opt_name;
}
gf_list_add(session->parsed_args, ai );
}
}
if (!ai) return;
if (type==GF_ARGTYPE_META_REPORTING) {
//meta option declared as true by default at init but notified as not found
if (ai->meta_state==3) {
ai->meta_state = 0;
if (!was_found) ai->opt_found = 0;
}
if (!ai->meta_state) {
ai->opt_found = 0;
ai->meta_state = 1;
if (!ai->meta_filter) {
ai->meta_filter = meta_filter->freg->name;
ai->meta_opt = sub_opt_name;
}
}
if (was_found)
ai->meta_state = 2;
} else if (was_found && (!ai->meta_state || !meta_filter) ) {
ai->opt_found = 1;
//initial declaration from filter setup: meta args are declared as true by default
if (type==GF_ARGTYPE_LOCAL) {
//forbid further meta state change
ai->meta_state = 1;
//meta option, mark as not found
if (meta_filter) ai->meta_state = 3;
else ai->meta_filter = NULL;
}
}
}
GF_EXPORT
GF_FilterSession *gf_fs_new_defaults(u32 inflags)
{
GF_FilterSession *fsess;
GF_FilterSchedulerType sched_type = GF_FS_SCHEDULER_LOCK_FREE;
u32 flags = 0;
s32 nb_threads = gf_opts_get_int("core", "threads");
const char *blacklist = gf_opts_get_key("core", "blacklist");
const char *opt = gf_opts_get_key("core", "sched");
if (!opt) sched_type = GF_FS_SCHEDULER_LOCK_FREE;
else if (!strcmp(opt, "lock")) sched_type = GF_FS_SCHEDULER_LOCK;
else if (!strcmp(opt, "flock")) sched_type = GF_FS_SCHEDULER_LOCK_FORCE;
else if (!strcmp(opt, "direct")) sched_type = GF_FS_SCHEDULER_DIRECT;
else if (!strcmp(opt, "free")) sched_type = GF_FS_SCHEDULER_LOCK_FREE;
else if (!strcmp(opt, "freex")) sched_type = GF_FS_SCHEDULER_LOCK_FREE_X;
else {
GF_LOG(GF_LOG_ERROR, GF_LOG_FILTER, ("Unrecognized scheduler type %s\n", opt));
return NULL;
}
if (inflags & GF_FS_FLAG_LOAD_META)
flags |= GF_FS_FLAG_LOAD_META;
if (inflags & GF_FS_FLAG_NON_BLOCKING)
flags |= GF_FS_FLAG_NON_BLOCKING;
if (inflags & GF_FS_FLAG_NO_GRAPH_CACHE)
flags |= GF_FS_FLAG_NO_GRAPH_CACHE;
if (inflags & GF_FS_FLAG_PRINT_CONNECTIONS)
flags |= GF_FS_FLAG_PRINT_CONNECTIONS;
if (inflags & GF_FS_FLAG_NO_IMPLICIT)
flags |= GF_FS_FLAG_NO_IMPLICIT;
if (gf_opts_get_bool("core", "dbg-edges"))
flags |= GF_FS_FLAG_PRINT_CONNECTIONS;
if (gf_opts_get_bool("core", "full-link"))
flags |= GF_FS_FLAG_FULL_LINK;
if (gf_opts_get_bool("core", "no-reg"))
flags |= GF_FS_FLAG_NO_REGULATION;
if (gf_opts_get_bool("core", "no-reassign"))
flags |= GF_FS_FLAG_NO_REASSIGN;
if (gf_opts_get_bool("core", "no-graph-cache"))
flags |= GF_FS_FLAG_NO_GRAPH_CACHE;
if (gf_opts_get_bool("core", "no-probe"))
flags |= GF_FS_FLAG_NO_PROBE;
else if (inflags & GF_FS_FLAG_NO_PROBE)
flags |= GF_FS_FLAG_NO_PROBE;
if (gf_opts_get_bool("core", "no-argchk"))
flags |= GF_FS_FLAG_NO_ARG_CHECK;
if (gf_opts_get_bool("core", "no-reservoir"))
flags |= GF_FS_FLAG_NO_RESERVOIR;
fsess = gf_fs_new(nb_threads, sched_type, flags, blacklist);
if (!fsess) return NULL;
gf_fs_set_max_resolution_chain_length(fsess, gf_opts_get_int("core", "max-chain") );
gf_fs_set_max_sleep_time(fsess, gf_opts_get_int("core", "max-sleep") );
opt = gf_opts_get_key("core", "seps");
if (opt)
gf_fs_set_separators(fsess, opt);
return fsess;
}
GF_EXPORT
GF_Err gf_fs_set_separators(GF_FilterSession *session, const char *separator_set)
{
if (!session) return GF_BAD_PARAM;
if (separator_set && (strlen(separator_set)<5)) return GF_BAD_PARAM;
if (separator_set) {
session->sep_args = separator_set[0];
session->sep_name = separator_set[1];
session->sep_frag = separator_set[2];
session->sep_list = separator_set[3];
session->sep_neg = separator_set[4];
} else {
session->sep_args = ':';
session->sep_name = '=';
session->sep_frag = '#';
session->sep_list = ',';
session->sep_neg = '!';
}
return GF_OK;
}
GF_EXPORT
GF_Err gf_fs_set_max_resolution_chain_length(GF_FilterSession *session, u32 max_chain_length)
{
if (!session) return GF_BAD_PARAM;
session->max_resolve_chain_len = max_chain_length;
return GF_OK;
}
GF_EXPORT
GF_Err gf_fs_set_max_sleep_time(GF_FilterSession *session, u32 max_sleep)
{
if (!session) return GF_BAD_PARAM;
session->max_sleep = max_sleep;
return GF_OK;
}
GF_EXPORT
u32 gf_fs_get_max_resolution_chain_length(GF_FilterSession *session)
{
if (!session) return 0;
return session->max_resolve_chain_len;
}
GF_EXPORT
void gf_fs_remove_filter_register(GF_FilterSession *session, GF_FilterRegister *freg)
{
if (!session || !freg) return;
gf_mx_p(session->filters_mx);
gf_list_del_item(session->registry, freg);
gf_mx_v(session->filters_mx);
gf_filter_sess_reset_graph(session, freg);
}
GF_EXPORT
void gf_fs_set_ui_callback(GF_FilterSession *fs, Bool (*ui_event_proc)(void *opaque, GF_Event *event), void *cbk_udta)
{
if (fs) {
fs->ui_event_proc = ui_event_proc;
fs->ui_opaque = cbk_udta;
if (!fs->ui_event_proc) {
fs->ui_event_proc = fs_default_event_proc;
fs->ui_opaque = fs;
}
}
}
void gf_propalloc_del(void *it)
{
GF_PropertyEntry *pe = (GF_PropertyEntry *)it;
if (pe->prop.value.data.ptr) gf_free(pe->prop.value.data.ptr);
gf_free(pe);
}
GF_EXPORT
Bool gf_fs_enum_unmapped_options(GF_FilterSession *fsess, u32 *idx, const char **argname, u32 *argtype, const char **meta_filter, const char **meta_sub_opt)
{
if (!fsess || !fsess->parsed_args) return GF_FALSE;
u32 i, count = gf_list_count(fsess->parsed_args);
for (i=*idx; i<count; i++) {
GF_FSArgItem *ai = gf_list_get(fsess->parsed_args, i);
(*idx)++;
if (ai->opt_found || (ai->meta_state==2)) continue;
if (argname) *argname = ai->argname;
if (argtype) *argtype = ai->type;
if (meta_filter) *meta_filter = ai->meta_filter;
if (meta_sub_opt) *meta_sub_opt = ai->meta_opt;
return GF_TRUE;
}
return GF_FALSE;
}
void task_canceled(GF_FSTask *task);
static void gf_task_del(void *p)
{
GF_FSTask *t = (GF_FSTask *) p;
task_canceled(t);
gf_free(p);
}
GF_EXPORT
void gf_fs_del(GF_FilterSession *fsess)
{
assert(fsess);
gf_fs_stop(fsess);
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Session destroy begin\n"));
if (fsess->parsed_args) {
while (gf_list_count(fsess->parsed_args)) {
GF_FSArgItem *ai = gf_list_pop_back(fsess->parsed_args);
gf_free(ai->argname);
gf_free(ai);
}
gf_list_del(fsess->parsed_args);
}
//temporary until we don't introduce fsess_stop
assert(fsess->run_status != GF_OK);
if (fsess->filters) {
u32 i, pass, count=gf_list_count(fsess->filters);
//first pass: disconnect all filters, since some may have references to property maps or packets
for (i=0; i<count; i++) {
u32 j;
GF_Filter *filter = gf_list_get(fsess->filters, i);
filter->process_th_id = 0;
filter->scheduled_for_next_task = GF_TRUE;
if (filter->detached_pid_inst) {
while (gf_list_count(filter->detached_pid_inst)) {
GF_FilterPidInst *pidi = gf_list_pop_front(filter->detached_pid_inst);
gf_filter_pid_inst_del(pidi);
}
gf_list_del(filter->detached_pid_inst);
filter->detached_pid_inst = NULL;
}
if (filter->postponed_packets) {
while (gf_list_count(filter->postponed_packets)) {
GF_FilterPacket *pck = gf_list_pop_front(filter->postponed_packets);
gf_filter_packet_destroy(pck);
}
gf_list_del(filter->postponed_packets);
filter->postponed_packets = NULL;
}
gf_mx_p(filter->tasks_mx);
for (j=0; j<filter->num_input_pids; j++) {
GF_FilterPidInst *pidi = gf_list_get(filter->input_pids, j);
gf_filter_pid_inst_reset(pidi);
}
gf_mx_v(filter->tasks_mx);
filter->scheduled_for_next_task = GF_FALSE;
}
//second pass, finalize all
for (pass=0; pass<2; pass++) {
Bool has_scripts = GF_FALSE;
for (i=0; i<count; i++) {
GF_Filter *filter = gf_list_get(fsess->filters, i);
if (!pass && (filter->freg->flags & GF_FS_REG_SCRIPT)) {
has_scripts = GF_TRUE;
continue;
}
if (filter->freg->finalize && !filter->finalized) {
filter->finalized = GF_TRUE;
FSESS_CHECK_THREAD(filter)
filter->freg->finalize(filter);
}
}
if (!has_scripts) break;
}
while (gf_list_count(fsess->filters)) {
GF_Filter *filter = gf_list_pop_back(fsess->filters);
gf_filter_del(filter);
}
gf_list_del(fsess->filters);
fsess->filters = NULL;
}
gf_fs_unload_script(fsess, NULL);
#ifdef GPAC_USE_DOWNLOADER
if (fsess->download_manager) gf_dm_del(fsess->download_manager);
#endif
#ifndef GPAC_DISABLE_FONTS
if (fsess->font_manager) gf_font_manager_del(fsess->font_manager);
#endif
if (fsess->registry) {
while (gf_list_count(fsess->registry)) {
GF_FilterRegister *freg = gf_list_pop_back(fsess->registry);
if (freg->register_free) freg->register_free(fsess, freg);
}
gf_list_del(fsess->registry);
}
if (fsess->tasks)
gf_fq_del(fsess->tasks, gf_task_del);
if (fsess->tasks_reservoir)
gf_fq_del(fsess->tasks_reservoir, gf_void_del);
#ifndef GPAC_DISABLE_THREADS
if (fsess->threads) {
if (fsess->main_thread_tasks)
gf_fq_del(fsess->main_thread_tasks, gf_task_del);
while (gf_list_count(fsess->threads)) {
GF_SessionThread *sess_th = gf_list_pop_back(fsess->threads);
gf_th_del(sess_th->th);
gf_free(sess_th);
}
gf_list_del(fsess->threads);
}
#endif
if (fsess->prop_maps_reservoir)
gf_fq_del(fsess->prop_maps_reservoir, gf_propmap_del);
#if GF_PROPS_HASHTABLE_SIZE
if (fsess->prop_maps_list_reservoir)
gf_fq_del(fsess->prop_maps_list_reservoir, (gf_destruct_fun) gf_list_del);
#endif
if (fsess->prop_maps_entry_reservoir)
gf_fq_del(fsess->prop_maps_entry_reservoir, gf_void_del);
if (fsess->prop_maps_entry_data_alloc_reservoir)
gf_fq_del(fsess->prop_maps_entry_data_alloc_reservoir, gf_propalloc_del);
if (fsess->pcks_refprops_reservoir)
gf_fq_del(fsess->pcks_refprops_reservoir, gf_void_del);
if (fsess->props_mx)
gf_mx_del(fsess->props_mx);
if (fsess->info_mx)
gf_mx_del(fsess->info_mx);
if (fsess->ui_mx)
gf_mx_del(fsess->ui_mx);
if (fsess->semaphore_other && (fsess->semaphore_other != fsess->semaphore_main) )
gf_sema_del(fsess->semaphore_other);
if (fsess->semaphore_main)
gf_sema_del(fsess->semaphore_main);
if (fsess->tasks_mx)
gf_mx_del(fsess->tasks_mx);
if (fsess->filters_mx)
gf_mx_del(fsess->filters_mx);
if (fsess->evt_mx) gf_mx_del(fsess->evt_mx);
if (fsess->event_listeners) gf_list_del(fsess->event_listeners);
if (fsess->links) {
gf_filter_sess_reset_graph(fsess, NULL);
gf_list_del(fsess->links);
}
if (fsess->links_mx) gf_mx_del(fsess->links_mx);
#ifndef GPAC_DISABLE_3D
gf_list_del(fsess->gl_providers);
if (fsess->gl_driver) {
fsess->gl_driver->Shutdown(fsess->gl_driver);
gf_modules_close_interface((GF_BaseInterface *)fsess->gl_driver);
}
#endif
if (fsess->auto_inc_nums) {
while(gf_list_count(fsess->auto_inc_nums)) {
GF_FSAutoIncNum *aint = gf_list_pop_back(fsess->auto_inc_nums);
gf_free(aint);
}
gf_list_del(fsess->auto_inc_nums);
}
#ifdef GF_FS_ENABLE_LOCALES
if (fsess->uri_relocators) gf_list_del(fsess->uri_relocators);
if (fsess->locales.szAbsRelocatedPath) gf_free(fsess->locales.szAbsRelocatedPath);
#endif
if (fsess->blacklist) gf_free(fsess->blacklist);
gf_free(fsess);
GF_LOG(GF_LOG_DEBUG, GF_LOG_FILTER, ("Session destroyed\n"));
}
GF_EXPORT
u32 gf_fs_filters_registers_count(GF_FilterSession *fsess)
{
return fsess ? gf_list_count(fsess->registry) : 0;
}
GF_EXPORT
const GF_FilterRegister * gf_fs_get_filter_register(GF_FilterSession *fsess, u32 idx)
{
return gf_list_get(fsess->registry, idx);
}
#ifdef CHECK_TASK_LIST_INTEGRITY
static void check_task_list_enum(void *udta, void *item)
{
assert(udta != item);
}
static void check_task_list(GF_FilterQueue *fq, GF_FSTask *task)
{
if (fq) {
gf_fq_enum(fq, check_task_list_enum, task);
}
}
#endif
void gf_fs_post_task_ex(GF_FilterSession *fsess, gf_fs_task_callback task_fun, GF_Filter *filter, GF_FilterPid *pid, const char *log_name, void *udta, Bool is_configure, Bool force_main_thread, Bool force_direct_call, u32 class_type)
{
GF_FSTask *task;
Bool notified = GF_FALSE;
assert(fsess);
assert(task_fun);
//only flatten calls if in main thread (we still have some broken filters using threading that could trigger tasks)
if ((force_direct_call || fsess->direct_mode)
&& (!filter || !filter->in_process)
&& fsess->tasks_in_process
&& (gf_th_id()==fsess->main_th.th_id)
) {
GF_FSTask atask;
u64 task_time = gf_sys_clock_high_res();
memset(&atask, 0, sizeof(GF_FSTask));
atask.filter = filter;
atask.pid = pid;
atask.run_task = task_fun;
atask.log_name = log_name;
atask.udta = udta;
GF_LOG(GF_LOG_DEBUG, GF_LOG_SCHEDULER, ("Thread 0 task#%d %p executing Filter %s::%s (%d tasks pending)\n", fsess->main_th.nb_tasks, &atask, filter ? filter->name : "none", log_name, fsess->tasks_pending));
if (filter)
filter->scheduled_for_next_task = GF_TRUE;
task_fun(&atask);
filter = atask.filter;
if (filter) {
filter->time_process += gf_sys_clock_high_res() - task_time;
filter->scheduled_for_next_task = GF_FALSE;
filter->nb_tasks_done++;
}
if (!atask.requeue_request)
return;
//asked to requeue the task, post it
}
/*this was a gf_filter_process_task request but direct call could not be done or requeue is requested.
process_task_queued was incremented by caller without checking for existing process task
- If the task was not treated, dec / inc will give the same state, undo process_task_queued increment
- If the task was requeued, dec will undo the increment done when requeing the task in gf_filter_check_pending_tasks
In both cases, inc will redo the same logic as in gf_filter_post_process_task_internal, not creating task if gf_filter_process_task is
already scheduled for the filter
We must use safe_int_dec/safe_int_inc here for multi thread cases - cf issue #1778
*/
if (force_direct_call) {
assert(filter);
safe_int_dec(&filter->process_task_queued);
if (safe_int_inc(&filter->process_task_queued) > 1) {
return;
}
}
task = gf_fq_pop(fsess->tasks_reservoir);
if (!task) {
GF_SAFEALLOC(task, GF_FSTask);
if (!task) {
GF_LOG(GF_LOG_ERROR, GF_LOG_SCHEDULER, ("No more memory to post new task\n"));
return;
}
}
task->filter = filter;
task->pid = pid;
task->run_task = task_fun;
task->log_name = log_name;
task->udta = udta;
task->class_type = class_type;
if (filter && is_configure) {
if (filter->freg->flags & GF_FS_REG_CONFIGURE_MAIN_THREAD)
force_main_thread = GF_TRUE;
}
if (filter) {
gf_mx_p(filter->tasks_mx);
//no tasks and not scheduled
if (! filter->scheduled_for_next_task && !gf_fq_count(filter->tasks)) {