Thu Apr 28 2011 17:13:33

Asterisk developer's documentation


format_sln.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Anthony Minessale
00005  * Anthony Minessale (anthmct@yahoo.com)
00006  *
00007  * See http://www.asterisk.org for more information about
00008  * the Asterisk project. Please do not directly contact
00009  * any of the maintainers of this project for assistance;
00010  * the project provides a web site, mailing lists and IRC
00011  * channels for your use.
00012  *
00013  * This program is free software, distributed under the terms of
00014  * the GNU General Public License Version 2. See the LICENSE file
00015  * at the top of the source tree.
00016  */
00017 
00018 /*! \file
00019  *
00020  * \brief RAW SLINEAR Format
00021  * \arg File name extensions: sln, raw
00022  * \ingroup formats
00023  */
00024  
00025 #include "asterisk.h"
00026 
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 233694 $")
00028 
00029 #include "asterisk/mod_format.h"
00030 #include "asterisk/module.h"
00031 #include "asterisk/endian.h"
00032 
00033 #define BUF_SIZE  320      /* 320 bytes, 160 samples */
00034 #define  SLIN_SAMPLES   160
00035 
00036 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
00037 {
00038    int res;
00039    /* Send a frame from the file to the appropriate channel */
00040 
00041    s->fr.frametype = AST_FRAME_VOICE;
00042    s->fr.subclass = AST_FORMAT_SLINEAR;
00043    s->fr.mallocd = 0;
00044    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
00045    if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
00046       if (res)
00047          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00048       return NULL;
00049    }
00050    *whennext = s->fr.samples = res/2;
00051    s->fr.datalen = res;
00052    return &s->fr;
00053 }
00054 
00055 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
00056 {
00057    int res;
00058    if (f->frametype != AST_FRAME_VOICE) {
00059       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00060       return -1;
00061    }
00062    if (f->subclass != AST_FORMAT_SLINEAR) {
00063       ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
00064       return -1;
00065    }
00066    if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00067          ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00068          return -1;
00069    }
00070    return 0;
00071 }
00072 
00073 static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00074 {
00075    off_t offset=0,min,cur,max;
00076 
00077    min = 0;
00078    sample_offset <<= 1;
00079    cur = ftello(fs->f);
00080    fseeko(fs->f, 0, SEEK_END);
00081    max = ftello(fs->f);
00082    if (whence == SEEK_SET)
00083       offset = sample_offset;
00084    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00085       offset = sample_offset + cur;
00086    else if (whence == SEEK_END)
00087       offset = max - sample_offset;
00088    if (whence != SEEK_FORCECUR) {
00089       offset = (offset > max)?max:offset;
00090    }
00091    /* always protect against seeking past begining. */
00092    offset = (offset < min)?min:offset;
00093    return fseeko(fs->f, offset, SEEK_SET);
00094 }
00095 
00096 static int slinear_trunc(struct ast_filestream *fs)
00097 {
00098    return ftruncate(fileno(fs->f), ftello(fs->f));
00099 }
00100 
00101 static off_t slinear_tell(struct ast_filestream *fs)
00102 {
00103    return ftello(fs->f) / 2;
00104 }
00105 
00106 static const struct ast_format slin_f = {
00107    .name = "sln",
00108    .exts = "sln|raw",
00109    .format = AST_FORMAT_SLINEAR,
00110    .write = slinear_write,
00111    .seek = slinear_seek,
00112    .trunc = slinear_trunc,
00113    .tell = slinear_tell,
00114    .read = slinear_read,
00115    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00116 };
00117 
00118 static int load_module(void)
00119 {
00120    if (ast_format_register(&slin_f))
00121       return AST_MODULE_LOAD_FAILURE;
00122    return AST_MODULE_LOAD_SUCCESS;
00123 }
00124 
00125 static int unload_module(void)
00126 {
00127    return ast_format_unregister(slin_f.name);
00128 }  
00129 
00130 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw Signed Linear Audio support (SLN)",
00131    .load = load_module,
00132    .unload = unload_module,
00133    .load_pri = 10,
00134 );