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 if (s
->fmt
->format
== AST_FORMAT_G722
)
106 *whennext
= s
->fr
.samples
= res
* 2;
108 *whennext
= s
->fr
.samples
= res
;
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 */
118 fseeko(fs
->f
, 0, SEEK_END
);
123 offset
= sample_offset
;
126 offset
= max
- sample_offset
;
130 offset
= cur
+ sample_offset
;
133 ast_log(LOG_WARNING
, "invalid whence %d, assuming SEEK_SET\n", whence
);
134 offset
= sample_offset
;
137 ast_log(LOG_WARNING
, "negative offset %ld, resetting to 0\n", (long) offset
);
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
;
145 size_t written
= fwrite(src
, 1, (left
> BUF_SIZE
) ? BUF_SIZE
: left
, fs
->f
);
150 ret
= 0; /* successful */
153 ast_log(LOG_WARNING
, "offset too large %ld, truncating to %ld\n", (long) offset
, (long) max
);
156 ret
= fseeko(fs
->f
, offset
, SEEK_SET
);
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
)
175 if (f
->frametype
!= AST_FRAME_VOICE
) {
176 ast_log(LOG_WARNING
, "Asked to write non-voice frame!\n");
179 if (f
->subclass
!= fs
->fmt
->format
) {
180 ast_log(LOG_WARNING
, "Asked to write incompatible format frame (%d)!\n", f
->subclass
);
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. */
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
) );
208 memset(buf
, 0x55, 512);
210 to_write
= fpos
- cur
;
211 if (to_write
> sizeof(buf
))
212 to_write
= sizeof(buf
);
213 fwrite(buf
, 1, to_write
, fs
->f
);
218 if (fseek(s
->f
, fpos
, SEEK_SET
) < 0) {
219 ast_log( LOG_WARNING
, "Cannot seek in file: %s\n", strerror(errno
) );
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
));
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
253 #if __BYTE_ORDER == __LITTLE_ENDIAN
255 (((((b) ) & 0xFF) << 24) | \
256 ((((b) >> 8) & 0xFF) << 16) | \
257 ((((b) >> 16) & 0xFF) << 8) | \
258 ((((b) >> 24) & 0xFF) ))
260 (((((b) ) & 0xFF) << 8) | \
261 ((((b) >> 8) & 0xFF) ))
262 #define ltohl(b) htoll(b)
263 #define ltohs(b) htols(b)
265 #error "Endianess not defined"
269 static int check_header(FILE *f
)
276 uint32_t sample_rate
;
279 if (fread(header
, 1, AU_HEADER_SIZE
, f
) != AU_HEADER_SIZE
) {
280 ast_log(LOG_WARNING
, "Read failed (header)\n");
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
);
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
);
302 channels
= ltohl(header
[AU_HDR_CHANNELS_OFF
]);
304 ast_log(LOG_WARNING
, "Not in mono: channels=%d\n", channels
);
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
);
317 static int update_header(FILE *f
)
324 fseek(f
, 0, SEEK_END
);
326 /* data starts 24 bytes in */
327 bytes
= end
- AU_HEADER_SIZE
;
328 datalen
= htoll(bytes
);
331 ast_log(LOG_WARNING
, "Unable to find our position\n");
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");
338 if (fwrite(&datalen
, 1, sizeof(datalen
), f
) != sizeof(datalen
)) {
339 ast_log(LOG_WARNING
, "Unable to set write file size\n");
342 if (fseek(f
, cur
, SEEK_SET
)) {
343 ast_log(LOG_WARNING
, "Unable to return to position\n");
349 static int write_header(FILE *f
)
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");
369 static int au_open(struct ast_filestream
*s
)
371 if (check_header(s
->f
) < 0)
376 static int au_rewrite(struct ast_filestream
*s
, const char *comment
)
378 if (write_header(s
->f
))
383 /* XXX check this, probably incorrect */
384 static int au_seek(struct ast_filestream
*fs
, off_t sample_offset
, int whence
)
387 long offset
= 0, bytes
;
389 if (fs
->fmt
->format
== AST_FORMAT_G722
)
390 bytes
= sample_offset
/ 2;
392 bytes
= sample_offset
;
394 min
= AU_HEADER_SIZE
;
396 fseek(fs
->f
, 0, SEEK_END
);
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
)))
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
= {
431 .format
= AST_FORMAT_ALAW
,
437 .buf_size
= BUF_SIZE
+ AST_FRIENDLY_OFFSET
,
438 #ifdef REALTIME_WRITE
440 .rewrite
= pcma_rewrite
,
441 .desc_size
= sizeof(struct pcm_desc
),
445 static const struct ast_format pcm_f
= {
447 .exts
= "pcm|ulaw|ul|mu",
448 .format
= AST_FORMAT_ULAW
,
454 .buf_size
= BUF_SIZE
+ AST_FRIENDLY_OFFSET
,
457 static const struct ast_format g722_f
= {
460 .format
= AST_FORMAT_G722
,
466 .buf_size
= (BUF_SIZE
* 2) + AST_FRIENDLY_OFFSET
,
469 static const struct ast_format au_f
= {
472 .format
= AST_FORMAT_ULAW
,
474 .rewrite
= au_rewrite
,
480 .buf_size
= BUF_SIZE
+ AST_FRIENDLY_OFFSET
, /* this many shorts */
483 static int load_module(void)
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");