diff options
author | Kinsey Moore <kmoore@digium.com> | 2014-06-26 12:43:47 +0000 |
---|---|---|
committer | Kinsey Moore <kmoore@digium.com> | 2014-06-26 12:43:47 +0000 |
commit | e977b7936b02bcd33a35bf7c4e5b51f496f727e5 (patch) | |
tree | 02c6fd7d4127021f3e20f783e350da4d78123bbd /main | |
parent | 22e62ac6f640f6442968e5ab9146d3a7fe496f95 (diff) |
Bridging: Allow channels to define bridging hooks
This patch allows the current owner of a channel to define various
feature hooks to be made available once the channel has entered a
bridge. This includes any hooks that are setup on the
ast_bridge_features struct such as DTMF hooks, bridge event hooks
(join, leave, etc.), and interval hooks.
Review: https://reviewboard.asterisk.org/r/3649/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@417361 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'main')
-rw-r--r-- | main/bridge.c | 58 | ||||
-rw-r--r-- | main/bridge_channel.c | 9 | ||||
-rw-r--r-- | main/channel.c | 68 |
3 files changed, 134 insertions, 1 deletions
diff --git a/main/bridge.c b/main/bridge.c index 35287e278..41053eb8a 100644 --- a/main/bridge.c +++ b/main/bridge.c @@ -3327,6 +3327,64 @@ static int bridge_dtmf_hook_sort(const void *obj_left, const void *obj_right, in return cmp; } +/*! \brief Callback for merging hook ao2_containers */ +static int merge_container_cb(void *obj, void *data, int flags) +{ + ao2_link(data, obj); + return 0; +} + +/*! \brief Wrapper for interval hooks that calls into the wrapped hook */ +static int interval_wrapper_cb(struct ast_bridge_channel *bridge_channel, void *obj) +{ + struct ast_bridge_hook_timer *hook = obj; + + return hook->generic.callback(bridge_channel, hook->generic.hook_pvt); +} + +/*! \brief Destructor for the hook wrapper */ +static void interval_wrapper_pvt_dtor(void *obj) +{ + ao2_cleanup(obj); +} + +/*! \brief Wrap the provided interval hook and add it to features */ +static void wrap_hook(struct ast_bridge_features *features, struct ast_bridge_hook_timer *hook) +{ + /* Break out of the current wrapper if it exists to avoid multiple layers */ + if (hook->generic.callback == interval_wrapper_cb) { + hook = hook->generic.hook_pvt; + } + + ast_bridge_interval_hook(features, hook->timer.flags, hook->timer.interval, + interval_wrapper_cb, ao2_bump(hook), interval_wrapper_pvt_dtor, + hook->generic.remove_flags.flags); +} + +void ast_bridge_features_merge(struct ast_bridge_features *into, const struct ast_bridge_features *from) +{ + struct ast_bridge_hook_timer *hook; + int idx; + + /* Merge hook containers */ + ao2_callback(from->dtmf_hooks, 0, merge_container_cb, into->dtmf_hooks); + ao2_callback(from->other_hooks, 0, merge_container_cb, into->other_hooks); + + /* Merge hook heaps */ + ast_heap_wrlock(from->interval_hooks); + for (idx = 1; (hook = ast_heap_peek(from->interval_hooks, idx)); idx++) { + wrap_hook(into, hook); + } + ast_heap_unlock(from->interval_hooks); + + /* Merge feature flags */ + into->feature_flags.flags |= from->feature_flags.flags; + into->usable |= from->usable; + + into->mute |= from->mute; + into->dtmf_passthrough |= from->dtmf_passthrough; +} + /* XXX ASTERISK-21271 make ast_bridge_features_init() static when make ast_bridge_join() requires features to be allocated. */ int ast_bridge_features_init(struct ast_bridge_features *features) { diff --git a/main/bridge_channel.c b/main/bridge_channel.c index 9b1b5737e..7d1c65367 100644 --- a/main/bridge_channel.c +++ b/main/bridge_channel.c @@ -2187,6 +2187,7 @@ static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_ch int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel) { int res = 0; + struct ast_bridge_features *channel_features; ast_format_copy(&bridge_channel->read_format, ast_channel_readformat(bridge_channel->chan)); ast_format_copy(&bridge_channel->write_format, ast_channel_writeformat(bridge_channel->chan)); @@ -2201,8 +2202,8 @@ int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel) */ ast_bridge_lock(bridge_channel->bridge); - /* Make sure we're still good to be put into a bridge */ ast_channel_lock(bridge_channel->chan); + /* Make sure we're still good to be put into a bridge */ if (ast_channel_internal_bridge(bridge_channel->chan) || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) { ast_channel_unlock(bridge_channel->chan); @@ -2214,6 +2215,12 @@ int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel) return -1; } ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge); + + /* Attach features requested by the channel */ + channel_features = ast_channel_feature_hooks_get(bridge_channel->chan); + if (channel_features) { + ast_bridge_features_merge(bridge_channel->features, channel_features); + } ast_channel_unlock(bridge_channel->chan); /* Add the jitterbuffer if the channel requires it */ diff --git a/main/channel.c b/main/channel.c index 04c396a71..7d9f04837 100644 --- a/main/channel.c +++ b/main/channel.c @@ -10453,3 +10453,71 @@ void ast_channel_end_dtmf(struct ast_channel *chan, char digit, struct timeval s ast_log(LOG_DTMF, "DTMF end '%c' simulated on %s due to %s, duration %ld ms\n", digit, ast_channel_name(chan), why, duration); } + +static void features_destroy(void *obj) +{ + ast_bridge_features_destroy(obj); +} + +static const struct ast_datastore_info bridge_features_info = { + .type = "bridge-features", + .destroy = features_destroy, +}; + +struct ast_bridge_features *ast_channel_feature_hooks_get(struct ast_channel *chan) +{ + struct ast_datastore *datastore; + + datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL); + if (!datastore) { + return NULL; + } + return datastore->data; +} + +static int channel_feature_hooks_set_full(struct ast_channel *chan, struct ast_bridge_features *features, int replace) +{ + struct ast_datastore *datastore; + struct ast_bridge_features *ds_features; + + datastore = ast_channel_datastore_find(chan, &bridge_features_info, NULL); + if (datastore) { + ds_features = datastore->data; + if (replace) { + ast_bridge_features_cleanup(ds_features); + ast_bridge_features_init(ds_features); + } + if (features) { + ast_bridge_features_merge(ds_features, features); + } + return 0; + } + + datastore = ast_datastore_alloc(&bridge_features_info, NULL); + if (!datastore) { + return -1; + } + + ds_features = ast_bridge_features_new(); + if (!ds_features) { + ast_datastore_free(datastore); + return -1; + } + + if (features) { + ast_bridge_features_merge(ds_features, features); + } + datastore->data = ds_features; + ast_channel_datastore_add(chan, datastore); + return 0; +} + +int ast_channel_feature_hooks_append(struct ast_channel *chan, struct ast_bridge_features *features) +{ + return channel_feature_hooks_set_full(chan, features, 0); +} + +int ast_channel_feature_hooks_replace(struct ast_channel *chan, struct ast_bridge_features *features) +{ + return channel_feature_hooks_set_full(chan, features, 1); +} |