MediaProcessors
Classes | Macros | Typedefs | Functions | Variables
procs.c File Reference
#include "procs.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <ctype.h>
#include <libcjson/cJSON.h>
#include <libmediaprocsutils/uri_parser.h>
#include <libmediaprocsutils/log.h>
#include <libmediaprocsutils/stat_codes.h>
#include <libmediaprocsutils/check_utils.h>
#include <libmediaprocsutils/fair_lock.h>
#include <libmediaprocsutils/llist.h>
#include "proc.h"
#include "proc_if.h"
Include dependency graph for procs.c:

Go to the source code of this file.

Classes

struct  procs_module_ctx_s
 
struct  procs_reg_elem_s
 
struct  procs_ctx_s
 

Macros

#define LOGD_CTX_INIT(CTX)
 
#define LOGD(...)
 
#define TAG_HAS(NEEDLE)   (strstr(tag, NEEDLE)!= NULL)
 
#define TAG_IS(TAG)   (strcmp(tag, TAG)== 0)
 
#define LOCK_PROCS_CTX_API(PROCS_CTX)   ASSERT(pthread_mutex_lock(&PROCS_CTX->api_mutex)== 0);
 
#define UNLOCK_PROCS_CTX_API(PROCS_CTX)   ASSERT(pthread_mutex_unlock(&PROCS_CTX->api_mutex)== 0);
 
#define LOCK_PROCS_REG_ELEM_API(PROCS_CTX, REG_ELEM, EXIT_CODE_ON_FAILURE)
 
#define UNLOCK_PROCS_REG_ELEM_API(REG_ELEM)   ASSERT(pthread_mutex_unlock(&REG_ELEM->api_mutex)== 0);
 
#define PROCS_MAX_NUM_PROC_INSTANCES   8192
 
#define PROCS_FIFO_SIZE   2
 
#define PROC_ID_STR_FMT   "{\"proc_id\":%d}"
 

Typedefs

typedef struct procs_module_ctx_s procs_module_ctx_t
 
typedef struct procs_reg_elem_s procs_reg_elem_t
 
typedef struct procs_ctx_s procs_ctx_t
 

Functions

static int register_proc_if (const proc_if_t *proc_if, log_ctx_t *log_ctx)
 
static int unregister_proc_if (const char *proc_name, log_ctx_t *log_ctx)
 
static const proc_if_tget_proc_if_by_name (const char *proc_name, log_ctx_t *log_ctx)
 
static int procs_instance_opt (procs_ctx_t *procs_ctx, const char *tag, log_ctx_t *log_ctx, va_list arg)
 
static int procs_rest_get (procs_ctx_t *procs_ctx, log_ctx_t *log_ctx, char **ref_rest_str, const char *filter_str)
 
static int proc_register (procs_ctx_t *procs_ctx, const char *proc_name, const char *settings_str, log_ctx_t *log_ctx, int *ref_id, va_list arg)
 
static int proc_unregister (procs_ctx_t *procs_ctx, int id, log_ctx_t *log_ctx)
 
static int procs_id_opt (procs_ctx_t *procs_ctx, const char *tag, log_ctx_t *log_ctx, va_list arg)
 
static int procs_id_get (procs_reg_elem_t *procs_reg_elem, proc_ctx_t *proc_ctx, log_ctx_t *log_ctx, void **ref_reponse)
 
static proc_ctx_tprocs_id_opt_fetch_proc_ctx (procs_ctx_t *procs_ctx, int proc_id, const char *tag, va_list arg, log_ctx_t *log_ctx)
 
int procs_module_open (log_ctx_t *log_ctx)
 
void procs_module_close ()
 
int procs_module_opt (const char *tag,...)
 
procs_ctx_tprocs_open (log_ctx_t *log_ctx, size_t max_procs_num, const char *prefix_name, const char *procs_href)
 
void procs_close (procs_ctx_t **ref_procs_ctx)
 
int procs_opt (procs_ctx_t *procs_ctx, const char *tag,...)
 
int procs_send_frame (procs_ctx_t *procs_ctx, int proc_id, const proc_frame_ctx_t *proc_frame_ctx)
 
int procs_recv_frame (procs_ctx_t *procs_ctx, int proc_id, proc_frame_ctx_t **ref_proc_frame_ctx)
 

Variables

static procs_module_ctx_tprocs_module_ctx = NULL
 

Detailed Description

Author
Rafael Antoniello

Definition in file procs.c.

Macro Definition Documentation

#define LOCK_PROCS_CTX_API (   PROCS_CTX)    ASSERT(pthread_mutex_lock(&PROCS_CTX->api_mutex)== 0);

Macro: lock PROCS module instance API critical section.

Definition at line 71 of file procs.c.

#define LOCK_PROCS_REG_ELEM_API (   PROCS_CTX,
  REG_ELEM,
  EXIT_CODE_ON_FAILURE 
)
Value:
if(pthread_mutex_trylock(&PROCS_CTX->api_mutex)!= EBUSY) {\
EXIT_CODE_ON_FAILURE;\
}\
ASSERT(pthread_mutex_lock(&REG_ELEM->api_mutex)== 0);
#define ASSERT(COND)
Definition: check_utils.h:51

Macro: lock PROCS instance register element API critical section. It is important to remark that this macro is thought to make sure the developer lock instance API critical section first.

Definition at line 85 of file procs.c.

#define PROCS_MAX_NUM_PROC_INSTANCES   8192

Limit value for the maximum number of processors that can be instantiated in the system. This value may be just modified, in compilation, if needed.

Definition at line 102 of file procs.c.

#define TAG_HAS (   NEEDLE)    (strstr(tag, NEEDLE)!= NULL)

Returns non-zero if given 'tag' string contains 'needle' sub-string.

Definition at line 61 of file procs.c.

#define TAG_IS (   TAG)    (strcmp(tag, TAG)== 0)

Returns non-zero if 'tag' string is equal to given TAG string.

Definition at line 66 of file procs.c.

#define UNLOCK_PROCS_CTX_API (   PROCS_CTX)    ASSERT(pthread_mutex_unlock(&PROCS_CTX->api_mutex)== 0);

Macro: unlock PROCS module instance API critical section.

Definition at line 77 of file procs.c.

#define UNLOCK_PROCS_REG_ELEM_API (   REG_ELEM)    ASSERT(pthread_mutex_unlock(&REG_ELEM->api_mutex)== 0);

Macro: unlock PROCS instance register element API critical section.

Definition at line 94 of file procs.c.

Typedef Documentation

typedef struct procs_ctx_s procs_ctx_t

Module's instance context structure. PROCS module context structure is statically defined in the program.

Module's context structure. PROCS module context structure is statically defined in the program.

Processors registration structure.

Function Documentation

void procs_close ( procs_ctx_t **  ref_procs_ctx)

De-initialize and release the processors (PROCS) module instance context structure.

Parameters
ref_procs_ctxReference to the pointer to the processors (PROCS) module instance context structure to be release, obtained in a previous call to the 'procs_open()' function. Pointer is set to NULL on return.

Definition at line 417 of file procs.c.

static proc_ctx_t * procs_id_opt_fetch_proc_ctx ( procs_ctx_t procs_ctx,
int  proc_id,
const char *  tag,
va_list  arg,
log_ctx_t log_ctx 
)
static

This function fetches the processor context structure to be used for API options requests. As a special case to take into account, this function checks if a new processor name is specified on an eventual PUT settings (namely, it checks if 'proc_name' setting is present on a PUT operation, and if it is the case, checks if 'proc_name' actually changes). If a new processor name is requested, the current processor should be released and substituted by a new processor of the new type given (recycling the same processors register slot). Substituted processor settings are backup, and set to the new one. This way, if some setting on the old processor apply, will be set on the new processor. Otherwise, not applicable settings will be just ignored.

Definition at line 1218 of file procs.c.

void procs_module_close ( )

Close PROCS module. This is a global function and should be called only once at the end of the life of the application.

Definition at line 273 of file procs.c.

int procs_module_open ( log_ctx_t log_ctx)

Open PROCS module. This is a global function and should be called only once at the very beginning and during the life of the application.

Parameters
log_ctxPointer to a externally defined LOG module context structure.
Returns
Status code (STAT_SUCCESS code in case of success, for other code values please refer to .stat_codes.h).

Definition at line 244 of file procs.c.

int procs_module_opt ( const char *  tag,
  ... 
)

Processors module options. This function represents the API of the PROCS module. This function is thread-safe and can be called concurrently.

Parameters
tagProcessors option tag, namely, option identifier string. The following options are available:
  1. "PROCS_REGISTER_TYPE"
  2. "PROCS_UNREGISTER_TYPE"
...Variable list of parameters according to selected option. Refer to Tags description below to see the different additional parameters corresponding to each option tag.
Returns
Status code (STAT_SUCCESS code in case of success, for other code values please refer to .stat_codes.h).

Tags description (additional variable arguments per tag)

  • Tag "PROCS_REGISTER_TYPE":
    Register the interface of an specific processor type.
    Additional variable arguments for function procs_module_opt() are:
    Parameters
    proc_ifPointer to the processor interface structure (static and unambiguous interface of the type of processor we are registering). Code example:
    1 ...
    2 const proc_if_t proc_if_bypass_proc= {
    3  "bypass_processor",
    4  bypass_proc_open,
    5  bypass_proc_close,
    6  bypass_proc_rest_put,
    7  bypass_proc_rest_get,
    8  bypass_proc_process_frame,
    9  bypass_proc_opt,
    10  NULL, NULL, NULL
    11 };
    12 ...
    13 ret_code= procs_module_opt("PROCS_REGISTER_TYPE", &proc_if_bypass_proc);
  • Tag "PROCS_UNREGISTER_TYPE":
    Unregister interface of an specific processor type.
    Additional variable arguments for function procs_module_opt() are:
    Parameters
    proc_namePointer to a character string with the unambiguous processor type name. Code example:
    1 ret_code= procs_module_opt("PROCS_UNREGISTER_TYPE", "bypass_processor");

Tag "PROCS_GET_TYPE":
Get a copy of the interface of an specific processor type.
Additional variable arguments for function procs_module_opt() are:

Parameters
proc_namePointer to a character string with the unambiguous processor type name.
ref_proc_if_cpyReference to the pointer to the copy of the requested processor interface context structure. If the requested processor exist, a copy of the context structure will be returned by this argument; otherwise, reference content will be set to NULL. Code example:
1 proc_if_t *proc_if_cpy= NULL;
2 ...
3 ret_code= procs_module_opt("PROCS_GET_TYPE", "bypass_processor",
4  &proc_if_cpy);

Definition at line 294 of file procs.c.

procs_ctx_t* procs_open ( log_ctx_t log_ctx,
size_t  max_procs_num,
const char *  prefix_name,
const char *  procs_href 
)

Allocates and initializes processors (PROCS) module instance context structure.

Parameters
log_ctxPointer to the LOG module context structure.
max_procs_numMaximum number of processors that can be created (and managed) by this instance.
prefix_nameModule's API REST prefix name (256 characters maximum). This parameter is optional (NULL may be passed); if not specified, the default name "procs" is used.
procs_hrefModule's API REST href attribute specifying the URL path the API refers to. This parameter is optional (NULL may be passed).
Returns
Pointer to the processors context structure on success, NULL if fails. Code example:
The following call creates a "processors module instance" capable of handling 16 processors. Prefix name "video_processors" suggest we will use this module instance to handle only "video processors", and we give an href such that the module's API may be used applying on the following URL: GET/PUT/POST/DELETE -> 127.0.0.1/video_processors.json
1 procs_ctx_t *procs_ctx;
2 ...
3 procs_ctx= procs_open(log_ctx, 16, "video_processors", "127.0.0.1");

Definition at line 336 of file procs.c.

int procs_opt ( procs_ctx_t procs_ctx,
const char *  tag,
  ... 
)

Processors module instance options. This function represents the API of the PROCS module instance, and exposes all the available options to operate the different processors instances. This function is thread-safe and can be called concurrently.

Parameters
procs_ctxPointer to the processors (PROCS) module instance context structure.
tagProcessors option tag, namely, option identifier string. The following options are available:
  1. "PROCS_POST"
  2. "PROCS_GET"
  3. "PROCS_ID_DELETE"
  4. "PROCS_ID_GET"
  5. "PROCS_ID_PUT"
...Variable list of parameters according to selected option. Refer to Tags description below to see the different additional parameters corresponding to each option tag.
Returns
Status code (STAT_SUCCESS code in case of success, for other code values please refer to .stat_codes.h).

Tags description (additional variable arguments per tag)

  • Tag "PROCS_POST":
    Instantiate and register new processor.
    Additional variable arguments for function procs_opt() are:
    Parameters
    proc_namePointer to a character string with the unambiguous processor type name.
    settings_strCharacter string containing initial settings for the processor. String format can be either a query-string or JSON.
    rest_strReference to the pointer to a character string returning the processor identifier in JSON format as follows: '{"proc_id":id_number}' Code example:
    1 char *rest_str= NULL;
    2 ...
    3 ret_code= procs_opt(procs_ctx, "PROCS_POST", "bypass_processor",
    4  "setting1=100", &rest_str);
  • Tag "PROCS_GET":
    Get the representational state of the processors instances list.
    Additional variable arguments for function procs_opt() are:
    Parameters
    ref_strReference to the pointer to a character string returning the processors list representational state.
    filter_strCharacter string indicating one of the following filters apply:
    • "proc_name==x": Filter the returning list discarding all the processors that are not of the type 'x';
    • "proc_name!=x": Filter the returning list discarding all the processors that are of the type 'x'. This parameter is optional, and can be set to NULL (no filter apply). Code example:
      1 char *rest_str= NULL;
      2 ...
      3 ret_code= procs_opt(procs_ctx, "PROCS_GET", &rest_str, NULL);
  • Tag "PROCS_ID_DELETE":
    Unregister and release a processor instance.
    Additional variable arguments for function procs_opt() are:
    Parameters
    proc_idProcessor instance unambiguous Id. Code example:
    1 ret_code= procs_opt(procs_ctx, "PROCS_ID_DELETE", proc_id);
  • Tag "PROCS_ID_GET":
    Get the representational state of a processor instance (including current settings).
    Additional variable arguments for function procs_opt() are:
    Parameters
    proc_idProcessor instance unambiguous Id.
    ref_strReference to the pointer to a character string returning the processor's representational state. Code example:
    1 char *rest_str= NULL;
    2 ...
    3 ret_code= procs_opt(procs_ctx, "PROCS_ID_GET", proc_id, &rest_str);
  • Tag "PROCS_ID_PUT":
    Put (pass) new settings to a processor instance.
    Additional variable arguments for function procs_opt() are:
    Parameters
    proc_idProcessor instance unambiguous Id.
    strPointer to a character string containing new settings for the processor instance. String format can be either a query-string or JSON. Code example:
    1 ret_code= procs_opt(procs_ctx, "PROCS_ID_PUT", proc_id, "setting1=100");

Definition at line 474 of file procs.c.

int procs_recv_frame ( procs_ctx_t procs_ctx,
int  proc_id,
proc_frame_ctx_t **  ref_proc_frame_ctx 
)

Get new processed frame of data from the indicated processor's output FIFO buffer (the processor instance is indicated by the processor Id.). This function blocks until a new frame is available to be read from the processor's output FIFO. To unblock this function, the corresponding processor instance should be deleted. This function is thread-safe and can be called concurrently.

Parameters
procs_ctxPointer to the processors (PROCS) module instance context structure.
proc_idProcessor instance unambiguous Id.
ref_proc_frame_ctxReference to the pointer to a structure characterizing the output processed frame. This function will return the processed frame passing its structure pointer by argument.
Returns
Status code (STAT_SUCCESS code in case of success, for other code values please refer to .stat_codes.h).

Definition at line 545 of file procs.c.

int procs_send_frame ( procs_ctx_t procs_ctx,
int  proc_id,
const proc_frame_ctx_t proc_frame_ctx 
)

Put new frame of data to be processed in the indicated processor's input FIFO buffer (the processor instance is indicated by the processor Id.). This function blocks until a slot is available to be able to push the new frame into the processor's input FIFO. To unblock this function, the corresponding processor instance should be deleted. This function is thread-safe and can be called concurrently.

Parameters
procs_ctxPointer to the processors (PROCS) module instance context structure.
proc_idProcessor instance unambiguous Id.
proc_frame_ctxPointer to the structure characterizing the input frame to be processed. The frame is duplicated and inserted in the FIFO buffer.
Returns
Status code (STAT_SUCCESS code in case of success, for other code values please refer to .stat_codes.h).

Definition at line 504 of file procs.c.

Variable Documentation

procs_module_ctx_t* procs_module_ctx = NULL
static

PROCS module static instance.

Definition at line 242 of file procs.c.