Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:51 2009

## Asterisk developer's documentation

# format_g723.c File Reference

Old-style G.723.1 frame/timestamp format. More...

`#include <unistd.h>`
`#include <netinet/in.h>`
`#include <arpa/inet.h>`
`#include <stdlib.h>`
`#include <stdio.h>`
`#include <errno.h>`
`#include <string.h>`
`#include <sys/time.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 "../channels/adtranvofr.h"`

Include dependency graph for format_g723.c:

Go to the source code of this file.

## Data Structures

struct | ast_filestream

## Defines

#define | G723_MAX_SIZE 1024

## Functions

| AST_MUTEX_DEFINE_STATIC (g723_lock)
char * | description ()
| Provides a description of the module.
static void | g723_close (struct ast_filestream *s)
static char * | g723_getcomment (struct ast_filestream *s)
static struct ast_filestream * | g723_open (FILE *f)
static struct ast_frame * | g723_read (struct ast_filestream *s, int *whennext)
static struct ast_filestream * | g723_rewrite (FILE *f, const char *comment)
static int | g723_seek (struct ast_filestream *fs, long sample_offset, int whence)
static long | g723_tell (struct ast_filestream *fs)
static int | g723_trunc (struct ast_filestream *fs)
static int | g723_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 = "G.723.1 Simple Timestamp File Format"
static char * | exts = "g723|g723sf"
static int | glistcnt = 0
static char * | name = "g723sf"

## Detailed Description

Old-style G.723.1 frame/timestamp format.

- Extensions: g723, g723sf

Definition in file format_g723.c.

## Define Documentation

#define G723_MAX_SIZE 1024

Definition at line 51 of file format_g723.c.

Referenced by g723_read().

## Function Documentation

AST_MUTEX_DEFINE_STATIC | ( | g723_lock |  | ) |

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 269 of file format_g723.c.

    00270 {
    00271    return desc;
    00272 }

static void g723_close | ( | struct ast_filestream * | s | ) | `[static]`

Definition at line 167 of file format_g723.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), ast_filestream::f, free, and LOG_WARNING.

Referenced by load_module().

    00168 {
    00169    if (ast_mutex_lock(&g723_lock)) {
    00170       ast_log(LOG_WARNING, "Unable to lock g723 list\n");
    00171       return;
    00172    }
    00173    glistcnt--;
    00174    ast_mutex_unlock(&g723_lock);
    00175    ast_update_use_count();
    00176    fclose(s->f);
    00177    free(s);
    00178    s = NULL;
    00179 }

static char* g723_getcomment | ( | struct ast_filestream * | s | ) | `[static]`

Definition at line 238 of file format_g723.c.

Referenced by load_module().

    00239 {
    00240    return NULL;
    00241 }

static struct ast_filestream* g723_open | ( | FILE * | f | ) | `[static, read]`

Definition at line 72 of file format_g723.c.

References AST_FORMAT_G723_1, AST_FRAME_VOICE, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), ast_filestream::buf, ast_frame::data, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, free, LOG_WARNING, malloc, ast_frame::mallocd, ast_frame::src, and ast_frame::subclass.

Referenced by load_module().

    00073 {
    00074    /* We don't have any header to read or anything really, but
    00075       if we did, it would go here.  We also might want to check
    00076       and be sure it's a valid file.  */
    00077    struct ast_filestream *tmp;
    00078    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
    00079       memset(tmp, 0, sizeof(struct ast_filestream));
    00080       if (ast_mutex_lock(&g723_lock)) {
    00081          ast_log(LOG_WARNING, "Unable to lock g723 list\n");
    00082          free(tmp);
    00083          return NULL;
    00084       }
    00085       tmp->f = f;
    00086       tmp->fr = (struct ast_frame *)tmp->buf;
    00087       tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
    00088       tmp->fr->frametype = AST_FRAME_VOICE;
    00089       tmp->fr->subclass = AST_FORMAT_G723_1;
    00090       /* datalen will vary for each frame */
    00091       tmp->fr->src = name;
    00092       tmp->fr->mallocd = 0;
    00093       glistcnt++;
    00094       ast_mutex_unlock(&g723_lock);
    00095       ast_update_use_count();
    00096    }
    00097    return tmp;
    00098 }

static struct ast_frame* g723_read | ( | struct ast_filestream * | s,
|  | int * | whennext |
| ) |  |  | `[static, read]`

Definition at line 122 of file format_g723.c.

References AST_FRIENDLY_OFFSET, ast_log(), ast_filestream::buf, ast_frame::data, ast_frame::datalen, ast_filestream::f, ast_filestream::fr, G723_MAX_SIZE, LOG_WARNING, ast_frame::offset, and ast_frame::samples.

Referenced by load_module().

    00123 {
    00124    unsigned short size;
    00125    int res;
    00126    int delay;
    00127    /* Read the delay for the next packet, and schedule again if necessary */
    00128    if (fread(&delay, 1, 4, s->f) == 4)
    00129       delay = ntohl(delay);
    00130    else
    00131       delay = -1;
    00132    if (fread(&size, 1, 2, s->f) != 2) {
    00133       /* Out of data, or the file is no longer valid.  In any case
    00134          go ahead and stop the stream */
    00135       return NULL;
    00136    }
    00137    /* Looks like we have a frame to read from here */
    00138    size = ntohs(size);
    00139    if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
    00140       ast_log(LOG_WARNING, "Size %d is invalid\n", size);
    00141       /* The file is apparently no longer any good, as we
    00142          shouldn't ever get frames even close to this
    00143          size.  */
    00144       return NULL;
    00145    }
    00146    /* Read the data into the buffer */
    00147    s->fr->offset = AST_FRIENDLY_OFFSET;
    00148    s->fr->datalen = size;
    00149    s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
    00150    if ((res = fread(s->fr->data, 1, size, s->f)) != size) {
    00151       ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
    00152       return NULL;
    00153    }
    00154 #if 0
    00155       /* Average out frames <= 50 ms */
    00156       if (delay < 50)
    00157          s->fr->timelen = 30;
    00158       else
    00159          s->fr->timelen = delay;
    00160 #else
    00161       s->fr->samples = 240;
    00162 #endif
    00163    *whennext = s->fr->samples;
    00164    return s->fr;
    00165 }

static struct ast_filestream* g723_rewrite | ( | FILE * | f,
|  | const char * | comment |
| ) |  |  | `[static, read]`

Definition at line 100 of file format_g723.c.

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

Referenced by load_module().

    00101 {
    00102    /* We don't have any header to read or anything really, but
    00103       if we did, it would go here.  We also might want to check
    00104       and be sure it's a valid file.  */
    00105    struct ast_filestream *tmp;
    00106    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
    00107       memset(tmp, 0, sizeof(struct ast_filestream));
    00108       if (ast_mutex_lock(&g723_lock)) {
    00109          ast_log(LOG_WARNING, "Unable to lock g723 list\n");
    00110          free(tmp);
    00111          return NULL;
    00112       }
    00113       tmp->f = f;
    00114       glistcnt++;
    00115       ast_mutex_unlock(&g723_lock);
    00116       ast_update_use_count();
    00117    } else
    00118       ast_log(LOG_WARNING, "Out of memory\n");
    00119    return tmp;
    00120 }

static int g723_seek | ( | struct ast_filestream * | fs,
|  | long | sample_offset,
|  | int | whence |
| ) |  |  | `[static]`

Definition at line 220 of file format_g723.c.

Referenced by load_module().

    00221 {
    00222    return -1;
    00223 }

static long g723_tell | ( | struct ast_filestream * | fs | ) | `[static]`

Definition at line 233 of file format_g723.c.

Referenced by load_module().

    00234 {
    00235    return -1;
    00236 }

static int g723_trunc | ( | struct ast_filestream * | fs | ) | `[static]`

Definition at line 225 of file format_g723.c.

References ast_filestream::f.

Referenced by load_module().

    00226 {
    00227    /* Truncate file to current length */
    00228    if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
    00229       return -1;
    00230    return 0;
    00231 }

static int g723_write | ( | struct ast_filestream * | fs,
|  | struct ast_frame * | f |
| ) |  |  | `[static]`

Definition at line 182 of file format_g723.c.

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

Referenced by load_module().

    00183 {
    00184    u_int32_t delay;
    00185    u_int16_t size;
    00186    int res;
    00187    if (fs->fr) {
    00188       ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
    00189       return -1;
    00190    }
    00191    if (f->frametype != AST_FRAME_VOICE) {
    00192       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
    00193       return -1;
    00194    }
    00195    if (f->subclass != AST_FORMAT_G723_1) {
    00196       ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
    00197       return -1;
    00198    }
    00199    delay = 0;
    00200    if (f->datalen <= 0) {
    00201       ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
    00202       return 0;
    00203    }
    00204    if ((res = fwrite(&delay, 1, 4, fs->f)) != 4) {
    00205       ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
    00206       return -1;
    00207    }
    00208    size = htons(f->datalen);
    00209    if ((res = fwrite(&size, 1, 2, fs->f)) != 2) {
    00210       ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
    00211       return -1;
    00212    }
    00213    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
    00214       ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
    00215       return -1;
    00216    }
    00217    return 0;
    00218 }

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 275 of file format_g723.c.

References ASTERISK_GPL_KEY.

    00276 {
    00277    return ASTERISK_GPL_KEY;
    00278 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

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.

TE STUFF END

Definition at line 243 of file format_g723.c.

References AST_FORMAT_G723_1, ast_format_register(), g723_close(), g723_getcomment(), g723_open(), g723_read(), g723_rewrite(), g723_seek(), g723_tell(), g723_trunc(), and g723_write().

    00244 {
    00245    return ast_format_register(name, exts, AST_FORMAT_G723_1,
    00246                         g723_open,
    00247                         g723_rewrite,
    00248                         g723_write,
    00249                         g723_seek,
    00250                         g723_trunc,
    00251                         g723_tell,
    00252                         g723_read,
    00253                         g723_close,
    00254                         g723_getcomment);
    00255
    00256
    00257 }

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 259 of file format_g723.c.

References ast_format_unregister().

    00260 {
    00261    return ast_format_unregister(name);
    00262 }

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 264 of file format_g723.c.

    00265 {
    00266    return glistcnt;
    00267 }

## Variable Documentation

char* desc = "G.723.1 Simple Timestamp File Format"`[static]`

Definition at line 69 of file format_g723.c.

char* exts = "g723|g723sf"`[static]`

Definition at line 70 of file format_g723.c.

int glistcnt = 0`[static]`

Definition at line 66 of file format_g723.c.

char* name = "g723sf"`[static]`

Definition at line 68 of file format_g723.c.