Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:54 2009

## Asterisk developer's documentation

# format_vox.c File Reference

Flat, binary, ADPCM vox file format. 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_vox.c:

Go to the source code of this file.

## Data Structures

struct | ast_filestream

## Defines

#define | BUF_SIZE 80

## Functions

| AST_MUTEX_DEFINE_STATIC (vox_lock)
char * | description ()
| Provides a description of the module.
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.
static void | vox_close (struct ast_filestream *s)
static char * | vox_getcomment (struct ast_filestream *s)
static struct ast_filestream * | vox_open (FILE *f)
static struct ast_frame * | vox_read (struct ast_filestream *s, int *whennext)
static struct ast_filestream * | vox_rewrite (FILE *f, const char *comment)
static int | vox_seek (struct ast_filestream *fs, long sample_offset, int whence)
static long | vox_tell (struct ast_filestream *fs)
static int | vox_trunc (struct ast_filestream *fs)
static int | vox_write (struct ast_filestream *fs, struct ast_frame *f)

## Variables

static char * | desc = "Dialogic VOX (ADPCM) File Format"
static char * | exts = "vox"
static int | glistcnt = 0
static char * | name = "vox"

## Detailed Description

Flat, binary, ADPCM vox file format.

- File name extensions: vox

Definition in file format_vox.c.

## Define Documentation

#define BUF_SIZE 80

Definition at line 48 of file format_vox.c.

## Function Documentation

AST_MUTEX_DEFINE_STATIC | ( | vox_lock |  | ) |

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 244 of file format_vox.c.

    00245 {
    00246    return desc;
    00247 }

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 250 of file format_vox.c.

References ASTERISK_GPL_KEY.

    00251 {
    00252    return ASTERISK_GPL_KEY;
    00253 }

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 218 of file format_vox.c.

References AST_FORMAT_ADPCM, ast_format_register(), vox_close(), vox_getcomment(), vox_open(), vox_read(), vox_rewrite(), vox_seek(), vox_tell(), vox_trunc(), and vox_write().

    00219 {
    00220    return ast_format_register(name, exts, AST_FORMAT_ADPCM,
    00221                         vox_open,
    00222                         vox_rewrite,
    00223                         vox_write,
    00224                         vox_seek,
    00225                         vox_trunc,
    00226                         vox_tell,
    00227                         vox_read,
    00228                         vox_close,
    00229                         vox_getcomment);
    00230
    00231
    00232 }

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 234 of file format_vox.c.

References ast_format_unregister().

    00235 {
    00236    return ast_format_unregister(name);
    00237 }

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 239 of file format_vox.c.

    00240 {
    00241    return glistcnt;
    00242 }

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

Definition at line 124 of file format_vox.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().

    00125 {
    00126    if (ast_mutex_lock(&vox_lock)) {
    00127       ast_log(LOG_WARNING, "Unable to lock vox list\n");
    00128       return;
    00129    }
    00130    glistcnt--;
    00131    ast_mutex_unlock(&vox_lock);
    00132    ast_update_use_count();
    00133    fclose(s->f);
    00134    free(s);
    00135    s = NULL;
    00136 }

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

Definition at line 176 of file format_vox.c.

Referenced by load_module().

    00177 {
    00178    return NULL;
    00179 }

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

Definition at line 74 of file format_vox.c.

References AST_FORMAT_ADPCM, 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, ast_filestream::lasttimeout, LOG_WARNING, malloc, ast_frame::mallocd, ast_frame::src, and ast_frame::subclass.

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(&vox_lock)) {
    00083          ast_log(LOG_WARNING, "Unable to lock vox list\n");
    00084          free(tmp);
    00085          return NULL;
    00086       }
    00087       tmp->f = f;
    00088       tmp->fr.data = tmp->buf;
    00089       tmp->fr.frametype = AST_FRAME_VOICE;
    00090       tmp->fr.subclass = AST_FORMAT_ADPCM;
    00091       /* datalen will vary for each frame */
    00092       tmp->fr.src = name;
    00093       tmp->fr.mallocd = 0;
    00094       tmp->lasttimeout = -1;
    00095       glistcnt++;
    00096       ast_mutex_unlock(&vox_lock);
    00097       ast_update_use_count();
    00098    }
    00099    return tmp;
    00100 }

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

Definition at line 138 of file format_vox.c.

References AST_FORMAT_ADPCM, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_filestream::buf, BUF_SIZE, ast_frame::data, ast_frame::datalen, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, LOG_WARNING, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, and ast_frame::subclass.

Referenced by load_module().

    00139 {
    00140    int res;
    00141    /* Send a frame from the file to the appropriate channel */
    00142    s->fr.frametype = AST_FRAME_VOICE;
    00143    s->fr.subclass = AST_FORMAT_ADPCM;
    00144    s->fr.offset = AST_FRIENDLY_OFFSET;
    00145    s->fr.mallocd = 0;
    00146    s->fr.data = s->buf;
    00147    if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
    00148       if (res)
    00149          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
    00150       return NULL;
    00151    }
    00152    s->fr.samples = res * 2;
    00153    s->fr.datalen = res;
    00154    *whennext = s->fr.samples;
    00155    return &s->fr;
    00156 }

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

Definition at line 102 of file format_vox.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().

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

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

Definition at line 181 of file format_vox.c.

References ast_filestream::f, offset, and SEEK_FORCECUR.

Referenced by load_module().

    00182 {
    00183      off_t offset=0,min,cur,max,distance;
    00184
    00185      min = 0;
    00186      cur = ftell(fs->f);
    00187      fseek(fs->f, 0, SEEK_END);
    00188     max = ftell(fs->f);
    00189
    00190      /* have to fudge to frame here, so not fully to sample */
    00191      distance = sample_offset/2;
    00192      if(whence == SEEK_SET)
    00193      offset = distance;
    00194      else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
    00195      offset = distance + cur;
    00196      else if(whence == SEEK_END)
    00197      offset = max - distance;
    00198      if (whence != SEEK_FORCECUR) {
    00199      offset = (offset > max)?max:offset;
    00200      offset = (offset < min)?min:offset;
    00201      }
    00202      fseek(fs->f, offset, SEEK_SET);
    00203     return ftell(fs->f);
    00204 }

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

Definition at line 211 of file format_vox.c.

References ast_filestream::f, and offset.

Referenced by load_module().

    00212 {
    00213      off_t offset;
    00214      offset = ftell(fs->f) << 1;
    00215      return offset;
    00216 }

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

Definition at line 206 of file format_vox.c.

References ast_filestream::f.

Referenced by load_module().

    00207 {
    00208      return ftruncate(fileno(fs->f), ftell(fs->f));
    00209 }

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

Definition at line 158 of file format_vox.c.

References AST_FORMAT_ADPCM, 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().

    00159 {
    00160    int res;
    00161    if (f->frametype != AST_FRAME_VOICE) {
    00162       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
    00163       return -1;
    00164    }
    00165    if (f->subclass != AST_FORMAT_ADPCM) {
    00166       ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
    00167       return -1;
    00168    }
    00169    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
    00170          ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
    00171          return -1;
    00172    }
    00173    return 0;
    00174 }

## Variable Documentation

char* desc = "Dialogic VOX (ADPCM) File Format"`[static]`

Definition at line 71 of file format_vox.c.

char* exts = "vox"`[static]`

Definition at line 72 of file format_vox.c.

int glistcnt = 0`[static]`

Definition at line 68 of file format_vox.c.

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

Definition at line 70 of file format_vox.c.