Let's also include aclocal.m4
[asterisk-bristuff.git] / formats / format_pcm.c
bloba3deced7ae04f325d49b2139dd9e4ed012b91baf
1 /*
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.
19 /*! \file
21 * \brief Flat, binary, ulaw PCM file format.
22 * \arg File name extension: pcm, ulaw, ul, mu
24 * \ingroup formats
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <unistd.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.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 ? */
57 #ifdef REALTIME_WRITE
58 struct pcm_desc {
59 unsigned long start_time;
62 /* Returns time in msec since system boot. */
63 static unsigned long get_time(void)
65 struct tms buf;
66 clock_t cur;
68 cur = times( &buf );
69 if( cur < 0 ) {
70 ast_log( LOG_WARNING, "Cannot get current time\n" );
71 return 0;
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();
80 return 0;
83 static int pcma_rewrite(struct ast_filestream *s, const char *comment)
85 return pcma_open(s);
87 #endif
89 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
91 int res;
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;
97 s->fr.mallocd = 0;
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) {
100 if (res)
101 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
102 return NULL;
104 s->fr.datalen = res;
105 if (s->fmt->format == AST_FORMAT_G722)
106 *whennext = s->fr.samples = res * 2;
107 else
108 *whennext = s->fr.samples = res;
109 return &s->fr;
112 static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
114 off_t cur, max, offset = 0;
115 int ret = -1; /* assume error */
117 cur = ftello(fs->f);
118 fseeko(fs->f, 0, SEEK_END);
119 max = ftello(fs->f);
121 switch (whence) {
122 case SEEK_SET:
123 offset = sample_offset;
124 break;
125 case SEEK_END:
126 offset = max - sample_offset;
127 break;
128 case SEEK_CUR:
129 case SEEK_FORCECUR:
130 offset = cur + sample_offset;
131 break;
132 default:
133 ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
134 offset = sample_offset;
136 if (offset < 0) {
137 ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
138 offset = 0;
140 if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
141 size_t left = offset - max;
142 const char *src = (fs->fmt->format == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence;
144 while (left) {
145 size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
146 if (written == -1)
147 break; /* error */
148 left -= written;
150 ret = 0; /* successful */
151 } else {
152 if (offset > max) {
153 ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
154 offset = max;
156 ret = fseeko(fs->f, offset, SEEK_SET);
158 return ret;
161 static int pcm_trunc(struct ast_filestream *fs)
163 return ftruncate(fileno(fs->f), ftello(fs->f));
166 static off_t pcm_tell(struct ast_filestream *fs)
168 return ftello(fs->f);
171 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
173 int res;
175 if (f->frametype != AST_FRAME_VOICE) {
176 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
177 return -1;
179 if (f->subclass != fs->fmt->format) {
180 ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass);
181 return -1;
184 #ifdef REALTIME_WRITE
185 if (s->fmt->format == AST_FORMAT_ALAW) {
186 struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
187 struct stat stat_buf;
188 unsigned long cur_time = get_time();
189 unsigned long fpos = ( cur_time - pd->start_time ) * 8; /* 8 bytes per msec */
190 /* Check if we have written to this position yet. If we have, then increment pos by one frame
191 * for some degree of protection against receiving packets in the same clock tick.
194 fstat(fileno(fs->f), &stat_buf );
195 if (stat_buf.st_size > fpos )
196 fpos += f->datalen; /* Incrementing with the size of this current frame */
198 if (stat_buf.st_size < fpos) {
199 /* fill the gap with 0x55 rather than 0. */
200 char buf[1024];
201 unsigned long cur, to_write;
203 cur = stat_buf.st_size;
204 if (fseek(fs->f, cur, SEEK_SET) < 0) {
205 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
206 return -1;
208 memset(buf, 0x55, 512);
209 while (cur < fpos) {
210 to_write = fpos - cur;
211 if (to_write > sizeof(buf))
212 to_write = sizeof(buf);
213 fwrite(buf, 1, to_write, fs->f);
214 cur += to_write;
218 if (fseek(s->f, fpos, SEEK_SET) < 0) {
219 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
220 return -1;
223 #endif /* REALTIME_WRITE */
225 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
226 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
227 return -1;
229 return 0;
232 /* SUN .au support routines */
234 #define AU_HEADER_SIZE 24
235 #define AU_HEADER(var) uint32_t var[6]
237 #define AU_HDR_MAGIC_OFF 0
238 #define AU_HDR_HDR_SIZE_OFF 1
239 #define AU_HDR_DATA_SIZE_OFF 2
240 #define AU_HDR_ENCODING_OFF 3
241 #define AU_HDR_SAMPLE_RATE_OFF 4
242 #define AU_HDR_CHANNELS_OFF 5
244 #define AU_ENC_8BIT_ULAW 1
246 #define AU_MAGIC 0x2e736e64
247 #if __BYTE_ORDER == __BIG_ENDIAN
248 #define htoll(b) (b)
249 #define htols(b) (b)
250 #define ltohl(b) (b)
251 #define ltohs(b) (b)
252 #else
253 #if __BYTE_ORDER == __LITTLE_ENDIAN
254 #define htoll(b) \
255 (((((b) ) & 0xFF) << 24) | \
256 ((((b) >> 8) & 0xFF) << 16) | \
257 ((((b) >> 16) & 0xFF) << 8) | \
258 ((((b) >> 24) & 0xFF) ))
259 #define htols(b) \
260 (((((b) ) & 0xFF) << 8) | \
261 ((((b) >> 8) & 0xFF) ))
262 #define ltohl(b) htoll(b)
263 #define ltohs(b) htols(b)
264 #else
265 #error "Endianess not defined"
266 #endif
267 #endif
269 static int check_header(FILE *f)
271 AU_HEADER(header);
272 uint32_t magic;
273 uint32_t hdr_size;
274 uint32_t data_size;
275 uint32_t encoding;
276 uint32_t sample_rate;
277 uint32_t channels;
279 if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
280 ast_log(LOG_WARNING, "Read failed (header)\n");
281 return -1;
283 magic = ltohl(header[AU_HDR_MAGIC_OFF]);
284 if (magic != (uint32_t) AU_MAGIC) {
285 ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
287 hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
288 if (hdr_size < AU_HEADER_SIZE) {
289 hdr_size = AU_HEADER_SIZE;
291 /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
292 encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
293 if (encoding != AU_ENC_8BIT_ULAW) {
294 ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
295 return -1;
297 sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
298 if (sample_rate != DEFAULT_SAMPLE_RATE) {
299 ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
300 return -1;
302 channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
303 if (channels != 1) {
304 ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
305 return -1;
307 /* Skip to data */
308 fseek(f, 0, SEEK_END);
309 data_size = ftell(f) - hdr_size;
310 if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
311 ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
312 return -1;
314 return data_size;
317 static int update_header(FILE *f)
319 off_t cur, end;
320 uint32_t datalen;
321 int bytes;
323 cur = ftell(f);
324 fseek(f, 0, SEEK_END);
325 end = ftell(f);
326 /* data starts 24 bytes in */
327 bytes = end - AU_HEADER_SIZE;
328 datalen = htoll(bytes);
330 if (cur < 0) {
331 ast_log(LOG_WARNING, "Unable to find our position\n");
332 return -1;
334 if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
335 ast_log(LOG_WARNING, "Unable to set our position\n");
336 return -1;
338 if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
339 ast_log(LOG_WARNING, "Unable to set write file size\n");
340 return -1;
342 if (fseek(f, cur, SEEK_SET)) {
343 ast_log(LOG_WARNING, "Unable to return to position\n");
344 return -1;
346 return 0;
349 static int write_header(FILE *f)
351 AU_HEADER(header);
353 header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
354 header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
355 header[AU_HDR_DATA_SIZE_OFF] = 0;
356 header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
357 header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
358 header[AU_HDR_CHANNELS_OFF] = htoll(1);
360 /* Write an au header, ignoring sizes which will be filled in later */
361 fseek(f, 0, SEEK_SET);
362 if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
363 ast_log(LOG_WARNING, "Unable to write header\n");
364 return -1;
366 return 0;
369 static int au_open(struct ast_filestream *s)
371 if (check_header(s->f) < 0)
372 return -1;
373 return 0;
376 static int au_rewrite(struct ast_filestream *s, const char *comment)
378 if (write_header(s->f))
379 return -1;
380 return 0;
383 /* XXX check this, probably incorrect */
384 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
386 off_t min, max, cur;
387 long offset = 0, bytes;
389 if (fs->fmt->format == AST_FORMAT_G722)
390 bytes = sample_offset / 2;
391 else
392 bytes = sample_offset;
394 min = AU_HEADER_SIZE;
395 cur = ftello(fs->f);
396 fseek(fs->f, 0, SEEK_END);
397 max = ftello(fs->f);
399 if (whence == SEEK_SET)
400 offset = bytes + min;
401 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
402 offset = bytes + cur;
403 else if (whence == SEEK_END)
404 offset = max - bytes;
405 if (whence != SEEK_FORCECUR) {
406 offset = (offset > max) ? max : offset;
409 /* always protect the header space. */
410 offset = (offset < min) ? min : offset;
412 return fseeko(fs->f, offset, SEEK_SET);
415 static int au_trunc(struct ast_filestream *fs)
417 if (ftruncate(fileno(fs->f), ftell(fs->f)))
418 return -1;
419 return update_header(fs->f);
422 static off_t au_tell(struct ast_filestream *fs)
424 off_t offset = ftello(fs->f);
425 return offset - AU_HEADER_SIZE;
428 static const struct ast_format alaw_f = {
429 .name = "alaw",
430 .exts = "alaw|al",
431 .format = AST_FORMAT_ALAW,
432 .write = pcm_write,
433 .seek = pcm_seek,
434 .trunc = pcm_trunc,
435 .tell = pcm_tell,
436 .read = pcm_read,
437 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
438 #ifdef REALTIME_WRITE
439 .open = pcma_open,
440 .rewrite = pcma_rewrite,
441 .desc_size = sizeof(struct pcm_desc),
442 #endif
445 static const struct ast_format pcm_f = {
446 .name = "pcm",
447 .exts = "pcm|ulaw|ul|mu",
448 .format = AST_FORMAT_ULAW,
449 .write = pcm_write,
450 .seek = pcm_seek,
451 .trunc = pcm_trunc,
452 .tell = pcm_tell,
453 .read = pcm_read,
454 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
457 static const struct ast_format g722_f = {
458 .name = "g722",
459 .exts = "g722",
460 .format = AST_FORMAT_G722,
461 .write = pcm_write,
462 .seek = pcm_seek,
463 .trunc = pcm_trunc,
464 .tell = pcm_tell,
465 .read = pcm_read,
466 .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
469 static const struct ast_format au_f = {
470 .name = "au",
471 .exts = "au",
472 .format = AST_FORMAT_ULAW,
473 .open = au_open,
474 .rewrite = au_rewrite,
475 .write = pcm_write,
476 .seek = au_seek,
477 .trunc = au_trunc,
478 .tell = au_tell,
479 .read = pcm_read,
480 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
483 static int load_module(void)
485 int index;
487 /* XXX better init ? */
488 for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
489 ulaw_silence[index] = AST_LIN2MU(0);
490 for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
491 alaw_silence[index] = AST_LIN2A(0);
493 return ast_format_register(&pcm_f)
494 || ast_format_register(&alaw_f)
495 || ast_format_register(&au_f)
496 || ast_format_register(&g722_f);
499 static int unload_module(void)
501 return ast_format_unregister(pcm_f.name)
502 || ast_format_unregister(alaw_f.name)
503 || ast_format_unregister(au_f.name)
504 || ast_format_unregister(g722_f.name);
507 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz");