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