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