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