Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:52 2009

## 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_filestream * | h263_open (FILE *f)
static struct ast_frame * | h263_read (struct ast_filestream *s, int *whennext)
static struct ast_filestream * | h263_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.

- File name extension: h263

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

    00274 {
    00275    return desc;
    00276 }

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(), ast_filestream::f, free, and LOG_WARNING.

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

Referenced by load_module().

    00221 {
    00222    return NULL;
    00223 }

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

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(), ast_frame::data, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, free, ast_filestream::h263, LOG_WARNING, malloc, ast_frame::mallocd, ast_frame::src, and ast_frame::subclass.

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, read]`

Definition at line 142 of file format_h263.c.

References AST_FORMAT_H263, AST_FRAME_VIDEO, AST_FRIENDLY_OFFSET, ast_log(), ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, ast_filestream::h263, ast_filestream::lastts, LOG_WARNING, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, and ast_frame::subclass.

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       return NULL;
    00165    }
    00166    if ((res = fread(s->h263, 1, len, s->f)) != len) {
    00167       if (res)
    00168          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
    00169       return NULL;
    00170    }
    00171    s->fr.samples = s->lastts;
    00172    s->fr.datalen = len;
    00173    s->fr.subclass |= mark;
    00174    s->fr.delivery.tv_sec = 0;
    00175    s->fr.delivery.tv_usec = 0;
    00176    if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
    00177       s->lastts = ntohl(ts);
    00178       *whennext = s->lastts * 4/45;
    00179    } else
    00180       *whennext = 0;
    00181    return &s->fr;
    00182 }

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

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

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

Referenced by load_module().

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

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

Definition at line 239 of file format_h263.c.

References ast_filestream::f, and offset.

Referenced by load_module().

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

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

Definition at line 231 of file format_h263.c.

References ast_filestream::f.

Referenced by load_module().

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

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

Definition at line 184 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().

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

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

References ASTERISK_GPL_KEY.

    00280 {
    00281    return ASTERISK_GPL_KEY;
    00282 }

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 247 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().

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

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

References ast_format_unregister().

    00264 {
    00265    return ast_format_unregister(name);
    00266 }

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

    00269 {
    00270    return glistcnt;
    00271 }

## 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.