chan_sip: Fix dialog reference leaked to scheduler for reinvite_timeout.
[asterisk/asterisk.git] / bridges / bridge_softmix.c
index 4583435..53be7d9 100644 (file)
@@ -45,8 +45,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
-#include "asterisk/bridging.h"
-#include "asterisk/bridging_technology.h"
+#include "asterisk/bridge.h"
+#include "asterisk/bridge_technology.h"
 #include "asterisk/frame.h"
 #include "asterisk/options.h"
 #include "asterisk/logger.h"
@@ -119,6 +119,14 @@ struct softmix_channel {
 
 struct softmix_bridge_data {
        struct ast_timer *timer;
+       /*!
+        * \brief Bridge pointer passed to the softmix mixing thread.
+        *
+        * \note Does not need a reference because the bridge will
+        * always exist while the mixing thread exists even if the
+        * bridge is no longer actively using the softmix technology.
+        */
+       struct ast_bridge *bridge;
        /*! Lock for signaling the mixing thread. */
        ast_mutex_t lock;
        /*! Condition, used if we need to wake up the mixing thread. */
@@ -155,14 +163,14 @@ struct softmix_mixing_array {
 struct softmix_translate_helper_entry {
        int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
                                      and re-init if it was usable. */
-       struct ast_format dst_format; /*!< The destination format for this helper */
+       struct ast_format *dst_format; /*!< The destination format for this helper */
        struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
        struct ast_frame *out_frame; /*!< The output frame from the last translation */
        AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
 };
 
 struct softmix_translate_helper {
-       struct ast_format slin_src; /*!< the source format expected for all the translators */
+       struct ast_format *slin_src; /*!< the source format expected for all the translators */
        AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
 };
 
@@ -172,12 +180,17 @@ static struct softmix_translate_helper_entry *softmix_translate_helper_entry_all
        if (!(entry = ast_calloc(1, sizeof(*entry)))) {
                return NULL;
        }
-       ast_format_copy(&entry->dst_format, dst);
+       entry->dst_format = ao2_bump(dst);
+       /* initialize this to one so that the first time through the cleanup code after
+          allocation it won't be removed from the entry list */
+       entry->num_times_requested = 1;
        return entry;
 }
 
 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
 {
+       ao2_cleanup(entry->dst_format);
+
        if (entry->trans_pvt) {
                ast_translator_free_path(entry->trans_pvt);
        }
@@ -191,7 +204,7 @@ static void *softmix_translate_helper_free_entry(struct softmix_translate_helper
 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
 {
        memset(trans_helper, 0, sizeof(*trans_helper));
-       ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
+       trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
 }
 
 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
@@ -207,11 +220,11 @@ static void softmix_translate_helper_change_rate(struct softmix_translate_helper
 {
        struct softmix_translate_helper_entry *entry;
 
-       ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
+       trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
        AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
                if (entry->trans_pvt) {
                        ast_translator_free_path(entry->trans_pvt);
-                       if (!(entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src))) {
+                       if (!(entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src))) {
                                AST_LIST_REMOVE_CURRENT(entry);
                                entry = softmix_translate_helper_free_entry(entry);
                        }
@@ -260,25 +273,38 @@ static void softmix_process_write_audio(struct softmix_translate_helper *trans_h
                for (i = 0; i < sc->write_frame.samples; i++) {
                        ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
                }
+               /* check to see if any entries exist for the format. if not we'll want
+                  to remove it during cleanup */
+               AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
+                       if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
+                               ++entry->num_times_requested;
+                               break;
+                       }
+               }
                /* do not do any special write translate optimization if we had to make
                 * a special mix for them to remove their own audio. */
                return;
        }
 
+       /* Attempt to optimize channels using the same translation path/codec. Build a list of entries
+          of translation paths and track the number of references for each type. Each one of the same
+          type should be able to use the same out_frame. Since the optimization is only necessary for
+          multiple channels (>=2) using the same codec make sure resources are allocated only when
+          needed and released when not (see also softmix_translate_helper_cleanup */
        AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
-               if (ast_format_cmp(&entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
+               if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
                        entry->num_times_requested++;
                } else {
                        continue;
                }
                if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
-                       entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src);
+                       entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src);
                }
                if (entry->trans_pvt && !entry->out_frame) {
                        entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
                }
                if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
-                       ast_format_copy(&sc->write_frame.subclass.format, &entry->out_frame->subclass.format);
+                       ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format);
                        memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
                        sc->write_frame.datalen = entry->out_frame->datalen;
                        sc->write_frame.samples = entry->out_frame->samples;
@@ -296,44 +322,75 @@ static void softmix_translate_helper_cleanup(struct softmix_translate_helper *tr
 {
        struct softmix_translate_helper_entry *entry;
 
-       AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
+               /* if it hasn't been requested then remove it */
+               if (!entry->num_times_requested) {
+                       AST_LIST_REMOVE_CURRENT(entry);
+                       softmix_translate_helper_free_entry(entry);
+                       continue;
+               }
+
                if (entry->out_frame) {
                        ast_frfree(entry->out_frame);
                        entry->out_frame = NULL;
                }
+
+               /* nothing is optimized for a single path reference, so there is
+                  no reason to continue to hold onto the codec */
+               if (entry->num_times_requested == 1 && entry->trans_pvt) {
+                       ast_translator_free_path(entry->trans_pvt);
+                       entry->trans_pvt = NULL;
+               }
+
+               /* for each iteration (a mixing run) in the bridge softmix thread the number
+                  of references to a given entry is recalculated, so reset the number of
+                  times requested */
                entry->num_times_requested = 0;
        }
+       AST_LIST_TRAVERSE_SAFE_END;
 }
 
 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
 {
        struct softmix_channel *sc = bridge_channel->tech_pvt;
-       unsigned int channel_read_rate = ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan));
+       unsigned int channel_read_rate = ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan));
 
        ast_mutex_lock(&sc->lock);
        if (reset) {
                ast_slinfactory_destroy(&sc->factory);
                ast_dsp_free(sc->dsp);
        }
-       /* Setup read/write frame parameters */
+
+       /* Setup write frame parameters */
        sc->write_frame.frametype = AST_FRAME_VOICE;
-       ast_format_set(&sc->write_frame.subclass.format, ast_format_slin_by_rate(rate), 0);
+       /*
+        * NOTE: The write_frame format holds a reference because translation
+        * could be needed and the format changed to the translated format
+        * for the channel.  The translated format may not be a
+        * static cached format.
+        */
+       ao2_replace(sc->write_frame.subclass.format, ast_format_cache_get_slin_by_rate(rate));
        sc->write_frame.data.ptr = sc->final_buf;
        sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
        sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
 
+       /* Setup read frame parameters */
        sc->read_frame.frametype = AST_FRAME_VOICE;
-       ast_format_set(&sc->read_frame.subclass.format, ast_format_slin_by_rate(channel_read_rate), 0);
+       /*
+        * NOTE: The read_frame format does not hold a reference because it
+        * will always be a signed linear format.
+        */
+       sc->read_frame.subclass.format = ast_format_cache_get_slin_by_rate(channel_read_rate);
        sc->read_frame.data.ptr = sc->our_buf;
        sc->read_frame.datalen = SOFTMIX_DATALEN(channel_read_rate, interval);
        sc->read_frame.samples = SOFTMIX_SAMPLES(channel_read_rate, interval);
 
        /* Setup smoother */
-       ast_slinfactory_init_with_format(&sc->factory, &sc->write_frame.subclass.format);
+       ast_slinfactory_init_with_format(&sc->factory, sc->write_frame.subclass.format);
 
        /* set new read and write formats on channel. */
-       ast_set_read_format(bridge_channel->chan, &sc->read_frame.subclass.format);
-       ast_set_write_format(bridge_channel->chan, &sc->write_frame.subclass.format);
+       ast_set_read_format(bridge_channel->chan, sc->read_frame.subclass.format);
+       ast_set_write_format(bridge_channel->chan, sc->write_frame.subclass.format);
 
        /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
        sc->dsp = ast_dsp_new_with_rate(channel_read_rate);
@@ -378,11 +435,12 @@ static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridg
  *
  * \param bridge_channel Which channel source is changing.
  *
- * \return Nothing
+ * \retval 0 on success.
+ * \retval -1 on error.
  */
-static void softmix_src_change(struct ast_bridge_channel *bridge_channel)
+static int softmix_src_change(struct ast_bridge_channel *bridge_channel)
 {
-       ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_SRCCHANGE, NULL, 0);
+       return ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_SRCCHANGE, NULL, 0);
 }
 
 /*! \brief Function called when a channel is joined into the bridge */
@@ -437,6 +495,9 @@ static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_ch
        /* Drop the factory */
        ast_slinfactory_destroy(&sc->factory);
 
+       /* Drop any formats on the frames */
+       ao2_cleanup(sc->write_frame.subclass.format);
+
        /* Drop the DSP */
        ast_dsp_free(sc->dsp);
 
@@ -444,29 +505,6 @@ static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_ch
        ast_free(sc);
 }
 
-/*!
- * \internal
- * \brief Pass the given frame to everyone else.
- * \since 12.0.0
- *
- * \param bridge What bridge to distribute frame.
- * \param bridge_channel Channel to optionally not pass frame to. (NULL to pass to everyone)
- * \param frame Frame to pass.
- *
- * \return Nothing
- */
-static void softmix_pass_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
-{
-       struct ast_bridge_channel *cur;
-
-       AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
-               if (cur == bridge_channel) {
-                       continue;
-               }
-               ast_bridge_channel_queue_frame(cur, frame);
-       }
-}
-
 static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
 {
        struct ast_bridge_channel *cur;
@@ -499,14 +537,14 @@ static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bri
        int video_src_priority;
 
        /* Determine if the video frame should be distributed or not */
-       switch (bridge->video_mode.mode) {
+       switch (bridge->softmix.video_mode.mode) {
        case AST_BRIDGE_VIDEO_MODE_NONE:
                break;
        case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
                video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
                if (video_src_priority == 1) {
                        /* Pass to me and everyone else. */
-                       softmix_pass_everyone_else(bridge, NULL, frame);
+                       ast_bridge_queue_everyone_else(bridge, NULL, frame);
                }
                break;
        case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
@@ -514,14 +552,14 @@ static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bri
                ast_mutex_lock(&sc->lock);
                ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
                        sc->video_talker.energy_average,
-                       ast_format_get_video_mark(&frame->subclass.format));
+                       frame->subclass.frame_ending);
                ast_mutex_unlock(&sc->lock);
                video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
                if (video_src_priority == 1) {
                        int num_src = ast_bridge_number_video_src(bridge);
                        int echo = num_src > 1 ? 0 : 1;
 
-                       softmix_pass_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
+                       ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
                } else if (video_src_priority == 2) {
                        softmix_pass_video_top_priority(bridge, frame);
                }
@@ -555,7 +593,7 @@ static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bri
        ast_mutex_lock(&sc->lock);
        ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
 
-       if (bridge->video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
+       if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
                int cur_slot = sc->video_talker.energy_history_cur_slot;
 
                sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
@@ -589,8 +627,7 @@ static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bri
 
        /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
         * is not determined to be talking. */
-       if (!(bridge_channel->tech_args.drop_silence && !sc->talking) &&
-               (frame->frametype == AST_FRAME_VOICE && ast_format_is_slinear(&frame->subclass.format))) {
+       if (!(bridge_channel->tech_args.drop_silence && !sc->talking)) {
                ast_slinfactory_feed(&sc->factory, frame);
        }
 
@@ -598,7 +635,7 @@ static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bri
        ast_mutex_unlock(&sc->lock);
 
        if (update_talking != -1) {
-               ast_bridge_notify_talking(bridge_channel, update_talking);
+               ast_bridge_channel_notify_talking(bridge_channel, update_talking);
        }
 }
 
@@ -611,12 +648,16 @@ static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bri
  * \param bridge_channel Which channel is writing the frame.
  * \param frame What is being written.
  *
- * \return Nothing
+ * \retval 0 Frame accepted into the bridge.
+ * \retval -1 Frame needs to be deferred.
  */
-static void softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
+static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
 {
-/* BUGBUG need to look at channel roles to determine what to do with control frame. */
-       /*! \todo BUGBUG softmix_bridge_write_control() not written */
+       /*
+        * XXX Softmix needs to use channel roles to determine what to
+        * do with control frames.
+        */
+       return 0;
 }
 
 /*!
@@ -628,8 +669,8 @@ static void softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_b
  * \param bridge_channel Which channel is writing the frame.
  * \param frame What is being written.
  *
- * \retval 0 on success
- * \retval -1 on failure
+ * \retval 0 Frame accepted into the bridge.
+ * \retval -1 Frame needs to be deferred.
  *
  * \note On entry, bridge is already locked.
  */
@@ -637,30 +678,46 @@ static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_cha
 {
        int res = 0;
 
-       if (!bridge->tech_pvt || !bridge_channel->tech_pvt) {
-               return -1;
+       if (!bridge->tech_pvt || (bridge_channel && !bridge_channel->tech_pvt)) {
+               /* "Accept" the frame and discard it. */
+               return 0;
        }
 
+       /*
+        * XXX Softmix needs to use channel roles to determine who gets
+        * what frame.  Possible roles: announcer, recorder, agent,
+        * supervisor.
+        */
        switch (frame->frametype) {
+       case AST_FRAME_NULL:
+               /* "Accept" the frame and discard it. */
+               break;
        case AST_FRAME_DTMF_BEGIN:
        case AST_FRAME_DTMF_END:
-               softmix_pass_everyone_else(bridge, bridge_channel, frame);
+               res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
                break;
        case AST_FRAME_VOICE:
-               softmix_bridge_write_voice(bridge, bridge_channel, frame);
+               if (bridge_channel) {
+                       softmix_bridge_write_voice(bridge, bridge_channel, frame);
+               }
                break;
        case AST_FRAME_VIDEO:
-               softmix_bridge_write_video(bridge, bridge_channel, frame);
+               if (bridge_channel) {
+                       softmix_bridge_write_video(bridge, bridge_channel, frame);
+               }
                break;
        case AST_FRAME_CONTROL:
-               softmix_bridge_write_control(bridge, bridge_channel, frame);
+               res = softmix_bridge_write_control(bridge, bridge_channel, frame);
                break;
        case AST_FRAME_BRIDGE_ACTION:
-               softmix_pass_everyone_else(bridge, bridge_channel, frame);
+               res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
                break;
+       case AST_FRAME_BRIDGE_ACTION_SYNC:
+               ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n");
+               ast_assert(0);
        default:
-               ast_debug(3, "Frame type %d unsupported\n", frame->frametype);
-               res = -1;
+               ast_debug(3, "Frame type %u unsupported\n", frame->frametype);
+               /* "Accept" the frame and discard it. */
                break;
        }
 
@@ -674,8 +731,8 @@ static void gather_softmix_stats(struct softmix_stats *stats,
        int channel_native_rate;
        int i;
        /* Gather stats about channel sample rates. */
-       channel_native_rate = MAX(ast_format_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
-               ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan)));
+       channel_native_rate = MAX(ast_format_get_sample_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
+               ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan)));
 
        if (channel_native_rate > stats->highest_supported_rate) {
                stats->highest_supported_rate = channel_native_rate;
@@ -702,7 +759,7 @@ static void gather_softmix_stats(struct softmix_stats *stats,
  * if necessary.
  *
  * \retval 0, no changes to internal rate
- * \ratval 1, internal rate was changed, update all the channels on the next mixing iteration.
+ * \retval 1, internal rate was changed, update all the channels on the next mixing iteration.
  */
 static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
 {
@@ -718,7 +775,7 @@ static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct so
                 * from the current rate we are using. */
                if (softmix_data->internal_rate != stats->locked_rate) {
                        softmix_data->internal_rate = stats->locked_rate;
-                       ast_debug(1, "Bridge is locked in at sample rate %d\n",
+                       ast_debug(1, "Bridge is locked in at sample rate %u\n",
                                softmix_data->internal_rate);
                        return 1;
                }
@@ -758,14 +815,14 @@ static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct so
                        }
                }
 
-               ast_debug(1, "Bridge changed from %d To %d\n",
+               ast_debug(1, "Bridge changed from %u To %u\n",
                        softmix_data->internal_rate, best_rate);
                softmix_data->internal_rate = best_rate;
                return 1;
        } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
                /* In this case, the highest supported rate is actually lower than the internal rate */
                softmix_data->internal_rate = stats->highest_supported_rate;
-               ast_debug(1, "Bridge changed from %d to %d\n",
+               ast_debug(1, "Bridge changed from %u to %u\n",
                        softmix_data->internal_rate, stats->highest_supported_rate);
                return 1;
        }
@@ -832,10 +889,14 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
                goto softmix_cleanup;
        }
 
+       /*
+        * XXX Softmix needs to use channel roles to determine who gets
+        * what audio mixed.
+        */
        while (!softmix_data->stop && bridge->num_active) {
                struct ast_bridge_channel *bridge_channel;
                int timeout = -1;
-               enum ast_format_id cur_slin_id = ast_format_slin_by_rate(softmix_data->internal_rate);
+               struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate);
                unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
                unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
 
@@ -844,7 +905,9 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
                         * all the memcpys used during this process depend on this assumption.  Rather
                         * than checking this over and over again through out the code, this single
                         * verification is done on each iteration. */
-                       ast_log(LOG_WARNING, "Conference mixing error, requested mixing length greater than mixing buffer.\n");
+                       ast_log(LOG_WARNING,
+                               "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
+                               bridge->uniqueid);
                        goto softmix_cleanup;
                }
 
@@ -861,7 +924,7 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
                /* These variables help determine if a rate change is required */
                if (!stat_iteration_counter) {
                        memset(&stats, 0, sizeof(stats));
-                       stats.locked_rate = bridge->internal_sample_rate;
+                       stats.locked_rate = bridge->softmix.internal_sample_rate;
                }
 
                /* If the sample rate has changed, update the translator helper */
@@ -915,9 +978,8 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
                        ast_mutex_lock(&sc->lock);
 
                        /* Make SLINEAR write frame from local buffer */
-                       if (sc->write_frame.subclass.format.id != cur_slin_id) {
-                               ast_format_set(&sc->write_frame.subclass.format, cur_slin_id, 0);
-                       }
+                       ao2_t_replace(sc->write_frame.subclass.format, cur_slin,
+                               "Replace softmix channel slin format");
                        sc->write_frame.datalen = softmix_datalen;
                        sc->write_frame.samples = softmix_samples;
                        memcpy(sc->final_buf, buf, softmix_datalen);
@@ -944,15 +1006,17 @@ static int softmix_mixing_loop(struct ast_bridge *bridge)
                /* Wait for the timing source to tell us to wake up and get things done */
                ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
                if (ast_timer_ack(timer, 1) < 0) {
-                       ast_log(LOG_ERROR, "Failed to acknowledge timer in softmix bridge.\n");
+                       ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
+                               bridge->uniqueid);
                        ast_bridge_lock(bridge);
                        goto softmix_cleanup;
                }
                ast_bridge_lock(bridge);
 
                /* make sure to detect mixing interval changes if they occur. */
-               if (bridge->internal_mixing_interval && (bridge->internal_mixing_interval != softmix_data->internal_mixing_interval)) {
-                       softmix_data->internal_mixing_interval = bridge->internal_mixing_interval;
+               if (bridge->softmix.internal_mixing_interval
+                       && (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) {
+                       softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval;
                        ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
                        update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
                }
@@ -977,8 +1041,8 @@ softmix_cleanup:
  */
 static void *softmix_mixing_thread(void *data)
 {
-       struct ast_bridge *bridge = data;
-       struct softmix_bridge_data *softmix_data;
+       struct softmix_bridge_data *softmix_data = data;
+       struct ast_bridge *bridge = softmix_data->bridge;
 
        ast_bridge_lock(bridge);
        if (bridge->callid) {
@@ -987,7 +1051,6 @@ static void *softmix_mixing_thread(void *data)
 
        ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
 
-       softmix_data = bridge->tech_pvt;
        while (!softmix_data->stop) {
                if (!bridge->num_active) {
                        /* Wait for something to happen to the bridge. */
@@ -1026,6 +1089,7 @@ static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data
                softmix_data->timer = NULL;
        }
        ast_mutex_destroy(&softmix_data->lock);
+       ast_cond_destroy(&softmix_data->cond);
        ast_free(softmix_data);
 }
 
@@ -1038,9 +1102,12 @@ static int softmix_bridge_create(struct ast_bridge *bridge)
        if (!softmix_data) {
                return -1;
        }
+       softmix_data->bridge = bridge;
        ast_mutex_init(&softmix_data->lock);
+       ast_cond_init(&softmix_data->cond, NULL);
        softmix_data->timer = ast_timer_open();
        if (!softmix_data->timer) {
+               ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
                softmix_bridge_data_destroy(softmix_data);
                return -1;
        }
@@ -1051,7 +1118,8 @@ static int softmix_bridge_create(struct ast_bridge *bridge)
        bridge->tech_pvt = softmix_data;
 
        /* Start the mixing thread. */
-       if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread, bridge)) {
+       if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread,
+               softmix_data)) {
                softmix_data->thread = AST_PTHREADT_NULL;
                softmix_bridge_data_destroy(softmix_data);
                bridge->tech_pvt = NULL;
@@ -1061,6 +1129,29 @@ static int softmix_bridge_create(struct ast_bridge *bridge)
        return 0;
 }
 
+/*!
+ * \internal
+ * \brief Request the softmix mixing thread stop.
+ * \since 12.0.0
+ *
+ * \param bridge Which bridge is being stopped.
+ *
+ * \return Nothing
+ */
+static void softmix_bridge_stop(struct ast_bridge *bridge)
+{
+       struct softmix_bridge_data *softmix_data;
+
+       softmix_data = bridge->tech_pvt;
+       if (!softmix_data) {
+               return;
+       }
+
+       ast_mutex_lock(&softmix_data->lock);
+       softmix_data->stop = 1;
+       ast_mutex_unlock(&softmix_data->lock);
+}
+
 /*! \brief Function called when a bridge is destroyed */
 static void softmix_bridge_destroy(struct ast_bridge *bridge)
 {
@@ -1080,7 +1171,7 @@ static void softmix_bridge_destroy(struct ast_bridge *bridge)
        softmix_data->thread = AST_PTHREADT_NULL;
        ast_mutex_unlock(&softmix_data->lock);
        if (thread != AST_PTHREADT_NULL) {
-               ast_debug(1, "Waiting for mixing thread to die.\n");
+               ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
                pthread_join(thread, NULL);
        }
 
@@ -1093,6 +1184,7 @@ static struct ast_bridge_technology softmix_bridge = {
        .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
        .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
        .create = softmix_bridge_create,
+       .stop = softmix_bridge_stop,
        .destroy = softmix_bridge_destroy,
        .join = softmix_bridge_join,
        .leave = softmix_bridge_leave,
@@ -1102,17 +1194,17 @@ static struct ast_bridge_technology softmix_bridge = {
 
 static int unload_module(void)
 {
-       ast_format_cap_destroy(softmix_bridge.format_capabilities);
+       ao2_cleanup(softmix_bridge.format_capabilities);
+       softmix_bridge.format_capabilities = NULL;
        return ast_bridge_technology_unregister(&softmix_bridge);
 }
 
 static int load_module(void)
 {
-       struct ast_format tmp;
-       if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc())) {
+       if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                return AST_MODULE_LOAD_DECLINE;
        }
-       ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
+       ast_format_cap_append(softmix_bridge.format_capabilities, ast_format_slin, 0);
        return ast_bridge_technology_register(&softmix_bridge);
 }