00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00030
00031 #include <unistd.h>
00032 #include <netinet/in.h>
00033 #include <arpa/inet.h>
00034 #include <stdlib.h>
00035 #include <sys/time.h>
00036 #include <stdio.h>
00037 #include <errno.h>
00038 #include <string.h>
00039
00040 #include "asterisk/lock.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/file.h"
00043 #include "asterisk/logger.h"
00044 #include "asterisk/sched.h"
00045 #include "asterisk/module.h"
00046 #include "asterisk/endian.h"
00047 #include "asterisk/ulaw.h"
00048 #include "asterisk/alaw.h"
00049
00050 #define BUF_SIZE 160
00051
00052 static char ulaw_silence[BUF_SIZE];
00053 static char alaw_silence[BUF_SIZE];
00054
00055
00056
00057 #ifdef REALTIME_WRITE
00058 struct pcm_desc {
00059 unsigned long start_time;
00060 };
00061
00062
00063 static unsigned long get_time(void)
00064 {
00065 struct tms buf;
00066 clock_t cur;
00067
00068 cur = times( &buf );
00069 if( cur < 0 ) {
00070 ast_log( LOG_WARNING, "Cannot get current time\n" );
00071 return 0;
00072 }
00073 return cur * 1000 / sysconf( _SC_CLK_TCK );
00074 }
00075
00076 static int pcma_open(struct ast_filestream *s)
00077 {
00078 if (s->fmt->format == AST_FORMAT_ALAW)
00079 pd->starttime = get_time();
00080 return 0;
00081 }
00082
00083 static int pcma_rewrite(struct ast_filestream *s, const char *comment)
00084 {
00085 return pcma_open(s);
00086 }
00087 #endif
00088
00089 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
00090 {
00091 int res;
00092
00093
00094
00095 s->fr.frametype = AST_FRAME_VOICE;
00096 s->fr.subclass = s->fmt->format;
00097 s->fr.mallocd = 0;
00098 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
00099 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
00100 if (res)
00101 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00102 return NULL;
00103 }
00104 s->fr.datalen = res;
00105 *whennext = s->fr.samples = res;
00106 return &s->fr;
00107 }
00108
00109 static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00110 {
00111 off_t cur, max, offset = 0;
00112 int ret = -1;
00113
00114 cur = ftello(fs->f);
00115 fseeko(fs->f, 0, SEEK_END);
00116 max = ftello(fs->f);
00117
00118 switch (whence) {
00119 case SEEK_SET:
00120 offset = sample_offset;
00121 break;
00122 case SEEK_END:
00123 offset = max - sample_offset;
00124 break;
00125 case SEEK_CUR:
00126 case SEEK_FORCECUR:
00127 offset = cur + sample_offset;
00128 break;
00129 default:
00130 ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
00131 offset = sample_offset;
00132 }
00133 if (offset < 0) {
00134 ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
00135 offset = 0;
00136 }
00137 if (whence == SEEK_FORCECUR && offset > max) {
00138 size_t left = offset - max;
00139 const char *src = (fs->fmt->format == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence;
00140
00141 while (left) {
00142 size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
00143 if (written == -1)
00144 break;
00145 left -= written;
00146 }
00147 ret = 0;
00148 } else {
00149 if (offset > max) {
00150 ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
00151 offset = max;
00152 }
00153 ret = fseeko(fs->f, offset, SEEK_SET);
00154 }
00155 return ret;
00156 }
00157
00158 static int pcm_trunc(struct ast_filestream *fs)
00159 {
00160 return ftruncate(fileno(fs->f), ftello(fs->f));
00161 }
00162
00163 static off_t pcm_tell(struct ast_filestream *fs)
00164 {
00165 return ftello(fs->f);
00166 }
00167
00168 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
00169 {
00170 int res;
00171
00172 if (f->frametype != AST_FRAME_VOICE) {
00173 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00174 return -1;
00175 }
00176 if (f->subclass != fs->fmt->format) {
00177 ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass);
00178 return -1;
00179 }
00180
00181 #ifdef REALTIME_WRITE
00182 if (s->fmt->format == AST_FORMAT_ALAW) {
00183 struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
00184 struct stat stat_buf;
00185 unsigned long cur_time = get_time();
00186 unsigned long fpos = ( cur_time - pd->start_time ) * 8;
00187
00188
00189
00190
00191 fstat(fileno(fs->f), &stat_buf );
00192 if (stat_buf.st_size > fpos )
00193 fpos += f->datalen;
00194
00195 if (stat_buf.st_size < fpos) {
00196
00197 char buf[1024];
00198 unsigned long cur, to_write;
00199
00200 cur = stat_buf.st_size;
00201 if (fseek(fs->f, cur, SEEK_SET) < 0) {
00202 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
00203 return -1;
00204 }
00205 memset(buf, 0x55, 512);
00206 while (cur < fpos) {
00207 to_write = fpos - cur;
00208 if (to_write > sizeof(buf))
00209 to_write = sizeof(buf);
00210 fwrite(buf, 1, to_write, fs->f);
00211 cur += to_write;
00212 }
00213 }
00214
00215 if (fseek(s->f, fpos, SEEK_SET) < 0) {
00216 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
00217 return -1;
00218 }
00219 }
00220 #endif
00221
00222 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00223 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00224 return -1;
00225 }
00226 return 0;
00227 }
00228
00229
00230
00231 #define AU_HEADER_SIZE 24
00232 #define AU_HEADER(var) uint32_t var[6]
00233
00234 #define AU_HDR_MAGIC_OFF 0
00235 #define AU_HDR_HDR_SIZE_OFF 1
00236 #define AU_HDR_DATA_SIZE_OFF 2
00237 #define AU_HDR_ENCODING_OFF 3
00238 #define AU_HDR_SAMPLE_RATE_OFF 4
00239 #define AU_HDR_CHANNELS_OFF 5
00240
00241 #define AU_ENC_8BIT_ULAW 1
00242
00243 #define AU_MAGIC 0x2e736e64
00244 #if __BYTE_ORDER == __BIG_ENDIAN
00245 #define htoll(b) (b)
00246 #define htols(b) (b)
00247 #define ltohl(b) (b)
00248 #define ltohs(b) (b)
00249 #else
00250 #if __BYTE_ORDER == __LITTLE_ENDIAN
00251 #define htoll(b) \
00252 (((((b) ) & 0xFF) << 24) | \
00253 ((((b) >> 8) & 0xFF) << 16) | \
00254 ((((b) >> 16) & 0xFF) << 8) | \
00255 ((((b) >> 24) & 0xFF) ))
00256 #define htols(b) \
00257 (((((b) ) & 0xFF) << 8) | \
00258 ((((b) >> 8) & 0xFF) ))
00259 #define ltohl(b) htoll(b)
00260 #define ltohs(b) htols(b)
00261 #else
00262 #error "Endianess not defined"
00263 #endif
00264 #endif
00265
00266 static int check_header(FILE *f)
00267 {
00268 AU_HEADER(header);
00269 uint32_t magic;
00270 uint32_t hdr_size;
00271 uint32_t data_size;
00272 uint32_t encoding;
00273 uint32_t sample_rate;
00274 uint32_t channels;
00275
00276 if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
00277 ast_log(LOG_WARNING, "Read failed (header)\n");
00278 return -1;
00279 }
00280 magic = ltohl(header[AU_HDR_MAGIC_OFF]);
00281 if (magic != (uint32_t) AU_MAGIC) {
00282 ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
00283 }
00284
00285
00286 hdr_size = AU_HEADER_SIZE;
00287
00288 encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
00289 if (encoding != AU_ENC_8BIT_ULAW) {
00290 ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
00291 return -1;
00292 }
00293 sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
00294 if (sample_rate != DEFAULT_SAMPLE_RATE) {
00295 ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
00296 return -1;
00297 }
00298 channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
00299 if (channels != 1) {
00300 ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
00301 return -1;
00302 }
00303
00304 fseek(f, 0, SEEK_END);
00305 data_size = ftell(f) - hdr_size;
00306 if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
00307 ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
00308 return -1;
00309 }
00310 return data_size;
00311 }
00312
00313 static int update_header(FILE *f)
00314 {
00315 off_t cur, end;
00316 uint32_t datalen;
00317 int bytes;
00318
00319 cur = ftell(f);
00320 fseek(f, 0, SEEK_END);
00321 end = ftell(f);
00322
00323 bytes = end - AU_HEADER_SIZE;
00324 datalen = htoll(bytes);
00325
00326 if (cur < 0) {
00327 ast_log(LOG_WARNING, "Unable to find our position\n");
00328 return -1;
00329 }
00330 if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
00331 ast_log(LOG_WARNING, "Unable to set our position\n");
00332 return -1;
00333 }
00334 if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
00335 ast_log(LOG_WARNING, "Unable to set write file size\n");
00336 return -1;
00337 }
00338 if (fseek(f, cur, SEEK_SET)) {
00339 ast_log(LOG_WARNING, "Unable to return to position\n");
00340 return -1;
00341 }
00342 return 0;
00343 }
00344
00345 static int write_header(FILE *f)
00346 {
00347 AU_HEADER(header);
00348
00349 header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
00350 header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
00351 header[AU_HDR_DATA_SIZE_OFF] = 0;
00352 header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
00353 header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
00354 header[AU_HDR_CHANNELS_OFF] = htoll(1);
00355
00356
00357 fseek(f, 0, SEEK_SET);
00358 if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
00359 ast_log(LOG_WARNING, "Unable to write header\n");
00360 return -1;
00361 }
00362 return 0;
00363 }
00364
00365 static int au_open(struct ast_filestream *s)
00366 {
00367 if (check_header(s->f) < 0)
00368 return -1;
00369 return 0;
00370 }
00371
00372 static int au_rewrite(struct ast_filestream *s, const char *comment)
00373 {
00374 if (write_header(s->f))
00375 return -1;
00376 return 0;
00377 }
00378
00379
00380 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00381 {
00382 off_t min, max, cur;
00383 long offset = 0, samples;
00384
00385 samples = sample_offset;
00386 min = AU_HEADER_SIZE;
00387 cur = ftello(fs->f);
00388 fseek(fs->f, 0, SEEK_END);
00389 max = ftello(fs->f);
00390 if (whence == SEEK_SET)
00391 offset = samples + min;
00392 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00393 offset = samples + cur;
00394 else if (whence == SEEK_END)
00395 offset = max - samples;
00396 if (whence != SEEK_FORCECUR) {
00397 offset = (offset > max) ? max : offset;
00398 }
00399
00400 offset = (offset < min) ? min : offset;
00401 return fseeko(fs->f, offset, SEEK_SET);
00402 }
00403
00404 static int au_trunc(struct ast_filestream *fs)
00405 {
00406 if (ftruncate(fileno(fs->f), ftell(fs->f)))
00407 return -1;
00408 return update_header(fs->f);
00409 }
00410
00411 static off_t au_tell(struct ast_filestream *fs)
00412 {
00413 off_t offset = ftello(fs->f);
00414 return offset - AU_HEADER_SIZE;
00415 }
00416
00417 static const struct ast_format alaw_f = {
00418 .name = "alaw",
00419 .exts = "alaw|al",
00420 .format = AST_FORMAT_ALAW,
00421 .write = pcm_write,
00422 .seek = pcm_seek,
00423 .trunc = pcm_trunc,
00424 .tell = pcm_tell,
00425 .read = pcm_read,
00426 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00427 #ifdef REALTIME_WRITE
00428 .open = pcma_open,
00429 .rewrite = pcma_rewrite,
00430 .desc_size = sizeof(struct pcm_desc),
00431 #endif
00432 };
00433
00434 static const struct ast_format pcm_f = {
00435 .name = "pcm",
00436 .exts = "pcm|ulaw|ul|mu",
00437 .format = AST_FORMAT_ULAW,
00438 .write = pcm_write,
00439 .seek = pcm_seek,
00440 .trunc = pcm_trunc,
00441 .tell = pcm_tell,
00442 .read = pcm_read,
00443 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00444 };
00445
00446 static const struct ast_format g722_f = {
00447 .name = "g722",
00448 .exts = "g722",
00449 .format = AST_FORMAT_G722,
00450 .write = pcm_write,
00451 .seek = pcm_seek,
00452 .trunc = pcm_trunc,
00453 .tell = pcm_tell,
00454 .read = pcm_read,
00455 .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
00456 };
00457
00458 static const struct ast_format au_f = {
00459 .name = "au",
00460 .exts = "au",
00461 .format = AST_FORMAT_ULAW,
00462 .open = au_open,
00463 .rewrite = au_rewrite,
00464 .write = pcm_write,
00465 .seek = au_seek,
00466 .trunc = au_trunc,
00467 .tell = au_tell,
00468 .read = pcm_read,
00469 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00470 };
00471
00472 static int load_module(void)
00473 {
00474 int index;
00475
00476
00477 for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++)
00478 ulaw_silence[index] = AST_LIN2MU(0);
00479 for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++)
00480 alaw_silence[index] = AST_LIN2A(0);
00481
00482 return ast_format_register(&pcm_f)
00483 || ast_format_register(&alaw_f)
00484 || ast_format_register(&au_f)
00485 || ast_format_register(&g722_f);
00486 }
00487
00488 static int unload_module(void)
00489 {
00490 return ast_format_unregister(pcm_f.name)
00491 || ast_format_unregister(alaw_f.name)
00492 || ast_format_unregister(au_f.name)
00493 || ast_format_unregister(g722_f.name);
00494 }
00495
00496 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz");