stasis: No need to keep a stasis type ref in a stasis msg or cache object.
[asterisk/asterisk.git] / main / bridge_basic.c
index 5e20f30..284d178 100644 (file)
@@ -29,8 +29,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_REGISTER_FILE()
-
 #include "asterisk/channel.h"
 #include "asterisk/utils.h"
 #include "asterisk/linkedlists.h"
@@ -1296,8 +1294,6 @@ struct attended_transfer_properties {
                AST_STRING_FIELD(exten);
                /*! Context of transfer target */
                AST_STRING_FIELD(context);
-               /*! Sound to play on failure */
-               AST_STRING_FIELD(failsound);
                /*! Sound to play when transfer completes */
                AST_STRING_FIELD(xfersound);
                /*! The channel technology of the transferer channel */
@@ -1421,12 +1417,21 @@ static struct attended_transfer_properties *attended_transfer_properties_alloc(
        struct ast_flags *transferer_features;
 
        props = ao2_alloc(sizeof(*props), attended_transfer_properties_destructor);
-       if (!props || ast_string_field_init(props, 64)) {
+       if (!props) {
+               ast_log(LOG_ERROR, "Unable to create props - channel %s, context %s\n",
+                       ast_channel_name(transferer), context);
                return NULL;
        }
 
        ast_cond_init(&props->cond, NULL);
 
+       if (ast_string_field_init(props, 64)) {
+               ast_log(LOG_ERROR, "Unable to initialize prop fields - channel %s, context %s\n",
+                       ast_channel_name(transferer), context);
+               ao2_ref(props, -1);
+               return NULL;
+       }
+
        props->target_framehook_id = -1;
        props->transferer = ast_channel_ref(transferer);
 
@@ -1447,7 +1452,6 @@ static struct attended_transfer_properties *attended_transfer_properties_alloc(
        props->atxfernoanswertimeout = xfer_cfg->atxfernoanswertimeout;
        props->atxferloopdelay = xfer_cfg->atxferloopdelay;
        ast_string_field_set(props, context, get_transfer_context(transferer, context));
-       ast_string_field_set(props, failsound, xfer_cfg->xferfailsound);
        ast_string_field_set(props, xfersound, xfer_cfg->xfersound);
        ao2_ref(xfer_cfg, -1);
 
@@ -1543,6 +1547,23 @@ static void stimulate_attended_transfer(struct attended_transfer_properties *pro
        ao2_unlock(props);
 }
 
+static void remove_attended_transfer_stimulus(struct attended_transfer_properties *props,
+               enum attended_transfer_stimulus stimulus)
+{
+       struct stimulus_list *list;
+
+       ao2_lock(props);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&props->stimulus_queue, list, next) {
+               if (list->stimulus == stimulus) {
+                       AST_LIST_REMOVE_CURRENT(next);
+                       ast_free(list);
+                       break;
+               }
+       }
+       AST_LIST_TRAVERSE_SAFE_END;
+       ao2_unlock(props);
+}
+
 /*!
  * \brief Get a desired transfer party for a bridge the transferer is not in.
  *
@@ -1708,6 +1729,44 @@ static void play_sound(struct ast_channel *chan, const char *sound)
 }
 
 /*!
+ * \brief Helper method to play a fail sound on a channel in a bridge
+ *
+ * \param chan The channel to play the fail sound to
+ */
+static void play_failsound(struct ast_channel *chan)
+{
+       char *sound;
+
+       ast_channel_lock(chan);
+       sound = ast_get_chan_features_xferfailsound(chan);
+       ast_channel_unlock(chan);
+
+       if (sound) {
+               play_sound(chan, sound);
+               ast_free(sound);
+       }
+}
+
+/*!
+ * \brief Helper method to stream a fail sound on a channel
+ *
+ * \param chan The channel to stream the fail sound to
+ */
+static void stream_failsound(struct ast_channel *chan)
+{
+       char *sound;
+
+       ast_channel_lock(chan);
+       sound = ast_get_chan_features_xferfailsound(chan);
+       ast_channel_unlock(chan);
+
+       if (sound) {
+               ast_stream_and_wait(chan, sound, AST_DIGIT_NONE);
+               ast_free(sound);
+       }
+}
+
+/*!
  * \brief Helper method to place a channel in a bridge on hold
  */
 static void hold(struct ast_channel *chan)
@@ -2049,7 +2108,7 @@ static enum attended_transfer_state calling_target_exit(struct attended_transfer
 {
        switch (stimulus) {
        case STIMULUS_TRANSFEREE_HANGUP:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                publish_transfer_fail(props);
                return TRANSFER_FAIL;
        case STIMULUS_DTMF_ATXFER_COMPLETE:
@@ -2061,7 +2120,7 @@ static enum attended_transfer_state calling_target_exit(struct attended_transfer
        case STIMULUS_TRANSFER_TARGET_HANGUP:
        case STIMULUS_TIMEOUT:
        case STIMULUS_DTMF_ATXFER_ABORT:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                return TRANSFER_REBRIDGE;
        case STIMULUS_DTMF_ATXFER_THREEWAY:
                bridge_unhold(props->transferee_bridge);
@@ -2090,7 +2149,7 @@ static enum attended_transfer_state hesitant_exit(struct attended_transfer_prope
 {
        switch (stimulus) {
        case STIMULUS_TRANSFEREE_HANGUP:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                publish_transfer_fail(props);
                return TRANSFER_FAIL;
        case STIMULUS_DTMF_ATXFER_COMPLETE:
@@ -2101,7 +2160,7 @@ static enum attended_transfer_state hesitant_exit(struct attended_transfer_prope
        case STIMULUS_TRANSFER_TARGET_HANGUP:
        case STIMULUS_TIMEOUT:
        case STIMULUS_DTMF_ATXFER_ABORT:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                return TRANSFER_RESUME;
        case STIMULUS_DTMF_ATXFER_THREEWAY:
                return TRANSFER_THREEWAY;
@@ -2163,7 +2222,7 @@ static enum attended_transfer_state consulting_exit(struct attended_transfer_pro
                 * a sound to the transferer to indicate the transferee is gone.
                 */
                bridge_basic_change_personality(props->target_bridge, BRIDGE_BASIC_PERSONALITY_NORMAL, NULL);
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                ast_bridge_merge_inhibit(props->target_bridge, -1);
                /* These next two lines are here to ensure that our reference to the target bridge
                 * is cleaned up properly and that the target bridge is not destroyed when the
@@ -2178,8 +2237,9 @@ static enum attended_transfer_state consulting_exit(struct attended_transfer_pro
                bridge_unhold(props->transferee_bridge);
                return TRANSFER_COMPLETE;
        case STIMULUS_TRANSFER_TARGET_HANGUP:
+               return TRANSFER_REBRIDGE;
        case STIMULUS_DTMF_ATXFER_ABORT:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                return TRANSFER_REBRIDGE;
        case STIMULUS_DTMF_ATXFER_THREEWAY:
                bridge_unhold(props->transferee_bridge);
@@ -2211,7 +2271,7 @@ static enum attended_transfer_state double_checking_exit(struct attended_transfe
 {
        switch (stimulus) {
        case STIMULUS_TRANSFEREE_HANGUP:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                publish_transfer_fail(props);
                return TRANSFER_FAIL;
        case STIMULUS_TRANSFERER_HANGUP:
@@ -2221,7 +2281,7 @@ static enum attended_transfer_state double_checking_exit(struct attended_transfe
                return TRANSFER_COMPLETE;
        case STIMULUS_TRANSFER_TARGET_HANGUP:
        case STIMULUS_DTMF_ATXFER_ABORT:
-               play_sound(props->transferer, props->failsound);
+               play_failsound(props->transferer);
                return TRANSFER_RESUME;
        case STIMULUS_DTMF_ATXFER_THREEWAY:
                bridge_unhold(props->target_bridge);
@@ -2296,6 +2356,10 @@ static enum attended_transfer_state blond_nonfinal_exit(struct attended_transfer
                return TRANSFER_RESUME;
        case STIMULUS_TIMEOUT:
                ast_softhangup(props->recall_target, AST_SOFTHANGUP_EXPLICIT);
+               /* It is possible before we hung them up that they queued up a recall target answer
+                * so we remove it if present as it should not exist.
+                */
+               remove_attended_transfer_stimulus(props, STIMULUS_RECALL_TARGET_ANSWER);
        case STIMULUS_RECALL_TARGET_HANGUP:
                props->recall_target = ast_channel_unref(props->recall_target);
                return TRANSFER_RECALLING;
@@ -2760,7 +2824,8 @@ static struct ast_frame *transfer_target_framehook_cb(struct ast_channel *chan,
 
        if (event == AST_FRAMEHOOK_EVENT_READ &&
                        frame && frame->frametype == AST_FRAME_CONTROL &&
-                       frame->subclass.integer == AST_CONTROL_ANSWER) {
+                       frame->subclass.integer == AST_CONTROL_ANSWER &&
+                       !ast_check_hangup(chan)) {
 
                ast_debug(1, "Detected an answer for recall attempt on attended transfer %p\n", props);
                if (props->superstate == SUPERSTATE_TRANSFER) {
@@ -3043,7 +3108,9 @@ static int attach_framehook(struct attended_transfer_properties *props, struct a
        ao2_ref(props, +1);
        target_interface.data = props;
 
+       ast_channel_lock(channel);
        props->target_framehook_id = ast_framehook_attach(channel, &target_interface);
+       ast_channel_unlock(channel);
        if (props->target_framehook_id == -1) {
                ao2_ref(props, -1);
                return -1;
@@ -3103,7 +3170,8 @@ static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len
        ast_channel_lock(chan);
        xfer_cfg = ast_get_chan_features_xfer_config(chan);
        if (!xfer_cfg) {
-               ast_log(LOG_ERROR, "Unable to get transfer configuration\n");
+               ast_log(LOG_ERROR, "Channel %s: Unable to get transfer configuration\n",
+                       ast_channel_name(chan));
                ast_channel_unlock(chan);
                return -1;
        }
@@ -3147,9 +3215,9 @@ static int grab_transfer(struct ast_channel *chan, char *exten, size_t exten_len
                } else if (!res) {
                        /* 0 for invalid extension dialed. */
                        if (ast_strlen_zero(exten)) {
-                               ast_debug(1, "%s dialed no digits.\n", ast_channel_name(chan));
+                               ast_verb(3, "Channel %s: Dialed no digits.\n", ast_channel_name(chan));
                        } else {
-                               ast_debug(1, "%s dialed '%s@%s' does not exist.\n",
+                               ast_verb(3, "Channel %s: Dialed '%s@%s' does not exist.\n",
                                        ast_channel_name(chan), exten, context);
                        }
                        if (attempts < max_attempts) {
@@ -3236,9 +3304,12 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        /* Inhibit the bridge before we do anything else. */
        bridge = ast_bridge_channel_merge_inhibit(bridge_channel, +1);
 
+       ast_verb(3, "Channel %s: Started DTMF attended transfer.\n",
+               ast_channel_name(bridge_channel->chan));
+
        if (strcmp(bridge->v_table->name, "basic")) {
-               ast_log(LOG_ERROR, "Attended transfer attempted on unsupported bridge type '%s'.\n",
-                       bridge->v_table->name);
+               ast_log(LOG_ERROR, "Channel %s: Attended transfer attempted on unsupported bridge type '%s'.\n",
+                       ast_channel_name(bridge_channel->chan), bridge->v_table->name);
                ast_bridge_merge_inhibit(bridge, -1);
                ao2_ref(bridge, -1);
                return 0;
@@ -3260,7 +3331,8 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        props = attended_transfer_properties_alloc(bridge_channel->chan,
                attended_transfer ? attended_transfer->context : NULL);
        if (!props) {
-               ast_log(LOG_ERROR, "Unable to allocate control structure for performing attended transfer.\n");
+               ast_log(LOG_ERROR, "Channel %s: Unable to allocate control structure for performing attended transfer.\n",
+                       ast_channel_name(bridge_channel->chan));
                ast_bridge_merge_inhibit(bridge, -1);
                ao2_ref(bridge, -1);
                return 0;
@@ -3269,7 +3341,8 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        props->transferee_bridge = bridge;
 
        if (add_transferer_role(props->transferer, attended_transfer)) {
-               ast_log(LOG_ERROR, "Unable to set transferrer bridge role.\n");
+               ast_log(LOG_ERROR, "Channel %s: Unable to set transferrer bridge role.\n",
+                       ast_channel_name(bridge_channel->chan));
                attended_transfer_properties_shutdown(props);
                return 0;
        }
@@ -3278,7 +3351,15 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
 
        /* Grab the extension to transfer to */
        if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), props->context)) {
-               ast_log(LOG_WARNING, "Unable to acquire target extension for attended transfer.\n");
+               /*
+                * XXX The warning here really should be removed.  While the
+                * message is accurate, this is a normal exit for when the user
+                * fails to specify a valid transfer target.  e.g., The user
+                * hungup, didn't dial any digits, or dialed an invalid
+                * extension.
+                */
+               ast_log(LOG_WARNING, "Channel %s: Unable to acquire target extension for attended transfer.\n",
+                       ast_channel_name(bridge_channel->chan));
                ast_bridge_channel_write_unhold(bridge_channel);
                attended_transfer_properties_shutdown(props);
                return 0;
@@ -3289,13 +3370,15 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        /* Fill the variable with the extension and context we want to call */
        snprintf(destination, sizeof(destination), "%s@%s", props->exten, props->context);
 
-       ast_debug(1, "Attended transfer to '%s'\n", destination);
+       ast_debug(1, "Channel %s: Attended transfer target '%s'\n",
+               ast_channel_name(bridge_channel->chan), destination);
 
        /* Get a channel that is the destination we wish to call */
        props->transfer_target = dial_transfer(bridge_channel->chan, destination);
        if (!props->transfer_target) {
-               ast_log(LOG_ERROR, "Unable to request outbound channel for attended transfer target.\n");
-               ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
+               ast_log(LOG_ERROR, "Channel %s: Unable to request outbound channel for attended transfer target.\n",
+                       ast_channel_name(bridge_channel->chan));
+               stream_failsound(props->transferer);
                ast_bridge_channel_write_unhold(bridge_channel);
                attended_transfer_properties_shutdown(props);
                return 0;
@@ -3305,8 +3388,9 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        /* Create a bridge to use to talk to the person we are calling */
        props->target_bridge = ast_bridge_basic_new();
        if (!props->target_bridge) {
-               ast_log(LOG_ERROR, "Unable to create bridge for attended transfer target.\n");
-               ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
+               ast_log(LOG_ERROR, "Channel %s: Unable to create bridge for attended transfer target.\n",
+                       ast_channel_name(bridge_channel->chan));
+               stream_failsound(props->transferer);
                ast_bridge_channel_write_unhold(bridge_channel);
                ast_hangup(props->transfer_target);
                props->transfer_target = NULL;
@@ -3316,8 +3400,9 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        ast_bridge_merge_inhibit(props->target_bridge, +1);
 
        if (attach_framehook(props, props->transfer_target)) {
-               ast_log(LOG_ERROR, "Unable to attach framehook to transfer target.\n");
-               ast_stream_and_wait(props->transferer, props->failsound, AST_DIGIT_NONE);
+               ast_log(LOG_ERROR, "Channel %s: Unable to attach framehook to transfer target.\n",
+                       ast_channel_name(bridge_channel->chan));
+               stream_failsound(props->transferer);
                ast_bridge_channel_write_unhold(bridge_channel);
                ast_hangup(props->transfer_target);
                props->transfer_target = NULL;
@@ -3331,8 +3416,9 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
                        BRIDGE_BASIC_PERSONALITY_ATXFER, props);
 
        if (ast_call(props->transfer_target, destination, 0)) {
-               ast_log(LOG_ERROR, "Unable to place outbound call to transfer target.\n");
-               ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
+               ast_log(LOG_ERROR, "Channel %s: Unable to place outbound call to transfer target.\n",
+                       ast_channel_name(bridge_channel->chan));
+               stream_failsound(props->transferer);
                ast_bridge_channel_write_unhold(bridge_channel);
                ast_hangup(props->transfer_target);
                props->transfer_target = NULL;
@@ -3347,8 +3433,9 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        ast_channel_ref(props->transfer_target);
        if (ast_bridge_impart(props->target_bridge, props->transfer_target, NULL, NULL,
                AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
-               ast_log(LOG_ERROR, "Unable to place transfer target into bridge.\n");
-               ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
+               ast_log(LOG_ERROR, "Channel %s: Unable to place transfer target into bridge.\n",
+                       ast_channel_name(bridge_channel->chan));
+               stream_failsound(props->transferer);
                ast_bridge_channel_write_unhold(bridge_channel);
                ast_hangup(props->transfer_target);
                props->transfer_target = NULL;
@@ -3357,8 +3444,9 @@ static int feature_attended_transfer(struct ast_bridge_channel *bridge_channel,
        }
 
        if (ast_pthread_create_detached(&thread, NULL, attended_transfer_monitor_thread, props)) {
-               ast_log(LOG_ERROR, "Unable to create monitoring thread for attended transfer.\n");
-               ast_stream_and_wait(bridge_channel->chan, props->failsound, AST_DIGIT_NONE);
+               ast_log(LOG_ERROR, "Channel %s: Unable to create monitoring thread for attended transfer.\n",
+                       ast_channel_name(bridge_channel->chan));
+               stream_failsound(props->transferer);
                ast_bridge_channel_write_unhold(bridge_channel);
                attended_transfer_properties_shutdown(props);
                return 0;
@@ -3383,35 +3471,52 @@ static void blind_transfer_cb(struct ast_channel *new_channel, struct transfer_c
 /*! \brief Internal built in feature for blind transfers */
 static int feature_blind_transfer(struct ast_bridge_channel *bridge_channel, void *hook_pvt)
 {
-       char exten[AST_MAX_EXTENSION] = "";
+       char xfer_exten[AST_MAX_EXTENSION] = "";
        struct ast_bridge_features_blind_transfer *blind_transfer = hook_pvt;
-       const char *context;
+       const char *xfer_context;
        char *goto_on_blindxfr;
 
+       ast_verb(3, "Channel %s: Started DTMF blind transfer.\n",
+               ast_channel_name(bridge_channel->chan));
+
        ast_bridge_channel_write_hold(bridge_channel, NULL);
 
        ast_channel_lock(bridge_channel->chan);
-       context = ast_strdupa(get_transfer_context(bridge_channel->chan,
+       xfer_context = ast_strdupa(get_transfer_context(bridge_channel->chan,
                blind_transfer ? blind_transfer->context : NULL));
        goto_on_blindxfr = ast_strdupa(S_OR(pbx_builtin_getvar_helper(bridge_channel->chan,
                "GOTO_ON_BLINDXFR"), ""));
        ast_channel_unlock(bridge_channel->chan);
 
        /* Grab the extension to transfer to */
-       if (grab_transfer(bridge_channel->chan, exten, sizeof(exten), context)) {
+       if (grab_transfer(bridge_channel->chan, xfer_exten, sizeof(xfer_exten), xfer_context)) {
                ast_bridge_channel_write_unhold(bridge_channel);
                return 0;
        }
 
+       ast_debug(1, "Channel %s: Blind transfer target '%s@%s'\n",
+               ast_channel_name(bridge_channel->chan), xfer_exten, xfer_context);
+
        if (!ast_strlen_zero(goto_on_blindxfr)) {
-               ast_debug(1, "After transfer, transferer %s goes to %s\n",
-                               ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
-               ast_bridge_set_after_go_on(bridge_channel->chan, NULL, NULL, 0, goto_on_blindxfr);
+               const char *chan_context;
+               const char *chan_exten;
+               int chan_priority;
+
+               ast_debug(1, "Channel %s: After transfer, transferrer goes to %s\n",
+                       ast_channel_name(bridge_channel->chan), goto_on_blindxfr);
+
+               ast_channel_lock(bridge_channel->chan);
+               chan_context = ast_strdupa(ast_channel_context(bridge_channel->chan));
+               chan_exten = ast_strdupa(ast_channel_exten(bridge_channel->chan));
+               chan_priority = ast_channel_priority(bridge_channel->chan);
+               ast_channel_unlock(bridge_channel->chan);
+               ast_bridge_set_after_go_on(bridge_channel->chan,
+                       chan_context, chan_exten, chan_priority, goto_on_blindxfr);
        }
 
-       if (ast_bridge_transfer_blind(0, bridge_channel->chan, exten, context, blind_transfer_cb,
-                       bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS &&
-                       !ast_strlen_zero(goto_on_blindxfr)) {
+       if (ast_bridge_transfer_blind(0, bridge_channel->chan, xfer_exten, xfer_context,
+               blind_transfer_cb, bridge_channel->chan) != AST_BRIDGE_TRANSFER_SUCCESS
+               && !ast_strlen_zero(goto_on_blindxfr)) {
                ast_bridge_discard_after_goto(bridge_channel->chan);
        }
 
@@ -3557,4 +3662,3 @@ void ast_bridging_init_basic(void)
        ast_bridge_features_register(AST_BRIDGE_BUILTIN_ATTENDEDTRANSFER, feature_attended_transfer, NULL);
        ast_bridge_features_register(AST_BRIDGE_BUILTIN_BLINDTRANSFER, feature_blind_transfer, NULL);
 }
-