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 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00029
00030 #include <sys/types.h>
00031 #include <errno.h>
00032 #include <unistd.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <fcntl.h>
00037 #include <dirent.h>
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040
00041 #include "asterisk/frame.h"
00042 #include "asterisk/file.h"
00043 #include "asterisk/cli.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/channel.h"
00046 #include "asterisk/sched.h"
00047 #include "asterisk/options.h"
00048 #include "asterisk/translate.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/lock.h"
00051 #include "asterisk/app.h"
00052 #include "asterisk/pbx.h"
00053 #include "asterisk/linkedlists.h"
00054 #include "asterisk/module.h"
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 int ast_language_is_prefix = 1;
00065
00066 static AST_RWLIST_HEAD_STATIC(formats, ast_format);
00067
00068 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
00069 {
00070 struct ast_format *tmp;
00071
00072 AST_RWLIST_WRLOCK(&formats);
00073 AST_RWLIST_TRAVERSE(&formats, tmp, list) {
00074 if (!strcasecmp(f->name, tmp->name)) {
00075 AST_RWLIST_UNLOCK(&formats);
00076 ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
00077 return -1;
00078 }
00079 }
00080 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
00081 AST_RWLIST_UNLOCK(&formats);
00082 return -1;
00083 }
00084 *tmp = *f;
00085 tmp->module = mod;
00086 if (tmp->buf_size) {
00087
00088
00089
00090
00091 struct _test_align { void *a, *b; } p;
00092 int align = (char *)&p.b - (char *)&p.a;
00093 tmp->buf_size = ((f->buf_size + align - 1)/align)*align;
00094 }
00095
00096 memset(&tmp->list, 0, sizeof(tmp->list));
00097
00098 AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
00099 AST_RWLIST_UNLOCK(&formats);
00100 if (option_verbose > 1)
00101 ast_verbose( VERBOSE_PREFIX_2 "Registered file format %s, extension(s) %s\n", f->name, f->exts);
00102
00103 return 0;
00104 }
00105
00106 int ast_format_unregister(const char *name)
00107 {
00108 struct ast_format *tmp;
00109 int res = -1;
00110
00111 AST_RWLIST_WRLOCK(&formats);
00112 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
00113 if (!strcasecmp(name, tmp->name)) {
00114 AST_RWLIST_REMOVE_CURRENT(&formats, list);
00115 free(tmp);
00116 res = 0;
00117 }
00118 }
00119 AST_RWLIST_TRAVERSE_SAFE_END
00120 AST_RWLIST_UNLOCK(&formats);
00121
00122 if (!res) {
00123 if (option_verbose > 1)
00124 ast_verbose( VERBOSE_PREFIX_2 "Unregistered format %s\n", name);
00125 } else
00126 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
00127
00128 return res;
00129 }
00130
00131 int ast_stopstream(struct ast_channel *tmp)
00132 {
00133
00134 if (tmp->stream) {
00135 ast_closestream(tmp->stream);
00136 tmp->stream = NULL;
00137 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
00138 ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
00139 }
00140
00141 if (tmp->vstream != NULL) {
00142 ast_closestream(tmp->vstream);
00143 tmp->vstream = NULL;
00144 }
00145 return 0;
00146 }
00147
00148 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
00149 {
00150 int res = -1;
00151 int alt = 0;
00152 if (f->frametype == AST_FRAME_VIDEO) {
00153 if (fs->fmt->format < AST_FORMAT_MAX_AUDIO) {
00154
00155 if (!fs->vfs && fs->filename) {
00156 const char *type = ast_getformatname(f->subclass & ~0x1);
00157 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
00158 ast_log(LOG_DEBUG, "Opened video output file\n");
00159 }
00160 if (fs->vfs)
00161 return ast_writestream(fs->vfs, f);
00162
00163 return 0;
00164 } else {
00165
00166 alt = 1;
00167 }
00168 } else if (f->frametype != AST_FRAME_VOICE) {
00169 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
00170 return -1;
00171 }
00172 if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
00173 res = fs->fmt->write(fs, f);
00174 if (res < 0)
00175 ast_log(LOG_WARNING, "Natural write failed\n");
00176 else if (res > 0)
00177 ast_log(LOG_WARNING, "Huh??\n");
00178 } else {
00179
00180
00181 if (fs->trans && f->subclass != fs->lastwriteformat) {
00182 ast_translator_free_path(fs->trans);
00183 fs->trans = NULL;
00184 }
00185 if (!fs->trans)
00186 fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
00187 if (!fs->trans)
00188 ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
00189 fs->fmt->name, ast_getformatname(f->subclass));
00190 else {
00191 struct ast_frame *trf;
00192 fs->lastwriteformat = f->subclass;
00193
00194 trf = ast_translate(fs->trans, f, 0);
00195 if (trf) {
00196 res = fs->fmt->write(fs, trf);
00197 if (res)
00198 ast_log(LOG_WARNING, "Translated frame write failed\n");
00199 } else
00200 res = 0;
00201 }
00202 }
00203 return res;
00204 }
00205
00206 static int copy(const char *infile, const char *outfile)
00207 {
00208 int ifd, ofd, len;
00209 char buf[4096];
00210
00211 if ((ifd = open(infile, O_RDONLY)) < 0) {
00212 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
00213 return -1;
00214 }
00215 if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
00216 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
00217 close(ifd);
00218 return -1;
00219 }
00220 while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
00221 int res;
00222 if (len < 0) {
00223 ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
00224 break;
00225 }
00226
00227 res = write(ofd, buf, len);
00228 if (res != len) {
00229 ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
00230 len = -1;
00231 break;
00232 }
00233 }
00234 close(ifd);
00235 close(ofd);
00236 if (len < 0) {
00237 unlink(outfile);
00238 return -1;
00239 }
00240 return 0;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 static char *build_filename(const char *filename, const char *ext)
00250 {
00251 char *fn = NULL;
00252
00253 if (!strcmp(ext, "wav49"))
00254 ext = "WAV";
00255
00256 if (filename[0] == '/')
00257 asprintf(&fn, "%s.%s", filename, ext);
00258 else
00259 asprintf(&fn, "%s/sounds/%s.%s",
00260 ast_config_AST_DATA_DIR, filename, ext);
00261 return fn;
00262 }
00263
00264
00265
00266 static int exts_compare(const char *exts, const char *type)
00267 {
00268 char tmp[256];
00269 char *stringp = tmp, *ext;
00270
00271 ast_copy_string(tmp, exts, sizeof(tmp));
00272 while ((ext = strsep(&stringp, "|"))) {
00273 if (!strcmp(ext, type))
00274 return 1;
00275 }
00276
00277 return 0;
00278 }
00279
00280 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
00281 {
00282 struct ast_filestream *s;
00283
00284 int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;
00285 if ( (s = ast_calloc(1, l)) == NULL)
00286 return NULL;
00287 s->fmt = fmt;
00288 s->f = bfile;
00289
00290 if (fmt->desc_size)
00291 s->private = ((char *)(s+1)) + fmt->buf_size;
00292 if (fmt->buf_size)
00293 s->buf = (char *)(s+1);
00294 s->fr.src = fmt->name;
00295 return s;
00296 }
00297
00298
00299
00300
00301
00302 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
00303
00304 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
00305 {
00306 struct ast_format *f = s->fmt;
00307 int ret = -1;
00308
00309 if (mode == WRAP_OPEN && f->open && f->open(s))
00310 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
00311 else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
00312 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
00313 else {
00314
00315 ast_module_ref(f->module);
00316 ret = 0;
00317 }
00318 return ret;
00319 }
00320
00321 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
00322 {
00323 return fn_wrapper(s, comment, WRAP_REWRITE);
00324 }
00325
00326 static int open_wrapper(struct ast_filestream *s)
00327 {
00328 return fn_wrapper(s, NULL, WRAP_OPEN);
00329 }
00330
00331 enum file_action {
00332 ACTION_EXISTS = 1,
00333 ACTION_DELETE,
00334 ACTION_RENAME,
00335 ACTION_OPEN,
00336 ACTION_COPY
00337 };
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
00349 {
00350 struct ast_format *f;
00351 int res = (action == ACTION_EXISTS) ? 0 : -1;
00352
00353 AST_RWLIST_RDLOCK(&formats);
00354
00355 AST_RWLIST_TRAVERSE(&formats, f, list) {
00356 char *stringp, *ext = NULL;
00357
00358 if (fmt && !exts_compare(f->exts, fmt))
00359 continue;
00360
00361
00362
00363
00364
00365 stringp = ast_strdupa(f->exts);
00366 while ( (ext = strsep(&stringp, "|")) ) {
00367 struct stat st;
00368 char *fn = build_filename(filename, ext);
00369
00370 if (fn == NULL)
00371 continue;
00372
00373 if ( stat(fn, &st) ) {
00374 free(fn);
00375 continue;
00376 }
00377
00378
00379
00380 if (action == ACTION_OPEN) {
00381 struct ast_channel *chan = (struct ast_channel *)arg2;
00382 FILE *bfile;
00383 struct ast_filestream *s;
00384
00385 if ( !(chan->writeformat & f->format) &&
00386 !(f->format >= AST_FORMAT_MAX_AUDIO && fmt)) {
00387 free(fn);
00388 continue;
00389 }
00390 if ( (bfile = fopen(fn, "r")) == NULL) {
00391 free(fn);
00392 continue;
00393 }
00394 s = get_filestream(f, bfile);
00395 if (!s) {
00396 fclose(bfile);
00397 free(fn);
00398 continue;
00399 }
00400 if (open_wrapper(s)) {
00401 fclose(bfile);
00402 free(fn);
00403 free(s);
00404 continue;
00405 }
00406
00407 res = 1;
00408 s->lasttimeout = -1;
00409 s->fmt = f;
00410 s->trans = NULL;
00411 s->filename = NULL;
00412 if (s->fmt->format < AST_FORMAT_MAX_AUDIO) {
00413 if (chan->stream)
00414 ast_closestream(chan->stream);
00415 chan->stream = s;
00416 } else {
00417 if (chan->vstream)
00418 ast_closestream(chan->vstream);
00419 chan->vstream = s;
00420 }
00421 free(fn);
00422 break;
00423 }
00424 switch (action) {
00425 case ACTION_OPEN:
00426 break;
00427
00428 case ACTION_EXISTS:
00429 res |= f->format;
00430 break;
00431
00432 case ACTION_DELETE:
00433 if ( (res = unlink(fn)) )
00434 ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
00435 break;
00436
00437 case ACTION_RENAME:
00438 case ACTION_COPY: {
00439 char *nfn = build_filename((const char *)arg2, ext);
00440 if (!nfn)
00441 ast_log(LOG_WARNING, "Out of memory\n");
00442 else {
00443 res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
00444 if (res)
00445 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
00446 action == ACTION_COPY ? "copy" : "rename",
00447 fn, nfn, strerror(errno));
00448 free(nfn);
00449 }
00450 }
00451 break;
00452
00453 default:
00454 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
00455 }
00456 free(fn);
00457 }
00458 }
00459 AST_RWLIST_UNLOCK(&formats);
00460 return res;
00461 }
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
00475 char *buf, int buflen)
00476 {
00477 int res = -1;
00478 int langlen;
00479 const char *c = strrchr(filename, '/');
00480 int offset = c ? c - filename + 1 : 0;
00481
00482 if (preflang == NULL)
00483 preflang = "";
00484 langlen = strlen(preflang);
00485
00486 if (buflen < langlen + strlen(filename) + 2) {
00487 ast_log(LOG_WARNING, "buffer too small\n");
00488 buf[0] = '\0';
00489 buf = alloca(langlen + strlen(filename) + 2);
00490 }
00491 if (buf == NULL)
00492 return 0;
00493 buf[0] = '\0';
00494 for (;;) {
00495 if (ast_language_is_prefix) {
00496 if (langlen) {
00497 strcpy(buf, preflang);
00498 buf[langlen] = '/';
00499 strcpy(buf + langlen + 1, filename);
00500 } else
00501 strcpy(buf, filename);
00502 } else {
00503 strcpy(buf, filename);
00504 if (langlen) {
00505
00506 strcpy(buf + offset, preflang);
00507 sprintf(buf + offset + langlen, "/%s", filename + offset);
00508 }
00509 }
00510 res = ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
00511 if (res > 0)
00512 break;
00513 if (langlen == 0)
00514 break;
00515 if (preflang[langlen] == '_')
00516 langlen = 0;
00517 else
00518 langlen = (c = strchr(preflang, '_')) ? c - preflang : 0;
00519 }
00520 return res;
00521 }
00522
00523 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
00524 {
00525 return ast_openstream_full(chan, filename, preflang, 0);
00526 }
00527
00528 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
00529 {
00530
00531
00532
00533
00534
00535 int fmts, res, buflen;
00536 char *buf;
00537
00538 if (!asis) {
00539
00540 ast_stopstream(chan);
00541 if (chan->generator)
00542 ast_deactivate_generator(chan);
00543 }
00544 if (preflang == NULL)
00545 preflang = "";
00546 buflen = strlen(preflang) + strlen(filename) + 2;
00547 buf = alloca(buflen);
00548 if (buf == NULL)
00549 return NULL;
00550 fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
00551 if (fmts > 0)
00552 fmts &= AST_FORMAT_AUDIO_MASK;
00553 if (fmts < 1) {
00554 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
00555 return NULL;
00556 }
00557 chan->oldwriteformat = chan->writeformat;
00558
00559 res = ast_set_write_format(chan, fmts);
00560 res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
00561 if (res >= 0)
00562 return chan->stream;
00563 return NULL;
00564 }
00565
00566 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
00567 {
00568
00569
00570
00571 unsigned int format;
00572 char *buf;
00573 int buflen;
00574
00575 if (preflang == NULL)
00576 preflang = "";
00577 buflen = strlen(preflang) + strlen(filename) + 2;
00578 buf = alloca(buflen);
00579 if (buf == NULL)
00580 return NULL;
00581
00582 for (format = AST_FORMAT_MAX_AUDIO << 1; format <= AST_FORMAT_MAX_VIDEO; format = format << 1) {
00583 int fd;
00584 const char *fmt;
00585
00586 if (!(chan->nativeformats & format))
00587 continue;
00588 fmt = ast_getformatname(format);
00589 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1)
00590 continue;
00591 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
00592 if (fd >= 0)
00593 return chan->vstream;
00594 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
00595 }
00596 return NULL;
00597 }
00598
00599 struct ast_frame *ast_readframe(struct ast_filestream *s)
00600 {
00601 struct ast_frame *f = NULL;
00602 int whennext = 0;
00603 if (s && s->fmt)
00604 f = s->fmt->read(s, &whennext);
00605 return f;
00606 }
00607
00608 static int ast_readaudio_callback(void *data)
00609 {
00610 struct ast_filestream *s = data;
00611 int whennext = 0;
00612
00613 while(!whennext) {
00614 struct ast_frame *fr = s->fmt->read(s, &whennext);
00615 if (!fr || ast_write(s->owner, fr) ) {
00616 if (fr)
00617 ast_log(LOG_WARNING, "Failed to write frame\n");
00618 s->owner->streamid = -1;
00619 #ifdef HAVE_ZAPTEL
00620 ast_settimeout(s->owner, 0, NULL, NULL);
00621 #endif
00622 return 0;
00623 }
00624 }
00625 if (whennext != s->lasttimeout) {
00626 #ifdef HAVE_ZAPTEL
00627 if (s->owner->timingfd > -1)
00628 ast_settimeout(s->owner, whennext, ast_readaudio_callback, s);
00629 else
00630 #endif
00631 s->owner->streamid = ast_sched_add(s->owner->sched, whennext/8, ast_readaudio_callback, s);
00632 s->lasttimeout = whennext;
00633 return 0;
00634 }
00635 return 1;
00636 }
00637
00638 static int ast_readvideo_callback(void *data)
00639 {
00640 struct ast_filestream *s = data;
00641 int whennext = 0;
00642
00643 while (!whennext) {
00644 struct ast_frame *fr = s->fmt->read(s, &whennext);
00645 if (!fr || ast_write(s->owner, fr)) {
00646 if (fr)
00647 ast_log(LOG_WARNING, "Failed to write frame\n");
00648 s->owner->vstreamid = -1;
00649 return 0;
00650 }
00651 }
00652 if (whennext != s->lasttimeout) {
00653 s->owner->vstreamid = ast_sched_add(s->owner->sched, whennext/8, ast_readvideo_callback, s);
00654 s->lasttimeout = whennext;
00655 return 0;
00656 }
00657 return 1;
00658 }
00659
00660 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
00661 {
00662 s->owner = chan;
00663 return 0;
00664 }
00665
00666 int ast_playstream(struct ast_filestream *s)
00667 {
00668 if (s->fmt->format < AST_FORMAT_MAX_AUDIO)
00669 ast_readaudio_callback(s);
00670 else
00671 ast_readvideo_callback(s);
00672 return 0;
00673 }
00674
00675 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
00676 {
00677 return fs->fmt->seek(fs, sample_offset, whence);
00678 }
00679
00680 int ast_truncstream(struct ast_filestream *fs)
00681 {
00682 return fs->fmt->trunc(fs);
00683 }
00684
00685 off_t ast_tellstream(struct ast_filestream *fs)
00686 {
00687 return fs->fmt->tell(fs);
00688 }
00689
00690 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
00691 {
00692 return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00693 }
00694
00695 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
00696 {
00697 return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
00698 }
00699
00700 int ast_closestream(struct ast_filestream *f)
00701 {
00702 char *cmd = NULL;
00703 size_t size = 0;
00704
00705 if (f->owner) {
00706 if (f->fmt->format < AST_FORMAT_MAX_AUDIO) {
00707 f->owner->stream = NULL;
00708 if (f->owner->streamid > -1)
00709 ast_sched_del(f->owner->sched, f->owner->streamid);
00710 f->owner->streamid = -1;
00711 #ifdef HAVE_ZAPTEL
00712 ast_settimeout(f->owner, 0, NULL, NULL);
00713 #endif
00714 } else {
00715 f->owner->vstream = NULL;
00716 if (f->owner->vstreamid > -1)
00717 ast_sched_del(f->owner->sched, f->owner->vstreamid);
00718 f->owner->vstreamid = -1;
00719 }
00720 }
00721
00722 if (f->trans)
00723 ast_translator_free_path(f->trans);
00724
00725 if (f->realfilename && f->filename) {
00726 size = strlen(f->filename) + strlen(f->realfilename) + 15;
00727 cmd = alloca(size);
00728 memset(cmd,0,size);
00729 snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
00730 ast_safe_system(cmd);
00731 }
00732
00733 if (f->filename)
00734 free(f->filename);
00735 if (f->realfilename)
00736 free(f->realfilename);
00737 if (f->fmt->close)
00738 f->fmt->close(f);
00739 fclose(f->f);
00740 if (f->vfs)
00741 ast_closestream(f->vfs);
00742 ast_module_unref(f->fmt->module);
00743 free(f);
00744 return 0;
00745 }
00746
00747
00748
00749
00750
00751 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
00752 {
00753 char *buf;
00754 int buflen;
00755
00756 if (preflang == NULL)
00757 preflang = "";
00758 buflen = strlen(preflang) + strlen(filename) + 2;
00759 buf = alloca(buflen);
00760 if (buf == NULL)
00761 return 0;
00762 return fileexists_core(filename, fmt, preflang, buf, buflen);
00763 }
00764
00765 int ast_filedelete(const char *filename, const char *fmt)
00766 {
00767 return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
00768 }
00769
00770 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
00771 {
00772 return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
00773 }
00774
00775 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
00776 {
00777 return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
00778 }
00779
00780 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
00781 {
00782 struct ast_filestream *fs;
00783 struct ast_filestream *vfs=NULL;
00784 char fmt[256];
00785
00786 fs = ast_openstream(chan, filename, preflang);
00787 if (fs)
00788 vfs = ast_openvstream(chan, filename, preflang);
00789 if (vfs)
00790 ast_log(LOG_DEBUG, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
00791 if (fs){
00792 if (ast_applystream(chan, fs))
00793 return -1;
00794 if (vfs && ast_applystream(chan, vfs))
00795 return -1;
00796 ast_playstream(fs);
00797 if (vfs)
00798 ast_playstream(vfs);
00799 if (option_verbose > 2)
00800 ast_verbose(VERBOSE_PREFIX_3 "<%s> Playing '%s' (language '%s')\n", chan->name, filename, preflang ? preflang : "default");
00801
00802 return 0;
00803 }
00804 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
00805 return -1;
00806 }
00807
00808 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
00809 {
00810 FILE *bfile;
00811 struct ast_format *f;
00812 struct ast_filestream *fs = NULL;
00813 char *fn;
00814
00815 AST_RWLIST_RDLOCK(&formats);
00816
00817 AST_RWLIST_TRAVERSE(&formats, f, list) {
00818 fs = NULL;
00819 if (!exts_compare(f->exts, type))
00820 continue;
00821
00822 fn = build_filename(filename, type);
00823 errno = 0;
00824 bfile = fopen(fn, "r");
00825 if (!bfile || (fs = get_filestream(f, bfile)) == NULL ||
00826 open_wrapper(fs) ) {
00827 ast_log(LOG_WARNING, "Unable to open %s\n", fn);
00828 if (fs)
00829 ast_free(fs);
00830 if (bfile)
00831 fclose(bfile);
00832 free(fn);
00833 continue;
00834 }
00835
00836 fs->trans = NULL;
00837 fs->fmt = f;
00838 fs->flags = flags;
00839 fs->mode = mode;
00840 fs->filename = strdup(filename);
00841 fs->vfs = NULL;
00842 break;
00843 }
00844
00845 AST_RWLIST_UNLOCK(&formats);
00846 if (!fs)
00847 ast_log(LOG_WARNING, "No such format '%s'\n", type);
00848
00849 return fs;
00850 }
00851
00852 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
00853 {
00854 int fd, myflags = 0;
00855
00856 FILE *bfile = NULL;
00857 struct ast_format *f;
00858 struct ast_filestream *fs = NULL;
00859 char *buf = NULL;
00860 size_t size = 0;
00861 int format_found = 0;
00862
00863 AST_RWLIST_RDLOCK(&formats);
00864
00865
00866
00867 if (flags & O_APPEND) {
00868 flags &= ~O_APPEND;
00869 } else {
00870 myflags = O_TRUNC;
00871 }
00872
00873 myflags |= O_WRONLY | O_CREAT;
00874
00875
00876
00877
00878 AST_RWLIST_TRAVERSE(&formats, f, list) {
00879 char *fn, *orig_fn = NULL;
00880 if (fs)
00881 break;
00882
00883 if (!exts_compare(f->exts, type))
00884 continue;
00885 else
00886 format_found = 1;
00887
00888 fn = build_filename(filename, type);
00889 fd = open(fn, flags | myflags, mode);
00890 if (fd > -1) {
00891
00892 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
00893 if (!bfile) {
00894 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
00895 close(fd);
00896 fd = -1;
00897 }
00898 }
00899
00900 if (ast_opt_cache_record_files && (fd > -1)) {
00901 char *c;
00902
00903 fclose(bfile);
00904
00905
00906
00907
00908 orig_fn = ast_strdupa(fn);
00909 for (c = fn; *c; c++)
00910 if (*c == '/')
00911 *c = '_';
00912
00913 size = strlen(fn) + strlen(record_cache_dir) + 2;
00914 buf = alloca(size);
00915 strcpy(buf, record_cache_dir);
00916 strcat(buf, "/");
00917 strcat(buf, fn);
00918 free(fn);
00919 fn = buf;
00920 fd = open(fn, flags | myflags, mode);
00921 if (fd > -1) {
00922
00923 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
00924 if (!bfile) {
00925 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
00926 close(fd);
00927 fd = -1;
00928 }
00929 }
00930 }
00931 if (fd > -1) {
00932 errno = 0;
00933 fs = get_filestream(f, bfile);
00934 if (!fs || rewrite_wrapper(fs, comment)) {
00935 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
00936 close(fd);
00937 if (orig_fn) {
00938 unlink(fn);
00939 unlink(orig_fn);
00940 }
00941 if (fs)
00942 ast_free(fs);
00943 fs = NULL;
00944 continue;
00945 }
00946 fs->trans = NULL;
00947 fs->fmt = f;
00948 fs->flags = flags;
00949 fs->mode = mode;
00950 if (orig_fn) {
00951 fs->realfilename = strdup(orig_fn);
00952 fs->filename = strdup(fn);
00953 } else {
00954 fs->realfilename = NULL;
00955 fs->filename = strdup(filename);
00956 }
00957 fs->vfs = NULL;
00958
00959 f->seek(fs, 0, SEEK_END);
00960 } else if (errno != EEXIST) {
00961 ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
00962 if (orig_fn)
00963 unlink(orig_fn);
00964 }
00965
00966 if (!buf)
00967 free(fn);
00968 }
00969
00970 AST_RWLIST_UNLOCK(&formats);
00971
00972 if (!format_found)
00973 ast_log(LOG_WARNING, "No such format '%s'\n", type);
00974
00975 return fs;
00976 }
00977
00978
00979
00980
00981 static int waitstream_core(struct ast_channel *c, const char *breakon,
00982 const char *forward, const char *rewind, int skip_ms,
00983 int audiofd, int cmdfd, const char *context)
00984 {
00985 if (!breakon)
00986 breakon = "";
00987 if (!forward)
00988 forward = "";
00989 if (!rewind)
00990 rewind = "";
00991
00992
00993 ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
00994
00995 while (c->stream) {
00996 int res;
00997 int ms = ast_sched_wait(c->sched);
00998 if (ms < 0 && !c->timingfunc) {
00999 ast_stopstream(c);
01000 break;
01001 }
01002 if (ms < 0)
01003 ms = 1000;
01004 if (cmdfd < 0) {
01005 res = ast_waitfor(c, ms);
01006 if (res < 0) {
01007 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
01008 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01009 return res;
01010 }
01011 } else {
01012 int outfd;
01013 struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
01014 if (!rchan && (outfd < 0) && (ms)) {
01015
01016 if (errno == EINTR)
01017 continue;
01018 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
01019 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01020 return -1;
01021 } else if (outfd > -1) {
01022
01023 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01024 return 1;
01025 }
01026
01027 res = rchan ? 1 : 0;
01028 }
01029 if (res > 0) {
01030 struct ast_frame *fr = ast_read(c);
01031 if (!fr) {
01032 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01033 return -1;
01034 }
01035 switch(fr->frametype) {
01036 case AST_FRAME_DTMF_END:
01037 if (context) {
01038 const char exten[2] = { fr->subclass, '\0' };
01039 if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
01040 res = fr->subclass;
01041 ast_frfree(fr);
01042 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01043 return res;
01044 }
01045 } else {
01046 res = fr->subclass;
01047 if (strchr(forward,res)) {
01048 ast_stream_fastforward(c->stream, skip_ms);
01049 } else if (strchr(rewind,res)) {
01050 ast_stream_rewind(c->stream, skip_ms);
01051 } else if (strchr(breakon, res)) {
01052 ast_frfree(fr);
01053 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01054 return res;
01055 }
01056 }
01057 break;
01058 case AST_FRAME_CONTROL:
01059 switch(fr->subclass) {
01060 case AST_CONTROL_HANGUP:
01061 case AST_CONTROL_BUSY:
01062 case AST_CONTROL_CONGESTION:
01063 ast_frfree(fr);
01064 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01065 return -1;
01066 case AST_CONTROL_RINGING:
01067 case AST_CONTROL_ANSWER:
01068 case AST_CONTROL_VIDUPDATE:
01069 case AST_CONTROL_HOLD:
01070 case AST_CONTROL_UNHOLD:
01071
01072 break;
01073 default:
01074 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
01075 }
01076 break;
01077 case AST_FRAME_VOICE:
01078
01079 if (audiofd > -1)
01080 write(audiofd, fr->data, fr->datalen);
01081 default:
01082
01083 break;
01084 }
01085 ast_frfree(fr);
01086 }
01087 ast_sched_runq(c->sched);
01088 }
01089
01090 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
01091
01092 return (c->_softhangup ? -1 : 0);
01093 }
01094
01095 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
01096 {
01097 return waitstream_core(c, breakon, forward, rewind, ms,
01098 -1 , -1 , NULL );
01099 }
01100
01101 int ast_waitstream(struct ast_channel *c, const char *breakon)
01102 {
01103 return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
01104 }
01105
01106 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
01107 {
01108 return waitstream_core(c, breakon, NULL, NULL, 0,
01109 audiofd, cmdfd, NULL );
01110 }
01111
01112 int ast_waitstream_exten(struct ast_channel *c, const char *context)
01113 {
01114
01115
01116
01117 if (!context)
01118 context = c->context;
01119 return waitstream_core(c, NULL, NULL, NULL, 0,
01120 -1, -1, context);
01121 }
01122
01123
01124
01125
01126
01127
01128 int ast_stream_and_wait(struct ast_channel *chan, const char *file,
01129 const char *language, const char *digits)
01130 {
01131 int res = 0;
01132 if (!ast_strlen_zero(file)) {
01133 res = ast_streamfile(chan, file, language);
01134 if (!res)
01135 res = ast_waitstream(chan, digits);
01136 }
01137 return res;
01138 }
01139
01140 static int show_file_formats(int fd, int argc, char *argv[])
01141 {
01142 #define FORMAT "%-10s %-10s %-20s\n"
01143 #define FORMAT2 "%-10s %-10s %-20s\n"
01144 struct ast_format *f;
01145 int count_fmt = 0;
01146
01147 if (argc != 4)
01148 return RESULT_SHOWUSAGE;
01149 ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
01150
01151 AST_RWLIST_RDLOCK(&formats);
01152 AST_RWLIST_TRAVERSE(&formats, f, list) {
01153 ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
01154 count_fmt++;
01155 }
01156 AST_RWLIST_UNLOCK(&formats);
01157 ast_cli(fd, "%d file formats registered.\n", count_fmt);
01158 return RESULT_SUCCESS;
01159 #undef FORMAT
01160 #undef FORMAT2
01161 }
01162
01163 static int show_file_formats_deprecated(int fd, int argc, char *argv[])
01164 {
01165 #define FORMAT "%-10s %-10s %-20s\n"
01166 #define FORMAT2 "%-10s %-10s %-20s\n"
01167 struct ast_format *f;
01168 int count_fmt = 0;
01169
01170 if (argc != 3)
01171 return RESULT_SHOWUSAGE;
01172 ast_cli(fd, FORMAT, "Format", "Name", "Extensions");
01173
01174 if (AST_LIST_LOCK(&formats)) {
01175 ast_log(LOG_WARNING, "Unable to lock format list\n");
01176 return -1;
01177 }
01178
01179 AST_LIST_TRAVERSE(&formats, f, list) {
01180 ast_cli(fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
01181 count_fmt++;
01182 }
01183 AST_LIST_UNLOCK(&formats);
01184 ast_cli(fd, "%d file formats registered.\n", count_fmt);
01185 return RESULT_SUCCESS;
01186 #undef FORMAT
01187 #undef FORMAT2
01188 }
01189
01190 char show_file_formats_usage[] =
01191 "Usage: core show file formats\n"
01192 " Displays currently registered file formats (if any)\n";
01193
01194 struct ast_cli_entry cli_show_file_formats_deprecated = {
01195 { "show", "file", "formats" },
01196 show_file_formats_deprecated, NULL,
01197 NULL };
01198
01199 struct ast_cli_entry cli_file[] = {
01200 { { "core", "show", "file", "formats" },
01201 show_file_formats, "Displays file formats",
01202 show_file_formats_usage, NULL, &cli_show_file_formats_deprecated },
01203 };
01204
01205 int ast_file_init(void)
01206 {
01207 ast_cli_register_multiple(cli_file, sizeof(cli_file) / sizeof(struct ast_cli_entry));
01208 return 0;
01209 }