Wed Aug 7 17:16:05 2019

Asterisk developer's documentation


format_pcm.c File Reference

Flat, binary, ulaw PCM file format. More...

#include "asterisk.h"
#include "asterisk/mod_format.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"
#include "asterisk/ulaw.h"
#include "asterisk/alaw.h"

Go to the source code of this file.

Defines

#define AU_ENC_8BIT_ULAW   1
#define AU_HDR_CHANNELS_OFF   5
#define AU_HDR_DATA_SIZE_OFF   2
#define AU_HDR_ENCODING_OFF   3
#define AU_HDR_HDR_SIZE_OFF   1
#define AU_HDR_MAGIC_OFF   0
#define AU_HDR_SAMPLE_RATE_OFF   4
#define AU_HEADER(var)   uint32_t var[6]
#define AU_HEADER_SIZE   24
#define AU_MAGIC   0x2e736e64
#define BUF_SIZE   160
#define htoll(b)   (b)
#define htols(b)   (b)
#define ltohl(b)   (b)
#define ltohs(b)   (b)

Functions

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,"Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz",.load=load_module,.unload=unload_module,.load_pri=AST_MODPRI_APP_DEPEND)
static int au_open (struct ast_filestream *s)
static int au_rewrite (struct ast_filestream *s, const char *comment)
static int au_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
static off_t au_tell (struct ast_filestream *fs)
static int au_trunc (struct ast_filestream *fs)
static int check_header (FILE *f)
static int load_module (void)
static struct ast_framepcm_read (struct ast_filestream *s, int *whennext)
static int pcm_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
static off_t pcm_tell (struct ast_filestream *fs)
static int pcm_trunc (struct ast_filestream *fs)
static int pcm_write (struct ast_filestream *fs, struct ast_frame *f)
static int unload_module (void)
static int update_header (FILE *f)
static int write_header (FILE *f)

Variables

static struct ast_format alaw_f
static char alaw_silence [BUF_SIZE]
static struct ast_format au_f
static struct ast_format g722_f
static struct ast_format pcm_f
static char ulaw_silence [BUF_SIZE]

Detailed Description

Flat, binary, ulaw PCM file format.

Definition in file format_pcm.c.


Define Documentation

#define AU_ENC_8BIT_ULAW   1

Definition at line 257 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HDR_CHANNELS_OFF   5

Definition at line 255 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HDR_DATA_SIZE_OFF   2

Definition at line 252 of file format_pcm.c.

Referenced by update_header(), and write_header().

#define AU_HDR_ENCODING_OFF   3

Definition at line 253 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HDR_HDR_SIZE_OFF   1

Definition at line 251 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HDR_MAGIC_OFF   0

Definition at line 250 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HDR_SAMPLE_RATE_OFF   4

Definition at line 254 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HEADER ( var   )     uint32_t var[6]

Definition at line 248 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define AU_HEADER_SIZE   24

Definition at line 247 of file format_pcm.c.

Referenced by au_seek(), au_tell(), check_header(), update_header(), and write_header().

#define AU_MAGIC   0x2e736e64

Definition at line 259 of file format_pcm.c.

Referenced by check_header(), and write_header().

#define BUF_SIZE   160

Definition at line 41 of file format_pcm.c.

#define htoll (  )     (b)

Definition at line 261 of file format_pcm.c.

Referenced by update_header(), and write_header().

#define htols (  )     (b)

Definition at line 262 of file format_pcm.c.

Referenced by write_header().

#define ltohl (  )     (b)

Definition at line 263 of file format_pcm.c.

Referenced by check_header(), and check_header_fmt().

#define ltohs (  )     (b)

Definition at line 264 of file format_pcm.c.

Referenced by check_header(), and check_header_fmt().


Function Documentation

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_LOAD_ORDER  ,
"Raw/Sun uLaw/ALaw 8KHz   PCM,PCMA,AU,
G.722 16Khz"  ,
load = load_module,
unload = unload_module,
load_pri = AST_MODPRI_APP_DEPEND 
)
static int au_open ( struct ast_filestream s  )  [static]

Definition at line 382 of file format_pcm.c.

References check_header(), and ast_filestream::f.

00383 {
00384    if (check_header(s->f) < 0)
00385       return -1;
00386    return 0;
00387 }

static int au_rewrite ( struct ast_filestream s,
const char *  comment 
) [static]

Definition at line 389 of file format_pcm.c.

References ast_filestream::f, and write_header().

00390 {
00391    if (write_header(s->f))
00392       return -1;
00393    return 0;
00394 }

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

Definition at line 397 of file format_pcm.c.

References AST_FORMAT_G722, ast_log(), AST_LOG_WARNING, AU_HEADER_SIZE, errno, ast_filestream::f, ast_filestream::fmt, ast_format::format, and SEEK_FORCECUR.

00398 {
00399    off_t min = AU_HEADER_SIZE, max, cur;
00400    long offset = 0, bytes;
00401 
00402    if (fs->fmt->format == AST_FORMAT_G722)
00403       bytes = sample_offset / 2;
00404    else
00405       bytes = sample_offset;
00406 
00407    if ((cur = ftello(fs->f)) < 0) {
00408       ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
00409       return -1;
00410    }
00411 
00412    if (fseeko(fs->f, 0, SEEK_END) < 0) {
00413       ast_log(AST_LOG_WARNING, "Unable to seek to end of au filestream %p: %s\n", fs, strerror(errno));
00414       return -1;
00415    }
00416 
00417    if ((max = ftello(fs->f)) < 0) {
00418       ast_log(AST_LOG_WARNING, "Unable to determine max position in au filestream %p: %s\n", fs, strerror(errno));
00419       return -1;
00420    }
00421 
00422    if (whence == SEEK_SET)
00423       offset = bytes + min;
00424    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00425       offset = bytes + cur;
00426    else if (whence == SEEK_END)
00427       offset = max - bytes;
00428 
00429    if (whence != SEEK_FORCECUR) {
00430       offset = (offset > max) ? max : offset;
00431    }
00432 
00433    /* always protect the header space. */
00434    offset = (offset < min) ? min : offset;
00435 
00436    return fseeko(fs->f, offset, SEEK_SET);
00437 }

static off_t au_tell ( struct ast_filestream fs  )  [static]

Definition at line 459 of file format_pcm.c.

References AU_HEADER_SIZE, and ast_filestream::f.

00460 {
00461    off_t offset = ftello(fs->f);
00462    return offset - AU_HEADER_SIZE;
00463 }

static int au_trunc ( struct ast_filestream fs  )  [static]

Definition at line 439 of file format_pcm.c.

References ast_log(), AST_LOG_WARNING, errno, ast_filestream::f, and update_header().

00440 {
00441    int fd;
00442    off_t cur;
00443 
00444    if ((fd = fileno(fs->f)) < 0) {
00445       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for au filestream %p: %s\n", fs, strerror(errno));
00446       return -1;
00447    }
00448    if ((cur = ftello(fs->f)) < 0) {
00449       ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
00450       return -1;
00451    }
00452    /* Truncate file to current length */
00453    if (ftruncate(fd, cur)) {
00454       return -1;
00455    }
00456    return update_header(fs->f);
00457 }

static int check_header ( FILE *  f  )  [static]

Definition at line 282 of file format_pcm.c.

References ast_log(), AU_ENC_8BIT_ULAW, AU_HDR_CHANNELS_OFF, AU_HDR_ENCODING_OFF, AU_HDR_HDR_SIZE_OFF, AU_HDR_MAGIC_OFF, AU_HDR_SAMPLE_RATE_OFF, AU_HEADER, AU_HEADER_SIZE, AU_MAGIC, channels, DEFAULT_SAMPLE_RATE, encoding, LOG_WARNING, and ltohl.

Referenced by au_open().

00283 {
00284    AU_HEADER(header);
00285    uint32_t magic;
00286    uint32_t hdr_size;
00287    uint32_t data_size;
00288    uint32_t encoding;
00289    uint32_t sample_rate;
00290    uint32_t channels;
00291 
00292    if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
00293       ast_log(LOG_WARNING, "Read failed (header)\n");
00294       return -1;
00295    }
00296    magic = ltohl(header[AU_HDR_MAGIC_OFF]);
00297    if (magic != (uint32_t) AU_MAGIC) {
00298       ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
00299    }
00300    hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
00301    if (hdr_size < AU_HEADER_SIZE) {
00302       hdr_size = AU_HEADER_SIZE;
00303    }
00304 /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
00305    encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
00306    if (encoding != AU_ENC_8BIT_ULAW) {
00307       ast_log(LOG_WARNING, "Unexpected format: %u. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
00308       return -1;
00309    }
00310    sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
00311    if (sample_rate != DEFAULT_SAMPLE_RATE) {
00312       ast_log(LOG_WARNING, "Sample rate can only be 8000 not %u\n", sample_rate);
00313       return -1;
00314    }
00315    channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
00316    if (channels != 1) {
00317       ast_log(LOG_WARNING, "Not in mono: channels=%u\n", channels);
00318       return -1;
00319    }
00320    /* Skip to data */
00321    fseek(f, 0, SEEK_END);
00322    data_size = ftell(f) - hdr_size;
00323    if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
00324       ast_log(LOG_WARNING, "Failed to skip to data: %u\n", hdr_size);
00325       return -1;
00326    }
00327    return data_size;
00328 }

static int load_module ( void   )  [static]

Definition at line 520 of file format_pcm.c.

References ARRAY_LEN, ast_format_register, AST_LIN2A, AST_LIN2MU, AST_MODULE_LOAD_FAILURE, and AST_MODULE_LOAD_SUCCESS.

00521 {
00522    int i;
00523 
00524    /* XXX better init ? */
00525    for (i = 0; i < ARRAY_LEN(ulaw_silence); i++)
00526       ulaw_silence[i] = AST_LIN2MU(0);
00527    for (i = 0; i < ARRAY_LEN(alaw_silence); i++)
00528       alaw_silence[i] = AST_LIN2A(0);
00529 
00530    if ( ast_format_register(&pcm_f)
00531       || ast_format_register(&alaw_f)
00532       || ast_format_register(&au_f)
00533       || ast_format_register(&g722_f) )
00534       return AST_MODULE_LOAD_FAILURE;
00535    return AST_MODULE_LOAD_SUCCESS;
00536 }

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

Definition at line 80 of file format_pcm.c.

References AST_FORMAT_G722, AST_FRAME_SET_BUFFER, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_filestream::buf, BUF_SIZE, ast_frame_subclass::codec, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_filestream::fmt, ast_format::format, ast_filestream::fr, ast_frame::frametype, LOG_WARNING, ast_frame::mallocd, ast_frame::ptr, ast_frame::samples, and ast_frame::subclass.

00081 {
00082    int res;
00083    
00084    /* Send a frame from the file to the appropriate channel */
00085 
00086    s->fr.frametype = AST_FRAME_VOICE;
00087    s->fr.subclass.codec = s->fmt->format;
00088    s->fr.mallocd = 0;
00089    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
00090    if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
00091       if (res)
00092          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00093       return NULL;
00094    }
00095    s->fr.datalen = res;
00096    if (s->fmt->format == AST_FORMAT_G722)
00097       *whennext = s->fr.samples = res * 2;
00098    else
00099       *whennext = s->fr.samples = res;
00100    return &s->fr;
00101 }

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

Definition at line 103 of file format_pcm.c.

References AST_FORMAT_ALAW, ast_log(), AST_LOG_WARNING, BUF_SIZE, errno, ast_filestream::f, ast_filestream::fmt, ast_format::format, LOG_WARNING, ast_frame::offset, SEEK_FORCECUR, and ast_frame::src.

00104 {
00105    off_t cur, max, offset = 0;
00106    int ret = -1;  /* assume error */
00107 
00108    if ((cur = ftello(fs->f)) < 0) {
00109       ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
00110       return -1;
00111    }
00112 
00113    if (fseeko(fs->f, 0, SEEK_END) < 0) {
00114       ast_log(AST_LOG_WARNING, "Unable to seek to end of pcm filestream %p: %s\n", fs, strerror(errno));
00115       return -1;
00116    }
00117 
00118    if ((max = ftello(fs->f)) < 0) {
00119       ast_log(AST_LOG_WARNING, "Unable to determine max position in pcm filestream %p: %s\n", fs, strerror(errno));
00120       return -1;
00121    }
00122 
00123    switch (whence) {
00124    case SEEK_SET:
00125       offset = sample_offset;
00126       break;
00127    case SEEK_END:
00128       offset = max - sample_offset;
00129       break;
00130    case SEEK_CUR:
00131    case SEEK_FORCECUR:
00132       offset = cur + sample_offset;
00133       break;
00134    default:
00135       ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
00136       offset = sample_offset;
00137    }
00138    if (offset < 0) {
00139       ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
00140       offset = 0;
00141    }
00142    if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
00143       size_t left = offset - max;
00144       const char *src = (fs->fmt->format == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence;
00145 
00146       while (left) {
00147          size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
00148          if (written == -1)
00149             break;   /* error */
00150          left -= written;
00151       }
00152       ret = 0; /* successful */
00153    } else {
00154       if (offset > max) {
00155          ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
00156          offset = max;
00157       }
00158       ret = fseeko(fs->f, offset, SEEK_SET);
00159    }
00160    return ret;
00161 }

static off_t pcm_tell ( struct ast_filestream fs  )  [static]

Definition at line 179 of file format_pcm.c.

References ast_filestream::f.

00180 {
00181    return ftello(fs->f);
00182 }

static int pcm_trunc ( struct ast_filestream fs  )  [static]

Definition at line 163 of file format_pcm.c.

References ast_log(), AST_LOG_WARNING, errno, and ast_filestream::f.

00164 {
00165    int cur, fd;
00166 
00167    if ((fd = fileno(fs->f)) < 0) {
00168       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for pcm filestream %p: %s\n", fs, strerror(errno));
00169       return -1;
00170    }
00171    if ((cur = ftello(fs->f)) < 0) {
00172       ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
00173       return -1;
00174    }
00175    /* Truncate file to current length */
00176    return ftruncate(fd, cur);
00177 }

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

Definition at line 184 of file format_pcm.c.

References ast_filestream::_private, AST_FORMAT_ALAW, AST_FRAME_VOICE, ast_getformatname(), ast_log(), ast_frame_subclass::codec, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_filestream::fmt, ast_format::format, ast_frame::frametype, LOG_WARNING, ast_frame::ptr, and ast_frame::subclass.

00185 {
00186    int res;
00187 
00188    if (f->frametype != AST_FRAME_VOICE) {
00189       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00190       return -1;
00191    }
00192    if (f->subclass.codec != fs->fmt->format) {
00193       ast_log(LOG_WARNING, "Asked to write incompatible format frame (%s)!\n", ast_getformatname(f->subclass.codec));
00194       return -1;
00195    }
00196 
00197 #ifdef REALTIME_WRITE
00198    if (s->fmt->format == AST_FORMAT_ALAW) {
00199       struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
00200       struct stat stat_buf;
00201       unsigned long cur_time = get_time();
00202       unsigned long fpos = ( cur_time - pd->start_time ) * 8;  /* 8 bytes per msec */
00203       /* Check if we have written to this position yet. If we have, then increment pos by one frame
00204       *  for some degree of protection against receiving packets in the same clock tick.
00205       */
00206       
00207       fstat(fileno(fs->f), &stat_buf );
00208       if (stat_buf.st_size > fpos )
00209          fpos += f->datalen;  /* Incrementing with the size of this current frame */
00210 
00211       if (stat_buf.st_size < fpos) {
00212          /* fill the gap with 0x55 rather than 0. */
00213          char buf[1024];
00214          unsigned long cur, to_write;
00215 
00216          cur = stat_buf.st_size;
00217          if (fseek(fs->f, cur, SEEK_SET) < 0) {
00218             ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
00219             return -1;
00220          }
00221          memset(buf, 0x55, 512);
00222          while (cur < fpos) {
00223             to_write = fpos - cur;
00224             if (to_write > sizeof(buf))
00225                to_write = sizeof(buf);
00226             fwrite(buf, 1, to_write, fs->f);
00227             cur += to_write;
00228          }
00229       }
00230 
00231       if (fseek(s->f, fpos, SEEK_SET) < 0) {
00232          ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
00233          return -1;
00234       }
00235    }
00236 #endif   /* REALTIME_WRITE */
00237    
00238    if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00239       ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00240       return -1;
00241    }
00242    return 0;
00243 }

static int unload_module ( void   )  [static]

Definition at line 538 of file format_pcm.c.

References ast_format_unregister(), and ast_format::name.

static int update_header ( FILE *  f  )  [static]

Definition at line 330 of file format_pcm.c.

References ast_log(), AU_HDR_DATA_SIZE_OFF, AU_HEADER_SIZE, htoll, and LOG_WARNING.

Referenced by au_trunc().

00331 {
00332    off_t cur, end;
00333    uint32_t datalen;
00334    int bytes;
00335 
00336    cur = ftell(f);
00337    fseek(f, 0, SEEK_END);
00338    end = ftell(f);
00339    /* data starts 24 bytes in */
00340    bytes = end - AU_HEADER_SIZE;
00341    datalen = htoll(bytes);
00342 
00343    if (cur < 0) {
00344       ast_log(LOG_WARNING, "Unable to find our position\n");
00345       return -1;
00346    }
00347    if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
00348       ast_log(LOG_WARNING, "Unable to set our position\n");
00349       return -1;
00350    }
00351    if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
00352       ast_log(LOG_WARNING, "Unable to set write file size\n");
00353       return -1;
00354    }
00355    if (fseek(f, cur, SEEK_SET)) {
00356       ast_log(LOG_WARNING, "Unable to return to position\n");
00357       return -1;
00358    }
00359    return 0;
00360 }

static int write_header ( FILE *  f  )  [static]

Definition at line 362 of file format_pcm.c.

References ast_log(), AU_ENC_8BIT_ULAW, AU_HDR_CHANNELS_OFF, AU_HDR_DATA_SIZE_OFF, AU_HDR_ENCODING_OFF, AU_HDR_HDR_SIZE_OFF, AU_HDR_MAGIC_OFF, AU_HDR_SAMPLE_RATE_OFF, AU_HEADER, AU_HEADER_SIZE, AU_MAGIC, DEFAULT_SAMPLE_RATE, htoll, and LOG_WARNING.

Referenced by au_rewrite().

00363 {
00364    AU_HEADER(header);
00365 
00366    header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
00367    header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
00368    header[AU_HDR_DATA_SIZE_OFF] = 0;
00369    header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
00370    header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
00371    header[AU_HDR_CHANNELS_OFF] = htoll(1);
00372 
00373    /* Write an au header, ignoring sizes which will be filled in later */
00374    fseek(f, 0, SEEK_SET);
00375    if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
00376       ast_log(LOG_WARNING, "Unable to write header\n");
00377       return -1;
00378    }
00379    return 0;
00380 }


Variable Documentation

struct ast_format alaw_f [static]

Definition at line 465 of file format_pcm.c.

char alaw_silence[BUF_SIZE] [static]

Definition at line 44 of file format_pcm.c.

struct ast_format au_f [static]

Definition at line 506 of file format_pcm.c.

struct ast_format g722_f [static]

Definition at line 494 of file format_pcm.c.

struct ast_format pcm_f [static]

Definition at line 482 of file format_pcm.c.

char ulaw_silence[BUF_SIZE] [static]

Definition at line 43 of file format_pcm.c.


Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1