Thu May 24 14:22:32 2007

Asterisk developer's documentation


format_g729.c File Reference

Save to raw, headerless G729 data. More...

#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "asterisk.h"
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/sched.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"

Include dependency graph for format_g729.c:

Go to the source code of this file.

Data Structures

struct  ast_filestream

Functions

 AST_MUTEX_DEFINE_STATIC (g729_lock)
char * description ()
 Provides a description of the module.
static void g729_close (struct ast_filestream *s)
static char * g729_getcomment (struct ast_filestream *s)
static struct ast_filestreamg729_open (FILE *f)
static struct ast_frameg729_read (struct ast_filestream *s, int *whennext)
static struct ast_filestreamg729_rewrite (FILE *f, const char *comment)
static int g729_seek (struct ast_filestream *fs, long sample_offset, int whence)
static long g729_tell (struct ast_filestream *fs)
static int g729_trunc (struct ast_filestream *fs)
static int g729_write (struct ast_filestream *fs, struct ast_frame *f)
char * key ()
 Returns the ASTERISK_GPL_KEY.
int load_module ()
 Initialize the module.
int unload_module ()
 Cleanup all module structures, sockets, etc.
int usecount ()
 Provides a usecount.

Variables

static char * desc = "Raw G729 data"
static char * exts = "g729"
static int glistcnt = 0
static char * name = "g729"


Detailed Description

Save to raw, headerless G729 data.

Note:
This is not an encoder/decoder. The codec fo g729 is only available with a commercial license from Digium, due to patent restrictions. Check http://www.digium.com for information.

Definition in file format_g729.c.


Function Documentation

AST_MUTEX_DEFINE_STATIC ( g729_lock   ) 

char* description ( void   ) 

Provides a description of the module.

Returns:
a short description of your module

Definition at line 251 of file format_g729.c.

00252 {
00253    return desc;
00254 }

static void g729_close ( struct ast_filestream s  )  [static]

Definition at line 123 of file format_g729.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), free, LOG_WARNING, and s.

Referenced by load_module().

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 }

static char* g729_getcomment ( struct ast_filestream s  )  [static]

Definition at line 179 of file format_g729.c.

Referenced by load_module().

00180 {
00181    return NULL;
00182 }

static struct ast_filestream* g729_open ( FILE *  f  )  [static]

Definition at line 74 of file format_g729.c.

References AST_FORMAT_G729A, AST_FRAME_VOICE, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), free, LOG_WARNING, and malloc.

Referenced by load_module().

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 }

static struct ast_frame* g729_read ( struct ast_filestream s,
int *  whennext 
) [static]

Definition at line 137 of file format_g729.c.

References AST_FORMAT_G729A, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), LOG_WARNING, and s.

Referenced by load_module().

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 }

static struct ast_filestream* g729_rewrite ( FILE *  f,
const char *  comment 
) [static]

Definition at line 101 of file format_g729.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), free, LOG_WARNING, and malloc.

Referenced by load_module().

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 }

static int g729_seek ( struct ast_filestream fs,
long  sample_offset,
int  whence 
) [static]

Definition at line 184 of file format_g729.c.

References ast_filestream::f, offset, and SEEK_FORCECUR.

Referenced by load_module().

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 }

static long g729_tell ( struct ast_filestream fs  )  [static]

Definition at line 218 of file format_g729.c.

References ast_filestream::f, and offset.

Referenced by load_module().

00219 {
00220    off_t offset;
00221    offset = ftell(fs->f);
00222    return (offset/20)*160;
00223 }

static int g729_trunc ( struct ast_filestream fs  )  [static]

Definition at line 210 of file format_g729.c.

References ast_filestream::f.

Referenced by load_module().

00211 {
00212    /* Truncate file to current length */
00213    if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
00214       return -1;
00215    return 0;
00216 }

static int g729_write ( struct ast_filestream fs,
struct ast_frame f 
) [static]

Definition at line 157 of file format_g729.c.

References AST_FORMAT_G729A, AST_FRAME_VOICE, ast_log(), ast_frame::data, ast_frame::datalen, ast_filestream::f, ast_frame::frametype, LOG_WARNING, and ast_frame::subclass.

Referenced by load_module().

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 }

char* key ( void   ) 

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

 char *key(void) {
         return ASTERISK_GPL_KEY;
 }

Returns:
ASTERISK_GPL_KEY

Definition at line 257 of file format_g729.c.

References ASTERISK_GPL_KEY.

00258 {
00259    return ASTERISK_GPL_KEY;
00260 }

int load_module ( void   ) 

Initialize the module.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
int Always 0.

Definition at line 225 of file format_g729.c.

References AST_FORMAT_G729A, ast_format_register(), g729_close(), g729_getcomment(), g729_open(), g729_read(), g729_rewrite(), g729_seek(), g729_tell(), g729_trunc(), and g729_write().

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 }

int unload_module ( void   ) 

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
Zero on success, or non-zero on error.

Definition at line 241 of file format_g729.c.

References ast_format_unregister().

00242 {
00243    return ast_format_unregister(name);
00244 }  

int usecount ( void   ) 

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
The module's usecount.

Definition at line 246 of file format_g729.c.

00247 {
00248    return glistcnt;
00249 }


Variable Documentation

char* desc = "Raw G729 data" [static]

Definition at line 71 of file format_g729.c.

char* exts = "g729" [static]

Definition at line 72 of file format_g729.c.

int glistcnt = 0 [static]

Definition at line 68 of file format_g729.c.

char* name = "g729" [static]

Definition at line 70 of file format_g729.c.


Generated on Thu May 24 14:22:32 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7