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