-
Notifications
You must be signed in to change notification settings - Fork 519
/
os_divers.c
3355 lines (2896 loc) · 80.8 KB
/
os_divers.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 2000-2024
* All rights reserved
*
* This file is part of GPAC / common tools sub-project
*
* 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/tools.h>
#include <gpac/network.h>
#include <gpac/config_file.h>
#if defined(_WIN32_WCE)
#include <winbase.h>
#include <winsock.h>
#include <tlhelp32.h>
//#include <direct.h>
#if !defined(__GNUC__)
#pragma comment(lib, "toolhelp")
#endif
#elif defined(WIN32)
#include <time.h>
#include <sys/timeb.h>
#include <io.h>
#include <windows.h>
#include <tlhelp32.h>
#include <direct.h>
#if !defined(__GNUC__)
#pragma comment(lib, "winmm")
#endif
#else
#include <time.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/times.h>
#include <sys/resource.h>
#ifndef __BEOS__
#include <errno.h>
#endif
#define SLEEP_ABS_SELECT 1
static u32 sys_start_time = 0;
static u64 sys_start_time_hr = 0;
#endif
#ifndef _WIN32_WCE
#include <locale.h>
#endif
#include <gpac/revision.h>
#define GPAC_FULL_VERSION GPAC_VERSION "-rev" GPAC_GIT_REVISION
#define GPAC_COPYRIGHT "(c) 2000-2023 Telecom Paris distributed under LGPL v2.1+ - https://gpac.io"
GF_EXPORT
const char *gf_gpac_version()
{
return GPAC_FULL_VERSION;
}
#ifdef GPAC_MP4BOX_MINI
#define MINI_BUILD_DISCLAIMER "\n\tMINI build (encoders, decoders, audio and video output disabled)"
#endif
GF_EXPORT
const char *gf_gpac_copyright()
{
return GPAC_COPYRIGHT
#ifdef GPAC_MP4BOX_MINI
MINI_BUILD_DISCLAIMER
#endif
;
}
GF_EXPORT
const char *gf_gpac_copyright_cite()
{
return GPAC_COPYRIGHT
#ifdef GPAC_MP4BOX_MINI
MINI_BUILD_DISCLAIMER
#endif
"\n\n" \
"Please cite our work in your research:\n"
"\tGPAC Filters: https://doi.org/10.1145/3339825.3394929\n"
"\tGPAC: https://doi.org/10.1145/1291233.1291452\n"
;
}
GF_EXPORT
u32 gf_gpac_abi_major()
{
return GPAC_VERSION_MAJOR;
}
GF_EXPORT
u32 gf_gpac_abi_minor()
{
return GPAC_VERSION_MINOR;
}
GF_EXPORT
u32 gf_gpac_abi_micro()
{
return GPAC_VERSION_MICRO;
}
#if !defined(WIN32) && !defined(GPAC_CONFIG_EMSCRIPTEN)
GF_EXPORT
u32 gf_sys_clock()
{
struct timeval now;
gettimeofday(&now, NULL);
return (u32) ( ( (now.tv_sec)*1000 + (now.tv_usec) / 1000) - sys_start_time );
}
GF_EXPORT
u64 gf_sys_clock_high_res()
{
struct timeval now;
gettimeofday(&now, NULL);
return (now.tv_sec)*1000000 + (now.tv_usec) - sys_start_time_hr;
}
#endif
#ifdef GPAC_CONFIG_EMSCRIPTEN
#include <emscripten/emscripten.h>
GF_EXPORT
u32 gf_sys_clock()
{
return (u32)(emscripten_get_now() - sys_start_time);
}
GF_EXPORT
u64 gf_sys_clock_high_res()
{
return (long long)(emscripten_get_now() * 1000.0) - sys_start_time_hr;
}
#endif
Bool gf_sys_enable_remotery(Bool start, Bool is_shutdown);
static Bool gpac_disable_rti = GF_FALSE;
GF_EXPORT
void gf_sleep(u32 ms)
{
gf_rmt_begin(sleep, GF_RMT_AGGREGATE);
#ifdef WIN32
Sleep(ms);
#else
s32 sel_err;
struct timeval tv;
#ifndef SLEEP_ABS_SELECT
u32 prev, now, elapsed;
#endif
#ifdef SLEEP_ABS_SELECT
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
#else
prev = gf_sys_clock();
#endif
do {
errno = 0;
#ifndef SLEEP_ABS_SELECT
now = gf_sys_clock();
elapsed = (now - prev);
if ( elapsed >= ms ) {
break;
}
prev = now;
ms -= elapsed;
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
#endif
sel_err = select(0, NULL, NULL, NULL, &tv);
} while ( sel_err && (errno == EINTR) );
#endif
gf_rmt_end();
}
#ifndef gettimeofday
#ifdef _WIN32_WCE
#include <time.h>
//#include <wce_time.h>
/*
* Author of first version (timeval.h): by Wu Yongwei
* Author of Windows CE version: Mateusz Loskot (mateusz@loskot.net)
*
* All code here is considered in the public domain though we do wish our names
* could be retained if anyone uses them.
*/
/*
* Constants used internally by time functions.
*/
#ifndef _TM_DEFINED
struct tm
{
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
#define _TM_DEFINED
#endif /* _TM_DEFINED */
#ifndef _TIMEZONE_DEFINED
struct timezone
{
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
#define _TIMEZONE_DEFINED
#endif /* _TIMEZONE_DEFINED */
#if defined(_MSC_VER) || defined(__BORLANDC__)
#define EPOCHFILETIME (116444736000000000i64)
#else
#define EPOCHFILETIME (116444736000000000LL)
#endif
int gettimeofday(struct timeval *tp, struct timezone *tzp)
{
SYSTEMTIME st;
FILETIME ft;
LARGE_INTEGER li;
TIME_ZONE_INFORMATION tzi;
__int64 t;
if (NULL != tp)
{
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time */
t /= 10; /* In microseconds */
tp->tv_sec = (long)(t / 1000000);
tp->tv_usec = (long)(t % 1000000);
}
if (NULL != tzp)
{
GetTimeZoneInformation(&tzi);
tzp->tz_minuteswest = tzi.Bias;
if (tzi.StandardDate.wMonth != 0)
{
tzp->tz_minuteswest += tzi.StandardBias * 60;
}
if (tzi.DaylightDate.wMonth != 0)
{
tzp->tz_dsttime = 1;
}
else
{
tzp->tz_dsttime = 0;
}
}
return 0;
}
#if _GPAC_UNUSED
/*
time between jan 1, 1601 and jan 1, 1970 in units of 100 nanoseconds
FILETIME in Win32 is from jan 1, 1601
*/
s32 __gettimeofday(struct timeval *tp, void *tz)
{
FILETIME ft;
SYSTEMTIME st;
s32 val;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
val = (s32) ((*(LONGLONG *) &ft - TIMESPEC_TO_FILETIME_OFFSET) / 10000000);
tp->tv_sec = (u32) val;
val = (s32 ) ((*(LONGLONG *) &ft - TIMESPEC_TO_FILETIME_OFFSET - ((LONGLONG) val * (LONGLONG) 10000000)) * 100);
tp->tv_usec = val;
return 0;
}
#endif
#elif defined(WIN32)
static s32 gettimeofday(struct timeval *tp, void *tz)
{
struct _timeb timebuffer;
_ftime( &timebuffer );
tp->tv_sec = (long) (timebuffer.time);
tp->tv_usec = timebuffer.millitm * 1000;
return 0;
}
#endif
#endif
#ifdef _WIN32_WCE
void CE_Assert(u32 valid, char *file, u32 line)
{
if (!valid) {
char szBuf[2048];
u16 wcBuf[2048];
sprintf(szBuf, "File %s : line %d", file, line);
CE_CharToWide(szBuf, wcBuf);
MessageBox(NULL, wcBuf, _T("GPAC Assertion Failure"), MB_OK);
exit(EXIT_FAILURE);
}
}
void CE_WideToChar(unsigned short *w_str, char *str)
{
WideCharToMultiByte(CP_ACP, 0, w_str, -1, str, GF_MAX_PATH, NULL, NULL);
}
void CE_CharToWide(char *str, unsigned short *w_str)
{
MultiByteToWideChar(CP_ACP, 0, str, -1, w_str, GF_MAX_PATH);
}
#endif
GF_EXPORT
void gf_rand_init(Bool Reset)
{
if (Reset) {
srand(1);
} else {
#if defined(_WIN32_WCE)
srand( (u32) GetTickCount() );
#else
srand( (u32) time(NULL) );
#endif
}
}
GF_EXPORT
u32 gf_rand()
{
return rand();
}
#ifndef _WIN32_WCE
#include <sys/stat.h>
#endif
GF_EXPORT
void gf_utc_time_since_1970(u32 *sec, u32 *msec)
{
#if defined (WIN32) && !defined(_WIN32_WCE)
struct _timeb tb;
_ftime( &tb );
*sec = (u32) tb.time;
*msec = tb.millitm;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
*sec = (u32) tv.tv_sec;
*msec = tv.tv_usec/1000;
#endif
}
GF_EXPORT
void gf_get_user_name(char buf[1024])
{
strcpy(buf, "gpac-user");
#if 0
s32 len;
char *t;
strcpy(buf, "");
len = 1024;
GetUserName(buf, &len);
if (!len) {
t = getenv("USER");
if (t) strcpy(buf, t);
}
#endif
#if 0
struct passwd *pw;
pw = getpwuid(getuid());
strcpy(buf, "");
if (pw && pw->pw_name) {
strncpy(name, pw->pw_name, 1023);
name[1023] = 0;
}
#endif
}
#ifndef WIN32
GF_EXPORT
char * my_str_upr(char *str)
{
u32 i;
for (i=0; i<strlen(str); i++) {
str[i] = toupper(str[i]);
}
return str;
}
GF_EXPORT
char * my_str_lwr(char *str)
{
u32 i;
for (i=0; i<strlen(str); i++) {
str[i] = tolower(str[i]);
}
return str;
}
#endif
/*seems OK under mingw also*/
#ifdef WIN32
#ifdef _WIN32_WCE
Bool gf_prompt_has_input()
{
return 0;
}
char gf_prompt_get_char() {
return 0;
}
GF_EXPORT
void gf_prompt_set_echo_off(Bool echo_off) {
return;
}
GF_Err gf_prompt_get_size(u32 *width, u32 *height)
{
return GF_NOT_SUPPORTED;
}
#else
#include <conio.h>
#include <windows.h>
Bool gf_prompt_has_input()
{
return kbhit();
}
char gf_prompt_get_char()
{
#ifdef GPAC_ENABLE_COVERAGE
if (gf_sys_is_cov_mode()) return 0;
#endif
return getchar();
}
GF_EXPORT
void gf_prompt_set_echo_off(Bool echo_off)
{
#ifdef GPAC_ENABLE_COVERAGE
if (gf_sys_is_cov_mode()) return;
#endif
DWORD flags;
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
BOOL ret = GetConsoleMode(hStdin, &flags);
if (!ret) {
DWORD err = GetLastError();
GF_LOG(GF_LOG_ERROR, GF_LOG_CONSOLE, ("[Console] GetConsoleMode() return with the following error code: %d\n", err));
return;
}
if (echo_off) flags &= ~ENABLE_ECHO_INPUT;
else flags |= ENABLE_ECHO_INPUT;
SetConsoleMode(hStdin, flags);
}
GF_EXPORT
GF_Err gf_prompt_get_size(u32 *width, u32 *height)
{
CONSOLE_SCREEN_BUFFER_INFO info;
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
BOOL ret = GetConsoleScreenBufferInfo(hStdin, &info);
if (!ret) {
DWORD err = GetLastError();
GF_LOG(GF_LOG_ERROR, GF_LOG_CONSOLE, ("[Console] GetConsoleScreenBufferInfo() return with the following error code: %d\n", err));
return GF_IO_ERR;
}
if (width) *width = info.dwSize.X;
if (height) *height = info.dwSize.Y;
return GF_OK;
}
#endif
#else
/*linux kbhit/getchar- borrowed on debian mailing lists, (author Mike Brownlow)*/
#include <termios.h>
#include <sys/ioctl.h>
static struct termios t_orig, t_new;
static s32 ch_peek = -1;
static void init_keyboard()
{
tcgetattr(0, &t_orig);
t_new = t_orig;
t_new.c_lflag &= ~ICANON;
t_new.c_lflag &= ~ECHO;
t_new.c_lflag &= ~ISIG;
t_new.c_cc[VMIN] = 1;
t_new.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &t_new);
}
static void close_keyboard(Bool new_line)
{
tcsetattr(0,TCSANOW, &t_orig);
if (new_line) fprintf(stderr, "\n");
}
GF_EXPORT
void gf_prompt_set_echo_off(Bool echo_off)
{
#ifdef GPAC_ENABLE_COVERAGE
if (gf_sys_is_cov_mode()) return;
#endif
init_keyboard();
if (echo_off) t_orig.c_lflag &= ~ECHO;
else t_orig.c_lflag |= ECHO;
close_keyboard(GF_FALSE);
}
GF_EXPORT
Bool gf_prompt_has_input()
{
u8 ch;
s32 nread;
pid_t fg = tcgetpgrp(STDIN_FILENO);
//we are not foreground nor piped (used for IDEs), can't read stdin
if ((fg!=-1) && (fg != getpgrp())) {
return GF_FALSE;
}
init_keyboard();
if (ch_peek != -1) return GF_TRUE;
t_new.c_cc[VMIN]=0;
tcsetattr(0, TCSANOW, &t_new);
nread = (s32) read(0, &ch, 1);
t_new.c_cc[VMIN]=1;
tcsetattr(0, TCSANOW, &t_new);
if(nread == 1) {
ch_peek = ch;
return GF_TRUE;
}
close_keyboard(GF_FALSE);
return GF_FALSE;
}
GF_EXPORT
char gf_prompt_get_char()
{
char ch;
#ifdef GPAC_ENABLE_COVERAGE
if (gf_sys_is_cov_mode()) return 0;
#endif
if (ch_peek != -1) {
ch = ch_peek;
ch_peek = -1;
close_keyboard(GF_TRUE);
return ch;
}
if (0==read(0,&ch,1))
ch = 0;
close_keyboard(GF_TRUE);
return ch;
}
GF_EXPORT
GF_Err gf_prompt_get_size(u32 *width, u32 *height)
{
#if defined(TIOCGWINSZ)
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != 0) return GF_IO_ERR;
if (width) *width = ws.ws_col;
if (height) *height = ws.ws_row;
return GF_OK;
#elif defined(WIOCGETD)
struct uwdata w;
if (ioctl(2, WIOCGETD, &w) != 0) return GF_IO_ERR;
if (width && (w.uw_width > 0))
*width = w.uw_width / w.uw_hs;
if (height && (w.uw_height > 0))
*height = w.uw_height / w.uw_vs;
return GF_OK;
#else
return GF_NOT_SUPPORTED;
#endif
}
#endif
static u32 sys_init = 0;
static u32 last_update_time = 0;
static u64 last_process_k_u_time = 0;
GF_SystemRTInfo the_rti;
#if defined(_WIN32_WCE)
static LARGE_INTEGER frequency , init_counter;
static u64 last_total_k_u_time = 0;
static u32 mem_usage_at_startup = 0;
#ifndef GetCurrentPermissions
DWORD GetCurrentPermissions();
#endif
#ifndef SetProcPermissions
void SetProcPermissions(DWORD );
#endif
#elif defined(WIN32)
static LARGE_INTEGER frequency , init_counter;
static u64 last_proc_idle_time = 0;
static u64 last_proc_k_u_time = 0;
static HINSTANCE psapi_hinst = NULL;
typedef BOOL(WINAPI* NTGetSystemTimes)(VOID *,VOID *,VOID *);
NTGetSystemTimes MyGetSystemTimes = NULL;
typedef BOOL(WINAPI* NTGetProcessMemoryInfo)(HANDLE,VOID *,DWORD);
NTGetProcessMemoryInfo MyGetProcessMemoryInfo = NULL;
typedef int(WINAPI* NTQuerySystemInfo)(ULONG,PVOID,ULONG,PULONG);
NTQuerySystemInfo MyQuerySystemInfo = NULL;
#ifndef PROCESS_MEMORY_COUNTERS
typedef struct _PROCESS_MEMORY_COUNTERS
{
DWORD cb;
DWORD PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;
#endif
#ifndef SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
{
LARGE_INTEGER IdleTime;
LARGE_INTEGER KernelTime;
LARGE_INTEGER UserTime;
LARGE_INTEGER Reserved1[2];
ULONG Reserved2;
} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
#endif
#else
static u64 last_cpu_u_k_time = 0;
static u64 last_cpu_idle_time = 0;
static u64 mem_at_startup = 0;
#endif
#ifdef WIN32
static u32 (*OS_GetSysClock)();
u32 gf_sys_clock()
{
return OS_GetSysClock();
}
static u64 (*OS_GetSysClockHR)();
u64 gf_sys_clock_high_res()
{
return OS_GetSysClockHR();
}
#endif
#ifdef WIN32
static u32 OS_GetSysClockHIGHRES()
{
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
now.QuadPart -= init_counter.QuadPart;
return (u32) ((now.QuadPart * 1000) / frequency.QuadPart);
}
static u64 OS_GetSysClockHIGHRES_FULL()
{
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
now.QuadPart -= init_counter.QuadPart;
return (u64) ((now.QuadPart * 1000000) / frequency.QuadPart);
}
static u32 OS_GetSysClockNORMAL()
{
#ifdef _WIN32_WCE
return GetTickCount();
#else
return timeGetTime();
#endif
}
static u64 OS_GetSysClockNORMAL_FULL()
{
u64 res = OS_GetSysClockNORMAL();
return res*1000;
}
#endif /* WIN32 */
#if defined(__sh__)
/* Avoid exception for denormalized floating point values */
static int
sh4_get_fpscr()
{
int ret;
asm volatile ("sts fpscr,%0" : "=r" (ret));
return ret;
}
static void
sh4_put_fpscr(int nv)
{
asm volatile ("lds %0,fpscr" : : "r" (nv));
}
#define SH4_FPSCR_FR 0x00200000
#define SH4_FPSCR_SZ 0x00100000
#define SH4_FPSCR_PR 0x00080000
#define SH4_FPSCR_DN 0x00040000
#define SH4_FPSCR_RN 0x00000003
#define SH4_FPSCR_RN_N 0
#define SH4_FPSCR_RN_Z 1
extern int __fpscr_values[2];
void
sh4_change_fpscr(int off, int on)
{
int b = sh4_get_fpscr();
off = ~off;
off |= 0x00180000;
on &= ~ 0x00180000;
b &= off;
b |= on;
sh4_put_fpscr(b);
__fpscr_values[0] &= off;
__fpscr_values[0] |= on;
__fpscr_values[1] &= off;
__fpscr_values[1] |= on;
}
#endif
GF_EXPORT
struct tm *gf_gmtime(const time_t *time)
{
#ifdef _WIN32_WCE
FILETIME filet;
LPSYSTEMTIME syst;
*(LONGLONG *) &filet = (sec/* - GF_NTP_SEC_1900_TO_1970*/) * 10000000 + TIMESPEC_TO_FILETIME_OFFSET;
FileTimeToSystemTime(&filet, &syst);
if (syst.wSecond>60)
syst.wSecond=60;
#endif
struct tm *tm = gmtime(time);
//see issue #859, no clue how this happened...
if (tm->tm_sec>60)
tm->tm_sec = 60;
return tm;
}
#ifdef GPAC_MEMORY_TRACKING
void gf_mem_enable_tracker(u32 mem_track_type);
#endif
static u64 memory_at_gpac_startup = 0;
static u32 gpac_argc = 0;
const char **gpac_argv = NULL;
Bool *gpac_argv_state = NULL;
static Bool gpac_test_mode = GF_FALSE;
static Bool gpac_old_arch = GF_FALSE;
static Bool gpac_discard_config = GF_FALSE;
//in error.c
#ifndef GPAC_DISABLE_LOG
extern FILE *gpac_log_file;
extern Bool gpac_log_time_start;
extern Bool gpac_log_utc_time;
#endif
GF_EXPORT
Bool gf_log_use_file()
{
#ifndef GPAC_DISABLE_LOG
return gpac_log_file ? GF_TRUE : GF_FALSE;
#else
return GF_FALSE;
#endif
}
static void progress_quiet(const void *cbck, const char *title, u64 done, u64 total) { }
void gpac_disable_progress()
{
gf_set_progress_callback(NULL, progress_quiet);
}
GF_EXPORT
Bool gf_sys_is_test_mode()
{
return gpac_test_mode;
}
GF_EXPORT
Bool gf_sys_old_arch_compat()
{
return gpac_old_arch;
}
#ifdef GPAC_ENABLE_COVERAGE
GF_EXPORT
Bool gf_sys_is_cov_mode()
{
return gpac_test_mode;
}
#endif
const char *gpac_log_file_name=NULL;
#ifndef GPAC_DISABLE_LOG
extern Bool gpac_log_dual;
#ifdef GPAC_CONFIG_EMSCRIPTEN
extern Bool gpac_log_console;
#endif
#endif
GF_EXPORT
void gf_log_reset_file()
{
#ifndef GPAC_DISABLE_LOG
if (gpac_log_file_name) {
if (gpac_log_file) gf_fclose(gpac_log_file);
#ifdef GPAC_CONFIG_EMSCRIPTEN
gpac_log_console = GF_FALSE;
if (gpac_log_file_name && !strcmp(gpac_log_file_name, "console")) {
gpac_log_file = NULL;
gpac_log_console = GF_TRUE;
return;
}
#endif
gpac_log_file = gf_fopen(gpac_log_file_name, "wt");
}
#endif
}
static Bool gpac_has_global_filter_args=GF_FALSE;
static Bool gpac_has_global_filter_meta_args=GF_FALSE;
#include <gpac/thread.h>
GF_Mutex *logs_mx = NULL;
Bool gf_sys_has_filter_global_args()
{
return gpac_has_global_filter_args;
}
Bool gf_sys_has_filter_global_meta_args()
{
return gpac_has_global_filter_meta_args;
}
static u32 gpac_quiet = 0;
char gf_prog_lf = '\r';
#ifndef GPAC_DISABLE_NETWORK
extern Bool gpac_use_poll;
#endif
GF_EXPORT
GF_Err gf_sys_set_args(s32 argc, const char **argv)
{
s32 i;
if (!gpac_argc) {
Bool gf_opts_load_option(const char *arg_name, const char *val, Bool *consumed_next, GF_Err *e);
#ifdef GPAC_ENABLE_RESTRICT
void gf_cfg_load_restrict();
#endif
#ifndef GPAC_DISABLE_NETWORK
gpac_use_poll = GF_TRUE;
#endif
for (i=1; i<argc; i++) {
Bool consumed;
GF_Err e;
Bool use_sep = GF_FALSE;
Bool bool_value = GF_TRUE;
char szArg[1024];
char *arg_val;
const char *arg = argv[i];
if (!arg) continue;
arg_val = strchr(arg, '=');
if (arg_val) {
u32 len = (u32) (arg_val-arg);
if (len>1023) len = 1023;
strncpy(szArg, arg, len);
szArg[len]=0;
arg_val++;
arg = szArg;
use_sep = GF_TRUE;
} else if (i+1<argc) {
arg_val = (char *) argv[i+1];
}
if ((arg[0] != '-') || ! arg[1]) {
continue;
}
if (arg_val && (!strcmp(arg_val, "no") || !strcmp(arg_val, "false") || !strcmp(arg_val, "0") ) )
bool_value = GF_FALSE;
if (arg[1]=='-') {
gpac_has_global_filter_args = GF_TRUE;
} else if (arg[1]=='+') {
gpac_has_global_filter_meta_args = GF_TRUE;
} else if (!strcmp(arg, "-log-file") || !strcmp(arg, "-lf")) {
#ifndef GPAC_DISABLE_LOG
gpac_log_file_name = arg_val;
#endif
if (!use_sep) i += 1;
} else if (!strcmp(arg, "-log-dual") || !strcmp(arg, "-ld")) {
#ifndef GPAC_DISABLE_LOG