bridge_softmix.c: Fix crash if channel fails to join mixing tech.
[asterisk/asterisk.git] / bridges / bridge_softmix.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  * David Vossel <dvossel@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Multi-party software based channel mixing
23  *
24  * \author Joshua Colp <jcolp@digium.com>
25  * \author David Vossel <dvossel@digium.com>
26  *
27  * \ingroup bridges
28  */
29
30 /*** MODULEINFO
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_REGISTER_FILE()
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <errno.h>
44 #include <unistd.h>
45
46 #include "asterisk/module.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/bridge.h"
49 #include "asterisk/bridge_technology.h"
50 #include "asterisk/frame.h"
51 #include "asterisk/options.h"
52 #include "asterisk/logger.h"
53 #include "asterisk/slinfactory.h"
54 #include "asterisk/astobj2.h"
55 #include "asterisk/timing.h"
56 #include "asterisk/translate.h"
57
58 #define MAX_DATALEN 8096
59
60 /*! The minimum sample rate of the bridge. */
61 #define SOFTMIX_MIN_SAMPLE_RATE 8000    /* 8 kHz sample rate */
62
63 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
64 #define DEFAULT_SOFTMIX_INTERVAL 20
65
66 /*! \brief Size of the buffer used for sample manipulation */
67 #define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10))
68
69 /*! \brief Number of samples we are dealing with */
70 #define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2)
71
72 /*! \brief Number of mixing iterations to perform between gathering statistics. */
73 #define SOFTMIX_STAT_INTERVAL 100
74
75 /* This is the threshold in ms at which a channel's own audio will stop getting
76  * mixed out its own write audio stream because it is not talking. */
77 #define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500
78 #define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160
79
80 #define DEFAULT_ENERGY_HISTORY_LEN 150
81
82 struct video_follow_talker_data {
83         /*! audio energy history */
84         int energy_history[DEFAULT_ENERGY_HISTORY_LEN];
85         /*! The current slot being used in the history buffer, this
86          *  increments and wraps around */
87         int energy_history_cur_slot;
88         /*! The current energy sum used for averages. */
89         int energy_accum;
90         /*! The current energy average */
91         int energy_average;
92 };
93
94 /*! \brief Structure which contains per-channel mixing information */
95 struct softmix_channel {
96         /*! Lock to protect this structure */
97         ast_mutex_t lock;
98         /*! Factory which contains audio read in from the channel */
99         struct ast_slinfactory factory;
100         /*! Frame that contains mixed audio to be written out to the channel */
101         struct ast_frame write_frame;
102         /*! Current expected read slinear format. */
103         struct ast_format *read_slin_format;
104         /*! DSP for detecting silence */
105         struct ast_dsp *dsp;
106         /*!
107          * \brief TRUE if a channel is talking.
108          *
109          * \note This affects how the channel's audio is mixed back to
110          * it.
111          */
112         unsigned int talking:1;
113         /*! TRUE if the channel provided audio for this mixing interval */
114         unsigned int have_audio:1;
115         /*! Buffer containing final mixed audio from all sources */
116         short final_buf[MAX_DATALEN];
117         /*! Buffer containing only the audio from the channel */
118         short our_buf[MAX_DATALEN];
119         /*! Data pertaining to talker mode for video conferencing */
120         struct video_follow_talker_data video_talker;
121 };
122
123 struct softmix_bridge_data {
124         struct ast_timer *timer;
125         /*!
126          * \brief Bridge pointer passed to the softmix mixing thread.
127          *
128          * \note Does not need a reference because the bridge will
129          * always exist while the mixing thread exists even if the
130          * bridge is no longer actively using the softmix technology.
131          */
132         struct ast_bridge *bridge;
133         /*! Lock for signaling the mixing thread. */
134         ast_mutex_t lock;
135         /*! Condition, used if we need to wake up the mixing thread. */
136         ast_cond_t cond;
137         /*! Thread handling the mixing */
138         pthread_t thread;
139         unsigned int internal_rate;
140         unsigned int internal_mixing_interval;
141         /*! TRUE if the mixing thread should stop */
142         unsigned int stop:1;
143 };
144
145 struct softmix_stats {
146         /*! Each index represents a sample rate used above the internal rate. */
147         unsigned int sample_rates[16];
148         /*! Each index represents the number of channels using the same index in the sample_rates array.  */
149         unsigned int num_channels[16];
150         /*! The number of channels above the internal sample rate */
151         unsigned int num_above_internal_rate;
152         /*! The number of channels at the internal sample rate */
153         unsigned int num_at_internal_rate;
154         /*! The absolute highest sample rate preferred by any channel in the bridge */
155         unsigned int highest_supported_rate;
156         /*! Is the sample rate locked by the bridge, if so what is that rate.*/
157         unsigned int locked_rate;
158 };
159
160 struct softmix_mixing_array {
161         unsigned int max_num_entries;
162         unsigned int used_entries;
163         int16_t **buffers;
164 };
165
166 struct softmix_translate_helper_entry {
167         int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
168                                       and re-init if it was usable. */
169         struct ast_format *dst_format; /*!< The destination format for this helper */
170         struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
171         struct ast_frame *out_frame; /*!< The output frame from the last translation */
172         AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
173 };
174
175 struct softmix_translate_helper {
176         struct ast_format *slin_src; /*!< the source format expected for all the translators */
177         AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
178 };
179
180 static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
181 {
182         struct softmix_translate_helper_entry *entry;
183         if (!(entry = ast_calloc(1, sizeof(*entry)))) {
184                 return NULL;
185         }
186         entry->dst_format = ao2_bump(dst);
187         /* initialize this to one so that the first time through the cleanup code after
188            allocation it won't be removed from the entry list */
189         entry->num_times_requested = 1;
190         return entry;
191 }
192
193 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
194 {
195         ao2_cleanup(entry->dst_format);
196
197         if (entry->trans_pvt) {
198                 ast_translator_free_path(entry->trans_pvt);
199         }
200         if (entry->out_frame) {
201                 ast_frfree(entry->out_frame);
202         }
203         ast_free(entry);
204         return NULL;
205 }
206
207 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
208 {
209         memset(trans_helper, 0, sizeof(*trans_helper));
210         trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
211 }
212
213 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
214 {
215         struct softmix_translate_helper_entry *entry;
216
217         while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
218                 softmix_translate_helper_free_entry(entry);
219         }
220 }
221
222 static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
223 {
224         struct softmix_translate_helper_entry *entry;
225
226         trans_helper->slin_src = ast_format_cache_get_slin_by_rate(sample_rate);
227         AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
228                 if (entry->trans_pvt) {
229                         ast_translator_free_path(entry->trans_pvt);
230                         if (!(entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src))) {
231                                 AST_LIST_REMOVE_CURRENT(entry);
232                                 entry = softmix_translate_helper_free_entry(entry);
233                         }
234                 }
235         }
236         AST_LIST_TRAVERSE_SAFE_END;
237 }
238
239 /*!
240  * \internal
241  * \brief Get the next available audio on the softmix channel's read stream
242  * and determine if it should be mixed out or not on the write stream.
243  *
244  * \retval pointer to buffer containing the exact number of samples requested on success.
245  * \retval NULL if no samples are present
246  */
247 static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
248 {
249         if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
250                 ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
251                 sc->have_audio = 1;
252                 return sc->our_buf;
253         }
254         sc->have_audio = 0;
255         return NULL;
256 }
257
258 /*!
259  * \internal
260  * \brief Process a softmix channel's write audio
261  *
262  * \details This function will remove the channel's talking from its own audio if present and
263  * possibly even do the channel's write translation for it depending on how many other
264  * channels use the same write format.
265  */
266 static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
267         struct ast_format *raw_write_fmt,
268         struct softmix_channel *sc)
269 {
270         struct softmix_translate_helper_entry *entry = NULL;
271         int i;
272
273         /* If we provided audio that was not determined to be silence,
274          * then take it out while in slinear format. */
275         if (sc->have_audio && sc->talking) {
276                 for (i = 0; i < sc->write_frame.samples; i++) {
277                         ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
278                 }
279                 /* check to see if any entries exist for the format. if not we'll want
280                    to remove it during cleanup */
281                 AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
282                         if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
283                                 ++entry->num_times_requested;
284                                 break;
285                         }
286                 }
287                 /* do not do any special write translate optimization if we had to make
288                  * a special mix for them to remove their own audio. */
289                 return;
290         }
291
292         /* Attempt to optimize channels using the same translation path/codec. Build a list of entries
293            of translation paths and track the number of references for each type. Each one of the same
294            type should be able to use the same out_frame. Since the optimization is only necessary for
295            multiple channels (>=2) using the same codec make sure resources are allocated only when
296            needed and released when not (see also softmix_translate_helper_cleanup */
297         AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
298                 if (ast_format_cmp(entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
299                         entry->num_times_requested++;
300                 } else {
301                         continue;
302                 }
303                 if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
304                         entry->trans_pvt = ast_translator_build_path(entry->dst_format, trans_helper->slin_src);
305                 }
306                 if (entry->trans_pvt && !entry->out_frame) {
307                         entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
308                 }
309                 if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
310                         ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format);
311                         memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
312                         sc->write_frame.datalen = entry->out_frame->datalen;
313                         sc->write_frame.samples = entry->out_frame->samples;
314                 }
315                 break;
316         }
317
318         /* add new entry into list if this format destination was not matched. */
319         if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
320                 AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
321         }
322 }
323
324 static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
325 {
326         struct softmix_translate_helper_entry *entry;
327
328         AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
329                 /* if it hasn't been requested then remove it */
330                 if (!entry->num_times_requested) {
331                         AST_LIST_REMOVE_CURRENT(entry);
332                         softmix_translate_helper_free_entry(entry);
333                         continue;
334                 }
335
336                 if (entry->out_frame) {
337                         ast_frfree(entry->out_frame);
338                         entry->out_frame = NULL;
339                 }
340
341                 /* nothing is optimized for a single path reference, so there is
342                    no reason to continue to hold onto the codec */
343                 if (entry->num_times_requested == 1 && entry->trans_pvt) {
344                         ast_translator_free_path(entry->trans_pvt);
345                         entry->trans_pvt = NULL;
346                 }
347
348                 /* for each iteration (a mixing run) in the bridge softmix thread the number
349                    of references to a given entry is recalculated, so reset the number of
350                    times requested */
351                 entry->num_times_requested = 0;
352         }
353         AST_LIST_TRAVERSE_SAFE_END;
354 }
355
356 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
357 {
358         struct softmix_channel *sc = bridge_channel->tech_pvt;
359         struct ast_format *slin_format;
360         int setup_fail;
361
362         /* The callers have already ensured that sc is never NULL. */
363         ast_assert(sc != NULL);
364
365         slin_format = ast_format_cache_get_slin_by_rate(rate);
366
367         ast_mutex_lock(&sc->lock);
368         if (reset) {
369                 ast_slinfactory_destroy(&sc->factory);
370                 ast_dsp_free(sc->dsp);
371         }
372
373         /* Setup write frame parameters */
374         sc->write_frame.frametype = AST_FRAME_VOICE;
375         /*
376          * NOTE: The write_frame format holds a reference because translation
377          * could be needed and the format changed to the translated format
378          * for the channel.  The translated format may not be a
379          * static cached format.
380          */
381         ao2_replace(sc->write_frame.subclass.format, slin_format);
382         sc->write_frame.data.ptr = sc->final_buf;
383         sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
384         sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
385
386         /*
387          * NOTE: The read_slin_format does not hold a reference because it
388          * will always be a signed linear format.
389          */
390         sc->read_slin_format = slin_format;
391
392         /* Setup smoother */
393         setup_fail = ast_slinfactory_init_with_format(&sc->factory, slin_format);
394
395         /* set new read and write formats on channel. */
396         ast_channel_lock(bridge_channel->chan);
397         setup_fail |= ast_set_read_format_path(bridge_channel->chan,
398                 ast_channel_rawreadformat(bridge_channel->chan), slin_format);
399         ast_channel_unlock(bridge_channel->chan);
400         setup_fail |= ast_set_write_format(bridge_channel->chan, slin_format);
401
402         /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
403         sc->dsp = ast_dsp_new_with_rate(rate);
404         if (setup_fail || !sc->dsp) {
405                 /* Bad news.  Could not setup the channel for softmix. */
406                 ast_mutex_unlock(&sc->lock);
407                 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
408                 return;
409         }
410
411         /* we want to aggressively detect silence to avoid feedback */
412         if (bridge_channel->tech_args.talking_threshold) {
413                 ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
414         } else {
415                 ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
416         }
417
418         ast_mutex_unlock(&sc->lock);
419 }
420
421 /*!
422  * \internal
423  * \brief Poke the mixing thread in case it is waiting for an active channel.
424  * \since 12.0.0
425  *
426  * \param softmix_data Bridge mixing data.
427  *
428  * \return Nothing
429  */
430 static void softmix_poke_thread(struct softmix_bridge_data *softmix_data)
431 {
432         ast_mutex_lock(&softmix_data->lock);
433         ast_cond_signal(&softmix_data->cond);
434         ast_mutex_unlock(&softmix_data->lock);
435 }
436
437 /*! \brief Function called when a channel is unsuspended from the bridge */
438 static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
439 {
440         if (bridge->tech_pvt) {
441                 softmix_poke_thread(bridge->tech_pvt);
442         }
443 }
444
445 /*!
446  * \internal
447  * \brief Indicate a source change to the channel.
448  * \since 12.0.0
449  *
450  * \param bridge_channel Which channel source is changing.
451  *
452  * \retval 0 on success.
453  * \retval -1 on error.
454  */
455 static int softmix_src_change(struct ast_bridge_channel *bridge_channel)
456 {
457         return ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_SRCCHANGE, NULL, 0);
458 }
459
460 /*! \brief Function called when a channel is joined into the bridge */
461 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
462 {
463         struct softmix_channel *sc;
464         struct softmix_bridge_data *softmix_data;
465
466         softmix_data = bridge->tech_pvt;
467         if (!softmix_data) {
468                 return -1;
469         }
470
471         /* Create a new softmix_channel structure and allocate various things on it */
472         if (!(sc = ast_calloc(1, sizeof(*sc)))) {
473                 return -1;
474         }
475
476         softmix_src_change(bridge_channel);
477
478         /* Can't forget the lock */
479         ast_mutex_init(&sc->lock);
480
481         /* Can't forget to record our pvt structure within the bridged channel structure */
482         bridge_channel->tech_pvt = sc;
483
484         set_softmix_bridge_data(softmix_data->internal_rate,
485                 softmix_data->internal_mixing_interval
486                         ? softmix_data->internal_mixing_interval
487                         : DEFAULT_SOFTMIX_INTERVAL,
488                 bridge_channel, 0);
489
490         softmix_poke_thread(softmix_data);
491         return 0;
492 }
493
494 /*! \brief Function called when a channel leaves the bridge */
495 static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
496 {
497         struct softmix_channel *sc = bridge_channel->tech_pvt;
498
499         if (!sc) {
500                 return;
501         }
502         bridge_channel->tech_pvt = NULL;
503
504         softmix_src_change(bridge_channel);
505
506         /* Drop mutex lock */
507         ast_mutex_destroy(&sc->lock);
508
509         /* Drop the factory */
510         ast_slinfactory_destroy(&sc->factory);
511
512         /* Drop any formats on the frames */
513         ao2_cleanup(sc->write_frame.subclass.format);
514
515         /* Drop the DSP */
516         ast_dsp_free(sc->dsp);
517
518         /* Eep! drop ourselves */
519         ast_free(sc);
520 }
521
522 static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
523 {
524         struct ast_bridge_channel *cur;
525
526         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
527                 if (cur->suspended) {
528                         continue;
529                 }
530                 if (ast_bridge_is_video_src(bridge, cur->chan) == 1) {
531                         ast_bridge_channel_queue_frame(cur, frame);
532                         break;
533                 }
534         }
535 }
536
537 /*!
538  * \internal
539  * \brief Determine what to do with a video frame.
540  * \since 12.0.0
541  *
542  * \param bridge Which bridge is getting the frame
543  * \param bridge_channel Which channel is writing the frame.
544  * \param frame What is being written.
545  *
546  * \return Nothing
547  */
548 static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
549 {
550         struct softmix_channel *sc;
551         int video_src_priority;
552
553         /* Determine if the video frame should be distributed or not */
554         switch (bridge->softmix.video_mode.mode) {
555         case AST_BRIDGE_VIDEO_MODE_NONE:
556                 break;
557         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
558                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
559                 if (video_src_priority == 1) {
560                         /* Pass to me and everyone else. */
561                         ast_bridge_queue_everyone_else(bridge, NULL, frame);
562                 }
563                 break;
564         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
565                 sc = bridge_channel->tech_pvt;
566                 ast_mutex_lock(&sc->lock);
567                 ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
568                         sc->video_talker.energy_average,
569                         frame->subclass.frame_ending);
570                 ast_mutex_unlock(&sc->lock);
571                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
572                 if (video_src_priority == 1) {
573                         int num_src = ast_bridge_number_video_src(bridge);
574                         int echo = num_src > 1 ? 0 : 1;
575
576                         ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
577                 } else if (video_src_priority == 2) {
578                         softmix_pass_video_top_priority(bridge, frame);
579                 }
580                 break;
581         }
582 }
583
584 /*!
585  * \internal
586  * \brief Determine what to do with a voice frame.
587  * \since 12.0.0
588  *
589  * \param bridge Which bridge is getting the frame
590  * \param bridge_channel Which channel is writing the frame.
591  * \param frame What is being written.
592  *
593  * \return Nothing
594  */
595 static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
596 {
597         struct softmix_channel *sc = bridge_channel->tech_pvt;
598         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
599         int totalsilence = 0;
600         int cur_energy = 0;
601         int silence_threshold = bridge_channel->tech_args.silence_threshold ?
602                 bridge_channel->tech_args.silence_threshold :
603                 DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
604         char update_talking = -1;  /* if this is set to 0 or 1, tell the bridge that the channel has started or stopped talking. */
605
606         /* Write the frame into the conference */
607         ast_mutex_lock(&sc->lock);
608
609         if (ast_format_cmp(frame->subclass.format, sc->read_slin_format) != AST_FORMAT_CMP_EQUAL) {
610                 /*
611                  * The incoming frame is not the expected format.  Update
612                  * the channel's translation path to get us slinear from
613                  * the new format for the next frame.
614                  *
615                  * There is the possibility that this frame is an old slinear
616                  * rate frame that was in flight when the softmix bridge
617                  * changed rates.  If so it will self correct on subsequent
618                  * frames.
619                  */
620                 ast_channel_lock(bridge_channel->chan);
621                 ast_debug(1, "Channel %s wrote unexpected format into bridge.  Got %s, expected %s.\n",
622                         ast_channel_name(bridge_channel->chan),
623                         ast_format_get_name(frame->subclass.format),
624                         ast_format_get_name(sc->read_slin_format));
625                 ast_set_read_format_path(bridge_channel->chan, frame->subclass.format,
626                         sc->read_slin_format);
627                 ast_channel_unlock(bridge_channel->chan);
628         }
629
630         /* The channel will be leaving soon if there is no dsp. */
631         if (sc->dsp) {
632                 ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
633         }
634
635         if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
636                 int cur_slot = sc->video_talker.energy_history_cur_slot;
637
638                 sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
639                 sc->video_talker.energy_accum += cur_energy;
640                 sc->video_talker.energy_history[cur_slot] = cur_energy;
641                 sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
642                 sc->video_talker.energy_history_cur_slot++;
643                 if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
644                         sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
645                 }
646         }
647
648         if (totalsilence < silence_threshold) {
649                 if (!sc->talking) {
650                         update_talking = 1;
651                 }
652                 sc->talking = 1; /* tell the write process we have audio to be mixed out */
653         } else {
654                 if (sc->talking) {
655                         update_talking = 0;
656                 }
657                 sc->talking = 0;
658         }
659
660         /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
661          * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
662          * the audio by flushing the buffer before adding new audio in. */
663         if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
664                 ast_slinfactory_flush(&sc->factory);
665         }
666
667         /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
668          * is not determined to be talking. */
669         if (!(bridge_channel->tech_args.drop_silence && !sc->talking)) {
670                 ast_slinfactory_feed(&sc->factory, frame);
671         }
672
673         /* Alllll done */
674         ast_mutex_unlock(&sc->lock);
675
676         if (update_talking != -1) {
677                 ast_bridge_channel_notify_talking(bridge_channel, update_talking);
678         }
679 }
680
681 /*!
682  * \internal
683  * \brief Determine what to do with a control frame.
684  * \since 12.0.0
685  *
686  * \param bridge Which bridge is getting the frame
687  * \param bridge_channel Which channel is writing the frame.
688  * \param frame What is being written.
689  *
690  * \retval 0 Frame accepted into the bridge.
691  * \retval -1 Frame needs to be deferred.
692  */
693 static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
694 {
695         /*
696          * XXX Softmix needs to use channel roles to determine what to
697          * do with control frames.
698          */
699         return 0;
700 }
701
702 /*!
703  * \internal
704  * \brief Determine what to do with a frame written into the bridge.
705  * \since 12.0.0
706  *
707  * \param bridge Which bridge is getting the frame
708  * \param bridge_channel Which channel is writing the frame.
709  * \param frame What is being written.
710  *
711  * \retval 0 Frame accepted into the bridge.
712  * \retval -1 Frame needs to be deferred.
713  *
714  * \note On entry, bridge is already locked.
715  */
716 static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
717 {
718         int res = 0;
719
720         if (!bridge->tech_pvt || !bridge_channel || !bridge_channel->tech_pvt) {
721                 /* "Accept" the frame and discard it. */
722                 return 0;
723         }
724
725         /*
726          * XXX Softmix needs to use channel roles to determine who gets
727          * what frame.  Possible roles: announcer, recorder, agent,
728          * supervisor.
729          */
730         switch (frame->frametype) {
731         case AST_FRAME_NULL:
732                 /* "Accept" the frame and discard it. */
733                 break;
734         case AST_FRAME_DTMF_BEGIN:
735         case AST_FRAME_DTMF_END:
736                 res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
737                 break;
738         case AST_FRAME_VOICE:
739                 if (bridge_channel) {
740                         softmix_bridge_write_voice(bridge, bridge_channel, frame);
741                 }
742                 break;
743         case AST_FRAME_VIDEO:
744                 if (bridge_channel) {
745                         softmix_bridge_write_video(bridge, bridge_channel, frame);
746                 }
747                 break;
748         case AST_FRAME_CONTROL:
749                 res = softmix_bridge_write_control(bridge, bridge_channel, frame);
750                 break;
751         case AST_FRAME_BRIDGE_ACTION:
752                 res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
753                 break;
754         case AST_FRAME_BRIDGE_ACTION_SYNC:
755                 ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n");
756                 ast_assert(0);
757         default:
758                 ast_debug(3, "Frame type %u unsupported\n", frame->frametype);
759                 /* "Accept" the frame and discard it. */
760                 break;
761         }
762
763         return res;
764 }
765
766 static void gather_softmix_stats(struct softmix_stats *stats,
767         const struct softmix_bridge_data *softmix_data,
768         struct ast_bridge_channel *bridge_channel)
769 {
770         int channel_native_rate;
771
772         /* Gather stats about channel sample rates. */
773         ast_channel_lock(bridge_channel->chan);
774         channel_native_rate = MAX(SOFTMIX_MIN_SAMPLE_RATE,
775                 ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan)));
776         ast_channel_unlock(bridge_channel->chan);
777
778         if (stats->highest_supported_rate < channel_native_rate) {
779                 stats->highest_supported_rate = channel_native_rate;
780         }
781         if (softmix_data->internal_rate < channel_native_rate) {
782                 int i;
783
784                 for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
785                         if (stats->sample_rates[i] == channel_native_rate) {
786                                 stats->num_channels[i]++;
787                                 break;
788                         } else if (!stats->sample_rates[i]) {
789                                 stats->sample_rates[i] = channel_native_rate;
790                                 stats->num_channels[i]++;
791                                 break;
792                         }
793                 }
794                 stats->num_above_internal_rate++;
795         } else if (softmix_data->internal_rate == channel_native_rate) {
796                 stats->num_at_internal_rate++;
797         }
798 }
799
800 /*!
801  * \internal
802  * \brief Analyse mixing statistics and change bridges internal rate
803  * if necessary.
804  *
805  * \retval 0, no changes to internal rate
806  * \retval 1, internal rate was changed, update all the channels on the next mixing iteration.
807  */
808 static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
809 {
810         int i;
811
812         /*
813          * Re-adjust the internal bridge sample rate if
814          * 1. The bridge's internal sample rate is locked in at a sample
815          *    rate other than the current sample rate being used.
816          * 2. two or more channels support a higher sample rate
817          * 3. no channels support the current sample rate or a higher rate
818          */
819         if (stats->locked_rate) {
820                 /* if the rate is locked by the bridge, only update it if it differs
821                  * from the current rate we are using. */
822                 if (softmix_data->internal_rate != stats->locked_rate) {
823                         ast_debug(1, "Locking at new rate.  Bridge changed from %u to %u.\n",
824                                 softmix_data->internal_rate, stats->locked_rate);
825                         softmix_data->internal_rate = stats->locked_rate;
826                         return 1;
827                 }
828         } else if (stats->num_above_internal_rate >= 2) {
829                 /* the highest rate is just used as a starting point */
830                 unsigned int best_rate = stats->highest_supported_rate;
831                 int best_index = -1;
832
833                 for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
834                         if (stats->num_channels[i]) {
835                                 break;
836                         }
837                         if (2 <= stats->num_channels[i]) {
838                                 /* Two or more channels support this rate. */
839                                 if (best_index == -1
840                                         || stats->sample_rates[best_index] < stats->sample_rates[i]) {
841                                         /*
842                                          * best_rate starts out being the first sample rate
843                                          * greater than the internal sample rate that two or
844                                          * more channels support.
845                                          *
846                                          * or
847                                          *
848                                          * There are multiple rates above the internal rate
849                                          * and this rate is higher than the previous rate two
850                                          * or more channels support.
851                                          */
852                                         best_rate = stats->sample_rates[i];
853                                         best_index = i;
854                                 }
855                         } else if (best_index == -1) {
856                                 /*
857                                  * It is possible that multiple channels exist with native sample
858                                  * rates above the internal sample rate, but none of those channels
859                                  * have the same rate in common.  In this case, the lowest sample
860                                  * rate among those channels is picked. Over time as additional
861                                  * statistic runs are made the internal sample rate number will
862                                  * adjust to the most optimal sample rate, but it may take multiple
863                                  * iterations.
864                                  */
865                                 best_rate = MIN(best_rate, stats->sample_rates[i]);
866                         }
867                 }
868
869                 ast_debug(1, "Multiple above internal rate.  Bridge changed from %u to %u.\n",
870                         softmix_data->internal_rate, best_rate);
871                 softmix_data->internal_rate = best_rate;
872                 return 1;
873         } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
874                 /* In this case, the highest supported rate is actually lower than the internal rate */
875                 ast_debug(1, "All below internal rate.  Bridge changed from %u to %u.\n",
876                         softmix_data->internal_rate, stats->highest_supported_rate);
877                 softmix_data->internal_rate = stats->highest_supported_rate;
878                 return 1;
879         }
880         return 0;
881 }
882
883 static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
884 {
885         memset(mixing_array, 0, sizeof(*mixing_array));
886         mixing_array->max_num_entries = starting_num_entries;
887         if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
888                 ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
889                 return -1;
890         }
891         return 0;
892 }
893
894 static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
895 {
896         ast_free(mixing_array->buffers);
897 }
898
899 static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
900 {
901         int16_t **tmp;
902         /* give it some room to grow since memory is cheap but allocations can be expensive */
903         mixing_array->max_num_entries = num_entries;
904         if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
905                 ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
906                 return -1;
907         }
908         mixing_array->buffers = tmp;
909         return 0;
910 }
911
912 /*!
913  * \brief Mixing loop.
914  *
915  * \retval 0 on success
916  * \retval -1 on failure
917  */
918 static int softmix_mixing_loop(struct ast_bridge *bridge)
919 {
920         struct softmix_stats stats = { { 0 }, };
921         struct softmix_mixing_array mixing_array;
922         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
923         struct ast_timer *timer;
924         struct softmix_translate_helper trans_helper;
925         int16_t buf[MAX_DATALEN];
926         unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
927         int timingfd;
928         int update_all_rates = 0; /* set this when the internal sample rate has changed */
929         unsigned int idx;
930         unsigned int x;
931         int res = -1;
932
933         timer = softmix_data->timer;
934         timingfd = ast_timer_fd(timer);
935         softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
936         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
937
938         /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
939         if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10)) {
940                 goto softmix_cleanup;
941         }
942
943         /*
944          * XXX Softmix needs to use channel roles to determine who gets
945          * what audio mixed.
946          */
947         while (!softmix_data->stop && bridge->num_active) {
948                 struct ast_bridge_channel *bridge_channel;
949                 int timeout = -1;
950                 struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate);
951                 unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
952                 unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
953
954                 if (softmix_datalen > MAX_DATALEN) {
955                         /* This should NEVER happen, but if it does we need to know about it. Almost
956                          * all the memcpys used during this process depend on this assumption.  Rather
957                          * than checking this over and over again through out the code, this single
958                          * verification is done on each iteration. */
959                         ast_log(LOG_WARNING,
960                                 "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
961                                 bridge->uniqueid);
962                         goto softmix_cleanup;
963                 }
964
965                 /* Grow the mixing array buffer as participants are added. */
966                 if (mixing_array.max_num_entries < bridge->num_channels
967                         && softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5)) {
968                         goto softmix_cleanup;
969                 }
970
971                 /* init the number of buffers stored in the mixing array to 0.
972                  * As buffers are added for mixing, this number is incremented. */
973                 mixing_array.used_entries = 0;
974
975                 /* These variables help determine if a rate change is required */
976                 if (!stat_iteration_counter) {
977                         memset(&stats, 0, sizeof(stats));
978                         stats.locked_rate = bridge->softmix.internal_sample_rate;
979                 }
980
981                 /* If the sample rate has changed, update the translator helper */
982                 if (update_all_rates) {
983                         softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
984                 }
985
986                 /* Go through pulling audio from each factory that has it available */
987                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
988                         struct softmix_channel *sc = bridge_channel->tech_pvt;
989
990                         if (!sc) {
991                                 /* This channel failed to join successfully. */
992                                 continue;
993                         }
994
995                         /* Update the sample rate to match the bridge's native sample rate if necessary. */
996                         if (update_all_rates) {
997                                 set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
998                         }
999
1000                         /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
1001                         if (!stat_iteration_counter) {
1002                                 gather_softmix_stats(&stats, softmix_data, bridge_channel);
1003                         }
1004
1005                         /* if the channel is suspended, don't check for audio, but still gather stats */
1006                         if (bridge_channel->suspended) {
1007                                 continue;
1008                         }
1009
1010                         /* Try to get audio from the factory if available */
1011                         ast_mutex_lock(&sc->lock);
1012                         if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
1013                                 mixing_array.used_entries++;
1014                         }
1015                         ast_mutex_unlock(&sc->lock);
1016                 }
1017
1018                 /* mix it like crazy */
1019                 memset(buf, 0, softmix_datalen);
1020                 for (idx = 0; idx < mixing_array.used_entries; ++idx) {
1021                         for (x = 0; x < softmix_samples; ++x) {
1022                                 ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x);
1023                         }
1024                 }
1025
1026                 /* Next step go through removing the channel's own audio and creating a good frame... */
1027                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1028                         struct softmix_channel *sc = bridge_channel->tech_pvt;
1029
1030                         if (!sc || bridge_channel->suspended) {
1031                                 /* This channel failed to join successfully or is suspended. */
1032                                 continue;
1033                         }
1034
1035                         ast_mutex_lock(&sc->lock);
1036
1037                         /* Make SLINEAR write frame from local buffer */
1038                         ao2_t_replace(sc->write_frame.subclass.format, cur_slin,
1039                                 "Replace softmix channel slin format");
1040                         sc->write_frame.datalen = softmix_datalen;
1041                         sc->write_frame.samples = softmix_samples;
1042                         memcpy(sc->final_buf, buf, softmix_datalen);
1043
1044                         /* process the softmix channel's new write audio */
1045                         softmix_process_write_audio(&trans_helper, ast_channel_rawwriteformat(bridge_channel->chan), sc);
1046
1047                         ast_mutex_unlock(&sc->lock);
1048
1049                         /* A frame is now ready for the channel. */
1050                         ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame);
1051                 }
1052
1053                 update_all_rates = 0;
1054                 if (!stat_iteration_counter) {
1055                         update_all_rates = analyse_softmix_stats(&stats, softmix_data);
1056                         stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
1057                 }
1058                 stat_iteration_counter--;
1059
1060                 ast_bridge_unlock(bridge);
1061                 /* cleanup any translation frame data from the previous mixing iteration. */
1062                 softmix_translate_helper_cleanup(&trans_helper);
1063                 /* Wait for the timing source to tell us to wake up and get things done */
1064                 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
1065                 if (ast_timer_ack(timer, 1) < 0) {
1066                         ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
1067                                 bridge->uniqueid);
1068                         ast_bridge_lock(bridge);
1069                         goto softmix_cleanup;
1070                 }
1071                 ast_bridge_lock(bridge);
1072
1073                 /* make sure to detect mixing interval changes if they occur. */
1074                 if (bridge->softmix.internal_mixing_interval
1075                         && (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) {
1076                         softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval;
1077                         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
1078                         update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
1079                 }
1080         }
1081
1082         res = 0;
1083
1084 softmix_cleanup:
1085         softmix_translate_helper_destroy(&trans_helper);
1086         softmix_mixing_array_destroy(&mixing_array);
1087         return res;
1088 }
1089
1090 /*!
1091  * \internal
1092  * \brief Mixing thread.
1093  * \since 12.0.0
1094  *
1095  * \note The thread does not have its own reference to the
1096  * bridge.  The lifetime of the thread is tied to the lifetime
1097  * of the mixing technology association with the bridge.
1098  */
1099 static void *softmix_mixing_thread(void *data)
1100 {
1101         struct softmix_bridge_data *softmix_data = data;
1102         struct ast_bridge *bridge = softmix_data->bridge;
1103
1104         ast_bridge_lock(bridge);
1105         if (bridge->callid) {
1106                 ast_callid_threadassoc_add(bridge->callid);
1107         }
1108
1109         ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
1110
1111         while (!softmix_data->stop) {
1112                 if (!bridge->num_active) {
1113                         /* Wait for something to happen to the bridge. */
1114                         ast_bridge_unlock(bridge);
1115                         ast_mutex_lock(&softmix_data->lock);
1116                         if (!softmix_data->stop) {
1117                                 ast_cond_wait(&softmix_data->cond, &softmix_data->lock);
1118                         }
1119                         ast_mutex_unlock(&softmix_data->lock);
1120                         ast_bridge_lock(bridge);
1121                         continue;
1122                 }
1123
1124                 if (softmix_mixing_loop(bridge)) {
1125                         /*
1126                          * A mixing error occurred.  Sleep and try again later so we
1127                          * won't flood the logs.
1128                          */
1129                         ast_bridge_unlock(bridge);
1130                         sleep(1);
1131                         ast_bridge_lock(bridge);
1132                 }
1133         }
1134
1135         ast_bridge_unlock(bridge);
1136
1137         ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid);
1138
1139         return NULL;
1140 }
1141
1142 static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data)
1143 {
1144         if (softmix_data->timer) {
1145                 ast_timer_close(softmix_data->timer);
1146                 softmix_data->timer = NULL;
1147         }
1148         ast_mutex_destroy(&softmix_data->lock);
1149         ast_cond_destroy(&softmix_data->cond);
1150         ast_free(softmix_data);
1151 }
1152
1153 /*! \brief Function called when a bridge is created */
1154 static int softmix_bridge_create(struct ast_bridge *bridge)
1155 {
1156         struct softmix_bridge_data *softmix_data;
1157
1158         softmix_data = ast_calloc(1, sizeof(*softmix_data));
1159         if (!softmix_data) {
1160                 return -1;
1161         }
1162         softmix_data->bridge = bridge;
1163         ast_mutex_init(&softmix_data->lock);
1164         ast_cond_init(&softmix_data->cond, NULL);
1165         softmix_data->timer = ast_timer_open();
1166         if (!softmix_data->timer) {
1167                 ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
1168                 softmix_bridge_data_destroy(softmix_data);
1169                 return -1;
1170         }
1171         /* start at minimum rate, let it grow from there */
1172         softmix_data->internal_rate = SOFTMIX_MIN_SAMPLE_RATE;
1173         softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
1174
1175         bridge->tech_pvt = softmix_data;
1176
1177         /* Start the mixing thread. */
1178         if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread,
1179                 softmix_data)) {
1180                 softmix_data->thread = AST_PTHREADT_NULL;
1181                 softmix_bridge_data_destroy(softmix_data);
1182                 bridge->tech_pvt = NULL;
1183                 return -1;
1184         }
1185
1186         return 0;
1187 }
1188
1189 /*!
1190  * \internal
1191  * \brief Request the softmix mixing thread stop.
1192  * \since 12.0.0
1193  *
1194  * \param bridge Which bridge is being stopped.
1195  *
1196  * \return Nothing
1197  */
1198 static void softmix_bridge_stop(struct ast_bridge *bridge)
1199 {
1200         struct softmix_bridge_data *softmix_data;
1201
1202         softmix_data = bridge->tech_pvt;
1203         if (!softmix_data) {
1204                 return;
1205         }
1206
1207         ast_mutex_lock(&softmix_data->lock);
1208         softmix_data->stop = 1;
1209         ast_mutex_unlock(&softmix_data->lock);
1210 }
1211
1212 /*! \brief Function called when a bridge is destroyed */
1213 static void softmix_bridge_destroy(struct ast_bridge *bridge)
1214 {
1215         struct softmix_bridge_data *softmix_data;
1216         pthread_t thread;
1217
1218         softmix_data = bridge->tech_pvt;
1219         if (!softmix_data) {
1220                 return;
1221         }
1222
1223         /* Stop the mixing thread. */
1224         ast_mutex_lock(&softmix_data->lock);
1225         softmix_data->stop = 1;
1226         ast_cond_signal(&softmix_data->cond);
1227         thread = softmix_data->thread;
1228         softmix_data->thread = AST_PTHREADT_NULL;
1229         ast_mutex_unlock(&softmix_data->lock);
1230         if (thread != AST_PTHREADT_NULL) {
1231                 ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
1232                 pthread_join(thread, NULL);
1233         }
1234
1235         softmix_bridge_data_destroy(softmix_data);
1236         bridge->tech_pvt = NULL;
1237 }
1238
1239 static struct ast_bridge_technology softmix_bridge = {
1240         .name = "softmix",
1241         .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
1242         .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
1243         .create = softmix_bridge_create,
1244         .stop = softmix_bridge_stop,
1245         .destroy = softmix_bridge_destroy,
1246         .join = softmix_bridge_join,
1247         .leave = softmix_bridge_leave,
1248         .unsuspend = softmix_bridge_unsuspend,
1249         .write = softmix_bridge_write,
1250 };
1251
1252 static int unload_module(void)
1253 {
1254         ast_bridge_technology_unregister(&softmix_bridge);
1255         return 0;
1256 }
1257
1258 static int load_module(void)
1259 {
1260         if (ast_bridge_technology_register(&softmix_bridge)) {
1261                 unload_module();
1262                 return AST_MODULE_LOAD_DECLINE;
1263         }
1264         return AST_MODULE_LOAD_SUCCESS;
1265 }
1266
1267 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");