[svn-commits] file: branch file/bridging r81328 - /team/file/bridging/bridges/bridge_softmix.c
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Tue Aug 28 21:26:39 CDT 2007
Author: file
Date: Tue Aug 28 21:26:38 2007
New Revision: 81328
URL: http://svn.digium.com/view/asterisk?view=rev&rev=81328
Log:
Add working software based mixing bridge technology.
Modified:
team/file/bridging/bridges/bridge_softmix.c
Modified: team/file/bridging/bridges/bridge_softmix.c
URL: http://svn.digium.com/view/asterisk/team/file/bridging/bridges/bridge_softmix.c?view=diff&rev=81328&r1=81327&r2=81328
==============================================================================
--- team/file/bridging/bridges/bridge_softmix.c (original)
+++ team/file/bridging/bridges/bridge_softmix.c Tue Aug 28 21:26:38 2007
@@ -32,51 +32,56 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <sys/time.h>
+#include <signal.h>
+#include <errno.h>
+#include <unistd.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/bridging.h"
#include "asterisk/frame.h"
-#include "asterisk/slinfactory.h"
#include "asterisk/options.h"
#include "asterisk/logger.h"
-
-#define SOFTMIX_SAMPLES 160
-
-struct softmix_bridge_pvt {
- struct ast_slinfactory *factory;
+#include "asterisk/slinfactory.h"
+
+#define SOFTMIX_INTERVAL 20 /* Valid options are 10, 20, and 30 */
+#define SOFTMIX_SAMPLES (160*(SOFTMIX_INTERVAL/10))
+
+struct softmix_channel {
+ ast_mutex_t lock;
+ struct ast_slinfactory factory;
+ struct ast_frame frame;
+ int have_audio:1;
+ int have_frame:1;
+ short final_buf[SOFTMIX_SAMPLES];
+ short our_buf[SOFTMIX_SAMPLES];
};
-
-struct softmix_bridge_channel_pvt {
- struct ast_slinfactory *factory;
- struct ast_frame *frame;
-};
-
-/* Called when the bridge is created */
-static int softmix_bridge_create(struct ast_bridge *bridge)
-{
- return 0;
-}
-
-/* Called when the bridge is destroyed */
-static int softmix_bridge_destroy(struct ast_bridge *bridge)
-{
- return 0;
-}
/* Called when a channel is joined to the bridge. We need to setup a smoother so that frames from all channels will be consistent */
static int softmix_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
- struct softmix_bridge_channel_pvt *sbcp = NULL;
-
- /* Allocate new memory for the bridged channel private structure */
- if (!(sbcp = ast_calloc(1, sizeof(*sbcp))))
+ struct softmix_channel *sc = NULL;
+
+ /* Create a new softmix_channel structure and allocate various things on it */
+ if (!(sc = ast_calloc(1, sizeof(*sc))))
return -1;
- /* Associate our private structure with the bridged channel, it would be rather useless without it */
- bridge_channel->bridge_pvt = sbcp;
+ /* Can't forget the lock */
+ ast_mutex_init(&sc->lock);
+
+ /* Setup smoother */
+ ast_slinfactory_init(&sc->factory);
+
+ /* Setup frame parameters */
+ sc->frame.frametype = AST_FRAME_VOICE;
+ sc->frame.subclass = AST_FORMAT_SLINEAR;
+ sc->frame.data = sc->final_buf;
+ sc->frame.datalen = sizeof(sc->final_buf);
+ sc->frame.samples = SOFTMIX_SAMPLES;
+
+ /* Can't forget to record our pvt structure within the bridged channel structure */
+ bridge_channel->bridge_pvt = sc;
return 0;
}
@@ -84,25 +89,165 @@
/* Called when a channel leaves the bridge */
static int softmix_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ /* Drop mutex lock */
+ ast_mutex_destroy(&sc->lock);
+
+ /* Drop the factory */
+ ast_slinfactory_destroy(&sc->factory);
+
+ /* Eep! drop ourselves */
+ ast_free(sc);
+
return 0;
}
/* Called when a frame comes from a channel and should go into the bridge */
static enum ast_bridge_write_result softmix_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
- return AST_BRIDGE_WRITE_FAILED;
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ /* Only accept audio frames, all others are unsupported */
+ if (frame->frametype != AST_FRAME_VOICE)
+ return AST_BRIDGE_WRITE_UNSUPPORTED;
+
+ ast_mutex_lock(&sc->lock);
+
+ /* If a frame was provided add it to the smoother */
+ if (frame->frametype == AST_FRAME_VOICE && frame->subclass == AST_FORMAT_SLINEAR)
+ ast_slinfactory_feed(&sc->factory, frame);
+
+ /* If a frame is ready to be written out, do so */
+ if (sc->have_frame) {
+ ast_write(bridge_channel->chan, &sc->frame);
+ sc->have_frame = 0;
+ }
+
+ /* Alllll done */
+ ast_mutex_unlock(&sc->lock);
+
+ return AST_BRIDGE_WRITE_SUCCESS;
+}
+
+/* Called when a bridged channel thread is interrupted */
+static int softmix_bridge_poke(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
+{
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ ast_mutex_lock(&sc->lock);
+
+ if (sc->have_frame) {
+ ast_write(bridge_channel->chan, &sc->frame);
+ sc->have_frame = 0;
+ }
+
+ ast_mutex_unlock(&sc->lock);
+
+ return 0;
+}
+
+/* Main bridge thread function */
+static int softmix_bridge_thread(struct ast_bridge *bridge)
+{
+ struct timeval base = ast_tvnow();
+ int skew = 0, previous_skew = 0, last_adjust = 0;
+ ast_cond_t cond;
+
+ ast_cond_init(&cond, NULL);
+
+ while (bridge->thread != AST_PTHREADT_STOP) {
+ struct ast_bridge_channel *bridge_channel = NULL;
+ short buf[SOFTMIX_SAMPLES] = {0, };
+ struct timeval next_wakeup;
+ struct timespec ts = {0, };
+
+ /* Go through pulling audio from each factory that has it available */
+ AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, list) {
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+
+ ast_mutex_lock(&sc->lock);
+
+ /* Try to get audio from the factory if available */
+ if (ast_slinfactory_available(&sc->factory) >= SOFTMIX_SAMPLES && ast_slinfactory_read(&sc->factory, sc->our_buf, SOFTMIX_SAMPLES)) {
+ short *data1, *data2;
+ int i;
+
+ /* Put into the local final buffer */
+ for (i = 0, data1 = buf, data2 = sc->our_buf; i < SOFTMIX_SAMPLES; i++, data1++, data2++)
+ ast_slinear_saturated_add(data1, data2);
+ /* Yay we have our own audio */
+ sc->have_audio = 1;
+ } else {
+ /* Awww we don't have audio ;( */
+ sc->have_audio = 0;
+ }
+ ast_mutex_unlock(&sc->lock);
+ }
+
+ /* Next step go through removing the channel's own audio and creating a good frame... */
+ AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, list) {
+ struct softmix_channel *sc = bridge_channel->bridge_pvt;
+ int i = 0;
+
+ /* Copy from local final buffer to our final buffer while subtracting our audio if present */
+ for (i = 0; i < SOFTMIX_SAMPLES; i++) {
+ sc->final_buf[i] = buf[i];
+ /* Subtract our own audio from the end frame if present */
+ if (sc->have_audio)
+ ast_slinear_saturated_subtract(&sc->final_buf[i], &sc->our_buf[i]);
+ }
+
+ /* The frame is now ready for use... */
+ sc->have_frame = 1;
+
+ /* Poke bridged channel thread just in case */
+ pthread_kill(bridge_channel->thread, SIGURG);
+ }
+
+ /* If there is a skew value incorporate it into the base time to adjust for timing */
+ if (previous_skew && skew) {
+ int adjust = abs(skew-previous_skew), new_adjust = 0;
+ if (adjust > 0)
+ new_adjust = abs(adjust-last_adjust);
+ base = ast_tvadd(base, ast_tv(0, ((SOFTMIX_INTERVAL-new_adjust)+SOFTMIX_INTERVAL)*1000));
+ last_adjust = new_adjust;
+ } else {
+ base = ast_tvadd(base, ast_tv(0, (SOFTMIX_INTERVAL*2)*1000));
+ }
+
+ /* Calculate next time until we should wake up */
+ next_wakeup = ast_tvadd(ast_tvnow(), ast_tvsub(base, ast_tvnow()));
+
+ /* Apply this time to the timespec */
+ ts.tv_sec = next_wakeup.tv_sec;
+ ts.tv_nsec = next_wakeup.tv_usec * 1000;
+
+ /* Wait until then */
+ ast_cond_timedwait(&cond, &bridge->lock, &ts);
+
+ /* Before we calculate the skew value... record the previous one */
+ previous_skew = skew;
+
+ /* Calculate a skew value (what we wanted versus what we got) */
+ skew = ast_tvdiff_ms(ast_tvnow(), next_wakeup);
+ }
+
+ ast_cond_destroy(&cond);
+
+ return 0;
}
static struct ast_bridge_technology softmix_bridge = {
.name = "softmix",
- .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX,
+ .capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX | AST_BRIDGE_CAPABILITY_THREAD | AST_BRIDGE_CAPABILITY_MULTITHREADED,
.preference = AST_BRIDGE_PREFERENCE_LOW,
.formats = AST_FORMAT_SLINEAR,
- .create = softmix_bridge_create,
- .destroy = softmix_bridge_destroy,
.join = softmix_bridge_join,
.leave = softmix_bridge_leave,
.write = softmix_bridge_write,
+ .thread = softmix_bridge_thread,
+ .poke = softmix_bridge_poke,
};
static int unload_module(void)
More information about the svn-commits
mailing list