/
gpac.c
3709 lines (3340 loc) · 99.4 KB
/
gpac.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-2024
* All rights reserved
*
* This file is part of GPAC / gpac application
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terms 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 <gpac/main.h>
#include <gpac/filters.h>
#include <gpac/thread.h>
#include <gpac/network.h>
#include "gpac.h"
/*
exported for gpac_help.c
*/
GF_FilterSession *session=NULL;
u32 list_filters = 0;
u32 print_filter_info = 0;
int alias_argc = 0;
char **alias_argv = NULL;
GF_List *args_used = NULL;
GF_List *args_alloc = NULL;
u32 gen_doc = 0;
u32 help_flags = 0;
FILE *sidebar_md=NULL;
FILE *helpout = NULL;
const char *auto_gen_md_warning = "<!-- automatically generated - do not edit, patch gpac/applications/gpac/gpac.c -->\n";
#ifndef GPAC_CONFIG_ANDROID
u32 compositor_mode = 0;
#endif
//the default set of separators
char separator_set[7] = GF_FS_DEFAULT_SEPS;
/*
exported by gpac_help.c
*/
#ifndef GPAC_DISABLE_DOC
extern const char *gpac_doc;
#endif
/*
static vars
*/
static GF_SystemRTInfo rti;
static Bool dump_stats = GF_FALSE;
static Bool dump_graph = GF_FALSE;
static Bool print_meta_filters = GF_FALSE;
static Bool load_test_filters = GF_FALSE;
static s32 nb_loops = 0;
static s32 runfor = 0;
static Bool runfor_exit = GF_FALSE;
static Bool runfor_fast = GF_FALSE;
static u32 exit_mode = 0;
static Bool enable_prompt = GF_FALSE;
static Bool exit_nocleanup = GF_FALSE;
static u32 enable_reports = 0;
static char *report_filter = NULL;
static Bool do_unit_tests = GF_FALSE;
static Bool use_step_mode = GF_FALSE;
static u32 loops_done = 0;
static GF_Err evt_ret_val = GF_OK;
static Bool in_sig_handler = GF_FALSE;
static Bool custom_event_proc=GF_FALSE;
static u64 run_start_time = 0;
#ifdef GPAC_CONFIG_EMSCRIPTEN
static Bool has_console;
void SET_CONSOLE(int code)
{
if (!has_console) return;
switch (code) {
case GF_CONSOLE_CLEAR: code = 1; break;
case GF_CONSOLE_SAVE: code = 2; break;
case GF_CONSOLE_RESTORE: code = 3; break;
default: return;
}
MAIN_THREAD_EM_ASM({
libgpac.gpac_set_console($0);
}, code);
}
#else
#define SET_CONSOLE(_code) gf_sys_set_console_code(stderr, _code)
#endif
#if defined(ASAN_ENABLED) && defined(GPAC_CONFIG_EMSCRIPTEN)
#include <sanitizer/lsan_interface.h>
#endif
//forward defs - c.f. end of file
static void gpac_print_report(GF_FilterSession *fsess, Bool is_init, Bool is_final);
static void cleanup_logs(void);
static Bool gpac_handle_prompt(GF_FilterSession *fsess, char char_code);
static void gpac_fsess_task_help(void);
static const char *make_fileio(const char *inargs, const char **out_arg, u32 mode, GF_Err *e);
static void cleanup_file_io(void);
static GF_Filter *load_custom_filter(GF_FilterSession *sess, char *opts, GF_Err *e);
static u32 gpac_unit_tests(GF_MemTrackerType mem_track);
static Bool revert_cache_file(void *cbck, char *item_name, char *item_path, GF_FileEnumInfo *file_info);
#ifdef GPAC_DEFER_MODE
static GF_Err print_pid_props(char *arg);
static GF_Err probe_pid_link(char *arg);
static GF_Err print_pid_dests(char *arg);
#endif
#ifdef WIN32
#include <windows.h>
static BOOL WINAPI gpac_sig_handler(DWORD sig);
#else
#include <signal.h>
static void gpac_sig_handler(int sig);
#endif
static int gpac_do_creds(char *creds_args);
static Bool gpac_event_proc(void *opaque, GF_Event *event)
{
GF_FilterSession *fsess = (GF_FilterSession *)opaque;
if ((event->type==GF_EVENT_PROGRESS) && (event->progress.progress_type==3)) {
gpac_print_report(fsess, GF_FALSE, GF_FALSE);
}
else if (event->type==GF_EVENT_QUIT) {
if (event->message.error>0)
evt_ret_val = event->message.error;
gf_fs_abort(fsess, GF_FS_FLUSH_ALL);
}
#if !defined(GPAC_CONFIG_ANDROID) && !defined(GPAC_DISABLE_COMPOSITOR)
else if (custom_event_proc) {
return mp4c_event_proc(opaque, event);
}
#endif
return GF_FALSE;
}
static Bool gpac_fsess_task(GF_FilterSession *fsess, void *callback, u32 *reschedule_ms)
{
if (enable_prompt && gf_prompt_has_input()) {
#if !defined(GPAC_CONFIG_ANDROID) && !defined(GPAC_DISABLE_COMPOSITOR)
if ((compositor_mode==LOAD_MP4C) && mp4c_handle_prompt(gf_prompt_get_char())) {
} else
#endif
if (!gpac_handle_prompt(fsess, gf_prompt_get_char()))
return GF_FALSE;
}
#if !defined(GPAC_CONFIG_ANDROID) && !defined(GPAC_DISABLE_COMPOSITOR)
if ((compositor_mode==LOAD_MP4C) && mp4c_task()) {
gf_fs_abort(fsess, GF_FS_FLUSH_NONE);
}
#endif
if (runfor>0) {
u64 now = gf_sys_clock_high_res();
if (!run_start_time) {
run_start_time = now;
} else if (now - run_start_time > runfor) {
//segfault requested
if (exit_mode==1) {
exit(127);
}
//deadlock requested
else if (exit_mode==2) {
while(1) {
gf_sleep(1);
}
}
if (nb_loops || loops_done) {
gf_fs_abort(fsess, runfor_exit ? GF_FS_FLUSH_NONE : (runfor_fast ? GF_FS_FLUSH_FAST : GF_FS_FLUSH_ALL));
run_start_time = 0;
} else {
if (runfor_exit)
exit(0);
gf_fs_abort(fsess, runfor_fast ? GF_FS_FLUSH_FAST : GF_FS_FLUSH_ALL);
}
return GF_FALSE;
}
}
if (gf_fs_is_last_task(fsess))
return GF_FALSE;
//check every 50 ms
*reschedule_ms = 50;
return GF_TRUE;
}
#if defined(GPAC_CONFIG_EMSCRIPTEN) && defined(__EMSCRIPTEN_PTHREADS__)
static void reset_em_thread();
#endif
static int gpac_exit_fun(int code)
{
u32 i;
if (code>=0) {
for (i=1; i<gf_sys_get_argc(); i++) {
if (!gf_sys_is_arg_used(i)) {
GF_LOG(GF_LOG_ERROR, GF_LOG_APP, ("Warning: argument %s set but not used\n", gf_sys_get_arg(i) ));
}
}
} else {
//negative code is unrecognized option, don't print unused arguments
code = 1;
}
if (alias_argv) {
while (gf_list_count(args_alloc)) {
gf_free(gf_list_pop_back(args_alloc));
}
gf_list_del(args_alloc);
args_alloc = NULL;
gf_list_del(args_used);
args_used = NULL;
gf_free(alias_argv);
alias_argv = NULL;
alias_argc = 0;
}
if ((helpout != stdout) && (helpout != stderr)) {
if (gen_doc==2) {
fprintf(helpout, ".SH EXAMPLES\n.TP\nBasic and advanced examples are available at https://wiki.gpac.io/Filters\n");
fprintf(helpout, ".SH MORE\n.LP\nAuthors: GPAC developers, see git repo history (-log)\n"
".br\nFor bug reports, feature requests, more information and source code, visit https://github.com/gpac/gpac\n"
".br\nbuild: %s\n"
".br\nCopyright: %s\n.br\n"
".SH SEE ALSO\n"
".LP\ngpac(1), MP4Box(1)\n", GPAC_VERSION, gf_gpac_copyright());
}
gf_fclose(helpout);
}
if (sidebar_md) {
gf_fclose(sidebar_md);
sidebar_md = NULL;
}
cleanup_logs();
gf_sys_close();
#ifdef GPAC_MEMORY_TRACKING
if (!code && (gf_memory_size() || gf_file_handles_count() )) {
gf_log_set_tool_level(GF_LOG_MEMORY, GF_LOG_INFO);
gf_memory_print();
code = 2;
}
#endif
#ifdef GPAC_CONFIG_EMSCRIPTEN
#ifdef ASAN_ENABLED
fprintf(stdout, "ASAN: checking mem leaks\n");
__lsan_do_leak_check();
#endif
#ifdef __EMSCRIPTEN_PTHREADS__
reset_em_thread();
#endif
MAIN_THREAD_EM_ASM({
if (typeof libgpac.gpac_done == 'function') libgpac.gpac_done($0);
}, code);
#endif
return code;
}
u32 get_u32(char *val, char *log_name)
{
u32 res;
if (sscanf(val, "%u", &res)==1) return res;
GF_LOG(GF_LOG_ERROR, GF_LOG_APP, ("%s must be an unsigned integer (got %s), using 0\n", log_name, val));
return 0;
}
s32 get_s32(char *val, char *log_name)
{
s32 res;
if (sscanf(val, "%d", &res)==1) return res;
GF_LOG(GF_LOG_ERROR, GF_LOG_APP, ("%s must be a signed integer (got %s), using 0\n", log_name, val));
return 0;
}
#define gpac_exit(_code) \
return gpac_exit_fun(_code)
#if defined(GPAC_CONFIG_DARWIN) && !defined(GPAC_CONFIG_IOS)
#include <syslog.h>
static void gpac_syslog(void *cbck, GF_LOG_Level log_level, GF_LOG_Tool log_tool, const char* fmt, va_list vlist)
{
int prio;
char szMsg[4000];
vsprintf(szMsg, fmt, vlist);
if (log_level==GF_LOG_DEBUG) prio = LOG_DEBUG;
else if (log_level==GF_LOG_INFO) prio = LOG_INFO;
else if (log_level==GF_LOG_WARNING) prio = LOG_WARNING;
else prio = LOG_ERR;
syslog(prio, "%s", szMsg);
}
#endif
//the main on emscriptem is structured as:
// main
// gpac_run - ends with calling emscripten_set_main_loop_arg
//
// gpac_done (end of main in regular build) is called after last task in em_main_loop
//
#ifdef GPAC_CONFIG_EMSCRIPTEN
#ifdef __EMSCRIPTEN_PTHREADS__
#include <emscripten/threading.h>
#endif
static int gpac_run();
static int gpac_done(GF_Err e, Bool exit_only);
static Bool use_video_display;
static Bool window_swap;
static s32 run_steps;
void gpac_on_video_swap()
{
window_swap = GF_TRUE;
}
static void em_main_loop(void*arg)
{
u32 run = run_steps;
window_swap = GF_FALSE;
while (session && run) {
GF_Err e = gf_fs_run(session);
if (gf_fs_is_last_task(session)) {
gf_fs_stop(session);
gpac_done(GF_OK, GF_FALSE);
emscripten_cancel_main_loop();
return;
}
if (e==GF_NOT_READY)
break;
if (use_video_display && window_swap) {
break;
}
run--;
}
}
s32 em_raf_fps;
void gf_fs_force_non_blocking(GF_FilterSession *fs);
void gpac_force_step_mode(Bool for_display)
{
if (!session) return;
if (!use_step_mode) {
use_step_mode = GF_TRUE;
fprintf(stdout, "forcing step mode\n");
gf_fs_force_non_blocking(session);
}
if (for_display) {
use_video_display = GF_TRUE;
//main loop callback not set, use 0 (let browser choose) since we display
if (em_raf_fps<0) em_raf_fps = 0;
}
else {
//audio out, don't force num_steps
//if (run_steps<0) run_steps = 1;
}
}
GF_EXPORT
void gpac_em_sig_handler(int type)
{
if (!session) return;
switch (type) {
case 1:
fprintf(stderr, "Aborting with full flush ...\n");
gf_fs_abort(session, GF_FS_FLUSH_ALL);
break;
case 2:
fprintf(stderr, "Aborting with fast flush ...\n");
gf_fs_abort(session, GF_FS_FLUSH_FAST);
break;
case 3:
fprintf(stderr, "Aborting without flush %s...\n", nb_loops ? "and stoping loops" : "");
gf_fs_abort(session, GF_FS_FLUSH_NONE);
nb_loops=0;
break;
case 4:
if (!enable_reports) {
enable_reports = 2;
report_filter = NULL;
gf_fs_set_ui_callback(session, gpac_event_proc, session);
gf_fs_enable_reporting(session, GF_TRUE);
gpac_print_report(session, GF_TRUE, GF_FALSE);
} else {
enable_reports = 0;
gf_fs_enable_reporting(session, GF_FALSE);
SET_CONSOLE(GF_CONSOLE_CLEAR);
SET_CONSOLE(GF_CONSOLE_RESTORE);
}
break;
}
}
#endif
//all vars valid for all loops (-sloop)
static Bool alias_is_play = GF_FALSE;
static u32 nb_filters = 0;
static GF_List *links_directive=NULL;
static GF_List *loaded_filters=NULL;
static char *view_conn_for_filter = NULL;
static GF_SysArgMode argmode = GF_ARGMODE_BASE;
static u32 sflags=0;
static Bool override_seps=GF_FALSE;
static int argc;
static char **argv;
static Bool view_filter_conn = GF_FALSE;
static Bool dump_codecs = GF_FALSE;
static Bool dump_formats = GF_FALSE;
static Bool dump_proto_schemes = GF_FALSE;
static Bool write_profile=GF_FALSE;
static Bool write_core_opts=GF_FALSE;
static Bool write_extensions=GF_FALSE;
static const char *session_js=NULL;
static Bool has_xopt = GF_FALSE;
static Bool nothing_to_do = GF_TRUE;
#ifdef GPAC_DEFER_MODE
static Bool defer_mode = GF_FALSE;
#endif
#ifndef GPAC_DISABLE_NETWORK
static Bool enum_net_ifces(void *cbk, const char *name, const char *IP, u32 flags)
{
char *prev_name = cbk;
if (strcmp(prev_name, name)) {
if (prev_name[0]) fprintf(stdout, "\n");
strcpy(prev_name, name);
fprintf(stdout, "%s", name);
fprintf(stdout, ": %s", (flags & GF_NETIF_ACTIVE) ? "Up" : "Down");
if (flags & GF_NETIF_NO_MCAST) fprintf(stdout, " NoMulticast");
if (flags & GF_NETIF_RECV_ONLY) fprintf(stdout, " ReceiveOnly");
if (flags & GF_NETIF_LOOPBACK) fprintf(stdout, " Loopback");
fprintf(stdout, "\n");
}
fprintf(stdout, "\tIPv%d %s\n", (flags & GF_NETIF_IPV6) ? 6 : 4, IP ? IP : "address not assigned");
return GF_FALSE;
}
#endif
#ifdef GPAC_DEFER_MODE
static void run_sess(void)
{
if (use_step_mode) {
//max run (in case some filters are asking for RT reschedule
//this is only used for defer mdoe
u32 retry=10;
while (retry && !gf_fs_is_last_task(session)) {
gf_fs_run(session);
retry--;
}
} else {
gf_fs_run(session);
}
}
#endif
#ifndef GPAC_CONFIG_ANDROID
static
#endif
int gpac_main(int _argc, char **_argv)
{
GF_Err e=GF_OK;
int i;
const char *profile=NULL;
GF_MemTrackerType mem_track=GF_MemTrackerNone;
Bool has_alias = GF_FALSE;
Bool alias_set = GF_FALSE;
Bool do_creds = GF_FALSE;
char *creds_args=NULL;
//init static vars
helpout = stdout;
list_filters = print_filter_info = gen_doc = help_flags = 0;
strcpy(separator_set, GF_FS_DEFAULT_SEPS);
//bools
dump_stats = dump_graph = print_meta_filters = load_test_filters = GF_FALSE;
runfor_exit = runfor_fast = enable_prompt = use_step_mode = in_sig_handler = custom_event_proc = GF_FALSE;
//s32
nb_loops = runfor = 0;
//u32
exit_mode = enable_reports = loops_done = 0;
//u64
run_start_time = 0;
report_filter = NULL;
evt_ret_val = GF_OK;
//initialize all vars valid for all loops (-sloop)
argc = _argc;
argv = _argv;
alias_is_play = GF_FALSE;
nb_filters = 0;
links_directive=NULL;
loaded_filters=NULL;
view_conn_for_filter = NULL;
session_js=NULL;
argmode = GF_ARGMODE_BASE;
sflags=0;
override_seps = view_filter_conn = dump_codecs = dump_formats = dump_proto_schemes = GF_FALSE;
write_profile = write_core_opts = write_extensions = has_xopt = GF_FALSE;
nothing_to_do = GF_TRUE;
#ifdef GPAC_DEFER_MODE
defer_mode = GF_FALSE;
#endif
#ifdef GPAC_CONFIG_EMSCRIPTEN
window_swap = GF_FALSE;
use_video_display = GF_FALSE;
//default requestAnimationFrame frequency is disabled, if step mode is forced without target fps use 200
em_raf_fps = -1;
run_steps = -1;
#endif
#ifdef WIN32
Bool gpac_is_global_launch();
char *argv_w[2];
if ((argc == 1) && gpac_is_global_launch()) {
argc = 2;
argv_w[0] = argv[0];
argv_w[1] = "-gui";
argv = argv_w;
}
#endif
//look for mem track and profile, and check for link directives
for (i=1; i<argc; i++) {
char *arg = argv[i];
if (!strcmp(arg, "-mem-track") || !strcmp(arg, "-mem-track-stack")) {
#ifdef GPAC_MEMORY_TRACKING
mem_track = !strcmp(arg, "-mem-track-stack") ? GF_MemTrackerBackTrace : GF_MemTrackerSimple;
#else
fprintf(stderr, "WARNING - GPAC not compiled with Memory Tracker - ignoring \"%s\"\n", arg);
#endif
} else if (!strncmp(arg, "-p=", 3)) {
profile = arg+3;
} else if (!strncmp(arg, "-creds", 6)) {
do_creds = GF_TRUE;
if (!strncmp(arg, "-creds=", 7)) creds_args = arg+7;
} else if (!strncmp(arg, "-mkl=", 5)) {
return gpac_make_lang(arg+5);
}
}
gf_sys_init(mem_track, profile);
#ifdef GPAC_CONFIG_ANDROID
//prevent destruction of JSRT until we unload the JNI gpac wrapper (see applications/gpac_android/src/main/jni/gpac_jni.cpp)
gf_opts_set_key("temp", "static-jsrt", "true");
#endif
if (do_creds) {
gf_sys_set_args(argc, (const char **)argv);
return gpac_do_creds(creds_args);
}
// gf_log_set_tool_level(GF_LOG_ALL, GF_LOG_WARNING);
gf_log_set_tool_level(GF_LOG_APP, GF_LOG_INFO);
if (gf_opts_get_key_count("gpac.alias")) {
for (i=1; i<argc; i++) {
char *arg = argv[i];
if (gf_opts_get_key("gpac.alias", arg) != NULL) {
has_alias = GF_TRUE;
if (!strcmp(arg, "-play"))
alias_is_play = GF_TRUE;
break;
}
}
if (has_alias) {
if (! gpac_expand_alias(argc, argv) ) {
gpac_exit(1);
}
argc = alias_argc;
argv = alias_argv;
}
}
#ifndef GPAC_CONFIG_ANDROID
if (compositor_mode) {
//we by default want 2 additional threads in gui:
//main thread might get locked on vsync
//second thread might be busy decoding audio/video
//third thread will then be able to refill all buffers/perform networks tasks
gf_opts_set_key("temp", "threads", "2");
//the number of threads will be overridden if set at command line
if (compositor_mode==LOAD_MP4C)
enable_prompt = GF_TRUE;
}
#endif
//this will parse default args
e = gf_sys_set_args(argc, (const char **) argv);
if (e) {
fprintf(stderr, "Error assigning libgpac arguments: %s\n", gf_error_to_string(e) );
gpac_exit(1);
}
if (!profile || (strcmp(profile, "0") && strcmp(profile, "n")) )
gpac_load_suggested_filter_args();
#ifndef GPAC_DISABLE_LOG
if (gf_log_tool_level_on(GF_LOG_APP, GF_LOG_DEBUG)) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_APP, ("GPAC args: "));
for (i=1; i<argc; i++) {
char *arg = argv[i];
GF_LOG(GF_LOG_DEBUG, GF_LOG_APP, ("%s ", arg));
}
GF_LOG(GF_LOG_DEBUG, GF_LOG_APP, ("\n"));
}
#endif
//emscripten, always use step mode by default, unless explicitly disabled by -step=N<0
#ifdef GPAC_CONFIG_EMSCRIPTEN
use_step_mode = GF_TRUE;
#endif
for (i=1; i<argc; i++) {
char szArgName[1024];
char *arg = argv[i];
char *arg_val = strchr(arg, '=');
if (arg_val) {
u32 len = (u32) (arg_val - arg);
if (len>1023) len=1023;
strncpy(szArgName, arg, len);
szArgName[len]=0;
arg = szArgName;
arg_val++;
}
gf_sys_mark_arg_used(i, GF_TRUE);
if ((!has_xopt && !strcmp(arg, "-h")) || !strcmp(arg, "-help") || !strcmp(arg, "-ha") || !strcmp(arg, "-hx") || !strcmp(arg, "-hh")) {
if (!strcmp(arg, "-ha")) argmode = GF_ARGMODE_ADVANCED;
else if (!strcmp(arg, "-hx")) argmode = GF_ARGMODE_EXPERT;
else if (!strcmp(arg, "-hh")) argmode = GF_ARGMODE_ALL;
gf_opts_set_key("temp", "gpac-help", arg+1);
if (i+1<argc)
gf_sys_mark_arg_used(i+1, GF_TRUE);
if ((i+1==argc) || (argv[i+1][0]=='-')) {
gpac_usage(argmode);
gpac_exit(0);
} else if (!strcmp(argv[i+1], "log")) {
gpac_core_help(GF_ARGMODE_ALL, GF_TRUE);
gpac_exit(0);
} else if (!strcmp(argv[i+1], "core")) {
gpac_core_help(argmode, GF_FALSE);
gpac_exit(0);
} else if (!strcmp(argv[i+1], "modules") || !strcmp(argv[i+1], "module")) {
if (i+2<argc) {
gf_sys_mark_arg_used(i+2, GF_TRUE);
gpac_modules_help(argv[i+2]);
} else {
gpac_modules_help(NULL);
}
gpac_exit(0);
} else if (!strcmp(argv[i+1], "doc")) {
gpac_filter_help();
gpac_exit(0);
} else if (!strcmp(argv[i+1], "alias")) {
gpac_alias_help(argmode);
gpac_exit(0);
} else if (!strcmp(argv[i+1], "props")) {
if (i+2<argc) {
gf_sys_mark_arg_used(i+2, GF_TRUE);
dump_all_props(argv[i+2]);
} else {
dump_all_props(NULL);
}
gpac_exit(0);
} else if (!strcmp(argv[i+1], "colors")) {
dump_all_colors();
gpac_exit(0);
} else if (!strcmp(argv[i+1], "layouts")) {
dump_all_audio_cicp();
gpac_exit(0);
} else if (!strcmp(argv[i+1], "cfg")) {
gpac_config_help();
gpac_exit(0);
} else if (!strcmp(argv[i+1], "prompt")) {
gpac_fsess_task_help();
gpac_exit(0);
#ifdef GPAC_DEFER_MODE
} else if (!strcmp(argv[i+1], "defer")) {
gpac_defer_help();
gpac_exit(0);
#endif
} else if (!strcmp(argv[i+1], "mp4c")) {
#if defined(GPAC_CONFIG_ANDROID) || defined(GPAC_DISABLE_COMPOSITOR)
gf_sys_format_help(helpout, help_flags, "-mp4c unavailable for android\n");
#else
mp4c_help(argmode);
#endif
gpac_exit(0);
}
//old syntax -gui -h us still valid, but also allow -h gui to be consistent with other help options
else if (!strcmp(argv[i+1], "gui")) {
gf_opts_set_key("temp", "gpac-help", "yes");
session = gf_fs_new_defaults(0);
gf_set_progress_callback(session, NULL);
gf_fs_load_filter(session, "compositor:player=gui", NULL);
gf_fs_run(session);
gf_fs_del(session);
session = NULL;
gpac_exit(0);
} else if (!strcmp(argv[i+1], "codecs")) {
dump_codecs = GF_TRUE;
sflags |= GF_FS_FLAG_LOAD_META | GF_FS_FLAG_NO_GRAPH_CACHE;
i++;
} else if (!strcmp(argv[i+1], "formats") || !strcmp(argv[i+1], "exts")) {
dump_formats = GF_TRUE;
sflags |= GF_FS_FLAG_LOAD_META | GF_FS_FLAG_NO_GRAPH_CACHE;
i++;
} else if (!strcmp(argv[i+1], "protocols")) {
dump_proto_schemes = GF_TRUE;
sflags |= GF_FS_FLAG_LOAD_META | GF_FS_FLAG_NO_GRAPH_CACHE;
i++;
} else if (!strcmp(argv[i+1], "net")) {
#ifndef GPAC_DISABLE_NETWORK
char szName[100];
szName[0]=0;
gf_net_enum_interfaces(enum_net_ifces, szName);
#endif
gpac_exit(0);
} else if (!strcmp(argv[i+1], "links")) {
view_filter_conn = GF_TRUE;
if ((i+2<argc) && (argv[i+2][0] != '-')) {
view_conn_for_filter = argv[i+2];
gf_sys_mark_arg_used(i+2, GF_TRUE);
i++;
}
i++;
} else if (!strcmp(argv[i+1], "creds")) {
gpac_credentials_help(argmode);
gpac_exit(0);
} else if (!strcmp(argv[i+1], "bin")) {
gf_sys_format_help(helpout, help_flags, "GPAC binary information:\n"
"Version: %s\n"
"Compilation configuration: " GPAC_CONFIGURATION "\n"
"Enabled features: %s\n"
"Disabled features: %s\n", gf_gpac_version(), gf_sys_features(GF_FALSE), gf_sys_features(GF_TRUE)
);
gpac_exit(0);
} else if (!strcmp(argv[i+1], "filters")) {
list_filters = 1;
sflags |= GF_FS_FLAG_NO_GRAPH_CACHE;
i++;
} else if (!strcmp(argv[i+1], "filters:*") || !strcmp(argv[i+1], "filters:@")) {
list_filters = 2;
sflags |= GF_FS_FLAG_NO_GRAPH_CACHE;
i++;
} else {
print_filter_info = 1;
if (!strcmp(argv[i+1], "*:*") || !strcmp(argv[i+1], "@:@"))
print_filter_info = 2;
sflags |= GF_FS_FLAG_NO_GRAPH_CACHE;
}
}
else if (has_xopt && !strcmp(arg, "-h")) {
gf_opts_set_key("temp", "gpac-help", "yes");
}
else if (!strcmp(arg, "-genmd") || !strcmp(arg, "-genman")) {
argmode = GF_ARGMODE_ALL;
if (!strcmp(arg, "-genmd")) {
gf_opts_set_key("temp", "gendoc", "yes");
gen_doc = 1;
help_flags = GF_PRINTARG_MD;
helpout = gf_fopen("gpac_general.md", "w");
fprintf(helpout, "%s", auto_gen_md_warning);
fprintf(helpout, "# General Usage of gpac\n");
} else {
gf_opts_set_key("temp", "gendoc", "yes");
gen_doc = 2;
help_flags = GF_PRINTARG_MAN;
helpout = gf_fopen("gpac.1", "w");
fprintf(helpout, ".TH gpac 1 2019 gpac GPAC\n");
fprintf(helpout, ".\n.SH NAME\n.LP\ngpac \\- GPAC command-line filter session manager\n"
".SH SYNOPSIS\n.LP\n.B gpac\n"
".RI [options] FILTER [LINK] FILTER [...]\n.br\n.\n");
}
gpac_usage(GF_ARGMODE_ALL);
if (gen_doc==1) {
fprintf(helpout, "# Using Aliases\n");
} else {
fprintf(helpout, ".SH Using Aliases\n.PL\n");
}
gpac_alias_help(GF_ARGMODE_EXPERT);
gpac_credentials_help(GF_ARGMODE_EXPERT);
if (gen_doc==1) {
gf_fclose(helpout);
helpout = gf_fopen("core_config.md", "w");
fprintf(helpout, "%s", auto_gen_md_warning);
}
gpac_config_help();
if (gen_doc==1) {
gf_fclose(helpout);
helpout = gf_fopen("core_options.md", "w");
fprintf(helpout, "%s", auto_gen_md_warning);
fprintf(helpout, "# GPAC Core Options\n");
}
gpac_core_help(argmode, GF_FALSE);
if (gen_doc==1) {
gf_fclose(helpout);
helpout = gf_fopen("core_logs.md", "w");
fprintf(helpout, "%s", auto_gen_md_warning);
fprintf(helpout, "# GPAC Log System\n");
}
gpac_core_help(argmode, GF_TRUE);
if (gen_doc==1) {
gf_fclose(helpout);
helpout = gf_fopen("filters_general.md", "w");
fprintf(helpout, "%s", auto_gen_md_warning);
}
#ifndef GPAC_DISABLE_DOC
gf_sys_format_help(helpout, help_flags, "%s", gpac_doc);
#endif
if (gen_doc==1) {
gf_fclose(helpout);
helpout = gf_fopen("filters_properties.md", "w");
fprintf(helpout, "%s", auto_gen_md_warning);
}
gf_sys_format_help(helpout, help_flags, "# GPAC Built-in properties\n");
dump_all_props(NULL);
// dump_codecs = GF_TRUE;
if (gen_doc==2) {
fprintf(helpout, ".SH EXAMPLES\n.TP\nBasic and advanced examples are available at https://wiki.gpac.io/Filters\n");
fprintf(helpout, ".SH MORE\n.LP\nAuthors: GPAC developers, see git repo history (-log)\n"
".br\nFor bug reports, feature requests, more information and source code, visit https://github.com/gpac/gpac\n"
".br\nbuild: %s\n"
".br\nCopyright: %s\n.br\n"
".SH SEE ALSO\n"
".LP\ngpac-filters(1),MP4Box(1)\n", GPAC_VERSION, gf_gpac_copyright());
gf_fclose(helpout);
helpout = gf_fopen("gpac-filters.1", "w");
fprintf(helpout, ".TH gpac 1 2019 gpac GPAC\n");
fprintf(helpout, ".\n.SH NAME\n.LP\ngpac \\- GPAC command-line filter session manager\n"
".SH SYNOPSIS\n.LP\n.B gpac\n"
".RI [options] FILTER [LINK] FILTER [...]\n.br\n.\n"
".SH DESCRIPTION\n.LP"
"\nThis page describes all filters usually present in GPAC\n"
"\nTo check for help on a filter not listed here, use gpac -h myfilter\n"
"\n"
);
}
list_filters = 1;
}
else if (!strcmp(arg, "-ltf")) {
load_test_filters = GF_TRUE;
} else if (!strncmp(arg, "-lcf", 4)) {
} else if (!strcmp(arg, "-stats")) {
dump_stats = GF_TRUE;
} else if (!strcmp(arg, "-graph")) {
dump_graph = GF_TRUE;
} else if (strstr(arg, ":*") || strstr(arg, ":@")) {
if (list_filters)
list_filters = 3;
else
print_meta_filters = GF_TRUE;
} else if (!strcmp(arg, "-wc")) {
write_core_opts = GF_TRUE;
} else if (!strcmp(arg, "-we")) {
write_extensions = GF_TRUE;
} else if (!strcmp(arg, "-wf")) {
write_profile = GF_TRUE;
} else if (!strcmp(arg, "-wfx")) {
write_profile = GF_TRUE;
sflags |= GF_FS_FLAG_LOAD_META;
} else if (!strcmp(arg, "-sloop")) {
nb_loops = -1;
if (arg_val) nb_loops = get_s32(arg_val, "sloop");
} else if (!strcmp(arg, "-runfor")) {
if (arg_val) runfor = 1000*get_u32(arg_val, "runfor");
} else if (!strcmp(arg, "-runforf")) {
if (arg_val) runfor = 1000*get_u32(arg_val, "runfor");
runfor_fast = GF_TRUE;
} else if (!strcmp(arg, "-runforx")) {
if (arg_val) runfor = 1000*get_u32(arg_val, "runforx");
runfor_exit = GF_TRUE;
} else if (!strcmp(arg, "-runfors")) {
if (arg_val) runfor = 1000*get_u32(arg_val, "runfors");
exit_mode = 1;
} else if (!strcmp(arg, "-runforl")) {
if (arg_val) runfor = 1000*get_u32(arg_val, "runforl");
exit_mode = 2;
} else if (!strcmp(arg, "-uncache")) {
const char *cache_dir = gf_opts_get_key("core", "cache");
gf_enum_directory(cache_dir, GF_FALSE, revert_cache_file, NULL, ".txt");
fprintf(stderr, "GPAC Cache dir %s flattened\n", cache_dir);
gpac_exit(0);
} else if (!strcmp(arg, "-cfg")) {
nothing_to_do = GF_FALSE;
}
else if (!strcmp(arg, "-alias") || !strcmp(arg, "-aliasdoc")) {
char *alias_val;
Bool exists;
if (!arg_val) {
fprintf(stderr, "-alias does not have any argument, check usage \"gpac -h\"\n");
gpac_exit(1);
}
alias_val = arg_val ? strchr(arg_val, ' ') : NULL;
if (alias_val) alias_val[0] = 0;
session = gf_fs_new_defaults(sflags);
exists = gf_fs_filter_exists(session, arg_val);
gf_fs_del(session);
if (exists) {
fprintf(stderr, "alias %s has the same name as an existing filter, not allowed\n", arg_val);
if (alias_val) alias_val[0] = ' ';
gpac_exit(1);
}
gf_opts_set_key(!strcmp(arg, "-alias") ? "gpac.alias" : "gpac.aliasdoc", arg_val, alias_val ? alias_val+1 : NULL);
fprintf(stderr, "Set %s for %s to %s\n", arg, arg_val, alias_val ? alias_val+1 : "NULL");
if (alias_val) alias_val[0] = ' ';
alias_set = GF_TRUE;
}
else if (!strncmp(arg, "-seps", 5)) {
parse_sep_set(arg_val, &override_seps);
} else if (!strcmp(arg, "-mem-track") || !strcmp(arg, "-mem-track-stack")) {
} else if (!strcmp(arg, "-k")) {
if (arg_val && (!strcmp(arg_val, "0") || !strcmp(arg_val, "no") || !strcmp(arg_val, "false")))
enable_prompt = GF_FALSE;
else
enable_prompt = GF_TRUE;
} else if (!strcmp(arg, "-qe")) {
exit_nocleanup = GF_TRUE;
} else if (!strcmp(arg, "-js")) {
session_js = arg_val;
} else if (!strcmp(arg, "-r")) {
enable_reports = 2;
if (arg_val && !strlen(arg_val)) {
enable_reports = 1;
} else {
report_filter = arg_val;
}
} else if (!strcmp(arg, "-unit-tests")) {
do_unit_tests = GF_TRUE;
} else if (!strcmp(arg, "-cl")) {
sflags |= GF_FS_FLAG_NO_IMPLICIT;
} else if (!strcmp(arg, "-sid")) {
sflags |= GF_FS_FLAG_REQUIRE_SOURCE_ID;
#ifdef GPAC_DEFER_MODE
} else if (!strcmp(arg, "-dl")) {
sflags |= GF_FS_FLAG_FORCE_DEFER_LINK;
defer_mode=GF_TRUE;
} else if (!strcmp(arg, "-np")) {
sflags |= GF_FS_FLAG_PREVENT_PLAY;