Change several bridge functions to return error status.
[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         /*!
104          * \brief TRUE if a channel is talking.
105          *
106          * \note This affects how the channel's audio is mixed back to
107          * it.
108          */
109         unsigned int talking:1;
110         /*! TRUE if the channel provided audio for this mixing interval */
111         unsigned int have_audio:1;
112         /*! Buffer containing final mixed audio from all sources */
113         short final_buf[MAX_DATALEN];
114         /*! Buffer containing only the audio from the channel */
115         short our_buf[MAX_DATALEN];
116         /*! Data pertaining to talker mode for video conferencing */
117         struct video_follow_talker_data video_talker;
118 };
119
120 struct softmix_bridge_data {
121         struct ast_timer *timer;
122         /*! Lock for signaling the mixing thread. */
123         ast_mutex_t lock;
124         /*! Condition, used if we need to wake up the mixing thread. */
125         ast_cond_t cond;
126         /*! Thread handling the mixing */
127         pthread_t thread;
128         unsigned int internal_rate;
129         unsigned int internal_mixing_interval;
130         /*! TRUE if the mixing thread should stop */
131         unsigned int stop:1;
132 };
133
134 struct softmix_stats {
135         /*! Each index represents a sample rate used above the internal rate. */
136         unsigned int sample_rates[16];
137         /*! Each index represents the number of channels using the same index in the sample_rates array.  */
138         unsigned int num_channels[16];
139         /*! the number of channels above the internal sample rate */
140         unsigned int num_above_internal_rate;
141         /*! the number of channels at the internal sample rate */
142         unsigned int num_at_internal_rate;
143         /*! the absolute highest sample rate supported by any channel in the bridge */
144         unsigned int highest_supported_rate;
145         /*! Is the sample rate locked by the bridge, if so what is that rate.*/
146         unsigned int locked_rate;
147 };
148
149 struct softmix_mixing_array {
150         unsigned int max_num_entries;
151         unsigned int used_entries;
152         int16_t **buffers;
153 };
154
155 struct softmix_translate_helper_entry {
156         int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
157                                       and re-init if it was usable. */
158         struct ast_format dst_format; /*!< The destination format for this helper */
159         struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
160         struct ast_frame *out_frame; /*!< The output frame from the last translation */
161         AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
162 };
163
164 struct softmix_translate_helper {
165         struct ast_format slin_src; /*!< the source format expected for all the translators */
166         AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
167 };
168
169 static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
170 {
171         struct softmix_translate_helper_entry *entry;
172         if (!(entry = ast_calloc(1, sizeof(*entry)))) {
173                 return NULL;
174         }
175         ast_format_copy(&entry->dst_format, dst);
176         return entry;
177 }
178
179 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
180 {
181         if (entry->trans_pvt) {
182                 ast_translator_free_path(entry->trans_pvt);
183         }
184         if (entry->out_frame) {
185                 ast_frfree(entry->out_frame);
186         }
187         ast_free(entry);
188         return NULL;
189 }
190
191 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
192 {
193         memset(trans_helper, 0, sizeof(*trans_helper));
194         ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
195 }
196
197 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
198 {
199         struct softmix_translate_helper_entry *entry;
200
201         while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
202                 softmix_translate_helper_free_entry(entry);
203         }
204 }
205
206 static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
207 {
208         struct softmix_translate_helper_entry *entry;
209
210         ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
211         AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
212                 if (entry->trans_pvt) {
213                         ast_translator_free_path(entry->trans_pvt);
214                         if (!(entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src))) {
215                                 AST_LIST_REMOVE_CURRENT(entry);
216                                 entry = softmix_translate_helper_free_entry(entry);
217                         }
218                 }
219         }
220         AST_LIST_TRAVERSE_SAFE_END;
221 }
222
223 /*!
224  * \internal
225  * \brief Get the next available audio on the softmix channel's read stream
226  * and determine if it should be mixed out or not on the write stream.
227  *
228  * \retval pointer to buffer containing the exact number of samples requested on success.
229  * \retval NULL if no samples are present
230  */
231 static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
232 {
233         if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
234                 ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
235                 sc->have_audio = 1;
236                 return sc->our_buf;
237         }
238         sc->have_audio = 0;
239         return NULL;
240 }
241
242 /*!
243  * \internal
244  * \brief Process a softmix channel's write audio
245  *
246  * \details This function will remove the channel's talking from its own audio if present and
247  * possibly even do the channel's write translation for it depending on how many other
248  * channels use the same write format.
249  */
250 static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
251         struct ast_format *raw_write_fmt,
252         struct softmix_channel *sc)
253 {
254         struct softmix_translate_helper_entry *entry = NULL;
255         int i;
256
257         /* If we provided audio that was not determined to be silence,
258          * then take it out while in slinear format. */
259         if (sc->have_audio && sc->talking) {
260                 for (i = 0; i < sc->write_frame.samples; i++) {
261                         ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
262                 }
263                 /* do not do any special write translate optimization if we had to make
264                  * a special mix for them to remove their own audio. */
265                 return;
266         }
267
268         AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
269                 if (ast_format_cmp(&entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
270                         entry->num_times_requested++;
271                 } else {
272                         continue;
273                 }
274                 if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
275                         entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src);
276                 }
277                 if (entry->trans_pvt && !entry->out_frame) {
278                         entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
279                 }
280                 if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
281                         ast_format_copy(&sc->write_frame.subclass.format, &entry->out_frame->subclass.format);
282                         memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
283                         sc->write_frame.datalen = entry->out_frame->datalen;
284                         sc->write_frame.samples = entry->out_frame->samples;
285                 }
286                 break;
287         }
288
289         /* add new entry into list if this format destination was not matched. */
290         if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
291                 AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
292         }
293 }
294
295 static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
296 {
297         struct softmix_translate_helper_entry *entry;
298
299         AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
300                 if (entry->out_frame) {
301                         ast_frfree(entry->out_frame);
302                         entry->out_frame = NULL;
303                 }
304                 entry->num_times_requested = 0;
305         }
306 }
307
308 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
309 {
310         struct softmix_channel *sc = bridge_channel->tech_pvt;
311         unsigned int channel_read_rate = ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan));
312
313         ast_mutex_lock(&sc->lock);
314         if (reset) {
315                 ast_slinfactory_destroy(&sc->factory);
316                 ast_dsp_free(sc->dsp);
317         }
318         /* Setup read/write frame parameters */
319         sc->write_frame.frametype = AST_FRAME_VOICE;
320         ast_format_set(&sc->write_frame.subclass.format, ast_format_slin_by_rate(rate), 0);
321         sc->write_frame.data.ptr = sc->final_buf;
322         sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
323         sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
324
325         sc->read_frame.frametype = AST_FRAME_VOICE;
326         ast_format_set(&sc->read_frame.subclass.format, ast_format_slin_by_rate(channel_read_rate), 0);
327         sc->read_frame.data.ptr = sc->our_buf;
328         sc->read_frame.datalen = SOFTMIX_DATALEN(channel_read_rate, interval);
329         sc->read_frame.samples = SOFTMIX_SAMPLES(channel_read_rate, interval);
330
331         /* Setup smoother */
332         ast_slinfactory_init_with_format(&sc->factory, &sc->write_frame.subclass.format);
333
334         /* set new read and write formats on channel. */
335         ast_set_read_format(bridge_channel->chan, &sc->read_frame.subclass.format);
336         ast_set_write_format(bridge_channel->chan, &sc->write_frame.subclass.format);
337
338         /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
339         sc->dsp = ast_dsp_new_with_rate(channel_read_rate);
340         /* we want to aggressively detect silence to avoid feedback */
341         if (bridge_channel->tech_args.talking_threshold) {
342                 ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
343         } else {
344                 ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
345         }
346
347         ast_mutex_unlock(&sc->lock);
348 }
349
350 /*!
351  * \internal
352  * \brief Poke the mixing thread in case it is waiting for an active channel.
353  * \since 12.0.0
354  *
355  * \param softmix_data Bridge mixing data.
356  *
357  * \return Nothing
358  */
359 static void softmix_poke_thread(struct softmix_bridge_data *softmix_data)
360 {
361         ast_mutex_lock(&softmix_data->lock);
362         ast_cond_signal(&softmix_data->cond);
363         ast_mutex_unlock(&softmix_data->lock);
364 }
365
366 /*! \brief Function called when a channel is unsuspended from the bridge */
367 static void softmix_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
368 {
369         if (bridge->tech_pvt) {
370                 softmix_poke_thread(bridge->tech_pvt);
371         }
372 }
373
374 /*!
375  * \internal
376  * \brief Indicate a source change to the channel.
377  * \since 12.0.0
378  *
379  * \param bridge_channel Which channel source is changing.
380  *
381  * \retval 0 on success.
382  * \retval -1 on error.
383  */
384 static int softmix_src_change(struct ast_bridge_channel *bridge_channel)
385 {
386         return ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_SRCCHANGE, NULL, 0);
387 }
388
389 /*! \brief Function called when a channel is joined into the bridge */
390 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
391 {
392         struct softmix_channel *sc;
393         struct softmix_bridge_data *softmix_data;
394
395         softmix_data = bridge->tech_pvt;
396         if (!softmix_data) {
397                 return -1;
398         }
399
400         /* Create a new softmix_channel structure and allocate various things on it */
401         if (!(sc = ast_calloc(1, sizeof(*sc)))) {
402                 return -1;
403         }
404
405         softmix_src_change(bridge_channel);
406
407         /* Can't forget the lock */
408         ast_mutex_init(&sc->lock);
409
410         /* Can't forget to record our pvt structure within the bridged channel structure */
411         bridge_channel->tech_pvt = sc;
412
413         set_softmix_bridge_data(softmix_data->internal_rate,
414                 softmix_data->internal_mixing_interval
415                         ? softmix_data->internal_mixing_interval
416                         : DEFAULT_SOFTMIX_INTERVAL,
417                 bridge_channel, 0);
418
419         softmix_poke_thread(softmix_data);
420         return 0;
421 }
422
423 /*! \brief Function called when a channel leaves the bridge */
424 static void softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
425 {
426         struct softmix_channel *sc = bridge_channel->tech_pvt;
427
428         if (!sc) {
429                 return;
430         }
431         bridge_channel->tech_pvt = NULL;
432
433         softmix_src_change(bridge_channel);
434
435         /* Drop mutex lock */
436         ast_mutex_destroy(&sc->lock);
437
438         /* Drop the factory */
439         ast_slinfactory_destroy(&sc->factory);
440
441         /* Drop the DSP */
442         ast_dsp_free(sc->dsp);
443
444         /* Eep! drop ourselves */
445         ast_free(sc);
446 }
447
448 /*!
449  * \internal
450  * \brief Pass the given frame to everyone else.
451  * \since 12.0.0
452  *
453  * \param bridge What bridge to distribute frame.
454  * \param bridge_channel Channel to optionally not pass frame to. (NULL to pass to everyone)
455  * \param frame Frame to pass.
456  *
457  * \return Nothing
458  */
459 static void softmix_pass_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
460 {
461         struct ast_bridge_channel *cur;
462
463         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
464                 if (cur == bridge_channel) {
465                         continue;
466                 }
467                 ast_bridge_channel_queue_frame(cur, frame);
468         }
469 }
470
471 static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
472 {
473         struct ast_bridge_channel *cur;
474
475         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
476                 if (cur->suspended) {
477                         continue;
478                 }
479                 if (ast_bridge_is_video_src(bridge, cur->chan) == 1) {
480                         ast_bridge_channel_queue_frame(cur, frame);
481                         break;
482                 }
483         }
484 }
485
486 /*!
487  * \internal
488  * \brief Determine what to do with a video frame.
489  * \since 12.0.0
490  *
491  * \param bridge Which bridge is getting the frame
492  * \param bridge_channel Which channel is writing the frame.
493  * \param frame What is being written.
494  *
495  * \return Nothing
496  */
497 static void softmix_bridge_write_video(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
498 {
499         struct softmix_channel *sc;
500         int video_src_priority;
501
502         /* Determine if the video frame should be distributed or not */
503         switch (bridge->video_mode.mode) {
504         case AST_BRIDGE_VIDEO_MODE_NONE:
505                 break;
506         case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
507                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
508                 if (video_src_priority == 1) {
509                         /* Pass to me and everyone else. */
510                         softmix_pass_everyone_else(bridge, NULL, frame);
511                 }
512                 break;
513         case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
514                 sc = bridge_channel->tech_pvt;
515                 ast_mutex_lock(&sc->lock);
516                 ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan,
517                         sc->video_talker.energy_average,
518                         ast_format_get_video_mark(&frame->subclass.format));
519                 ast_mutex_unlock(&sc->lock);
520                 video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
521                 if (video_src_priority == 1) {
522                         int num_src = ast_bridge_number_video_src(bridge);
523                         int echo = num_src > 1 ? 0 : 1;
524
525                         softmix_pass_everyone_else(bridge, echo ? NULL : bridge_channel, frame);
526                 } else if (video_src_priority == 2) {
527                         softmix_pass_video_top_priority(bridge, frame);
528                 }
529                 break;
530         }
531 }
532
533 /*!
534  * \internal
535  * \brief Determine what to do with a voice frame.
536  * \since 12.0.0
537  *
538  * \param bridge Which bridge is getting the frame
539  * \param bridge_channel Which channel is writing the frame.
540  * \param frame What is being written.
541  *
542  * \return Nothing
543  */
544 static void softmix_bridge_write_voice(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
545 {
546         struct softmix_channel *sc = bridge_channel->tech_pvt;
547         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
548         int totalsilence = 0;
549         int cur_energy = 0;
550         int silence_threshold = bridge_channel->tech_args.silence_threshold ?
551                 bridge_channel->tech_args.silence_threshold :
552                 DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
553         char update_talking = -1;  /* if this is set to 0 or 1, tell the bridge that the channel has started or stopped talking. */
554
555         /* Write the frame into the conference */
556         ast_mutex_lock(&sc->lock);
557         ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
558
559         if (bridge->video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
560                 int cur_slot = sc->video_talker.energy_history_cur_slot;
561
562                 sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
563                 sc->video_talker.energy_accum += cur_energy;
564                 sc->video_talker.energy_history[cur_slot] = cur_energy;
565                 sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
566                 sc->video_talker.energy_history_cur_slot++;
567                 if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
568                         sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
569                 }
570         }
571
572         if (totalsilence < silence_threshold) {
573                 if (!sc->talking) {
574                         update_talking = 1;
575                 }
576                 sc->talking = 1; /* tell the write process we have audio to be mixed out */
577         } else {
578                 if (sc->talking) {
579                         update_talking = 0;
580                 }
581                 sc->talking = 0;
582         }
583
584         /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
585          * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
586          * the audio by flushing the buffer before adding new audio in. */
587         if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
588                 ast_slinfactory_flush(&sc->factory);
589         }
590
591         /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
592          * is not determined to be talking. */
593         if (!(bridge_channel->tech_args.drop_silence && !sc->talking) &&
594                 (frame->frametype == AST_FRAME_VOICE && ast_format_is_slinear(&frame->subclass.format))) {
595                 ast_slinfactory_feed(&sc->factory, frame);
596         }
597
598         /* Alllll done */
599         ast_mutex_unlock(&sc->lock);
600
601         if (update_talking != -1) {
602                 ast_bridge_notify_talking(bridge_channel, update_talking);
603         }
604 }
605
606 /*!
607  * \internal
608  * \brief Determine what to do with a control frame.
609  * \since 12.0.0
610  *
611  * \param bridge Which bridge is getting the frame
612  * \param bridge_channel Which channel is writing the frame.
613  * \param frame What is being written.
614  *
615  * \return Nothing
616  */
617 static void softmix_bridge_write_control(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
618 {
619 /* BUGBUG need to look at channel roles to determine what to do with control frame. */
620         /*! \todo BUGBUG softmix_bridge_write_control() not written */
621 }
622
623 /*!
624  * \internal
625  * \brief Determine what to do with a frame written into the bridge.
626  * \since 12.0.0
627  *
628  * \param bridge Which bridge is getting the frame
629  * \param bridge_channel Which channel is writing the frame.
630  * \param frame What is being written.
631  *
632  * \retval 0 on success
633  * \retval -1 on failure
634  *
635  * \note On entry, bridge is already locked.
636  */
637 static int softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
638 {
639         int res = 0;
640
641         if (!bridge->tech_pvt || !bridge_channel->tech_pvt) {
642                 return -1;
643         }
644
645         switch (frame->frametype) {
646         case AST_FRAME_DTMF_BEGIN:
647         case AST_FRAME_DTMF_END:
648                 softmix_pass_everyone_else(bridge, bridge_channel, frame);
649                 break;
650         case AST_FRAME_VOICE:
651                 softmix_bridge_write_voice(bridge, bridge_channel, frame);
652                 break;
653         case AST_FRAME_VIDEO:
654                 softmix_bridge_write_video(bridge, bridge_channel, frame);
655                 break;
656         case AST_FRAME_CONTROL:
657                 softmix_bridge_write_control(bridge, bridge_channel, frame);
658                 break;
659         case AST_FRAME_BRIDGE_ACTION:
660                 softmix_pass_everyone_else(bridge, bridge_channel, frame);
661                 break;
662         default:
663                 ast_debug(3, "Frame type %d unsupported\n", frame->frametype);
664                 res = -1;
665                 break;
666         }
667
668         return res;
669 }
670
671 static void gather_softmix_stats(struct softmix_stats *stats,
672         const struct softmix_bridge_data *softmix_data,
673         struct ast_bridge_channel *bridge_channel)
674 {
675         int channel_native_rate;
676         int i;
677         /* Gather stats about channel sample rates. */
678         channel_native_rate = MAX(ast_format_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
679                 ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan)));
680
681         if (channel_native_rate > stats->highest_supported_rate) {
682                 stats->highest_supported_rate = channel_native_rate;
683         }
684         if (channel_native_rate > softmix_data->internal_rate) {
685                 for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
686                         if (stats->sample_rates[i] == channel_native_rate) {
687                                 stats->num_channels[i]++;
688                                 break;
689                         } else if (!stats->sample_rates[i]) {
690                                 stats->sample_rates[i] = channel_native_rate;
691                                 stats->num_channels[i]++;
692                                 break;
693                         }
694                 }
695                 stats->num_above_internal_rate++;
696         } else if (channel_native_rate == softmix_data->internal_rate) {
697                 stats->num_at_internal_rate++;
698         }
699 }
700 /*!
701  * \internal
702  * \brief Analyse mixing statistics and change bridges internal rate
703  * if necessary.
704  *
705  * \retval 0, no changes to internal rate
706  * \ratval 1, internal rate was changed, update all the channels on the next mixing iteration.
707  */
708 static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
709 {
710         int i;
711         /* Re-adjust the internal bridge sample rate if
712          * 1. The bridge's internal sample rate is locked in at a sample
713          *    rate other than the current sample rate being used.
714          * 2. two or more channels support a higher sample rate
715          * 3. no channels support the current sample rate or a higher rate
716          */
717         if (stats->locked_rate) {
718                 /* if the rate is locked by the bridge, only update it if it differs
719                  * from the current rate we are using. */
720                 if (softmix_data->internal_rate != stats->locked_rate) {
721                         softmix_data->internal_rate = stats->locked_rate;
722                         ast_debug(1, "Bridge is locked in at sample rate %d\n",
723                                 softmix_data->internal_rate);
724                         return 1;
725                 }
726         } else if (stats->num_above_internal_rate >= 2) {
727                 /* the highest rate is just used as a starting point */
728                 unsigned int best_rate = stats->highest_supported_rate;
729                 int best_index = -1;
730
731                 for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
732                         if (stats->num_channels[i]) {
733                                 break;
734                         }
735                         /* best_rate starts out being the first sample rate
736                          * greater than the internal sample rate that 2 or
737                          * more channels support. */
738                         if (stats->num_channels[i] >= 2 && (best_index == -1)) {
739                                 best_rate = stats->sample_rates[i];
740                                 best_index = i;
741                         /* If it has been detected that multiple rates above
742                          * the internal rate are present, compare those rates
743                          * to each other and pick the highest one two or more
744                          * channels support. */
745                         } else if (((best_index != -1) &&
746                                 (stats->num_channels[i] >= 2) &&
747                                 (stats->sample_rates[best_index] < stats->sample_rates[i]))) {
748                                 best_rate = stats->sample_rates[i];
749                                 best_index = i;
750                         /* It is possible that multiple channels exist with native sample
751                          * rates above the internal sample rate, but none of those channels
752                          * have the same rate in common.  In this case, the lowest sample
753                          * rate among those channels is picked. Over time as additional
754                          * statistic runs are made the internal sample rate number will
755                          * adjust to the most optimal sample rate, but it may take multiple
756                          * iterations. */
757                         } else if (best_index == -1) {
758                                 best_rate = MIN(best_rate, stats->sample_rates[i]);
759                         }
760                 }
761
762                 ast_debug(1, "Bridge changed from %d To %d\n",
763                         softmix_data->internal_rate, best_rate);
764                 softmix_data->internal_rate = best_rate;
765                 return 1;
766         } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
767                 /* In this case, the highest supported rate is actually lower than the internal rate */
768                 softmix_data->internal_rate = stats->highest_supported_rate;
769                 ast_debug(1, "Bridge changed from %d to %d\n",
770                         softmix_data->internal_rate, stats->highest_supported_rate);
771                 return 1;
772         }
773         return 0;
774 }
775
776 static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
777 {
778         memset(mixing_array, 0, sizeof(*mixing_array));
779         mixing_array->max_num_entries = starting_num_entries;
780         if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
781                 ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure.\n");
782                 return -1;
783         }
784         return 0;
785 }
786
787 static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
788 {
789         ast_free(mixing_array->buffers);
790 }
791
792 static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
793 {
794         int16_t **tmp;
795         /* give it some room to grow since memory is cheap but allocations can be expensive */
796         mixing_array->max_num_entries = num_entries;
797         if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
798                 ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure.\n");
799                 return -1;
800         }
801         mixing_array->buffers = tmp;
802         return 0;
803 }
804
805 /*!
806  * \brief Mixing loop.
807  *
808  * \retval 0 on success
809  * \retval -1 on failure
810  */
811 static int softmix_mixing_loop(struct ast_bridge *bridge)
812 {
813         struct softmix_stats stats = { { 0 }, };
814         struct softmix_mixing_array mixing_array;
815         struct softmix_bridge_data *softmix_data = bridge->tech_pvt;
816         struct ast_timer *timer;
817         struct softmix_translate_helper trans_helper;
818         int16_t buf[MAX_DATALEN];
819         unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
820         int timingfd;
821         int update_all_rates = 0; /* set this when the internal sample rate has changed */
822         unsigned int idx;
823         unsigned int x;
824         int res = -1;
825
826         timer = softmix_data->timer;
827         timingfd = ast_timer_fd(timer);
828         softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
829         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
830
831         /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
832         if (softmix_mixing_array_init(&mixing_array, bridge->num_channels + 10)) {
833                 goto softmix_cleanup;
834         }
835
836         while (!softmix_data->stop && bridge->num_active) {
837                 struct ast_bridge_channel *bridge_channel;
838                 int timeout = -1;
839                 enum ast_format_id cur_slin_id = ast_format_slin_by_rate(softmix_data->internal_rate);
840                 unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
841                 unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
842
843                 if (softmix_datalen > MAX_DATALEN) {
844                         /* This should NEVER happen, but if it does we need to know about it. Almost
845                          * all the memcpys used during this process depend on this assumption.  Rather
846                          * than checking this over and over again through out the code, this single
847                          * verification is done on each iteration. */
848                         ast_log(LOG_WARNING,
849                                 "Bridge %s: Conference mixing error, requested mixing length greater than mixing buffer.\n",
850                                 bridge->uniqueid);
851                         goto softmix_cleanup;
852                 }
853
854                 /* Grow the mixing array buffer as participants are added. */
855                 if (mixing_array.max_num_entries < bridge->num_channels
856                         && softmix_mixing_array_grow(&mixing_array, bridge->num_channels + 5)) {
857                         goto softmix_cleanup;
858                 }
859
860                 /* init the number of buffers stored in the mixing array to 0.
861                  * As buffers are added for mixing, this number is incremented. */
862                 mixing_array.used_entries = 0;
863
864                 /* These variables help determine if a rate change is required */
865                 if (!stat_iteration_counter) {
866                         memset(&stats, 0, sizeof(stats));
867                         stats.locked_rate = bridge->internal_sample_rate;
868                 }
869
870                 /* If the sample rate has changed, update the translator helper */
871                 if (update_all_rates) {
872                         softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
873                 }
874
875                 /* Go through pulling audio from each factory that has it available */
876                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
877                         struct softmix_channel *sc = bridge_channel->tech_pvt;
878
879                         /* Update the sample rate to match the bridge's native sample rate if necessary. */
880                         if (update_all_rates) {
881                                 set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
882                         }
883
884                         /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
885                         if (!stat_iteration_counter) {
886                                 gather_softmix_stats(&stats, softmix_data, bridge_channel);
887                         }
888
889                         /* if the channel is suspended, don't check for audio, but still gather stats */
890                         if (bridge_channel->suspended) {
891                                 continue;
892                         }
893
894                         /* Try to get audio from the factory if available */
895                         ast_mutex_lock(&sc->lock);
896                         if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
897                                 mixing_array.used_entries++;
898                         }
899                         ast_mutex_unlock(&sc->lock);
900                 }
901
902                 /* mix it like crazy */
903                 memset(buf, 0, softmix_datalen);
904                 for (idx = 0; idx < mixing_array.used_entries; ++idx) {
905                         for (x = 0; x < softmix_samples; ++x) {
906                                 ast_slinear_saturated_add(buf + x, mixing_array.buffers[idx] + x);
907                         }
908                 }
909
910                 /* Next step go through removing the channel's own audio and creating a good frame... */
911                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
912                         struct softmix_channel *sc = bridge_channel->tech_pvt;
913
914                         if (bridge_channel->suspended) {
915                                 continue;
916                         }
917
918                         ast_mutex_lock(&sc->lock);
919
920                         /* Make SLINEAR write frame from local buffer */
921                         if (sc->write_frame.subclass.format.id != cur_slin_id) {
922                                 ast_format_set(&sc->write_frame.subclass.format, cur_slin_id, 0);
923                         }
924                         sc->write_frame.datalen = softmix_datalen;
925                         sc->write_frame.samples = softmix_samples;
926                         memcpy(sc->final_buf, buf, softmix_datalen);
927
928                         /* process the softmix channel's new write audio */
929                         softmix_process_write_audio(&trans_helper, ast_channel_rawwriteformat(bridge_channel->chan), sc);
930
931                         ast_mutex_unlock(&sc->lock);
932
933                         /* A frame is now ready for the channel. */
934                         ast_bridge_channel_queue_frame(bridge_channel, &sc->write_frame);
935                 }
936
937                 update_all_rates = 0;
938                 if (!stat_iteration_counter) {
939                         update_all_rates = analyse_softmix_stats(&stats, softmix_data);
940                         stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
941                 }
942                 stat_iteration_counter--;
943
944                 ast_bridge_unlock(bridge);
945                 /* cleanup any translation frame data from the previous mixing iteration. */
946                 softmix_translate_helper_cleanup(&trans_helper);
947                 /* Wait for the timing source to tell us to wake up and get things done */
948                 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
949                 if (ast_timer_ack(timer, 1) < 0) {
950                         ast_log(LOG_ERROR, "Bridge %s: Failed to acknowledge timer in softmix.\n",
951                                 bridge->uniqueid);
952                         ast_bridge_lock(bridge);
953                         goto softmix_cleanup;
954                 }
955                 ast_bridge_lock(bridge);
956
957                 /* make sure to detect mixing interval changes if they occur. */
958                 if (bridge->internal_mixing_interval && (bridge->internal_mixing_interval != softmix_data->internal_mixing_interval)) {
959                         softmix_data->internal_mixing_interval = bridge->internal_mixing_interval;
960                         ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
961                         update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
962                 }
963         }
964
965         res = 0;
966
967 softmix_cleanup:
968         softmix_translate_helper_destroy(&trans_helper);
969         softmix_mixing_array_destroy(&mixing_array);
970         return res;
971 }
972
973 /*!
974  * \internal
975  * \brief Mixing thread.
976  * \since 12.0.0
977  *
978  * \note The thread does not have its own reference to the
979  * bridge.  The lifetime of the thread is tied to the lifetime
980  * of the mixing technology association with the bridge.
981  */
982 static void *softmix_mixing_thread(void *data)
983 {
984         struct ast_bridge *bridge = data;
985         struct softmix_bridge_data *softmix_data;
986
987         ast_bridge_lock(bridge);
988         if (bridge->callid) {
989                 ast_callid_threadassoc_add(bridge->callid);
990         }
991
992         ast_debug(1, "Bridge %s: starting mixing thread\n", bridge->uniqueid);
993
994         softmix_data = bridge->tech_pvt;
995         while (!softmix_data->stop) {
996                 if (!bridge->num_active) {
997                         /* Wait for something to happen to the bridge. */
998                         ast_bridge_unlock(bridge);
999                         ast_mutex_lock(&softmix_data->lock);
1000                         if (!softmix_data->stop) {
1001                                 ast_cond_wait(&softmix_data->cond, &softmix_data->lock);
1002                         }
1003                         ast_mutex_unlock(&softmix_data->lock);
1004                         ast_bridge_lock(bridge);
1005                         continue;
1006                 }
1007
1008                 if (softmix_mixing_loop(bridge)) {
1009                         /*
1010                          * A mixing error occurred.  Sleep and try again later so we
1011                          * won't flood the logs.
1012                          */
1013                         ast_bridge_unlock(bridge);
1014                         sleep(1);
1015                         ast_bridge_lock(bridge);
1016                 }
1017         }
1018
1019         ast_bridge_unlock(bridge);
1020
1021         ast_debug(1, "Bridge %s: stopping mixing thread\n", bridge->uniqueid);
1022
1023         return NULL;
1024 }
1025
1026 static void softmix_bridge_data_destroy(struct softmix_bridge_data *softmix_data)
1027 {
1028         if (softmix_data->timer) {
1029                 ast_timer_close(softmix_data->timer);
1030                 softmix_data->timer = NULL;
1031         }
1032         ast_mutex_destroy(&softmix_data->lock);
1033         ast_free(softmix_data);
1034 }
1035
1036 /*! \brief Function called when a bridge is created */
1037 static int softmix_bridge_create(struct ast_bridge *bridge)
1038 {
1039         struct softmix_bridge_data *softmix_data;
1040
1041         softmix_data = ast_calloc(1, sizeof(*softmix_data));
1042         if (!softmix_data) {
1043                 return -1;
1044         }
1045         ast_mutex_init(&softmix_data->lock);
1046         softmix_data->timer = ast_timer_open();
1047         if (!softmix_data->timer) {
1048                 softmix_bridge_data_destroy(softmix_data);
1049                 return -1;
1050         }
1051         /* start at 8khz, let it grow from there */
1052         softmix_data->internal_rate = 8000;
1053         softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
1054
1055         bridge->tech_pvt = softmix_data;
1056
1057         /* Start the mixing thread. */
1058         if (ast_pthread_create(&softmix_data->thread, NULL, softmix_mixing_thread, bridge)) {
1059                 softmix_data->thread = AST_PTHREADT_NULL;
1060                 softmix_bridge_data_destroy(softmix_data);
1061                 bridge->tech_pvt = NULL;
1062                 return -1;
1063         }
1064
1065         return 0;
1066 }
1067
1068 /*!
1069  * \internal
1070  * \brief Request the softmix mixing thread stop.
1071  * \since 12.0.0
1072  *
1073  * \param bridge Which bridge is being stopped.
1074  *
1075  * \return Nothing
1076  */
1077 static void softmix_bridge_stop(struct ast_bridge *bridge)
1078 {
1079         struct softmix_bridge_data *softmix_data;
1080
1081         softmix_data = bridge->tech_pvt;
1082         if (!softmix_data) {
1083                 return;
1084         }
1085
1086         ast_mutex_lock(&softmix_data->lock);
1087         softmix_data->stop = 1;
1088         ast_mutex_unlock(&softmix_data->lock);
1089 }
1090
1091 /*! \brief Function called when a bridge is destroyed */
1092 static void softmix_bridge_destroy(struct ast_bridge *bridge)
1093 {
1094         struct softmix_bridge_data *softmix_data;
1095         pthread_t thread;
1096
1097         softmix_data = bridge->tech_pvt;
1098         if (!softmix_data) {
1099                 return;
1100         }
1101
1102         /* Stop the mixing thread. */
1103         ast_mutex_lock(&softmix_data->lock);
1104         softmix_data->stop = 1;
1105         ast_cond_signal(&softmix_data->cond);
1106         thread = softmix_data->thread;
1107         softmix_data->thread = AST_PTHREADT_NULL;
1108         ast_mutex_unlock(&softmix_data->lock);
1109         if (thread != AST_PTHREADT_NULL) {
1110                 ast_debug(1, "Bridge %s: Waiting for mixing thread to die.\n", bridge->uniqueid);
1111                 pthread_join(thread, NULL);
1112         }
1113
1114         softmix_bridge_data_destroy(softmix_data);
1115         bridge->tech_pvt = NULL;
1116 }
1117
1118 static struct ast_bridge_technology softmix_bridge = {
1119         .name = "softmix",
1120         .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
1121         .preference = AST_BRIDGE_PREFERENCE_BASE_MULTIMIX,
1122         .create = softmix_bridge_create,
1123         .stop = softmix_bridge_stop,
1124         .destroy = softmix_bridge_destroy,
1125         .join = softmix_bridge_join,
1126         .leave = softmix_bridge_leave,
1127         .unsuspend = softmix_bridge_unsuspend,
1128         .write = softmix_bridge_write,
1129 };
1130
1131 static int unload_module(void)
1132 {
1133         ast_format_cap_destroy(softmix_bridge.format_capabilities);
1134         return ast_bridge_technology_unregister(&softmix_bridge);
1135 }
1136
1137 static int load_module(void)
1138 {
1139         struct ast_format tmp;
1140         if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc())) {
1141                 return AST_MODULE_LOAD_DECLINE;
1142         }
1143         ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
1144         return ast_bridge_technology_register(&softmix_bridge);
1145 }
1146
1147 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");