Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


bridge_softmix.c
Go to the documentation of this file.
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 /*** MODULEINFO
33  <support_level>core</support_level>
34  ***/
35 
36 #include "asterisk.h"
37 
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 379091 $")
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/time.h>
44 #include <signal.h>
45 #include <errno.h>
46 #include <unistd.h>
47 
48 #include "asterisk/module.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/bridging.h"
52 #include "asterisk/frame.h"
53 #include "asterisk/options.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/slinfactory.h"
56 #include "asterisk/astobj2.h"
57 #include "asterisk/timing.h"
58 
59 /*! \brief Interval at which mixing will take place. Valid options are 10, 20, and 40. */
60 #define SOFTMIX_INTERVAL 20
61 
62 /*! \brief Size of the buffer used for sample manipulation */
63 #define SOFTMIX_DATALEN (160 * (SOFTMIX_INTERVAL / 10))
64 
65 /*! \brief Number of samples we are dealing with */
66 #define SOFTMIX_SAMPLES (SOFTMIX_DATALEN / 2)
67 
68 /*! \brief Define used to turn on 16 kHz audio support */
69 /* #define SOFTMIX_16_SUPPORT */
70 
71 /*! \brief Structure which contains per-channel mixing information */
73  /*! Lock to protect this structure */
75  /*! Factory which contains audio read in from the channel */
77  /*! Frame that contains mixed audio to be written out to the channel */
78  struct ast_frame frame;
79  /*! Bit used to indicate that the channel provided audio for this mixing interval */
80  int have_audio:1;
81  /*! Bit used to indicate that a frame is available to be written out to the channel */
82  int have_frame:1;
83  /*! Buffer containing final mixed audio from all sources */
85  /*! Buffer containing only the audio from the channel */
87 };
88 
89 /*! \brief Function called when a bridge is created */
90 static int softmix_bridge_create(struct ast_bridge *bridge)
91 {
92  struct ast_timer *timer;
93 
94  if (!(timer = ast_timer_open())) {
95  return -1;
96  }
97 
98  bridge->bridge_pvt = timer;
99 
100  return 0;
101 }
102 
103 /*! \brief Function called when a bridge is destroyed */
104 static int softmix_bridge_destroy(struct ast_bridge *bridge)
105 {
106  if (!bridge->bridge_pvt) {
107  return -1;
108  }
109  ast_timer_close((struct ast_timer *) bridge->bridge_pvt);
110  bridge->bridge_pvt = NULL;
111  return 0;
112 }
113 
114 /*! \brief Function called when a channel is joined into the bridge */
115 static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
116 {
117  struct softmix_channel *sc = NULL;
118 
119  /* Create a new softmix_channel structure and allocate various things on it */
120  if (!(sc = ast_calloc(1, sizeof(*sc)))) {
121  return -1;
122  }
123 
124  /* Can't forget the lock */
125  ast_mutex_init(&sc->lock);
126 
127  /* Setup smoother */
129 
130  /* Setup frame parameters */
132 #ifdef SOFTMIX_16_SUPPORT
134 #else
136 #endif
137  sc->frame.data.ptr = sc->final_buf;
140 
141  /* Can't forget to record our pvt structure within the bridged channel structure */
142  bridge_channel->bridge_pvt = sc;
143 
144  return 0;
145 }
146 
147 /*! \brief Function called when a channel leaves the bridge */
148 static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
149 {
150  struct softmix_channel *sc = bridge_channel->bridge_pvt;
151 
152  if (!(bridge_channel->bridge_pvt)) {
153  return 0;
154  }
155  bridge_channel->bridge_pvt = NULL;
156 
157  /* Drop mutex lock */
158  ast_mutex_destroy(&sc->lock);
159 
160  /* Drop the factory */
162 
163  /* Eep! drop ourselves */
164  ast_free(sc);
165 
166  return 0;
167 }
168 
169 /*! \brief Function called when a channel writes a frame into the bridge */
170 static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
171 {
172  struct softmix_channel *sc = bridge_channel->bridge_pvt;
173 
174  /* Only accept audio frames, all others are unsupported */
175  if (frame->frametype != AST_FRAME_VOICE) {
177  }
178 
179  ast_mutex_lock(&sc->lock);
180 
181  /* If a frame was provided add it to the smoother */
182 #ifdef SOFTMIX_16_SUPPORT
183  if (frame->frametype == AST_FRAME_VOICE && frame->subclass.codec == AST_FORMAT_SLINEAR16) {
184 #else
185  if (frame->frametype == AST_FRAME_VOICE && frame->subclass.codec == AST_FORMAT_SLINEAR) {
186 #endif
187  ast_slinfactory_feed(&sc->factory, frame);
188  }
189 
190  /* If a frame is ready to be written out, do so */
191  if (sc->have_frame) {
192  ast_write(bridge_channel->chan, &sc->frame);
193  sc->have_frame = 0;
194  }
195 
196  /* Alllll done */
197  ast_mutex_unlock(&sc->lock);
198 
200 }
201 
202 /*! \brief Function called when the channel's thread is poked */
203 static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
204 {
205  struct softmix_channel *sc = bridge_channel->bridge_pvt;
206 
207  ast_mutex_lock(&sc->lock);
208 
209  if (sc->have_frame) {
210  ast_write(bridge_channel->chan, &sc->frame);
211  sc->have_frame = 0;
212  }
213 
214  ast_mutex_unlock(&sc->lock);
215 
216  return 0;
217 }
218 
219 /*! \brief Function which acts as the mixing thread */
220 static int softmix_bridge_thread(struct ast_bridge *bridge)
221 {
222  struct ast_timer *timer = (struct ast_timer *) bridge->bridge_pvt;
223  int timingfd = ast_timer_fd(timer);
224 
225  ast_timer_set_rate(timer, (1000 / SOFTMIX_INTERVAL));
226 
227  while (!bridge->stop && !bridge->refresh && bridge->array_num) {
228  struct ast_bridge_channel *bridge_channel = NULL;
229  short buf[SOFTMIX_DATALEN] = {0, };
230  int timeout = -1;
231 
232  /* Go through pulling audio from each factory that has it available */
233  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
234  struct softmix_channel *sc = bridge_channel->bridge_pvt;
235 
236  ast_mutex_lock(&sc->lock);
237 
238  /* Try to get audio from the factory if available */
240  short *data1, *data2;
241  int i;
242 
243  /* Put into the local final buffer */
244  for (i = 0, data1 = buf, data2 = sc->our_buf; i < SOFTMIX_DATALEN; i++, data1++, data2++)
245  ast_slinear_saturated_add(data1, data2);
246  /* Yay we have our own audio */
247  sc->have_audio = 1;
248  } else {
249  /* Awww we don't have audio ;( */
250  sc->have_audio = 0;
251  }
252  ast_mutex_unlock(&sc->lock);
253  }
254 
255  /* Next step go through removing the channel's own audio and creating a good frame... */
256  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
257  struct softmix_channel *sc = bridge_channel->bridge_pvt;
258  int i = 0;
259 
260  /* Copy from local final buffer to our final buffer */
261  memcpy(sc->final_buf, buf, sizeof(sc->final_buf));
262 
263  /* If we provided audio then take it out */
264  if (sc->have_audio) {
265  for (i = 0; i < SOFTMIX_DATALEN; i++) {
267  }
268  }
269 
270  /* The frame is now ready for use... */
271  sc->have_frame = 1;
272 
273  /* Poke bridged channel thread just in case */
274  pthread_kill(bridge_channel->thread, SIGURG);
275  }
276 
277  ao2_unlock(bridge);
278 
279  /* Wait for the timing source to tell us to wake up and get things done */
280  ast_waitfor_n_fd(&timingfd, 1, &timeout, NULL);
281 
282  if (ast_timer_ack(timer, 1) < 0) {
283  ast_log(LOG_ERROR, "Failed to acknowledge timer in softmix bridge\n");
284  ao2_lock(bridge);
285  break;
286  }
287 
288  ao2_lock(bridge);
289  }
290 
291  return 0;
292 }
293 
295  .name = "softmix",
297  .preference = AST_BRIDGE_PREFERENCE_LOW,
298 #ifdef SOFTMIX_16_SUPPORT
299  .formats = AST_FORMAT_SLINEAR16,
300 #else
301  .formats = AST_FORMAT_SLINEAR,
302 #endif
303  .create = softmix_bridge_create,
304  .destroy = softmix_bridge_destroy,
305  .join = softmix_bridge_join,
306  .leave = softmix_bridge_leave,
307  .write = softmix_bridge_write,
308  .thread = softmix_bridge_thread,
309  .poke = softmix_bridge_poke,
310 };
311 
312 static int unload_module(void)
313 {
314  return ast_bridge_technology_unregister(&softmix_bridge);
315 }
316 
317 static int load_module(void)
318 {
319  return ast_bridge_technology_register(&softmix_bridge);
320 }
321 
322 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multi-party software based channel mixing");
union ast_frame_subclass subclass
Definition: frame.h:146
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
pthread_t thread
Definition: bridging.h:133
static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Function called when a channel leaves the bridge.
static int softmix_bridge_create(struct ast_bridge *bridge)
Function called when a bridge is created.
void * ptr
Definition: frame.h:160
#define SOFTMIX_DATALEN
Size of the buffer used for sample manipulation.
static int softmix_bridge_destroy(struct ast_bridge *bridge)
Function called when a bridge is destroyed.
static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Function called when the channel&#39;s thread is poked.
int ast_slinfactory_feed(struct ast_slinfactory *sf, struct ast_frame *f)
Feed audio into a slinfactory.
Definition: slinfactory.c:77
void ast_timer_close(struct ast_timer *handle)
Close an opened timing handle.
Definition: timing.c:150
struct ast_timer * ast_timer_open(void)
Open a timer.
Definition: timing.c:123
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ao2_unlock(a)
Definition: astobj2.h:497
#define ast_bridge_technology_register(technology)
See __ast_bridge_technology_register()
format_t codec
Definition: frame.h:137
unsigned int ast_slinfactory_available(const struct ast_slinfactory *sf)
Retrieve number of samples currently in a slinfactory.
Definition: slinfactory.c:196
size_t array_num
Definition: bridging.h:171
int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
Waits for input on an fd.
Definition: channel.c:3176
unsigned int refresh
Definition: bridging.h:157
General Asterisk PBX channel definitions.
static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
Function called when a channel writes a frame into the bridge.
static int softmix_bridge_thread(struct ast_bridge *bridge)
Function which acts as the mixing thread.
Asterisk internal frame definitions.
Channel Bridging API.
Channel Bridging API.
int datalen
Definition: frame.h:148
#define ao2_lock(a)
Definition: astobj2.h:488
static force_inline void ast_slinear_saturated_add(short *input, short *value)
Definition: utils.h:312
unsigned int stop
Definition: bridging.h:155
#define AST_FORMAT_SLINEAR16
Definition: frame.h:272
int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
Unregister a bridge technology from use.
Definition: bridging.c:99
void ast_slinfactory_destroy(struct ast_slinfactory *sf)
Destroy the contents of a slinfactory.
Definition: slinfactory.c:64
#define SOFTMIX_SAMPLES
Number of samples we are dealing with.
static int unload_module(void)
Structure that contains information about a bridge.
Definition: bridging.h:149
int ast_timer_ack(const struct ast_timer *handle, unsigned int quantity)
Acknowledge a timer event.
Definition: timing.c:172
#define LOG_ERROR
Definition: logger.h:155
static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Function called when a channel is joined into the bridge.
Define used to turn on 16 kHz audio support.
struct ast_frame frame
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
struct ast_slinfactory factory
A machine to gather up arbitrary frames and convert them to raw slinear on demand.
#define ast_free(a)
Definition: astmm.h:97
static struct ast_bridge_technology softmix_bridge
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:4916
void ast_slinfactory_init(struct ast_slinfactory *sf)
Initialize a slinfactory.
Definition: slinfactory.c:39
int ast_timer_fd(const struct ast_timer *handle)
Get a poll()-able file descriptor for a timer.
Definition: timing.c:158
int ast_timer_set_rate(const struct ast_timer *handle, unsigned int rate)
Set the timing tick rate.
Definition: timing.c:163
Support for logging to various files, console and syslog Configuration in file logger.conf.
short our_buf[SOFTMIX_DATALEN]
struct ast_channel * chan
Definition: bridging.h:125
Structure that contains information regarding a channel in a bridge.
Definition: bridging.h:117
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
#define ast_calloc(a, b)
Definition: astmm.h:82
Structure that is the essence of a bridge technology.
#define SOFTMIX_INTERVAL
Interval at which mixing will take place. Valid options are 10, 20, and 40.
ast_bridge_write_result
Return values for bridge technology write function.
Definition: bridging.h:102
Data structure associated with a single frame of data.
Definition: frame.h:142
int ast_slinfactory_read(struct ast_slinfactory *sf, short *buf, size_t samples)
Read samples from a slinfactory.
Definition: slinfactory.c:142
Options provided by main asterisk program.
static force_inline void ast_slinear_saturated_subtract(short *input, short *value)
Definition: utils.h:325
short final_buf[SOFTMIX_DATALEN]
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_mutex_init(pmutex)
Definition: lock.h:152
#define ast_mutex_destroy(a)
Definition: lock.h:154
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
static struct ast_timer * timer
Definition: chan_iax2.c:313
Asterisk module definitions.
static int load_module(void)
union ast_frame::@172 data
ast_mutex_t lock
void * bridge_pvt
Definition: bridging.h:163
Timing source management.
Structure for mutex and tracking information.
Definition: lock.h:121
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
#define ast_mutex_unlock(a)
Definition: lock.h:156