Fork me on GitHub
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 
38 gint64 janus_get_monotonic_time(void);
39 
43 gint64 janus_get_real_time(void);
44 
51 char *janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT;
52 
56 gboolean janus_is_true(const char *value);
57 
62 gboolean janus_strcmp_const_time(const void *str1, const void *str2);
63 
67 guint32 janus_random_uint32(void);
68 
71 guint64 janus_random_uint64(void);
72 
75 char *janus_random_uuid(void);
76 
84 guint64 *janus_uint64_dup(guint64 num);
85 
90 guint64 janus_uint64_hash(guint64 num);
91 
97 int janus_string_to_uint8(const char *str, uint8_t *num);
98 
104 int janus_string_to_uint16(const char *str, uint16_t *num);
105 
111 int janus_string_to_uint32(const char *str, uint32_t *num);
112 
115 
117 typedef gsize janus_flags;
118 
121 void janus_flags_reset(janus_flags *flags);
122 
126 void janus_flags_set(janus_flags *flags, gsize flag);
127 
131 void janus_flags_clear(janus_flags *flags, gsize flag);
132 
137 gboolean janus_flags_is_set(janus_flags *flags, gsize flag);
139 
145 int janus_mkdir(const char *dir, mode_t mode);
146 
152 gchar *janus_make_absolute_path(const gchar *base_dir, const gchar *path);
153 
158 int janus_get_codec_pt(const char *sdp, const char *codec);
159 
164 const char *janus_get_codec_from_pt(const char *sdp, int pt);
165 
169 int janus_pidfile_create(const char *file);
170 
173 int janus_pidfile_remove(void);
174 
178 void janus_protected_folder_add(const char *folder);
179 
183 gboolean janus_is_folder_protected(const char *path);
184 
187 
193 void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
194 
200 gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
201 
212 #define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
213  do { \
214  error_code = 0; \
215  unsigned int i; \
216  for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
217  json_t *val = json_object_get(obj, params[i].name); \
218  if(!val) { \
219  if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
220  error_code = (missing_code); \
221  if(log_error) \
222  JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
223  if(error_cause != NULL) \
224  g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
225  break; \
226  } \
227  continue; \
228  } \
229  if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
230  error_code = (invalid_code); \
231  char type_name[20]; \
232  janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
233  if(log_error) \
234  JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
235  if(error_cause != NULL) \
236  g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
237  break; \
238  } \
239  } \
240  } while(0)
241 
250 #define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
251  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)
252 
262 #define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
263  do { \
264  if (secret) { \
265  static struct janus_json_parameter secret_parameters[] = { \
266  {member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
267  }; \
268  JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
269  if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
270  error_code = (unauthorized_code); \
271  JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
272  if(error_cause != NULL) \
273  g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
274  } \
275  } \
276  } while(0)
277 
282 gboolean janus_vp8_is_keyframe(const char *buffer, int len);
283 
288 gboolean janus_vp9_is_keyframe(const char *buffer, int len);
289 
294 gboolean janus_h264_is_keyframe(const char *buffer, int len);
295 
301 gboolean janus_av1_is_keyframe(const char *buffer, int len);
302 
308 gboolean janus_h265_is_keyframe(const char *buffer, int len);
309 
315 
319 
329 int janus_vp8_parse_descriptor(char *buffer, int len,
330  uint16_t *picid, uint8_t *tl0picidx, uint8_t *tid, uint8_t *y, uint8_t *keyidx);
331 
337 void janus_vp8_simulcast_descriptor_update(char *buffer, int len, janus_vp8_simulcast_context *context, gboolean switched);
338 
340 typedef struct janus_vp9_svc_info {
342  uint8_t fbit, pbit, dbit, ubit, bbit, ebit;
344 
351 int janus_vp9_parse_svc(char *buffer, int len, gboolean *found, janus_vp9_svc_info *info);
352 
358 guint32 janus_push_bits(guint32 word, size_t num, guint32 val);
359 
365 void janus_set1(guint8 *data, size_t i, guint8 val);
366 
372 void janus_set2(guint8 *data, size_t i, guint32 val);
373 
379 void janus_set3(guint8 *data, size_t i, guint32 val);
380 
386 void janus_set4(guint8 *data, size_t i, guint32 val);
387 
398 size_t janus_gzip_compress(int compression, char *text, size_t tlen, char *compressed, size_t zlen);
399 
400 #endif
janus_is_true
gboolean janus_is_true(const char *value)
Helper to parse yes/no|true/false configuration values.
Definition: utils.c:45
janus_vp8_simulcast_context
VP8 simulcasting context, in order to make sure SSRC changes result in coherent picid/temporal level ...
Definition: utils.h:311
janus_set4
void janus_set4(guint8 *data, size_t i, guint32 val)
Helper method to set four bytes at a memory position.
Definition: utils.c:1169
janus_pidfile_remove
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition: utils.c:504
janus_vp8_simulcast_descriptor_update
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:984
janus_uint64_hash
guint64 janus_uint64_hash(guint64 num)
Helper to hash a guint64 number to another guint64 number.
Definition: utils.c:138
janus_vp8_simulcast_context::base_picid
uint16_t base_picid
Definition: utils.h:312
janus_flags_set
void janus_flags_set(janus_flags *flags, gsize flag)
Janus flags set method.
Definition: utils.c:180
janus_vp9_svc_info::temporal_layer
int temporal_layer
Definition: utils.h:341
janus_pidfile_create
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition: utils.c:465
janus_vp8_simulcast_context
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 ...
janus_get_monotonic_time
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:33
janus_gzip_compress
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:1177
janus_set3
void janus_set3(guint8 *data, size_t i, guint32 val)
Helper method to set three bytes at a memory position.
Definition: utils.c:1163
janus_json_parameter::name
const gchar * name
Definition: utils.h:30
janus_json_is_valid
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:612
janus_flags_is_set
gboolean janus_flags_is_set(janus_flags *flags, gsize flag)
Janus flags check method.
Definition: utils.c:192
janus_vp9_svc_info::spatial_layer
int spatial_layer
Definition: utils.h:341
janus_string_replace
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:201
janus_json_parameter
Definition: utils.h:29
janus_uint64_dup
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition: utils.c:132
janus_string_to_uint8
int janus_string_to_uint8(const char *str, uint8_t *num)
Helper method to convert a string to a uint8_t.
Definition: utils.c:145
janus_random_uint64
guint64 janus_random_uint64(void)
Helper to generate random 64-bit unsigned integers (useful for Janus IDs)
Definition: utils.c:77
janus_vp9_is_keyframe
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:728
janus_vp8_parse_descriptor
int janus_vp8_parse_descriptor(char *buffer, int len, 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:871
janus_vp9_svc_info::pbit
uint8_t pbit
Definition: utils.h:342
janus_h265_is_keyframe
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:856
janus_protected_folders_clear
void janus_protected_folders_clear(void)
Cleanup the list of protected folder.
Definition: utils.c:565
janus_vp8_simulcast_context_reset
void janus_vp8_simulcast_context_reset(janus_vp8_simulcast_context *context)
Set (or reset) the context fields to their default values.
Definition: utils.c:972
janus_vp8_simulcast_context::last_tlzi
uint8_t last_tlzi
Definition: utils.h:313
janus_mkdir
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:272
janus_make_absolute_path
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:299
janus_json_parameter::jtype
json_type jtype
Definition: utils.h:31
janus_vp9_svc_info::ebit
uint8_t ebit
Definition: utils.h:342
janus_string_to_uint32
int janus_string_to_uint32(const char *str, uint32_t *num)
Helper method to convert a string to a uint32_t.
Definition: utils.c:165
janus_get_real_time
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:39
janus_random_uint32
guint32 janus_random_uint32(void)
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)
Definition: utils.c:73
janus_protected_folder_add
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:524
json_t
struct json_t json_t
Definition: plugin.h:236
janus_flags
gsize janus_flags
Janus flags container.
Definition: utils.h:117
janus_get_json_type_name
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:572
janus_get_codec_pt
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:309
janus_get_codec_from_pt
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:406
janus_string_to_uint16
int janus_string_to_uint16(const char *str, uint16_t *num)
Helper method to convert a string to a uint16_t.
Definition: utils.c:155
janus_vp9_svc_info::dbit
uint8_t dbit
Definition: utils.h:342
janus_vp9_svc_info
struct janus_vp9_svc_info janus_vp9_svc_info
VP9 SVC info, as parsed from a payload descriptor.
janus_vp9_svc_info::bbit
uint8_t bbit
Definition: utils.h:342
janus_vp9_svc_info::ubit
uint8_t ubit
Definition: utils.h:342
janus_vp9_parse_svc
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:1009
janus_vp9_svc_info
VP9 SVC info, as parsed from a payload descriptor.
Definition: utils.h:340
janus_flags_clear
void janus_flags_clear(janus_flags *flags, gsize flag)
Janus flags clear method.
Definition: utils.c:186
janus_vp8_is_keyframe
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:652
janus_set2
void janus_set2(guint8 *data, size_t i, guint32 val)
Helper method to set two bytes at a memory position.
Definition: utils.c:1158
janus_is_folder_protected
gboolean janus_is_folder_protected(const char *path)
Check if the path points to a protected folder.
Definition: utils.c:532
janus_h264_is_keyframe
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:812
janus_strcmp_const_time
gboolean janus_strcmp_const_time(const void *str1, const void *str2)
Helper to compare strings in constant time.
Definition: utils.c:49
janus_flags_reset
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition: utils.c:175
janus_vp8_simulcast_context::base_tlzi_prev
uint8_t base_tlzi_prev
Definition: utils.h:313
janus_vp9_svc_info::fbit
uint8_t fbit
Definition: utils.h:342
janus_vp8_simulcast_context::last_picid
uint16_t last_picid
Definition: utils.h:312
janus_vp8_simulcast_context::base_picid_prev
uint16_t base_picid_prev
Definition: utils.h:312
janus_json_parameter::flags
unsigned int flags
Definition: utils.h:32
janus_vp8_simulcast_context::base_tlzi
uint8_t base_tlzi
Definition: utils.h:313
janus_av1_is_keyframe
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:845
janus_push_bits
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:1150
janus_random_uuid
char * janus_random_uuid(void)
Helper to generate random UUIDs (needed by some plugins)
Definition: utils.c:93
janus_set1
void janus_set1(guint8 *data, size_t i, guint8 val)
Helper method to set one byte at a memory position.
Definition: utils.c:1154