Tue Aug 20 16:34:25 2013

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) 2007, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Multi-party software based channel mixing
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  *
00025  * \ingroup bridges
00026  *
00027  * \todo This bridge operates in 8 kHz mode unless a define is uncommented.
00028  * This needs to be improved so the bridge moves between the dominant codec as needed depending
00029  * on channels present in the bridge and transcoding capabilities.
00030  */
00031 
00032 /*** MODULEINFO
00033    <support_level>core</support_level>
00034  ***/
00035 
00036 #include "asterisk.h"
00037 
00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 379091 $")
00039 
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <string.h>
00043 #include <sys/time.h>
00044 #include <signal.h>
00045 #include <errno.h>
00046 #include <unistd.h>
00047 
00048 #include "asterisk/module.h"
00049 #include "asterisk/channel.h"
00050 #include "asterisk/bridging.h"
00051 #include "asterisk/bridging_technology.h"
00052 #include "asterisk/frame.h"
00053 #include "asterisk/options.h"
00054 #include "asterisk/logger.h"
00055 #include "asterisk/slinfactory.h"
00056 #include "asterisk/astobj2.h"
00057 #include "asterisk/timing.h"
00058 
00059 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
00060 #define SOFTMIX_INTERVAL 20
00061 
00062 /*! \brief Size of the buffer used for sample manipulation */
00063 #define SOFTMIX_DATALEN (160 * (SOFTMIX_INTERVAL / 10))
00064 
00065 /*! \brief Number of samples we are dealing with */
00066 #define SOFTMIX_SAMPLES (SOFTMIX_DATALEN / 2)
00067 
00068 /*! \brief Define used to turn on 16 kHz audio support */
00069 /* #define SOFTMIX_16_SUPPORT */
00070 
00071 /*! \brief Structure which contains per-channel mixing information */
00072 struct softmix_channel {
00073    /*! Lock to protect this structure */
00074    ast_mutex_t lock;
00075    /*! Factory which contains audio read in from the channel */
00076    struct ast_slinfactory factory;
00077    /*! Frame that contains mixed audio to be written out to the channel */
00078    struct ast_frame frame;
00079    /*! Bit used to indicate that the channel provided audio for this mixing interval */
00080    int have_audio:1;
00081    /*! Bit used to indicate that a frame is available to be written out to the channel */
00082    int have_frame:1;
00083    /*! Buffer containing final mixed audio from all sources */
00084    short final_buf[SOFTMIX_DATALEN];
00085    /*! Buffer containing only the audio from the channel */
00086    short our_buf[SOFTMIX_DATALEN];
00087 };
00088 
00089 /*! \brief Function called when a bridge is created */
00090 static int softmix_bridge_create(struct ast_bridge *bridge)
00091 {
00092    struct ast_timer *timer;
00093 
00094    if (!(timer = ast_timer_open())) {
00095       return -1;
00096    }
00097 
00098    bridge->bridge_pvt = timer;
00099 
00100    return 0;
00101 }
00102 
00103 /*! \brief Function called when a bridge is destroyed */
00104 static int softmix_bridge_destroy(struct ast_bridge *bridge)
00105 {
00106    if (!bridge->bridge_pvt) {
00107       return -1;
00108    }
00109    ast_timer_close((struct ast_timer *) bridge->bridge_pvt);
00110    bridge->bridge_pvt = NULL;
00111    return 0;
00112 }
00113 
00114 /*! \brief Function called when a channel is joined into the bridge */
00115 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00116 {
00117    struct softmix_channel *sc = NULL;
00118 
00119    /* Create a new softmix_channel structure and allocate various things on it */
00120    if (!(sc = ast_calloc(1, sizeof(*sc)))) {
00121       return -1;
00122    }
00123 
00124    /* Can't forget the lock */
00125    ast_mutex_init(&sc->lock);
00126 
00127    /* Setup smoother */
00128    ast_slinfactory_init(&sc->factory);
00129 
00130    /* Setup frame parameters */
00131    sc->frame.frametype = AST_FRAME_VOICE;
00132 #ifdef SOFTMIX_16_SUPPORT
00133    sc->frame.subclass.codec = AST_FORMAT_SLINEAR16;
00134 #else
00135    sc->frame.subclass.codec = AST_FORMAT_SLINEAR;
00136 #endif
00137    sc->frame.data.ptr = sc->final_buf;
00138    sc->frame.datalen = SOFTMIX_DATALEN;
00139    sc->frame.samples = SOFTMIX_SAMPLES;
00140 
00141    /* Can't forget to record our pvt structure within the bridged channel structure */
00142    bridge_channel->bridge_pvt = sc;
00143 
00144    return 0;
00145 }
00146 
00147 /*! \brief Function called when a channel leaves the bridge */
00148 static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00149 {
00150    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00151 
00152    if (!(bridge_channel->bridge_pvt)) {
00153       return 0;
00154    }
00155    bridge_channel->bridge_pvt = NULL;
00156 
00157    /* Drop mutex lock */
00158    ast_mutex_destroy(&sc->lock);
00159 
00160    /* Drop the factory */
00161    ast_slinfactory_destroy(&sc->factory);
00162 
00163    /* Eep! drop ourselves */
00164    ast_free(sc);
00165 
00166    return 0;
00167 }
00168 
00169 /*! \brief Function called when a channel writes a frame into the bridge */
00170 static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
00171 {
00172    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00173 
00174    /* Only accept audio frames, all others are unsupported */
00175    if (frame->frametype != AST_FRAME_VOICE) {
00176       return AST_BRIDGE_WRITE_UNSUPPORTED;
00177    }
00178 
00179    ast_mutex_lock(&sc->lock);
00180 
00181    /* If a frame was provided add it to the smoother */
00182 #ifdef SOFTMIX_16_SUPPORT
00183    if (frame->frametype == AST_FRAME_VOICE && frame->subclass.codec == AST_FORMAT_SLINEAR16) {
00184 #else
00185    if (frame->frametype == AST_FRAME_VOICE && frame->subclass.codec == AST_FORMAT_SLINEAR) {
00186 #endif
00187       ast_slinfactory_feed(&sc->factory, frame);
00188    }
00189 
00190    /* If a frame is ready to be written out, do so */
00191    if (sc->have_frame) {
00192       ast_write(bridge_channel->chan, &sc->frame);
00193       sc->have_frame = 0;
00194    }
00195 
00196    /* Alllll done */
00197    ast_mutex_unlock(&sc->lock);
00198 
00199    return AST_BRIDGE_WRITE_SUCCESS;
00200 }
00201 
00202 /*! \brief Function called when the channel's thread is poked */
00203 static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
00204 {
00205    struct softmix_channel *sc = bridge_channel->bridge_pvt;
00206 
00207    ast_mutex_lock(&sc->lock);
00208 
00209    if (sc->have_frame) {
00210       ast_write(bridge_channel->chan, &sc->frame);
00211       sc->have_frame = 0;
00212    }
00213 
00214    ast_mutex_unlock(&sc->lock);
00215 
00216    return 0;
00217 }
00218 
00219 /*! \brief Function which acts as the mixing thread */
00220 static int softmix_bridge_thread(struct ast_bridge *bridge)
00221 {
00222    struct ast_timer *timer = (struct ast_timer *) bridge->bridge_pvt;
00223    int timingfd = ast_timer_fd(timer);
00224 
00225    ast_timer_set_rate(timer, (1000 / SOFTMIX_INTERVAL));
00226 
00227    while (!bridge->stop && !bridge->refresh && bridge->array_num) {
00228       struct ast_bridge_channel *bridge_channel = NULL;
00229       short buf[SOFTMIX_DATALEN] = {0, };
00230       int timeout = -1;
00231 
00232       /* Go through pulling audio from each factory that has it available */
00233       AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
00234          struct softmix_channel *sc = bridge_channel->bridge_pvt;
00235 
00236          ast_mutex_lock(&sc->lock);
00237 
00238          /* Try to get audio from the factory if available */
00239          if (ast_slinfactory_available(&sc->factory) >= SOFTMIX_SAMPLES && ast_slinfactory_read(&sc->factory, sc->our_buf, SOFTMIX_SAMPLES)) {
00240             short *data1, *data2;
00241             int i;
00242 
00243             /* Put into the local final buffer */
00244             for (i = 0, data1 = buf, data2 = sc->our_buf; i < SOFTMIX_DATALEN; i++, data1++, data2++)
00245                ast_slinear_saturated_add(data1, data2);
00246             /* Yay we have our own audio */
00247             sc->have_audio = 1;
00248          } else {
00249             /* Awww we don't have audio ;( */
00250             sc->have_audio = 0;
00251          }
00252          ast_mutex_unlock(&sc->lock);
00253       }
00254 
00255       /* Next step go through removing the channel's own audio and creating a good frame... */
00256       AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
00257          struct softmix_channel *sc = bridge_channel->bridge_pvt;
00258          int i = 0;
00259 
00260          /* Copy from local final buffer to our final buffer */
00261          memcpy(sc->final_buf, buf, sizeof(sc->final_buf));
00262 
00263          /* If we provided audio then take it out */
00264          if (sc->have_audio) {
00265             for (i = 0; i < SOFTMIX_DATALEN; i++) {
00266                ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
00267             }
00268          }
00269 
00270          /* The frame is now ready for use... */
00271          sc->have_frame = 1;
00272 
00273          /* Poke bridged channel thread just in case */
00274          pthread_kill(bridge_channel->thread, SIGURG);
00275       }
00276 
00277       ao2_unlock(bridge);
00278 
00279       /* Wait for the timing source to tell us to wake up and get things done */
00280       ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
00281 
00282       if (ast_timer_ack(timer, 1) < 0) {
00283          ast_log(LOG_ERROR, "Failed to acknowledge timer in softmix bridge\n");
00284          ao2_lock(bridge);
00285          break;
00286       }
00287 
00288       ao2_lock(bridge);
00289    }
00290 
00291    return 0;
00292 }
00293 
00294 static struct ast_bridge_technology softmix_bridge = {
00295    .name = "softmix",
00296    .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED,
00297    .preference = AST_BRIDGE_PREFERENCE_LOW,
00298 #ifdef SOFTMIX_16_SUPPORT
00299    .formats = AST_FORMAT_SLINEAR16,
00300 #else
00301    .formats = AST_FORMAT_SLINEAR,
00302 #endif
00303    .create = softmix_bridge_create,
00304    .destroy = softmix_bridge_destroy,
00305    .join = softmix_bridge_join,
00306    .leave = softmix_bridge_leave,
00307    .write = softmix_bridge_write,
00308    .thread = softmix_bridge_thread,
00309    .poke = softmix_bridge_poke,
00310 };
00311 
00312 static int unload_module(void)
00313 {
00314    return ast_bridge_technology_unregister(&softmix_bridge);
00315 }
00316 
00317 static int load_module(void)
00318 {
00319    return ast_bridge_technology_register(&softmix_bridge);
00320 }
00321 
00322 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");

Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1