2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Flat, binary, ulaw PCM file format.
22 * \arg File name extension: pcm, ulaw, ul, mu
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
47 #include "asterisk/ulaw.h"
48 #include "asterisk/alaw.h"
50 #define BUF_SIZE 160 /* 160 bytes, and same number of samples */
52 static char ulaw_silence[BUF_SIZE];
53 static char alaw_silence[BUF_SIZE];
55 /* #define REALTIME_WRITE */ /* XXX does it work at all ? */
59 unsigned long start_time;
62 /* Returns time in msec since system boot. */
63 static unsigned long get_time(void)
70 ast_log( LOG_WARNING, "Cannot get current time\n" );
73 return cur * 1000 / sysconf( _SC_CLK_TCK );
76 static int pcma_open(struct ast_filestream *s)
78 if (s->fmt->format == AST_FORMAT_ALAW)
79 pd->starttime = get_time();
83 static int pcma_rewrite(struct ast_filestream *s, const char *comment)
89 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
93 /* Send a frame from the file to the appropriate channel */
95 s->fr.frametype = AST_FRAME_VOICE;
96 s->fr.subclass = s->fmt->format;
98 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
99 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
101 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
105 *whennext = s->fr.samples = res;
109 static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
111 off_t cur, max, offset = 0;
112 int ret = -1; /* assume error */
115 fseeko(fs->f, 0, SEEK_END);
120 offset = sample_offset;
123 offset = max - sample_offset;
127 offset = cur + sample_offset;
130 ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
131 offset = sample_offset;
134 ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
137 if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
138 size_t left = offset - max;
139 const char *src = (fs->fmt->format == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence;
142 size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
147 ret = 0; /* successful */
150 ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
153 ret = fseeko(fs->f, offset, SEEK_SET);
158 static int pcm_trunc(struct ast_filestream *fs)
160 return ftruncate(fileno(fs->f), ftello(fs->f));
163 static off_t pcm_tell(struct ast_filestream *fs)
165 return ftello(fs->f);
168 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
172 if (f->frametype != AST_FRAME_VOICE) {
173 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
176 if (f->subclass != fs->fmt->format) {
177 ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass);
181 #ifdef REALTIME_WRITE
182 if (s->fmt->format == AST_FORMAT_ALAW) {
183 struct pcm_desc *pd = (struct pcm_desc *)fs->private;
184 struct stat stat_buf;
185 unsigned long cur_time = get_time();
186 unsigned long fpos = ( cur_time - pd->start_time ) * 8; /* 8 bytes per msec */
187 /* Check if we have written to this position yet. If we have, then increment pos by one frame
188 * for some degree of protection against receiving packets in the same clock tick.
191 fstat(fileno(fs->f), &stat_buf );
192 if (stat_buf.st_size > fpos )
193 fpos += f->datalen; /* Incrementing with the size of this current frame */
195 if (stat_buf.st_size < fpos) {
196 /* fill the gap with 0x55 rather than 0. */
198 unsigned long cur, to_write;
200 cur = stat_buf.st_size;
201 if (fseek(fs->f, cur, SEEK_SET) < 0) {
202 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
205 memset(buf, 0x55, 512);
207 to_write = fpos - cur;
208 if (to_write > sizeof(buf))
209 to_write = sizeof(buf);
210 fwrite(buf, 1, to_write, fs->f);
215 if (fseek(s->f, fpos, SEEK_SET) < 0) {
216 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
220 #endif /* REALTIME_WRITE */
222 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
223 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
229 /* SUN .au support routines */
231 #define AU_HEADER_SIZE 24
232 #define AU_HEADER(var) uint32_t var[6]
234 #define AU_HDR_MAGIC_OFF 0
235 #define AU_HDR_HDR_SIZE_OFF 1
236 #define AU_HDR_DATA_SIZE_OFF 2
237 #define AU_HDR_ENCODING_OFF 3
238 #define AU_HDR_SAMPLE_RATE_OFF 4
239 #define AU_HDR_CHANNELS_OFF 5
241 #define AU_ENC_8BIT_ULAW 1
243 #define AU_MAGIC 0x2e736e64
244 #if __BYTE_ORDER == __BIG_ENDIAN
250 #if __BYTE_ORDER == __LITTLE_ENDIAN
252 (((((b) ) & 0xFF) << 24) | \
253 ((((b) >> 8) & 0xFF) << 16) | \
254 ((((b) >> 16) & 0xFF) << 8) | \
255 ((((b) >> 24) & 0xFF) ))
257 (((((b) ) & 0xFF) << 8) | \
258 ((((b) >> 8) & 0xFF) ))
259 #define ltohl(b) htoll(b)
260 #define ltohs(b) htols(b)
262 #error "Endianess not defined"
266 static int check_header(FILE *f)
273 uint32_t sample_rate;
276 if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
277 ast_log(LOG_WARNING, "Read failed (header)\n");
280 magic = ltohl(header[AU_HDR_MAGIC_OFF]);
281 if (magic != (uint32_t) AU_MAGIC) {
282 ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
284 /* hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
285 if (hdr_size < AU_HEADER_SIZE)*/
286 hdr_size = AU_HEADER_SIZE;
287 /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
288 encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
289 if (encoding != AU_ENC_8BIT_ULAW) {
290 ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
293 sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
294 if (sample_rate != DEFAULT_SAMPLE_RATE) {
295 ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
298 channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
300 ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
304 fseek(f, 0, SEEK_END);
305 data_size = ftell(f) - hdr_size;
306 if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
307 ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
313 static int update_header(FILE *f)
320 fseek(f, 0, SEEK_END);
322 /* data starts 24 bytes in */
323 bytes = end - AU_HEADER_SIZE;
324 datalen = htoll(bytes);
327 ast_log(LOG_WARNING, "Unable to find our position\n");
330 if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
331 ast_log(LOG_WARNING, "Unable to set our position\n");
334 if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
335 ast_log(LOG_WARNING, "Unable to set write file size\n");
338 if (fseek(f, cur, SEEK_SET)) {
339 ast_log(LOG_WARNING, "Unable to return to position\n");
345 static int write_header(FILE *f)
349 header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
350 header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
351 header[AU_HDR_DATA_SIZE_OFF] = 0;
352 header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
353 header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
354 header[AU_HDR_CHANNELS_OFF] = htoll(1);
356 /* Write an au header, ignoring sizes which will be filled in later */
357 fseek(f, 0, SEEK_SET);
358 if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
359 ast_log(LOG_WARNING, "Unable to write header\n");
365 static int au_open(struct ast_filestream *s)
367 if (check_header(s->f) < 0)
372 static int au_rewrite(struct ast_filestream *s, const char *comment)
374 if (write_header(s->f))
379 /* XXX check this, probably incorrect */
380 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
383 long offset = 0, samples;
385 samples = sample_offset;
386 min = AU_HEADER_SIZE;
388 fseek(fs->f, 0, SEEK_END);
390 if (whence == SEEK_SET)
391 offset = samples + min;
392 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
393 offset = samples + cur;
394 else if (whence == SEEK_END)
395 offset = max - samples;
396 if (whence != SEEK_FORCECUR) {
397 offset = (offset > max) ? max : offset;
399 /* always protect the header space. */
400 offset = (offset < min) ? min : offset;
401 return fseeko(fs->f, offset, SEEK_SET);
404 static int au_trunc(struct ast_filestream *fs)
406 if (ftruncate(fileno(fs->f), ftell(fs->f)))
408 return update_header(fs->f);
411 static off_t au_tell(struct ast_filestream *fs)
413 off_t offset = ftello(fs->f);
414 return offset - AU_HEADER_SIZE;
417 static const struct ast_format alaw_f = {
420 .format = AST_FORMAT_ALAW,
426 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
427 #ifdef REALTIME_WRITE
429 .rewrite = pcma_rewrite,
430 .desc_size = sizeof(struct pcm_desc),
434 static const struct ast_format pcm_f = {
436 .exts = "pcm|ulaw|ul|mu",
437 .format = AST_FORMAT_ULAW,
443 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
446 static const struct ast_format g722_f = {
449 .format = AST_FORMAT_G722,
455 .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
458 static const struct ast_format au_f = {
461 .format = AST_FORMAT_ULAW,
463 .rewrite = au_rewrite,
469 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
472 static int load_module(void)
476 /* XXX better init ? */
477 for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
478 ulaw_silence[index] = AST_LIN2MU(0);
479 for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
480 alaw_silence[index] = AST_LIN2A(0);
482 return ast_format_register(&pcm_f)
483 || ast_format_register(&alaw_f)
484 || ast_format_register(&au_f)
485 || ast_format_register(&g722_f);
488 static int unload_module(void)
490 return ast_format_unregister(pcm_f.name)
491 || ast_format_unregister(alaw_f.name)
492 || ast_format_unregister(au_f.name)
493 || ast_format_unregister(g722_f.name);
496 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz");