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