Merge "resource_channels.c: add hangup reason "answered_elsewhere"."
[asterisk/asterisk.git] / codecs / codec_gsm.c
old mode 100755 (executable)
new mode 100644 (file)
index f0efe1a..f80c955
 /*
- * Asterisk -- A telephony toolkit for Linux.
- *
- * Translate between signed linear and Global System for Mobile Communications (GSM)
+ * Asterisk -- An open source telephony toolkit.
  *
  * The GSM code is from TOAST.  Copyright information for that package is available
- * in  the GSM directory.
- * 
- * Copyright (C) 1999, Mark Spencer
+ * in the GSM directory.
+ *
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.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
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
  */
 
-#define TYPE_SILENCE    0x2
-#define TYPE_HIGH       0x0
-#define TYPE_LOW        0x1
-#define TYPE_MASK       0x3
-
-#include <asterisk/lock.h>
-#include <asterisk/translate.h>
-#include <asterisk/module.h>
-#include <asterisk/logger.h>
-#include <asterisk/channel.h>
-#include <pthread.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <netinet/in.h>
-#include <string.h>
-#include <stdio.h>
-
-#include "gsm/inc/gsm.h"
-#include "../formats/msgsm.h"
+/*! \file
+ *
+ * \brief Translate between signed linear and Global System for Mobile Communications (GSM)
+ *
+ * \ingroup codecs
+ */
 
-/* Sample frame data */
-#include "slin_gsm_ex.h"
-#include "gsm_slin_ex.h"
+/*** MODULEINFO
+       <depend>gsm</depend>
+       <support_level>core</support_level>
+ ***/
 
-static ast_mutex_t localuser_lock = AST_MUTEX_INITIALIZER;
-static int localusecnt=0;
+#include "asterisk.h"
 
-static char *tdesc = "GSM/PCM16 (signed linear) Codec Translator";
+ASTERISK_REGISTER_FILE()
 
-struct ast_translator_pvt {
-       gsm gsm;
-       struct ast_frame f;
-       /* Space to build offset */
-       char offset[AST_FRIENDLY_OFFSET];
-       /* Buffer for our outgoing frame */
-       short outbuf[8000];
-       /* Enough to store a full second */
-       short buf[8000];
-       int tail;
-};
+#include "asterisk/translate.h"
+#include "asterisk/config.h"
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
 
-#define gsm_coder_pvt ast_translator_pvt
+#ifdef HAVE_GSM_HEADER
+#include "gsm.h"
+#elif defined(HAVE_GSM_GSM_HEADER)
+#include <gsm/gsm.h>
+#endif
 
-static struct ast_translator_pvt *gsm_new(void)
-{
-       struct gsm_coder_pvt *tmp;
-       tmp = malloc(sizeof(struct gsm_coder_pvt));
-       if (tmp) {
-               if (!(tmp->gsm = gsm_create())) {
-                       free(tmp);
-                       tmp = NULL;
-               }
-               tmp->tail = 0;
-               localusecnt++;
-       }
-       return tmp;
-}
+#include "../formats/msgsm.h"
 
-static struct ast_frame *lintogsm_sample(void)
-{
-       static struct ast_frame f;
-       f.frametype = AST_FRAME_VOICE;
-       f.subclass = AST_FORMAT_SLINEAR;
-       f.datalen = sizeof(slin_gsm_ex);
-       /* Assume 8000 Hz */
-       f.samples = sizeof(slin_gsm_ex)/2;
-       f.mallocd = 0;
-       f.offset = 0;
-       f.src = __PRETTY_FUNCTION__;
-       f.data = slin_gsm_ex;
-       return &f;
-}
+#define BUFFER_SAMPLES 8000
+#define GSM_SAMPLES    160
+#define        GSM_FRAME_LEN   33
+#define        MSGSM_FRAME_LEN 65
 
-static struct ast_frame *gsmtolin_sample(void)
-{
-       static struct ast_frame f;
-       f.frametype = AST_FRAME_VOICE;
-       f.subclass = AST_FORMAT_GSM;
-       f.datalen = sizeof(gsm_slin_ex);
-       /* All frames are 20 ms long */
-       f.samples = 160;
-       f.mallocd = 0;
-       f.offset = 0;
-       f.src = __PRETTY_FUNCTION__;
-       f.data = gsm_slin_ex;
-       return &f;
-}
+/* Sample frame data */
+#include "asterisk/slin.h"
+#include "ex_gsm.h"
 
-static struct ast_frame *gsmtolin_frameout(struct ast_translator_pvt *tmp)
+struct gsm_translator_pvt {    /* both gsm2lin and lin2gsm */
+       gsm gsm;
+       int16_t buf[BUFFER_SAMPLES];    /* lin2gsm, temporary storage */
+};
+
+static int gsm_new(struct ast_trans_pvt *pvt)
 {
-       if (!tmp->tail)
-               return NULL;
-       /* Signed linear is no particular frame size, so just send whatever
-          we have in the buffer in one lump sum */
-       tmp->f.frametype = AST_FRAME_VOICE;
-       tmp->f.subclass = AST_FORMAT_SLINEAR;
-       tmp->f.datalen = tmp->tail * 2;
-       /* Assume 8000 Hz */
-       tmp->f.samples = tmp->tail;
-       tmp->f.mallocd = 0;
-       tmp->f.offset = AST_FRIENDLY_OFFSET;
-       tmp->f.src = __PRETTY_FUNCTION__;
-       tmp->f.data = tmp->buf;
-       /* Reset tail pointer */
-       tmp->tail = 0;
-
-       return &tmp->f; 
+       struct gsm_translator_pvt *tmp = pvt->pvt;
+       
+       return (tmp->gsm = gsm_create()) ? 0 : -1;
 }
 
-static int gsmtolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
+/*! \brief decode and store in outbuf. */
+static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-       /* Assuming there's space left, decode into the current buffer at
-          the tail location.  Read in as many frames as there are */
+       struct gsm_translator_pvt *tmp = pvt->pvt;
        int x;
-       unsigned char data[66];
-       int msgsm=0;
-       
-       if ((f->datalen % 33) && (f->datalen % 65)) {
-               ast_log(LOG_WARNING, "Huh?  A GSM frame that isn't a multiple of 33 or 65 bytes long from %s (%d)?\n", f->src, f->datalen);
-               return -1;
-       }
-       
-       if (f->datalen % 65 == 0) 
-               msgsm = 1;
-               
-       for (x=0;x<f->datalen;x+=(msgsm ? 65 : 33)) {
-               if (msgsm) {
+       int16_t *dst = pvt->outbuf.i16;
+       /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
+       int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
+               MSGSM_FRAME_LEN : GSM_FRAME_LEN;
+
+       for (x=0; x < f->datalen; x += flen) {
+               unsigned char data[2 * GSM_FRAME_LEN];
+               unsigned char *src;
+               int len;
+               if (flen == MSGSM_FRAME_LEN) {
+                       len = 2*GSM_SAMPLES;
+                       src = data;
                        /* Translate MSGSM format to Real GSM format before feeding in */
-                       conv65(f->data + x, data);
-                       if (tmp->tail + 320 < sizeof(tmp->buf)/2) {     
-                               if (gsm_decode(tmp->gsm, data, tmp->buf + tmp->tail)) {
-                                       ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
-                                       return -1;
-                               }
-                               tmp->tail+=160;
-                               if (gsm_decode(tmp->gsm, data + 33, tmp->buf + tmp->tail)) {
-                                       ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
-                                       return -1;
-                               }
-                               tmp->tail+=160;
-                       } else {
-                               ast_log(LOG_WARNING, "Out of (MS) buffer space\n");
-                               return -1;
-                       }
+                       /* XXX what's the point here! we should just work
+                        * on the full format.
+                        */
+                       conv65(f->data.ptr + x, data);
                } else {
-                       if (tmp->tail + 160 < sizeof(tmp->buf)/2) {     
-                               if (gsm_decode(tmp->gsm, f->data + x, tmp->buf + tmp->tail)) {
-                                       ast_log(LOG_WARNING, "Invalid GSM data\n");
-                                       return -1;
-                               }
-                               tmp->tail+=160;
-                       } else {
-                               ast_log(LOG_WARNING, "Out of buffer space\n");
+                       len = GSM_SAMPLES;
+                       src = f->data.ptr + x;
+               }
+               /* XXX maybe we don't need to check */
+               if (pvt->samples + len > BUFFER_SAMPLES) {      
+                       ast_log(LOG_WARNING, "Out of buffer space\n");
+                       return -1;
+               }
+               if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
+                       ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
+                       return -1;
+               }
+               pvt->samples += GSM_SAMPLES;
+               pvt->datalen += 2 * GSM_SAMPLES;
+               if (flen == MSGSM_FRAME_LEN) {
+                       if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
+                               ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
                                return -1;
                        }
+                       pvt->samples += GSM_SAMPLES;
+                       pvt->datalen += 2 * GSM_SAMPLES;
                }
        }
        return 0;
 }
 
-static int lintogsm_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
+/*! \brief store samples into working buffer for later decode */
+static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-       /* Just add the frames to our stream */
+       struct gsm_translator_pvt *tmp = pvt->pvt;
+
        /* XXX We should look at how old the rest of our stream is, and if it
           is too old, then we should overwrite it entirely, otherwise we can
           get artifacts of earlier talk that do not belong */
-       if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
-               memcpy((tmp->buf + tmp->tail), f->data, f->datalen);
-               tmp->tail += f->datalen/2;
-       } else {
+       if (pvt->samples + f->samples > BUFFER_SAMPLES) {
                ast_log(LOG_WARNING, "Out of buffer space\n");
                return -1;
        }
+       memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
+       pvt->samples += f->samples;
        return 0;
 }
 
-static struct ast_frame *lintogsm_frameout(struct ast_translator_pvt *tmp)
+/*! \brief encode and produce a frame */
+static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
 {
-       int x=0;
-       /* We can't work on anything less than a frame in size */
-       if (tmp->tail < 160)
-               return NULL;
-       tmp->f.frametype = AST_FRAME_VOICE;
-       tmp->f.subclass = AST_FORMAT_GSM;
-       tmp->f.mallocd = 0;
-       tmp->f.offset = AST_FRIENDLY_OFFSET;
-       tmp->f.src = __PRETTY_FUNCTION__;
-       tmp->f.data = tmp->outbuf;
-       while(tmp->tail >= 160) {
-               if ((x+1) * 33 >= sizeof(tmp->outbuf)) {
-                       ast_log(LOG_WARNING, "Out of buffer space\n");
-                       break;
-               }
+       struct gsm_translator_pvt *tmp = pvt->pvt;
+       struct ast_frame *result = NULL;
+       struct ast_frame *last = NULL;
+       int samples = 0; /* output samples */
+
+       while (pvt->samples >= GSM_SAMPLES) {
+               struct ast_frame *current;
+
                /* Encode a frame of data */
-               gsm_encode(tmp->gsm, tmp->buf, ((gsm_byte *) tmp->outbuf) + (x * 33));
-               /* Assume 8000 Hz -- 20 ms */
-               tmp->tail -= 160;
-               /* Move the data at the end of the buffer to the front */
-               if (tmp->tail)
-                       memmove(tmp->buf, tmp->buf + 160, tmp->tail * 2);
-               x++;
+               gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c);
+               samples += GSM_SAMPLES;
+               pvt->samples -= GSM_SAMPLES;
+
+               current = ast_trans_frameout(pvt, GSM_FRAME_LEN, GSM_SAMPLES);
+               if (!current) {
+                       continue;
+               } else if (last) {
+                       AST_LIST_NEXT(last, frame_list) = current;
+               } else {
+                       result = current;
+               }
+               last = current;
+       }
+
+       /* Move the data at the end of the buffer to the front */
+       if (samples) {
+               memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
        }
-       tmp->f.datalen = x * 33;
-       tmp->f.samples = x * 160;
-       return &tmp->f; 
+
+       return result;
 }
 
-static void gsm_destroy_stuff(struct ast_translator_pvt *pvt)
+static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
 {
-       if (pvt->gsm)
-               gsm_destroy(pvt->gsm);
-       free(pvt);
-       localusecnt--;
+       struct gsm_translator_pvt *tmp = pvt->pvt;
+       if (tmp->gsm)
+               gsm_destroy(tmp->gsm);
 }
 
-static struct ast_translator gsmtolin =
-       { "gsmtolin", 
-          AST_FORMAT_GSM, AST_FORMAT_SLINEAR,
-          gsm_new,
-          gsmtolin_framein,
-          gsmtolin_frameout,
-          gsm_destroy_stuff,
-          gsmtolin_sample
-          };
-
-static struct ast_translator lintogsm =
-       { "lintogsm", 
-          AST_FORMAT_SLINEAR, AST_FORMAT_GSM,
-          gsm_new,
-          lintogsm_framein,
-          lintogsm_frameout,
-          gsm_destroy_stuff,
-          lintogsm_sample
-          };
-
-int unload_module(void)
+static struct ast_translator gsmtolin = {
+       .name = "gsmtolin",
+       .src_codec = {
+               .name = "gsm",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 8000,
+       },
+       .dst_codec = {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 8000,
+       },
+       .format = "slin",
+       .newpvt = gsm_new,
+       .framein = gsmtolin_framein,
+       .destroy = gsm_destroy_stuff,
+       .sample = gsm_sample,
+       .buffer_samples = BUFFER_SAMPLES,
+       .buf_size = BUFFER_SAMPLES * 2,
+       .desc_size = sizeof (struct gsm_translator_pvt ),
+};
+
+static struct ast_translator lintogsm = {
+       .name = "lintogsm",
+       .src_codec = {
+               .name = "slin",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 8000,
+       },
+       .dst_codec = {
+               .name = "gsm",
+               .type = AST_MEDIA_TYPE_AUDIO,
+               .sample_rate = 8000,
+       },
+       .format = "gsm",
+       .newpvt = gsm_new,
+       .framein = lintogsm_framein,
+       .frameout = lintogsm_frameout,
+       .destroy = gsm_destroy_stuff,
+       .sample = slin8_sample,
+       .desc_size = sizeof (struct gsm_translator_pvt ),
+       .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
+};
+
+static int unload_module(void)
 {
        int res;
-       ast_mutex_lock(&localuser_lock);
+
        res = ast_unregister_translator(&lintogsm);
-       if (!res)
-               res = ast_unregister_translator(&gsmtolin);
-       if (localusecnt)
-               res = -1;
-       ast_mutex_unlock(&localuser_lock);
+       res |= ast_unregister_translator(&gsmtolin);
+
        return res;
 }
 
-int load_module(void)
+static int load_module(void)
 {
        int res;
-       res=ast_register_translator(&gsmtolin);
-       if (!res) 
-               res=ast_register_translator(&lintogsm);
-       else
-               ast_unregister_translator(&gsmtolin);
-       return res;
-}
 
-char *description(void)
-{
-       return tdesc;
-}
+       res = ast_register_translator(&gsmtolin);
+       res |= ast_register_translator(&lintogsm);
 
-int usecount(void)
-{
-       int res;
-       STANDARD_USECOUNT(res);
-       return res;
-}
+       if (res) {
+               unload_module();
+               return AST_MODULE_LOAD_FAILURE;
+       }
 
-char *key()
-{
-       return ASTERISK_GPL_KEY;
+       return AST_MODULE_LOAD_SUCCESS;
 }
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+);