Fri Sep 25 19:28:35 2009

Asterisk developer's documentation


format_ilbc.c File Reference

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

#include "asterisk.h"
#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/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.

Defines

#define ILBC_BUF_SIZE   50
#define ILBC_SAMPLES   240

Functions

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Raw iLBC data")
static struct ast_frameilbc_read (struct ast_filestream *s, int *whennext)
static int ilbc_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
static off_t 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)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_format ilbc_f


Detailed Description

Save to raw, headerless iLBC data.

Definition in file format_ilbc.c.


Define Documentation

#define ILBC_BUF_SIZE   50

Definition at line 53 of file format_ilbc.c.

Referenced by ilbc_read(), ilbc_seek(), and ilbc_tell().

#define ILBC_SAMPLES   240

Definition at line 54 of file format_ilbc.c.

Referenced by ilbc_read(), ilbc_seek(), and ilbc_tell().


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Raw iLBC data"   
)

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

Definition at line 56 of file format_ilbc.c.

References AST_FORMAT_ILBC, AST_FRAME_SET_BUFFER, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_filestream::buf, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, ILBC_BUF_SIZE, ILBC_SAMPLES, LOG_WARNING, ast_frame::mallocd, ast_frame::samples, and ast_frame::subclass.

00057 {
00058    int res;
00059    /* Send a frame from the file to the appropriate channel */
00060    s->fr.frametype = AST_FRAME_VOICE;
00061    s->fr.subclass = AST_FORMAT_ILBC;
00062    s->fr.mallocd = 0;
00063    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
00064    if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00065       if (res)
00066          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00067       return NULL;
00068    }
00069    *whennext = s->fr.samples = ILBC_SAMPLES;
00070    return &s->fr;
00071 }

static int ilbc_seek ( struct ast_filestream fs,
off_t  sample_offset,
int  whence 
) [static]

Definition at line 95 of file format_ilbc.c.

References ast_filestream::f, ILBC_BUF_SIZE, ILBC_SAMPLES, offset, and SEEK_FORCECUR.

00096 {
00097    long bytes;
00098    off_t min,cur,max,offset=0;
00099    min = 0;
00100    cur = ftello(fs->f);
00101    fseeko(fs->f, 0, SEEK_END);
00102    max = ftello(fs->f);
00103    
00104    bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES);
00105    if (whence == SEEK_SET)
00106       offset = bytes;
00107    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00108       offset = cur + bytes;
00109    else if (whence == SEEK_END)
00110       offset = max - bytes;
00111    if (whence != SEEK_FORCECUR) {
00112       offset = (offset > max)?max:offset;
00113    }
00114    /* protect against seeking beyond begining. */
00115    offset = (offset < min)?min:offset;
00116    if (fseeko(fs->f, offset, SEEK_SET) < 0)
00117       return -1;
00118    return 0;
00119 }

static off_t ilbc_tell ( struct ast_filestream fs  )  [static]

Definition at line 129 of file format_ilbc.c.

References ast_filestream::f, ILBC_BUF_SIZE, ILBC_SAMPLES, and offset.

00130 {
00131    off_t offset = ftello(fs->f);
00132    return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES;
00133 }

static int ilbc_trunc ( struct ast_filestream fs  )  [static]

Definition at line 121 of file format_ilbc.c.

References ast_filestream::f.

00122 {
00123    /* Truncate file to current length */
00124    if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
00125       return -1;
00126    return 0;
00127 }

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

Definition at line 73 of file format_ilbc.c.

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

00074 {
00075    int res;
00076    if (f->frametype != AST_FRAME_VOICE) {
00077       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00078       return -1;
00079    }
00080    if (f->subclass != AST_FORMAT_ILBC) {
00081       ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass);
00082       return -1;
00083    }
00084    if (f->datalen % 50) {
00085       ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
00086       return -1;
00087    }
00088    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00089          ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
00090          return -1;
00091    }
00092    return 0;
00093 }

static int load_module ( void   )  [static]

Definition at line 147 of file format_ilbc.c.

References ast_format_register.

00148 {
00149    return ast_format_register(&ilbc_f);
00150 }

static int unload_module ( void   )  [static]

Definition at line 152 of file format_ilbc.c.

References ast_format_unregister(), and ast_format::name.

00153 {
00154    return ast_format_unregister(ilbc_f.name);
00155 }  


Variable Documentation

struct ast_format ilbc_f [static]

Definition at line 135 of file format_ilbc.c.


Generated on Fri Sep 25 19:28:35 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.5