Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## Asterisk developer's documentation

# format_pcm.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (C) 1999 - 2006, Digium, Inc.
    00005  *
    00006  * Mark Spencer <markster@digium.com>
    00007  *
    00008  * See http://www.asterisk.org for more information about
    00009  * the Asterisk project. Please do not directly contact
    00010  * any of the maintainers of this project for assistance;
    00011  * the project provides a web site, mailing lists and IRC
    00012  * channels for your use.
    00013  *
    00014  * This program is free software, distributed under the terms of
    00015  * the GNU General Public License Version 2. See the LICENSE file
    00016  * at the top of the source tree.
    00017  */
    00018
    00019 /*! \file
    00020  *
    00021  * \brief Flat, binary, ulaw PCM file format.
    00022  * \arg File name extension: pcm, ulaw, ul, mu
    00023  *
    00024  * \ingroup formats
    00025  */
    00026
    00027 #include <unistd.h>
    00028 #include <netinet/in.h>
    00029 #include <arpa/inet.h>
    00030 #include <stdlib.h>
    00031 #include <sys/time.h>
    00032 #include <stdio.h>
    00033 #include <errno.h>
    00034 #include <string.h>
    00035
    00036 #include "asterisk.h"
    00037
    00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7819 $")
    00039
    00040 #include "asterisk/lock.h"
    00041 #include "asterisk/channel.h"
    00042 #include "asterisk/file.h"
    00043 #include "asterisk/logger.h"
    00044 #include "asterisk/sched.h"
    00045 #include "asterisk/module.h"
    00046 #include "asterisk/endian.h"
    00047 #include "asterisk/ulaw.h"
    00048
    00049 #define BUF_SIZE 160    /* 160 samples */
    00050
    00051 struct ast_filestream {
    00052    void *reserved[AST_RESERVED_POINTERS];
    00053    /* This is what a filestream means to us */
    00054    FILE *f; /* Descriptor */
    00055    struct ast_channel *owner;
    00056    struct ast_frame fr;          /* Frame information */
    00057    char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
    00058    char empty;                   /* Empty character */
    00059    unsigned char buf[BUF_SIZE];           /* Output Buffer */
    00060    struct timeval last;
    00061 };
    00062
    00063
    00064 AST_MUTEX_DEFINE_STATIC(pcm_lock);
    00065 static int glistcnt = 0;
    00066
    00067 static char *name = "pcm";
    00068 static char *desc = "Raw uLaw 8khz Audio support (PCM)";
    00069 static char *exts = "pcm|ulaw|ul|mu";
    00070
    00071 static char ulaw_silence[BUF_SIZE];
    00072
    00073 static struct ast_filestream *pcm_open(FILE *f)
    00074 {
    00075    /* We don't have any header to read or anything really, but
    00076       if we did, it would go here.  We also might want to check
    00077       and be sure it's a valid file.  */
    00078    struct ast_filestream *tmp;
    00079    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
    00080       memset(tmp, 0, sizeof(struct ast_filestream));
    00081       if (ast_mutex_lock(&pcm_lock)) {
    00082          ast_log(LOG_WARNING, "Unable to lock pcm list\n");
    00083          free(tmp);
    00084          return NULL;
    00085       }
    00086       tmp->f = f;
    00087       tmp->fr.data = tmp->buf;
    00088       tmp->fr.frametype = AST_FRAME_VOICE;
    00089       tmp->fr.subclass = AST_FORMAT_ULAW;
    00090       /* datalen will vary for each frame */
    00091       tmp->fr.src = name;
    00092       tmp->fr.mallocd = 0;
    00093       glistcnt++;
    00094       ast_mutex_unlock(&pcm_lock);
    00095       ast_update_use_count();
    00096    }
    00097    return tmp;
    00098 }
    00099
    00100 static struct ast_filestream *pcm_rewrite(FILE *f, const char *comment)
    00101 {
    00102    /* We don't have any header to read or anything really, but
    00103       if we did, it would go here.  We also might want to check
    00104       and be sure it's a valid file.  */
    00105    struct ast_filestream *tmp;
    00106    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
    00107       memset(tmp, 0, sizeof(struct ast_filestream));
    00108       if (ast_mutex_lock(&pcm_lock)) {
    00109          ast_log(LOG_WARNING, "Unable to lock pcm list\n");
    00110          free(tmp);
    00111          return NULL;
    00112       }
    00113       tmp->f = f;
    00114       glistcnt++;
    00115       ast_mutex_unlock(&pcm_lock);
    00116       ast_update_use_count();
    00117    } else
    00118       ast_log(LOG_WARNING, "Out of memory\n");
    00119    return tmp;
    00120 }
    00121
    00122 static void pcm_close(struct ast_filestream *s)
    00123 {
    00124    if (ast_mutex_lock(&pcm_lock)) {
    00125       ast_log(LOG_WARNING, "Unable to lock pcm list\n");
    00126       return;
    00127    }
    00128    glistcnt--;
    00129    ast_mutex_unlock(&pcm_lock);
    00130    ast_update_use_count();
    00131    fclose(s->f);
    00132    free(s);
    00133    s = NULL;
    00134 }
    00135
    00136 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
    00137 {
    00138    int res;
    00139    int delay;
    00140    /* Send a frame from the file to the appropriate channel */
    00141
    00142    s->fr.frametype = AST_FRAME_VOICE;
    00143    s->fr.subclass = AST_FORMAT_ULAW;
    00144    s->fr.offset = AST_FRIENDLY_OFFSET;
    00145    s->fr.mallocd = 0;
    00146    s->fr.data = s->buf;
    00147    if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
    00148       if (res)
    00149          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
    00150       return NULL;
    00151    }
    00152    s->fr.samples = res;
    00153    s->fr.datalen = res;
    00154    delay = s->fr.samples;
    00155    *whennext = delay;
    00156    return &s->fr;
    00157 }
    00158
    00159 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
    00160 {
    00161    int res;
    00162    if (f->frametype != AST_FRAME_VOICE) {
    00163       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
    00164       return -1;
    00165    }
    00166    if (f->subclass != AST_FORMAT_ULAW) {
    00167       ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
    00168       return -1;
    00169    }
    00170    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
    00171          ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
    00172          return -1;
    00173    }
    00174    return 0;
    00175 }
    00176
    00177 static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
    00178 {
    00179    long cur, max, offset = 0;
    00180
    00181    cur = ftell(fs->f);
    00182    fseek(fs->f, 0, SEEK_END);
    00183    max = ftell(fs->f);
    00184
    00185    switch (whence) {
    00186    case SEEK_SET:
    00187       offset = sample_offset;
    00188       break;
    00189    case SEEK_END:
    00190       offset = max - sample_offset;
    00191       break;
    00192    case SEEK_CUR:
    00193    case SEEK_FORCECUR:
    00194       offset = cur + sample_offset;
    00195       break;
    00196    }
    00197
    00198    switch (whence) {
    00199    case SEEK_FORCECUR:
    00200       if (offset > max) {
    00201          size_t left = offset - max;
    00202          size_t res;
    00203
    00204          while (left) {
    00205             res = fwrite(ulaw_silence, sizeof(ulaw_silence[0]),
    00206                     (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
    00207             if (res == -1)
    00208                return res;
    00209             left -= res * sizeof(ulaw_silence[0]);
    00210          }
    00211          return offset;
    00212       }
    00213       /* fall through */
    00214    default:
    00215       offset = (offset > max) ? max : offset;
    00216       offset = (offset < 0) ? 0 : offset;
    00217       return fseek(fs->f, offset, SEEK_SET);
    00218    }
    00219 }
    00220
    00221 static int pcm_trunc(struct ast_filestream *fs)
    00222 {
    00223    return ftruncate(fileno(fs->f), ftell(fs->f));
    00224 }
    00225
    00226 static long pcm_tell(struct ast_filestream *fs)
    00227 {
    00228    off_t offset;
    00229    offset = ftell(fs->f);
    00230    return offset;
    00231 }
    00232
    00233 static char *pcm_getcomment(struct ast_filestream *s)
    00234 {
    00235    return NULL;
    00236 }
    00237
    00238 int load_module()
    00239 {
    00240    int index;
    00241
    00242    for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
    00243       ulaw_silence[index] = AST_LIN2MU(0);
    00244
    00245    return ast_format_register(name, exts, AST_FORMAT_ULAW,
    00246                pcm_open,
    00247                pcm_rewrite,
    00248                pcm_write,
    00249                pcm_seek,
    00250                pcm_trunc,
    00251                pcm_tell,
    00252                pcm_read,
    00253                pcm_close,
    00254                pcm_getcomment);
    00255 }
    00256
    00257 int unload_module()
    00258 {
    00259    return ast_format_unregister(name);
    00260 }
    00261
    00262 int usecount()
    00263 {
    00264    return glistcnt;
    00265 }
    00266
    00267 char *description()
    00268 {
    00269    return desc;
    00270 }
    00271
    00272
    00273 char *key()
    00274 {
    00275    return ASTERISK_GPL_KEY;
    00276 }