2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, 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 Two channel bridging module which groups bridges into batches of threads
23 * \author Joshua Colp <jcolp@digium.com>
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <sys/types.h>
39 #include "asterisk/module.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/bridging.h"
42 #include "asterisk/bridging_technology.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/astobj2.h"
46 /*! \brief Number of buckets our multiplexed thread container can have */
47 #define MULTIPLEXED_BUCKETS 53
49 /*! \brief Number of channels we handle in a single thread */
50 #define MULTIPLEXED_MAX_CHANNELS 8
52 /*! \brief Structure which represents a single thread handling multiple 2 channel bridges */
53 struct multiplexed_thread {
56 /*! Pipe used to wake up the multiplexed thread */
58 /*! Channels in this thread */
59 struct ast_channel *chans[MULTIPLEXED_MAX_CHANNELS];
60 /*! Number of channels in this thread */
62 /*! Bit used to indicate that the thread is waiting on channels */
63 unsigned int waiting:1;
64 /*! Number of channels actually being serviced by this thread */
65 unsigned int service_count;
68 /*! \brief Container of all operating multiplexed threads */
69 static struct ao2_container *multiplexed_threads;
71 /*! \brief Callback function for finding a free multiplexed thread */
72 static int find_multiplexed_thread(void *obj, void *arg, int flags)
74 struct multiplexed_thread *multiplexed_thread = obj;
75 return (multiplexed_thread->count <= (MULTIPLEXED_MAX_CHANNELS - 2)) ? CMP_MATCH | CMP_STOP : 0;
78 /*! \brief Destroy callback for a multiplexed thread structure */
79 static void destroy_multiplexed_thread(void *obj)
81 struct multiplexed_thread *multiplexed_thread = obj;
83 if (multiplexed_thread->pipe[0] > -1) {
84 close(multiplexed_thread->pipe[0]);
86 if (multiplexed_thread->pipe[1] > -1) {
87 close(multiplexed_thread->pipe[1]);
93 /*! \brief Create function which finds/reserves/references a multiplexed thread structure */
94 static int multiplexed_bridge_create(struct ast_bridge *bridge)
96 struct multiplexed_thread *multiplexed_thread;
98 ao2_lock(multiplexed_threads);
100 /* Try to find an existing thread to handle our additional channels */
101 if (!(multiplexed_thread = ao2_callback(multiplexed_threads, 0, find_multiplexed_thread, NULL))) {
104 /* If we failed we will have to create a new one from scratch */
105 if (!(multiplexed_thread = ao2_alloc(sizeof(*multiplexed_thread), destroy_multiplexed_thread))) {
106 ast_debug(1, "Failed to find or create a new multiplexed thread for bridge '%p'\n", bridge);
107 ao2_unlock(multiplexed_threads);
111 multiplexed_thread->pipe[0] = multiplexed_thread->pipe[1] = -1;
112 /* Setup a pipe so we can poke the thread itself when needed */
113 if (pipe(multiplexed_thread->pipe)) {
114 ast_debug(1, "Failed to create a pipe for poking a multiplexed thread for bridge '%p'\n", bridge);
115 ao2_ref(multiplexed_thread, -1);
116 ao2_unlock(multiplexed_threads);
120 /* Setup each pipe for non-blocking operation */
121 flags = fcntl(multiplexed_thread->pipe[0], F_GETFL);
122 if (fcntl(multiplexed_thread->pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
123 ast_log(LOG_WARNING, "Failed to setup first nudge pipe for non-blocking operation on %p (%d: %s)\n", bridge, errno, strerror(errno));
124 ao2_ref(multiplexed_thread, -1);
125 ao2_unlock(multiplexed_threads);
128 flags = fcntl(multiplexed_thread->pipe[1], F_GETFL);
129 if (fcntl(multiplexed_thread->pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
130 ast_log(LOG_WARNING, "Failed to setup second nudge pipe for non-blocking operation on %p (%d: %s)\n", bridge, errno, strerror(errno));
131 ao2_ref(multiplexed_thread, -1);
132 ao2_unlock(multiplexed_threads);
136 /* Set up default parameters */
137 multiplexed_thread->thread = AST_PTHREADT_NULL;
139 /* Finally link us into the container so others may find us */
140 ao2_link(multiplexed_threads, multiplexed_thread);
141 ast_debug(1, "Created multiplexed thread '%p' for bridge '%p'\n", multiplexed_thread, bridge);
143 ast_debug(1, "Found multiplexed thread '%p' for bridge '%p'\n", multiplexed_thread, bridge);
146 /* Bump the count of the thread structure up by two since the channels for this bridge will be joining shortly */
147 multiplexed_thread->count += 2;
149 ao2_unlock(multiplexed_threads);
151 bridge->bridge_pvt = multiplexed_thread;
156 /*! \brief Internal function which nudges the thread */
157 static void multiplexed_nudge(struct multiplexed_thread *multiplexed_thread)
161 if (multiplexed_thread->thread == AST_PTHREADT_NULL) {
165 if (write(multiplexed_thread->pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
166 ast_log(LOG_ERROR, "We couldn't poke multiplexed thread '%p'... something is VERY wrong\n", multiplexed_thread);
169 while (multiplexed_thread->waiting) {
176 /*! \brief Destroy function which unreserves/unreferences/removes a multiplexed thread structure */
177 static int multiplexed_bridge_destroy(struct ast_bridge *bridge)
179 struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
181 ao2_lock(multiplexed_threads);
183 multiplexed_thread->count -= 2;
185 if (!multiplexed_thread->count) {
186 ast_debug(1, "Unlinking multiplexed thread '%p' since nobody is using it anymore\n", multiplexed_thread);
187 ao2_unlink(multiplexed_threads, multiplexed_thread);
190 multiplexed_nudge(multiplexed_thread);
192 ao2_unlock(multiplexed_threads);
194 ao2_ref(multiplexed_thread, -1);
199 /*! \brief Thread function that executes for multiplexed threads */
200 static void *multiplexed_thread_function(void *data)
202 struct multiplexed_thread *multiplexed_thread = data;
203 int fds = multiplexed_thread->pipe[0];
205 ao2_lock(multiplexed_thread);
207 ast_debug(1, "Starting actual thread for multiplexed thread '%p'\n", multiplexed_thread);
209 while (multiplexed_thread->thread != AST_PTHREADT_STOP) {
210 struct ast_channel *winner = NULL, *first = multiplexed_thread->chans[0];
211 int to = -1, outfd = -1;
213 /* Move channels around so not just the first one gets priority */
214 memmove(multiplexed_thread->chans, multiplexed_thread->chans + 1, sizeof(struct ast_channel *) * (multiplexed_thread->service_count - 1));
215 multiplexed_thread->chans[multiplexed_thread->service_count - 1] = first;
217 multiplexed_thread->waiting = 1;
218 ao2_unlock(multiplexed_thread);
219 winner = ast_waitfor_nandfds(multiplexed_thread->chans, multiplexed_thread->service_count, &fds, 1, NULL, &outfd, &to);
220 multiplexed_thread->waiting = 0;
221 ao2_lock(multiplexed_thread);
222 if (multiplexed_thread->thread == AST_PTHREADT_STOP) {
229 if (read(multiplexed_thread->pipe[0], &nudge, sizeof(nudge)) < 0) {
230 if (errno != EINTR && errno != EAGAIN) {
231 ast_log(LOG_WARNING, "read() failed for pipe on multiplexed thread '%p': %s\n", multiplexed_thread, strerror(errno));
235 if (winner && winner->bridge) {
236 struct ast_bridge *bridge = winner->bridge;
238 ao2_unlock(multiplexed_thread);
239 while ((bridge = winner->bridge) && ao2_trylock(bridge)) {
241 if (multiplexed_thread->thread == AST_PTHREADT_STOP) {
246 if (!stop && bridge) {
247 ast_bridge_handle_trip(bridge, NULL, winner, -1);
250 ao2_lock(multiplexed_thread);
254 multiplexed_thread->thread = AST_PTHREADT_NULL;
256 ast_debug(1, "Stopping actual thread for multiplexed thread '%p'\n", multiplexed_thread);
258 ao2_unlock(multiplexed_thread);
259 ao2_ref(multiplexed_thread, -1);
264 /*! \brief Helper function which adds or removes a channel and nudges the thread */
265 static void multiplexed_add_or_remove(struct multiplexed_thread *multiplexed_thread, struct ast_channel *chan, int add)
268 pthread_t thread = AST_PTHREADT_NULL;
270 ao2_lock(multiplexed_thread);
272 multiplexed_nudge(multiplexed_thread);
274 for (i = 0; i < MULTIPLEXED_MAX_CHANNELS; i++) {
275 if (multiplexed_thread->chans[i] == chan) {
277 multiplexed_thread->chans[i] = NULL;
278 multiplexed_thread->service_count--;
282 } else if (!multiplexed_thread->chans[i] && add) {
283 multiplexed_thread->chans[i] = chan;
284 multiplexed_thread->service_count++;
289 if (multiplexed_thread->service_count && multiplexed_thread->thread == AST_PTHREADT_NULL) {
290 ao2_ref(multiplexed_thread, +1);
291 if (ast_pthread_create(&multiplexed_thread->thread, NULL, multiplexed_thread_function, multiplexed_thread)) {
292 ao2_ref(multiplexed_thread, -1);
293 ast_debug(1, "Failed to create an actual thread for multiplexed thread '%p', trying next time\n", multiplexed_thread);
295 } else if (!multiplexed_thread->service_count && multiplexed_thread->thread != AST_PTHREADT_NULL) {
296 thread = multiplexed_thread->thread;
297 multiplexed_thread->thread = AST_PTHREADT_STOP;
298 } else if (!add && removed) {
299 memmove(multiplexed_thread->chans + i, multiplexed_thread->chans + i + 1, sizeof(struct ast_channel *) * (MULTIPLEXED_MAX_CHANNELS - (i + 1)));
302 ao2_unlock(multiplexed_thread);
304 if (thread != AST_PTHREADT_NULL) {
305 pthread_join(thread, NULL);
311 /*! \brief Join function which actually adds the channel into the array to be monitored */
312 static int multiplexed_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
314 struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan, *c1 = AST_LIST_LAST(&bridge->channels)->chan;
315 struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
317 ast_debug(1, "Adding channel '%s' to multiplexed thread '%p' for monitoring\n", bridge_channel->chan->name, multiplexed_thread);
319 multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 1);
321 /* If the second channel has not yet joined do not make things compatible */
326 if ((ast_format_cmp(&c0->writeformat, &c1->readformat) == AST_FORMAT_CMP_EQUAL) &&
327 (ast_format_cmp(&c0->readformat, &c1->writeformat) == AST_FORMAT_CMP_EQUAL) &&
328 (ast_format_cap_identical(c0->nativeformats, c1->nativeformats))) {
332 return ast_channel_make_compatible(c0, c1);
335 /*! \brief Leave function which actually removes the channel from the array */
336 static int multiplexed_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
338 struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
340 ast_debug(1, "Removing channel '%s' from multiplexed thread '%p'\n", bridge_channel->chan->name, multiplexed_thread);
342 multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 0);
347 /*! \brief Suspend function which means control of the channel is going elsewhere */
348 static void multiplexed_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
350 struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
352 ast_debug(1, "Suspending channel '%s' from multiplexed thread '%p'\n", bridge_channel->chan->name, multiplexed_thread);
354 multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 0);
359 /*! \brief Unsuspend function which means control of the channel is coming back to us */
360 static void multiplexed_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
362 struct multiplexed_thread *multiplexed_thread = bridge->bridge_pvt;
364 ast_debug(1, "Unsuspending channel '%s' from multiplexed thread '%p'\n", bridge_channel->chan->name, multiplexed_thread);
366 multiplexed_add_or_remove(multiplexed_thread, bridge_channel->chan, 1);
371 /*! \brief Write function for writing frames into the bridge */
372 static enum ast_bridge_write_result multiplexed_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
374 struct ast_bridge_channel *other;
376 if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
377 return AST_BRIDGE_WRITE_FAILED;
380 if (!(other = (AST_LIST_FIRST(&bridge->channels) == bridge_channel ? AST_LIST_LAST(&bridge->channels) : AST_LIST_FIRST(&bridge->channels)))) {
381 return AST_BRIDGE_WRITE_FAILED;
384 if (other->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
385 ast_write(other->chan, frame);
388 return AST_BRIDGE_WRITE_SUCCESS;
391 static struct ast_bridge_technology multiplexed_bridge = {
392 .name = "multiplexed_bridge",
393 .capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX,
394 .preference = AST_BRIDGE_PREFERENCE_HIGH,
395 .create = multiplexed_bridge_create,
396 .destroy = multiplexed_bridge_destroy,
397 .join = multiplexed_bridge_join,
398 .leave = multiplexed_bridge_leave,
399 .suspend = multiplexed_bridge_suspend,
400 .unsuspend = multiplexed_bridge_unsuspend,
401 .write = multiplexed_bridge_write,
404 static int unload_module(void)
406 int res = ast_bridge_technology_unregister(&multiplexed_bridge);
408 ao2_ref(multiplexed_threads, -1);
409 multiplexed_bridge.format_capabilities = ast_format_cap_destroy(multiplexed_bridge.format_capabilities);
414 static int load_module(void)
416 if (!(multiplexed_threads = ao2_container_alloc(MULTIPLEXED_BUCKETS, NULL, NULL))) {
417 return AST_MODULE_LOAD_DECLINE;
419 if (!(multiplexed_bridge.format_capabilities = ast_format_cap_alloc())) {
420 return AST_MODULE_LOAD_DECLINE;
422 ast_format_cap_add_all_by_type(multiplexed_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
423 ast_format_cap_add_all_by_type(multiplexed_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
424 ast_format_cap_add_all_by_type(multiplexed_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
425 return ast_bridge_technology_register(&multiplexed_bridge);
428 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Multiplexed two channel bridging module");