Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:54 2009

## Asterisk developer's documentation

# format_sln.c File Reference

RAW SLINEAR 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_sln.c:

Go to the source code of this file.

## Data Structures

struct | ast_filestream

## Defines

#define | BUF_SIZE 320

## Functions

| AST_MUTEX_DEFINE_STATIC (slinear_lock)
char * | description ()
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module ()
| Initialize the module.
static void | slinear_close (struct ast_filestream *s)
static char * | slinear_getcomment (struct ast_filestream *s)
static struct ast_filestream * | slinear_open (FILE *f)
static struct ast_frame * | slinear_read (struct ast_filestream *s, int *whennext)
static struct ast_filestream * | slinear_rewrite (FILE *f, const char *comment)
static int | slinear_seek (struct ast_filestream *fs, long sample_offset, int whence)
static long | slinear_tell (struct ast_filestream *fs)
static int | slinear_trunc (struct ast_filestream *fs)
static int | slinear_write (struct ast_filestream *fs, struct ast_frame *f)
int | unload_module ()
| Cleanup all module structures, sockets, etc.
int | usecount ()
| Provides a usecount.

## Variables

static char * | desc = "Raw Signed Linear Audio support (SLN)"
static char * | exts = "sln|raw"
static int | glistcnt = 0
static char * | name = "sln"

## Detailed Description

RAW SLINEAR Format.

- File name extensions: sln, raw

Definition in file format_sln.c.

## Define Documentation

#define BUF_SIZE 320

Definition at line 46 of file format_sln.c.

## Function Documentation

AST_MUTEX_DEFINE_STATIC | ( | slinear_lock |  | ) |

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 238 of file format_sln.c.

    00239 {
    00240    return desc;
    00241 }

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 244 of file format_sln.c.

References ASTERISK_GPL_KEY.

    00245 {
    00246    return ASTERISK_GPL_KEY;
    00247 }

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 212 of file format_sln.c.

References ast_format_register(), AST_FORMAT_SLINEAR, slinear_close(), slinear_getcomment(), slinear_open(), slinear_read(), slinear_rewrite(), slinear_seek(), slinear_tell(), slinear_trunc(), and slinear_write().

    00213 {
    00214    return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
    00215                         slinear_open,
    00216                         slinear_rewrite,
    00217                         slinear_write,
    00218                         slinear_seek,
    00219                         slinear_trunc,
    00220                         slinear_tell,
    00221                         slinear_read,
    00222                         slinear_close,
    00223                         slinear_getcomment);
    00224
    00225
    00226 }

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

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

    00118 {
    00119    if (ast_mutex_lock(&slinear_lock)) {
    00120       ast_log(LOG_WARNING, "Unable to lock slinear list\n");
    00121       return;
    00122    }
    00123    glistcnt--;
    00124    ast_mutex_unlock(&slinear_lock);
    00125    ast_update_use_count();
    00126    fclose(s->f);
    00127    free(s);
    00128    s = NULL;
    00129 }

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

Definition at line 207 of file format_sln.c.

Referenced by load_module().

    00208 {
    00209    return NULL;
    00210 }

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

Definition at line 68 of file format_sln.c.

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

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

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

Definition at line 131 of file format_sln.c.

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

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

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

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

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

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

Definition at line 172 of file format_sln.c.

References ast_filestream::f, offset, and SEEK_FORCECUR.

Referenced by load_module().

    00173 {
    00174    off_t offset=0,min,cur,max;
    00175
    00176    min = 0;
    00177    sample_offset <<= 1;
    00178    cur = ftell(fs->f);
    00179    fseek(fs->f, 0, SEEK_END);
    00180    max = ftell(fs->f);
    00181    if (whence == SEEK_SET)
    00182       offset = sample_offset;
    00183    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
    00184       offset = sample_offset + cur;
    00185    else if (whence == SEEK_END)
    00186       offset = max - sample_offset;
    00187    if (whence != SEEK_FORCECUR) {
    00188       offset = (offset > max)?max:offset;
    00189    }
    00190    /* always protect against seeking past begining. */
    00191    offset = (offset < min)?min:offset;
    00192    return fseek(fs->f, offset, SEEK_SET);
    00193 }

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

Definition at line 200 of file format_sln.c.

References ast_filestream::f, and offset.

Referenced by load_module().

    00201 {
    00202    off_t offset;
    00203    offset = ftell(fs->f);
    00204    return offset / 2;
    00205 }

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

Definition at line 195 of file format_sln.c.

References ast_filestream::f.

Referenced by load_module().

    00196 {
    00197    return ftruncate(fileno(fs->f), ftell(fs->f));
    00198 }

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

Definition at line 154 of file format_sln.c.

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

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

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 228 of file format_sln.c.

References ast_format_unregister().

    00229 {
    00230    return ast_format_unregister(name);
    00231 }

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 233 of file format_sln.c.

    00234 {
    00235    return glistcnt;
    00236 }

## Variable Documentation

char* desc = "Raw Signed Linear Audio support (SLN)"`[static]`

Definition at line 65 of file format_sln.c.

char* exts = "sln|raw"`[static]`

Definition at line 66 of file format_sln.c.

int glistcnt = 0`[static]`

Definition at line 62 of file format_sln.c.

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

Definition at line 64 of file format_sln.c.