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 /* Use JANUS_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
23 #define JANUS_JSON_BOOL JSON_TRUE
24 #define JANUS_JSON_PARAM_REQUIRED 1
25 #define JANUS_JSON_PARAM_POSITIVE 2
26 #define JANUS_JSON_PARAM_NONEMPTY 4
27 
29  const gchar *name;
30  json_type jtype;
31  unsigned int flags;
32 };
33 
37 gint64 janus_get_monotonic_time(void);
38 
42 gint64 janus_get_real_time(void);
43 
50 char *janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT;
51 
55 gboolean janus_is_true(const char *value);
56 
61 gboolean janus_strcmp_const_time(const void *str1, const void *str2);
62 
66 guint32 janus_random_uint32(void);
67 
70 guint64 janus_random_uint64(void);
71 
79 guint64 *janus_uint64_dup(guint64 num);
80 
83 
85 typedef gsize janus_flags;
86 
90 
94 void janus_flags_set(janus_flags *flags, gsize flag);
95 
99 void janus_flags_clear(janus_flags *flags, gsize flag);
100 
105 gboolean janus_flags_is_set(janus_flags *flags, gsize flag);
107 
113 int janus_mkdir(const char *dir, mode_t mode);
114 
119 int janus_get_codec_pt(const char *sdp, const char *codec);
120 
125 const char *janus_get_codec_from_pt(const char *sdp, int pt);
126 
130 int janus_pidfile_create(const char *file);
131 
134 int janus_pidfile_remove(void);
135 
141 void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
142 
148 gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
149 
160 #define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
161  do { \
162  error_code = 0; \
163  unsigned int i; \
164  for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
165  json_t *val = json_object_get(obj, params[i].name); \
166  if(!val) { \
167  if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
168  error_code = (missing_code); \
169  if(log_error) \
170  JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
171  if(error_cause != NULL) \
172  g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
173  break; \
174  } \
175  continue; \
176  } \
177  if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
178  error_code = (invalid_code); \
179  char type_name[20]; \
180  janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
181  if(log_error) \
182  JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
183  if(error_cause != NULL) \
184  g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
185  break; \
186  } \
187  } \
188  } while(0)
189 
198 #define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
199  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)
200 
210 #define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
211  do { \
212  if (secret) { \
213  static struct janus_json_parameter secret_parameters[] = { \
214  {member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
215  }; \
216  JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
217  if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
218  error_code = (unauthorized_code); \
219  JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
220  if(error_cause != NULL) \
221  g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
222  } \
223  } \
224  } while(0)
225 
230 gboolean janus_vp8_is_keyframe(char *buffer, int len);
231 
236 gboolean janus_vp9_is_keyframe(char *buffer, int len);
237 
242 gboolean janus_h264_is_keyframe(char *buffer, int len);
243 
246  uint16_t last_picid, base_picid, base_picid_prev;
247  uint8_t last_tlzi, base_tlzi, base_tlzi_prev;
249 
253 
263 int janus_vp8_parse_descriptor(char *buffer, int len,
264  uint16_t *picid, uint8_t *tl0picidx, uint8_t *tid, uint8_t *y, uint8_t *keyidx);
265 
271 void janus_vp8_simulcast_descriptor_update(char *buffer, int len, janus_vp8_simulcast_context *context, gboolean switched);
272 
285 int janus_vp9_parse_svc(char *buffer, int len, int *found,
286  int *spatial_layer, int *temporal_layer,
287  uint8_t *p, uint8_t *d, uint8_t *u, uint8_t *b, uint8_t *e);
288 
294 guint32 janus_push_bits(guint32 word, size_t num, guint32 val);
295 
301 void janus_set1(guint8 *data, size_t i, guint8 val);
302 
308 void janus_set2(guint8 *data, size_t i, guint32 val);
309 
315 void janus_set3(guint8 *data, size_t i, guint32 val);
316 
322 void janus_set4(guint8 *data, size_t i, guint32 val);
323 
324 #endif
unsigned int flags
Definition: utils.h:31
void janus_set2(guint8 *data, size_t i, guint32 val)
Helper method to set two bytes at a memory position.
Definition: utils.c:956
gsize janus_flags
Janus flags container.
Definition: utils.h:85
struct json_t json_t
Definition: plugin.h:223
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:220
guint64 janus_random_uint64(void)
Helper to generate random 64-bit unsigned integers (useful for Janus IDs)
Definition: utils.c:74
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:309
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition: utils.c:364
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:419
void janus_vp8_simulcast_context_reset(janus_vp8_simulcast_context *context)
Set (or reset) the context fields to their default values.
Definition: utils.c:778
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:193
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition: utils.c:96
void janus_set4(guint8 *data, size_t i, guint32 val)
Helper method to set four bytes at a memory position.
Definition: utils.c:967
VP8 simulcasting context, in order to make sure SSRC changes result in coherent picid/temporal level ...
Definition: utils.h:245
const gchar * name
Definition: utils.h:29
gint64 janus_get_monotonic_time(void)
Helper to retrieve the system monotonic time, as Glib&#39;s g_get_monotonic_time may not be available (on...
Definition: utils.c:30
json_type jtype
Definition: utils.h:30
gint64 janus_get_real_time(void)
Helper to retrieve the system real time, as Glib&#39;s g_get_real_time may not be available (only since 2...
Definition: utils.c:36
uint8_t last_tlzi
Definition: utils.h:247
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 ...
Definition: utils.h:28
void janus_set3(guint8 *data, size_t i, guint32 val)
Helper method to set three bytes at a memory position.
Definition: utils.c:961
guint32 janus_random_uint32(void)
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)
Definition: utils.c:70
gboolean janus_flags_is_set(janus_flags *flags, gsize flag)
Janus flags check method.
Definition: utils.c:113
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:948
gboolean janus_vp9_is_keyframe(char *buffer, int len)
Helper method to check if a VP9 frame is a keyframe or not.
Definition: utils.c:572
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:122
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition: utils.c:403
gboolean janus_vp8_is_keyframe(char *buffer, int len)
Helper method to check if a VP8 frame is a keyframe or not.
Definition: utils.c:499
void janus_flags_set(janus_flags *flags, gsize flag)
Janus flags set method.
Definition: utils.c:101
int janus_vp9_parse_svc(char *buffer, int len, int *found, int *spatial_layer, int *temporal_layer, uint8_t *p, uint8_t *d, uint8_t *u, uint8_t *b, uint8_t *e)
Helper method to parse a VP9 payload descriptor for SVC-related info (e.g., when SVC is enabled) ...
Definition: utils.c:815
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition: utils.c:90
gboolean janus_is_true(const char *value)
Helper to parse yes/no|true/false configuration values.
Definition: utils.c:42
uint16_t last_picid
Definition: utils.h:246
gboolean janus_h264_is_keyframe(char *buffer, int len)
Helper method to check if an H.264 frame is a keyframe or not.
Definition: utils.c:643
void janus_flags_clear(janus_flags *flags, gsize flag)
Janus flags clear method.
Definition: utils.c:107
gboolean janus_strcmp_const_time(const void *str1, const void *str2)
Helper to compare strings in constant time.
Definition: utils.c:46
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:790
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:677
void janus_set1(guint8 *data, size_t i, guint8 val)
Helper method to set one byte at a memory position.
Definition: utils.c:952
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:459