git migration: Refactor the ASTERISK_FILE_VERSION macro
[asterisk/asterisk.git] / main / bridge_channel.c
index d94b738..9e30068 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+ASTERISK_REGISTER_FILE()
 
 #include <signal.h>
-#include <semaphore.h>
 
 #include "asterisk/heap.h"
 #include "asterisk/astobj2.h"
@@ -56,6 +55,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/parking.h"
 #include "asterisk/causes.h"
 #include "asterisk/test.h"
+#include "asterisk/sem.h"
 
 /*!
  * \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
@@ -101,7 +101,7 @@ struct bridge_sync {
        /*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
        unsigned int id;
        /*! Semaphore used for synchronization */
-       sem_t sem;
+       struct ast_sem sem;
        /*! Pointer to next entry in the list */
        AST_LIST_ENTRY(bridge_sync) list;
 };
@@ -124,7 +124,7 @@ static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
 {
        memset(sync_struct, 0, sizeof(*sync_struct));
        sync_struct->id = id;
-       sem_init(&sync_struct->sem, 0, 0);
+       ast_sem_init(&sync_struct->sem, 0, 0);
 
        AST_RWLIST_WRLOCK(&sync_structs);
        AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
@@ -157,7 +157,7 @@ static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
        AST_LIST_TRAVERSE_SAFE_END;
        AST_RWLIST_UNLOCK(&sync_structs);
 
-       sem_destroy(&sync_struct->sem);
+       ast_sem_destroy(&sync_struct->sem);
 }
 
 /*!
@@ -189,7 +189,7 @@ static void bridge_sync_wait(struct bridge_sync *sync_struct)
                .tv_nsec = timeout_val.tv_usec * 1000,
        };
 
-       sem_timedwait(&sync_struct->sem, &timeout_spec);
+       ast_sem_timedwait(&sync_struct->sem, &timeout_spec);
 }
 
 /*!
@@ -204,7 +204,7 @@ static void bridge_sync_wait(struct bridge_sync *sync_struct)
  */
 static void bridge_sync_signal(struct bridge_sync *sync_struct)
 {
-       sem_post(&sync_struct->sem);
+       ast_sem_post(&sync_struct->sem);
 }
 
 void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
@@ -979,7 +979,8 @@ int ast_bridge_channel_write_control_data(struct ast_bridge_channel *bridge_chan
 
 int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
 {
-       RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
+       struct ast_json *blob;
+       int res;
        size_t datalen;
 
        if (!ast_strlen_zero(moh_class)) {
@@ -990,12 +991,16 @@ int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, con
        } else {
                moh_class = NULL;
                datalen = 0;
+               blob = NULL;
        }
 
        ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
 
-       return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
+       res = ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
                moh_class, datalen);
+
+       ast_json_unref(blob);
+       return res;
 }
 
 int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
@@ -1510,117 +1515,182 @@ static void testsuite_notify_feature_success(struct ast_channel *chan, const cha
 #endif /* TEST_FRAMEWORK */
 }
 
-/*!
- * \internal
- * \brief Internal function that executes a feature on a bridge channel
- * \note Neither the bridge nor the bridge_channel locks should be held when entering
- * this function.
- */
-static void bridge_channel_feature(struct ast_bridge_channel *bridge_channel, const char *starting_dtmf)
+static int bridge_channel_feature_digit_add(
+       struct ast_bridge_channel *bridge_channel, int digit, size_t dtmf_len)
+{
+       if (dtmf_len < ARRAY_LEN(bridge_channel->dtmf_hook_state.collected) - 1) {
+               /* Add the new digit to the DTMF string so we can do our matching */
+               bridge_channel->dtmf_hook_state.collected[dtmf_len++] = digit;
+               bridge_channel->dtmf_hook_state.collected[dtmf_len] = '\0';
+
+               ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
+                         bridge_channel, ast_channel_name(bridge_channel->chan),
+                         bridge_channel->dtmf_hook_state.collected);
+       }
+
+       return dtmf_len;
+}
+
+static unsigned int bridge_channel_feature_digit_timeout(struct ast_bridge_channel *bridge_channel)
 {
-       struct ast_bridge_features *features = bridge_channel->features;
-       struct ast_bridge_hook_dtmf *hook = NULL;
-       char dtmf[MAXIMUM_DTMF_FEATURE_STRING];
-       size_t dtmf_len;
        unsigned int digit_timeout;
-       RAII_VAR(struct ast_features_general_config *, gen_cfg, NULL, ao2_cleanup);
+       struct ast_features_general_config *gen_cfg;
 
+       /* Determine interdigit timeout */
        ast_channel_lock(bridge_channel->chan);
        gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
+       ast_channel_unlock(bridge_channel->chan);
+
        if (!gen_cfg) {
                ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
-               ast_channel_unlock(bridge_channel->chan);
-               return;
+               return 3000; /* Pick a reasonable failsafe timeout in ms */
        }
+
        digit_timeout = gen_cfg->featuredigittimeout;
-       ast_channel_unlock(bridge_channel->chan);
+       ao2_ref(gen_cfg, -1);
 
-       if (ast_strlen_zero(starting_dtmf)) {
-               dtmf[0] = '\0';
-               dtmf_len = 0;
-       } else {
-               ast_copy_string(dtmf, starting_dtmf, sizeof(dtmf));
-               dtmf_len = strlen(dtmf);
+       return digit_timeout;
+}
+
+void ast_bridge_channel_feature_digit_add(struct ast_bridge_channel *bridge_channel, int digit)
+{
+       if (digit) {
+               bridge_channel_feature_digit_add(
+                       bridge_channel, digit, strlen(bridge_channel->dtmf_hook_state.collected));
        }
+}
 
-       /*
-        * Check if any feature DTMF hooks match or could match and
-        * try to collect more DTMF digits.
-        */
-       for (;;) {
-               int res;
+void ast_bridge_channel_feature_digit(struct ast_bridge_channel *bridge_channel, int digit)
+{
+       struct ast_bridge_features *features = bridge_channel->features;
+       struct ast_bridge_hook_dtmf *hook = NULL;
+       size_t dtmf_len;
 
-               if (dtmf_len) {
-                       ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
-                               bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
+       struct sanity_check_of_dtmf_size {
+               char check[1 / (ARRAY_LEN(bridge_channel->dtmf_hook_state.collected) == ARRAY_LEN(hook->dtmf.code))];
+       };
 
-                       /* See if a DTMF feature hook matches or can match */
-                       hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
-                       if (!hook) {
-                               ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
-                                       bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
-                               break;
+       dtmf_len = strlen(bridge_channel->dtmf_hook_state.collected);
+       if (!dtmf_len && !digit) {
+               /* Nothing to do */
+               return;
+       }
+
+       if (digit) {
+               dtmf_len = bridge_channel_feature_digit_add(bridge_channel, digit, dtmf_len);
+       }
+
+       while (digit) {
+               /* See if a DTMF feature hook matches or can match */
+               hook = ao2_find(features->dtmf_hooks, bridge_channel->dtmf_hook_state.collected,
+                       OBJ_SEARCH_PARTIAL_KEY);
+               if (!hook) {
+                       ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
+                               bridge_channel, ast_channel_name(bridge_channel->chan),
+                               bridge_channel->dtmf_hook_state.collected);
+                       break;
+               } else if (dtmf_len != strlen(hook->dtmf.code)) {
+                       unsigned int digit_timeout;
+                       /* Need more digits to match */
+                       ao2_ref(hook, -1);
+                       digit_timeout = bridge_channel_feature_digit_timeout(bridge_channel);
+                       bridge_channel->dtmf_hook_state.interdigit_timeout =
+                               ast_tvadd(ast_tvnow(), ast_samp2tv(digit_timeout, 1000));
+                       return;
+               } else {
+                       int remove_me;
+                       int already_suspended;
+
+                       ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
+                               hook, bridge_channel->dtmf_hook_state.collected, bridge_channel,
+                               ast_channel_name(bridge_channel->chan));
+
+                       /*
+                        * Clear the collected digits before executing the hook
+                        * in case the hook starts another sequence.
+                        */
+                       bridge_channel->dtmf_hook_state.collected[0] = '\0';
+
+                       ast_bridge_channel_lock_bridge(bridge_channel);
+                       already_suspended = bridge_channel->suspended;
+                       if (!already_suspended) {
+                               bridge_channel_internal_suspend_nolock(bridge_channel);
                        }
-                       if (strlen(hook->dtmf.code) == dtmf_len) {
-                               ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
-                                       hook, dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
-                               break;
+                       ast_bridge_unlock(bridge_channel->bridge);
+                       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
+
+                       /* Execute the matched hook on this channel. */
+                       remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
+                       if (remove_me) {
+                               ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
+                                       hook, bridge_channel, ast_channel_name(bridge_channel->chan));
+                               ao2_unlink(features->dtmf_hooks, hook);
                        }
+                       testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
                        ao2_ref(hook, -1);
-                       hook = NULL;
 
-                       if (ARRAY_LEN(dtmf) - 1 <= dtmf_len) {
-                               /* We have reached the maximum length of a DTMF feature string. */
-                               break;
+                       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
+                       if (!already_suspended) {
+                               bridge_channel_unsuspend(bridge_channel);
                        }
-               }
 
-               res = ast_waitfordigit(bridge_channel->chan, digit_timeout);
-               if (!res) {
-                       ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
-                               bridge_channel, ast_channel_name(bridge_channel->chan));
-                       break;
-               }
-               if (res < 0) {
-                       ast_debug(1, "DTMF feature string collection failed on %p(%s) for some reason\n",
-                               bridge_channel, ast_channel_name(bridge_channel->chan));
-                       break;
+                       /*
+                        * If we are handing the channel off to an external hook for
+                        * ownership, we are not guaranteed what kind of state it will
+                        * come back in.  If the channel hungup, we need to detect that
+                        * here if the hook did not already change the state.
+                        */
+                       if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
+                               ast_bridge_channel_kick(bridge_channel, 0);
+                               bridge_channel->dtmf_hook_state.collected[0] = '\0';
+                               return;
+                       }
+
+                       /* if there is dtmf that has been collected then loop back through,
+                          but set digit to -1 so it doesn't try to do an add since the dtmf
+                          is already in the buffer */
+                       dtmf_len = strlen(bridge_channel->dtmf_hook_state.collected);
+                       if (!dtmf_len) {
+                               return;
+                       }
                }
+       }
 
-               /* Add the new DTMF into the DTMF string so we can do our matching */
-               dtmf[dtmf_len] = res;
-               dtmf[++dtmf_len] = '\0';
+       if (!digit) {
+               ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
+                       bridge_channel, ast_channel_name(bridge_channel->chan));
        }
 
-       if (hook) {
-               int remove_me;
+       /* Timeout or DTMF digit didn't allow a match with any hooks. */
+       if (features->dtmf_passthrough) {
+               /* Stream the collected DTMF to the other channels. */
+               bridge_channel_write_dtmf_stream(bridge_channel,
+                       bridge_channel->dtmf_hook_state.collected);
+       }
+       bridge_channel->dtmf_hook_state.collected[0] = '\0';
 
-               /* Execute the matched hook on this channel. */
-               remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
-               if (remove_me) {
-                       ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
-                               hook, bridge_channel, ast_channel_name(bridge_channel->chan));
-                       ao2_unlink(features->dtmf_hooks, hook);
-               }
-               testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
-               ao2_ref(hook, -1);
+       ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
+}
 
-               /*
-                * If we are handing the channel off to an external hook for
-                * ownership, we are not guaranteed what kind of state it will
-                * come back in.  If the channel hungup, we need to detect that
-                * here if the hook did not already change the state.
-                */
-               if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
-                       ast_bridge_channel_kick(bridge_channel, 0);
-               }
-       } else {
-               if (features->dtmf_passthrough) {
-                       /* Stream any collected DTMF to the other channels. */
-                       bridge_channel_write_dtmf_stream(bridge_channel, dtmf);
-               }
-               ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
+/*!
+ * \internal
+ * \brief Handle bridge channel DTMF feature timeout expiration.
+ * \since 12.8.0
+ *
+ * \param bridge_channel Channel to check expired interdigit timer on.
+ *
+ * \return Nothing
+ */
+static void bridge_channel_handle_feature_timeout(struct ast_bridge_channel *bridge_channel)
+{
+       if (!bridge_channel->dtmf_hook_state.collected[0]
+               || 0 < ast_tvdiff_ms(bridge_channel->dtmf_hook_state.interdigit_timeout,
+                       ast_tvnow())) {
+               /* Not within a sequence or not timed out. */
+               return;
        }
+
+       ast_bridge_channel_feature_digit(bridge_channel, 0);
 }
 
 /*!
@@ -1965,6 +2035,19 @@ int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
                        bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
                return -1;
        }
+
+       if (swap) {
+               int dissolve = ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
+
+               /* This flag is cleared so the act of this channel leaving does not cause it to dissolve if need be */
+               ast_clear_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
+
+               ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
+               bridge_channel_internal_pull(swap);
+
+               ast_set2_flag(&bridge->feature_flags, dissolve, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
+       }
+
        bridge_channel->in_bridge = 1;
        bridge_channel->just_joined = 1;
        AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
@@ -1986,10 +2069,6 @@ int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
                bridge->uniqueid);
 
        ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
-       if (swap) {
-               ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
-               bridge_channel_internal_pull(swap);
-       }
 
        /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
        pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
@@ -2077,6 +2156,25 @@ static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_chan
 
 /*!
  * \internal
+ * \param bridge_channel Channel to read wr_queue alert pipe.
+ *
+ * \return Nothing
+ */
+static void bridge_channel_read_wr_queue_alert(struct ast_bridge_channel *bridge_channel)
+{
+       char nudge;
+
+       if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
+               if (errno != EINTR && errno != EAGAIN) {
+                       ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
+                               bridge_channel, ast_channel_name(bridge_channel->chan),
+                               strerror(errno));
+               }
+       }
+}
+
+/*!
+ * \internal
  * \brief Handle bridge channel write frame to channel.
  * \since 12.0.0
  *
@@ -2087,21 +2185,49 @@ static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_chan
 static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
 {
        struct ast_frame *fr;
-       char nudge;
        struct sync_payload *sync_payload;
 
        ast_bridge_channel_lock(bridge_channel);
-       if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
-               if (errno != EINTR && errno != EAGAIN) {
-                       ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
-                               bridge_channel, ast_channel_name(bridge_channel->chan), strerror(errno));
+
+       /* It's not good to have unbalanced frames and alert_pipe alerts. */
+       ast_assert(!AST_LIST_EMPTY(&bridge_channel->wr_queue));
+       if (AST_LIST_EMPTY(&bridge_channel->wr_queue)) {
+               /* No frame, flush the alert pipe of excess alerts. */
+               ast_log(LOG_WARNING, "Weird.  No frame from bridge for %s to process?\n",
+                       ast_channel_name(bridge_channel->chan));
+               bridge_channel_read_wr_queue_alert(bridge_channel);
+               ast_bridge_channel_unlock(bridge_channel);
+               return;
+       }
+
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&bridge_channel->wr_queue, fr, frame_list) {
+               if (bridge_channel->dtmf_hook_state.collected[0]) {
+                       switch (fr->frametype) {
+                       case AST_FRAME_BRIDGE_ACTION:
+                       case AST_FRAME_BRIDGE_ACTION_SYNC:
+                               /* Defer processing these frames while DTMF is collected. */
+                               continue;
+                       default:
+                               break;
+                       }
                }
+               bridge_channel_read_wr_queue_alert(bridge_channel);
+               AST_LIST_REMOVE_CURRENT(frame_list);
+               break;
        }
-       fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list);
+       AST_LIST_TRAVERSE_SAFE_END;
+
        ast_bridge_channel_unlock(bridge_channel);
        if (!fr) {
+               /*
+                * Wait some to reduce CPU usage from a tight loop
+                * without any wait because we only have deferred
+                * frames in the wr_queue.
+                */
+               usleep(1);
                return;
        }
+
        switch (fr->frametype) {
        case AST_FRAME_BRIDGE_ACTION:
                bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
@@ -2128,38 +2254,36 @@ static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channe
 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
 {
        struct ast_bridge_features *features = bridge_channel->features;
-       struct ast_bridge_hook_dtmf *hook;
+       struct ast_bridge_hook_dtmf *hook = NULL;
        char dtmf[2];
 
-       /* See if this DTMF matches the beginning of any feature hooks. */
+       /*
+        * See if we are already matching a DTMF feature hook sequence or
+        * if this DTMF matches the beginning of any DTMF feature hooks.
+        */
        dtmf[0] = frame->subclass.integer;
        dtmf[1] = '\0';
-       hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
-       if (hook) {
+       if (bridge_channel->dtmf_hook_state.collected[0]
+               || (hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_SEARCH_PARTIAL_KEY))) {
                enum ast_frame_type frametype = frame->frametype;
 
                bridge_frame_free(frame);
                frame = NULL;
 
-               ao2_ref(hook, -1);
+               ao2_cleanup(hook);
 
-               /* Collect any more needed DTMF to execute a hook. */
-               bridge_channel_suspend(bridge_channel);
-               ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
                switch (frametype) {
                case AST_FRAME_DTMF_BEGIN:
-                       bridge_channel_feature(bridge_channel, NULL);
+                       /* Just eat the frame. */
                        break;
                case AST_FRAME_DTMF_END:
-                       bridge_channel_feature(bridge_channel, dtmf);
+                       ast_bridge_channel_feature_digit(bridge_channel, dtmf[0]);
                        break;
                default:
                        /* Unexpected frame type. */
                        ast_assert(0);
                        break;
                }
-               ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
-               bridge_channel_unsuspend(bridge_channel);
 #ifdef TEST_FRAMEWORK
        } else if (frame->frametype == AST_FRAME_DTMF_END) {
                /* Only transmit this event on DTMF end or else every DTMF
@@ -2259,6 +2383,60 @@ static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channe
 
 /*!
  * \internal
+ * \brief Determine how long till the DTMF interdigit timeout.
+ * \since 12.8.0
+ *
+ * \param bridge_channel Channel to determine how long can wait.
+ *
+ * \retval ms Number of milliseconds to wait.
+ * \retval -1 to wait forever.
+ */
+static int bridge_channel_feature_timeout(struct ast_bridge_channel *bridge_channel)
+{
+       int ms;
+
+       if (bridge_channel->dtmf_hook_state.collected[0]) {
+               ms = ast_tvdiff_ms(bridge_channel->dtmf_hook_state.interdigit_timeout,
+                       ast_tvnow());
+               if (ms < 0) {
+                       /* Expire immediately. */
+                       ms = 0;
+               }
+       } else {
+               /* Timer is not active so wait forever. */
+               ms = -1;
+       }
+
+       return ms;
+}
+
+/*!
+ * \internal
+ * \brief Determine how long till a timeout.
+ * \since 12.8.0
+ *
+ * \param bridge_channel Channel to determine how long can wait.
+ *
+ * \retval ms Number of milliseconds to wait.
+ * \retval -1 to wait forever.
+ */
+static int bridge_channel_next_timeout(struct ast_bridge_channel *bridge_channel)
+{
+       int ms_interval;
+       int ms;
+
+       ms_interval = bridge_channel_next_interval(bridge_channel);
+       ms = bridge_channel_feature_timeout(bridge_channel);
+       if (ms < 0 || (0 <= ms_interval && ms_interval < ms)) {
+               /* Interval hook timeout is next. */
+               ms = ms_interval;
+       }
+
+       return ms;
+}
+
+/*!
+ * \internal
  * \brief Wait for something to happen on the bridge channel and handle it.
  * \since 12.0.0
  *
@@ -2286,7 +2464,7 @@ static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
        } else {
                ast_bridge_channel_unlock(bridge_channel);
                outfd = -1;
-               ms = bridge_channel_next_interval(bridge_channel);
+               ms = bridge_channel_next_timeout(bridge_channel);
                chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
                        &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
                if (ast_channel_unbridged(bridge_channel->chan)) {
@@ -2303,11 +2481,17 @@ static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
                        && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
                        if (chan) {
                                bridge_handle_trip(bridge_channel);
-                       } else if (-1 < outfd) {
-                               bridge_channel_handle_write(bridge_channel);
                        } else if (ms == 0) {
-                               /* An interval expired. */
+                               /* An interdigit timeout or interval expired. */
+                               bridge_channel_handle_feature_timeout(bridge_channel);
                                bridge_channel_handle_interval(bridge_channel);
+                       } else if (-1 < outfd) {
+                               /*
+                                * Must do this after checking timeouts or may have
+                                * an infinite loop due to deferring write queue
+                                * actions while trying to match DTMF feature hooks.
+                                */
+                               bridge_channel_handle_write(bridge_channel);
                        }
                }
                bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
@@ -2356,11 +2540,11 @@ static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_ch
        ao2_iterator_destroy(&iter);
 }
 
-/*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
 int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
 {
        int res = 0;
        struct ast_bridge_features *channel_features;
+       struct ast_channel *swap;
 
        ast_debug(1, "Bridge %s: %p(%s) is joining\n",
                bridge_channel->bridge->uniqueid,
@@ -2404,6 +2588,9 @@ int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
                bridge_channel->bridge->callid = ast_read_threadstorage_callid();
        }
 
+       /* Take the swap channel ref from the bridge_channel struct. */
+       swap = bridge_channel->swap;
+
        if (bridge_channel_internal_push(bridge_channel)) {
                int cause = bridge_channel->bridge->cause;
 
@@ -2429,11 +2616,21 @@ int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
                }
 
                ast_bridge_unlock(bridge_channel->bridge);
+
+               /* Must release any swap ref after unlocking the bridge. */
+               ao2_t_cleanup(swap, "Bridge push with swap successful");
+               swap = NULL;
+
                bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
+
                while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
                        /* Wait for something to do. */
                        bridge_channel_wait(bridge_channel);
                }
+
+               /* Force a timeout on any accumulated DTMF hook digits. */
+               ast_bridge_channel_feature_digit(bridge_channel, 0);
+
                bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
                ast_bridge_channel_lock_bridge(bridge_channel);
        }
@@ -2444,6 +2641,9 @@ int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
 
        ast_bridge_unlock(bridge_channel->bridge);
 
+       /* Must release any swap ref after unlocking the bridge. */
+       ao2_t_cleanup(swap, "Bridge push with swap failed or exited immediately");
+
        /* Complete any active hold before exiting the bridge. */
        if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
                ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
@@ -2596,7 +2796,7 @@ static void bridge_channel_destroy(void *obj)
        struct ast_frame *fr;
 
        if (bridge_channel->callid) {
-               bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
+               bridge_channel->callid = 0;
        }
 
        if (bridge_channel->bridge) {