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