Fork me on GitHub
Macros | Functions
utils.c File Reference

Utilities and helpers. More...

#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <zlib.h>
#include "utils.h"
#include "debug.h"
#include "mutex.h"
Include dependency graph for utils.c:

Macros

#define swap2(d)   d
 

Functions

gint64 janus_get_monotonic_time (void)
 Helper to retrieve the system monotonic time, as Glib's g_get_monotonic_time may not be available (only since 2.28) More...
 
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.28) More...
 
gboolean janus_is_true (const char *value)
 Helper to parse yes/no|true/false configuration values. More...
 
gboolean janus_strcmp_const_time (const void *str1, const void *str2)
 Helper to compare strings in constant time. More...
 
guint32 janus_random_uint32 (void)
 Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.) More...
 
guint64 janus_random_uint64 (void)
 Helper to generate random 64-bit unsigned integers (useful for Janus IDs) More...
 
char * janus_random_uuid (void)
 Helper to generate random UUIDs (needed by some plugins) More...
 
guint64 * janus_uint64_dup (guint64 num)
 Helper to generate an allocated copy of a guint64 number. More...
 
guint64 janus_uint64_hash (guint64 num)
 Helper to hash a guint64 number to another guint64 number. More...
 
int janus_string_to_uint8 (const char *str, uint8_t *num)
 Helper method to convert a string to a uint8_t. More...
 
int janus_string_to_uint16 (const char *str, uint16_t *num)
 Helper method to convert a string to a uint16_t. More...
 
int janus_string_to_uint32 (const char *str, uint32_t *num)
 Helper method to convert a string to a uint32_t. More...
 
void janus_flags_reset (janus_flags *flags)
 Janus flags reset method. More...
 
void janus_flags_set (janus_flags *flags, gsize flag)
 Janus flags set method. More...
 
void janus_flags_clear (janus_flags *flags, gsize flag)
 Janus flags clear method. More...
 
gboolean janus_flags_is_set (janus_flags *flags, gsize flag)
 Janus flags check method. More...
 
char * janus_string_replace (char *message, const char *old_string, const char *new_string)
 Helper to replace strings. More...
 
int janus_mkdir (const char *dir, mode_t mode)
 Helper to create a new directory, and recursively create parent directories if needed. More...
 
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 path then just g_strdup it. More...
 
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. More...
 
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. More...
 
int janus_pidfile_create (const char *file)
 Create and lock a PID file. More...
 
int janus_pidfile_remove (void)
 Unlock and remove a previously created PID file. More...
 
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 dumps) More...
 
gboolean janus_is_folder_protected (const char *path)
 Check if the path points to a protected folder. More...
 
void janus_protected_folders_clear (void)
 Cleanup the list of protected folder. More...
 
void janus_get_json_type_name (int jtype, unsigned int flags, char *type_name)
 Creates a string describing the JSON type and constraint. More...
 
gboolean janus_json_is_valid (json_t *val, json_type jtype, unsigned int flags)
 Checks whether the JSON value matches the type and constraint. More...
 
gboolean janus_vp8_is_keyframe (const char *buffer, int len)
 Helper method to check if a VP8 frame is a keyframe or not. More...
 
gboolean janus_vp9_is_keyframe (const char *buffer, int len)
 Helper method to check if a VP9 frame is a keyframe or not. More...
 
gboolean janus_h264_is_keyframe (const char *buffer, int len)
 Helper method to check if an H.264 frame is a keyframe or not. More...
 
gboolean janus_av1_is_keyframe (const char *buffer, int len)
 Helper method to check if an AV1 frame is a keyframe or not. More...
 
gboolean janus_h265_is_keyframe (const char *buffer, int len)
 Helper method to check if an H.265 frame is a keyframe or not. More...
 
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) More...
 
void janus_vp8_simulcast_context_reset (janus_vp8_simulcast_context *context)
 Set (or reset) the context fields to their default values. More...
 
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. More...
 
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) More...
 
guint32 janus_push_bits (guint32 word, size_t num, guint32 val)
 Helper method to push individual bits at the end of a word. More...
 
void janus_set1 (guint8 *data, size_t i, guint8 val)
 Helper method to set one byte at a memory position. More...
 
void janus_set2 (guint8 *data, size_t i, guint32 val)
 Helper method to set two bytes at a memory position. More...
 
void janus_set3 (guint8 *data, size_t i, guint32 val)
 Helper method to set three bytes at a memory position. More...
 
void janus_set4 (guint8 *data, size_t i, guint32 val)
 Helper method to set four bytes at a memory position. More...
 
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) More...
 

Detailed Description

Utilities and helpers.

Author
Lorenzo Miniero loren.nosp@m.zo@m.nosp@m.eetec.nosp@m.ho.c.nosp@m.om

Core

Macro Definition Documentation

◆ swap2

#define swap2 (   d)    d

Function Documentation

◆ 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.

Note
Currently only a placeholder, always returns FALSE
Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
Returns
TRUE if it's a keyframe, FALSE otherwise

◆ janus_flags_clear()

void janus_flags_clear ( janus_flags flags,
gsize  flag 
)

Janus flags clear method.

Parameters
[in]flagsThe janus_flags instance to update
[in]flagThe flag to clear

◆ janus_flags_is_set()

gboolean janus_flags_is_set ( janus_flags flags,
gsize  flag 
)

Janus flags check method.

Parameters
[in]flagsThe janus_flags instance to check
[in]flagThe flag to check
Returns
true if the flag is set, false otherwise

◆ janus_flags_reset()

void janus_flags_reset ( janus_flags flags)

Janus flags reset method.

Parameters
[in]flagsThe janus_flags instance to reset

◆ janus_flags_set()

void janus_flags_set ( janus_flags flags,
gsize  flag 
)

Janus flags set method.

Parameters
[in]flagsThe janus_flags instance to update
[in]flagThe flag to set

◆ 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.

Parameters
sdpThe SDP to parse
ptThe payload type to look for
Returns
The codec name, if found, NULL otherwise

◆ 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.

Parameters
sdpThe SDP to parse
codecThe codec to look for
Returns
The payload type, if found, -1 otherwise

◆ 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.

Parameters
jtypeThe JSON type, e.g., JSON_STRING
flagsIndicates constraints for the described type
[out]type_nameThe type description, e.g., "a positive integer"; required size is 19 characters
Returns
0 if successful, a negative integer otherwise

◆ 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 (only since 2.28)

Returns
The system monotonic time

◆ 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.28)

Returns
The system real time

◆ 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)

Note
It's up to you to provide a buffer large enough for the compressed data: in case the buffer isn't large enough, the request will fail
Parameters
[in]compressionCompression factor (1=fastest, 9=best compression)
[in]textPointer to the string to compress
[in]tlenLength of the string to compress
[in]compressedPointer to the buffer where to compress the string to
[in]zlenSize of the output buffer
Returns
The size of the compressed data, if successful, or 0 otherwise

◆ 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.

Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
Returns
TRUE if it's a keyframe, FALSE otherwise

◆ 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.

Note
Currently only a placeholder, always returns FALSE
Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
Returns
TRUE if it's a keyframe, FALSE otherwise

◆ janus_is_folder_protected()

gboolean janus_is_folder_protected ( const char *  path)

Check if the path points to a protected folder.

Parameters
pathPath we need to check
Returns
TRUE if the folder is protected, and FALSE otherwise

◆ janus_is_true()

gboolean janus_is_true ( const char *  value)

Helper to parse yes/no|true/false configuration values.

Parameters
valueThe configuration value to parse
Returns
true if the value contains a "yes", "YES", "true", TRUE", "1", false otherwise

◆ 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.

Parameters
valThe JSON value to be checked
jtypeThe JSON type, e.g., JSON_STRING
flagsIndicates constraints for the described type
Returns
TRUE if the value is valid

◆ 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 path then just g_strdup it.

Parameters
[in]base_dirPath which will be prepended to path if it's relative
[in]pathSome relative or absolute path
Returns
g_strdup'ed absolute path. Should be freed with g_free() when no longer needed

◆ janus_mkdir()

int janus_mkdir ( const char *  dir,
mode_t  mode 
)

Helper to create a new directory, and recursively create parent directories if needed.

Parameters
dirPath to the new folder to create
modeFile permissions for the new directory file
Returns
An integer like the regular mkdir does
Note
A failure may indicate that creating any of the subdirectories failed: some may still have been created

◆ janus_pidfile_create()

int janus_pidfile_create ( const char *  file)

Create and lock a PID file.

Parameters
filePath to the PID file to use
Returns
0 if successful, a negative integer otherwise

◆ janus_pidfile_remove()

int janus_pidfile_remove ( void  )

Unlock and remove a previously created PID file.

Returns
0 if successful, a negative integer otherwise

◆ 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 dumps)

Parameters
folderFolder to protect

◆ janus_protected_folders_clear()

void janus_protected_folders_clear ( void  )

Cleanup the list of protected folder.

◆ janus_push_bits()

guint32 janus_push_bits ( guint32  word,
size_t  num,
guint32  val 
)
inline

Helper method to push individual bits at the end of a word.

Parameters
[in]wordInitial value of word
[in]numNumber of bits to push
[in]valValue of bits to push
Returns
0 New word value

◆ janus_random_uint32()

guint32 janus_random_uint32 ( void  )

Helper to generate random 32-bit unsigned integers (useful for SSRCs, etc.)

Note
Currently just wraps g_random_int()
Returns
A random 32-bit unsigned integer

◆ janus_random_uint64()

guint64 janus_random_uint64 ( void  )

Helper to generate random 64-bit unsigned integers (useful for Janus IDs)

Returns
A random 64-bit unsigned integer

◆ janus_random_uuid()

char* janus_random_uuid ( void  )

Helper to generate random UUIDs (needed by some plugins)

Returns
A random UUID string, which must be deallocated with g_free

◆ janus_set1()

void janus_set1 ( guint8 *  data,
size_t  i,
guint8  val 
)
inline

Helper method to set one byte at a memory position.

Parameters
[in]datamemory data pointer
[in]iposition in memory to change
[in]valvalue to write

◆ janus_set2()

void janus_set2 ( guint8 *  data,
size_t  i,
guint32  val 
)
inline

Helper method to set two bytes at a memory position.

Parameters
[in]datamemory data pointer
[in]iposition in memory to change
[in]valvalue to write

◆ janus_set3()

void janus_set3 ( guint8 *  data,
size_t  i,
guint32  val 
)
inline

Helper method to set three bytes at a memory position.

Parameters
[in]datamemory data pointer
[in]iposition in memory to change
[in]valvalue to write

◆ janus_set4()

void janus_set4 ( guint8 *  data,
size_t  i,
guint32  val 
)
inline

Helper method to set four bytes at a memory position.

Parameters
[in]datamemory data pointer
[in]iposition in memory to change
[in]valvalue to write

◆ janus_strcmp_const_time()

gboolean janus_strcmp_const_time ( const void *  str1,
const void *  str2 
)

Helper to compare strings in constant time.

Parameters
str1The first string to compare
str2The second string to compare
Returns
true if the strings are the same, false otherwise

◆ janus_string_replace()

char* janus_string_replace ( char *  message,
const char *  old_string,
const char *  new_string 
)

Helper to replace strings.

Parameters
messageThe string that contains the text to replace, which may be freed if it is too short
old_stringThe old text to replace
new_stringThe new text
Returns
A pointer to the updated text string (re-allocated or just updated)

◆ 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.

Note
The value of num should be ignored, if the method returned an error
Parameters
[in]strThe string to convert
[out]numPointer to the converted number
Returns
0 if successful, or a negative integer otherwise (e.g., -ERANGE if the value is out of range)

◆ 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.

Note
The value of num should be ignored, if the method returned an error
Parameters
[in]strThe string to convert
[out]numPointer to the converted number
Returns
0 if successful, or a negative integer otherwise (e.g., -ERANGE if the value is out of range)

◆ 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.

Note
The value of num should be ignored, if the method returned an error
Parameters
[in]strThe string to convert
[out]numPointer to the converted number
Returns
0 if successful, or a negative integer otherwise (e.g., -ERANGE if the value is out of range)

◆ janus_uint64_dup()

guint64* janus_uint64_dup ( guint64  num)

Helper to generate an allocated copy of a guint64 number.

Note
While apparently silly, this is needed in order to make sure guint64 values used as keys in GHashTable operations are not lost: using temporary guint64 numbers in a g_hash_table_insert, for instance, will cause the key to contain garbage as soon as the temporary variable is lost, and all opererations on the key to fail
Parameters
numThe guint64 number to duplicate
Returns
A pointer to a guint64 number, if successful, NULL otherwise

◆ janus_uint64_hash()

guint64 janus_uint64_hash ( guint64  num)

Helper to hash a guint64 number to another guint64 number.

Note
We currently only use for event handlers
Parameters

◆ 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.

Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
Returns
TRUE if it's a keyframe, FALSE otherwise

◆ 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)

Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
[out]picidThe Picture ID
[out]tl0picidxTemporal level zero index
[out]tidTemporal-layer index
[out]yLayer sync bit
[out]keyidxTemporal key frame index
Returns
0 in case of success, a negative integer otherwise

◆ 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.

Parameters
[in]contextThe context to (re)set

◆ 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.

Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
[in]contextThe context to use as a reference
[in]switchedWhether there has been a source switch or not (important to compute offsets)

◆ 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.

Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
Returns
TRUE if it's a keyframe, FALSE otherwise

◆ 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)

Parameters
[in]bufferThe RTP payload to process
[in]lenThe length of the RTP payload
[out]foundWhether any SVC related info has been found or not
[out]infoPointer to a janus_vp9_svc_info structure for passing the parsed info back
Returns
0 in case of success, a negative integer otherwise