Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
#ifndef JANUS_UTILS_H
#define JANUS_UTILS_H
#include <stdint.h>
#include <glib.h>
#include <jansson.h>
#define JANUS_JSON_STRING JSON_STRING
#define JANUS_JSON_INTEGER JSON_INTEGER
#define JANUS_JSON_OBJECT JSON_OBJECT
#define JANUS_JSON_ARRAY JSON_ARRAY
/* Use JANUS_JSON_BOOL instead of the non-existing JSON_BOOLEAN */
#define JANUS_JSON_BOOL JSON_TRUE
#define JANUS_JSON_PARAM_REQUIRED 1
#define JANUS_JSON_PARAM_POSITIVE 2
#define JANUS_JSON_PARAM_NONEMPTY 4
struct janus_json_parameter {
const gchar *name;
json_type jtype;
unsigned int flags;
};
#ifndef htonll
#define htonll(x) ((1==htonl(1)) ? (x) : ((guint64)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#endif
#ifndef ntohll
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((guint64)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
#endif
void janus_mark_started(void);
gint64 janus_get_monotonic_time_internal(void);
gint64 janus_get_monotonic_time(void);
gint64 janus_get_real_time(void);
char *janus_string_replace(char *message, const char *old_string, const char *new_string) G_GNUC_WARN_UNUSED_RESULT;
size_t janus_strlcat(char *dest, const char *src, size_t dest_size);
int janus_strlcat_fast(char *dest, const char *src, size_t dest_size, size_t *offset);
gboolean janus_is_true(const char *value);
gboolean janus_strcmp_const_time(const void *str1, const void *str2);
guint32 janus_random_uint32(void);
guint64 janus_random_uint64_full(void);
guint64 janus_random_uint64(void);
char *janus_random_uuid(void);
guint64 *janus_uint64_dup(guint64 num);
guint64 janus_uint64_hash(guint64 num);
int janus_string_to_uint8(const char *str, uint8_t *num);
int janus_string_to_uint16(const char *str, uint16_t *num);
int janus_string_to_uint32(const char *str, uint32_t *num);
typedef gsize janus_flags;
void janus_flags_reset(janus_flags *flags);
void janus_flags_set(janus_flags *flags, gsize flag);
void janus_flags_clear(janus_flags *flags, gsize flag);
gboolean janus_flags_is_set(janus_flags *flags, gsize flag);
int janus_mkdir(const char *dir, mode_t mode);
gchar *janus_make_absolute_path(const gchar *base_dir, const gchar *path);
int janus_get_codec_pt(const char *sdp, const char *codec);
const char *janus_get_codec_from_pt(const char *sdp, int pt);
int janus_pidfile_create(const char *file);
int janus_pidfile_remove(void);
void janus_protected_folder_add(const char *folder);
gboolean janus_is_folder_protected(const char *path);
void janus_protected_folders_clear(void);
void janus_get_json_type_name(int jtype, unsigned int flags, char *type_name);
gboolean janus_json_is_valid(json_t *val, json_type jtype, unsigned int flags);
#define JANUS_VALIDATE_JSON_OBJECT_FORMAT(missing_format, invalid_format, obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
do { \
error_code = 0; \
unsigned int i; \
for(i = 0; i < sizeof(params) / sizeof(struct janus_json_parameter); i++) { \
json_t *val = json_object_get(obj, params[i].name); \
if(!val) { \
if((params[i].flags & JANUS_JSON_PARAM_REQUIRED) != 0) { \
error_code = (missing_code); \
if(log_error) \
JANUS_LOG(LOG_ERR, missing_format "\n", params[i].name); \
if(error_cause != NULL) \
g_snprintf(error_cause, sizeof(error_cause), missing_format, params[i].name); \
break; \
} \
continue; \
} \
if(!janus_json_is_valid(val, params[i].jtype, params[i].flags)) { \
error_code = (invalid_code); \
char type_name[20]; \
janus_get_json_type_name(params[i].jtype, params[i].flags, type_name); \
if(log_error) \
JANUS_LOG(LOG_ERR, invalid_format "\n", params[i].name, type_name); \
if(error_cause != NULL) \
g_snprintf(error_cause, sizeof(error_cause), invalid_format, params[i].name, type_name); \
break; \
} \
} \
} while(0)
#define JANUS_VALIDATE_JSON_OBJECT(obj, params, error_code, error_cause, log_error, missing_code, invalid_code) \
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)
#define JANUS_CHECK_SECRET(secret, obj, member, error_code, error_cause, missing_code, invalid_code, unauthorized_code) \
do { \
if (secret) { \
static struct janus_json_parameter secret_parameters[] = { \
{member, JSON_STRING, JANUS_JSON_PARAM_REQUIRED} \
}; \
JANUS_VALIDATE_JSON_OBJECT(obj, secret_parameters, error_code, error_cause, TRUE, missing_code, invalid_code); \
if(error_code == 0 && !janus_strcmp_const_time((secret), json_string_value(json_object_get(obj, member)))) { \
error_code = (unauthorized_code); \
JANUS_LOG(LOG_ERR, "Unauthorized (wrong %s)\n", member); \
if(error_cause != NULL) \
g_snprintf(error_cause, sizeof(error_cause), "Unauthorized (wrong %s)", member); \
} \
} \
} while(0)
gboolean janus_vp8_is_keyframe(const char *buffer, int len);
gboolean janus_vp9_is_keyframe(const char *buffer, int len);
gboolean janus_h264_is_keyframe(const char *buffer, int len);
gboolean janus_h264_is_i_frame(const char *buffer, int len);
gboolean janus_h264_is_b_frame(const char *buffer, int len);
gboolean janus_av1_is_keyframe(const char *buffer, int len);
gboolean janus_h265_is_keyframe(const char *buffer, int len);
gboolean janus_is_keyframe(int codec, const char *buffer, int len);
typedef struct janus_vp8_simulcast_context {
uint16_t last_picid, base_picid, base_picid_prev;
uint8_t last_tlzi, base_tlzi, base_tlzi_prev;
} janus_vp8_simulcast_context;
void janus_vp8_simulcast_context_reset(janus_vp8_simulcast_context *context);
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);
void janus_vp8_simulcast_descriptor_update(char *buffer, int len, janus_vp8_simulcast_context *context, gboolean switched);
typedef struct janus_vp9_svc_info {
int spatial_layer, temporal_layer;
uint8_t fbit, pbit, dbit, ubit, bbit, ebit;
} janus_vp9_svc_info;
int janus_vp9_parse_svc(char *buffer, int len, gboolean *found, janus_vp9_svc_info *info);
typedef struct janus_red_block {
uint8_t pt;
uint32_t ts_offset;
uint8_t *data;
uint16_t length;
} janus_red_block;
GList *janus_red_parse_blocks(char *buffer, int len);
int janus_red_pack_blocks(char *buffer, int len, GList *blocks);
int janus_red_replace_block_pt(char *buffer, int len, int pt);
guint32 janus_push_bits(guint32 word, size_t num, guint32 val);
void janus_set1(guint8 *data, size_t i, guint8 val);
void janus_set2(guint8 *data, size_t i, guint32 val);
void janus_set3(guint8 *data, size_t i, guint32 val);
void janus_set4(guint8 *data, size_t i, guint32 val);
uint8_t janus_bitstream_getbit(uint8_t *base, uint32_t offset);
uint32_t janus_bitstream_getbits(uint8_t *base, uint8_t num, uint32_t *offset);
size_t janus_gzip_compress(int compression, char *text, size_t tlen, char *compressed, size_t zlen);
#endif
json_t
struct json_t json_t
Definition plugin.h:236
janus_json_parameter
Definition utils.h:29
janus_json_parameter::jtype
json_type jtype
Definition utils.h:31
janus_json_parameter::name
const gchar * name
Definition utils.h:30
janus_json_parameter::flags
unsigned int flags
Definition utils.h:32
janus_red_block
Helper struct to address a specific RED block.
Definition utils.h:431
janus_red_block::length
uint16_t length
Definition utils.h:435
janus_red_block::data
uint8_t * data
Definition utils.h:434
janus_red_block::pt
uint8_t pt
Definition utils.h:432
janus_red_block::ts_offset
uint32_t ts_offset
Definition utils.h:433
janus_vp8_simulcast_context
VP8 simulcasting context, in order to make sure SSRC changes result in coherent picid/temporal level ...
Definition utils.h:387
janus_vp8_simulcast_context::last_picid
uint16_t last_picid
Definition utils.h:388
janus_vp8_simulcast_context::base_tlzi
uint8_t base_tlzi
Definition utils.h:389
janus_vp8_simulcast_context::last_tlzi
uint8_t last_tlzi
Definition utils.h:389
janus_vp8_simulcast_context::base_tlzi_prev
uint8_t base_tlzi_prev
Definition utils.h:389
janus_vp8_simulcast_context::base_picid
uint16_t base_picid
Definition utils.h:388
janus_vp8_simulcast_context::base_picid_prev
uint16_t base_picid_prev
Definition utils.h:388
janus_vp9_svc_info
VP9 SVC info, as parsed from a payload descriptor.
Definition utils.h:417
janus_vp9_svc_info::temporal_layer
int temporal_layer
Definition utils.h:418
janus_vp9_svc_info::dbit
uint8_t dbit
Definition utils.h:419
janus_vp9_svc_info::pbit
uint8_t pbit
Definition utils.h:419
janus_vp9_svc_info::ebit
uint8_t ebit
Definition utils.h:419
janus_vp9_svc_info::spatial_layer
int spatial_layer
Definition utils.h:418
janus_vp9_svc_info::bbit
uint8_t bbit
Definition utils.h:419
janus_vp9_svc_info::fbit
uint8_t fbit
Definition utils.h:419
janus_vp9_svc_info::ubit
uint8_t ubit
Definition utils.h:419
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:912
janus_random_uint64
guint64 janus_random_uint64(void)
Helper to generate random 52 bit unsigned integers.
Definition utils.c:103
janus_flags
gsize janus_flags
Janus flags container.
Definition utils.h:172
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:707
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:1419
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:900
janus_is_keyframe
gboolean janus_is_keyframe(int codec, const char *buffer, int len)
Helper method to check if keyframe or not, using one of the codec specific helper methods according t...
Definition utils.c:939
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:1440
janus_random_uint32
guint32 janus_random_uint32(void)
Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.).
Definition utils.c:85
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:215
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:169
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:667
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:312
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:179
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:1408
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:159
janus_h264_is_i_frame
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:904
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:1060
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:1072
janus_random_uuid
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:107
janus_uint64_hash
guint64 janus_uint64_hash(guint64 num)
Helper to hash a guint64 number to another guint64 number.
Definition utils.c:152
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:51
janus_pidfile_remove
int janus_pidfile_remove(void)
Unlock and remove a previously created PID file.
Definition utils.c:559
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:455
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:1413
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:1105
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:47
janus_red_parse_blocks
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:1247
janus_strlcat
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:284
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:349
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:579
janus_is_true
gboolean janus_is_true(const char *value)
Helper to parse yes/no|true/false configuration values.
Definition utils.c:57
janus_flags_reset
void janus_flags_reset(janus_flags *flags)
Janus flags reset method.
Definition utils.c:189
janus_uint64_dup
guint64 * janus_uint64_dup(guint64 num)
Helper to generate an allocated copy of a guint64 number.
Definition utils.c:146
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:1398
janus_red_pack_blocks
int janus_red_pack_blocks(char *buffer, int len, GList *blocks)
Helper method to pack multiple buffers in a RED payload.
Definition utils.c:1327
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:627
janus_flags_clear
void janus_flags_clear(janus_flags *flags, gsize flag)
Janus flags clear method.
Definition utils.c:200
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:923
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:783
janus_is_folder_protected
gboolean janus_is_folder_protected(const char *path)
Check if the path points to a protected folder.
Definition utils.c:587
janus_protected_folders_clear
void janus_protected_folders_clear(void)
Cleanup the list of protected folder.
Definition utils.c:620
janus_bitstream_getbits
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:1430
janus_get_monotonic_time_internal
gint64 janus_get_monotonic_time_internal(void)
Helper to retrieve the system monotonic time, as Glib's g_get_monotonic_time may not be available (on...
Definition utils.c:35
janus_flags_set
void janus_flags_set(janus_flags *flags, gsize flag)
Janus flags set method.
Definition utils.c:194
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:1404
janus_flags_is_set
gboolean janus_flags_is_set(janus_flags *flags, gsize flag)
Janus flags check method.
Definition utils.c:206
janus_pidfile_create
int janus_pidfile_create(const char *file)
Create and lock a PID file.
Definition utils.c:520
janus_vp8_parse_descriptor
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:957
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:339
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:61
janus_h264_is_b_frame
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:908
janus_strlcat_fast
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:291
janus_random_uint64_full
guint64 janus_random_uint64_full(void)
Helper to generate random 64-bit unsigned integers.
Definition utils.c:94
janus_mark_started
void janus_mark_started(void)
Definition utils.c:42
janus_red_replace_block_pt
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:1373
janus_bitstream_getbit
uint8_t janus_bitstream_getbit(uint8_t *base, uint32_t offset)
Helpers to read a bit from a bitstream.
Definition utils.c:1426