a0b1474971638d8f8aa28174d45ed6b3fa20b114
[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 /*! \brief Function called when a channel is joined into the bridge */
444 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
445 {
446         struct softmix_channel *sc;
447         struct softmix_bridge_data *softmix_data;
448
449         softmix_data = bridge->tech_pvt;
450         if (!softmix_data) {
451                 return -1;
452         }
453
454         /* Create a new softmix_channel structure and allocate various things on it */
455         if (!(sc = ast_calloc(1, sizeof(*sc)))) {
456                 return -1;
457         }
458
459         /* Can't forget the lock */
460         ast_mutex_init(&sc->lock);
461
462         /* Can't forget to record our pvt structure within the bridged channel structure */
463         bridge_channel->tech_pvt = sc;
464
465         set_softmix_bridge_data(softmix_data->internal_rate,
466                 softmix_data->internal_mixing_interval
467                         ? softmix_data->internal_mixing_interval
468                         : DEFAULT_SOFTMIX_INTERVAL,
469                 bridge_channel, 0);
470
471         softmix_poke_thread(softmix_data);
472         return 0;
473 }
474
475 /*! \brief Function called when a channel leaves the bridge */
476 static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
477 {
478         struct softmix_channel *sc = bridge_channel->tech_pvt;
479
480         if (!sc) {
481                 return;
482         }
483         bridge_channel->tech_pvt = NULL;
484
485         /* Drop mutex lock */
486         ast_mutex_destroy(&sc->lock);
487
488         /* Drop the factory */
489         ast_slinfactory_destroy(&sc->factory);
490
491         /* Drop any formats on the frames */
492         ao2_cleanup(sc->write_frame.subclass.format);
493
494         /* Drop the DSP */
495         ast_dsp_free(sc->dsp);
496
497         /* Eep! drop ourselves */
498         ast_free(sc);
499 }
500
501 static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
502 {
503         struct ast_bridge_channel *cur;
504
505         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
506                 if (cur->suspended) {
507                         continue;
508                 }
509                 if (ast_bridge_is_video_src(bridge, cur->chan) == 1) {
510                         ast_bridge_channel_queue_frame(cur, frame);
511                         break;
512                 }
513         }
514 }
515
516 /*!
517  * \internal
518  * \brief Determine what to do with a video frame.
519  * \since 12.0.0
520  *
521  * \param bridge Which bridge is getting the frame
522  * \param bridge_channel Which channel is writing the frame.
523  * \param frame What is being written.
524  *
525  * \return Nothing
526  */
527 static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
528 {
529         struct softmix_channel *sc;
530         int video_src_priority;
531
532         /* Determine if the video frame should be distributed or not */
533         switch (bridge->softmix.video_mode.mode) {
534         case AST_BRIDGE_VIDEO_MODE_NONE:
535                 break;
536         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
537                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
538                 if (video_src_priority == 1) {
539                         /* Pass to me and everyone else. */
540                         ast_bridge_queue_everyone_else(bridge, NULL, frame);
541                 }
542                 break;
543         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
544                 sc = bridge_channel->tech_pvt;
545                 ast_mutex_lock(&sc->lock);
546                 ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
547                         sc->video_talker.energy_average,
548                         frame->subclass.frame_ending);
549                 ast_mutex_unlock(&sc->lock);
550                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
551                 if (video_src_priority == 1) {
552                         int num_src = ast_bridge_number_video_src(bridge);
553                         int echo = num_src > 1 ? 0 : 1;
554
555                         ast_bridge_queue_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
556                 } else if (video_src_priority == 2) {
557                         softmix_pass_video_top_priority(bridge, frame);
558                 }
559                 break;
560         }
561 }
562
563 /*!
564  * \internal
565  * \brief Determine what to do with a voice frame.
566  * \since 12.0.0
567  *
568  * \param bridge Which bridge is getting the frame
569  * \param bridge_channel Which channel is writing the frame.
570  * \param frame What is being written.
571  *
572  * \return Nothing
573  */
574 static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
575 {
576         struct softmix_channel *sc = bridge_channel->tech_pvt;
577         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
578         int totalsilence = 0;
579         int cur_energy = 0;
580         int silence_threshold = bridge_channel->tech_args.silence_threshold ?
581                 bridge_channel->tech_args.silence_threshold :
582                 DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
583         char update_talking = -1;  /* if this is set to 0 or 1, tell the bridge that the channel has started or stopped talking. */
584
585         /* Write the frame into the conference */
586         ast_mutex_lock(&sc->lock);
587
588         if (ast_format_cmp(frame->subclass.format, sc->read_slin_format) != AST_FORMAT_CMP_EQUAL) {
589                 /*
590                  * The incoming frame is not the expected format.  Update
591                  * the channel's translation path to get us slinear from
592                  * the new format for the next frame.
593                  *
594                  * There is the possibility that this frame is an old slinear
595                  * rate frame that was in flight when the softmix bridge
596                  * changed rates.  If so it will self correct on subsequent
597                  * frames.
598                  */
599                 ast_channel_lock(bridge_channel->chan);
600                 ast_debug(1, "Channel %s wrote unexpected format into bridge.  Got %s, expected %s.\n",
601                         ast_channel_name(bridge_channel->chan),
602                         ast_format_get_name(frame->subclass.format),
603                         ast_format_get_name(sc->read_slin_format));
604                 ast_set_read_format_path(bridge_channel->chan, frame->subclass.format,
605                         sc->read_slin_format);
606                 ast_channel_unlock(bridge_channel->chan);
607         }
608
609         /* The channel will be leaving soon if there is no dsp. */
610         if (sc->dsp) {
611                 ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
612         }
613
614         if (bridge->softmix.video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
615                 int cur_slot = sc->video_talker.energy_history_cur_slot;
616
617                 sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
618                 sc->video_talker.energy_accum += cur_energy;
619                 sc->video_talker.energy_history[cur_slot] = cur_energy;
620                 sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
621                 sc->video_talker.energy_history_cur_slot++;
622                 if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
623                         sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
624                 }
625         }
626
627         if (totalsilence < silence_threshold) {
628                 if (!sc->talking) {
629                         update_talking = 1;
630                 }
631                 sc->talking = 1; /* tell the write process we have audio to be mixed out */
632         } else {
633                 if (sc->talking) {
634                         update_talking = 0;
635                 }
636                 sc->talking = 0;
637         }
638
639         /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
640          * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
641          * the audio by flushing the buffer before adding new audio in. */
642         if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
643                 ast_slinfactory_flush(&sc->factory);
644         }
645
646         /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
647          * is not determined to be talking. */
648         if (!(bridge_channel->tech_args.drop_silence && !sc->talking)) {
649                 ast_slinfactory_feed(&sc->factory, frame);
650         }
651
652         /* Alllll done */
653         ast_mutex_unlock(&sc->lock);
654
655         if (update_talking != -1) {
656                 ast_bridge_channel_notify_talking(bridge_channel, update_talking);
657         }
658 }
659
660 /*!
661  * \internal
662  * \brief Determine what to do with a control frame.
663  * \since 12.0.0
664  *
665  * \param bridge Which bridge is getting the frame
666  * \param bridge_channel Which channel is writing the frame.
667  * \param frame What is being written.
668  *
669  * \retval 0 Frame accepted into the bridge.
670  * \retval -1 Frame needs to be deferred.
671  */
672 static int softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
673 {
674         /*
675          * XXX Softmix needs to use channel roles to determine what to
676          * do with control frames.
677          */
678
679         switch (frame->subclass.integer) {
680         case AST_CONTROL_VIDUPDATE:
681                 ast_bridge_queue_everyone_else(bridge, NULL, frame);
682                 break;
683         default:
684                 break;
685         }
686
687         return 0;
688 }
689
690 /*!
691  * \internal
692  * \brief Determine what to do with a frame written into the bridge.
693  * \since 12.0.0
694  *
695  * \param bridge Which bridge is getting the frame
696  * \param bridge_channel Which channel is writing the frame.
697  * \param frame What is being written.
698  *
699  * \retval 0 Frame accepted into the bridge.
700  * \retval -1 Frame needs to be deferred.
701  *
702  * \note On entry, bridge is already locked.
703  */
704 static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
705 {
706         int res = 0;
707
708         if (!bridge->tech_pvt || !bridge_channel || !bridge_channel->tech_pvt) {
709                 /* "Accept" the frame and discard it. */
710                 return 0;
711         }
712
713         /*
714          * XXX Softmix needs to use channel roles to determine who gets
715          * what frame.  Possible roles: announcer, recorder, agent,
716          * supervisor.
717          */
718         switch (frame->frametype) {
719         case AST_FRAME_NULL:
720                 /* "Accept" the frame and discard it. */
721                 break;
722         case AST_FRAME_DTMF_BEGIN:
723         case AST_FRAME_DTMF_END:
724                 res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
725                 break;
726         case AST_FRAME_VOICE:
727                 if (bridge_channel) {
728                         softmix_bridge_write_voice(bridge, bridge_channel, frame);
729                 }
730                 break;
731         case AST_FRAME_VIDEO:
732                 if (bridge_channel) {
733                         softmix_bridge_write_video(bridge, bridge_channel, frame);
734                 }
735                 break;
736         case AST_FRAME_CONTROL:
737                 res = softmix_bridge_write_control(bridge, bridge_channel, frame);
738                 break;
739         case AST_FRAME_BRIDGE_ACTION:
740                 res = ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
741                 break;
742         case AST_FRAME_BRIDGE_ACTION_SYNC:
743                 ast_log(LOG_ERROR, "Synchronous bridge action written to a softmix bridge.\n");
744                 ast_assert(0);
745         default:
746                 ast_debug(3, "Frame type %u unsupported\n", frame->frametype);
747                 /* "Accept" the frame and discard it. */
748                 break;
749         }
750
751         return res;
752 }
753
754 static void gather_softmix_stats(struct softmix_stats *stats,
755         const struct softmix_bridge_data *softmix_data,
756         struct ast_bridge_channel *bridge_channel)
757 {
758         int channel_native_rate;
759
760         /* Gather stats about channel sample rates. */
761         ast_channel_lock(bridge_channel->chan);
762         channel_native_rate = MAX(SOFTMIX_MIN_SAMPLE_RATE,
763                 ast_format_get_sample_rate(ast_channel_rawreadformat(bridge_channel->chan)));
764         ast_channel_unlock(bridge_channel->chan);
765
766         if (stats->highest_supported_rate < channel_native_rate) {
767                 stats->highest_supported_rate = channel_native_rate;
768         }
769         if (softmix_data->internal_rate < channel_native_rate) {
770                 int i;
771
772                 for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
773                         if (stats->sample_rates[i] == channel_native_rate) {
774                                 stats->num_channels[i]++;
775                                 break;
776                         } else if (!stats->sample_rates[i]) {
777                                 stats->sample_rates[i] = channel_native_rate;
778                                 stats->num_channels[i]++;
779                                 break;
780                         }
781                 }
782                 stats->num_above_internal_rate++;
783         } else if (softmix_data->internal_rate == channel_native_rate) {
784                 stats->num_at_internal_rate++;
785         }
786 }
787
788 /*!
789  * \internal
790  * \brief Analyse mixing statistics and change bridges internal rate
791  * if necessary.
792  *
793  * \retval 0, no changes to internal rate
794  * \retval 1, internal rate was changed, update all the channels on the next mixing iteration.
795  */
796 static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
797 {
798         int i;
799
800         /*
801          * Re-adjust the internal bridge sample rate if
802          * 1. The bridge's internal sample rate is locked in at a sample
803          *    rate other than the current sample rate being used.
804          * 2. two or more channels support a higher sample rate
805          * 3. no channels support the current sample rate or a higher rate
806          */
807         if (stats->locked_rate) {
808                 /* if the rate is locked by the bridge, only update it if it differs
809                  * from the current rate we are using. */
810                 if (softmix_data->internal_rate != stats->locked_rate) {
811                         ast_debug(1, "Locking at new rate.  Bridge changed from %u to %u.\n",
812                                 softmix_data->internal_rate, stats->locked_rate);
813                         softmix_data->internal_rate = stats->locked_rate;
814                         return 1;
815                 }
816         } else if (stats->num_above_internal_rate >= 2) {
817                 /* the highest rate is just used as a starting point */
818                 unsigned int best_rate = stats->highest_supported_rate;
819                 int best_index = -1;
820
821                 for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
822                         if (stats->num_channels[i]) {
823                                 break;
824                         }
825                         if (2 <= stats->num_channels[i]) {
826                                 /* Two or more channels support this rate. */
827                                 if (best_index == -1
828                                         || stats->sample_rates[best_index] < stats->sample_rates[i]) {
829                                         /*
830                                          * best_rate starts out being the first sample rate
831                                          * greater than the internal sample rate that two or
832                                          * more channels support.
833                                          *
834                                          * or
835                                          *
836                                          * There are multiple rates above the internal rate
837                                          * and this rate is higher than the previous rate two
838                                          * or more channels support.
839                                          */
840                                         best_rate = stats->sample_rates[i];
841                                         best_index = i;
842                                 }
843                         } else if (best_index == -1) {
844                                 /*
845                                  * It is possible that multiple channels exist with native sample
846                                  * rates above the internal sample rate, but none of those channels
847                                  * have the same rate in common.  In this case, the lowest sample
848                                  * rate among those channels is picked. Over time as additional
849                                  * statistic runs are made the internal sample rate number will
850                                  * adjust to the most optimal sample rate, but it may take multiple
851                                  * iterations.
852                                  */
853                                 best_rate = MIN(best_rate, stats->sample_rates[i]);
854                         }
855                 }
856
857                 ast_debug(1, "Multiple above internal rate.  Bridge changed from %u to %u.\n",
858                         softmix_data->internal_rate, best_rate);
859                 softmix_data->internal_rate = best_rate;
860                 return 1;
861         } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
862                 /* In this case, the highest supported rate is actually lower than the internal rate */
863                 ast_debug(1, "All below internal rate.  Bridge changed from %u to %u.\n",
864                         softmix_data->internal_rate, stats->highest_supported_rate);
865                 softmix_data->internal_rate = stats->highest_supported_rate;
866                 return 1;
867         }
868         return 0;
869 }
870
871 static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
872 {
873         memset(mixing_array, 0, sizeof(*mixing_array));
874         mixing_array->max_num_entries = starting_num_entries;
875         if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
876                 ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
877                 return -1;
878         }
879         return 0;
880 }
881
882 static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
883 {
884         ast_free(mixing_array->buffers);
885 }
886
887 static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
888 {
889         int16_t **tmp;
890         /* give it some room to grow since memory is cheap but allocations can be expensive */
891         mixing_array->max_num_entries = num_entries;
892         if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
893                 ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
894                 return -1;
895         }
896         mixing_array->buffers = tmp;
897         return 0;
898 }
899
900 /*!
901  * \brief Mixing loop.
902  *
903  * \retval 0 on success
904  * \retval -1 on failure
905  */
906 static int softmix_mixing_loop(struct ast_bridge *bridge)
907 {
908         struct softmix_stats stats = { { 0 }, };
909         struct softmix_mixing_array mixing_array;
910         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
911         struct ast_timer *timer;
912         struct softmix_translate_helper trans_helper;
913         int16_t buf[MAX_DATALEN];
914         unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
915         int timingfd;
916         int update_all_rates = 0; /* set this when the internal sample rate has changed */
917         unsigned int idx;
918         unsigned int x;
919         int res = -1;
920
921         timer = softmix_data->timer;
922         timingfd = ast_timer_fd(timer);
923         softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
924         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
925
926         /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
927         if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10)) {
928                 goto softmix_cleanup;
929         }
930
931         /*
932          * XXX Softmix needs to use channel roles to determine who gets
933          * what audio mixed.
934          */
935         while (!softmix_data->stop && bridge->num_active) {
936                 struct ast_bridge_channel *bridge_channel;
937                 int timeout = -1;
938                 struct ast_format *cur_slin = ast_format_cache_get_slin_by_rate(softmix_data->internal_rate);
939                 unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
940                 unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
941
942                 if (softmix_datalen > MAX_DATALEN) {
943                         /* This should NEVER happen, but if it does we need to know about it. Almost
944                          * all the memcpys used during this process depend on this assumption.  Rather
945                          * than checking this over and over again through out the code, this single
946                          * verification is done on each iteration. */
947                         ast_log(LOG_WARNING,
948                                 "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
949                                 bridge->uniqueid);
950                         goto softmix_cleanup;
951                 }
952
953                 /* Grow the mixing array buffer as participants are added. */
954                 if (mixing_array.max_num_entries < bridge->num_channels
955                         && softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5)) {
956                         goto softmix_cleanup;
957                 }
958
959                 /* init the number of buffers stored in the mixing array to 0.
960                  * As buffers are added for mixing, this number is incremented. */
961                 mixing_array.used_entries = 0;
962
963                 /* These variables help determine if a rate change is required */
964                 if (!stat_iteration_counter) {
965                         memset(&stats, 0, sizeof(stats));
966                         stats.locked_rate = bridge->softmix.internal_sample_rate;
967                 }
968
969                 /* If the sample rate has changed, update the translator helper */
970                 if (update_all_rates) {
971                         softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
972                 }
973
974                 /* Go through pulling audio from each factory that has it available */
975                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
976                         struct softmix_channel *sc = bridge_channel->tech_pvt;
977
978                         if (!sc) {
979                                 /* This channel failed to join successfully. */
980                                 continue;
981                         }
982
983                         /* Update the sample rate to match the bridge's native sample rate if necessary. */
984                         if (update_all_rates) {
985                                 set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
986                         }
987
988                         /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
989                         if (!stat_iteration_counter) {
990                                 gather_softmix_stats(&stats, softmix_data, bridge_channel);
991                         }
992
993                         /* if the channel is suspended, don't check for audio, but still gather stats */
994                         if (bridge_channel->suspended) {
995                                 continue;
996                         }
997
998                         /* Try to get audio from the factory if available */
999                         ast_mutex_lock(&sc->lock);
1000                         if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
1001                                 mixing_array.used_entries++;
1002                         }
1003                         ast_mutex_unlock(&sc->lock);
1004                 }
1005
1006                 /* mix it like crazy */
1007                 memset(buf, 0, softmix_datalen);
1008                 for (idx = 0; idx < mixing_array.used_entries; ++idx) {
1009                         for (x = 0; x < softmix_samples; ++x) {
1010                                 ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x);
1011                         }
1012                 }
1013
1014                 /* Next step go through removing the channel's own audio and creating a good frame... */
1015                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1016                         struct softmix_channel *sc = bridge_channel->tech_pvt;
1017
1018                         if (!sc || bridge_channel->suspended) {
1019                                 /* This channel failed to join successfully or is suspended. */
1020                                 continue;
1021                         }
1022
1023                         ast_mutex_lock(&sc->lock);
1024
1025                         /* Make SLINEAR write frame from local buffer */
1026                         ao2_t_replace(sc->write_frame.subclass.format, cur_slin,
1027                                 "Replace softmix channel slin format");
1028                         sc->write_frame.datalen = softmix_datalen;
1029                         sc->write_frame.samples = softmix_samples;
1030                         memcpy(sc->final_buf, buf, softmix_datalen);
1031
1032                         /* process the softmix channel's new write audio */
1033                         softmix_process_write_audio(&trans_helper, ast_channel_rawwriteformat(bridge_channel->chan), sc);
1034
1035                         ast_mutex_unlock(&sc->lock);
1036
1037                         /* A frame is now ready for the channel. */
1038                         ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame);
1039                 }
1040
1041                 update_all_rates = 0;
1042                 if (!stat_iteration_counter) {
1043                         update_all_rates = analyse_softmix_stats(&stats, softmix_data);
1044                         stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
1045                 }
1046                 stat_iteration_counter--;
1047
1048                 ast_bridge_unlock(bridge);
1049                 /* cleanup any translation frame data from the previous mixing iteration. */
1050                 softmix_translate_helper_cleanup(&trans_helper);
1051                 /* Wait for the timing source to tell us to wake up and get things done */
1052                 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
1053                 if (ast_timer_ack(timer, 1) < 0) {
1054                         ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
1055                                 bridge->uniqueid);
1056                         ast_bridge_lock(bridge);
1057                         goto softmix_cleanup;
1058                 }
1059                 ast_bridge_lock(bridge);
1060
1061                 /* make sure to detect mixing interval changes if they occur. */
1062                 if (bridge->softmix.internal_mixing_interval
1063                         && (bridge->softmix.internal_mixing_interval != softmix_data->internal_mixing_interval)) {
1064                         softmix_data->internal_mixing_interval = bridge->softmix.internal_mixing_interval;
1065                         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
1066                         update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
1067                 }
1068         }
1069
1070         res = 0;
1071
1072 softmix_cleanup:
1073         softmix_translate_helper_destroy(&trans_helper);
1074         softmix_mixing_array_destroy(&mixing_array);
1075         return res;
1076 }
1077
1078 /*!
1079  * \internal
1080  * \brief Mixing thread.
1081  * \since 12.0.0
1082  *
1083  * \note The thread does not have its own reference to the
1084  * bridge.  The lifetime of the thread is tied to the lifetime
1085  * of the mixing technology association with the bridge.
1086  */
1087 static void *softmix_mixing_thread(void *data)
1088 {
1089         struct softmix_bridge_data *softmix_data = data;
1090         struct ast_bridge *bridge = softmix_data->bridge;
1091
1092         ast_bridge_lock(bridge);
1093         if (bridge->callid) {
1094                 ast_callid_threadassoc_add(bridge->callid);
1095         }
1096
1097         ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
1098
1099         while (!softmix_data->stop) {
1100                 if (!bridge->num_active) {
1101                         /* Wait for something to happen to the bridge. */
1102                         ast_bridge_unlock(bridge);
1103                         ast_mutex_lock(&softmix_data->lock);
1104                         if (!softmix_data->stop) {
1105                                 ast_cond_wait(&softmix_data->cond, &softmix_data->lock);
1106                         }
1107                         ast_mutex_unlock(&softmix_data->lock);
1108                         ast_bridge_lock(bridge);
1109                         continue;
1110                 }
1111
1112                 if (softmix_mixing_loop(bridge)) {
1113                         /*
1114                          * A mixing error occurred.  Sleep and try again later so we
1115                          * won't flood the logs.
1116                          */
1117                         ast_bridge_unlock(bridge);
1118                         sleep(1);
1119                         ast_bridge_lock(bridge);
1120                 }
1121         }
1122
1123         ast_bridge_unlock(bridge);
1124
1125         ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid);
1126
1127         return NULL;
1128 }
1129
1130 static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data)
1131 {
1132         if (softmix_data->timer) {
1133                 ast_timer_close(softmix_data->timer);
1134                 softmix_data->timer = NULL;
1135         }
1136         ast_mutex_destroy(&softmix_data->lock);
1137         ast_cond_destroy(&softmix_data->cond);
1138         ast_free(softmix_data);
1139 }
1140
1141 /*! \brief Function called when a bridge is created */
1142 static int softmix_bridge_create(struct ast_bridge *bridge)
1143 {
1144         struct softmix_bridge_data *softmix_data;
1145
1146         softmix_data = ast_calloc(1, sizeof(*softmix_data));
1147         if (!softmix_data) {
1148                 return -1;
1149         }
1150         softmix_data->bridge = bridge;
1151         ast_mutex_init(&softmix_data->lock);
1152         ast_cond_init(&softmix_data->cond, NULL);
1153         softmix_data->timer = ast_timer_open();
1154         if (!softmix_data->timer) {
1155                 ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
1156                 softmix_bridge_data_destroy(softmix_data);
1157                 return -1;
1158         }
1159         /* start at minimum rate, let it grow from there */
1160         softmix_data->internal_rate = SOFTMIX_MIN_SAMPLE_RATE;
1161         softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
1162
1163         bridge->tech_pvt = softmix_data;
1164
1165         /* Start the mixing thread. */
1166         if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread,
1167                 softmix_data)) {
1168                 softmix_data->thread = AST_PTHREADT_NULL;
1169                 softmix_bridge_data_destroy(softmix_data);
1170                 bridge->tech_pvt = NULL;
1171                 return -1;
1172         }
1173
1174         return 0;
1175 }
1176
1177 /*!
1178  * \internal
1179  * \brief Request the softmix mixing thread stop.
1180  * \since 12.0.0
1181  *
1182  * \param bridge Which bridge is being stopped.
1183  *
1184  * \return Nothing
1185  */
1186 static void softmix_bridge_stop(struct ast_bridge *bridge)
1187 {
1188         struct softmix_bridge_data *softmix_data;
1189
1190         softmix_data = bridge->tech_pvt;
1191         if (!softmix_data) {
1192                 return;
1193         }
1194
1195         ast_mutex_lock(&softmix_data->lock);
1196         softmix_data->stop = 1;
1197         ast_mutex_unlock(&softmix_data->lock);
1198 }
1199
1200 /*! \brief Function called when a bridge is destroyed */
1201 static void softmix_bridge_destroy(struct ast_bridge *bridge)
1202 {
1203         struct softmix_bridge_data *softmix_data;
1204         pthread_t thread;
1205
1206         softmix_data = bridge->tech_pvt;
1207         if (!softmix_data) {
1208                 return;
1209         }
1210
1211         /* Stop the mixing thread. */
1212         ast_mutex_lock(&softmix_data->lock);
1213         softmix_data->stop = 1;
1214         ast_cond_signal(&softmix_data->cond);
1215         thread = softmix_data->thread;
1216         softmix_data->thread = AST_PTHREADT_NULL;
1217         ast_mutex_unlock(&softmix_data->lock);
1218         if (thread != AST_PTHREADT_NULL) {
1219                 ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
1220                 pthread_join(thread, NULL);
1221         }
1222
1223         softmix_bridge_data_destroy(softmix_data);
1224         bridge->tech_pvt = NULL;
1225 }
1226
1227 static struct ast_bridge_technology softmix_bridge = {
1228         .name = "softmix",
1229         .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
1230         .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
1231         .create = softmix_bridge_create,
1232         .stop = softmix_bridge_stop,
1233         .destroy = softmix_bridge_destroy,
1234         .join = softmix_bridge_join,
1235         .leave = softmix_bridge_leave,
1236         .unsuspend = softmix_bridge_unsuspend,
1237         .write = softmix_bridge_write,
1238 };
1239
1240 static int unload_module(void)
1241 {
1242         ast_bridge_technology_unregister(&softmix_bridge);
1243         return 0;
1244 }
1245
1246 static int load_module(void)
1247 {
1248         if (ast_bridge_technology_register(&softmix_bridge)) {
1249                 unload_module();
1250                 return AST_MODULE_LOAD_DECLINE;
1251         }
1252         return AST_MODULE_LOAD_SUCCESS;
1253 }
1254
1255 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");