Fork me on GitHub
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
12#ifndef JANUS_UTILS_H
13#define JANUS_UTILS_H
14
15#include <stdint.h>
16#include <glib.h>
17#include <jansson.h>
18
19#define JANUS_JSON_STRING JSON_STRING
20#define JANUS_JSON_INTEGER JSON_INTEGER
21#define JANUS_JSON_OBJECT JSON_OBJECT
22#define JANUS_JSON_ARRAY JSON_ARRAY
23/* Use JANUS_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
24#define JANUS_JSON_BOOL JSON_TRUE
25#define JANUS_JSON_PARAM_REQUIRED 1
26#define JANUS_JSON_PARAM_POSITIVE 2
27#define JANUS_JSON_PARAM_NONEMPTY 4
28
30 const gchar *name;
31 json_type jtype;
32 unsigned int flags;
33};
34
35#ifndef htonll
36#define htonll(x) ((1==htonl(1)) ? (x) : ((guint64)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
37#endif
38#ifndef ntohll
39#define ntohll(x) ((1==ntohl(1)) ? (x) : ((guint64)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
40#endif
41
42
46gint64 janus_get_monotonic_time(void);
47
51gint64 janus_get_real_time(void);
52
59char *janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT;
60
66size_t janus_strlcat(char *dest, const char *src, size_t dest_size);
67
76int janus_strlcat_fast(char *dest, const char *src, size_t dest_size, size_t *offset);
77
81gboolean janus_is_true(const char *value);
82
87gboolean janus_strcmp_const_time(const void *str1, const void *str2);
88
93guint32 janus_random_uint32(void);
94
101guint64 janus_random_uint64_full(void);
102
115guint64 janus_random_uint64(void);
116
121char *janus_random_uuid(void);
122
130guint64 *janus_uint64_dup(guint64 num);
131
136guint64 janus_uint64_hash(guint64 num);
137
143int janus_string_to_uint8(const char *str, uint8_t *num);
144
150int janus_string_to_uint16(const char *str, uint16_t *num);
151
157int janus_string_to_uint32(const char *str, uint32_t *num);
158
162
163typedef gsize janus_flags;
164
167void janus_flags_reset(janus_flags *flags);
168
172void janus_flags_set(janus_flags *flags, gsize flag);
173
177void janus_flags_clear(janus_flags *flags, gsize flag);
178
183gboolean janus_flags_is_set(janus_flags *flags, gsize flag);
185
191int janus_mkdir(const char *dir, mode_t mode);
192
198gchar *janus_make_absolute_path(const gchar *base_dir, const gchar *path);
199
204int janus_get_codec_pt(const char *sdp, const char *codec);
205
210const char *janus_get_codec_from_pt(const char *sdp, int pt);
211
215int janus_pidfile_create(const char *file);
216
219int janus_pidfile_remove(void);
220
224void janus_protected_folder_add(const char *folder);
225
229gboolean janus_is_folder_protected(const char *path);
230
233
238void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
239
245gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
246
257#define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
258 do { \
259 error_code = 0; \
260 unsigned int i; \
261 for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
262 json_t *val = json_object_get(obj, params[i].name); \
263 if(!val) { \
264 if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
265 error_code = (missing_code); \
266 if(log_error) \
267 JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
268 if(error_cause != NULL) \
269 g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
270 break; \
271 } \
272 continue; \
273 } \
274 if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
275 error_code = (invalid_code); \
276 char type_name[20]; \
277 janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
278 if(log_error) \
279 JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
280 if(error_cause != NULL) \
281 g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
282 break; \
283 } \
284 } \
285 } while(0)
286
295#define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
296 JANUS_VALIDATE_JSON_OBJECT_FORMAT("Missing mandatory element (%s)", "Invalid element type (%s should be %s)", obj, params, error_code, error_cause, log_error, missing_code, invalid_code)
297
307#define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
308 do { \
309 if (secret) { \
310 static struct janus_json_parameter secret_parameters[] = { \
311 {member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
312 }; \
313 JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
314 if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
315 error_code = (unauthorized_code); \
316 JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
317 if(error_cause != NULL) \
318 g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
319 } \
320 } \
321 } while(0)
322
327gboolean janus_vp8_is_keyframe(const char *buffer, int len);
328
333gboolean janus_vp9_is_keyframe(const char *buffer, int len);
334
343gboolean janus_h264_is_keyframe(const char *buffer, int len);
344
349gboolean janus_h264_is_i_frame(const char *buffer, int len);
350
355gboolean janus_h264_is_b_frame(const char *buffer, int len);
356
361gboolean janus_av1_is_keyframe(const char *buffer, int len);
362
367gboolean janus_h265_is_keyframe(const char *buffer, int len);
368
374
378
389int janus_vp8_parse_descriptor(char *buffer, int len,
390 gboolean *m, uint16_t *picid, uint8_t *tl0picidx, uint8_t *tid, uint8_t *y, uint8_t *keyidx);
391
397void janus_vp8_simulcast_descriptor_update(char *buffer, int len, janus_vp8_simulcast_context *context, gboolean switched);
398
404
411int janus_vp9_parse_svc(char *buffer, int len, gboolean *found, janus_vp9_svc_info *info);
412
414typedef struct janus_red_block {
415 uint8_t pt;
416 uint32_t ts_offset;
417 uint8_t *data;
418 uint16_t length;
425GList *janus_red_parse_blocks(char *buffer, int len);
431int janus_red_pack_blocks(char *buffer, int len, GList *blocks);
437int janus_red_replace_block_pt(char *buffer, int len, int pt);
438
444guint32 janus_push_bits(guint32 word, size_t num, guint32 val);
445
451void janus_set1(guint8 *data, size_t i, guint8 val);
452
458void janus_set2(guint8 *data, size_t i, guint32 val);
459
465void janus_set3(guint8 *data, size_t i, guint32 val);
466
472void janus_set4(guint8 *data, size_t i, guint32 val);
473
478uint8_t janus_bitstream_getbit(uint8_t *base, uint32_t offset);
484uint32_t janus_bitstream_getbits(uint8_t *base, uint8_t num, uint32_t *offset);
485
496size_t janus_gzip_compress(int compression, char *text, size_t tlen, char *compressed, size_t zlen);
497
498#endif
struct json_t json_t
Definition plugin.h:236
Definition utils.h:29
json_type jtype
Definition utils.h:31
const gchar * name
Definition utils.h:30
unsigned int flags
Definition utils.h:32
Helper struct to address a specific RED block.
Definition utils.h:414
uint16_t length
Definition utils.h:418
uint8_t * data
Definition utils.h:417
uint8_t pt
Definition utils.h:415
uint32_t ts_offset
Definition utils.h:416
VP8 simulcasting context, in order to make sure SSRC changes result in coherent picid/temporal level ...
Definition utils.h:370
uint16_t last_picid
Definition utils.h:371
uint8_t base_tlzi
Definition utils.h:372
uint8_t last_tlzi
Definition utils.h:372
uint8_t base_tlzi_prev
Definition utils.h:372
uint16_t base_picid
Definition utils.h:371
uint16_t base_picid_prev
Definition utils.h:371
VP9 SVC info, as parsed from a payload descriptor.
Definition utils.h:400
int temporal_layer
Definition utils.h:401
uint8_t dbit
Definition utils.h:402
uint8_t pbit
Definition utils.h:402
uint8_t ebit
Definition utils.h:402
int spatial_layer
Definition utils.h:401
uint8_t bbit
Definition utils.h:402
uint8_t fbit
Definition utils.h:402
uint8_t ubit
Definition utils.h:402
gboolean janus_av1_is_keyframe(const char *buffer, int len)
Helper method to check if an AV1 frame is a keyframe or not.
Definition utils.c:901
guint64 janus_random_uint64(void)
Helper to generate random 52 bit unsigned integers.
Definition utils.c:92
gsize janus_flags
Janus flags container.
Definition utils.h:163
gboolean janus_vp8_is_keyframe(const char *buffer, int len)
Helper method to check if a VP8 frame is a keyframe or not.
Definition utils.c:696
void janus_set4(guint8 *data, size_t i, guint32 val)
Helper method to set four bytes at a memory position.
Definition utils.c:1390
gboolean janus_h264_is_keyframe(const char *buffer, int len)
Helper method to check if an H.264 frame is a keyframe or not.
Definition utils.c:889
struct janus_vp8_simulcast_context janus_vp8_simulcast_context
VP8 simulcasting context, in order to make sure SSRC changes result in coherent picid/temporal level ...
size_t janus_gzip_compress(int compression, char *text, size_t tlen, char *compressed, size_t zlen)
Helper method to compress a string to gzip (using zlib)
Definition utils.c:1411
guint32 janus_random_uint32(void)
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)
Definition utils.c:74
char * janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT
Helper to replace strings.
Definition utils.c:204
int janus_string_to_uint16(const char *str, uint16_t *num)
Helper method to convert a string to a uint16_t.
Definition utils.c:158
gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags)
Checks whether the JSON value matches the type and constraint.
Definition utils.c:656
int janus_mkdir(const char *dir, mode_t mode)
Helper to create a new directory, and recursively create parent directories if needed.
Definition utils.c:301
int janus_string_to_uint32(const char *str, uint32_t *num)
Helper method to convert a string to a uint32_t.
Definition utils.c:168
void janus_set2(guint8 *data, size_t i, guint32 val)
Helper method to set two bytes at a memory position.
Definition utils.c:1379
int janus_string_to_uint8(const char *str, uint8_t *num)
Helper method to convert a string to a uint8_t.
Definition utils.c:148
gboolean janus_h264_is_i_frame(const char *buffer, int len)
Helper method to check if an H.264 frame contains an I-Frame or not.
Definition utils.c:893
void janus_vp8_simulcast_context_reset(janus_vp8_simulcast_context *context)
Set (or reset) the context fields to their default values.
Definition utils.c:1031
void janus_vp8_simulcast_descriptor_update(char *buffer, int len, janus_vp8_simulcast_context *context, gboolean switched)
Use the context info to update the RTP header of a packet, if needed.
Definition utils.c:1043
char * janus_random_uuid(void)
Helper to generate random UUIDs (needed by some plugins) Warning: this will fall back to a non-crypto...
Definition utils.c:96
guint64 janus_uint64_hash(guint64 num)
Helper to hash a guint64 number to another guint64 number.
Definition utils.c:141
struct janus_vp9_svc_info janus_vp9_svc_info
VP9 SVC info, as parsed from a payload descriptor.
gint64 janus_get_real_time(void)
Helper to retrieve the system real time, as Glib's g_get_real_time may not be available (only since 2...
Definition utils.c:40
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition utils.c:548
const char * janus_get_codec_from_pt(const char *sdp, int pt)
Ugly and dirty helper to quickly get the codec associated with a payload type in an SDP.
Definition utils.c:444
void janus_set3(guint8 *data, size_t i, guint32 val)
Helper method to set three bytes at a memory position.
Definition utils.c:1384
int janus_vp9_parse_svc(char *buffer, int len, gboolean *found, janus_vp9_svc_info *info)
Helper method to parse a VP9 payload descriptor for SVC-related info (e.g., when SVC is enabled)
Definition utils.c:1076
gint64 janus_get_monotonic_time(void)
Helper to retrieve the system monotonic time, as Glib's g_get_monotonic_time may not be available (on...
Definition utils.c:34
GList * janus_red_parse_blocks(char *buffer, int len)
Helper method to parse an RTP payload to return a list of RED blocks.
Definition utils.c:1218
size_t janus_strlcat(char *dest, const char *src, size_t dest_size)
Helper method to concatenate strings and log an error if truncation occurred.
Definition utils.c:273
int janus_get_codec_pt(const char *sdp, const char *codec)
Ugly and dirty helper to quickly get the payload type associated with a codec in an SDP.
Definition utils.c:338
void janus_protected_folder_add(const char *folder)
Add a folder to the protected list (meaning we won't create files there, like recordings or pcap dump...
Definition utils.c:568
gboolean janus_is_true(const char *value)
Helper to parse yes/no|true/false configuration values.
Definition utils.c:46
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition utils.c:178
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition utils.c:135
guint32 janus_push_bits(guint32 word, size_t num, guint32 val)
Helper method to push individual bits at the end of a word.
Definition utils.c:1369
int janus_red_pack_blocks(char *buffer, int len, GList *blocks)
Helper method to pack multiple buffers in a RED payload.
Definition utils.c:1298
void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name)
Creates a string describing the JSON type and constraint.
Definition utils.c:616
void janus_flags_clear(janus_flags *flags, gsize flag)
Janus flags clear method.
Definition utils.c:189
gboolean janus_h265_is_keyframe(const char *buffer, int len)
Helper method to check if an H.265 frame is a keyframe or not.
Definition utils.c:912
gboolean janus_vp9_is_keyframe(const char *buffer, int len)
Helper method to check if a VP9 frame is a keyframe or not.
Definition utils.c:772
gboolean janus_is_folder_protected(const char *path)
Check if the path points to a protected folder.
Definition utils.c:576
void janus_protected_folders_clear(void)
Cleanup the list of protected folder.
Definition utils.c:609
uint32_t janus_bitstream_getbits(uint8_t *base, uint8_t num, uint32_t *offset)
Helpers to read agroup of bits from a bitstream.
Definition utils.c:1401
struct janus_red_block janus_red_block
Helper struct to address a specific RED block.
void janus_flags_set(janus_flags *flags, gsize flag)
Janus flags set method.
Definition utils.c:183
void janus_set1(guint8 *data, size_t i, guint8 val)
Helper method to set one byte at a memory position.
Definition utils.c:1375
gboolean janus_flags_is_set(janus_flags *flags, gsize flag)
Janus flags check method.
Definition utils.c:195
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition utils.c:509
int janus_vp8_parse_descriptor(char *buffer, int len, gboolean *m, uint16_t *picid, uint8_t *tl0picidx, uint8_t *tid, uint8_t *y, uint8_t *keyidx)
Helper method to parse a VP8 payload descriptor for useful info (e.g., when simulcasting)
Definition utils.c:928
gchar * janus_make_absolute_path(const gchar *base_dir, const gchar *path)
Helper to convert path relative to base_dir to absolute path. If path already represents absolute pat...
Definition utils.c:328
gboolean janus_strcmp_const_time(const void *str1, const void *str2)
Helper to compare strings in constant time.
Definition utils.c:50
gboolean janus_h264_is_b_frame(const char *buffer, int len)
Helper method to check if an H.264 frame contains a B-Frame or not.
Definition utils.c:897
int janus_strlcat_fast(char *dest, const char *src, size_t dest_size, size_t *offset)
Alternative helper method to concatenate strings and log an error if truncation occurred,...
Definition utils.c:280
guint64 janus_random_uint64_full(void)
Helper to generate random 64-bit unsigned integers.
Definition utils.c:83
int janus_red_replace_block_pt(char *buffer, int len, int pt)
Helper method to overwrite all RTP payload types in RED blocks.
Definition utils.c:1344
uint8_t janus_bitstream_getbit(uint8_t *base, uint32_t offset)
Helpers to read a bit from a bitstream.
Definition utils.c:1397