[asterisk-commits] branch file/coremedia - r7315 in /team/file/coremedia: ./ include/asterisk/ s...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Dec 3 16:16:49 CST 2005


Author: file
Date: Sat Dec  3 16:16:45 2005
New Revision: 7315

URL: http://svn.digium.com/view/asterisk?rev=7315&view=rev
Log:
Add shims support - it also gets inherited through format changes where the translation path gets destroyed and rebuilt.

Added:
    team/file/coremedia/shims/
    team/file/coremedia/shims/Makefile
    team/file/coremedia/shims/shim_test.c
Modified:
    team/file/coremedia/Makefile
    team/file/coremedia/channel.c
    team/file/coremedia/coremedia.c
    team/file/coremedia/include/asterisk/coremedia.h

Modified: team/file/coremedia/Makefile
URL: http://svn.digium.com/view/asterisk/team/file/coremedia/Makefile?rev=7315&r1=7314&r2=7315&view=diff
==============================================================================
--- team/file/coremedia/Makefile (original)
+++ team/file/coremedia/Makefile Sat Dec  3 16:16:45 2005
@@ -334,7 +334,7 @@
 ASTCFLAGS+= $(BUSYDETECT)
 ASTCFLAGS+= $(OPTIONS)
 ASTCFLAGS+= -fomit-frame-pointer 
-SUBDIRS=res channels pbx apps codecs formats agi cdr funcs utils stdtime
+SUBDIRS=res channels pbx apps codecs formats agi cdr funcs utils stdtime shims
 
 OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
 	file.o say.o pbx.o cli.o md5.o term.o \

Modified: team/file/coremedia/channel.c
URL: http://svn.digium.com/view/asterisk/team/file/coremedia/channel.c?rev=7315&r1=7314&r2=7315&view=diff
==============================================================================
--- team/file/coremedia/channel.c (original)
+++ team/file/coremedia/channel.c Sat Dec  3 16:16:45 2005
@@ -924,10 +924,16 @@
 		ast_moh_cleanup(chan);
 
 	/* Free translatosr */
-	if (chan->readtrans)
-		ast_coremedia_translate_free(chan->readtrans);
-	if (chan->writetrans)
-		ast_coremedia_translate_free(chan->writetrans);
+	if (chan->readtrans) {
+	  ast_coremedia_translate_free(chan->readtrans);
+	  if (chan->readtrans->shims != NULL)
+	    ast_coremedia_shims_destroy(chan->readtrans->shims);
+	}
+	if (chan->writetrans) {
+	  if (chan->writetrans->shims != NULL)
+	    ast_coremedia_shims_destroy(chan->writetrans->shims);
+	  ast_coremedia_translate_free(chan->writetrans);
+	}
 	if (chan->pbx) 
 		ast_log(LOG_WARNING, "PBX may not have been terminated properly on '%s'\n", chan->name);
 	free_cid(&chan->cid);
@@ -1253,14 +1259,20 @@
 
 static void free_translation(struct ast_channel *clone)
 {
-	if (clone->writetrans)
-		ast_coremedia_translate_free(clone->writetrans);
-	if (clone->readtrans)
-		ast_coremedia_translate_free(clone->readtrans);
-	clone->writetrans = NULL;
-	clone->readtrans = NULL;
-	clone->rawwriteformat = clone->nativeformat;
-	clone->rawreadformat = clone->nativeformat;
+  if (clone->writetrans) {
+    if (clone->writetrans->shims)
+      ast_coremedia_shims_destroy(clone->writetrans->shims);
+    ast_coremedia_translate_free(clone->writetrans);
+  }
+  if (clone->readtrans) {
+    if (clone->readtrans->shims)
+      ast_coremedia_shims_destroy(clone->readtrans->shims);
+    ast_coremedia_translate_free(clone->readtrans);
+  }
+  clone->writetrans = NULL;
+  clone->readtrans = NULL;
+  clone->rawwriteformat = clone->nativeformat;
+  clone->rawreadformat = clone->nativeformat;
 }
 
 /*--- ast_hangup: Hangup a channel */
@@ -2310,6 +2322,7 @@
 
 static int set_format(struct ast_channel *chan, ast_coremedia_t *fmt, const int direction)
 {
+  struct ast_coremedia_shim *shims = NULL;
 
   /* Okay we probably need to setup a translation path */
   ast_mutex_lock(&chan->lock);
@@ -2317,10 +2330,16 @@
   case 0:
     /* Reading */
     if (chan->readtrans != NULL) {
+      /* Inherit any shims if here */
+      if (chan->readtrans->shims)
+	shims = chan->readtrans->shims;
       ast_coremedia_translate_free(chan->readtrans);
       chan->readtrans = NULL;
     }
-    if (chan->nativeformat != fmt) {
+    if (shims != NULL) {
+      chan->readtrans = ast_coremedia_translate_new(chan->nativeformat, NULL);
+      chan->readtrans->shims = shims;
+    } else if (chan->nativeformat != fmt) {
       /* Build the translation path */
       chan->readtrans = ast_coremedia_translate_new(chan->nativeformat, fmt);
     } else {
@@ -2332,10 +2351,17 @@
   case 1:
     /* Writing */
     if (chan->writetrans != NULL) {
+      /* Inherit any shims if here */
+      if (chan->writetrans->shims != NULL)
+	shims = chan->writetrans->shims;
       ast_coremedia_translate_free(chan->writetrans);
       chan->writetrans = NULL;
     }
-    if (chan->nativeformat != fmt) {
+    if (shims != NULL) {
+      /* We have shims... HAVE to go through signed linear */
+      chan->writetrans = ast_coremedia_translate_new(NULL, chan->nativeformat);
+      chan->writetrans->shims = shims;
+    } else if (chan->nativeformat != fmt) {
       /* Build the translation path */
       chan->writetrans = ast_coremedia_translate_new(fmt, chan->nativeformat);
     } else {

Modified: team/file/coremedia/coremedia.c
URL: http://svn.digium.com/view/asterisk/team/file/coremedia/coremedia.c?rev=7315&r1=7314&r2=7315&view=diff
==============================================================================
--- team/file/coremedia/coremedia.c (original)
+++ team/file/coremedia/coremedia.c Sat Dec  3 16:16:45 2005
@@ -24,6 +24,7 @@
 
 #include "asterisk.h"
 
+#include "asterisk/channel.h"
 #include "asterisk/lock.h"
 #include "asterisk/logger.h"
 #include "asterisk/cli.h"
@@ -61,6 +62,80 @@
   /* Embed signed linear passthrough into ourselves */
   ast_coremedia_register(&slinear_entry);
   return;
+}
+
+/* Create a brand new shim and attach it to a channel */
+int ast_coremedia_shimmy(struct ast_channel *channel, struct ast_coremedia_shim *shim, struct ast_coremedia_shim_pvt *pvt, int direction)
+{
+  int res = -1;
+  struct ast_coremedia_shim *new_shim = NULL;
+
+  /* Duplicate the shim for addition to the coremedia translator path */
+  new_shim = (struct ast_coremedia_shim*)malloc(sizeof(struct ast_coremedia_shim));
+  if (new_shim == NULL)
+    return -1;
+  memset(new_shim, 0, sizeof(struct ast_coremedia_shim));
+  new_shim->callback = shim->callback;
+  new_shim->destroy = shim->destroy;
+  /* Link the created pvt structure to the new shim */
+  new_shim->pvt = pvt;
+  /* Make sure we have no next */
+  new_shim->next = NULL;
+
+  switch (direction) {
+  case 0:
+    /* Make sure we go through signed linear */
+    if (channel->readtrans == NULL && ast_set_read_format_cm(channel, NULL)) {
+      ast_log(LOG_WARNING, "Failed to set read format to signed linear!\n");
+    } else {
+      /* All is fine */
+      ast_mutex_lock(&channel->lock);
+      new_shim->next = channel->readtrans->shims;
+      channel->readtrans->shims = new_shim;
+      ast_mutex_unlock(&channel->lock);
+      res = 0;
+    }
+    break;
+  case 1:
+    if (channel->writetrans == NULL && ast_set_write_format_cm(channel, NULL)) {
+      ast_log(LOG_WARNING, "Failed to set write format to signed linear!\n");
+    } else {
+      ast_mutex_lock(&channel->lock);
+      new_shim->next = channel->writetrans->shims;
+      channel->writetrans->shims = new_shim;
+      ast_mutex_unlock(&channel->lock);
+      res = 0;
+    }
+    break;
+  default:
+    break;
+  }
+
+  /* If this failed in some way - get rid of our duplicated shim */
+  if (res != 0) {
+    ast_coremedia_shims_destroy(new_shim);
+  }
+
+  return res;
+}
+
+/* Destroy any linked shims */
+int ast_coremedia_shims_destroy(struct ast_coremedia_shim *shim)
+{
+  int res = -1;
+  struct ast_coremedia_shim *current = NULL, *previous = NULL;
+
+  current = shim;
+  while (current) {
+    /* If an instance is allocated - destroy it */
+    previous = current;
+    current = current->next;
+    /* Free the used memory */
+    free(previous);
+    previous = NULL;
+  }
+
+  return res;
 }
 
 /* Create a brand new coremedia handle */
@@ -160,6 +235,9 @@
   /* Now setup the timing */
   new_path->nextin = new_path->nextout = ast_tv(0, 0);
 
+  /* Start off with no shims */
+  new_path->shims = NULL;
+
   return new_path;
 }
 
@@ -168,7 +246,7 @@
 {
   struct timeval delivery;
   struct ast_frame *frame = NULL;
-
+  struct ast_coremedia_shim *shim = NULL;
 
   /* Do our pass through the decoder if available */
   if (path->decoder != NULL) {
@@ -176,15 +254,26 @@
     frame = path->decoder->frameout(path->dec_state);
     /* Override subclass to be that of the decoder entry */
     frame->subclass = path->decoder_entry->subclass;
+    /* Get the sample rate of the decoded entry */
+  } else {
+    frame = f;
+    /* Using the subclass - get the sample rate */
+  }
+
+  /* If we are here we are absolutely signed linear - so hand off to the shims */
+  shim = path->shims;
+  while (shim) {
+    if (shim->callback) {
+      frame = shim->callback(shim->pvt, frame);
+    }
+    shim = shim->next;
   }
 
   /* Do our pass through the encoder if available */
   if (path->encoder != NULL) {
+    /* Make sure the sample rate is the same - otherwise we need to change it */
     /* Now run it through this encoder */
-    if (frame != NULL)
-      path->encoder->framein(path->enc_state, frame);
-    else /* Didn't decode - probably straight signed linear */
-      path->encoder->framein(path->enc_state, f);
+    path->encoder->framein(path->enc_state, frame);
     frame = path->encoder->frameout(path->enc_state);
     /* Override subclass to be that of the encoder entry */
     frame->subclass = path->encoder_entry->subclass;
@@ -382,6 +471,14 @@
     ast_verbose(VERBOSE_PREFIX_2 "Linked %s to coremedia handler '%s' of type '%s' with bitrate %d and sample size of %d\n", direction ? "decoder" : "encoder", cm->name, type_to_name(cm->type), cm->bitrate, cm->samples);
 
   return 0;
+}
+
+/* Link a file structure to a coremedia handler by name, bitrate, and samples */
+int ast_coremedia_link_file(char *name, int bitrate, int samples)
+{
+  int success = -1;
+
+  return success;
 }
 
 /* Link an encoder or decoder to a coremedia handler by name, bitrate, and samples */

Modified: team/file/coremedia/include/asterisk/coremedia.h
URL: http://svn.digium.com/view/asterisk/team/file/coremedia/include/asterisk/coremedia.h?rev=7315&r1=7314&r2=7315&view=diff
==============================================================================
--- team/file/coremedia/include/asterisk/coremedia.h (original)
+++ team/file/coremedia/include/asterisk/coremedia.h Sat Dec  3 16:16:45 2005
@@ -59,6 +59,10 @@
     struct ast_coremedia_entry *entry;
   };
 
+  /* coremedia file access and usage */
+  struct ast_coremedia_file {
+  };
+
   /* coremedia translator */
   struct ast_coremedia_translator {
     /*! Direction - encoder or decoder? */
@@ -103,7 +107,22 @@
     struct ast_coremedia_handle *next;
   };
 
+  /* Translation private structure */
   struct ast_translator_pvt;
+  /* Shim private structure */
+  struct ast_coremedia_shim_pvt;
+
+  /* coremedia translator shim */
+  struct ast_coremedia_shim {
+    /*! Callback for shim */
+    struct ast_frame * (*callback)(struct ast_coremedia_shim_pvt *pvt, struct ast_frame *frame);
+    /*! Destroyer of instance */
+    void (*destroy)(struct ast_coremedia_shim_pvt *pvt);
+    /*! Instance of shim */
+    struct ast_coremedia_shim_pvt *pvt;
+    /*! Next shim in list */
+    struct ast_coremedia_shim *next;
+  };
 
   /* coremedia translator path */
   struct ast_coremedia_translator_path {
@@ -117,6 +136,8 @@
     struct ast_translator_pvt *dec_state;
     /*! Encoder state */
     struct ast_translator_pvt *enc_state;
+    /*! Any shims present on this translator path */
+    struct ast_coremedia_shim *shims;
     /*! Timing is crucial... sorta */
     struct timeval nextin;
     struct timeval nextout;
@@ -126,24 +147,33 @@
 
   void ast_coremedia_init(void);
 
+  /* Shims support */
+  int ast_coremedia_shimmy(struct ast_channel *channel, struct ast_coremedia_shim *shim, struct ast_coremedia_shim_pvt *pvt, int direction);
+  int ast_coremedia_shims_destroy(struct ast_coremedia_shim *shim);
+
+  /* Handles support */
   struct ast_coremedia_handle *ast_coremedia_handle_new(struct ast_coremedia_entry *entry);
   struct ast_coremedia_handle *ast_coremedia_handle_combine(struct ast_coremedia_handle *handle_1, struct ast_coremedia_handle *handle_2);
   int ast_coremedia_handle_free(struct ast_coremedia_handle *handle);
 
+  /* Translation support */
   struct ast_coremedia_translator_path *ast_coremedia_translate_new(struct ast_coremedia_entry *decoder, struct ast_coremedia_entry *encoder);
   struct ast_frame *ast_coremedia_translate(struct ast_coremedia_translator_path *path, struct ast_frame *f, int consume);
   int ast_coremedia_translate_free(struct ast_coremedia_translator_path *path);
 
+  /* RTP mapping support */
   struct ast_coremedia_rtp *ast_coremedia_rtp_get_by_payload(int payload);
   struct ast_coremedia_rtp *ast_coremedia_rtp_get_by_subclass(int subclass);
   int ast_coremedia_rtp_register(struct ast_coremedia_rtp *rtp);
   int ast_coremedia_rtp_unregister(struct ast_coremedia_rtp *rtp);
 
+  /* Base entry support */
   struct ast_coremedia_entry *ast_coremedia_get(char *name, int bitrate, int samples);
   struct ast_coremedia_entry *ast_coremedia_get_by_format(int format);
   int ast_coremedia_register(struct ast_coremedia_entry *cm);
   int ast_coremedia_unregister(struct ast_coremedia_entry *cm);
 
+  /* Encoder/Decoder Linking */
   int ast_coremedia_link(struct ast_coremedia_entry *cm, struct ast_coremedia_translator *tl, int direction);
   int ast_coremedia_link_by_data(char *name, int bitrate, int samples, struct ast_coremedia_translator *tl, int direction);
 

Added: team/file/coremedia/shims/Makefile
URL: http://svn.digium.com/view/asterisk/team/file/coremedia/shims/Makefile?rev=7315&view=auto
==============================================================================
--- team/file/coremedia/shims/Makefile (added)
+++ team/file/coremedia/shims/Makefile Sat Dec  3 16:16:45 2005
@@ -1,0 +1,48 @@
+#
+# Asterisk -- A telephony toolkit for Linux.
+# 
+# Makefile for coremedia shims
+#
+# Copyright (C) 2005, Joshua Colp
+#
+# Joshua Colp <jcolp at asterlink.com>
+#
+# This program is free software, distributed under the terms of
+# the GNU General Public License
+#
+
+SHIMS=shim_test.so
+
+ifeq (${OSARCH},CYGWIN)
+CYGSOLINK=-Wl,--out-implib=lib$@.a -Wl,--export-all-symbols
+CYGSOLIB=-L.. -L. -lasterisk.dll
+else
+CFLAGS+=-fPIC
+endif
+
+ifeq ($(findstring BSD,${OSARCH}),BSD)
+  CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib
+endif
+
+all: $(SHIMS)
+
+clean:
+	rm -f *.so *.o .depend
+
+%.so : %.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
+
+install: all
+	for x in $(SHIMS); do $(INSTALL) -m 755 $$x $(DESTDIR)$(MODULES_DIR) ; done
+
+ifneq ($(wildcard .depend),)
+  include .depend
+endif
+
+depend: .depend
+
+.depend:
+	../build_tools/mkdep $(CFLAGS) `ls *.c`
+
+env:
+	env

Added: team/file/coremedia/shims/shim_test.c
URL: http://svn.digium.com/view/asterisk/team/file/coremedia/shims/shim_test.c?rev=7315&view=auto
==============================================================================
--- team/file/coremedia/shims/shim_test.c (added)
+++ team/file/coremedia/shims/shim_test.c Sat Dec  3 16:16:45 2005
@@ -1,0 +1,119 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2005, Joshua Colp
+ *
+ * Joshua Colp <jcolp at asterlink.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Shims test application
+ * 
+ * \ingroup applications
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "asterisk.h"
+
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+#include "asterisk/app.h"
+#include "asterisk/coremedia.h"
+
+static char *tdesc = "Shims Test Application";
+static char *app = "ShimsTest";
+static char *synopsis = 
+"Shims test application.";
+static char *descrip = "This application tests the shims system in coremedia.\n";
+
+STANDARD_LOCAL_USER;
+
+LOCAL_USER_DECL;
+
+/* Our shim callback */
+struct ast_frame *shimstest_callback(struct ast_coremedia_shim_pvt *pvt, struct ast_frame *frame);
+struct ast_frame *shimstest_callback(struct ast_coremedia_shim_pvt *pvt, struct ast_frame *frame)
+{
+  return frame;
+}
+
+static struct ast_coremedia_shim shimstest =
+  { shimstest_callback,
+    NULL,
+  };
+
+static int app_exec(struct ast_channel *chan, void *data)
+{
+	int res = 0;
+	struct localuser *u;
+
+	LOCAL_USER_ADD(u);
+
+	/* Add the shim to the channel */
+	ast_coremedia_shimmy(chan, &shimstest, NULL, 1);
+
+	LOCAL_USER_REMOVE(u);
+
+	return res;
+}
+
+int unload_module(void)
+{
+	int res;
+
+	res = ast_unregister_application(app);
+
+	STANDARD_HANGUP_LOCALUSERS;
+
+	return res;	
+}
+
+int load_module(void)
+{
+	return ast_register_application(app, app_exec, synopsis, descrip);
+}
+
+int reload(void)
+{
+	/* This function will be called if a 'reload' is requested */
+
+	return 0;
+}
+
+char *description(void)
+{
+	return tdesc;
+}
+
+int usecount(void)
+{
+	int res;
+
+	STANDARD_USECOUNT(res);
+
+	return res;
+}
+
+char *key()
+{
+	return ASTERISK_GPL_KEY;
+}



More information about the asterisk-commits mailing list