]> git.ipfire.org Git - thirdparty/tvheadend.git/blob
c2fcee716
[thirdparty/tvheadend.git] /
1 /*
2 * Tvheadend - structures
3 * Copyright (C) 2007 Andreas Ă–man
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef TVHEADEND_H
19 #define TVHEADEND_H
20
21 #include "build.h"
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <netinet/in.h>
27 #include <sys/time.h>
28 #include <libgen.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include "queue.h"
34 #include "tvh_thread.h"
35 #include "tvh_string.h"
36 #include "hts_strtab.h"
37 #include "htsmsg.h"
38 #include "tprofile.h"
39 #include "tvhlog.h"
40
41 #include "redblack.h"
42
43 #include "tvh_locale.h"
44
45 #define ERRNO_AGAIN(e) ((e) == EAGAIN || (e) == EINTR || (e) == EWOULDBLOCK)
46
47 #include "compat.h"
48
49 typedef struct {
50 const char *name;
51 const uint32_t *enabled;
52 } tvh_caps_t;
53
54 extern int tvheadend_running;
55 extern const char *tvheadend_version;
56 extern const char *tvheadend_cwd;
57 extern const char *tvheadend_webroot;
58 extern const tvh_caps_t tvheadend_capabilities[];
59
60 static inline int tvheadend_is_running(void)
61 {
62 return atomic_get(&tvheadend_running);
63 }
64
65 htsmsg_t *tvheadend_capabilities_list(int check);
66
67 typedef struct str_list
68 {
69 int max;
70 int num;
71 char **str;
72 } str_list_t;
73
74 #define PTS_UNSET INT64_C(0x8000000000000000)
75 #define PTS_MASK INT64_C(0x00000001ffffffff)
76
77 extern tvh_mutex_t global_lock;
78 extern tvh_mutex_t tasklet_lock;
79 extern tvh_mutex_t fork_lock;
80
81 extern int tvheadend_webui_port;
82 extern int tvheadend_webui_debug;
83 extern int tvheadend_htsp_port;
84
85 #if defined(__has_feature)
86 #if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer)
87 #define CLANG_SANITIZER 1
88 #endif
89 #endif
90
91 /*
92 *
93 */
94 #define UILEVEL_DEFAULT (-1)
95 #define UILEVEL_BASIC 0
96 #define UILEVEL_ADVANCED 1
97 #define UILEVEL_EXPERT 2
98
99 /*
100 *
101 */
102 #define CHICON_NONE 0
103 #define CHICON_LOWERCASE 1
104 #define CHICON_SVCNAME 2
105
106 /*
107 *
108 */
109 #define PICON_STANDARD 0
110 #define PICON_ISVCTYPE 1
111
112 /*
113 * timer support functions
114 */
115
116 #if ENABLE_GTIMER_CHECK
117 #define GTIMER_TRACEID_ const char *id,
118 #define GTIMER_FCN(n) check_##n
119 #else
120 #define GTIMER_TRACEID_
121 #define GTIMER_FCN(n) n
122 #endif
123
124 /*
125 * global timer - monotonic
126 */
127
128 typedef void (mti_callback_t)(void *opaque);
129
130 #define MTIMER_MAGIC1 0x0d62a9de
131
132 typedef struct mtimer {
133 LIST_ENTRY(mtimer) mti_link;
134 #if ENABLE_TRACE
135 uint32_t mti_magic1;
136 #endif
137 mti_callback_t *mti_callback;
138 void *mti_opaque;
139 int64_t mti_expire;
140 #if ENABLE_GTIMER_CHECK
141 const char *mti_id;
142 #endif
143 } mtimer_t;
144
145 void GTIMER_FCN(mtimer_arm_rel)
146 (GTIMER_TRACEID_ mtimer_t *mti, mti_callback_t *callback, void *opaque, int64_t delta);
147 void GTIMER_FCN(mtimer_arm_abs)
148 (GTIMER_TRACEID_ mtimer_t *mti, mti_callback_t *callback, void *opaque, int64_t when);
149
150 #if ENABLE_GTIMER_CHECK
151 #define mtimer_arm_rel(a, b, c, d) GTIMER_FCN(mtimer_arm_rel)(SRCLINEID(), a, b, c, d)
152 #define mtimer_arm_abs(a, b, c, d) GTIMER_FCN(mtimer_arm_abs)(SRCLINEID(), a, b, c, d)
153 #endif
154
155 void mtimer_disarm(mtimer_t *mti);
156
157
158
159 /*
160 * global timer (based on the current system time - time())
161 */
162
163 #define GTIMER_MAGIC1 0x8a6f238f
164
165 typedef void (gti_callback_t)(void *opaque);
166
167 typedef struct gtimer {
168 LIST_ENTRY(gtimer) gti_link;
169 #if ENABLE_TRACE
170 uint32_t gti_magic1;
171 #endif
172 gti_callback_t *gti_callback;
173 void *gti_opaque;
174 time_t gti_expire;
175 #if ENABLE_GTIMER_CHECK
176 const char *gti_id;
177 #endif
178 } gtimer_t;
179
180 void GTIMER_FCN(gtimer_arm_rel)
181 (GTIMER_TRACEID_ gtimer_t *gti, gti_callback_t *callback, void *opaque, time_t delta);
182 void GTIMER_FCN(gtimer_arm_absn)
183 (GTIMER_TRACEID_ gtimer_t *gti, gti_callback_t *callback, void *opaque, time_t when);
184
185 #if ENABLE_GTIMER_CHECK
186 #define gtimer_arm_rel(a, b, c, d) GTIMER_FCN(gtimer_arm_rel)(SRCLINEID(), a, b, c, d)
187 #define gtimer_arm_absn(a, b, c, d) GTIMER_FCN(gtimer_arm_absn)(SRCLINEID(), a, b, c, d)
188 #endif
189
190 void gtimer_disarm(gtimer_t *gti);
191
192
193 /*
194 * tasklet
195 */
196
197 typedef void (tsk_callback_t)(void *opaque, int disarmed);
198
199 typedef struct tasklet {
200 TAILQ_ENTRY(tasklet) tsk_link;
201 tsk_callback_t *tsk_callback;
202 void *tsk_opaque;
203 void (*tsk_free)(void *);
204 } tasklet_t;
205
206 tasklet_t *tasklet_arm_alloc(tsk_callback_t *callback, void *opaque);
207 void tasklet_arm(tasklet_t *tsk, tsk_callback_t *callback, void *opaque);
208 void tasklet_disarm(tasklet_t *gti);
209
210 /**
211 *
212 */
213 #define TVH_KILL_KILL 0
214 #define TVH_KILL_TERM 1
215 #define TVH_KILL_INT 2
216 #define TVH_KILL_HUP 3
217 #define TVH_KILL_USR1 4
218 #define TVH_KILL_USR2 5
219
220 int tvh_kill_to_sig(int tvh_kill);
221
222 #define MIN(a,b) ((a) < (b) ? (a) : (b))
223 #define MAX(a,b) ((a) > (b) ? (a) : (b))
224 #define MINMAX(a,mi,ma) MAX(mi, MIN(ma, a))
225 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
226
227 int sri_to_rate(int sri);
228 int rate_to_sri(int rate);
229
230 typedef struct th_pipe
231 {
232 int rd;
233 int wr;
234 } th_pipe_t;
235
236 void doexit(int x);
237
238 int tvh_open(const char *pathname, int flags, mode_t mode);
239
240 int tvh_socket(int domain, int type, int protocol);
241
242 int tvh_pipe(int flags, th_pipe_t *pipe);
243
244 void tvh_pipe_close(th_pipe_t *pipe);
245
246 int tvh_write(int fd, const void *buf, size_t len);
247
248 int tvh_write_in_chunks(int fd, const void *buf, size_t len, size_t chunkSize);
249
250 int tvh_nonblock_write(int fd, const void *buf, size_t len);
251
252 FILE *tvh_fopen(const char *filename, const char *mode);
253
254 void hexdump(const char *pfx, const uint8_t *data, int len);
255
256 uint32_t tvh_crc32(const uint8_t *data, size_t datalen, uint32_t crc);
257
258 int base64_decode(uint8_t *out, const char *in, int out_size);
259
260 char *base64_encode(char *out, int out_size, const uint8_t *in, int in_size);
261
262 /* Calculate the output size needed to base64-encode x bytes. */
263 #define BASE64_SIZE(x) (((x)+2) / 3 * 4 + 1)
264
265 static inline int64_t ts_rescale(int64_t ts, int tb)
266 {
267 // return (ts * tb + (tb / 2)) / 90000LL;
268 return (ts * tb ) / 90000LL;
269 }
270
271 static inline int64_t ts_rescale_inv(int64_t ts, int tb)
272 {
273 return (ts * 90000LL) / tb;
274 }
275
276 char *md5sum ( const char *str, int lowercase );
277 char *sha256sum ( const char *str, int lowercase );
278 char *sha512sum256 ( const char *str, int lowercase );
279 char *sha256sum_base64 ( const char *str );
280
281 int makedirs ( int subsys, const char *path, int mode, int mstrict, gid_t gid, uid_t uid );
282
283 int rmtree ( const char *path );
284
285 char *regexp_escape ( const char *str );
286
287 #if ENABLE_ZLIB
288 uint8_t *tvh_gzip_inflate ( const uint8_t *data, size_t size, size_t orig );
289 uint8_t *tvh_gzip_deflate ( const uint8_t *data, size_t orig, size_t *size );
290 int tvh_gzip_deflate_fd ( int fd, const uint8_t *data, size_t orig, size_t *size, int speed );
291 int tvh_gzip_deflate_fd_header ( int fd, const uint8_t *data, size_t orig, size_t *size, int speed , const char *signature);
292 #endif
293
294 /* URL decoding */
295 char to_hex(char code);
296 char *url_encode(const char *str);
297 void http_deescape(char *str);
298
299 int mpegts_word_count(const uint8_t *tsb, int len, uint32_t mask);
300
301 int deferred_unlink(const char *filename, const char *rootdir);
302 void dvr_cutpoint_delete_files (const char *s);
303
304 void sha1_calc(uint8_t *dst, const uint8_t *d1, size_t d1_len, const uint8_t *d2, size_t d2_len);
305
306 uint32_t gcdU32(uint32_t a, uint32_t b);
307 static inline int32_t deltaI32(int32_t a, int32_t b) { return (a > b) ? (a - b) : (b - a); }
308 static inline uint32_t deltaU32(uint32_t a, uint32_t b) { return (a > b) ? (a - b) : (b - a); }
309
310 #define SKEL_DECLARE(name, type) type *name;
311 #define SKEL_ALLOC(name) do { if (!name) name = calloc(1, sizeof(*name)); } while (0)
312 #define SKEL_USED(name) do { name = NULL; } while (0)
313 #define SKEL_FREE(name) do { free(name); name = NULL; } while (0)
314
315 htsmsg_t *network_interfaces_enum(void *obj, const char *lang);
316
317 const char *gmtime2local(time_t gmt, char *buf, size_t buflen);
318
319 /* glibc wrapper */
320 #if ! ENABLE_QSORT_R
321 void
322 qsort_r(void *base, size_t nmemb, size_t size,
323 int (*cmp)(const void *, const void *, void *), void *aux);
324 #endif /* ENABLE_QSORT_R */
325
326 void tvh_qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg);
327
328 /* printing */
329 #ifndef __WORDSIZE
330 # if ULONG_MAX == 0xffffffffffffffff
331 # define __WORDSIZE 64
332 # elif ULONG_MAX == 0xffffffff
333 # define __WORDSIZE 32
334 # endif /* ULONG_MAX */
335 #endif /* __WORDSIZE */
336
337 #if __WORDSIZE == 32
338 # define PRItime_t "lld"
339 #elif __WORDSIZE == 64
340 # define PRItime_t "ld"
341 #else
342 # error "__WORDSIZE not properly defined"
343 #endif
344
345 /* transcoding */
346 #define TVH_NAME_LEN 32
347 #define TVH_TITLE_LEN 256
348
349 /* sanitizer helpers */
350 #if ENABLE_CCLANG_THREADSAN
351 void *blacklisted_memcpy(void *dest, const void *src, size_t n);
352 int blacklisted_close(int fildes);
353 #else
354 #define blacklisted_memcpy memcpy
355 #define blacklisted_close close
356 #endif
357
358 #endif /* TVHEADEND_H */