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