[libdvbpsi-devel] [PATCH] Introduce dvbpsi_new2() which calls logging callback with extra context parameter
Jean-Paul Saman
jpsaman at videolan.org
Mon May 27 12:15:58 CEST 2013
Hi Andrey,
Thanks for the patch, but I hate to spoil it for you ;)
On Sun, May 26, 2013 at 8:09 PM, Andrey Utkin <
andrey.krieger.utkin at gmail.com> wrote:
> This does not break original dvbpsi_new() mechanism.
> Rationale: when dvbpsi contexts are created in many objects, you may
> need to figure out, which object is the owner.
> ---
> src/dvbpsi.c | 19 +++++++++++++++++++
> src/dvbpsi.h | 8 ++++++++
> 2 files changed, 27 insertions(+)
>
The functionality you require is already present in libdvbpsi in the form
of a private pointer inside the dvbpsi_t structure, from dvbpsi.h header
file:
/*****************************************************************************
* dvbpsi_t
*****************************************************************************/
/*!
* \struct dvbpsi_s
* \brief DVBPSI handle structure
*
* This structure provides a handle to libdvbpsi API and should be used
instead
* of dvbpsi_decoder_t.
*/
/*!
* \typedef struct dvbpsi_s dvbpsi_t
* \brief dvbpsi_t type definition.
*/
struct dvbpsi_s
{
dvbpsi_decoder_t *p_decoder; /*!< private pointer
to
specific decoder
*/
/* Messages callback */
dvbpsi_message_cb pf_message; /*!< Log message
callback */
enum dvbpsi_msg_level i_msg_level; /*!< Log level */
/* private data pointer for use by caller, not by libdvbpsi itself ! */
void *p_sys; /*!< pointer to
private data
from caller. Do
not use
from inside
libdvbpsi. It
will crash any
application. */
};
The p_sys pointer can be used to attach your context. I think the
documentation for this could be improved,
since you missed this use case.
In addtion each PSI table attache function "dvbpsi_XXX_attach()" also
allows for passing private data to the callback function. The
example program dvbinfo makes use of this mechanism to pass on a 'context'.
The function
libdvbpsi_init() shows you how to use this, eg: lines 1747
(dvbpsi_pat_attach function), 648 (PAT callback function)
Kind regards,
Jean-Paul Saman.
> diff --git a/src/dvbpsi.c b/src/dvbpsi.c
> index 4d27720..e9a9ad9 100644
> --- a/src/dvbpsi.c
> +++ b/src/dvbpsi.c
> @@ -60,6 +60,19 @@ dvbpsi_t *dvbpsi_new(dvbpsi_message_cb callback, enum
> dvbpsi_msg_level level)
> return p_dvbpsi;
> }
>
> +dvbpsi_t *dvbpsi_new2(void *context, dvbpsi_message_cb2 callback, enum
> dvbpsi_msg_level level)
> +{
> + dvbpsi_t *p_dvbpsi = calloc(1, sizeof(dvbpsi_t));
> + if (p_dvbpsi == NULL)
> + return NULL;
> +
> + p_dvbpsi->p_decoder = NULL;
> + p_dvbpsi->context = context;
> + p_dvbpsi->pf_message2 = callback;
> + p_dvbpsi->i_msg_level = level;
> + return p_dvbpsi;
> +}
> +
>
> /*****************************************************************************
> * dvbpsi_delete
>
> *****************************************************************************/
> @@ -537,6 +550,8 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const
> dvbpsi_msg_level_t level, const char
> if (err > DVBPSI_MSG_NONE) {
> if (dvbpsi->pf_message)
> dvbpsi->pf_message(dvbpsi, level, msg);
> + else if (dvbpsi->pf_message2)
> + dvbpsi->pf_message2(dvbpsi->context, dvbpsi, level, msg);
> }
> free(msg);
> }
> @@ -563,6 +578,8 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const
> dvbpsi_msg_level_t level, const char
> if (err > 0) { \
> if (dvbpsi->pf_message) \
> dvbpsi->pf_message(dvbpsi, level, msg); \
> + else if (dvbpsi->pf_message2) \
> + dvbpsi->pf_message2(dvbpsi->context, dvbpsi, level, msg);\
> } \
> free(msg); \
> } while(0);
> @@ -581,6 +598,8 @@ void dvbpsi_message(dvbpsi_t *dvbpsi, const
> dvbpsi_msg_level_t level, const char
> if (err > 0) { \
> if (dvbpsi->pf_message) \
> dvbpsi->pf_message(dvbpsi, level, msg); \
> + else if (dvbpsi->pf_message2) \
> + dvbpsi->pf_message2(dvbpsi->context, dvbpsi, level, msg);\
> } \
> free(msg); \
> } while(0);
> diff --git a/src/dvbpsi.h b/src/dvbpsi.h
> index 062a9db..cb28b96 100644
> --- a/src/dvbpsi.h
> +++ b/src/dvbpsi.h
> @@ -95,6 +95,11 @@ typedef void (* dvbpsi_message_cb)(dvbpsi_t *handle,
> const dvbpsi_msg_level_t level,
> const char* msg);
>
> +typedef void (* dvbpsi_message_cb2)(void *context,
> + dvbpsi_t *handle,
> + const dvbpsi_msg_level_t level,
> + const char* msg);
> +
>
> /*****************************************************************************
> * dvbpsi_decoder_t
>
> *****************************************************************************/
> @@ -145,6 +150,8 @@ struct dvbpsi_s
> specific
> decoder */
> /* Messages callback */
> dvbpsi_message_cb pf_message; /*!< Log message
> callback */
> + void *context;
> + dvbpsi_message_cb2 pf_message2; /*!< Log message
> callback */
> enum dvbpsi_msg_level i_msg_level; /*!< Log level */
>
> /* private data pointer for use by caller, not by libdvbpsi itself !
> */
> @@ -169,6 +176,7 @@ struct dvbpsi_s
> * handle must be freed with dvbpsi_delete().
> */
> dvbpsi_t *dvbpsi_new(dvbpsi_message_cb callback, enum dvbpsi_msg_level
> level);
> +dvbpsi_t *dvbpsi_new2(void *context, dvbpsi_message_cb2 callback, enum
> dvbpsi_msg_level level);
>
>
> /*****************************************************************************
> * dvbpsi_delete
> --
> 1.8.1.5
>
> _______________________________________________
> libdvbpsi-devel mailing list
> libdvbpsi-devel at videolan.org
> http://mailman.videolan.org/listinfo/libdvbpsi-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/libdvbpsi-devel/attachments/20130527/a54722bd/attachment.html>
More information about the libdvbpsi-devel
mailing list