Fixes reliability issues with func_jitterbuffer's usage in the new ConfBridge applica...
[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 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <unistd.h>
41
42 #include "asterisk/module.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/bridging.h"
45 #include "asterisk/bridging_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/options.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/slinfactory.h"
50 #include "asterisk/astobj2.h"
51 #include "asterisk/timing.h"
52 #include "asterisk/translate.h"
53
54 #define MAX_DATALEN 8096
55
56 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
57 #define DEFAULT_SOFTMIX_INTERVAL 20
58
59 /*! \brief Size of the buffer used for sample manipulation */
60 #define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10))
61
62 /*! \brief Number of samples we are dealing with */
63 #define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2)
64
65 /*! \brief Number of mixing iterations to perform between gathering statistics. */
66 #define SOFTMIX_STAT_INTERVAL 100
67
68 /* This is the threshold in ms at which a channel's own audio will stop getting
69  * mixed out its own write audio stream because it is not talking. */
70 #define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500
71 #define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160
72
73 /*! \brief Structure which contains per-channel mixing information */
74 struct softmix_channel {
75         /*! Lock to protect this structure */
76         ast_mutex_t lock;
77         /*! Factory which contains audio read in from the channel */
78         struct ast_slinfactory factory;
79         /*! Frame that contains mixed audio to be written out to the channel */
80         struct ast_frame write_frame;
81         /*! Frame that contains mixed audio read from the channel */
82         struct ast_frame read_frame;
83         /*! DSP for detecting silence */
84         struct ast_dsp *dsp;
85         /*! Bit used to indicate if a channel is talking or not. This affects how
86          * the channel's audio is mixed back to it. */
87         int talking:1;
88         /*! Bit used to indicate that the channel provided audio for this mixing interval */
89         int have_audio:1;
90         /*! Bit used to indicate that a frame is available to be written out to the channel */
91         int have_frame:1;
92         /*! Buffer containing final mixed audio from all sources */
93         short final_buf[MAX_DATALEN];
94         /*! Buffer containing only the audio from the channel */
95         short our_buf[MAX_DATALEN];
96 };
97
98 struct softmix_bridge_data {
99         struct ast_timer *timer;
100         unsigned int internal_rate;
101         unsigned int internal_mixing_interval;
102 };
103
104 struct softmix_stats {
105                 /*! Each index represents a sample rate used above the internal rate. */
106                 unsigned int sample_rates[16];
107                 /*! Each index represents the number of channels using the same index in the sample_rates array.  */
108                 unsigned int num_channels[16];
109                 /*! the number of channels above the internal sample rate */
110                 unsigned int num_above_internal_rate;
111                 /*! the number of channels at the internal sample rate */
112                 unsigned int num_at_internal_rate;
113                 /*! the absolute highest sample rate supported by any channel in the bridge */
114                 unsigned int highest_supported_rate;
115                 /*! Is the sample rate locked by the bridge, if so what is that rate.*/
116                 unsigned int locked_rate;
117 };
118
119 struct softmix_mixing_array {
120         int max_num_entries;
121         int used_entries;
122         int16_t **buffers;
123 };
124
125 struct softmix_translate_helper_entry {
126         int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
127                                       and re-init if it was usable. */
128         struct ast_format dst_format; /*!< The destination format for this helper */
129         struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
130         struct ast_frame *out_frame; /*!< The output frame from the last translation */
131         AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
132 };
133
134 struct softmix_translate_helper {
135         struct ast_format slin_src; /*!< the source format expected for all the translators */
136         AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
137 };
138
139 static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
140 {
141         struct softmix_translate_helper_entry *entry;
142         if (!(entry = ast_calloc(1, sizeof(*entry)))) {
143                 return NULL;
144         }
145         ast_format_copy(&entry->dst_format, dst);
146         return entry;
147 }
148
149 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
150 {
151         if (entry->trans_pvt) {
152                 ast_translator_free_path(entry->trans_pvt);
153         }
154         if (entry->out_frame) {
155                 ast_frfree(entry->out_frame);
156         }
157         ast_free(entry);
158         return NULL;
159 }
160
161 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
162 {
163         memset(trans_helper, 0, sizeof(*trans_helper));
164         ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
165 }
166
167 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
168 {
169         struct softmix_translate_helper_entry *entry;
170
171         while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
172                 softmix_translate_helper_free_entry(entry);
173         }
174 }
175
176 static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
177 {
178         struct softmix_translate_helper_entry *entry;
179
180         ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
181         AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
182                 if (entry->trans_pvt) {
183                         ast_translator_free_path(entry->trans_pvt);
184                         if (!(entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src))) {
185                                 AST_LIST_REMOVE_CURRENT(entry);
186                                 entry = softmix_translate_helper_free_entry(entry);
187                         }
188                 }
189         }
190         AST_LIST_TRAVERSE_SAFE_END;
191 }
192
193 /*!
194  * \internal
195  * \brief Get the next available audio on the softmix channel's read stream
196  * and determine if it should be mixed out or not on the write stream. 
197  *
198  * \retval pointer to buffer containing the exact number of samples requested on success.
199  * \retval NULL if no samples are present
200  */
201 static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
202 {
203         if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
204                 ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
205                 sc->have_audio = 1;
206                 return sc->our_buf;
207         }
208         sc->have_audio = 0;
209         return NULL;
210 }
211
212 /*!
213  * \internal
214  * \brief Process a softmix channel's write audio
215  *
216  * \details This function will remove the channel's talking from its own audio if present and
217  * possibly even do the channel's write translation for it depending on how many other
218  * channels use the same write format.
219  */
220 static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
221         struct ast_format *raw_write_fmt,
222         struct softmix_channel *sc)
223 {
224         struct softmix_translate_helper_entry *entry = NULL;
225         int i;
226
227         /* If we provided audio that was not determined to be silence,
228          * then take it out while in slinear format. */
229         if (sc->have_audio && sc->talking) {
230                 for (i = 0; i < sc->write_frame.samples; i++) {
231                         ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
232                 }
233                 /* do not do any special write translate optimization if we had to make
234                  * a special mix for them to remove their own audio. */
235                 return;
236         }
237
238         AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
239                 if (ast_format_cmp(&entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
240                         entry->num_times_requested++;
241                 } else {
242                         continue;
243                 }
244                 if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
245                         entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src);
246                 }
247                 if (entry->trans_pvt && !entry->out_frame) {
248                         entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
249                 }
250                 if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
251                         ast_format_copy(&sc->write_frame.subclass.format, &entry->out_frame->subclass.format);
252                         memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
253                         sc->write_frame.datalen = entry->out_frame->datalen;
254                         sc->write_frame.samples = entry->out_frame->samples;
255                 }
256                 break;
257         }
258
259         /* add new entry into list if this format destination was not matched. */
260         if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
261                 AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
262         }
263 }
264
265 static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
266 {
267         struct softmix_translate_helper_entry *entry = NULL;
268         AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
269                 if (entry->out_frame) {
270                         ast_frfree(entry->out_frame);
271                         entry->out_frame = NULL;
272                 }
273                 entry->num_times_requested = 0;
274         }
275 }
276
277 static void softmix_bridge_data_destroy(void *obj)
278 {
279         struct softmix_bridge_data *softmix_data = obj;
280         ast_timer_close(softmix_data->timer);
281 }
282
283 /*! \brief Function called when a bridge is created */
284 static int softmix_bridge_create(struct ast_bridge *bridge)
285 {
286         struct softmix_bridge_data *softmix_data;
287
288         if (!(softmix_data = ao2_alloc(sizeof(*softmix_data), softmix_bridge_data_destroy))) {
289                 return -1;
290         }
291         if (!(softmix_data->timer = ast_timer_open())) {
292                 ao2_ref(softmix_data, -1);
293                 return -1;
294         }
295
296         /* start at 8khz, let it grow from there */
297         softmix_data->internal_rate = 8000;
298         softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
299
300         bridge->bridge_pvt = softmix_data;
301         return 0;
302 }
303
304 /*! \brief Function called when a bridge is destroyed */
305 static int softmix_bridge_destroy(struct ast_bridge *bridge)
306 {
307         struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
308         if (!bridge->bridge_pvt) {
309                 return -1;
310         }
311         ao2_ref(softmix_data, -1);
312         bridge->bridge_pvt = NULL;
313         return 0;
314 }
315
316 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
317 {
318         struct softmix_channel *sc = bridge_channel->bridge_pvt;
319         unsigned int channel_read_rate = ast_format_rate(&bridge_channel->chan->rawreadformat);
320
321         ast_mutex_lock(&sc->lock);
322         if (reset) {
323                 ast_slinfactory_destroy(&sc->factory);
324                 ast_dsp_free(sc->dsp);
325         }
326         /* Setup read/write frame parameters */
327         sc->write_frame.frametype = AST_FRAME_VOICE;
328         ast_format_set(&sc->write_frame.subclass.format, ast_format_slin_by_rate(rate), 0);
329         sc->write_frame.data.ptr = sc->final_buf;
330         sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
331         sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
332
333         sc->read_frame.frametype = AST_FRAME_VOICE;
334         ast_format_set(&sc->read_frame.subclass.format, ast_format_slin_by_rate(channel_read_rate), 0);
335         sc->read_frame.data.ptr = sc->our_buf;
336         sc->read_frame.datalen = SOFTMIX_DATALEN(channel_read_rate, interval);
337         sc->read_frame.samples = SOFTMIX_SAMPLES(channel_read_rate, interval);
338
339         /* Setup smoother */
340         ast_slinfactory_init_with_format(&sc->factory, &sc->write_frame.subclass.format);
341
342         /* set new read and write formats on channel. */
343         ast_set_read_format(bridge_channel->chan, &sc->read_frame.subclass.format);
344         ast_set_write_format(bridge_channel->chan, &sc->write_frame.subclass.format);
345
346         /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
347         sc->dsp = ast_dsp_new_with_rate(channel_read_rate);
348         /* we want to aggressively detect silence to avoid feedback */
349         if (bridge_channel->tech_args.talking_threshold) {
350                 ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
351         } else {
352                 ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
353         }
354
355         ast_mutex_unlock(&sc->lock);
356 }
357
358 /*! \brief Function called when a channel is joined into the bridge */
359 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
360 {
361         struct softmix_channel *sc = NULL;
362         struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
363
364         /* Create a new softmix_channel structure and allocate various things on it */
365         if (!(sc = ast_calloc(1, sizeof(*sc)))) {
366                 return -1;
367         }
368
369         /* Can't forget the lock */
370         ast_mutex_init(&sc->lock);
371
372         /* Can't forget to record our pvt structure within the bridged channel structure */
373         bridge_channel->bridge_pvt = sc;
374
375         set_softmix_bridge_data(softmix_data->internal_rate,
376                 softmix_data->internal_mixing_interval ? softmix_data->internal_mixing_interval : DEFAULT_SOFTMIX_INTERVAL,
377                 bridge_channel, 0);
378
379         return 0;
380 }
381
382 /*! \brief Function called when a channel leaves the bridge */
383 static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
384 {
385         struct softmix_channel *sc = bridge_channel->bridge_pvt;
386
387         if (!(bridge_channel->bridge_pvt)) {
388                 return 0;
389         }
390         bridge_channel->bridge_pvt = NULL;
391
392         /* Drop mutex lock */
393         ast_mutex_destroy(&sc->lock);
394
395         /* Drop the factory */
396         ast_slinfactory_destroy(&sc->factory);
397
398         /* Drop the DSP */
399         ast_dsp_free(sc->dsp);
400
401         /* Eep! drop ourselves */
402         ast_free(sc);
403
404         return 0;
405 }
406
407 /*!
408  * \internal
409  * \brief If the bridging core passes DTMF to us, then they want it to be distributed out to all memebers. Do that here.
410  */
411 static void softmix_pass_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
412 {
413         struct ast_bridge_channel *tmp;
414         AST_LIST_TRAVERSE(&bridge->channels, tmp, entry) {
415                 if (tmp == bridge_channel) {
416                         continue;
417                 }
418                 ast_write(tmp->chan, frame);
419         }
420 }
421
422 /*! \brief Function called when a channel writes a frame into the bridge */
423 static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
424 {
425         struct softmix_channel *sc = bridge_channel->bridge_pvt;
426         struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
427         int totalsilence = 0;
428         int silence_threshold = bridge_channel->tech_args.silence_threshold ?
429                 bridge_channel->tech_args.silence_threshold :
430                 DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
431         char update_talking = -1;  /* if this is set to 0 or 1, tell the bridge that the channel has started or stopped talking. */
432         int res = AST_BRIDGE_WRITE_SUCCESS;
433
434         /* Only accept audio frames, all others are unsupported */
435         if (frame->frametype == AST_FRAME_DTMF_END || frame->frametype == AST_FRAME_DTMF_BEGIN) {
436                 softmix_pass_dtmf(bridge, bridge_channel, frame);
437                 goto no_audio;
438         } else if (frame->frametype != AST_FRAME_VOICE) {
439                 res = AST_BRIDGE_WRITE_UNSUPPORTED;
440                 goto no_audio;
441         } else if (frame->datalen == 0) {
442                 goto no_audio;
443         }
444
445         /* If we made it here, we are going to write the frame into the conference */
446         ast_mutex_lock(&sc->lock);
447
448         ast_dsp_silence(sc->dsp, frame, &totalsilence);
449         if (totalsilence < silence_threshold) {
450                 if (!sc->talking) {
451                         update_talking = 1;
452                 }
453                 sc->talking = 1; /* tell the write process we have audio to be mixed out */
454         } else {
455                 if (sc->talking) {
456                         update_talking = 0;
457                 }
458                 sc->talking = 0;
459         }
460
461         /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
462          * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
463          * the audio by flushing the buffer before adding new audio in. */
464         if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
465                 ast_slinfactory_flush(&sc->factory);
466         }
467
468         /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
469          * is not determined to be talking. */
470         if (!(bridge_channel->tech_args.drop_silence && !sc->talking) &&
471                 (frame->frametype == AST_FRAME_VOICE && ast_format_is_slinear(&frame->subclass.format))) {
472                 ast_slinfactory_feed(&sc->factory, frame);
473         }
474
475         /* If a frame is ready to be written out, do so */
476         if (sc->have_frame) {
477                 ast_write(bridge_channel->chan, &sc->write_frame);
478                 sc->have_frame = 0;
479         }
480
481         /* Alllll done */
482         ast_mutex_unlock(&sc->lock);
483
484         if (update_talking != -1) {
485                 ast_bridge_notify_talking(bridge, bridge_channel, update_talking);
486         }
487
488         return res;
489
490 no_audio:
491         /* Even though the frame is not being written into the conference because it is not audio,
492          * we should use this opportunity to check to see if a frame is ready to be written out from
493          * the conference to the channel. */
494         ast_mutex_lock(&sc->lock);
495         if (sc->have_frame) {
496                 ast_write(bridge_channel->chan, &sc->write_frame);
497                 sc->have_frame = 0;
498         }
499         ast_mutex_unlock(&sc->lock);
500
501         return res;
502 }
503
504 /*! \brief Function called when the channel's thread is poked */
505 static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
506 {
507         struct softmix_channel *sc = bridge_channel->bridge_pvt;
508
509         ast_mutex_lock(&sc->lock);
510
511         if (sc->have_frame) {
512                 ast_write(bridge_channel->chan, &sc->write_frame);
513                 sc->have_frame = 0;
514         }
515
516         ast_mutex_unlock(&sc->lock);
517
518         return 0;
519 }
520
521 static void gather_softmix_stats(struct softmix_stats *stats,
522         const struct softmix_bridge_data *softmix_data,
523         struct ast_bridge_channel *bridge_channel)
524 {
525         int channel_native_rate;
526         int i;
527         /* Gather stats about channel sample rates. */
528         channel_native_rate = MAX(ast_format_rate(&bridge_channel->chan->rawwriteformat),
529                 ast_format_rate(&bridge_channel->chan->rawreadformat));
530
531         if (channel_native_rate > stats->highest_supported_rate) {
532                 stats->highest_supported_rate = channel_native_rate;
533         }
534         if (channel_native_rate > softmix_data->internal_rate) {
535                 for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
536                         if (stats->sample_rates[i] == channel_native_rate) {
537                                 stats->num_channels[i]++;
538                                 break;
539                         } else if (!stats->sample_rates[i]) {
540                                 stats->sample_rates[i] = channel_native_rate;
541                                 stats->num_channels[i]++;
542                                 break;
543                         }
544                 }
545                 stats->num_above_internal_rate++;
546         } else if (channel_native_rate == softmix_data->internal_rate) {
547                 stats->num_at_internal_rate++;
548         }
549 }
550 /*!
551  * \internal
552  * \brief Analyse mixing statistics and change bridges internal rate
553  * if necessary.
554  *
555  * \retval 0, no changes to internal rate 
556  * \ratval 1, internal rate was changed, update all the channels on the next mixing iteration.
557  */
558 static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
559 {
560         int i;
561         /* Re-adjust the internal bridge sample rate if
562          * 1. The bridge's internal sample rate is locked in at a sample
563          *    rate other than the current sample rate being used.
564          * 2. two or more channels support a higher sample rate
565          * 3. no channels support the current sample rate or a higher rate
566          */
567         if (stats->locked_rate) {
568                 /* if the rate is locked by the bridge, only update it if it differs
569                  * from the current rate we are using. */
570                 if (softmix_data->internal_rate != stats->locked_rate) {
571                         softmix_data->internal_rate = stats->locked_rate;
572                         ast_debug(1, " Bridge is locked in at sample rate %d\n", softmix_data->internal_rate);
573                         return 1;
574                 }
575         } else if (stats->num_above_internal_rate >= 2) {
576                 /* the highest rate is just used as a starting point */
577                 unsigned int best_rate = stats->highest_supported_rate;
578                 int best_index = -1;
579
580                 for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
581                         if (stats->num_channels[i]) {
582                                 break;
583                         }
584                         /* best_rate starts out being the first sample rate
585                          * greater than the internal sample rate that 2 or
586                          * more channels support. */
587                         if (stats->num_channels[i] >= 2 && (best_index == -1)) {
588                                 best_rate = stats->sample_rates[i];
589                                 best_index = i;
590                         /* If it has been detected that multiple rates above
591                          * the internal rate are present, compare those rates
592                          * to each other and pick the highest one two or more
593                          * channels support. */
594                         } else if (((best_index != -1) &&
595                                 (stats->num_channels[i] >= 2) &&
596                                 (stats->sample_rates[best_index] < stats->sample_rates[i]))) {
597                                 best_rate = stats->sample_rates[i];
598                                 best_index = i;
599                         /* It is possible that multiple channels exist with native sample
600                          * rates above the internal sample rate, but none of those channels
601                          * have the same rate in common.  In this case, the lowest sample
602                          * rate among those channels is picked. Over time as additional
603                          * statistic runs are made the internal sample rate number will
604                          * adjust to the most optimal sample rate, but it may take multiple
605                          * iterations. */
606                         } else if (best_index == -1) {
607                                 best_rate = MIN(best_rate, stats->sample_rates[i]);
608                         }
609                 }
610
611                 ast_debug(1, " Bridge changed from %d To %d\n", softmix_data->internal_rate, best_rate);
612                 softmix_data->internal_rate = best_rate;
613                 return 1;
614         } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
615                 /* In this case, the highest supported rate is actually lower than the internal rate */
616                 softmix_data->internal_rate = stats->highest_supported_rate;
617                 ast_debug(1, " Bridge changed from %d to %d\n", softmix_data->internal_rate, stats->highest_supported_rate);
618                 return 1;
619         }
620         return 0;
621 }
622
623 static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
624 {
625         memset(mixing_array, 0, sizeof(*mixing_array));
626         mixing_array->max_num_entries = starting_num_entries;
627         if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
628                 ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure. \n");
629                 return -1;
630         }
631         return 0;
632 }
633
634 static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
635 {
636         ast_free(mixing_array->buffers);
637 }
638
639 static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
640 {
641         int16_t **tmp;
642         /* give it some room to grow since memory is cheap but allocations can be expensive */
643         mixing_array->max_num_entries = num_entries;
644         if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
645                 ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure. \n");
646                 return -1;
647         }
648         mixing_array->buffers = tmp;
649         return 0;
650 }
651
652 /*! \brief Function which acts as the mixing thread */
653 static int softmix_bridge_thread(struct ast_bridge *bridge)
654 {
655         struct softmix_stats stats = { { 0 }, };
656         struct softmix_mixing_array mixing_array;
657         struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
658         struct ast_timer *timer;
659         struct softmix_translate_helper trans_helper;
660         int16_t buf[MAX_DATALEN] = { 0, };
661         unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
662         int timingfd;
663         int update_all_rates = 0; /* set this when the internal sample rate has changed */
664         int i, x;
665         int res = -1;
666
667         if (!(softmix_data = bridge->bridge_pvt)) {
668                 goto softmix_cleanup;
669         }
670
671         ao2_ref(softmix_data, 1);
672         timer = softmix_data->timer;
673         timingfd = ast_timer_fd(timer);
674         softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
675         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
676
677         /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
678         if (softmix_mixing_array_init(&mixing_array, bridge->num + 10)) {
679                 ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure. \n");
680                 goto softmix_cleanup;
681         }
682
683         while (!bridge->stop && !bridge->refresh && bridge->array_num) {
684                 struct ast_bridge_channel *bridge_channel = NULL;
685                 int timeout = -1;
686                 enum ast_format_id cur_slin_id = ast_format_slin_by_rate(softmix_data->internal_rate);
687                 unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
688                 unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
689
690                 if (softmix_datalen > MAX_DATALEN) {
691                         /* This should NEVER happen, but if it does we need to know about it. Almost
692                          * all the memcpys used during this process depend on this assumption.  Rather
693                          * than checking this over and over again through out the code, this single
694                          * verification is done on each iteration. */
695                         ast_log(LOG_WARNING, "Conference mixing error, requested mixing length greater than mixing buffer.\n");
696                         goto softmix_cleanup;
697                 }
698
699                 /* Grow the mixing array buffer as participants are added. */
700                 if (mixing_array.max_num_entries < bridge->num && softmix_mixing_array_grow(&mixing_array, bridge->num + 5)) {
701                         goto softmix_cleanup;
702                 }
703
704                 /* init the number of buffers stored in the mixing array to 0.
705                  * As buffers are added for mixing, this number is incremented. */
706                 mixing_array.used_entries = 0;
707
708                 /* These variables help determine if a rate change is required */
709                 if (!stat_iteration_counter) {
710                         memset(&stats, 0, sizeof(stats));
711                         stats.locked_rate = bridge->internal_sample_rate;
712                 }
713
714                 /* If the sample rate has changed, update the translator helper */
715                 if (update_all_rates) {
716                         softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
717                 }
718
719                 /* Go through pulling audio from each factory that has it available */
720                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
721                         struct softmix_channel *sc = bridge_channel->bridge_pvt;
722
723                         /* Update the sample rate to match the bridge's native sample rate if necessary. */
724                         if (update_all_rates) {
725                                 set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
726                         }
727
728                         /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
729                         if (!stat_iteration_counter) {
730                                 gather_softmix_stats(&stats, softmix_data, bridge_channel);
731                         }
732
733                         /* if the channel is suspended, don't check for audio, but still gather stats */
734                         if (bridge_channel->suspended) {
735                                 continue;
736                         }
737
738                         /* Try to get audio from the factory if available */
739                         ast_mutex_lock(&sc->lock);
740                         if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
741                                 mixing_array.used_entries++;
742                         }
743                         ast_mutex_unlock(&sc->lock);
744                 }
745
746                 /* mix it like crazy */
747                 memset(buf, 0, softmix_datalen);
748                 for (i = 0; i < mixing_array.used_entries; i++) {
749                         for (x = 0; x < softmix_samples; x++) {
750                                 ast_slinear_saturated_add(buf + x, mixing_array.buffers[i] + x);
751                         }
752                 }
753
754                 /* Next step go through removing the channel's own audio and creating a good frame... */
755                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
756                         struct softmix_channel *sc = bridge_channel->bridge_pvt;
757
758                         if (bridge_channel->suspended) {
759                                 continue;
760                         }
761
762                         ast_mutex_lock(&sc->lock);
763
764                         /* Make SLINEAR write frame from local buffer */
765                         if (sc->write_frame.subclass.format.id != cur_slin_id) {
766                                 ast_format_set(&sc->write_frame.subclass.format, cur_slin_id, 0);
767                         }
768                         sc->write_frame.datalen = softmix_datalen;
769                         sc->write_frame.samples = softmix_samples;
770                         memcpy(sc->final_buf, buf, softmix_datalen);
771
772                         /* process the softmix channel's new write audio */
773                         softmix_process_write_audio(&trans_helper, &bridge_channel->chan->rawwriteformat, sc);
774
775                         /* The frame is now ready for use... */
776                         sc->have_frame = 1;
777
778                         ast_mutex_unlock(&sc->lock);
779
780                         /* Poke bridged channel thread just in case */
781                         pthread_kill(bridge_channel->thread, SIGURG);
782                 }
783
784                 update_all_rates = 0;
785                 if (!stat_iteration_counter) {
786                         update_all_rates = analyse_softmix_stats(&stats, softmix_data);
787                         stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
788                 }
789                 stat_iteration_counter--;
790
791                 ao2_unlock(bridge);
792                 /* cleanup any translation frame data from the previous mixing iteration. */
793                 softmix_translate_helper_cleanup(&trans_helper);
794                 /* Wait for the timing source to tell us to wake up and get things done */
795                 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
796                 ast_timer_ack(timer, 1);
797                 ao2_lock(bridge);
798
799                 /* make sure to detect mixing interval changes if they occur. */
800                 if (bridge->internal_mixing_interval && (bridge->internal_mixing_interval != softmix_data->internal_mixing_interval)) {
801                         softmix_data->internal_mixing_interval = bridge->internal_mixing_interval;
802                         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
803                         update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
804                 }
805         }
806
807         res = 0;
808
809 softmix_cleanup:
810         softmix_translate_helper_destroy(&trans_helper);
811         softmix_mixing_array_destroy(&mixing_array);
812         if (softmix_data) {
813                 ao2_ref(softmix_data, -1);
814         }
815         return res;
816 }
817
818 static struct ast_bridge_technology softmix_bridge = {
819         .name = "softmix",
820         .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED | AST_BRIDGE_CAPABILITY_OPTIMIZE,
821         .preference = AST_BRIDGE_PREFERENCE_LOW,
822         .create = softmix_bridge_create,
823         .destroy = softmix_bridge_destroy,
824         .join = softmix_bridge_join,
825         .leave = softmix_bridge_leave,
826         .write = softmix_bridge_write,
827         .thread = softmix_bridge_thread,
828         .poke = softmix_bridge_poke,
829 };
830
831 static int unload_module(void)
832 {
833         ast_format_cap_destroy(softmix_bridge.format_capabilities);
834         return ast_bridge_technology_unregister(&softmix_bridge);
835 }
836
837 static int load_module(void)
838 {
839         struct ast_format tmp;
840         if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc())) {
841                 return AST_MODULE_LOAD_DECLINE;
842         }
843         ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
844         return ast_bridge_technology_register(&softmix_bridge);
845 }
846
847 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");