Fri May 26 01:48:40 2006

Asterisk developer's documentation


format_h263.c File Reference

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

Go to the source code of this file.

Data Structures

struct  ast_filestream

Functions

 AST_MUTEX_DEFINE_STATIC (h263_lock)
char * description ()
 Provides a description of the module.
static void h263_close (struct ast_filestream *s)
static char * h263_getcomment (struct ast_filestream *s)
static struct ast_filestreamh263_open (FILE *f)
static struct ast_frameh263_read (struct ast_filestream *s, int *whennext)
static struct ast_filestreamh263_rewrite (FILE *f, const char *comment)
static int h263_seek (struct ast_filestream *fs, long sample_offset, int whence)
static long h263_tell (struct ast_filestream *fs)
static int h263_trunc (struct ast_filestream *fs)
static int h263_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 h263 data"
static char * exts = "h263"
static int glistcnt = 0
static char * name = "h263"


Detailed Description

Save to raw, headerless h263 data.

Definition in file format_h263.c.


Function Documentation

AST_MUTEX_DEFINE_STATIC h263_lock   ) 
 

char* description void   ) 
 

Provides a description of the module.

Returns:
a short description of your module

Definition at line 272 of file format_h263.c.

00273 {
00274    return desc;
00275 }

static void h263_close struct ast_filestream s  )  [static]
 

Definition at line 128 of file format_h263.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), free, LOG_WARNING, and s.

Referenced by load_module().

00129 {
00130    if (ast_mutex_lock(&h263_lock)) {
00131       ast_log(LOG_WARNING, "Unable to lock h263 list\n");
00132       return;
00133    }
00134    glistcnt--;
00135    ast_mutex_unlock(&h263_lock);
00136    ast_update_use_count();
00137    fclose(s->f);
00138    free(s);
00139    s = NULL;
00140 }

static char* h263_getcomment struct ast_filestream s  )  [static]
 

Definition at line 219 of file format_h263.c.

Referenced by load_module().

00220 {
00221    return NULL;
00222 }

static struct ast_filestream* h263_open FILE *  f  )  [static]
 

Definition at line 72 of file format_h263.c.

References AST_FORMAT_H263, AST_FRAME_VIDEO, ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), free, LOG_WARNING, and malloc.

Referenced by load_module().

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

static struct ast_frame* h263_read struct ast_filestream s,
int *  whennext
[static]
 

Definition at line 142 of file format_h263.c.

References AST_FORMAT_H263, AST_FRAME_VIDEO, AST_FRIENDLY_OFFSET, ast_log(), LOG_WARNING, and s.

Referenced by load_module().

00143 {
00144    int res;
00145    int mark=0;
00146    unsigned short len;
00147    unsigned int ts;
00148    /* Send a frame from the file to the appropriate channel */
00149    s->fr.frametype = AST_FRAME_VIDEO;
00150    s->fr.subclass = AST_FORMAT_H263;
00151    s->fr.offset = AST_FRIENDLY_OFFSET;
00152    s->fr.mallocd = 0;
00153    s->fr.data = s->h263;
00154    if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) {
00155       return NULL;
00156    }
00157    len = ntohs(len);
00158    if (len & 0x8000) {
00159       mark = 1;
00160    }
00161    len &= 0x7fff;
00162    if (len > sizeof(s->h263)) {
00163       ast_log(LOG_WARNING, "Length %d is too long\n", len);
00164    }
00165    if ((res = fread(s->h263, 1, len, s->f)) != len) {
00166       if (res)
00167          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00168       return NULL;
00169    }
00170    s->fr.samples = s->lastts;
00171    s->fr.datalen = len;
00172    s->fr.subclass |= mark;
00173    s->fr.delivery.tv_sec = 0;
00174    s->fr.delivery.tv_usec = 0;
00175    if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
00176       s->lastts = ntohl(ts);
00177       *whennext = s->lastts * 4/45;
00178    } else
00179       *whennext = 0;
00180    return &s->fr;
00181 }

static struct ast_filestream* h263_rewrite FILE *  f,
const char *  comment
[static]
 

Definition at line 106 of file format_h263.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_update_use_count(), free, LOG_WARNING, and malloc.

Referenced by load_module().

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

static int h263_seek struct ast_filestream fs,
long  sample_offset,
int  whence
[static]
 

Definition at line 224 of file format_h263.c.

Referenced by load_module().

00225 {
00226    /* No way Jose */
00227    return -1;
00228 }

static long h263_tell struct ast_filestream fs  )  [static]
 

Definition at line 238 of file format_h263.c.

References ast_filestream::f, and offset.

Referenced by load_module().

00239 {
00240    /* XXX This is totally bogus XXX */
00241    off_t offset;
00242    offset = ftell(fs->f);
00243    return (offset/20)*160;
00244 }

static int h263_trunc struct ast_filestream fs  )  [static]
 

Definition at line 230 of file format_h263.c.

References ast_filestream::f.

Referenced by load_module().

00231 {
00232    /* Truncate file to current length */
00233    if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
00234       return -1;
00235    return 0;
00236 }

static int h263_write struct ast_filestream fs,
struct ast_frame f
[static]
 

Definition at line 183 of file format_h263.c.

References AST_FORMAT_H263, AST_FRAME_VIDEO, ast_log(), ast_frame::data, ast_frame::datalen, ast_filestream::f, ast_frame::frametype, LOG_WARNING, ast_frame::samples, and ast_frame::subclass.

Referenced by load_module().

00184 {
00185    int res;
00186    unsigned int ts;
00187    unsigned short len;
00188    int subclass;
00189    int mark=0;
00190    if (f->frametype != AST_FRAME_VIDEO) {
00191       ast_log(LOG_WARNING, "Asked to write non-video frame!\n");
00192       return -1;
00193    }
00194    subclass = f->subclass;
00195    if (subclass & 0x1)
00196       mark=0x8000;
00197    subclass &= ~0x1;
00198    if (subclass != AST_FORMAT_H263) {
00199       ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass);
00200       return -1;
00201    }
00202    ts = htonl(f->samples);
00203    if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
00204          ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
00205          return -1;
00206    }
00207    len = htons(f->datalen | mark);
00208    if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
00209          ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
00210          return -1;
00211    }
00212    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00213          ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00214          return -1;
00215    }
00216    return 0;
00217 }

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 278 of file format_h263.c.

References ASTERISK_GPL_KEY.

00279 {
00280    return ASTERISK_GPL_KEY;
00281 }

int load_module void   ) 
 

Initialize the module.

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.

Definition at line 246 of file format_h263.c.

References AST_FORMAT_H263, ast_format_register(), h263_close(), h263_getcomment(), h263_open(), h263_read(), h263_rewrite(), h263_seek(), h263_tell(), h263_trunc(), and h263_write().

00247 {
00248    return ast_format_register(name, exts, AST_FORMAT_H263,
00249                         h263_open,
00250                         h263_rewrite,
00251                         h263_write,
00252                         h263_seek,
00253                         h263_trunc,
00254                         h263_tell,
00255                         h263_read,
00256                         h263_close,
00257                         h263_getcomment);
00258                         
00259                         
00260 }

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 262 of file format_h263.c.

References ast_format_unregister().

00263 {
00264    return ast_format_unregister(name);
00265 }  

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 267 of file format_h263.c.

00268 {
00269    return glistcnt;
00270 }


Variable Documentation

char* desc = "Raw h263 data" [static]
 

Definition at line 69 of file format_h263.c.

char* exts = "h263" [static]
 

Definition at line 70 of file format_h263.c.

int glistcnt = 0 [static]
 

Definition at line 66 of file format_h263.c.

char* name = "h263" [static]
 

Definition at line 68 of file format_h263.c.


Generated on Fri May 26 01:48:41 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.6