Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## Asterisk developer's documentation

# format_g729.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2005, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * See http://www.asterisk.org for more information about
    00009  * the Asterisk project. Please do not directly contact
    00010  * any of the maintainers of this project for assistance;
    00011  * the project provides a web site, mailing lists and IRC
    00012  * channels for your use.
    00013  *
    00014  * This program is free software, distributed under the terms of
    00015  * the GNU General Public License Version 2. See the LICENSE file
    00016  * at the top of the source tree.
    00017  */
    00018
    00019 /*! \file
    00020  *
    00021  * \brief Save to raw, headerless G729 data.
    00022  * \note This is not an encoder/decoder. The codec fo g729 is only
    00023  * available with a commercial license from Digium, due to patent
    00024  * restrictions. Check http://www.digium.com for information.
    00025  * \arg Extensions: g729
    00026  * \ingroup formats
    00027  */
    00028
    00029 #include <unistd.h>
    00030 #include <netinet/in.h>
    00031 #include <arpa/inet.h>
    00032 #include <stdlib.h>
    00033 #include <sys/time.h>
    00034 #include <stdio.h>
    00035 #include <errno.h>
    00036 #include <string.h>
    00037
    00038 #include "asterisk.h"
    00039
    00040 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00041
    00042 #include "asterisk/lock.h"
    00043 #include "asterisk/channel.h"
    00044 #include "asterisk/file.h"
    00045 #include "asterisk/logger.h"
    00046 #include "asterisk/sched.h"
    00047 #include "asterisk/module.h"
    00048 #include "asterisk/endian.h"
    00049
    00050 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
    00051
    00052 /* Portions of the conversion code are by guido@sienanet.it */
    00053
    00054 struct ast_filestream {
    00055    void *reserved[AST_RESERVED_POINTERS];
    00056    /* Believe it or not, we must decode/recode to account for the
    00057       weird MS format */
    00058    /* This is what a filestream means to us */
    00059    FILE *f; /* Descriptor */
    00060    struct ast_frame fr;          /* Frame information */
    00061    char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
    00062    char empty;                   /* Empty character */
    00063    unsigned char g729[20];          /* Two Real G729 Frames */
    00064 };
    00065
    00066
    00067 AST_MUTEX_DEFINE_STATIC(g729_lock);
    00068 static int glistcnt = 0;
    00069
    00070 static char *name = "g729";
    00071 static char *desc = "Raw G729 data";
    00072 static char *exts = "g729";
    00073
    00074 static struct ast_filestream *g729_open(FILE *f)
    00075 {
    00076    /* We don't have any header to read or anything really, but
    00077       if we did, it would go here.  We also might want to check
    00078       and be sure it's a valid file.  */
    00079    struct ast_filestream *tmp;
    00080    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
    00081       memset(tmp, 0, sizeof(struct ast_filestream));
    00082       if (ast_mutex_lock(&g729_lock)) {
    00083          ast_log(LOG_WARNING, "Unable to lock g729 list\n");
    00084          free(tmp);
    00085          return NULL;
    00086       }
    00087       tmp->f = f;
    00088       tmp->fr.data = tmp->g729;
    00089       tmp->fr.frametype = AST_FRAME_VOICE;
    00090       tmp->fr.subclass = AST_FORMAT_G729A;
    00091       /* datalen will vary for each frame */
    00092       tmp->fr.src = name;
    00093       tmp->fr.mallocd = 0;
    00094       glistcnt++;
    00095       ast_mutex_unlock(&g729_lock);
    00096       ast_update_use_count();
    00097    }
    00098    return tmp;
    00099 }
    00100
    00101 static struct ast_filestream *g729_rewrite(FILE *f, const char *comment)
    00102 {
    00103    /* We don't have any header to read or anything really, but
    00104       if we did, it would go here.  We also might want to check
    00105       and be sure it's a valid file.  */
    00106    struct ast_filestream *tmp;
    00107    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
    00108       memset(tmp, 0, sizeof(struct ast_filestream));
    00109       if (ast_mutex_lock(&g729_lock)) {
    00110          ast_log(LOG_WARNING, "Unable to lock g729 list\n");
    00111          free(tmp);
    00112          return NULL;
    00113       }
    00114       tmp->f = f;
    00115       glistcnt++;
    00116       ast_mutex_unlock(&g729_lock);
    00117       ast_update_use_count();
    00118    } else
    00119       ast_log(LOG_WARNING, "Out of memory\n");
    00120    return tmp;
    00121 }
    00122
    00123 static void g729_close(struct ast_filestream *s)
    00124 {
    00125    if (ast_mutex_lock(&g729_lock)) {
    00126       ast_log(LOG_WARNING, "Unable to lock g729 list\n");
    00127       return;
    00128    }
    00129    glistcnt--;
    00130    ast_mutex_unlock(&g729_lock);
    00131    ast_update_use_count();
    00132    fclose(s->f);
    00133    free(s);
    00134    s = NULL;
    00135 }
    00136
    00137 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
    00138 {
    00139    int res;
    00140    /* Send a frame from the file to the appropriate channel */
    00141    s->fr.frametype = AST_FRAME_VOICE;
    00142    s->fr.subclass = AST_FORMAT_G729A;
    00143    s->fr.offset = AST_FRIENDLY_OFFSET;
    00144    s->fr.samples = 160;
    00145    s->fr.datalen = 20;
    00146    s->fr.mallocd = 0;
    00147    s->fr.data = s->g729;
    00148    if ((res = fread(s->g729, 1, 20, s->f)) != 20) {
    00149       if (res && (res != 10))
    00150          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
    00151       return NULL;
    00152    }
    00153    *whennext = s->fr.samples;
    00154    return &s->fr;
    00155 }
    00156
    00157 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
    00158 {
    00159    int res;
    00160    if (f->frametype != AST_FRAME_VOICE) {
    00161       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
    00162       return -1;
    00163    }
    00164    if (f->subclass != AST_FORMAT_G729A) {
    00165       ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
    00166       return -1;
    00167    }
    00168    if (f->datalen % 10) {
    00169       ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
    00170       return -1;
    00171    }
    00172    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
    00173          ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
    00174          return -1;
    00175    }
    00176    return 0;
    00177 }
    00178
    00179 static char *g729_getcomment(struct ast_filestream *s)
    00180 {
    00181    return NULL;
    00182 }
    00183
    00184 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
    00185 {
    00186    long bytes;
    00187    off_t min,cur,max,offset=0;
    00188    min = 0;
    00189    cur = ftell(fs->f);
    00190    fseek(fs->f, 0, SEEK_END);
    00191    max = ftell(fs->f);
    00192
    00193    bytes = 20 * (sample_offset / 160);
    00194    if (whence == SEEK_SET)
    00195       offset = bytes;
    00196    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
    00197       offset = cur + bytes;
    00198    else if (whence == SEEK_END)
    00199       offset = max - bytes;
    00200    if (whence != SEEK_FORCECUR) {
    00201       offset = (offset > max)?max:offset;
    00202    }
    00203    /* protect against seeking beyond begining. */
    00204    offset = (offset < min)?min:offset;
    00205    if (fseek(fs->f, offset, SEEK_SET) < 0)
    00206       return -1;
    00207    return 0;
    00208 }
    00209
    00210 static int g729_trunc(struct ast_filestream *fs)
    00211 {
    00212    /* Truncate file to current length */
    00213    if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
    00214       return -1;
    00215    return 0;
    00216 }
    00217
    00218 static long g729_tell(struct ast_filestream *fs)
    00219 {
    00220    off_t offset;
    00221    offset = ftell(fs->f);
    00222    return (offset/20)*160;
    00223 }
    00224
    00225 int load_module()
    00226 {
    00227    return ast_format_register(name, exts, AST_FORMAT_G729A,
    00228                         g729_open,
    00229                         g729_rewrite,
    00230                         g729_write,
    00231                         g729_seek,
    00232                         g729_trunc,
    00233                         g729_tell,
    00234                         g729_read,
    00235                         g729_close,
    00236                         g729_getcomment);
    00237
    00238
    00239 }
    00240
    00241 int unload_module()
    00242 {
    00243    return ast_format_unregister(name);
    00244 }
    00245
    00246 int usecount()
    00247 {
    00248    return glistcnt;
    00249 }
    00250
    00251 char *description()
    00252 {
    00253    return desc;
    00254 }
    00255
    00256
    00257 char *key()
    00258 {
    00259    return ASTERISK_GPL_KEY;
    00260 }