appending one list to another should leave the first list empty, and not require...
[asterisk-bristuff.git] / formats / format_pcm.c
blob6736ea99693ef5962ee65ece6a512359d24bca87
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 *whennext = s->fr.samples = res;
106 return &s->fr;
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 */
114 cur = ftello(fs->f);
115 fseeko(fs->f, 0, SEEK_END);
116 max = ftello(fs->f);
118 switch (whence) {
119 case SEEK_SET:
120 offset = sample_offset;
121 break;
122 case SEEK_END:
123 offset = max - sample_offset;
124 break;
125 case SEEK_CUR:
126 case SEEK_FORCECUR:
127 offset = cur + sample_offset;
128 break;
129 default:
130 ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
131 offset = sample_offset;
133 if (offset < 0) {
134 ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
135 offset = 0;
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;
141 while (left) {
142 size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
143 if (written == -1)
144 break; /* error */
145 left -= written;
147 ret = 0; /* successful */
148 } else {
149 if (offset > max) {
150 ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
151 offset = max;
153 ret = fseeko(fs->f, offset, SEEK_SET);
155 return ret;
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)
170 int res;
172 if (f->frametype != AST_FRAME_VOICE) {
173 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
174 return -1;
176 if (f->subclass != fs->fmt->format) {
177 ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass);
178 return -1;
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. */
197 char buf[1024];
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) );
203 return -1;
205 memset(buf, 0x55, 512);
206 while (cur < fpos) {
207 to_write = fpos - cur;
208 if (to_write > sizeof(buf))
209 to_write = sizeof(buf);
210 fwrite(buf, 1, to_write, fs->f);
211 cur += to_write;
215 if (fseek(s->f, fpos, SEEK_SET) < 0) {
216 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
217 return -1;
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));
224 return -1;
226 return 0;
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
245 #define htoll(b) (b)
246 #define htols(b) (b)
247 #define ltohl(b) (b)
248 #define ltohs(b) (b)
249 #else
250 #if __BYTE_ORDER == __LITTLE_ENDIAN
251 #define htoll(b) \
252 (((((b) ) & 0xFF) << 24) | \
253 ((((b) >> 8) & 0xFF) << 16) | \
254 ((((b) >> 16) & 0xFF) << 8) | \
255 ((((b) >> 24) & 0xFF) ))
256 #define htols(b) \
257 (((((b) ) & 0xFF) << 8) | \
258 ((((b) >> 8) & 0xFF) ))
259 #define ltohl(b) htoll(b)
260 #define ltohs(b) htols(b)
261 #else
262 #error "Endianess not defined"
263 #endif
264 #endif
266 static int check_header(FILE *f)
268 AU_HEADER(header);
269 uint32_t magic;
270 uint32_t hdr_size;
271 uint32_t data_size;
272 uint32_t encoding;
273 uint32_t sample_rate;
274 uint32_t channels;
276 if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
277 ast_log(LOG_WARNING, "Read failed (header)\n");
278 return -1;
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);
291 return -1;
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);
296 return -1;
298 channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
299 if (channels != 1) {
300 ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
301 return -1;
303 /* Skip to data */
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);
308 return -1;
310 return data_size;
313 static int update_header(FILE *f)
315 off_t cur, end;
316 uint32_t datalen;
317 int bytes;
319 cur = ftell(f);
320 fseek(f, 0, SEEK_END);
321 end = ftell(f);
322 /* data starts 24 bytes in */
323 bytes = end - AU_HEADER_SIZE;
324 datalen = htoll(bytes);
326 if (cur < 0) {
327 ast_log(LOG_WARNING, "Unable to find our position\n");
328 return -1;
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");
332 return -1;
334 if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
335 ast_log(LOG_WARNING, "Unable to set write file size\n");
336 return -1;
338 if (fseek(f, cur, SEEK_SET)) {
339 ast_log(LOG_WARNING, "Unable to return to position\n");
340 return -1;
342 return 0;
345 static int write_header(FILE *f)
347 AU_HEADER(header);
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");
360 return -1;
362 return 0;
365 static int au_open(struct ast_filestream *s)
367 if (check_header(s->f) < 0)
368 return -1;
369 return 0;
372 static int au_rewrite(struct ast_filestream *s, const char *comment)
374 if (write_header(s->f))
375 return -1;
376 return 0;
379 /* XXX check this, probably incorrect */
380 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
382 off_t min, max, cur;
383 long offset = 0, samples;
385 samples = sample_offset;
386 min = AU_HEADER_SIZE;
387 cur = ftello(fs->f);
388 fseek(fs->f, 0, SEEK_END);
389 max = ftello(fs->f);
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)))
407 return -1;
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 = {
418 .name = "alaw",
419 .exts = "alaw|al",
420 .format = AST_FORMAT_ALAW,
421 .write = pcm_write,
422 .seek = pcm_seek,
423 .trunc = pcm_trunc,
424 .tell = pcm_tell,
425 .read = pcm_read,
426 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
427 #ifdef REALTIME_WRITE
428 .open = pcma_open,
429 .rewrite = pcma_rewrite,
430 .desc_size = sizeof(struct pcm_desc),
431 #endif
434 static const struct ast_format pcm_f = {
435 .name = "pcm",
436 .exts = "pcm|ulaw|ul|mu",
437 .format = AST_FORMAT_ULAW,
438 .write = pcm_write,
439 .seek = pcm_seek,
440 .trunc = pcm_trunc,
441 .tell = pcm_tell,
442 .read = pcm_read,
443 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
446 static const struct ast_format g722_f = {
447 .name = "g722",
448 .exts = "g722",
449 .format = AST_FORMAT_G722,
450 .write = pcm_write,
451 .seek = pcm_seek,
452 .trunc = pcm_trunc,
453 .tell = pcm_tell,
454 .read = pcm_read,
455 .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
458 static const struct ast_format au_f = {
459 .name = "au",
460 .exts = "au",
461 .format = AST_FORMAT_ULAW,
462 .open = au_open,
463 .rewrite = au_rewrite,
464 .write = pcm_write,
465 .seek = au_seek,
466 .trunc = au_trunc,
467 .tell = au_tell,
468 .read = pcm_read,
469 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
472 static int load_module(void)
474 int index;
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");