Asterisk - The Open Source Telephony Project  GIT-master-a24979a
file.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18 
19 /*! \file
20  *
21  * \brief Generic File Format Support.
22  *
23  * \author Mark Spencer <markster@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 #include <dirent.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <math.h>
36 
37 #include "asterisk/_private.h" /* declare ast_file_init() */
38 #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
39 #include "asterisk/mod_format.h"
40 #include "asterisk/cli.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/app.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/linkedlists.h"
49 #include "asterisk/module.h"
50 #include "asterisk/astobj2.h"
51 #include "asterisk/test.h"
52 #include "asterisk/stasis.h"
53 #include "asterisk/json.h"
54 #include "asterisk/stasis_system.h"
55 #include "asterisk/media_cache.h"
56 
57 /*! \brief
58  * The following variable controls the layout of localized sound files.
59  * If 0, use the historical layout with prefix just before the filename
60  * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
61  * if 1 put the prefix at the beginning of the filename
62  * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
63  * The latter permits a language to be entirely in one directory.
64  *
65  * This is settable in asterisk.conf.
66  */
68 
70 
73 
74 static struct ast_json *json_array_from_list(const char *list, const char *sep)
75 {
77  char *stringp, *ext;
78 
79  stringp = ast_strdupa(list); /* this is in the stack so does not need to be freed */
80  if (!array || !stringp) {
81  return NULL;
82  }
83 
84  while ((ext = strsep(&stringp, sep))) {
86  return NULL;
87  }
88  }
89 
90  return ast_json_ref(array);
91 }
92 
93 static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
94 {
95  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
96  RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
97  RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
98 
99  if (!type) {
100  return -1;
101  }
102 
103  json_object = ast_json_pack("{s: s, s: o}",
104  "format", f->name,
105  "extensions", json_array_from_list(f->exts, "|"));
106  if (!json_object) {
107  return -1;
108  }
109 
110  json_payload = ast_json_payload_create(json_object);
111  if (!json_payload) {
112  return -1;
113  }
114 
115  msg = stasis_message_create(type, json_payload);
116  if (!msg) {
117  return -1;
118  }
119 
121  return 0;
122 }
123 
124 int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
125 {
126  struct ast_format_def *tmp;
127 
130  if (!strcasecmp(f->name, tmp->name)) {
132  ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
133  return -1;
134  }
135  }
136  if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
138  return -1;
139  }
140  *tmp = *f;
141  tmp->module = mod;
142  if (tmp->buf_size) {
143  /*
144  * Align buf_size properly, rounding up to the machine-specific
145  * alignment for pointers.
146  */
147  struct _test_align { void *a, *b; } p;
148  int align = (char *)&p.b - (char *)&p.a;
149  tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
150  }
151 
152  memset(&tmp->list, 0, sizeof(tmp->list));
153 
156  ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
158 
159  return 0;
160 }
161 
163 {
164  struct ast_format_def *tmp;
165  int res = -1;
166 
169  if (!strcasecmp(name, tmp->name)) {
172  ast_free(tmp);
173  res = 0;
174  }
175  }
178 
179  if (!res)
180  ast_verb(2, "Unregistered format %s\n", name);
181  else
182  ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
183 
184  return res;
185 }
186 
187 FILE *ast_file_mkftemp(char *template, mode_t mode)
188 {
189  FILE *p = NULL;
190  int pfd = mkstemp(template);
191  chmod(template, mode);
192  if (pfd > -1) {
193  p = fdopen(pfd, "w+");
194  if (!p) {
195  close(pfd);
196  pfd = -1;
197  }
198  }
199  return p;
200 }
201 
202 int ast_file_fdtemp(const char *path, char **filename, const char *template_name)
203 {
204  int fd;
205 
206  if (ast_asprintf(filename, "%s/%s", path, template_name) < 0) {
207  ast_log(LOG_ERROR, "Failed to set up temporary file path\n");
208  return -1;
209  }
210 
211  ast_mkdir(path, 0644);
212 
213  if ((fd = mkstemp(*filename)) < 0) {
214  ast_log(LOG_NOTICE, "Failed to create temporary file\n");
215  ast_free(*filename);
216  return -1;
217  }
218 
219  return fd;
220 }
221 
223 {
225 
226  /* Stop a running stream if there is one */
227  if (ast_channel_stream(tmp)) {
231  ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp)));
232  }
233  /* Stop the video stream too */
234  if (ast_channel_vstream(tmp) != NULL) {
237  }
238 
240 
241  return 0;
242 }
243 
244 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
245 {
246  int res = -1;
247  if (f->frametype == AST_FRAME_VIDEO) {
249  /* This is the audio portion. Call the video one... */
250  if (!fs->vfs && fs->filename) {
251  const char *type = ast_format_get_name(f->subclass.format);
252  fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
253  ast_debug(1, "Opened video output file\n");
254  }
255  if (fs->vfs)
256  return ast_writestream(fs->vfs, f);
257  /* else ignore */
258  return 0;
259  }
260  } else if (f->frametype != AST_FRAME_VOICE) {
261  ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
262  return -1;
263  }
265  res = fs->fmt->write(fs, f);
266  if (res < 0)
267  ast_log(LOG_WARNING, "Natural write failed\n");
268  else if (res > 0)
269  ast_log(LOG_WARNING, "Huh??\n");
270  } else {
271  /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
272  the one we've setup a translator for, we do the "wrong thing" XXX */
275  fs->trans = NULL;
276  }
277  if (!fs->trans) {
279  }
280  if (!fs->trans) {
281  ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
283  } else {
284  struct ast_frame *trf;
286  /* Get the translated frame but don't consume the original in case they're using it on another stream */
287  if ((trf = ast_translate(fs->trans, f, 0))) {
288  struct ast_frame *cur;
289 
290  /* the translator may have returned multiple frames, so process them */
291  for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
292  if ((res = fs->fmt->write(fs, trf))) {
293  ast_log(LOG_WARNING, "Translated frame write failed\n");
294  break;
295  }
296  }
297  ast_frfree(trf);
298  } else {
299  res = 0;
300  }
301  }
302  }
303  return res;
304 }
305 
306 static int copy(const char *infile, const char *outfile)
307 {
308  int ifd, ofd, len;
309  char buf[4096]; /* XXX make it larger. */
310 
311  if ((ifd = open(infile, O_RDONLY)) < 0) {
312  ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
313  return -1;
314  }
315  if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
316  ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
317  close(ifd);
318  return -1;
319  }
320  while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
321  int res;
322  if (len < 0) {
323  ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
324  break;
325  }
326  /* XXX handle partial writes */
327  res = write(ofd, buf, len);
328  if (res != len) {
329  ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
330  len = -1; /* error marker */
331  break;
332  }
333  }
334  close(ifd);
335  close(ofd);
336  if (len < 0) {
337  unlink(outfile);
338  return -1; /* error */
339  }
340  return 0; /* success */
341 }
342 
343 /*!
344  * \brief construct a filename. Absolute pathnames are preserved,
345  * relative names are prefixed by the sounds/ directory.
346  * The wav49 suffix is replaced by 'WAV'.
347  * Returns a malloc'ed string to be freed by the caller.
348  */
349 static char *build_filename(const char *filename, const char *ext)
350 {
351  char *fn = NULL;
352 
353  /* The wav49 -> WAV translation is duplicated in apps/app_mixmonitor.c, so
354  if you change it here you need to change it there as well */
355  if (!strcmp(ext, "wav49"))
356  ext = "WAV";
357 
358  if (filename[0] == '/') {
359  if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
360  fn = NULL;
361  }
362  } else {
363  if (ast_asprintf(&fn, "%s/sounds/%s.%s",
364  ast_config_AST_DATA_DIR, filename, ext) < 0) {
365  fn = NULL;
366  }
367  }
368  return fn;
369 }
370 
371 /* compare type against the list 'exts' */
372 /* XXX need a better algorithm */
373 static int type_in_list(const char *list, const char *type, int (*cmp)(const char *s1, const char *s2))
374 {
375  char *stringp = ast_strdupa(list), *item;
376 
377  while ((item = strsep(&stringp, "|"))) {
378  if (!cmp(item, type)) {
379  return 1;
380  }
381  }
382 
383  return 0;
384 }
385 
386 #define exts_compare(list, type) (type_in_list((list), (type), strcmp))
387 
388 /*!
389  * \internal
390  * \brief Close the file stream by canceling any pending read / write callbacks
391  */
392 static void filestream_close(struct ast_filestream *f)
393 {
394  enum ast_media_type format_type = ast_format_get_type(f->fmt->format);
395 
396  if (!f->owner) {
397  return;
398  }
399 
400  /* Stop a running stream if there is one */
401  switch (format_type)
402  {
406  ast_settimeout(f->owner, 0, NULL, NULL);
407  break;
411  break;
412  default:
413  ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name);
414  break;
415  }
416 }
417 
418 static void filestream_destructor(void *arg)
419 {
420  struct ast_filestream *f = arg;
421  int status;
422  int pid = -1;
423 
424  /* Stop a running stream if there is one */
426 
427  /* destroy the translator on exit */
428  if (f->trans)
429  ast_translator_free_path(f->trans);
430 
431  if (f->fmt->close) {
432  void (*closefn)(struct ast_filestream *) = f->fmt->close;
433  closefn(f);
434  }
435 
436  if (f->f) {
437  fclose(f->f);
438  }
439 
440  if (f->realfilename && f->filename) {
441  pid = ast_safe_fork(0);
442  if (!pid) {
443  execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
444  _exit(1);
445  }
446  else if (pid > 0) {
447  /* Block the parent until the move is complete.*/
448  waitpid(pid, &status, 0);
449  }
450  }
451 
452  ast_free(f->filename);
453  ast_free(f->realfilename);
454  if (f->vfs)
455  ast_closestream(f->vfs);
456  ast_free(f->write_buffer);
457  ast_free((void *)f->orig_chan_name);
458  ao2_cleanup(f->lastwriteformat);
459  ao2_cleanup(f->fr.subclass.format);
460  ast_module_unref(f->fmt->module);
461 }
462 
463 static struct ast_filestream *get_filestream(struct ast_format_def *fmt, FILE *bfile)
464 {
465  struct ast_filestream *s;
466  int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
467 
469  return NULL;
470  }
471 
473  if (!s) {
475  return NULL;
476  }
477  s->fmt = fmt;
478  s->f = bfile;
479 
480  if (fmt->desc_size)
481  s->_private = ((char *)(s + 1)) + fmt->buf_size;
482  if (fmt->buf_size)
483  s->buf = (char *)(s + 1);
484  s->fr.src = fmt->name;
485 
490  }
491  s->fr.mallocd = 0;
493 
494  return s;
495 }
496 
497 /*
498  * Default implementations of open and rewrite.
499  * Only use them if you don't have expensive stuff to do.
500  */
502 
503 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
504 {
505  struct ast_format_def *f = s->fmt;
506  int ret = -1;
507  int (*openfn)(struct ast_filestream *s);
508 
509  if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
510  ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
511  else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
512  ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
513  else {
514  /* preliminary checks succeed. */
515  ret = 0;
516  }
517  return ret;
518 }
519 
520 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
521 {
522  return fn_wrapper(s, comment, WRAP_REWRITE);
523 }
524 
525 static int open_wrapper(struct ast_filestream *s)
526 {
527  return fn_wrapper(s, NULL, WRAP_OPEN);
528 }
529 
531  ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
532  ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
533  ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
535  ACTION_COPY /* copy file. return 0 on success, -1 on error */
536 };
537 
538 /*!
539  * \internal
540  * \brief perform various actions on a file. Second argument
541  * \note arg2 depends on the command:
542  * unused for DELETE
543  * optional ast_format_cap holding all the formats found for a file, for EXISTS.
544  * destination file name (const char *) for COPY and RENAME
545  * struct ast_channel * for OPEN
546  * if fmt is NULL, OPEN will return the first matching entry,
547  * whereas other functions will run on all matching entries.
548  */
549 static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
550 {
551  struct ast_format_def *f;
552  int res = (action == ACTION_EXISTS) ? 0 : -1;
553 
555  /* Check for a specific format */
557  char *ext = NULL;
558  char storage[strlen(f->exts) + 1];
559  char *stringp;
560 
561  if (fmt && !exts_compare(f->exts, fmt))
562  continue;
563 
564  /* Look for a file matching the supported extensions.
565  * The file must exist, and for OPEN, must match
566  * one of the formats supported by the channel.
567  */
568  strcpy(storage, f->exts); /* safe - this is in the stack so does not need to be freed */
569  stringp = storage;
570  while ( (ext = strsep(&stringp, "|")) ) {
571  struct stat st;
572  char *fn = build_filename(filename, ext);
573 
574  if (fn == NULL)
575  continue;
576 
577  if ( stat(fn, &st) ) { /* file not existent */
578  ast_free(fn);
579  continue;
580  }
581  /* for 'OPEN' we need to be sure that the format matches
582  * what the channel can process
583  */
584  if (action == ACTION_OPEN) {
585  struct ast_channel *chan = (struct ast_channel *)arg2;
586  FILE *bfile;
587  struct ast_filestream *s;
588 
590  !(((ast_format_get_type(f->format) == AST_MEDIA_TYPE_AUDIO) && fmt) ||
591  ((ast_format_get_type(f->format) == AST_MEDIA_TYPE_VIDEO) && fmt))) {
592  ast_free(fn);
593  continue; /* not a supported format */
594  }
595  if ( (bfile = fopen(fn, "r")) == NULL) {
596  ast_free(fn);
597  continue; /* cannot open file */
598  }
599  s = get_filestream(f, bfile);
600  if (!s) {
601  fclose(bfile);
602  ast_free(fn); /* cannot allocate descriptor */
603  continue;
604  }
605  if (open_wrapper(s)) {
606  ast_free(fn);
607  ast_closestream(s);
608  continue; /* cannot run open on file */
609  }
610  if (st.st_size == 0) {
611  ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
612  }
613  /* ok this is good for OPEN */
614  res = 1; /* found */
615  s->lasttimeout = -1;
616  s->fmt = f;
617  s->trans = NULL;
618  s->filename = NULL;
620  if (ast_channel_stream(chan))
622  ast_channel_stream_set(chan, s);
623  } else {
624  if (ast_channel_vstream(chan))
626  ast_channel_vstream_set(chan, s);
627  }
628  ast_free(fn);
629  break;
630  }
631  switch (action) {
632  case ACTION_OPEN:
633  break; /* will never get here */
634 
635  case ACTION_EXISTS: /* return the matching format */
636  /* if arg2 is present, it is a format capabilities structure.
637  * Add this format to the set of formats this file can be played in */
638  if (arg2) {
639  ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0);
640  }
641  res = 1; /* file does exist and format it exists in is returned in arg2 */
642  break;
643 
644  case ACTION_DELETE:
645  if ( (res = unlink(fn)) )
646  ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
647  break;
648 
649  case ACTION_RENAME:
650  case ACTION_COPY: {
651  char *nfn = build_filename((const char *)arg2, ext);
652  if (!nfn)
653  ast_log(LOG_WARNING, "Out of memory\n");
654  else {
655  res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
656  if (res)
657  ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
658  action == ACTION_COPY ? "copy" : "rename",
659  fn, nfn, strerror(errno));
660  ast_free(nfn);
661  }
662  }
663  break;
664 
665  default:
666  ast_log(LOG_WARNING, "Unknown helper %u\n", action);
667  }
668  ast_free(fn);
669  }
670  }
672  return res;
673 }
674 
675 static int is_absolute_path(const char *filename)
676 {
677  return filename[0] == '/';
678 }
679 
680 static int is_remote_path(const char *filename)
681 {
682  return strstr(filename, "://") ? 1 : 0;
683 }
684 
685 /*!
686  * \brief test if a file exists for a given format.
687  * \note result_cap is OPTIONAL
688  * \retval 1 true and result_cap represents format capabilities file exists in.
689  * \retval 0 false
690  */
691 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
692  char *buf, int buflen, struct ast_format_cap *result_cap)
693 {
694  if (buf == NULL) {
695  return 0;
696  }
697 
699  return filehelper(buf, result_cap, NULL, ACTION_EXISTS);
700  }
701 
702  if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
703  if (lang) {
704  snprintf(buf, buflen, "%s/%s", lang, filename);
705  } else {
706  snprintf(buf, buflen, "%s", filename);
707  }
708  } else { /* old layout */
709  strcpy(buf, filename); /* first copy the full string */
710  if (lang) {
711  /* insert the language and suffix if needed */
712  const char *c = strrchr(filename, '/');
713  int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
714  snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
715  }
716  }
717 
718  return filehelper(buf, result_cap, fmt, ACTION_EXISTS);
719 }
720 
721 /*!
722  * \brief helper routine to locate a file with a given format
723  * and language preference.
724  *
725  * \note Try preflang, preflang with stripped '_' suffices, or NULL.
726  *
727  * \note The last parameter(s) point to a buffer of sufficient size,
728  * which on success is filled with the matching filename.
729  *
730  * \param filename Name of the file.
731  * \param fmt Format to look for the file in. OPTIONAL
732  * \param preflang The perfered language
733  * \param buf Returns the matching filename
734  * \param buflen Size of the buf
735  * \param result_cap OPTIONAL format capabilities result structure
736  * returns what formats the file was found in.
737  *
738  * \retval 1 true. file exists and result format is set
739  * \retval 0 false. file does not exist.
740  */
741 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
742  char *buf, int buflen, struct ast_format_cap *result_cap)
743 {
744  char *lang;
745 
746  if (buf == NULL) {
747  return 0;
748  }
749 
750  /* We try languages in the following order:
751  * preflang (may include dialect and style codes)
752  * lang (preflang without dialect - if any)
753  * <none>
754  * default (unless the same as preflang or lang without dialect)
755  */
756 
757  lang = ast_strdupa(preflang);
758 
759  /* Try preferred language, including removing any style or dialect codes */
760  while (!ast_strlen_zero(lang)) {
761  char *end;
762 
763  if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) {
764  return 1;
765  }
766 
767  if ((end = strrchr(lang, '_')) != NULL) {
768  *end = '\0';
769  continue;
770  }
771 
772  break;
773  }
774 
775  /* Try without any language */
776  if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) {
777  return 1;
778  }
779 
780  /* Finally try the default language unless it was already tried before */
781  if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
782  if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) {
783  return 1;
784  }
785  }
786 
787  return 0;
788 }
789 
790 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
791 {
792  return ast_openstream_full(chan, filename, preflang, 0);
793 }
794 
795 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
796 {
797  /*
798  * Use fileexists_core() to find a file in a compatible
799  * language and format, set up a suitable translator,
800  * and open the stream.
801  */
802  struct ast_format_cap *file_fmt_cap;
803  int res;
804  int buflen;
805  char *buf;
806 
807  if (!asis) {
808  /* do this first, otherwise we detect the wrong writeformat */
809  ast_stopstream(chan);
810  if (ast_channel_generator(chan))
812  }
813  if (preflang == NULL)
814  preflang = "";
815  buflen = strlen(preflang) + strlen(filename) + 4;
816  buf = ast_alloca(buflen);
817 
818  if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
819  return NULL;
820  }
821  if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
823 
824  ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
825  ao2_ref(file_fmt_cap, -1);
826  return NULL;
827  }
828 
829  /* Set the channel to a format we can work with and save off the previous format. */
830  ast_channel_lock(chan);
832  /* Set the channel to the best format that exists for the file. */
833  res = ast_set_write_format_from_cap(chan, file_fmt_cap);
834  ast_channel_unlock(chan);
835  /* don't need this anymore now that the channel's write format is set. */
836  ao2_ref(file_fmt_cap, -1);
837 
838  if (res == -1) { /* No format available that works with this channel */
839  return NULL;
840  }
841  res = filehelper(buf, chan, NULL, ACTION_OPEN);
842  if (res >= 0)
843  return ast_channel_stream(chan);
844  return NULL;
845 }
846 
847 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
848 {
849  /* As above, but for video. But here we don't have translators
850  * so we must enforce a format.
851  */
852  struct ast_format_cap *nativeformats, *tmp_cap;
853  char *buf;
854  int buflen;
855  int i, fd;
856 
857  if (preflang == NULL) {
858  preflang = "";
859  }
860  buflen = strlen(preflang) + strlen(filename) + 4;
861  buf = ast_alloca(buflen);
862 
863  ast_channel_lock(chan);
864  nativeformats = ao2_bump(ast_channel_nativeformats(chan));
865  ast_channel_unlock(chan);
866 
867  /* is the channel capable of video without translation ?*/
868  if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) {
869  ao2_cleanup(nativeformats);
870  return NULL;
871  }
873  ao2_cleanup(nativeformats);
874  return NULL;
875  }
876  /* Video is supported, so see what video formats exist for this file */
877  if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
878  ao2_ref(tmp_cap, -1);
879  ao2_cleanup(nativeformats);
880  return NULL;
881  }
882 
883  /* iterate over file formats and pick the first one compatible with the channel's native formats */
884  for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
885  struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
886 
888  !ast_format_cap_iscompatible(nativeformats, tmp_cap)) {
889  ao2_ref(format, -1);
890  continue;
891  }
892 
894  if (fd >= 0) {
895  ao2_ref(format, -1);
896  ao2_ref(tmp_cap, -1);
897  ao2_cleanup(nativeformats);
898  return ast_channel_vstream(chan);
899  }
900  ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
901  ao2_ref(format, -1);
902  }
903  ao2_ref(tmp_cap, -1);
904  ao2_cleanup(nativeformats);
905 
906  return NULL;
907 }
908 
909 static struct ast_frame *read_frame(struct ast_filestream *s, int *whennext)
910 {
911  struct ast_frame *fr, *new_fr;
912 
913  if (!s || !s->fmt) {
914  return NULL;
915  }
916 
917  if (!(fr = s->fmt->read(s, whennext))) {
918  return NULL;
919  }
920 
921  if (!(new_fr = ast_frisolate(fr))) {
922  ast_frfree(fr);
923  return NULL;
924  }
925 
926  if (new_fr != fr) {
927  ast_frfree(fr);
928  fr = new_fr;
929  }
930 
931  return fr;
932 }
933 
935 {
936  int whennext = 0;
937 
938  return read_frame(s, &whennext);
939 }
940 
945 };
946 
947 static int ast_fsread_audio(const void *data);
948 
949 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
950 {
951  int whennext = 0;
952 
953  while (!whennext) {
954  struct ast_frame *fr;
955 
956  if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
957  goto return_failure;
958  }
959 
960  fr = read_frame(s, &whennext);
961 
962  if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
963  if (fr) {
964  ast_debug(2, "Failed to write frame\n");
965  ast_frfree(fr);
966  }
967  goto return_failure;
968  }
969 
970  if (fr) {
971  ast_frfree(fr);
972  }
973  }
974 
975  if (whennext != s->lasttimeout) {
976  if (ast_channel_timingfd(s->owner) > -1) {
977  float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
978  unsigned int rate;
979 
980  rate = (unsigned int) roundf(samp_rate / ((float) whennext));
981 
982  ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
983  } else {
985  }
986  s->lasttimeout = whennext;
987  return FSREAD_SUCCESS_NOSCHED;
988  }
989  return FSREAD_SUCCESS_SCHED;
990 
991 return_failure:
993  ast_settimeout(s->owner, 0, NULL, NULL);
994  return FSREAD_FAILURE;
995 }
996 
997 static int ast_fsread_audio(const void *data)
998 {
999  struct ast_filestream *fs = (struct ast_filestream *)data;
1000  enum fsread_res res;
1001 
1002  res = ast_readaudio_callback(fs);
1003 
1004  if (res == FSREAD_SUCCESS_SCHED)
1005  return 1;
1006 
1007  return 0;
1008 }
1009 
1010 static int ast_fsread_video(const void *data);
1011 
1012 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
1013 {
1014  int whennext = 0;
1015 
1016  while (!whennext) {
1017  struct ast_frame *fr = read_frame(s, &whennext);
1018 
1019  if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
1020  if (fr) {
1021  ast_debug(2, "Failed to write frame\n");
1022  ast_frfree(fr);
1023  }
1025  return FSREAD_FAILURE;
1026  }
1027 
1028  if (fr) {
1029  ast_frfree(fr);
1030  }
1031  }
1032 
1033  if (whennext != s->lasttimeout) {
1035  s->lasttimeout = whennext;
1036  return FSREAD_SUCCESS_NOSCHED;
1037  }
1038 
1039  return FSREAD_SUCCESS_SCHED;
1040 }
1041 
1042 static int ast_fsread_video(const void *data)
1043 {
1044  struct ast_filestream *fs = (struct ast_filestream *)data;
1045  enum fsread_res res;
1046 
1047  res = ast_readvideo_callback(fs);
1048 
1049  if (res == FSREAD_SUCCESS_SCHED)
1050  return 1;
1051 
1052  return 0;
1053 }
1054 
1055 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
1056 {
1057  s->owner = chan;
1058  return 0;
1059 }
1060 
1062 {
1063  enum fsread_res res;
1064 
1066  res = ast_readaudio_callback(s);
1067  else
1068  res = ast_readvideo_callback(s);
1069 
1070  return (res == FSREAD_FAILURE) ? -1 : 0;
1071 }
1072 
1073 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
1074 {
1075  return fs->fmt->seek(fs, sample_offset, whence);
1076 }
1077 
1079 {
1080  return fs->fmt->trunc(fs);
1081 }
1082 
1084 {
1085  return fs->fmt->tell(fs);
1086 }
1087 
1089 {
1090  return ast_format_get_sample_rate(fs->fmt->format);
1091 }
1092 
1093 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
1094 {
1095  return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1096 }
1097 
1098 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
1099 {
1100  off_t offset = ast_tellstream(fs);
1101  if (ms * DEFAULT_SAMPLES_PER_MS > offset) {
1102  /* Don't even bother asking the file format to seek to a negative offset... */
1103  ast_debug(1, "Restarting, rather than seeking to negative offset %ld\n", (long) (offset - (ms * DEFAULT_SAMPLES_PER_MS)));
1104  return ast_seekstream(fs, 0, SEEK_SET);
1105  }
1106  return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1107 }
1108 
1110 {
1111  /* This used to destroy the filestream, but it now just decrements a refcount.
1112  * We close the stream in order to quit queuing frames now, because we might
1113  * change the writeformat, which could result in a subsequent write error, if
1114  * the format is different. */
1115  if (f == NULL) {
1116  return 0;
1117  }
1119  ao2_ref(f, -1);
1120  return 0;
1121 }
1122 
1123 
1124 /*
1125  * Look the various language-specific places where a file could exist.
1126  */
1127 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
1128 {
1129  char *buf;
1130  int buflen;
1131 
1132  if (preflang == NULL)
1133  preflang = "";
1134  buflen = strlen(preflang) + strlen(filename) + 4; /* room for everything */
1135  buf = ast_alloca(buflen);
1136  return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1137 }
1138 
1139 int ast_filedelete(const char *filename, const char *fmt)
1140 {
1142 }
1143 
1144 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
1145 {
1146  return filehelper(filename, filename2, fmt, ACTION_RENAME);
1147 }
1148 
1149 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
1150 {
1151  return filehelper(filename, filename2, fmt, ACTION_COPY);
1152 }
1153 
1154 static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file,
1155  void *obj, int max_depth)
1156 {
1157  DIR *dir;
1158  struct dirent *entry;
1159  int res;
1160 
1161  if (!(dir = opendir(path))) {
1162  ast_log(LOG_ERROR, "Error opening directory - %s: %s\n",
1163  path, strerror(errno));
1164  return -1;
1165  }
1166 
1167  --max_depth;
1168 
1169  res = 0;
1170 
1171  while ((entry = readdir(dir)) != NULL && !errno) {
1172  int is_file = 0;
1173  int is_dir = 0;
1174  RAII_VAR(char *, full_path, NULL, ast_free);
1175 
1176  if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
1177  continue;
1178  }
1179 
1180 /*
1181  * If the dirent structure has a d_type use it to determine if we are dealing with
1182  * a file or directory. Unfortunately if it doesn't have it, or if the type is
1183  * unknown, or a link then we'll need to use the stat function instead.
1184  */
1185 #ifdef _DIRENT_HAVE_D_TYPE
1186  if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK) {
1187  is_file = entry->d_type == DT_REG;
1188  is_dir = entry->d_type == DT_DIR;
1189  } else
1190 #endif
1191  {
1192  struct stat statbuf;
1193 
1194  /*
1195  * Don't use alloca or we risk blowing out the stack if recursing
1196  * into subdirectories.
1197  */
1198  full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
1199  if (!full_path) {
1200  return -1;
1201  }
1202  sprintf(full_path, "%s/%s", path, entry->d_name);
1203 
1204  if (stat(full_path, &statbuf)) {
1205  ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n",
1206  full_path, strerror(errno));
1207  /*
1208  * Output an error, but keep going. It could just be
1209  * a broken link and other files could be fine.
1210  */
1211  continue;
1212  }
1213 
1214  is_file = S_ISREG(statbuf.st_mode);
1215  is_dir = S_ISDIR(statbuf.st_mode);
1216  }
1217 
1218  if (is_file) {
1219  /* If the handler returns non-zero then stop */
1220  if ((res = on_file(path, entry->d_name, obj))) {
1221  break;
1222  }
1223  /* Otherwise move on to next item in directory */
1224  continue;
1225  }
1226 
1227  if (!is_dir) {
1228  ast_debug(5, "Skipping %s: not a regular file or directory\n", full_path);
1229  continue;
1230  }
1231 
1232  /* Only re-curse into sub-directories if not at the max depth */
1233  if (max_depth != 0) {
1234  if (!full_path) {
1235  /* Don't use alloca. See note above. */
1236  full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
1237  if (!full_path) {
1238  return -1;
1239  }
1240  sprintf(full_path, "%s/%s", path, entry->d_name);
1241  }
1242 
1243  if ((res = __ast_file_read_dirs(full_path, on_file, obj, max_depth))) {
1244  break;
1245  }
1246  }
1247  }
1248 
1249  closedir(dir);
1250 
1251  if (!res && errno) {
1252  ast_log(LOG_ERROR, "Error while reading directories - %s: %s\n",
1253  path, strerror(errno));
1254  res = -1;
1255  }
1256 
1257  return res;
1258 }
1259 
1260 #if !defined(__GLIBC__)
1261 /*!
1262  * \brief Lock to hold when iterating over directories.
1263  *
1264  * Currently, 'readdir' is not required to be thread-safe. In most modern implementations
1265  * it should be safe to make concurrent calls into 'readdir' that specify different directory
1266  * streams (glibc would be one of these). However, since it is potentially unsafe for some
1267  * implementations we'll use our own locking in order to achieve synchronization for those.
1268  */
1270 #endif
1271 
1272 int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
1273 {
1274  int res;
1275 
1276  errno = 0;
1277 
1278 #if !defined(__GLIBC__)
1280 #endif
1281 
1282  res = __ast_file_read_dirs(dir_name, on_file, obj, max_depth);
1283 
1284 #if !defined(__GLIBC__)
1286 #endif
1287 
1288  return res;
1289 }
1290 
1291 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
1292 {
1293  struct ast_filestream *fs;
1294  struct ast_filestream *vfs=NULL;
1295  off_t pos;
1296  int seekattempt;
1297  int res;
1298 
1299  fs = ast_openstream(chan, filename, preflang);
1300  if (!fs) {
1301  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
1302  ast_channel_lock(chan);
1303  ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
1304  filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
1305  ast_channel_unlock(chan);
1306  return -1;
1307  }
1308 
1309  /* check to see if there is any data present (not a zero length file),
1310  * done this way because there is no where for ast_openstream_full to
1311  * return the file had no data. */
1312  pos = ftello(fs->f);
1313  seekattempt = fseeko(fs->f, -1, SEEK_END);
1314  if (seekattempt) {
1315  if (errno == EINVAL) {
1316  /* Zero-length file, as opposed to a pipe */
1317  return 0;
1318  } else {
1319  ast_seekstream(fs, 0, SEEK_SET);
1320  }
1321  } else {
1322  fseeko(fs->f, pos, SEEK_SET);
1323  }
1324 
1325  vfs = ast_openvstream(chan, filename, preflang);
1326  if (vfs) {
1327  ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
1328  }
1329 
1332  if (ast_applystream(chan, fs))
1333  return -1;
1334  if (vfs && ast_applystream(chan, vfs))
1335  return -1;
1336  ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, ast_channel_name(chan));
1337  res = ast_playstream(fs);
1338  if (!res && vfs)
1339  res = ast_playstream(vfs);
1340 
1341  if (VERBOSITY_ATLEAST(3)) {
1342  ast_channel_lock(chan);
1343  ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default");
1344  ast_channel_unlock(chan);
1345  }
1346 
1347  return res;
1348 }
1349 
1350 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1351 {
1352  FILE *bfile;
1353  struct ast_format_def *f;
1354  struct ast_filestream *fs = NULL;
1355  char *fn;
1356  int format_found = 0;
1357 
1359 
1360  AST_RWLIST_TRAVERSE(&formats, f, list) {
1361  fs = NULL;
1362  if (!exts_compare(f->exts, type))
1363  continue;
1364  else
1365  format_found = 1;
1366 
1367  fn = build_filename(filename, type);
1368  if (!fn) {
1369  continue;
1370  }
1371  errno = 0;
1372  bfile = fopen(fn, "r");
1373 
1374  if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1375  ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1376  if (fs) {
1377  ast_closestream(fs);
1378  }
1379  fs = NULL;
1380  bfile = NULL;
1381  ast_free(fn);
1382  break;
1383  }
1384  /* found it */
1385  fs->trans = NULL;
1386  fs->fmt = f;
1387  fs->flags = flags;
1388  fs->mode = mode;
1389  fs->filename = ast_strdup(filename);
1390  fs->vfs = NULL;
1391  ast_free(fn);
1392  break;
1393  }
1394 
1396  if (!format_found)
1397  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1398 
1399  return fs;
1400 }
1401 
1402 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
1403 {
1404  int fd, myflags = 0;
1405  /* compiler claims this variable can be used before initialization... */
1406  FILE *bfile = NULL;
1407  struct ast_format_def *f;
1408  struct ast_filestream *fs = NULL;
1409  char *buf = NULL;
1410  size_t size = 0;
1411  int format_found = 0;
1412 
1414 
1415  /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1416  /* We really can't use O_APPEND as it will break WAV header updates */
1417  if (flags & O_APPEND) {
1418  flags &= ~O_APPEND;
1419  } else {
1420  myflags = O_TRUNC;
1421  }
1422 
1423  myflags |= O_WRONLY | O_CREAT;
1424 
1425  /* XXX need to fix this - we should just do the fopen,
1426  * not open followed by fdopen()
1427  */
1428  AST_RWLIST_TRAVERSE(&formats, f, list) {
1429  char *fn, *orig_fn = NULL;
1430  if (fs)
1431  break;
1432 
1433  if (!exts_compare(f->exts, type))
1434  continue;
1435  else
1436  format_found = 1;
1437 
1438  fn = build_filename(filename, type);
1439  if (!fn) {
1440  continue;
1441  }
1442  fd = open(fn, flags | myflags, mode);
1443  if (fd > -1) {
1444  /* fdopen() the resulting file stream */
1445  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1446  if (!bfile) {
1447  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1448  close(fd);
1449  fd = -1;
1450  }
1451  }
1452 
1453  if (ast_opt_cache_record_files && (fd > -1)) {
1454  char *c;
1455 
1456  fclose(bfile); /* this also closes fd */
1457  /*
1458  We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1459  What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1460  */
1461  orig_fn = ast_strdup(fn);
1462  for (c = fn; *c; c++)
1463  if (*c == '/')
1464  *c = '_';
1465 
1466  size = strlen(fn) + strlen(record_cache_dir) + 2;
1467  buf = ast_malloc(size);
1468  strcpy(buf, record_cache_dir);
1469  strcat(buf, "/");
1470  strcat(buf, fn);
1471  ast_free(fn);
1472  fn = buf;
1473  fd = open(fn, flags | myflags, mode);
1474  if (fd > -1) {
1475  /* fdopen() the resulting file stream */
1476  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1477  if (!bfile) {
1478  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1479  close(fd);
1480  fd = -1;
1481  }
1482  }
1483  }
1484  if (fd > -1) {
1485  errno = 0;
1486  fs = get_filestream(f, bfile);
1487  if (fs) {
1488  if ((fs->write_buffer = ast_malloc(32768))) {
1489  setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1490  }
1491  }
1492  if (!fs || rewrite_wrapper(fs, comment)) {
1493  ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1494  close(fd);
1495  if (orig_fn) {
1496  unlink(fn);
1497  unlink(orig_fn);
1498  ast_free(orig_fn);
1499  }
1500  if (fs) {
1501  ast_closestream(fs);
1502  fs = NULL;
1503  }
1504  /*
1505  * 'fn' was has either been allocated from build_filename, or that was freed
1506  * and now 'fn' points to memory allocated for 'buf'. Either way the memory
1507  * now needs to be released.
1508  */
1509  ast_free(fn);
1510  continue;
1511  }
1512  fs->trans = NULL;
1513  fs->fmt = f;
1514  fs->flags = flags;
1515  fs->mode = mode;
1516  if (orig_fn) {
1517  fs->realfilename = orig_fn;
1518  fs->filename = fn;
1519  /*
1520  * The above now manages the memory allocated for 'orig_fn' and 'fn', so
1521  * set them to NULL, so they don't get released at the end of the loop.
1522  */
1523  orig_fn = NULL;
1524  fn = NULL;
1525  } else {
1526  fs->realfilename = NULL;
1527  fs->filename = ast_strdup(filename);
1528  }
1529  fs->vfs = NULL;
1530  /* If truncated, we'll be at the beginning; if not truncated, then append */
1531  f->seek(fs, 0, SEEK_END);
1532  } else if (errno != EEXIST) {
1533  ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1534  if (orig_fn)
1535  unlink(orig_fn);
1536  }
1537  /* Free 'fn', or if 'fn' points to 'buf' then free 'buf' */
1538  ast_free(fn);
1539  ast_free(orig_fn);
1540  }
1541 
1543 
1544  if (!format_found)
1545  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1546 
1547  return fs;
1548 }
1549 
1550 static void waitstream_control(struct ast_channel *c,
1553  int skip_ms)
1554 {
1555  switch (type)
1556  {
1558  {
1559  int eoftest;
1561  eoftest = fgetc(ast_channel_stream(c)->f);
1562  if (feof(ast_channel_stream(c)->f)) {
1564  } else {
1565  ungetc(eoftest, ast_channel_stream(c)->f);
1566  }
1567  }
1568  break;
1571  break;
1572  default:
1573  break;
1574  }
1575 
1576  if (cb) {
1578  cb(c, ms_len, type);
1579  }
1580 
1581  ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1582  "Control: %s\r\n"
1583  "SkipMs: %d\r\n",
1585  (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
1586  skip_ms);
1587 }
1588 
1589 /*!
1590  * \brief the core of all waitstream() functions
1591  */
1592 static int waitstream_core(struct ast_channel *c,
1593  const char *breakon,
1594  const char *forward,
1595  const char *reverse,
1596  int skip_ms,
1597  int audiofd,
1598  int cmdfd,
1599  const char *context,
1601 {
1602  const char *orig_chan_name = NULL;
1603 
1604  int err = 0;
1605 
1606  if (!breakon)
1607  breakon = "";
1608  if (!forward)
1609  forward = "";
1610  if (!reverse)
1611  reverse = "";
1612 
1613  /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1615 
1618 
1619  if (ast_channel_stream(c) && cb) {
1621  cb(c, ms_len, AST_WAITSTREAM_CB_START);
1622  }
1623 
1624  while (ast_channel_stream(c)) {
1625  int res;
1626  int ms;
1627 
1628  if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
1629  ast_stopstream(c);
1630  err = 1;
1631  break;
1632  }
1633 
1635 
1636  if (ms < 0 && !ast_channel_timingfunc(c)) {
1637  ast_stopstream(c);
1638  break;
1639  }
1640  if (ms < 0)
1641  ms = 1000;
1642  if (cmdfd < 0) {
1643  res = ast_waitfor(c, ms);
1644  if (res < 0) {
1645  ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1647  return res;
1648  }
1649  } else {
1650  int outfd;
1651  struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1652  if (!rchan && (outfd < 0) && (ms)) {
1653  /* Continue */
1654  if (errno == EINTR)
1655  continue;
1656  ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1658  return -1;
1659  } else if (outfd > -1) { /* this requires cmdfd set */
1660  /* The FD we were watching has something waiting */
1662  return 1;
1663  }
1664  /* if rchan is set, it is 'c' */
1665  res = rchan ? 1 : 0; /* map into 'res' values */
1666  }
1667  if (res > 0) {
1668  struct ast_frame *fr = ast_read(c);
1669  if (!fr) {
1671  return -1;
1672  }
1673  switch (fr->frametype) {
1674  case AST_FRAME_DTMF_END:
1675  if (context) {
1676  const char exten[2] = { fr->subclass.integer, '\0' };
1678  S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
1679  res = fr->subclass.integer;
1680  ast_frfree(fr);
1682  return res;
1683  }
1684  } else {
1685  res = fr->subclass.integer;
1686  if (strchr(forward, res)) {
1688  } else if (strchr(reverse, res)) {
1690  } else if (strchr(breakon, res)) {
1691  ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1692  "Control: %s\r\n",
1694  "Break");
1695 
1696  ast_frfree(fr);
1698  return res;
1699  }
1700  }
1701  break;
1702  case AST_FRAME_CONTROL:
1703  switch (fr->subclass.integer) {
1707  /* Fall-through and break out */
1708  ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1709  "Control: %s\r\n",
1711  "Break");
1712  res = fr->subclass.integer;
1713  ast_frfree(fr);
1715  return res;
1717  if (!skip_ms) {
1718  skip_ms = 3000;
1719  }
1721  break;
1723  if (!skip_ms) {
1724  skip_ms = 3000;
1725  }
1727  break;
1728  case AST_CONTROL_HANGUP:
1729  case AST_CONTROL_BUSY:
1731  ast_frfree(fr);
1733  return -1;
1734  case AST_CONTROL_RINGING:
1735  case AST_CONTROL_ANSWER:
1736  case AST_CONTROL_VIDUPDATE:
1737  case AST_CONTROL_SRCUPDATE:
1738  case AST_CONTROL_SRCCHANGE:
1739  case AST_CONTROL_HOLD:
1740  case AST_CONTROL_UNHOLD:
1743  case AST_CONTROL_AOC:
1746  case AST_CONTROL_FLASH:
1747  case -1:
1748  /* Unimportant */
1749  break;
1750  default:
1751  ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer);
1752  }
1753  break;
1754  case AST_FRAME_VOICE:
1755  /* Write audio if appropriate */
1756  if (audiofd > -1) {
1757  if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1758  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1759  }
1760  }
1761  default:
1762  /* Ignore all others */
1763  break;
1764  }
1765  ast_frfree(fr);
1766  }
1768  }
1769 
1771 
1772  return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
1773 }
1774 
1776  const char *breakon,
1777  const char *forward,
1778  const char *reverse,
1779  int ms,
1781 {
1782  return waitstream_core(c, breakon, forward, reverse, ms,
1783  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1784 }
1785 
1786 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1787 {
1788  return waitstream_core(c, breakon, forward, reverse, ms,
1789  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1790 }
1791 
1792 /*! \internal
1793  * \brief Clean up the return value of a waitstream call
1794  *
1795  * It's possible for a control frame to come in from an external source and break the
1796  * playback. From a consumer of most ast_waitstream_* function callers, this should
1797  * appear like normal playback termination, i.e., return 0 and not the value of the
1798  * control frame.
1799  */
1800 static int sanitize_waitstream_return(int return_value)
1801 {
1802  switch (return_value) {
1806  /* Fall through and set return_value to 0 */
1807  return_value = 0;
1808  break;
1809  default:
1810  /* Do nothing */
1811  break;
1812  }
1813 
1814  return return_value;
1815 }
1816 
1817 int ast_waitstream(struct ast_channel *c, const char *breakon)
1818 {
1819  int res;
1820 
1821  res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1822 
1823  return sanitize_waitstream_return(res);
1824 }
1825 
1826 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1827 {
1828  int res;
1829 
1830  res = waitstream_core(c, breakon, NULL, NULL, 0,
1831  audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1832 
1833  return sanitize_waitstream_return(res);
1834 }
1835 
1836 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1837 {
1838  int res;
1839 
1840  /* Waitstream, with return in the case of a valid 1 digit extension */
1841  /* in the current or specified context being pressed */
1842  if (!context)
1844  res = waitstream_core(c, NULL, NULL, NULL, 0,
1845  -1, -1, context, NULL /* no callback */);
1846 
1847  return sanitize_waitstream_return(res);
1848 }
1849 
1850 /*
1851  * if the file name is non-empty, try to play it.
1852  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1853  * If digits == "" then we can simply check for non-zero.
1854  */
1855 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1856 {
1857  int res = 0;
1858  if (!ast_strlen_zero(file)) {
1859  res = ast_streamfile(chan, file, ast_channel_language(chan));
1860  if (!res) {
1861  res = ast_waitstream(chan, digits);
1862  }
1863  }
1864  return res;
1865 }
1866 
1867 char *ast_format_str_reduce(char *fmts)
1868 {
1869  struct ast_format_def *f;
1870  struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1871  char *fmts_str[AST_MAX_FORMATS];
1872  char *stringp, *type;
1873  char *orig = fmts;
1874  int i, j, x, first, found = 0;
1875  int len = strlen(fmts) + 1;
1876  int res;
1877 
1878  if (AST_RWLIST_RDLOCK(&formats)) {
1879  ast_log(LOG_WARNING, "Unable to lock format list\n");
1880  return NULL;
1881  }
1882 
1883  stringp = ast_strdupa(fmts);
1884 
1885  for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1887  if (exts_compare(f->exts, type)) {
1888  found = 1;
1889  break;
1890  }
1891  }
1892 
1893  fmts_str[x] = type;
1894  if (found) {
1895  fmts_ptr[x] = f;
1896  } else {
1897  fmts_ptr[x] = NULL;
1898  }
1899  }
1901 
1902  first = 1;
1903  for (i = 0; i < x; i++) {
1904  /* ignore invalid entries */
1905  if (!fmts_ptr[i]) {
1906  ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1907  continue;
1908  }
1909 
1910  /* special handling for the first entry */
1911  if (first) {
1912  res = snprintf(fmts, len, "%s", fmts_str[i]);
1913  fmts += res;
1914  len -= res;
1915  first = 0;
1916  continue;
1917  }
1918 
1919  found = 0;
1920  for (j = 0; j < i; j++) {
1921  /* this is a duplicate */
1922  if (fmts_ptr[j] == fmts_ptr[i]) {
1923  found = 1;
1924  break;
1925  }
1926  }
1927 
1928  if (!found) {
1929  res = snprintf(fmts, len, "|%s", fmts_str[i]);
1930  fmts += res;
1931  len -= res;
1932  }
1933  }
1934 
1935  if (first) {
1936  ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1937  return NULL;
1938  }
1939 
1940  return orig;
1941 }
1942 
1943 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1944 {
1945 #define FORMAT "%-10s %-10s %-20s\n"
1946 #define FORMAT2 "%-10s %-10s %-20s\n"
1947  struct ast_format_def *f;
1948  int count_fmt = 0;
1949 
1950  switch (cmd) {
1951  case CLI_INIT:
1952  e->command = "core show file formats";
1953  e->usage =
1954  "Usage: core show file formats\n"
1955  " Displays currently registered file formats (if any).\n";
1956  return NULL;
1957  case CLI_GENERATE:
1958  return NULL;
1959  }
1960 
1961  if (a->argc != 4)
1962  return CLI_SHOWUSAGE;
1963 
1964  ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1965  ast_cli(a->fd, FORMAT, "------", "----", "----------");
1966 
1969  ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts);
1970  count_fmt++;
1971  }
1973  ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1974  return CLI_SUCCESS;
1975 #undef FORMAT
1976 #undef FORMAT2
1977 }
1978 
1979 struct ast_format *ast_get_format_for_file_ext(const char *file_ext)
1980 {
1981  struct ast_format_def *f;
1984  if (exts_compare(f->exts, file_ext)) {
1985  return f->format;
1986  }
1987  }
1988 
1989  return NULL;
1990 }
1991 
1992 int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity)
1993 {
1994  struct ast_format_def *f;
1996 
1997  ast_assert(buffer && capacity);
1998 
2000  if (type_in_list(f->mime_types, mime_type, strcasecmp)) {
2001  size_t item_len = strcspn(f->exts, "|");
2002  size_t bytes_written = snprintf(buffer, capacity, ".%.*s", (int) item_len, f->exts);
2003  if (bytes_written < capacity) {
2004  /* Only return success if we didn't truncate */
2005  return 1;
2006  }
2007  }
2008  }
2009 
2010  return 0;
2011 }
2012 
2013 static struct ast_cli_entry cli_file[] = {
2014  AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
2015 };
2016 
2017 static void file_shutdown(void)
2018 {
2022 }
2023 
2024 int ast_file_init(void)
2025 {
2030  return 0;
2031 }
Prototypes for public functions only of internal interest,.
#define comment
Definition: ael_lex.c:976
jack_status_t status
Definition: app_jack.c:146
struct sla_ringing_trunk * first
Definition: app_meetme.c:1094
ast_mutex_t lock
Definition: app_meetme.c:1093
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
Asterisk main include file. File version handling, generic pbx functions.
#define DEFAULT_LANGUAGE
Definition: asterisk.h:46
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define AST_FILE_MODE
Definition: asterisk.h:32
#define DEFAULT_SAMPLES_PER_MS
Definition: asterisk.h:49
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
Definition: astobj2.h:501
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
static int tmp()
Definition: bt_open.c:389
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:122
static snd_pcm_format_t format
Definition: chan_alsa.c:106
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:120
static const char type[]
Definition: chan_ooh323.c:109
General Asterisk PBX channel definitions.
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
Clear a flag on a channel.
Definition: channel.c:11220
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
struct ast_channel * ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds, int *exception, int *outfd, int *ms)
Waits for activity on a group of channels.
Definition: channel.c:2989
#define ast_channel_lock(chan)
Definition: channel.h:2922
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
struct ast_format * ast_channel_oldwriteformat(struct ast_channel *chan)
const char * ast_channel_context(const struct ast_channel *chan)
void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3163
int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data, unsigned int is_ao2_obj)
Definition: channel.c:3186
void ast_channel_streamid_set(struct ast_channel *chan, int value)
struct ast_generator * ast_channel_generator(const struct ast_channel *chan)
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:2894
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5179
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4292
@ AST_FLAG_MASQ_NOSTREAM
Definition: channel.h:1014
@ AST_FLAG_END_DTMF_ONLY
Definition: channel.h:1007
struct ast_filestream * ast_channel_vstream(const struct ast_channel *chan)
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
const char * ast_channel_language(const struct ast_channel *chan)
int ast_channel_timingfd(const struct ast_channel *chan)
void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
Set a flag on a channel.
Definition: channel.c:11213
int ast_channel_vstreamid(const struct ast_channel *chan)
ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
int ast_settimeout(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data)
Enable or disable timer ticks for a channel.
Definition: channel.c:3181
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5880
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
int ast_channel_streamid(const struct ast_channel *chan)
struct ast_sched_context * ast_channel_sched(const struct ast_channel *chan)
int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2923
void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
int ast_set_write_format_from_cap(struct ast_channel *chan, struct ast_format_cap *formats)
Sets write format on channel chan Set write format for channel to whichever component of "format" is ...
Definition: channel.c:5898
Standard Command Line Interface.
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
ast_media_type
Types of media.
Definition: codec.h:30
@ AST_MEDIA_TYPE_AUDIO
Definition: codec.h:32
@ AST_MEDIA_TYPE_VIDEO
Definition: codec.h:33
#define SENTINEL
Definition: compiler.h:87
char * end
Definition: eagi_proxy.c:73
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1083
fsread_res
Definition: file.c:941
@ FSREAD_FAILURE
Definition: file.c:942
@ FSREAD_SUCCESS_SCHED
Definition: file.c:943
@ FSREAD_SUCCESS_NOSCHED
Definition: file.c:944
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:549
int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
Definition: file.c:1826
int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1291
int ast_file_fdtemp(const char *path, char **filename, const char *template_name)
Create a temporary file located at path.
Definition: file.c:202
int ast_language_is_prefix
The following variable controls the layout of localized sound files. If 0, use the historical layout ...
Definition: file.c:67
static char * build_filename(const char *filename, const char *ext)
construct a filename. Absolute pathnames are preserved, relative names are prefixed by the sounds/ di...
Definition: file.c:349
int ast_stopstream(struct ast_channel *tmp)
Stops a stream.
Definition: file.c:222
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:244
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1073
static int ast_fsread_video(const void *data)
Definition: file.c:1042
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:909
static struct ast_filestream * get_filestream(struct ast_format_def *fmt, FILE *bfile)
Definition: file.c:463
wrap_fn
Definition: file.c:501
@ WRAP_OPEN
Definition: file.c:501
@ WRAP_REWRITE
Definition: file.c:501
int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
Same as waitstream but allows stream to be forwarded or rewound.
Definition: file.c:1786
struct ast_format * ast_get_format_for_file_ext(const char *file_ext)
Get the ast_format associated with the given file extension.
Definition: file.c:1979
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
static void file_shutdown(void)
Definition: file.c:2017
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:1098
static int sanitize_waitstream_return(int return_value)
Definition: file.c:1800
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applies a open stream to a channel.
Definition: file.c:1055
int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth)
Recursively iterate through files and directories up to max_depth.
Definition: file.c:1272
static void filestream_destructor(void *arg)
Definition: file.c:418
static void waitstream_control(struct ast_channel *c, enum ast_waitstream_fr_cb_values type, ast_waitstream_fr_cb cb, int skip_ms)
Definition: file.c:1550
static int type_in_list(const char *list, const char *type, int(*cmp)(const char *s1, const char *s2))
Definition: file.c:373
int ast_filerename(const char *filename, const char *filename2, const char *fmt)
Renames a file.
Definition: file.c:1144
static void filestream_close(struct ast_filestream *f)
Definition: file.c:392
file_action
Definition: file.c:530
@ ACTION_OPEN
Definition: file.c:534
@ ACTION_EXISTS
Definition: file.c:531
@ ACTION_COPY
Definition: file.c:535
@ ACTION_DELETE
Definition: file.c:532
@ ACTION_RENAME
Definition: file.c:533
static int copy(const char *infile, const char *outfile)
Definition: file.c:306
int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
stream file until digit If the file name is non-empty, try to play it.
Definition: file.c:1855
struct ast_frame * ast_readframe(struct ast_filestream *s)
Read a frame from a filestream.
Definition: file.c:934
struct ast_filestream * ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts writing a file.
Definition: file.c:1402
static int is_remote_path(const char *filename)
Definition: file.c:680
static int ast_fsread_audio(const void *data)
Definition: file.c:997
static ast_mutex_t read_dirs_lock
Lock to hold when iterating over directories.
Definition: file.c:1269
static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file, void *obj, int max_depth)
Definition: file.c:1154
char * ast_format_str_reduce(char *fmts)
Definition: file.c:1867
int ast_truncstream(struct ast_filestream *fs)
Trunc stream at current location.
Definition: file.c:1078
static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
Definition: file.c:949
STASIS_MESSAGE_TYPE_DEFN(ast_format_register_type)
static int is_absolute_path(const char *filename)
Definition: file.c:675
int ast_ratestream(struct ast_filestream *fs)
Return the sample rate of the stream's format.
Definition: file.c:1088
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1109
static int waitstream_core(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int skip_ms, int audiofd, int cmdfd, const char *context, ast_waitstream_fr_cb cb)
the core of all waitstream() functions
Definition: file.c:1592
int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
Checks for the existence of a given file.
Definition: file.c:1127
static struct ast_cli_entry cli_file[]
Definition: file.c:2013
static int open_wrapper(struct ast_filestream *s)
Definition: file.c:525
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
Fast forward stream ms.
Definition: file.c:1093
static char * handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: file.c:1943
static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
Definition: file.c:503
#define FORMAT
static int fileexists_core(const char *filename, const char *fmt, const char *preflang, char *buf, int buflen, struct ast_format_cap *result_cap)
helper routine to locate a file with a given format and language preference.
Definition: file.c:741
static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
Definition: file.c:520
static int fileexists_test(const char *filename, const char *fmt, const char *lang, char *buf, int buflen, struct ast_format_cap *result_cap)
test if a file exists for a given format.
Definition: file.c:691
int ast_get_extension_for_mime_type(const char *mime_type, char *buffer, size_t capacity)
Get a suitable filename extension for the given MIME type.
Definition: file.c:1992
#define FORMAT2
static struct ast_json * json_array_from_list(const char *list, const char *sep)
Definition: file.c:74
int ast_waitstream_fr_w_cb(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms, ast_waitstream_fr_cb cb)
Same as waitstream_fr but allows a callback to be alerted when a user fastforwards or rewinds the fil...
Definition: file.c:1775
struct ast_filestream * ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
Starts reading from a file.
Definition: file.c:1350
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:1061
#define exts_compare(list, type)
Definition: file.c:386
struct ast_filestream * ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
Opens stream for use in seeking, playing.
Definition: file.c:790
int ast_filedelete(const char *filename, const char *fmt)
Deletes a file.
Definition: file.c:1139
static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
Definition: file.c:93
struct ast_filestream * ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
Opens stream for use in seeking, playing.
Definition: file.c:795
int ast_waitstream_exten(struct ast_channel *c, const char *context)
Waits for a stream to stop or digit matching a valid one digit exten to be pressed.
Definition: file.c:1836
int ast_file_init(void)
Definition: file.c:2024
struct ast_filestream * ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
Opens stream for use in seeking, playing.
Definition: file.c:847
FILE * ast_file_mkftemp(char *template, mode_t mode)
same as mkstemp, but return a FILE
Definition: file.c:187
int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod)
Register a new file format capability. Adds a format to Asterisk's format abilities.
Definition: file.c:124
static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
Definition: file.c:1012
int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
Copies a file.
Definition: file.c:1149
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1817
ast_waitstream_fr_cb_values
Definition: file.h:54
@ AST_WAITSTREAM_CB_FASTFORWARD
Definition: file.h:56
@ AST_WAITSTREAM_CB_REWIND
Definition: file.h:55
@ AST_WAITSTREAM_CB_START
Definition: file.h:57
void() ast_waitstream_fr_cb(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val)
callback used during dtmf controlled file playback to indicate location of playback in a file after r...
Definition: file.h:65
int(* ast_file_on_file)(const char *dir_name, const char *filename, void *obj)
Callback called for each file found when reading directories.
Definition: file.h:178
#define AST_MAX_FORMATS
Definition: file.h:44
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
struct stasis_message_type * ast_format_unregister_type(void)
Get the message type used for signaling a format unregistration.
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
struct stasis_message_type * ast_format_register_type(void)
Get the message type used for signaling a format registration.
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_EQUAL
Definition: format.h:36
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:324
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:734
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition: format_cap.h:38
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
int ast_format_cap_has_type(const struct ast_format_cap *cap, enum ast_media_type type)
Find out if the capabilities structure has any formats of a specific type.
Definition: format_cap.c:613
int ast_format_cap_iscompatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if any joint capabilities exist between two capabilities structures.
Definition: format_cap.c:653
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
static const char name[]
Definition: format_mp3.c:68
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const char * ext
Definition: http.c:150
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int ast_safe_fork(int stop_reaper)
Common routine to safely fork without a chance of a signal handler firing badly in the child.
Definition: main/app.c:3267
char * strsep(char **str, const char *delims)
float roundf(float x)
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
#define ast_frfree(fr)
@ AST_FRAME_VIDEO
@ AST_FRAME_DTMF_END
@ AST_FRAME_VOICE
@ AST_FRAME_CONTROL
@ AST_CONTROL_SRCUPDATE
@ AST_CONTROL_STREAM_RESTART
@ AST_CONTROL_STREAM_SUSPEND
@ AST_CONTROL_BUSY
@ AST_CONTROL_UNHOLD
@ AST_CONTROL_VIDUPDATE
@ AST_CONTROL_STREAM_REVERSE
@ AST_CONTROL_REDIRECTING
@ AST_CONTROL_CONGESTION
@ AST_CONTROL_ANSWER
@ AST_CONTROL_RINGING
@ AST_CONTROL_HANGUP
@ AST_CONTROL_STREAM_STOP
@ AST_CONTROL_HOLD
@ AST_CONTROL_CONNECTED_LINE
@ AST_CONTROL_STREAM_FORWARD
@ AST_CONTROL_FLASH
@ AST_CONTROL_AOC
@ AST_CONTROL_SRCCHANGE
@ AST_CONTROL_PVT_CAUSE_CODE
@ AST_CONTROL_UPDATE_RTP_PEER
#define AST_LOG_WARNING
#define ast_debug(level,...)
Log a DEBUG message.
#define VERBOSITY_ATLEAST(level)
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_NOTICE
#define LOG_WARNING
Asterisk JSON abstraction layer.
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
struct ast_json_payload * ast_json_payload_create(struct ast_json *json)
Create an ao2 object to pass json blobs as data payloads for stasis.
Definition: json.c:735
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
A set of macros to manage forward-linked lists.
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:570
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:718
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
Asterisk locking-related definitions:
#define ast_mutex_unlock(a)
Definition: lock.h:188
#define SCOPED_RDLOCK(varname, lock)
scoped lock specialization for read locks
Definition: lock.h:592
#define ast_mutex_lock(a)
Definition: lock.h:187
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition: lock.h:518
int errno
An in-memory media cache.
int ast_media_cache_retrieve(const char *uri, const char *preferred_file_name, char *file_path, size_t len)
Retrieve an item from the cache.
Definition: media_cache.c:157
Header for providers of file and format handling routines. Clients of these routines should include "...
Asterisk module definitions.
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:455
char record_cache_dir[AST_CACHE_DIR_LEN]
Definition: options.c:96
#define ast_opt_cache_record_files
Definition: options.h:120
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_DATA_DIR
Definition: options.c:158
Core PBX routines and definitions.
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4182
#define NULL
Definition: resample.c:96
Scheduler Routines (derived from cheops)
#define AST_SCHED_DEL_ACCESSOR(sched, obj, getter, setter)
Definition: sched.h:59
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:567
int ast_sched_runq(struct ast_sched_context *con)
Runs the queue.
Definition: sched.c:786
int ast_sched_wait(struct ast_sched_context *con) attribute_warn_unused_result
Determines number of seconds until the next outstanding event to take place.
Definition: sched.c:433
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1515
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition: stasis.c:1513
struct stasis_topic * ast_system_topic(void)
A Stasis Message Bus API topic which publishes messages regarding system changes.
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:87
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_alloca(init_len)
Definition: strings.h:826
Main Channel structure associated with a channel.
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
struct ast_filestream * vfs
Definition: mod_format.h:110
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
char * realfilename
Definition: mod_format.h:108
void * _private
Definition: mod_format.h:124
char * write_buffer
Definition: mod_format.h:126
struct ast_format_def * fmt
Definition: mod_format.h:103
struct ast_channel * owner
Definition: mod_format.h:116
struct ast_format * lastwriteformat
Definition: mod_format.h:114
struct ast_trans_pvt * trans
Definition: mod_format.h:112
char * filename
Definition: mod_format.h:107
const char * orig_chan_name
Definition: mod_format.h:125
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Each supported file format is described by the following structure.
Definition: mod_format.h:43
int(* trunc)(struct ast_filestream *fs)
Definition: mod_format.h:69
int(* seek)(struct ast_filestream *, off_t, int)
Definition: mod_format.h:68
int(* write)(struct ast_filestream *, struct ast_frame *)
Definition: mod_format.h:66
char name[80]
Definition: mod_format.h:44
struct ast_frame *(* read)(struct ast_filestream *, int *whennext)
Definition: mod_format.h:74
struct ast_module * module
Definition: mod_format.h:93
char mime_types[80]
Definition: mod_format.h:47
off_t(* tell)(struct ast_filestream *fs)
Definition: mod_format.h:70
struct ast_format * format
Definition: mod_format.h:48
void(* close)(struct ast_filestream *)
Definition: mod_format.h:77
struct ast_format_def::@267 list
char exts[80]
Definition: mod_format.h:45
Definition of a media format.
Definition: format.c:43
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@254 data
enum ast_frame_type frametype
const char * src
Abstract JSON element (object, array, string, int, ...).
Support for dynamic strings.
Definition: strings.h:604
Definition: search.h:40
Definition: file.c:69
ast_rwlock_t lock
Definition: file.c:69
Number structure.
Definition: app_followme.c:154
Test Framework API.
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:189
static struct aco_type item
Definition: test_config.c:1463
static struct test_val b
static struct test_val a
static struct test_val c
Support for translation of data formats. translate.c.
struct ast_trans_pvt * ast_translator_build_path(struct ast_format *dest, struct ast_format *source)
Builds a translator path Build a path (possibly NULL) from source to dest.
Definition: translate.c:485
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:475
struct ast_frame * ast_translate(struct ast_trans_pvt *tr, struct ast_frame *f, int consume)
translates one or more frames Apply an input frame into the translator and receive zero or one output...
Definition: translate.c:565
Utility functions.
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:936
#define ast_assert(a)
Definition: utils.h:734
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: main/utils.c:2377
#define ARRAY_LEN(a)
Definition: utils.h:661