2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Multi-party software based channel mixing
23 * \author Joshua Colp <jcolp@digium.com>
27 * \todo This bridge operates in 8 kHz mode unless a define is uncommented.
28 * This needs to be improved so the bridge moves between the dominant codec as needed depending
29 * on channels present in the bridge and transcoding capabilities.
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/module.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/bridging.h"
47 #include "asterisk/bridging_technology.h"
48 #include "asterisk/frame.h"
49 #include "asterisk/options.h"
50 #include "asterisk/logger.h"
51 #include "asterisk/slinfactory.h"
52 #include "asterisk/astobj2.h"
53 #include "asterisk/timing.h"
55 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
56 #define SOFTMIX_INTERVAL 20
58 /*! \brief Size of the buffer used for sample manipulation */
59 #define SOFTMIX_DATALEN (160 * (SOFTMIX_INTERVAL / 10))
61 /*! \brief Number of samples we are dealing with */
62 #define SOFTMIX_SAMPLES (SOFTMIX_DATALEN / 2)
64 /*! \brief Define used to turn on 16 kHz audio support */
65 /* #define SOFTMIX_16_SUPPORT */
67 /*! \brief Structure which contains per-channel mixing information */
68 struct softmix_channel {
69 /*! Lock to protect this structure */
71 /*! Factory which contains audio read in from the channel */
72 struct ast_slinfactory factory;
73 /*! Frame that contains mixed audio to be written out to the channel */
74 struct ast_frame frame;
75 /*! Bit used to indicate that the channel provided audio for this mixing interval */
77 /*! Bit used to indicate that a frame is available to be written out to the channel */
79 /*! Buffer containing final mixed audio from all sources */
80 short final_buf[SOFTMIX_DATALEN];
81 /*! Buffer containing only the audio from the channel */
82 short our_buf[SOFTMIX_DATALEN];
85 /*! \brief Function called when a bridge is created */
86 static int softmix_bridge_create(struct ast_bridge *bridge)
88 struct ast_timer *timer;
90 if (!(timer = ast_timer_open())) {
94 bridge->bridge_pvt = timer;
99 /*! \brief Function called when a bridge is destroyed */
100 static int softmix_bridge_destroy(struct ast_bridge *bridge)
102 ast_timer_close((struct ast_timer *) bridge->bridge_pvt);
107 /*! \brief Function called when a channel is joined into the bridge */
108 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
110 struct softmix_channel *sc = NULL;
112 /* Create a new softmix_channel structure and allocate various things on it */
113 if (!(sc = ast_calloc(1, sizeof(*sc)))) {
117 /* Can't forget the lock */
118 ast_mutex_init(&sc->lock);
121 ast_slinfactory_init(&sc->factory);
123 /* Setup frame parameters */
124 sc->frame.frametype = AST_FRAME_VOICE;
125 #ifdef SOFTMIX_16_SUPPORT
126 sc->frame.subclass.codec = AST_FORMAT_SLINEAR16;
128 sc->frame.subclass.codec = AST_FORMAT_SLINEAR;
130 sc->frame.data.ptr = sc->final_buf;
131 sc->frame.datalen = SOFTMIX_DATALEN;
132 sc->frame.samples = SOFTMIX_SAMPLES;
134 /* Can't forget to record our pvt structure within the bridged channel structure */
135 bridge_channel->bridge_pvt = sc;
140 /*! \brief Function called when a channel leaves the bridge */
141 static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
143 struct softmix_channel *sc = bridge_channel->bridge_pvt;
145 /* Drop mutex lock */
146 ast_mutex_destroy(&sc->lock);
148 /* Drop the factory */
149 ast_slinfactory_destroy(&sc->factory);
151 /* Eep! drop ourselves */
157 /*! \brief Function called when a channel writes a frame into the bridge */
158 static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
160 struct softmix_channel *sc = bridge_channel->bridge_pvt;
162 /* Only accept audio frames, all others are unsupported */
163 if (frame->frametype != AST_FRAME_VOICE) {
164 return AST_BRIDGE_WRITE_UNSUPPORTED;
167 ast_mutex_lock(&sc->lock);
169 /* If a frame was provided add it to the smoother */
170 #ifdef SOFTMIX_16_SUPPORT
171 if (frame->frametype == AST_FRAME_VOICE && frame->subclass.codec == AST_FORMAT_SLINEAR16) {
173 if (frame->frametype == AST_FRAME_VOICE && frame->subclass.codec == AST_FORMAT_SLINEAR) {
175 ast_slinfactory_feed(&sc->factory, frame);
178 /* If a frame is ready to be written out, do so */
179 if (sc->have_frame) {
180 ast_write(bridge_channel->chan, &sc->frame);
185 ast_mutex_unlock(&sc->lock);
187 return AST_BRIDGE_WRITE_SUCCESS;
190 /*! \brief Function called when the channel's thread is poked */
191 static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
193 struct softmix_channel *sc = bridge_channel->bridge_pvt;
195 ast_mutex_lock(&sc->lock);
197 if (sc->have_frame) {
198 ast_write(bridge_channel->chan, &sc->frame);
202 ast_mutex_unlock(&sc->lock);
207 /*! \brief Function which acts as the mixing thread */
208 static int softmix_bridge_thread(struct ast_bridge *bridge)
210 struct ast_timer *timer = (struct ast_timer *) bridge->bridge_pvt;
211 int timingfd = ast_timer_fd(timer);
213 ast_timer_set_rate(timer, (1000 / SOFTMIX_INTERVAL));
215 while (!bridge->stop && !bridge->refresh && bridge->array_num) {
216 struct ast_bridge_channel *bridge_channel = NULL;
217 short buf[SOFTMIX_DATALEN] = {0, };
220 /* Go through pulling audio from each factory that has it available */
221 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
222 struct softmix_channel *sc = bridge_channel->bridge_pvt;
224 ast_mutex_lock(&sc->lock);
226 /* Try to get audio from the factory if available */
227 if (ast_slinfactory_available(&sc->factory) >= SOFTMIX_SAMPLES && ast_slinfactory_read(&sc->factory, sc->our_buf, SOFTMIX_SAMPLES)) {
228 short *data1, *data2;
231 /* Put into the local final buffer */
232 for (i = 0, data1 = buf, data2 = sc->our_buf; i < SOFTMIX_DATALEN; i++, data1++, data2++)
233 ast_slinear_saturated_add(data1, data2);
234 /* Yay we have our own audio */
237 /* Awww we don't have audio ;( */
240 ast_mutex_unlock(&sc->lock);
243 /* Next step go through removing the channel's own audio and creating a good frame... */
244 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
245 struct softmix_channel *sc = bridge_channel->bridge_pvt;
248 /* Copy from local final buffer to our final buffer */
249 memcpy(sc->final_buf, buf, sizeof(sc->final_buf));
251 /* If we provided audio then take it out */
252 if (sc->have_audio) {
253 for (i = 0; i < SOFTMIX_DATALEN; i++) {
254 ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
258 /* The frame is now ready for use... */
261 /* Poke bridged channel thread just in case */
262 pthread_kill(bridge_channel->thread, SIGURG);
267 /* Wait for the timing source to tell us to wake up and get things done */
268 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
270 ast_timer_ack(timer, 1);
278 static struct ast_bridge_technology softmix_bridge = {
280 .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED,
281 .preference = AST_BRIDGE_PREFERENCE_LOW,
282 #ifdef SOFTMIX_16_SUPPORT
283 .formats = AST_FORMAT_SLINEAR16,
285 .formats = AST_FORMAT_SLINEAR,
287 .create = softmix_bridge_create,
288 .destroy = softmix_bridge_destroy,
289 .join = softmix_bridge_join,
290 .leave = softmix_bridge_leave,
291 .write = softmix_bridge_write,
292 .thread = softmix_bridge_thread,
293 .poke = softmix_bridge_poke,
296 static int unload_module(void)
298 return ast_bridge_technology_unregister(&softmix_bridge);
301 static int load_module(void)
303 return ast_bridge_technology_register(&softmix_bridge);
306 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");