I thought I was going to be able to leave 1.4 alone, but that was not the case.
[asterisk-bristuff.git] / formats / format_pcm.c
blob7b186e6021278bf25a20756d2e8451383c4bafab
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;
290 /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
291 encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
292 if (encoding != AU_ENC_8BIT_ULAW) {
293 ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
294 return -1;
296 sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
297 if (sample_rate != DEFAULT_SAMPLE_RATE) {
298 ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
299 return -1;
301 channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
302 if (channels != 1) {
303 ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
304 return -1;
306 /* Skip to data */
307 fseek(f, 0, SEEK_END);
308 data_size = ftell(f) - hdr_size;
309 if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
310 ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
311 return -1;
313 return data_size;
316 static int update_header(FILE *f)
318 off_t cur, end;
319 uint32_t datalen;
320 int bytes;
322 cur = ftell(f);
323 fseek(f, 0, SEEK_END);
324 end = ftell(f);
325 /* data starts 24 bytes in */
326 bytes = end - AU_HEADER_SIZE;
327 datalen = htoll(bytes);
329 if (cur < 0) {
330 ast_log(LOG_WARNING, "Unable to find our position\n");
331 return -1;
333 if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
334 ast_log(LOG_WARNING, "Unable to set our position\n");
335 return -1;
337 if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
338 ast_log(LOG_WARNING, "Unable to set write file size\n");
339 return -1;
341 if (fseek(f, cur, SEEK_SET)) {
342 ast_log(LOG_WARNING, "Unable to return to position\n");
343 return -1;
345 return 0;
348 static int write_header(FILE *f)
350 AU_HEADER(header);
352 header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
353 header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
354 header[AU_HDR_DATA_SIZE_OFF] = 0;
355 header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
356 header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
357 header[AU_HDR_CHANNELS_OFF] = htoll(1);
359 /* Write an au header, ignoring sizes which will be filled in later */
360 fseek(f, 0, SEEK_SET);
361 if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
362 ast_log(LOG_WARNING, "Unable to write header\n");
363 return -1;
365 return 0;
368 static int au_open(struct ast_filestream *s)
370 if (check_header(s->f) < 0)
371 return -1;
372 return 0;
375 static int au_rewrite(struct ast_filestream *s, const char *comment)
377 if (write_header(s->f))
378 return -1;
379 return 0;
382 /* XXX check this, probably incorrect */
383 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
385 off_t min, max, cur;
386 long offset = 0, bytes;
388 if (fs->fmt->format == AST_FORMAT_G722)
389 bytes = sample_offset / 2;
390 else
391 bytes = sample_offset;
393 min = AU_HEADER_SIZE;
394 cur = ftello(fs->f);
395 fseek(fs->f, 0, SEEK_END);
396 max = ftello(fs->f);
398 if (whence == SEEK_SET)
399 offset = bytes + min;
400 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
401 offset = bytes + cur;
402 else if (whence == SEEK_END)
403 offset = max - bytes;
404 if (whence != SEEK_FORCECUR) {
405 offset = (offset > max) ? max : offset;
408 /* always protect the header space. */
409 offset = (offset < min) ? min : offset;
411 return fseeko(fs->f, offset, SEEK_SET);
414 static int au_trunc(struct ast_filestream *fs)
416 if (ftruncate(fileno(fs->f), ftell(fs->f)))
417 return -1;
418 return update_header(fs->f);
421 static off_t au_tell(struct ast_filestream *fs)
423 off_t offset = ftello(fs->f);
424 return offset - AU_HEADER_SIZE;
427 static const struct ast_format alaw_f = {
428 .name = "alaw",
429 .exts = "alaw|al",
430 .format = AST_FORMAT_ALAW,
431 .write = pcm_write,
432 .seek = pcm_seek,
433 .trunc = pcm_trunc,
434 .tell = pcm_tell,
435 .read = pcm_read,
436 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
437 #ifdef REALTIME_WRITE
438 .open = pcma_open,
439 .rewrite = pcma_rewrite,
440 .desc_size = sizeof(struct pcm_desc),
441 #endif
444 static const struct ast_format pcm_f = {
445 .name = "pcm",
446 .exts = "pcm|ulaw|ul|mu",
447 .format = AST_FORMAT_ULAW,
448 .write = pcm_write,
449 .seek = pcm_seek,
450 .trunc = pcm_trunc,
451 .tell = pcm_tell,
452 .read = pcm_read,
453 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
456 static const struct ast_format g722_f = {
457 .name = "g722",
458 .exts = "g722",
459 .format = AST_FORMAT_G722,
460 .write = pcm_write,
461 .seek = pcm_seek,
462 .trunc = pcm_trunc,
463 .tell = pcm_tell,
464 .read = pcm_read,
465 .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
468 static const struct ast_format au_f = {
469 .name = "au",
470 .exts = "au",
471 .format = AST_FORMAT_ULAW,
472 .open = au_open,
473 .rewrite = au_rewrite,
474 .write = pcm_write,
475 .seek = au_seek,
476 .trunc = au_trunc,
477 .tell = au_tell,
478 .read = pcm_read,
479 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
482 static int load_module(void)
484 int index;
486 /* XXX better init ? */
487 for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
488 ulaw_silence[index] = AST_LIN2MU(0);
489 for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
490 alaw_silence[index] = AST_LIN2A(0);
492 return ast_format_register(&pcm_f)
493 || ast_format_register(&alaw_f)
494 || ast_format_register(&au_f)
495 || ast_format_register(&g722_f);
498 static int unload_module(void)
500 return ast_format_unregister(pcm_f.name)
501 || ast_format_unregister(alaw_f.name)
502 || ast_format_unregister(au_f.name)
503 || ast_format_unregister(g722_f.name);
506 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz");