Asterisk media architecture conversion - no more format bitfields
[asterisk/asterisk.git] / bridges / bridge_softmix.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Multi-party software based channel mixing
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  *
25  * \ingroup bridges
26  *
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.
30  */
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/time.h>
40 #include <signal.h>
41 #include <errno.h>
42 #include <unistd.h>
43
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"
54
55 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
56 #define SOFTMIX_INTERVAL 20
57
58 /*! \brief Size of the buffer used for sample manipulation */
59 #define SOFTMIX_DATALEN (160 * (SOFTMIX_INTERVAL / 10))
60
61 /*! \brief Number of samples we are dealing with */
62 #define SOFTMIX_SAMPLES (SOFTMIX_DATALEN / 2)
63
64 /*! \brief Define used to turn on 16 kHz audio support */
65 /* #define SOFTMIX_16_SUPPORT */
66
67 /*! \brief Structure which contains per-channel mixing information */
68 struct softmix_channel {
69         /*! Lock to protect this structure */
70         ast_mutex_t lock;
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 */
76         int have_audio:1;
77         /*! Bit used to indicate that a frame is available to be written out to the channel */
78         int have_frame:1;
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];
83 };
84
85 /*! \brief Function called when a bridge is created */
86 static int softmix_bridge_create(struct ast_bridge *bridge)
87 {
88         struct ast_timer *timer;
89
90         if (!(timer = ast_timer_open())) {
91                 return -1;
92         }
93
94         bridge->bridge_pvt = timer;
95
96         return 0;
97 }
98
99 /*! \brief Function called when a bridge is destroyed */
100 static int softmix_bridge_destroy(struct ast_bridge *bridge)
101 {
102         if (!bridge->bridge_pvt) {
103                 return -1;
104         }
105         ast_timer_close((struct ast_timer *) bridge->bridge_pvt);
106
107         return 0;
108 }
109
110 /*! \brief Function called when a channel is joined into the bridge */
111 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
112 {
113         struct softmix_channel *sc = NULL;
114
115         /* Create a new softmix_channel structure and allocate various things on it */
116         if (!(sc = ast_calloc(1, sizeof(*sc)))) {
117                 return -1;
118         }
119
120         /* Can't forget the lock */
121         ast_mutex_init(&sc->lock);
122
123         /* Setup smoother */
124         ast_slinfactory_init(&sc->factory);
125
126         /* Setup frame parameters */
127         sc->frame.frametype = AST_FRAME_VOICE;
128 #ifdef SOFTMIX_16_SUPPORT
129         ast_format_set(&sc->frame.subclass.format, AST_FORMAT_SLINEAR16, 0);
130 #else
131         ast_format_set(&sc->frame.subclass.format, AST_FORMAT_SLINEAR, 0);
132 #endif
133         sc->frame.data.ptr = sc->final_buf;
134         sc->frame.datalen = SOFTMIX_DATALEN;
135         sc->frame.samples = SOFTMIX_SAMPLES;
136
137         /* Can't forget to record our pvt structure within the bridged channel structure */
138         bridge_channel->bridge_pvt = sc;
139
140         return 0;
141 }
142
143 /*! \brief Function called when a channel leaves the bridge */
144 static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
145 {
146         struct softmix_channel *sc = bridge_channel->bridge_pvt;
147
148         /* Drop mutex lock */
149         ast_mutex_destroy(&sc->lock);
150
151         /* Drop the factory */
152         ast_slinfactory_destroy(&sc->factory);
153
154         /* Eep! drop ourselves */
155         ast_free(sc);
156
157         return 0;
158 }
159
160 /*! \brief Function called when a channel writes a frame into the bridge */
161 static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
162 {
163         struct softmix_channel *sc = bridge_channel->bridge_pvt;
164
165         /* Only accept audio frames, all others are unsupported */
166         if (frame->frametype != AST_FRAME_VOICE) {
167                 return AST_BRIDGE_WRITE_UNSUPPORTED;
168         }
169
170         ast_mutex_lock(&sc->lock);
171
172         /* If a frame was provided add it to the smoother */
173 #ifdef SOFTMIX_16_SUPPORT
174         if (frame->frametype == AST_FRAME_VOICE && frame->subclass.format.id == AST_FORMAT_SLINEAR16) {
175 #else
176         if (frame->frametype == AST_FRAME_VOICE && frame->subclass.format.id == AST_FORMAT_SLINEAR) {
177 #endif
178                 ast_slinfactory_feed(&sc->factory, frame);
179         }
180
181         /* If a frame is ready to be written out, do so */
182         if (sc->have_frame) {
183                 ast_write(bridge_channel->chan, &sc->frame);
184                 sc->have_frame = 0;
185         }
186
187         /* Alllll done */
188         ast_mutex_unlock(&sc->lock);
189
190         return AST_BRIDGE_WRITE_SUCCESS;
191 }
192
193 /*! \brief Function called when the channel's thread is poked */
194 static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
195 {
196         struct softmix_channel *sc = bridge_channel->bridge_pvt;
197
198         ast_mutex_lock(&sc->lock);
199
200         if (sc->have_frame) {
201                 ast_write(bridge_channel->chan, &sc->frame);
202                 sc->have_frame = 0;
203         }
204
205         ast_mutex_unlock(&sc->lock);
206
207         return 0;
208 }
209
210 /*! \brief Function which acts as the mixing thread */
211 static int softmix_bridge_thread(struct ast_bridge *bridge)
212 {
213         struct ast_timer *timer = (struct ast_timer *) bridge->bridge_pvt;
214         int timingfd = ast_timer_fd(timer);
215
216         ast_timer_set_rate(timer, (1000 / SOFTMIX_INTERVAL));
217
218         while (!bridge->stop && !bridge->refresh && bridge->array_num) {
219                 struct ast_bridge_channel *bridge_channel = NULL;
220                 short buf[SOFTMIX_DATALEN] = {0, };
221                 int timeout = -1;
222
223                 /* Go through pulling audio from each factory that has it available */
224                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
225                         struct softmix_channel *sc = bridge_channel->bridge_pvt;
226
227                         ast_mutex_lock(&sc->lock);
228
229                         /* Try to get audio from the factory if available */
230                         if (ast_slinfactory_available(&sc->factory) >= SOFTMIX_SAMPLES && ast_slinfactory_read(&sc->factory, sc->our_buf, SOFTMIX_SAMPLES)) {
231                                 short *data1, *data2;
232                                 int i;
233
234                                 /* Put into the local final buffer */
235                                 for (i = 0, data1 = buf, data2 = sc->our_buf; i < SOFTMIX_DATALEN; i++, data1++, data2++)
236                                         ast_slinear_saturated_add(data1, data2);
237                                 /* Yay we have our own audio */
238                                 sc->have_audio = 1;
239                         } else {
240                                 /* Awww we don't have audio ;( */
241                                 sc->have_audio = 0;
242                         }
243                         ast_mutex_unlock(&sc->lock);
244                 }
245
246                 /* Next step go through removing the channel's own audio and creating a good frame... */
247                 AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
248                         struct softmix_channel *sc = bridge_channel->bridge_pvt;
249                         int i = 0;
250
251                         /* Copy from local final buffer to our final buffer */
252                         memcpy(sc->final_buf, buf, sizeof(sc->final_buf));
253
254                         /* If we provided audio then take it out */
255                         if (sc->have_audio) {
256                                 for (i = 0; i < SOFTMIX_DATALEN; i++) {
257                                         ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
258                                 }
259                         }
260
261                         /* The frame is now ready for use... */
262                         sc->have_frame = 1;
263
264                         /* Poke bridged channel thread just in case */
265                         pthread_kill(bridge_channel->thread, SIGURG);
266                 }
267
268                 ao2_unlock(bridge);
269
270                 /* Wait for the timing source to tell us to wake up and get things done */
271                 ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
272
273                 ast_timer_ack(timer, 1);
274
275                 ao2_lock(bridge);
276         }
277
278         return 0;
279 }
280
281 static struct ast_bridge_technology softmix_bridge = {
282         .name = "softmix",
283         .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED,
284         .preference = AST_BRIDGE_PREFERENCE_LOW,
285         .create = softmix_bridge_create,
286         .destroy = softmix_bridge_destroy,
287         .join = softmix_bridge_join,
288         .leave = softmix_bridge_leave,
289         .write = softmix_bridge_write,
290         .thread = softmix_bridge_thread,
291         .poke = softmix_bridge_poke,
292 };
293
294 static int unload_module(void)
295 {
296         ast_format_cap_destroy(softmix_bridge.format_capabilities);
297         return ast_bridge_technology_unregister(&softmix_bridge);
298 }
299
300 static int load_module(void)
301 {
302         struct ast_format tmp;
303         if (!(softmix_bridge.format_capabilities = ast_format_cap_alloc())) {
304                 return AST_MODULE_LOAD_DECLINE;
305         }
306 #ifdef SOFTMIX_16_SUPPORT
307         ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR16, 0));
308 #else
309         ast_format_cap_add(softmix_bridge.format_capabilities, ast_format_set(&tmp, AST_FORMAT_SLINEAR, 0));
310 #endif
311         return ast_bridge_technology_register(&softmix_bridge);
312 }
313
314 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");