framehooks: Add callback for determining if a hook is consuming frames of a specific type.

In the past framehooks have had no capability to determine what frame types a hook
is actually interested in consuming. This has meant that code has had to assume they
want all frames, thus preventing native bridging.

This change adds a callback which allows a framehook to be queried for whether it
is consuming a frame of a specific type. The native RTP bridging module has also
been updated to take advantange of this, allowing native bridging to occur when
previously it would not.

ASTERISK-23497 #comment Reported by: Etienne Lessard
ASTERISK-23497 #close

Review: https://reviewboard.asterisk.org/r/3522/
........

Merged revisions 413681 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@413682 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Joshua Colp
2014-05-11 02:09:10 +00:00
parent e2ed86e4ca
commit d134150be2
6 changed files with 164 additions and 88 deletions

View File

@@ -288,10 +288,16 @@ static struct ast_frame *native_rtp_framehook(struct ast_channel *chan, struct a
return f;
}
/*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
static int native_rtp_framehook_consume(void *data, enum ast_frame_type type)
{
return (type == AST_FRAME_CONTROL ? 1 : 0);
}
/*! \brief Internal helper function which checks whether the channels are compatible with our native bridging */
static int native_rtp_bridge_capable(struct ast_channel *chan)
{
return !ast_channel_has_audio_frame_or_monitor(chan);
return !ast_channel_has_hook_requiring_audio(chan);
}
static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
@@ -392,6 +398,7 @@ static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_
static struct ast_framehook_interface hook = {
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = native_rtp_framehook,
.consume_cb = native_rtp_framehook_consume,
};
if (!data) {

View File

@@ -1049,8 +1049,8 @@ enum {
*/
AST_SOFTHANGUP_EXPLICIT = (1 << 5),
/*!
* Used to break a bridge so the channel can be spied upon
* instead of actually hanging up.
* Used to request that the bridge core re-evaluate the current
* bridging technology in use by the bridge this channel is in.
*/
AST_SOFTHANGUP_UNBRIDGE = (1 << 6),
/*!
@@ -4401,6 +4401,16 @@ const char *ast_channel_oldest_linkedid(const char *a, const char *b);
*/
int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan);
/*!
* \brief Check if the channel has any active hooks that require audio.
* \since 12.3.0
*
* \param chan The channel to check.
*
* \retval non-zero if channel has active audiohooks, audio framehooks, or monitor.
*/
int ast_channel_has_hook_requiring_audio(struct ast_channel *chan);
/*!
* \brief Removes the trailing identifiers from a channel name string
* \since 12.0.0

View File

@@ -199,7 +199,20 @@ typedef struct ast_frame *(*ast_framehook_event_callback)(
*/
typedef void (*ast_framehook_destroy_callback)(void *data);
#define AST_FRAMEHOOK_INTERFACE_VERSION 1
/*!
* \brief This callback is called to determine if the framehook is currently consuming
* frames of a given type
* \since 12
*
* \param data, The data pointer provided at framehook initilization.
* \param type, The type of frame.
*
* \return 0 if frame type is being ignored
* \return 1 if frame type is not being ignored
*/
typedef int (*ast_framehook_consume_callback)(void *data, enum ast_frame_type type);
#define AST_FRAMEHOOK_INTERFACE_VERSION 2
/*! This interface is required for attaching a framehook to a channel. */
struct ast_framehook_interface {
/*! framehook interface version number */
@@ -209,6 +222,10 @@ struct ast_framehook_interface {
/*! destroy_cb is optional. This function is called immediately before the framehook
* is destroyed to allow for stored_data cleanup. */
ast_framehook_destroy_callback destroy_cb;
/*! consume_cb is optional. This function is called to query whether the framehook is consuming
* frames of a specific type at this time. If this callback is not implemented it is assumed that the
* framehook will consume frames of all types. */
ast_framehook_consume_callback consume_cb;
/*! This pointer can represent any custom data to be stored on the !framehook. This
* data pointer will be provided during each event callback which allows the framehook
* to store any stateful data associated with the application using the hook. */
@@ -323,4 +340,19 @@ int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks);
*/
int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks);
/*!
* \brief Determine if a framehook list is free of active framehooks consuming a specific type of frame
* \since 12.3.0
* \pre The channel must be locked during this function call.
*
* \param framehooks the framehook list
* \retval 0, not empty
* \retval 1, is empty (aside from dying framehooks)
*
* \note This function is very similar to ast_framehook_list_is_empty, but it checks individual
* framehooks to see if they have been marked for destruction and doesn't count them if they are.
*/
int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
enum ast_frame_type type);
#endif /* _AST_FRAMEHOOK_H */

View File

@@ -2614,6 +2614,12 @@ static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
return frame;
}
/*! \brief Callback function which informs upstream if we are consuming a frame of a specific type */
static int transfer_target_framehook_consume(void *data, enum ast_frame_type type)
{
return (type == AST_FRAME_CONTROL ? 1 : 0);
}
static void transfer_target_framehook_destroy_cb(void *data)
{
struct attended_transfer_properties *props = data;
@@ -2847,6 +2853,7 @@ static int attach_framehook(struct attended_transfer_properties *props, struct a
.version = AST_FRAMEHOOK_INTERFACE_VERSION,
.event_cb = transfer_target_framehook_cb,
.destroy_cb = transfer_target_framehook_destroy_cb,
.consume_cb = transfer_target_framehook_consume,
};
ao2_ref(props, +1);

View File

@@ -2668,6 +2668,13 @@ int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
|| !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
}
int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
{
return ast_channel_monitor(chan)
|| !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
|| !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
}
static void destroy_hooks(struct ast_channel *chan)
{
if (ast_channel_audiohooks(chan)) {

View File

@@ -160,6 +160,10 @@ int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interfac
ast_frfree(frame);
}
if (ast_channel_is_bridged(chan)) {
ast_softhangup_nolock(chan, AST_SOFTHANGUP_UNBRIDGE);
}
return framehook->id;
}
@@ -214,6 +218,12 @@ int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
}
int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
{
return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
}
int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
enum ast_frame_type type)
{
struct ast_framehook *cur;
@@ -229,6 +239,9 @@ int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
if (cur->detach_and_destroy_me) {
continue;
}
if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
continue;
}
return 0;
}