forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.h
248 lines (221 loc) · 5.39 KB
/
misc.h
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
///
/// @file misc.h @brief Misc function header file
///
/// Copyright (c) 2009 - 2012 by Lutz Sammer. All Rights Reserved.
///
/// Contributor(s):
/// Copied from uwm.
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// This program 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 Affero General Public License for more details.
///
//////////////////////////////////////////////////////////////////////////////
/// @addtogroup misc
/// @{
extern int SysLogLevel; ///< how much information wanted
#ifdef __cplusplus
extern "C"
{
#endif
#include <syslog.h>
#include <stdarg.h>
#include <time.h> // clock_gettime
#include <sys/syscall.h>
//////////////////////////////////////////////////////////////////////////////
// Defines
//////////////////////////////////////////////////////////////////////////////
#define L_DEBUG (1 << 0)
#define L_AV_SYNC (1 << 1)
#define L_SOUND (1 << 2)
#define L_OSD (1 << 3)
#define L_DRM (1 << 4)
#define L_CODEC (1 << 5)
#define L_STILL (1 << 6)
#define L_TRICK (1 << 7)
#define L_MEDIA (1 << 8)
#define L_OPENGL (1 << 9)
#define L_OPENGL_TIME (1 << 10)
#define L_OPENGL_TIME_ALL (1 << 11)
typedef unsigned char uchar;
/**
** Show error.
*/
#define Error(fmt...) (void)( (SysLogLevel > 0) ? Syslog(LOG_ERR, 0, fmt) : (void)0 )
/**
** Show fatal error.
*/
#define Fatal(fmt...) do { Error(fmt); abort(); } while (0)
/**
** Show warning.
*/
#define Warning(fmt...) (void)( (SysLogLevel > 1) ? Syslog(LOG_WARNING, 0, fmt) : (void)0 )
/**
** Show info.
*/
#define Info(fmt...) (void)( (SysLogLevel > 2) ? Syslog(LOG_INFO, 0, fmt) : (void)0 )
/**
** Show debug.
*/
#define Debug(fmt...) Syslog(LOG_DEBUG, L_DEBUG, fmt)
/**
** Show debug with logging category.
*/
#define Debug2(cat, fmt...) Syslog(LOG_DEBUG, cat, fmt)
#ifndef AV_NOPTS_VALUE
#define AV_NOPTS_VALUE INT64_C(0x8000000000000000)
#endif
//////////////////////////////////////////////////////////////////////////////
// Prototypes
//////////////////////////////////////////////////////////////////////////////
static inline void Syslog(const int, const int, const char *format, ...)
__attribute__ ((format(printf, 3, 4)));
//////////////////////////////////////////////////////////////////////////////
// Inlines
//////////////////////////////////////////////////////////////////////////////
/**
** Syslog output function.
*/
static inline void Syslog(const int level, const int cat, const char *format, ...)
{
int DebugLogLevel = 0;
#ifdef DEBUG
DebugLogLevel |= L_DEBUG;
#endif
#ifdef AV_SYNC_DEBUG
DebugLogLevel |= L_AV_SYNC;
#endif
#ifdef SOUND_DEBUG
DebugLogLevel |= L_SOUND;
#endif
#ifdef OSD_DEBUG
DebugLogLevel |= L_OSD;
#endif
#ifdef DRM_DEBUG
DebugLogLevel |= L_DRM;
#endif
#ifdef CODEC_DEBUG
DebugLogLevel |= L_CODEC;
#endif
#ifdef STILL_DEBUG
DebugLogLevel |= L_STILL;
#endif
#ifdef TRICK_DEBUG
DebugLogLevel |= L_TRICK;
#endif
#ifdef MEDIA_DEBUG
DebugLogLevel |= L_MEDIA;
#endif
#ifdef GL_DEBUG
DebugLogLevel |= L_OPENGL;
#endif
#ifdef GL_DEBUG_TIME
DebugLogLevel |= L_OPENGL_TIME;
#endif
#ifdef GL_DEBUG_TIME_ALL
DebugLogLevel |= L_OPENGL_TIME_ALL;
#endif
va_list ap;
char fmt[256];
char prefix[20] = "";
if (level == LOG_DEBUG) {
switch (DebugLogLevel & cat) {
case L_DEBUG:
break;
case L_AV_SYNC:
strcpy(prefix, "[AV_Sync]");
break;
case L_SOUND:
strcpy(prefix, "[Sound]");
break;
case L_OSD:
strcpy(prefix, "[Osd]");
break;
case L_DRM:
strcpy(prefix, "[Drm]");
break;
case L_CODEC:
strcpy(prefix, "[Codec]");
break;
case L_STILL:
strcpy(prefix, "[Still]");
break;
case L_TRICK:
strcpy(prefix, "[Trick]");
break;
case L_MEDIA:
strcpy(prefix, "[Media]");
break;
case L_OPENGL:
case L_OPENGL_TIME:
case L_OPENGL_TIME_ALL:
strcpy(prefix, "[OpenGL]");
break;
default:
return;
}
}
pid_t threadId = syscall(__NR_gettid);
snprintf(fmt, sizeof(fmt), "[%d] [softhddevice]%s %s", threadId, prefix, format);
va_start(ap, format);
vsyslog(level, fmt, ap);
va_end(ap);
}
/**
** Nice time-stamp string.
**
** @param ts time stamp
*/
static inline const char *Timestamp2String(int64_t ts)
{
static char buf[3][16];
static int idx = 0;
if (ts == (int64_t) AV_NOPTS_VALUE) {
return "--:--:--.---";
}
idx = (idx + 1) % 3;
snprintf(buf[idx], sizeof(buf[idx]), "%2d:%02d:%02d.%03d",
(int)(ts / (3600000)), (int)((ts / (60000)) % 60),
(int)((ts / (1000)) % 60), (int)(ts % 1000));
return buf[idx];
}
/**
** Get ticks in ms.
**
** @returns ticks in ms,
*/
static inline uint32_t GetMsTicks(void)
{
#ifdef CLOCK_MONOTONIC
struct timespec tspec;
clock_gettime(CLOCK_MONOTONIC, &tspec);
return (tspec.tv_sec * 1000) + (tspec.tv_nsec / (1000 * 1000));
#else
struct timeval tval;
if (gettimeofday(&tval, NULL) < 0) {
return 0;
}
return (tval.tv_sec * 1000) + (tval.tv_usec / 1000);
#endif
}
/**
** Read the PES header length from PES header.
**
** @returns length
*/
static inline int PesHeadLength(const uint8_t *p)
{
return 9 + p[8];
}
#ifdef __cplusplus
}
#endif
/// @}