Mon Mar 31 07:40:41 2008

Asterisk developer's documentation


format_g729.c File Reference

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

Go to the source code of this file.

Defines

#define BUF_SIZE   20
#define G729A_SAMPLES   160

Functions

 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Raw G729 data")
static struct ast_frameg729_read (struct ast_filestream *s, int *whennext)
static int g729_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
static off_t g729_tell (struct ast_filestream *fs)
static int g729_trunc (struct ast_filestream *fs)
static int g729_write (struct ast_filestream *fs, struct ast_frame *f)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_format g729_f


Detailed Description

Save to raw, headerless G729 data.

Note:
This is not an encoder/decoder. The codec fo g729 is only available with a commercial license from Digium, due to patent restrictions. Check http://www.digium.com for information.

Definition in file format_g729.c.


Define Documentation

#define BUF_SIZE   20

Definition at line 54 of file format_g729.c.

#define G729A_SAMPLES   160

Definition at line 55 of file format_g729.c.

Referenced by g729_read(), g729_seek(), and g729_tell().


Function Documentation

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Raw G729 data"   
)

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

Definition at line 57 of file format_g729.c.

References AST_FORMAT_G729A, AST_FRAME_SET_BUFFER, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), BUF_SIZE, errno, G729A_SAMPLES, LOG_WARNING, and s.

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

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

Definition at line 97 of file format_g729.c.

References BUF_SIZE, ast_filestream::f, G729A_SAMPLES, offset, and SEEK_FORCECUR.

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

static off_t g729_tell ( struct ast_filestream fs  )  [static]

Definition at line 131 of file format_g729.c.

References BUF_SIZE, ast_filestream::f, G729A_SAMPLES, and offset.

00132 {
00133    off_t offset = ftello(fs->f);
00134    return (offset/BUF_SIZE)*G729A_SAMPLES;
00135 }

static int g729_trunc ( struct ast_filestream fs  )  [static]

Definition at line 123 of file format_g729.c.

References ast_filestream::f.

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

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

Definition at line 75 of file format_g729.c.

References AST_FORMAT_G729A, AST_FRAME_VOICE, ast_log(), errno, ast_filestream::f, f, and LOG_WARNING.

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

static int load_module ( void   )  [static]

Definition at line 149 of file format_g729.c.

References ast_format_register, and g729_f.

00150 {
00151    return ast_format_register(&g729_f);
00152 }

static int unload_module ( void   )  [static]

Definition at line 154 of file format_g729.c.

References ast_format_unregister(), g729_f, and ast_format::name.

00155 {
00156    return ast_format_unregister(g729_f.name);
00157 }  


Variable Documentation

struct ast_format g729_f [static]

Definition at line 137 of file format_g729.c.

Referenced by load_module(), and unload_module().


Generated on Mon Mar 31 07:40:41 2008 for Asterisk - the Open Source PBX by  doxygen 1.5.1