Sat Apr 26 2014 22:01:29

Asterisk developer's documentation


bridge_softmix.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2011, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  * David Vossel <dvossel@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Multi-party software based channel mixing
00023  *
00024  * \author Joshua Colp <jcolp@digium.com>
00025  * \author David Vossel <dvossel@digium.com>
00026  *
00027  * \ingroup bridges
00028  */
00029 
00030 /*** MODULEINFO
00031    <support_level>core</support_level>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 399353 $")
00037 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041 #include <sys/time.h>
00042 #include <signal.h>
00043 #include <errno.h>
00044 #include <unistd.h>
00045 
00046 #include "asterisk/module.h"
00047 #include "asterisk/channel.h"
00048 #include "asterisk/bridging.h"
00049 #include "asterisk/bridging_technology.h"
00050 #include "asterisk/frame.h"
00051 #include "asterisk/options.h"
00052 #include "asterisk/logger.h"
00053 #include "asterisk/slinfactory.h"
00054 #include "asterisk/astobj2.h"
00055 #include "asterisk/timing.h"
00056 #include "asterisk/translate.h"
00057 
00058 #define MAX_DATALEN 8096
00059 
00060 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
00061 #define DEFAULT_SOFTMIX_INTERVAL 20
00062 
00063 /*! \brief Size of the buffer used for sample manipulation */
00064 #define SOFTMIX_DATALEN(rate, interval) ((rate/50) * (interval / 10))
00065 
00066 /*! \brief Number of samples we are dealing with */
00067 #define SOFTMIX_SAMPLES(rate, interval) (SOFTMIX_DATALEN(rate, interval) / 2)
00068 
00069 /*! \brief Number of mixing iterations to perform between gathering statistics. */
00070 #define SOFTMIX_STAT_INTERVAL 100
00071 
00072 /* This is the threshold in ms at which a channel's own audio will stop getting
00073  * mixed out its own write audio stream because it is not talking. */
00074 #define DEFAULT_SOFTMIX_SILENCE_THRESHOLD 2500
00075 #define DEFAULT_SOFTMIX_TALKING_THRESHOLD 160
00076 
00077 #define DEFAULT_ENERGY_HISTORY_LEN 150
00078 
00079 struct video_follow_talker_data {
00080    /*! audio energy history */
00081    int energy_history[DEFAULT_ENERGY_HISTORY_LEN];
00082    /*! The current slot being used in the history buffer, this
00083     *  increments and wraps around */
00084    int energy_history_cur_slot;
00085    /*! The current energy sum used for averages. */
00086    int energy_accum;
00087    /*! The current energy average */
00088    int energy_average;
00089 };
00090 
00091 /*! \brief Structure which contains per-channel mixing information */
00092 struct softmix_channel {
00093    /*! Lock to protect this structure */
00094    ast_mutex_t lock;
00095    /*! Factory which contains audio read in from the channel */
00096    struct ast_slinfactory factory;
00097    /*! Frame that contains mixed audio to be written out to the channel */
00098    struct ast_frame write_frame;
00099    /*! Frame that contains mixed audio read from the channel */
00100    struct ast_frame read_frame;
00101    /*! DSP for detecting silence */
00102    struct ast_dsp *dsp;
00103    /*! Bit used to indicate if a channel is talking or not. This affects how
00104     * the channel's audio is mixed back to it. */
00105    int talking:1;
00106    /*! Bit used to indicate that the channel provided audio for this mixing interval */
00107    int have_audio:1;
00108    /*! Bit used to indicate that a frame is available to be written out to the channel */
00109    int have_frame:1;
00110    /*! Buffer containing final mixed audio from all sources */
00111    short final_buf[MAX_DATALEN];
00112    /*! Buffer containing only the audio from the channel */
00113    short our_buf[MAX_DATALEN];
00114    /*! Data pertaining to talker mode for video conferencing */
00115    struct video_follow_talker_data video_talker;
00116 };
00117 
00118 struct softmix_bridge_data {
00119    struct ast_timer *timer;
00120    unsigned int internal_rate;
00121    unsigned int internal_mixing_interval;
00122 };
00123 
00124 struct softmix_stats {
00125       /*! Each index represents a sample rate used above the internal rate. */
00126       unsigned int sample_rates[16];
00127       /*! Each index represents the number of channels using the same index in the sample_rates array.  */
00128       unsigned int num_channels[16];
00129       /*! the number of channels above the internal sample rate */
00130       unsigned int num_above_internal_rate;
00131       /*! the number of channels at the internal sample rate */
00132       unsigned int num_at_internal_rate;
00133       /*! the absolute highest sample rate supported by any channel in the bridge */
00134       unsigned int highest_supported_rate;
00135       /*! Is the sample rate locked by the bridge, if so what is that rate.*/
00136       unsigned int locked_rate;
00137 };
00138 
00139 struct softmix_mixing_array {
00140    int max_num_entries;
00141    int used_entries;
00142    int16_t **buffers;
00143 };
00144 
00145 struct softmix_translate_helper_entry {
00146    int num_times_requested; /*!< Once this entry is no longer requested, free the trans_pvt
00147                                  and re-init if it was usable. */
00148    struct ast_format dst_format; /*!< The destination format for this helper */
00149    struct ast_trans_pvt *trans_pvt; /*!< the translator for this slot. */
00150    struct ast_frame *out_frame; /*!< The output frame from the last translation */
00151    AST_LIST_ENTRY(softmix_translate_helper_entry) entry;
00152 };
00153 
00154 struct softmix_translate_helper {
00155    struct ast_format slin_src; /*!< the source format expected for all the translators */
00156    AST_LIST_HEAD_NOLOCK(, softmix_translate_helper_entry) entries;
00157 };
00158 
00159 static struct softmix_translate_helper_entry *softmix_translate_helper_entry_alloc(struct ast_format *dst)
00160 {
00161    struct softmix_translate_helper_entry *entry;
00162    if (!(entry = ast_calloc(1, sizeof(*entry)))) {
00163       return NULL;
00164    }
00165    ast_format_copy(&entry->dst_format, dst);
00166    return entry;
00167 }
00168 
00169 static void *softmix_translate_helper_free_entry(struct softmix_translate_helper_entry *entry)
00170 {
00171    if (entry->trans_pvt) {
00172       ast_translator_free_path(entry->trans_pvt);
00173    }
00174    if (entry->out_frame) {
00175       ast_frfree(entry->out_frame);
00176    }
00177    ast_free(entry);
00178    return NULL;
00179 }
00180 
00181 static void softmix_translate_helper_init(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
00182 {
00183    memset(trans_helper, 0, sizeof(*trans_helper));
00184    ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
00185 }
00186 
00187 static void softmix_translate_helper_destroy(struct softmix_translate_helper *trans_helper)
00188 {
00189    struct softmix_translate_helper_entry *entry;
00190 
00191    while ((entry = AST_LIST_REMOVE_HEAD(&trans_helper->entries, entry))) {
00192       softmix_translate_helper_free_entry(entry);
00193    }
00194 }
00195 
00196 static void softmix_translate_helper_change_rate(struct softmix_translate_helper *trans_helper, unsigned int sample_rate)
00197 {
00198    struct softmix_translate_helper_entry *entry;
00199 
00200    ast_format_set(&trans_helper->slin_src, ast_format_slin_by_rate(sample_rate), 0);
00201    AST_LIST_TRAVERSE_SAFE_BEGIN(&trans_helper->entries, entry, entry) {
00202       if (entry->trans_pvt) {
00203          ast_translator_free_path(entry->trans_pvt);
00204          if (!(entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src))) {
00205             AST_LIST_REMOVE_CURRENT(entry);
00206             entry = softmix_translate_helper_free_entry(entry);
00207          }
00208       }
00209    }
00210    AST_LIST_TRAVERSE_SAFE_END;
00211 }
00212 
00213 /*!
00214  * \internal
00215  * \brief Get the next available audio on the softmix channel's read stream
00216  * and determine if it should be mixed out or not on the write stream. 
00217  *
00218  * \retval pointer to buffer containing the exact number of samples requested on success.
00219  * \retval NULL if no samples are present
00220  */
00221 static int16_t *softmix_process_read_audio(struct softmix_channel *sc, unsigned int num_samples)
00222 {
00223    if ((ast_slinfactory_available(&sc->factory) >= num_samples) &&
00224       ast_slinfactory_read(&sc->factory, sc->our_buf, num_samples)) {
00225       sc->have_audio = 1;
00226       return sc->our_buf;
00227    }
00228    sc->have_audio = 0;
00229    return NULL;
00230 }
00231 
00232 /*!
00233  * \internal
00234  * \brief Process a softmix channel's write audio
00235  *
00236  * \details This function will remove the channel's talking from its own audio if present and
00237  * possibly even do the channel's write translation for it depending on how many other
00238  * channels use the same write format.
00239  */
00240 static void softmix_process_write_audio(struct softmix_translate_helper *trans_helper,
00241    struct ast_format *raw_write_fmt,
00242    struct softmix_channel *sc)
00243 {
00244    struct softmix_translate_helper_entry *entry = NULL;
00245    int i;
00246 
00247    /* If we provided audio that was not determined to be silence,
00248     * then take it out while in slinear format. */
00249    if (sc->have_audio && sc->talking) {
00250       for (i = 0; i < sc->write_frame.samples; i++) {
00251          ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
00252       }
00253       /* do not do any special write translate optimization if we had to make
00254        * a special mix for them to remove their own audio. */
00255       return;
00256    }
00257 
00258    AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
00259       if (ast_format_cmp(&entry->dst_format, raw_write_fmt) == AST_FORMAT_CMP_EQUAL) {
00260          entry->num_times_requested++;
00261       } else {
00262          continue;
00263       }
00264       if (!entry->trans_pvt && (entry->num_times_requested > 1)) {
00265          entry->trans_pvt = ast_translator_build_path(&entry->dst_format, &trans_helper->slin_src);
00266       }
00267       if (entry->trans_pvt && !entry->out_frame) {
00268          entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0);
00269       }
00270       if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) {
00271          ast_format_copy(&sc->write_frame.subclass.format, &entry->out_frame->subclass.format);
00272          memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen);
00273          sc->write_frame.datalen = entry->out_frame->datalen;
00274          sc->write_frame.samples = entry->out_frame->samples;
00275       }
00276       break;
00277    }
00278 
00279    /* add new entry into list if this format destination was not matched. */
00280    if (!entry && (entry = softmix_translate_helper_entry_alloc(raw_write_fmt))) {
00281       AST_LIST_INSERT_HEAD(&trans_helper->entries, entry, entry);
00282    }
00283 }
00284 
00285 static void softmix_translate_helper_cleanup(struct softmix_translate_helper *trans_helper)
00286 {
00287    struct softmix_translate_helper_entry *entry = NULL;
00288    AST_LIST_TRAVERSE(&trans_helper->entries, entry, entry) {
00289       if (entry->out_frame) {
00290          ast_frfree(entry->out_frame);
00291          entry->out_frame = NULL;
00292       }
00293       entry->num_times_requested = 0;
00294    }
00295 }
00296 
00297 static void softmix_bridge_data_destroy(void *obj)
00298 {
00299    struct softmix_bridge_data *softmix_data = obj;
00300 
00301    if (softmix_data->timer) {
00302       ast_timer_close(softmix_data->timer);
00303       softmix_data->timer = NULL;
00304    }
00305 }
00306 
00307 /*! \brief Function called when a bridge is created */
00308 static int softmix_bridge_create(struct ast_bridge *bridge)
00309 {
00310    struct softmix_bridge_data *softmix_data;
00311 
00312    if (!(softmix_data = ao2_alloc(sizeof(*softmix_data), softmix_bridge_data_destroy))) {
00313       return -1;
00314    }
00315    if (!(softmix_data->timer = ast_timer_open())) {
00316       ast_log(AST_LOG_WARNING, "Failed to open timer for softmix bridge\n");
00317       ao2_ref(softmix_data, -1);
00318       return -1;
00319    }
00320 
00321    /* start at 8khz, let it grow from there */
00322    softmix_data->internal_rate = 8000;
00323    softmix_data->internal_mixing_interval = DEFAULT_SOFTMIX_INTERVAL;
00324 
00325    bridge->bridge_pvt = softmix_data;
00326    return 0;
00327 }
00328 
00329 /*! \brief Function called when a bridge is destroyed */
00330 static int softmix_bridge_destroy(struct ast_bridge *bridge)
00331 {
00332    struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
00333    if (!bridge->bridge_pvt) {
00334       return -1;
00335    }
00336    ao2_ref(softmix_data, -1);
00337    bridge->bridge_pvt = NULL;
00338    return 0;
00339 }
00340 
00341 static void set_softmix_bridge_data(int rate, int interval, struct ast_bridge_channel *bridge_channel, int reset)
00342 {
00343    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00344    unsigned int channel_read_rate = ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan));
00345 
00346    ast_mutex_lock(&sc->lock);
00347    if (reset) {
00348       ast_slinfactory_destroy(&sc->factory);
00349       ast_dsp_free(sc->dsp);
00350    }
00351    /* Setup read/write frame parameters */
00352    sc->write_frame.frametype = AST_FRAME_VOICE;
00353    ast_format_set(&sc->write_frame.subclass.format, ast_format_slin_by_rate(rate), 0);
00354    sc->write_frame.data.ptr = sc->final_buf;
00355    sc->write_frame.datalen = SOFTMIX_DATALEN(rate, interval);
00356    sc->write_frame.samples = SOFTMIX_SAMPLES(rate, interval);
00357 
00358    sc->read_frame.frametype = AST_FRAME_VOICE;
00359    ast_format_set(&sc->read_frame.subclass.format, ast_format_slin_by_rate(channel_read_rate), 0);
00360    sc->read_frame.data.ptr = sc->our_buf;
00361    sc->read_frame.datalen = SOFTMIX_DATALEN(channel_read_rate, interval);
00362    sc->read_frame.samples = SOFTMIX_SAMPLES(channel_read_rate, interval);
00363 
00364    /* Setup smoother */
00365    ast_slinfactory_init_with_format(&sc->factory, &sc->write_frame.subclass.format);
00366 
00367    /* set new read and write formats on channel. */
00368    ast_set_read_format(bridge_channel->chan, &sc->read_frame.subclass.format);
00369    ast_set_write_format(bridge_channel->chan, &sc->write_frame.subclass.format);
00370 
00371    /* set up new DSP.  This is on the read side only right before the read frame enters the smoother.  */
00372    sc->dsp = ast_dsp_new_with_rate(channel_read_rate);
00373    /* we want to aggressively detect silence to avoid feedback */
00374    if (bridge_channel->tech_args.talking_threshold) {
00375       ast_dsp_set_threshold(sc->dsp, bridge_channel->tech_args.talking_threshold);
00376    } else {
00377       ast_dsp_set_threshold(sc->dsp, DEFAULT_SOFTMIX_TALKING_THRESHOLD);
00378    }
00379 
00380    ast_mutex_unlock(&sc->lock);
00381 }
00382 
00383 /*! \brief Function called when a channel is joined into the bridge */
00384 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00385 {
00386    struct softmix_channel *sc = NULL;
00387    struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
00388 
00389    /* Create a new softmix_channel structure and allocate various things on it */
00390    if (!(sc = ast_calloc(1, sizeof(*sc)))) {
00391       return -1;
00392    }
00393 
00394    /* Can't forget the lock */
00395    ast_mutex_init(&sc->lock);
00396 
00397    /* Can't forget to record our pvt structure within the bridged channel structure */
00398    bridge_channel->bridge_pvt = sc;
00399 
00400    set_softmix_bridge_data(softmix_data->internal_rate,
00401       softmix_data->internal_mixing_interval ? softmix_data->internal_mixing_interval : DEFAULT_SOFTMIX_INTERVAL,
00402       bridge_channel, 0);
00403 
00404    return 0;
00405 }
00406 
00407 /*! \brief Function called when a channel leaves the bridge */
00408 static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00409 {
00410    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00411 
00412    if (!(bridge_channel->bridge_pvt)) {
00413       return 0;
00414    }
00415    bridge_channel->bridge_pvt = NULL;
00416 
00417    /* Drop mutex lock */
00418    ast_mutex_destroy(&sc->lock);
00419 
00420    /* Drop the factory */
00421    ast_slinfactory_destroy(&sc->factory);
00422 
00423    /* Drop the DSP */
00424    ast_dsp_free(sc->dsp);
00425 
00426    /* Eep! drop ourselves */
00427    ast_free(sc);
00428 
00429    return 0;
00430 }
00431 
00432 /*!
00433  * \internal
00434  * \brief If the bridging core passes DTMF to us, then they want it to be distributed out to all memebers. Do that here.
00435  */
00436 static void softmix_pass_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
00437 {
00438    struct ast_bridge_channel *tmp;
00439    AST_LIST_TRAVERSE(&bridge->channels, tmp, entry) {
00440       if (tmp == bridge_channel) {
00441          continue;
00442       }
00443       ast_write(tmp->chan, frame);
00444    }
00445 }
00446 
00447 static void softmix_pass_video_top_priority(struct ast_bridge *bridge, struct ast_frame *frame)
00448 {
00449    struct ast_bridge_channel *tmp;
00450    AST_LIST_TRAVERSE(&bridge->channels, tmp, entry) {
00451       if (tmp->suspended) {
00452          continue;
00453       }
00454       if (ast_bridge_is_video_src(bridge, tmp->chan) == 1) {
00455          ast_write(tmp->chan, frame);
00456          break;
00457       }
00458    }
00459 }
00460 
00461 static void softmix_pass_video_all(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame, int echo)
00462 {
00463    struct ast_bridge_channel *tmp;
00464    AST_LIST_TRAVERSE(&bridge->channels, tmp, entry) {
00465       if (tmp->suspended) {
00466          continue;
00467       }
00468       if ((tmp->chan == bridge_channel->chan) && !echo) {
00469          continue;
00470       }
00471       ast_write(tmp->chan, frame);
00472    }
00473 }
00474 
00475 /*! \brief Function called when a channel writes a frame into the bridge */
00476 static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
00477 {
00478    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00479    struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
00480    int totalsilence = 0;
00481    int cur_energy = 0;
00482    int silence_threshold = bridge_channel->tech_args.silence_threshold ?
00483       bridge_channel->tech_args.silence_threshold :
00484       DEFAULT_SOFTMIX_SILENCE_THRESHOLD;
00485    char update_talking = -1;  /* if this is set to 0 or 1, tell the bridge that the channel has started or stopped talking. */
00486    int res = AST_BRIDGE_WRITE_SUCCESS;
00487 
00488    /* Only accept audio frames, all others are unsupported */
00489    if (frame->frametype == AST_FRAME_DTMF_END || frame->frametype == AST_FRAME_DTMF_BEGIN) {
00490       softmix_pass_dtmf(bridge, bridge_channel, frame);
00491       goto bridge_write_cleanup;
00492    } else if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO) {
00493       res = AST_BRIDGE_WRITE_UNSUPPORTED;
00494       goto bridge_write_cleanup;
00495    } else if (frame->datalen == 0) {
00496       goto bridge_write_cleanup;
00497    }
00498 
00499    /* Determine if this video frame should be distributed or not */
00500    if (frame->frametype == AST_FRAME_VIDEO) {
00501       int num_src = ast_bridge_number_video_src(bridge);
00502       int video_src_priority = ast_bridge_is_video_src(bridge, bridge_channel->chan);
00503 
00504       switch (bridge->video_mode.mode) {
00505       case AST_BRIDGE_VIDEO_MODE_NONE:
00506          break;
00507       case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
00508          if (video_src_priority == 1) {
00509             softmix_pass_video_all(bridge, bridge_channel, frame, 1);
00510          }
00511          break;
00512       case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
00513          ast_mutex_lock(&sc->lock);
00514          ast_bridge_update_talker_src_video_mode(bridge, bridge_channel->chan, sc->video_talker.energy_average, ast_format_get_video_mark(&frame->subclass.format));
00515          ast_mutex_unlock(&sc->lock);
00516          if (video_src_priority == 1) {
00517             int echo = num_src > 1 ? 0 : 1;
00518             softmix_pass_video_all(bridge, bridge_channel, frame, echo);
00519          } else if (video_src_priority == 2) {
00520             softmix_pass_video_top_priority(bridge, frame);
00521          }
00522          break;
00523       }
00524       goto bridge_write_cleanup;
00525    }
00526 
00527    /* If we made it here, we are going to write the frame into the conference */
00528    ast_mutex_lock(&sc->lock);
00529    ast_dsp_silence_with_energy(sc->dsp, frame, &totalsilence, &cur_energy);
00530 
00531    if (bridge->video_mode.mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC) {
00532       int cur_slot = sc->video_talker.energy_history_cur_slot;
00533       sc->video_talker.energy_accum -= sc->video_talker.energy_history[cur_slot];
00534       sc->video_talker.energy_accum += cur_energy;
00535       sc->video_talker.energy_history[cur_slot] = cur_energy;
00536       sc->video_talker.energy_average = sc->video_talker.energy_accum / DEFAULT_ENERGY_HISTORY_LEN;
00537       sc->video_talker.energy_history_cur_slot++;
00538       if (sc->video_talker.energy_history_cur_slot == DEFAULT_ENERGY_HISTORY_LEN) {
00539          sc->video_talker.energy_history_cur_slot = 0; /* wrap around */
00540       }
00541    }
00542 
00543    if (totalsilence < silence_threshold) {
00544       if (!sc->talking) {
00545          update_talking = 1;
00546       }
00547       sc->talking = 1; /* tell the write process we have audio to be mixed out */
00548    } else {
00549       if (sc->talking) {
00550          update_talking = 0;
00551       }
00552       sc->talking = 0;
00553    }
00554 
00555    /* Before adding audio in, make sure we haven't fallen behind. If audio has fallen
00556     * behind 4 times the amount of samples mixed on every iteration of the mixer, Re-sync
00557     * the audio by flushing the buffer before adding new audio in. */
00558    if (ast_slinfactory_available(&sc->factory) > (4 * SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval))) {
00559       ast_slinfactory_flush(&sc->factory);
00560    }
00561 
00562    /* If a frame was provided add it to the smoother, unless drop silence is enabled and this frame
00563     * is not determined to be talking. */
00564    if (!(bridge_channel->tech_args.drop_silence && !sc->talking) &&
00565       (frame->frametype == AST_FRAME_VOICE && ast_format_is_slinear(&frame->subclass.format))) {
00566       ast_slinfactory_feed(&sc->factory, frame);
00567    }
00568 
00569    /* If a frame is ready to be written out, do so */
00570    if (sc->have_frame) {
00571       ast_write(bridge_channel->chan, &sc->write_frame);
00572       sc->have_frame = 0;
00573    }
00574 
00575    /* Alllll done */
00576    ast_mutex_unlock(&sc->lock);
00577 
00578    if (update_talking != -1) {
00579       ast_bridge_notify_talking(bridge, bridge_channel, update_talking);
00580    }
00581 
00582    return res;
00583 
00584 bridge_write_cleanup:
00585    /* Even though the frame is not being written into the conference because it is not audio,
00586     * we should use this opportunity to check to see if a frame is ready to be written out from
00587     * the conference to the channel. */
00588    ast_mutex_lock(&sc->lock);
00589    if (sc->have_frame) {
00590       ast_write(bridge_channel->chan, &sc->write_frame);
00591       sc->have_frame = 0;
00592    }
00593    ast_mutex_unlock(&sc->lock);
00594 
00595    return res;
00596 }
00597 
00598 /*! \brief Function called when the channel's thread is poked */
00599 static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00600 {
00601    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00602 
00603    ast_mutex_lock(&sc->lock);
00604 
00605    if (sc->have_frame) {
00606       ast_write(bridge_channel->chan, &sc->write_frame);
00607       sc->have_frame = 0;
00608    }
00609 
00610    ast_mutex_unlock(&sc->lock);
00611 
00612    return 0;
00613 }
00614 
00615 static void gather_softmix_stats(struct softmix_stats *stats,
00616    const struct softmix_bridge_data *softmix_data,
00617    struct ast_bridge_channel *bridge_channel)
00618 {
00619    int channel_native_rate;
00620    int i;
00621    /* Gather stats about channel sample rates. */
00622    channel_native_rate = MAX(ast_format_rate(ast_channel_rawwriteformat(bridge_channel->chan)),
00623       ast_format_rate(ast_channel_rawreadformat(bridge_channel->chan)));
00624 
00625    if (channel_native_rate > stats->highest_supported_rate) {
00626       stats->highest_supported_rate = channel_native_rate;
00627    }
00628    if (channel_native_rate > softmix_data->internal_rate) {
00629       for (i = 0; i < ARRAY_LEN(stats->sample_rates); i++) {
00630          if (stats->sample_rates[i] == channel_native_rate) {
00631             stats->num_channels[i]++;
00632             break;
00633          } else if (!stats->sample_rates[i]) {
00634             stats->sample_rates[i] = channel_native_rate;
00635             stats->num_channels[i]++;
00636             break;
00637          }
00638       }
00639       stats->num_above_internal_rate++;
00640    } else if (channel_native_rate == softmix_data->internal_rate) {
00641       stats->num_at_internal_rate++;
00642    }
00643 }
00644 /*!
00645  * \internal
00646  * \brief Analyse mixing statistics and change bridges internal rate
00647  * if necessary.
00648  *
00649  * \retval 0, no changes to internal rate 
00650  * \ratval 1, internal rate was changed, update all the channels on the next mixing iteration.
00651  */
00652 static unsigned int analyse_softmix_stats(struct softmix_stats *stats, struct softmix_bridge_data *softmix_data)
00653 {
00654    int i;
00655    /* Re-adjust the internal bridge sample rate if
00656     * 1. The bridge's internal sample rate is locked in at a sample
00657     *    rate other than the current sample rate being used.
00658     * 2. two or more channels support a higher sample rate
00659     * 3. no channels support the current sample rate or a higher rate
00660     */
00661    if (stats->locked_rate) {
00662       /* if the rate is locked by the bridge, only update it if it differs
00663        * from the current rate we are using. */
00664       if (softmix_data->internal_rate != stats->locked_rate) {
00665          softmix_data->internal_rate = stats->locked_rate;
00666          ast_debug(1, " Bridge is locked in at sample rate %d\n", softmix_data->internal_rate);
00667          return 1;
00668       }
00669    } else if (stats->num_above_internal_rate >= 2) {
00670       /* the highest rate is just used as a starting point */
00671       unsigned int best_rate = stats->highest_supported_rate;
00672       int best_index = -1;
00673 
00674       for (i = 0; i < ARRAY_LEN(stats->num_channels); i++) {
00675          if (stats->num_channels[i]) {
00676             break;
00677          }
00678          /* best_rate starts out being the first sample rate
00679           * greater than the internal sample rate that 2 or
00680           * more channels support. */
00681          if (stats->num_channels[i] >= 2 && (best_index == -1)) {
00682             best_rate = stats->sample_rates[i];
00683             best_index = i;
00684          /* If it has been detected that multiple rates above
00685           * the internal rate are present, compare those rates
00686           * to each other and pick the highest one two or more
00687           * channels support. */
00688          } else if (((best_index != -1) &&
00689             (stats->num_channels[i] >= 2) &&
00690             (stats->sample_rates[best_index] < stats->sample_rates[i]))) {
00691             best_rate = stats->sample_rates[i];
00692             best_index = i;
00693          /* It is possible that multiple channels exist with native sample
00694           * rates above the internal sample rate, but none of those channels
00695           * have the same rate in common.  In this case, the lowest sample
00696           * rate among those channels is picked. Over time as additional
00697           * statistic runs are made the internal sample rate number will
00698           * adjust to the most optimal sample rate, but it may take multiple
00699           * iterations. */
00700          } else if (best_index == -1) {
00701             best_rate = MIN(best_rate, stats->sample_rates[i]);
00702          }
00703       }
00704 
00705       ast_debug(1, " Bridge changed from %d To %d\n", softmix_data->internal_rate, best_rate);
00706       softmix_data->internal_rate = best_rate;
00707       return 1;
00708    } else if (!stats->num_at_internal_rate && !stats->num_above_internal_rate) {
00709       /* In this case, the highest supported rate is actually lower than the internal rate */
00710       softmix_data->internal_rate = stats->highest_supported_rate;
00711       ast_debug(1, " Bridge changed from %d to %d\n", softmix_data->internal_rate, stats->highest_supported_rate);
00712       return 1;
00713    }
00714    return 0;
00715 }
00716 
00717 static int softmix_mixing_array_init(struct softmix_mixing_array *mixing_array, unsigned int starting_num_entries)
00718 {
00719    memset(mixing_array, 0, sizeof(*mixing_array));
00720    mixing_array->max_num_entries = starting_num_entries;
00721    if (!(mixing_array->buffers = ast_calloc(mixing_array->max_num_entries, sizeof(int16_t *)))) {
00722       ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure. \n");
00723       return -1;
00724    }
00725    return 0;
00726 }
00727 
00728 static void softmix_mixing_array_destroy(struct softmix_mixing_array *mixing_array)
00729 {
00730    ast_free(mixing_array->buffers);
00731 }
00732 
00733 static int softmix_mixing_array_grow(struct softmix_mixing_array *mixing_array, unsigned int num_entries)
00734 {
00735    int16_t **tmp;
00736    /* give it some room to grow since memory is cheap but allocations can be expensive */
00737    mixing_array->max_num_entries = num_entries;
00738    if (!(tmp = ast_realloc(mixing_array->buffers, (mixing_array->max_num_entries * sizeof(int16_t *))))) {
00739       ast_log(LOG_NOTICE, "Failed to re-allocate softmix mixing structure. \n");
00740       return -1;
00741    }
00742    mixing_array->buffers = tmp;
00743    return 0;
00744 }
00745 
00746 /*! \brief Function which acts as the mixing thread */
00747 static int softmix_bridge_thread(struct ast_bridge *bridge)
00748 {
00749    struct softmix_stats stats = { { 0 }, };
00750    struct softmix_mixing_array mixing_array;
00751    struct softmix_bridge_data *softmix_data = bridge->bridge_pvt;
00752    struct ast_timer *timer;
00753    struct softmix_translate_helper trans_helper;
00754    int16_t buf[MAX_DATALEN] = { 0, };
00755    unsigned int stat_iteration_counter = 0; /* counts down, gather stats at zero and reset. */
00756    int timingfd;
00757    int update_all_rates = 0; /* set this when the internal sample rate has changed */
00758    int i, x;
00759    int res = -1;
00760 
00761    if (!(softmix_data = bridge->bridge_pvt)) {
00762       goto softmix_cleanup;
00763    }
00764 
00765    ao2_ref(softmix_data, 1);
00766    timer = softmix_data->timer;
00767    timingfd = ast_timer_fd(timer);
00768    softmix_translate_helper_init(&trans_helper, softmix_data->internal_rate);
00769    ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
00770 
00771    /* Give the mixing array room to grow, memory is cheap but allocations are expensive. */
00772    if (softmix_mixing_array_init(&mixing_array, bridge->num + 10)) {
00773       ast_log(LOG_NOTICE, "Failed to allocate softmix mixing structure. \n");
00774       goto softmix_cleanup;
00775    }
00776 
00777    while (!bridge->stop && !bridge->refresh && bridge->array_num) {
00778       struct ast_bridge_channel *bridge_channel = NULL;
00779       int timeout = -1;
00780       enum ast_format_id cur_slin_id = ast_format_slin_by_rate(softmix_data->internal_rate);
00781       unsigned int softmix_samples = SOFTMIX_SAMPLES(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
00782       unsigned int softmix_datalen = SOFTMIX_DATALEN(softmix_data->internal_rate, softmix_data->internal_mixing_interval);
00783 
00784       if (softmix_datalen > MAX_DATALEN) {
00785          /* This should NEVER happen, but if it does we need to know about it. Almost
00786           * all the memcpys used during this process depend on this assumption.  Rather
00787           * than checking this over and over again through out the code, this single
00788           * verification is done on each iteration. */
00789          ast_log(LOG_WARNING, "Conference mixing error, requested mixing length greater than mixing buffer.\n");
00790          goto softmix_cleanup;
00791       }
00792 
00793       /* Grow the mixing array buffer as participants are added. */
00794       if (mixing_array.max_num_entries < bridge->num && softmix_mixing_array_grow(&mixing_array, bridge->num + 5)) {
00795          goto softmix_cleanup;
00796       }
00797 
00798       /* init the number of buffers stored in the mixing array to 0.
00799        * As buffers are added for mixing, this number is incremented. */
00800       mixing_array.used_entries = 0;
00801 
00802       /* These variables help determine if a rate change is required */
00803       if (!stat_iteration_counter) {
00804          memset(&stats, 0, sizeof(stats));
00805          stats.locked_rate = bridge->internal_sample_rate;
00806       }
00807 
00808       /* If the sample rate has changed, update the translator helper */
00809       if (update_all_rates) {
00810          softmix_translate_helper_change_rate(&trans_helper, softmix_data->internal_rate);
00811       }
00812 
00813       /* Go through pulling audio from each factory that has it available */
00814       AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
00815          struct softmix_channel *sc = bridge_channel->bridge_pvt;
00816 
00817          /* Update the sample rate to match the bridge's native sample rate if necessary. */
00818          if (update_all_rates) {
00819             set_softmix_bridge_data(softmix_data->internal_rate, softmix_data->internal_mixing_interval, bridge_channel, 1);
00820          }
00821 
00822          /* If stat_iteration_counter is 0, then collect statistics during this mixing interation */
00823          if (!stat_iteration_counter) {
00824             gather_softmix_stats(&stats, softmix_data, bridge_channel);
00825          }
00826 
00827          /* if the channel is suspended, don't check for audio, but still gather stats */
00828          if (bridge_channel->suspended) {
00829             continue;
00830          }
00831 
00832          /* Try to get audio from the factory if available */
00833          ast_mutex_lock(&sc->lock);
00834          if ((mixing_array.buffers[mixing_array.used_entries] = softmix_process_read_audio(sc, softmix_samples))) {
00835             mixing_array.used_entries++;
00836          }
00837          ast_mutex_unlock(&sc->lock);
00838       }
00839 
00840       /* mix it like crazy */
00841       memset(buf, 0, softmix_datalen);
00842       for (i = 0; i < mixing_array.used_entries; i++) {
00843          for (x = 0; x < softmix_samples; x++) {
00844             ast_slinear_saturated_add(buf + x, mixing_array.buffers[i] + x);
00845          }
00846       }
00847 
00848       /* Next step go through removing the channel's own audio and creating a good frame... */
00849       AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
00850          struct softmix_channel *sc = bridge_channel->bridge_pvt;
00851 
00852          if (bridge_channel->suspended) {
00853             continue;
00854          }
00855 
00856          ast_mutex_lock(&sc->lock);
00857 
00858          /* Make SLINEAR write frame from local buffer */
00859          if (sc->write_frame.subclass.format.id != cur_slin_id) {
00860             ast_format_set(&sc->write_frame.subclass.format, cur_slin_id, 0);
00861          }
00862          sc->write_frame.datalen = softmix_datalen;
00863          sc->write_frame.samples = softmix_samples;
00864          memcpy(sc->final_buf, buf, softmix_datalen);
00865 
00866          /* process the softmix channel's new write audio */
00867          softmix_process_write_audio(&trans_helper, ast_channel_rawwriteformat(bridge_channel->chan), sc);
00868 
00869          /* The frame is now ready for use... */
00870          sc->have_frame = 1;
00871 
00872          ast_mutex_unlock(&sc->lock);
00873 
00874          /* Poke bridged channel thread just in case */
00875          pthread_kill(bridge_channel->thread, SIGURG);
00876       }
00877 
00878       update_all_rates = 0;
00879       if (!stat_iteration_counter) {
00880          update_all_rates = analyse_softmix_stats(&stats, softmix_data);
00881          stat_iteration_counter = SOFTMIX_STAT_INTERVAL;
00882       }
00883       stat_iteration_counter--;
00884 
00885       ao2_unlock(bridge);
00886       /* cleanup any translation frame data from the previous mixing iteration. */
00887       softmix_translate_helper_cleanup(&trans_helper);
00888       /* Wait for the timing source to tell us to wake up and get things done */
00889       ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
00890       if (ast_timer_ack(timer, 1) < 0) {
00891          ast_log(LOG_ERROR, "Failed to acknowledge timer in softmix bridge\n");
00892          ao2_lock(bridge);
00893          goto softmix_cleanup;
00894       }
00895       ao2_lock(bridge);
00896 
00897       /* make sure to detect mixing interval changes if they occur. */
00898       if (bridge->internal_mixing_interval && (bridge->internal_mixing_interval != softmix_data->internal_mixing_interval)) {
00899          softmix_data->internal_mixing_interval = bridge->internal_mixing_interval;
00900          ast_timer_set_rate(timer, (1000 / softmix_data->internal_mixing_interval));
00901          update_all_rates = 1; /* if the interval changes, the rates must be adjusted as well just to be notified new interval.*/
00902       }
00903    }
00904 
00905    res = 0;
00906 
00907 softmix_cleanup:
00908    softmix_translate_helper_destroy(&trans_helper);
00909    softmix_mixing_array_destroy(&mixing_array);
00910    if (softmix_data) {
00911       ao2_ref(softmix_data, -1);
00912    }
00913    return res;
00914 }
00915 
00916 static struct ast_bridge_technology softmix_bridge = {
00917    .name = "softmix",
00918    .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED | AST_BRIDGE_CAPABILITY_OPTIMIZE | AST_BRIDGE_CAPABILITY_VIDEO,
00919    .preference = AST_BRIDGE_PREFERENCE_LOW,
00920    .create = softmix_bridge_create,
00921    .destroy = softmix_bridge_destroy,
00922    .join = softmix_bridge_join,
00923    .leave = softmix_bridge_leave,
00924    .write = softmix_bridge_write,
00925    .thread = softmix_bridge_thread,
00926    .poke = softmix_bridge_poke,
00927 };
00928 
00929 static int unload_module(void)
00930 {
00931    ast_format_cap_destroy(softmix_bridge.format_capabilities);
00932    return ast_bridge_technology_unregister(&softmix_bridge);
00933 }
00934 
00935 static int load_module(void)
00936 {
00937    struct ast_format tmp;
00938    if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc())) {
00939       return AST_MODULE_LOAD_DECLINE;
00940    }
00941    ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
00942    return ast_bridge_technology_register(&softmix_bridge);
00943 }
00944 
00945 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");