Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:53 2009

## Asterisk developer's documentation

# format_ilbc.c File Reference

Save to raw, headerless iLBC 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_ilbc.c:

Go to the source code of this file.

## Data Structures

struct | ast_filestream

## Functions

| AST_MUTEX_DEFINE_STATIC (ilbc_lock)
char * | description ()
| Provides a description of the module.
static void | ilbc_close (struct ast_filestream *s)
static char * | ilbc_getcomment (struct ast_filestream *s)
static struct ast_filestream * | ilbc_open (FILE *f)
static struct ast_frame * | ilbc_read (struct ast_filestream *s, int *whennext)
static struct ast_filestream * | ilbc_rewrite (FILE *f, const char *comment)
static int | ilbc_seek (struct ast_filestream *fs, long sample_offset, int whence)
static long | ilbc_tell (struct ast_filestream *fs)
static int | ilbc_trunc (struct ast_filestream *fs)
static int | ilbc_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 iLBC data"
static char * | exts = "ilbc"
static int | glistcnt = 0
static char * | name = "iLBC"

## Detailed Description

Save to raw, headerless iLBC data.

- File name extension: ilbc

Definition in file format_ilbc.c.

## Function Documentation

AST_MUTEX_DEFINE_STATIC | ( | ilbc_lock |  | ) |

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 250 of file format_ilbc.c.

    00251 {
    00252    return desc;
    00253 }

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

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

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

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

Definition at line 178 of file format_ilbc.c.

Referenced by load_module().

    00179 {
    00180    return NULL;
    00181 }

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

Definition at line 73 of file format_ilbc.c.

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

Referenced by load_module().

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

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

Definition at line 136 of file format_ilbc.c.

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

Referenced by load_module().

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

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

Definition at line 100 of file format_ilbc.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(&ilbc_lock)) {
    00109          ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
    00110          free(tmp);
    00111          return NULL;
    00112       }
    00113       tmp->f = f;
    00114       glistcnt++;
    00115       ast_mutex_unlock(&ilbc_lock);
    00116       ast_update_use_count();
    00117    } else
    00118       ast_log(LOG_WARNING, "Out of memory\n");
    00119    return tmp;
    00120 }

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

Definition at line 183 of file format_ilbc.c.

References ast_filestream::f, offset, and SEEK_FORCECUR.

Referenced by load_module().

    00184 {
    00185    long bytes;
    00186    off_t min,cur,max,offset=0;
    00187    min = 0;
    00188    cur = ftell(fs->f);
    00189    fseek(fs->f, 0, SEEK_END);
    00190    max = ftell(fs->f);
    00191
    00192    bytes = 50 * (sample_offset / 240);
    00193    if (whence == SEEK_SET)
    00194       offset = bytes;
    00195    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
    00196       offset = cur + bytes;
    00197    else if (whence == SEEK_END)
    00198       offset = max - bytes;
    00199    if (whence != SEEK_FORCECUR) {
    00200       offset = (offset > max)?max:offset;
    00201    }
    00202    /* protect against seeking beyond begining. */
    00203    offset = (offset < min)?min:offset;
    00204    if (fseek(fs->f, offset, SEEK_SET) < 0)
    00205       return -1;
    00206    return 0;
    00207 }

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

Definition at line 217 of file format_ilbc.c.

References ast_filestream::f, and offset.

Referenced by load_module().

    00218 {
    00219    off_t offset;
    00220    offset = ftell(fs->f);
    00221    return (offset/50)*240;
    00222 }

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

Definition at line 209 of file format_ilbc.c.

References ast_filestream::f.

Referenced by load_module().

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

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

Definition at line 156 of file format_ilbc.c.

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

    00157 {
    00158    int res;
    00159    if (f->frametype != AST_FRAME_VOICE) {
    00160       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
    00161       return -1;
    00162    }
    00163    if (f->subclass != AST_FORMAT_ILBC) {
    00164       ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass);
    00165       return -1;
    00166    }
    00167    if (f->datalen % 50) {
    00168       ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
    00169       return -1;
    00170    }
    00171    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
    00172          ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
    00173          return -1;
    00174    }
    00175    return 0;
    00176 }

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 256 of file format_ilbc.c.

References ASTERISK_GPL_KEY.

    00257 {
    00258    return ASTERISK_GPL_KEY;
    00259 }

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 224 of file format_ilbc.c.

References AST_FORMAT_ILBC, ast_format_register(), ilbc_close(), ilbc_getcomment(), ilbc_open(), ilbc_read(), ilbc_rewrite(), ilbc_seek(), ilbc_tell(), ilbc_trunc(), and ilbc_write().

    00225 {
    00226    return ast_format_register(name, exts, AST_FORMAT_ILBC,
    00227                         ilbc_open,
    00228                         ilbc_rewrite,
    00229                         ilbc_write,
    00230                         ilbc_seek,
    00231                         ilbc_trunc,
    00232                         ilbc_tell,
    00233                         ilbc_read,
    00234                         ilbc_close,
    00235                         ilbc_getcomment);
    00236
    00237
    00238 }

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 240 of file format_ilbc.c.

References ast_format_unregister().

    00241 {
    00242    return ast_format_unregister(name);
    00243 }

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 245 of file format_ilbc.c.

    00246 {
    00247    return glistcnt;
    00248 }

## Variable Documentation

char* desc = "Raw iLBC data"`[static]`

Definition at line 70 of file format_ilbc.c.

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

Definition at line 71 of file format_ilbc.c.

int glistcnt = 0`[static]`

Definition at line 67 of file format_ilbc.c.

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

Definition at line 69 of file format_ilbc.c.