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 
86 int janus_string_to_uint8(const char *str, uint8_t *num);
87 
93 int janus_string_to_uint16(const char *str, uint16_t *num);
94 
100 int janus_string_to_uint32(const char *str, uint32_t *num);
101 
104 
106 typedef gsize janus_flags;
107 
111 
115 void janus_flags_set(janus_flags *flags, gsize flag);
116 
120 void janus_flags_clear(janus_flags *flags, gsize flag);
121 
126 gboolean janus_flags_is_set(janus_flags *flags, gsize flag);
128 
134 int janus_mkdir(const char *dir, mode_t mode);
135 
140 int janus_get_codec_pt(const char *sdp, const char *codec);
141 
146 const char *janus_get_codec_from_pt(const char *sdp, int pt);
147 
151 int janus_pidfile_create(const char *file);
152 
155 int janus_pidfile_remove(void);
156 
162 void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
163 
169 gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
170 
181 #define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
182  do { \
183  error_code = 0; \
184  unsigned int i; \
185  for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
186  json_t *val = json_object_get(obj, params[i].name); \
187  if(!val) { \
188  if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
189  error_code = (missing_code); \
190  if(log_error) \
191  JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
192  if(error_cause != NULL) \
193  g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
194  break; \
195  } \
196  continue; \
197  } \
198  if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
199  error_code = (invalid_code); \
200  char type_name[20]; \
201  janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
202  if(log_error) \
203  JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
204  if(error_cause != NULL) \
205  g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
206  break; \
207  } \
208  } \
209  } while(0)
210 
219 #define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
220  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)
221 
231 #define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
232  do { \
233  if (secret) { \
234  static struct janus_json_parameter secret_parameters[] = { \
235  {member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
236  }; \
237  JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
238  if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
239  error_code = (unauthorized_code); \
240  JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
241  if(error_cause != NULL) \
242  g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
243  } \
244  } \
245  } while(0)
246 
251 gboolean janus_vp8_is_keyframe(const char *buffer, int len);
252 
257 gboolean janus_vp9_is_keyframe(const char *buffer, int len);
258 
263 gboolean janus_h264_is_keyframe(const char *buffer, int len);
264 
267  uint16_t last_picid, base_picid, base_picid_prev;
268  uint8_t last_tlzi, base_tlzi, base_tlzi_prev;
270 
274 
284 int janus_vp8_parse_descriptor(char *buffer, int len,
285  uint16_t *picid, uint8_t *tl0picidx, uint8_t *tid, uint8_t *y, uint8_t *keyidx);
286 
292 void janus_vp8_simulcast_descriptor_update(char *buffer, int len, janus_vp8_simulcast_context *context, gboolean switched);
293 
295 typedef struct janus_vp9_svc_info {
296  int spatial_layer, temporal_layer;
297  uint8_t fbit, pbit, dbit, ubit, bbit, ebit;
299 
306 int janus_vp9_parse_svc(char *buffer, int len, gboolean *found, janus_vp9_svc_info *info);
307 
313 guint32 janus_push_bits(guint32 word, size_t num, guint32 val);
314 
320 void janus_set1(guint8 *data, size_t i, guint8 val);
321 
327 void janus_set2(guint8 *data, size_t i, guint32 val);
328 
334 void janus_set3(guint8 *data, size_t i, guint32 val);
335 
341 void janus_set4(guint8 *data, size_t i, guint32 val);
342 
343 #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:1009
gsize janus_flags
Janus flags container.
Definition: utils.h:106
int janus_string_to_uint32(const char *str, uint32_t *num)
Helper method to convert a string to a uint32_t.
Definition: utils.c:116
struct json_t json_t
Definition: plugin.h:225
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:250
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:339
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition: utils.c:394
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:449
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:860
int janus_string_to_uint16(const char *str, uint16_t *num)
Helper method to convert a string to a uint16_t.
Definition: utils.c:106
void janus_vp8_simulcast_context_reset(janus_vp8_simulcast_context *context)
Set (or reset) the context fields to their default values.
Definition: utils.c:823
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:223
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:605
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:529
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition: utils.c:126
VP9 SVC info, as parsed from a payload descriptor.
Definition: utils.h:295
void janus_set4(guint8 *data, size_t i, guint32 val)
Helper method to set four bytes at a memory position.
Definition: utils.c:1020
VP8 simulcasting context, in order to make sure SSRC changes result in coherent picid/temporal level ...
Definition: utils.h:266
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
uint8_t ubit
Definition: utils.h:297
int janus_string_to_uint8(const char *str, uint8_t *num)
Helper method to convert a string to a uint8_t.
Definition: utils.c:96
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:268
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 ...
int temporal_layer
Definition: utils.h:296
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:1014
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:143
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:1001
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:689
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:152
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition: utils.c:433
void janus_flags_set(janus_flags *flags, gsize flag)
Janus flags set method.
Definition: utils.c:131
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition: utils.c:90
struct janus_vp9_svc_info janus_vp9_svc_info
VP9 SVC info, as parsed from a payload descriptor.
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:267
void janus_flags_clear(janus_flags *flags, gsize flag)
Janus flags clear method.
Definition: utils.c:137
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:835
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:722
void janus_set1(guint8 *data, size_t i, guint8 val)
Helper method to set one byte at a memory position.
Definition: utils.c:1005
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:489