Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


file.h File Reference

Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h. More...

#include <fcntl.h>
#include <sys/mman.h>

Go to the source code of this file.

Macros

#define AST_DIGIT_ANY   "0123456789#*ABCD"
 
#define AST_DIGIT_ANYNUM   "0123456789"
 
#define AST_MAX_FORMATS   10
 
#define AST_RESERVED_POINTERS   20
 
#define SEEK_FORCECUR   10
 

Functions

int ast_applystream (struct ast_channel *chan, struct ast_filestream *s)
 Applys a open stream to a channel. More...
 
int ast_closestream (struct ast_filestream *f)
 Closes a stream. More...
 
int ast_file_init (void)
 
int ast_filecopy (const char *oldname, const char *newname, const char *fmt)
 Copies a file. More...
 
int ast_filedelete (const char *filename, const char *fmt)
 Deletes a file. More...
 
int ast_fileexists (const char *filename, const char *fmt, const char *preflang)
 Checks for the existence of a given file. More...
 
int ast_filerename (const char *oldname, const char *newname, const char *fmt)
 Renames a file. More...
 
char * ast_format_str_reduce (char *fmts)
 
struct ast_filestreamast_openstream (struct ast_channel *chan, const char *filename, const char *preflang)
 Opens stream for use in seeking, playing. More...
 
struct ast_filestreamast_openstream_full (struct ast_channel *chan, const char *filename, const char *preflang, int asis)
 Opens stream for use in seeking, playing. More...
 
struct ast_filestreamast_openvstream (struct ast_channel *chan, const char *filename, const char *preflang)
 Opens stream for use in seeking, playing. More...
 
int ast_playstream (struct ast_filestream *s)
 Play a open stream on a channel. More...
 
struct ast_filestreamast_readfile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 Starts reading from a file. More...
 
struct ast_frameast_readframe (struct ast_filestream *s)
 Read a frame from a filestream. More...
 
int ast_seekstream (struct ast_filestream *fs, off_t sample_offset, int whence)
 Seeks into stream. More...
 
int ast_stopstream (struct ast_channel *c)
 Stops a stream. More...
 
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. More...
 
int ast_stream_fastforward (struct ast_filestream *fs, off_t ms)
 Fast forward stream ms. More...
 
int ast_stream_rewind (struct ast_filestream *fs, off_t ms)
 Rewind stream ms. More...
 
int ast_streamfile (struct ast_channel *c, const char *filename, const char *preflang)
 Streams a file. More...
 
off_t ast_tellstream (struct ast_filestream *fs)
 Tell where we are in a stream. More...
 
int ast_truncstream (struct ast_filestream *fs)
 Trunc stream at current location. More...
 
int ast_waitstream (struct ast_channel *c, const char *breakon)
 Waits for a stream to stop or digit to be pressed. More...
 
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. More...
 
int ast_waitstream_fr (struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms)
 Same as waitstream but allows stream to be forwarded or rewound. More...
 
int ast_waitstream_full (struct ast_channel *c, const char *breakon, int audiofd, int monfd)
 
struct ast_filestreamast_writefile (const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
 Starts writing a file. More...
 
int ast_writestream (struct ast_filestream *fs, struct ast_frame *f)
 Writes a frame to a stream. More...
 

Detailed Description

Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.

Definition in file file.h.

Macro Definition Documentation

#define AST_DIGIT_ANYNUM   "0123456789"

Definition at line 48 of file file.h.

Referenced by initreqprep().

#define AST_MAX_FORMATS   10

The maximum number of formats we expect to see in a format string

Definition at line 44 of file file.h.

Referenced by ast_format_str_reduce().

#define AST_RESERVED_POINTERS   20

Definition at line 329 of file file.h.

#define SEEK_FORCECUR   10

Function Documentation

int ast_applystream ( struct ast_channel chan,
struct ast_filestream s 
)

Applys a open stream to a channel.

Parameters
chanchannel to work
sast_filestream to apply
Return values
0on success.
-1on failure.

Definition at line 861 of file file.c.

References ast_filestream::owner.

Referenced by ast_streamfile(), handle_getoption(), handle_recordfile(), handle_streamfile(), and speech_streamfile().

862 {
863  s->owner = chan;
864  return 0;
865 }
struct ast_channel * owner
Definition: mod_format.h:115
int ast_closestream ( struct ast_filestream f)

Closes a stream.

Parameters
ffilestream to close Close a playback or recording stream
Return values
0on success.
-1on failure.

Definition at line 904 of file file.c.

References ao2_ref, and filestream_close().

Referenced by __ast_play_and_record(), ast_filehelper(), ast_hangup(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), ast_readfile(), ast_stopstream(), ast_writefile(), dictate_exec(), filestream_destructor(), gen_closestream(), handle_cli_file_convert(), handle_recordfile(), local_ast_moh_stop(), mixmonitor_ds_close_fs(), moh_files_release(), record_exec(), and recordthread().

905 {
906  /* This used to destroy the filestream, but it now just decrements a refcount.
907  * We close the stream in order to quit queuing frames now, because we might
908  * change the writeformat, which could result in a subsequent write error, if
909  * the format is different. */
910  filestream_close(f);
911  ao2_ref(f, -1);
912  return 0;
913 }
static void filestream_close(struct ast_filestream *f)
Definition: file.c:295
#define ao2_ref(o, delta)
Definition: astobj2.h:472
int ast_file_init ( void  )

Initialize file stuff

Initializes all the various file stuff. Basically just registers the cli stuff Returns 0 all the time

Provided by file.c

Definition at line 1503 of file file.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_register_atexit(), and file_shutdown().

1504 {
1507  return 0;
1508 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_file[]
Definition: file.c:1494
static void file_shutdown(void)
Definition: file.c:1498
int ast_register_atexit(void(*func)(void))
Register a function to be executed before Asterisk exits.
Definition: asterisk.c:998
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
int ast_filecopy ( const char *  oldname,
const char *  newname,
const char *  fmt 
)

Copies a file.

Parameters
oldnamename of the file you wish to copy (minus extension)
newnamename you wish the file to be copied to (minus extension)
fmtthe format of the file Copy a given file in a given format, or if fmt is NULL, then do so for all

Definition at line 941 of file file.c.

References ACTION_COPY, and ast_filehelper().

Referenced by copy_plain_file(), and vm_forwardoptions().

942 {
943  return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
944 }
static format_t ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and...
Definition: file.c:429
int ast_filedelete ( const char *  filename,
const char *  fmt 
)

Deletes a file.

Parameters
filenamename of the file you wish to delete (minus the extension)
fmtof the file Delete a given file in a given format, or if fmt is NULL, then do so for all

Definition at line 931 of file file.c.

References ACTION_DELETE, and ast_filehelper().

Referenced by __ast_play_and_record(), announce_thread(), ast_monitor_start(), ast_monitor_stop(), conf_free(), conf_run(), dial_exec_full(), handle_cli_file_convert(), leave_voicemail(), play_record_review(), record_exec(), setup_privacy_args(), and vm_delete().

932 {
933  return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
934 }
static format_t ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and...
Definition: file.c:429
int ast_fileexists ( const char *  filename,
const char *  fmt,
const char *  preflang 
)

Checks for the existence of a given file.

Parameters
filenamename of the file you wish to check, minus the extension
fmtthe format you wish to check (the extension)
preflang(the preferred language you wisht to find the file in) See if a given file exists in a given format. If fmt is NULL, any format is accepted.
Returns
0 if file does not exist, non-zero positive otherwise.

Definition at line 919 of file file.c.

References ast_alloca, ast_filestream::buf, and fileexists_core().

Referenced by announce_thread(), app_exec(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), common_exec(), dial_exec_full(), eivr_comm(), forward_message(), get_folder(), invent_message(), leave_voicemail(), meetme_menu_admin_extended(), minivm_delete_exec(), play_file(), play_message(), play_message_callerid(), readexten_exec(), record_exec(), retrydial_exec(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), sayname(), setup_privacy_args(), vm_intro(), vm_newuser(), vm_options(), and vm_tempgreeting().

920 {
921  char *buf;
922  int buflen;
923 
924  if (preflang == NULL)
925  preflang = "";
926  buflen = strlen(preflang) + strlen(filename) + 4; /* room for everything */
927  buf = ast_alloca(buflen);
928  return fileexists_core(filename, fmt, preflang, buf, buflen);
929 }
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
static format_t fileexists_core(const char *filename, const char *fmt, const char *preflang, char *buf, int buflen)
helper routine to locate a file with a given format and language preference. Try preflang, preflang with stripped &#39;_&#39; suffices, or NULL.
Definition: file.c:586
int ast_filerename ( const char *  oldname,
const char *  newname,
const char *  fmt 
)

Renames a file.

Parameters
oldnamethe name of the file you wish to act upon (minus the extension)
newnamethe name you wish to rename the file to (minus the extension)
fmtthe format of the file Rename a given file in a given format, or if fmt is NULL, then do so for all
Returns
-1 on failure

Definition at line 936 of file file.c.

References ACTION_RENAME, and ast_filehelper().

Referenced by __ast_play_and_record(), ast_monitor_stop(), forward_message(), leave_voicemail(), play_record_review(), rename_file(), and vm_forwardoptions().

937 {
938  return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
939 }
static format_t ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and...
Definition: file.c:429
char* ast_format_str_reduce ( char *  fmts)

Remove duplicate formats from a format string.

Parameters
fmtsa format string, this string will be modified
Return values
NULLerror
Returns
a pointer to the reduced format string, this is a pointer to fmts

Definition at line 1382 of file file.c.

References ast_log(), AST_MAX_FORMATS, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdupa, ast_format::exts, exts_compare(), f, first, len(), ast_format::list, LOG_WARNING, strsep(), and type.

Referenced by actual_load_config().

1383 {
1384  struct ast_format *f;
1385  struct ast_format *fmts_ptr[AST_MAX_FORMATS];
1386  char *fmts_str[AST_MAX_FORMATS];
1387  char *stringp, *type;
1388  char *orig = fmts;
1389  int i, j, x, first, found = 0;
1390  int len = strlen(fmts) + 1;
1391  int res;
1392 
1393  if (AST_RWLIST_RDLOCK(&formats)) {
1394  ast_log(LOG_WARNING, "Unable to lock format list\n");
1395  return NULL;
1396  }
1397 
1398  stringp = ast_strdupa(fmts);
1399 
1400  for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1402  if (exts_compare(f->exts, type)) {
1403  found = 1;
1404  break;
1405  }
1406  }
1407 
1408  fmts_str[x] = type;
1409  if (found) {
1410  fmts_ptr[x] = f;
1411  } else {
1412  fmts_ptr[x] = NULL;
1413  }
1414  }
1416 
1417  first = 1;
1418  for (i = 0; i < x; i++) {
1419  /* ignore invalid entries */
1420  if (!fmts_ptr[i]) {
1421  ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1422  continue;
1423  }
1424 
1425  /* special handling for the first entry */
1426  if (first) {
1427  res = snprintf(fmts, len, "%s", fmts_str[i]);
1428  fmts += res;
1429  len -= res;
1430  first = 0;
1431  continue;
1432  }
1433 
1434  found = 0;
1435  for (j = 0; j < i; j++) {
1436  /* this is a duplicate */
1437  if (fmts_ptr[j] == fmts_ptr[i]) {
1438  found = 1;
1439  break;
1440  }
1441  }
1442 
1443  if (!found) {
1444  res = snprintf(fmts, len, "|%s", fmts_str[i]);
1445  fmts += res;
1446  len -= res;
1447  }
1448  }
1449 
1450  if (first) {
1451  ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1452  return NULL;
1453  }
1454 
1455  return orig;
1456 }
char * strsep(char **str, const char *delims)
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char exts[80]
Definition: mod_format.h:45
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
static int exts_compare(const char *exts, const char *type)
Definition: file.c:280
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
struct ast_format::@189 list
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Definition: file.c:65
struct sla_ringing_trunk * first
Definition: app_meetme.c:965
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static struct ast_format f[]
Definition: format_g726.c:181
static const char type[]
Definition: chan_nbs.c:57
#define AST_MAX_FORMATS
Definition: file.h:44
struct ast_filestream* ast_openstream ( struct ast_channel chan,
const char *  filename,
const char *  preflang 
)

Opens stream for use in seeking, playing.

Parameters
chanchannel to work with
filenameto use
preflangprefered language to use
Return values
aast_filestream pointer if it opens the file.
NULLon error.

Definition at line 636 of file file.c.

References ast_openstream_full().

Referenced by ast_streamfile(), dictate_exec(), handle_getoption(), handle_streamfile(), and speech_streamfile().

637 {
638  return ast_openstream_full(chan, filename, preflang, 0);
639 }
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:641
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.

Parameters
chanchannel to work with
filenameto use
preflangprefered language to use
asisif set, don't clear generators
Return values
aast_filestream pointer if it opens the file.
NULLon error.

Definition at line 641 of file file.c.

References ACTION_OPEN, ast_alloca, ast_deactivate_generator(), ast_filehelper(), AST_FORMAT_AUDIO_MASK, ast_log(), ast_set_write_format(), ast_stopstream(), ast_filestream::buf, fileexists_core(), ast_channel::generator, LOG_WARNING, ast_channel::oldwriteformat, ast_channel::stream, and ast_channel::writeformat.

Referenced by ast_moh_files_next(), ast_openstream(), and gen_nextfile().

642 {
643  /*
644  * Use fileexists_core() to find a file in a compatible
645  * language and format, set up a suitable translator,
646  * and open the stream.
647  */
648  format_t fmts, res;
649  int buflen;
650  char *buf;
651 
652  if (!asis) {
653  /* do this first, otherwise we detect the wrong writeformat */
654  ast_stopstream(chan);
655  if (chan->generator)
657  }
658  if (preflang == NULL)
659  preflang = "";
660  buflen = strlen(preflang) + strlen(filename) + 4;
661  buf = ast_alloca(buflen);
662  fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
663  if (fmts > 0)
664  fmts &= AST_FORMAT_AUDIO_MASK;
665  if (fmts < 1) {
666  ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
667  return NULL;
668  }
669  chan->oldwriteformat = chan->writeformat;
670  /* Set the channel to a format we can work with */
671  res = ast_set_write_format(chan, fmts);
672  if (res == -1) { /* No format available that works with this channel */
673  return NULL;
674  }
675  res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
676  if (res >= 0)
677  return chan->stream;
678  return NULL;
679 }
format_t writeformat
Definition: channel.h:854
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define LOG_WARNING
Definition: logger.h:144
static format_t fileexists_core(const char *filename, const char *fmt, const char *preflang, char *buf, int buflen)
helper routine to locate a file with a given format and language preference. Try preflang, preflang with stripped &#39;_&#39; suffices, or NULL.
Definition: file.c:586
struct ast_generator * generator
Definition: channel.h:747
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of &quot;format&quot; is ...
Definition: channel.c:5307
format_t oldwriteformat
Definition: channel.h:837
static format_t ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and...
Definition: file.c:429
int64_t format_t
Definition: frame_defs.h:32
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define AST_FORMAT_AUDIO_MASK
Definition: frame.h:274
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:3107
struct ast_filestream * stream
Definition: channel.h:757
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
struct ast_filestream* ast_openvstream ( struct ast_channel chan,
const char *  filename,
const char *  preflang 
)

Opens stream for use in seeking, playing.

Parameters
chanchannel to work with
filenameto use
preflangprefered language to use
Return values
aast_filestream pointer if it opens the file.
NULLon error.

Definition at line 681 of file file.c.

References ACTION_OPEN, ast_alloca, ast_filehelper(), AST_FORMAT_FIRST_VIDEO_BIT, AST_FORMAT_VIDEO_MASK, ast_getformatname(), ast_log(), ast_filestream::buf, fileexists_core(), ast_filestream::fmt, format, LOG_WARNING, ast_channel::nativeformats, and ast_channel::vstream.

Referenced by ast_streamfile(), handle_getoption(), and handle_streamfile().

682 {
683  /* As above, but for video. But here we don't have translators
684  * so we must enforce a format.
685  */
687  char *buf;
688  int buflen;
689 
690  if (preflang == NULL)
691  preflang = "";
692  buflen = strlen(preflang) + strlen(filename) + 4;
693  buf = ast_alloca(buflen);
694 
695  for (format = AST_FORMAT_FIRST_VIDEO_BIT; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
696  int fd;
697  const char *fmt;
698 
699  if (!(chan->nativeformats & format))
700  continue;
701  fmt = ast_getformatname(format);
702  if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
703  continue;
704  fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
705  if (fd >= 0)
706  return chan->vstream;
707  ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
708  }
709  return NULL;
710 }
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define LOG_WARNING
Definition: logger.h:144
static format_t fileexists_core(const char *filename, const char *fmt, const char *preflang, char *buf, int buflen)
helper routine to locate a file with a given format and language preference. Try preflang, preflang with stripped &#39;_&#39; suffices, or NULL.
Definition: file.c:586
format_t nativeformats
Definition: channel.h:852
static format_t ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
perform various actions on a file. Second argument arg2 depends on the command: unused for EXISTS and...
Definition: file.c:429
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
int64_t format_t
Definition: frame_defs.h:32
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define AST_FORMAT_FIRST_VIDEO_BIT
Definition: frame.h:281
#define AST_FORMAT_VIDEO_MASK
Definition: frame.h:290
struct ast_filestream * vstream
Definition: channel.h:758
static snd_pcm_format_t format
Definition: chan_alsa.c:93
int ast_playstream ( struct ast_filestream s)

Play a open stream on a channel.

Parameters
sfilestream to play
Return values
0on success.
-1on failure.

Definition at line 867 of file file.c.

References AST_FORMAT_AUDIO_MASK, ast_readaudio_callback(), ast_readvideo_callback(), ast_filestream::fmt, ast_format::format, and FSREAD_FAILURE.

Referenced by ast_streamfile(), handle_getoption(), handle_streamfile(), and speech_streamfile().

868 {
869  enum fsread_res res;
870 
871  if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
872  res = ast_readaudio_callback(s);
873  else
874  res = ast_readvideo_callback(s);
875 
876  return (res == FSREAD_FAILURE) ? -1 : 0;
877 }
static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
Definition: file.c:816
format_t format
Definition: mod_format.h:47
struct ast_format * fmt
Definition: mod_format.h:102
fsread_res
Definition: file.c:744
#define AST_FORMAT_AUDIO_MASK
Definition: frame.h:274
static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
Definition: file.c:752
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.

Parameters
filenamethe name of the file to read from
typeformat of file you wish to read from
commentcomment to go with
flagsfile flags
check(unimplemented, hence negligible)
modeOpen mode Open an incoming file stream. flags are flags for the open() command, and if check is non-zero, then it will not read a file if there are any files that start with that name and have an extension Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
Return values
astruct ast_filestream on success.
NULLon failure.

Definition at line 997 of file file.c.

References ast_closestream(), ast_free, ast_log(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdup, build_filename(), errno, ast_format::exts, exts_compare(), f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, open_wrapper(), ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), and handle_cli_file_convert().

998 {
999  FILE *bfile;
1000  struct ast_format *f;
1001  struct ast_filestream *fs = NULL;
1002  char *fn;
1003  int format_found = 0;
1004 
1006 
1007  AST_RWLIST_TRAVERSE(&formats, f, list) {
1008  fs = NULL;
1009  if (!exts_compare(f->exts, type))
1010  continue;
1011  else
1012  format_found = 1;
1013 
1014  fn = build_filename(filename, type);
1015  if (!fn) {
1016  continue;
1017  }
1018  errno = 0;
1019  bfile = fopen(fn, "r");
1020 
1021  if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1022  ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1023  if (fs) {
1024  ast_closestream(fs);
1025  }
1026  fs = NULL;
1027  bfile = NULL;
1028  ast_free(fn);
1029  break;
1030  }
1031  /* found it */
1032  fs->trans = NULL;
1033  fs->fmt = f;
1034  fs->flags = flags;
1035  fs->mode = mode;
1036  fs->filename = ast_strdup(filename);
1037  fs->vfs = NULL;
1038  ast_free(fn);
1039  break;
1040  }
1041 
1043  if (!format_found)
1044  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1045 
1046  return fs;
1047 }
#define ast_strdup(a)
Definition: astmm.h:109
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char exts[80]
Definition: mod_format.h:45
static int open_wrapper(struct ast_filestream *s)
Definition: file.c:407
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
static int exts_compare(const char *exts, const char *type)
Definition: file.c:280
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:258
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format * fmt
Definition: mod_format.h:102
Definition: file.c:65
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_trans_pvt * trans
Definition: mod_format.h:111
int errno
#define ast_free(a)
Definition: astmm.h:97
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
static struct ast_format f[]
Definition: format_g726.c:181
static const char type[]
Definition: chan_nbs.c:57
char * filename
Definition: mod_format.h:106
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
struct ast_filestream * vfs
Definition: mod_format.h:109
static struct ast_filestream * get_filestream(struct ast_format *fmt, FILE *bfile)
Definition: file.c:360
struct ast_frame* ast_readframe ( struct ast_filestream s)

Read a frame from a filestream.

Parameters
sast_filestream to act on
Returns
a frame.
Return values
NULLif read failed.

Definition at line 737 of file file.c.

References read_frame().

Referenced by __ast_play_and_record(), dictate_exec(), gen_readframe(), handle_cli_file_convert(), and moh_files_readframe().

738 {
739  int whennext = 0;
740 
741  return read_frame(s, &whennext);
742 }
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:712
int ast_seekstream ( struct ast_filestream fs,
off_t  sample_offset,
int  whence 
)

Seeks into stream.

Parameters
fsast_filestream to perform seek on
sample_offsetnumbers of samples to seek
whenceSEEK_SET, SEEK_CUR, SEEK_END
Return values
0on success.
-1on failure.

Definition at line 879 of file file.c.

References ast_filestream::fmt, and ast_format::seek.

Referenced by __ast_read(), ast_control_streamfile(), ast_moh_files_next(), ast_stream_fastforward(), ast_stream_rewind(), ast_streamfile(), ast_write(), dictate_exec(), handle_getoption(), handle_recordfile(), handle_streamfile(), and speech_streamfile().

880 {
881  return fs->fmt->seek(fs, sample_offset, whence);
882 }
struct ast_format * fmt
Definition: mod_format.h:102
int(* seek)(struct ast_filestream *, off_t, int)
Definition: mod_format.h:67
int ast_stopstream ( struct ast_channel c)

Stops a stream.

Parameters
cThe channel you wish to stop playback on

Stop playback of a stream

Return values
0always
Note
The channel does not need to be locked before calling this function.

Definition at line 128 of file file.c.

References ast_channel_lock, ast_channel_unlock, ast_closestream(), ast_getformatname(), ast_log(), ast_set_write_format(), LOG_WARNING, ast_channel::oldwriteformat, ast_channel::stream, and ast_channel::vstream.

Referenced by adsi_transmit_message_full(), ast_control_streamfile(), ast_openstream_full(), ast_play_and_wait(), ast_readstring_full(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_enumeration_full_vi(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), ast_say_number_full_en(), ast_say_number_full_en_GB(), ast_say_number_full_es(), ast_say_number_full_fr(), ast_say_number_full_gr(), ast_say_number_full_he(), ast_say_number_full_hu(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_pt(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), ast_say_number_full_ur(), ast_say_number_full_vi(), ast_say_number_full_zh(), background_detect_exec(), conf_exec(), conf_run(), dial_exec_full(), directory_exec(), grab_transfer(), handle_getoption(), handle_speechrecognize(), handle_streamfile(), ices_exec(), ivr_dispatch(), leave_voicemail(), meetme_menu_admin(), meetme_menu_admin_extended(), menu_callback(), minivm_greet_exec(), mp3_exec(), NBScat_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_file(), play_mailbox_owner(), playback_exec(), queue_exec(), read_exec(), readexten_exec(), record_exec(), recordthread(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), select_item_seq(), send_waveform_to_channel(), speech_background(), vm_authenticate(), vm_execmain(), wait_for_winner(), waitstream_core(), and zapateller_exec().

129 {
130  ast_channel_lock(tmp);
131 
132  /* Stop a running stream if there is one */
133  if (tmp->stream) {
134  ast_closestream(tmp->stream);
135  tmp->stream = NULL;
136  if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
137  ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_getformatname(tmp->oldwriteformat));
138  }
139  /* Stop the video stream too */
140  if (tmp->vstream != NULL) {
141  ast_closestream(tmp->vstream);
142  tmp->vstream = NULL;
143  }
144 
145  ast_channel_unlock(tmp);
146 
147  return 0;
148 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define LOG_WARNING
Definition: logger.h:144
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of &quot;format&quot; is ...
Definition: channel.c:5307
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_channel_unlock(chan)
Definition: channel.h:2467
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
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.

Note
If digits == "" then we can simply check for non-zero.
Returns
0 if success.
Return values
-1if error.
digitif interrupted by a digit.

Definition at line 1370 of file file.c.

References ast_streamfile(), ast_strlen_zero(), ast_waitstream(), and ast_channel::language.

Referenced by __ast_play_and_record(), announce_user_count(), app_exec(), ast_pickup_call(), ast_record_review(), bridge_playfile(), builtin_atxfer(), builtin_automixmonitor(), builtin_blindtransfer(), confbridge_exec(), directory_exec(), feature_attended_transfer(), feature_blind_transfer(), forward_message(), grab_transfer(), invent_message(), ivr_dispatch(), join_conference_bridge(), leave_voicemail(), masq_park_call(), menu_callback(), park_call_exec(), parked_call_exec(), play_mailbox_owner(), play_message_callerid(), play_message_on_chan(), play_prompt_to_channel(), play_record_review(), play_sound_file(), sayname(), select_item_seq(), vm_forwardoptions(), vmsayname_exec(), wait_file2(), and xfer_park_call_helper().

1371 {
1372  int res = 0;
1373  if (!ast_strlen_zero(file)) {
1374  res = ast_streamfile(chan, file, chan->language);
1375  if (!res) {
1376  res = ast_waitstream(chan, digits);
1377  }
1378  }
1379  return res;
1380 }
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:946
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1343
const ast_string_field language
Definition: channel.h:787
int ast_stream_fastforward ( struct ast_filestream fs,
off_t  ms 
)

Fast forward stream ms.

Parameters
fsfilestream to act on
msmilliseconds to move
Return values
0on success.
-1on failure.

Definition at line 894 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by waitstream_core().

895 {
896  return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
897 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:879
#define DEFAULT_SAMPLES_PER_MS
Definition: asterisk.h:42
int ast_stream_rewind ( struct ast_filestream fs,
off_t  ms 
)

Rewind stream ms.

Parameters
fsfilestream to act on
msmilliseconds to move
Return values
0on success.
-1on failure.

Definition at line 899 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by __ast_play_and_record(), handle_recordfile(), record_exec(), and waitstream_core().

900 {
901  return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
902 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:879
#define DEFAULT_SAMPLES_PER_MS
Definition: asterisk.h:42
int ast_streamfile ( struct ast_channel c,
const char *  filename,
const char *  preflang 
)

Streams a file.

Parameters
cchannel to stream the file to
filenamethe name of the file you wish to stream, minus the extension
preflangthe preferred language you wish to have the file streamed to you in Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel Also, it will stop any existing streams on the channel.
Return values
0on success.
-1on failure.

Definition at line 946 of file file.c.

References ast_applystream(), ast_debug, AST_FLAG_MASQ_NOSTREAM, ast_getformatname(), ast_getformatname_multiple(), ast_log(), ast_openstream(), ast_openvstream(), ast_playstream(), ast_seekstream(), ast_strdup, ast_test_flag, ast_test_suite_event_notify, ast_verb, errno, ast_filestream::f, ast_filestream::fmt, ast_format::format, LOG_WARNING, ast_channel::name, ast_channel::nativeformats, ast_filestream::orig_chan_name, ast_filestream::vfs, and ast_channel::writeformat.

Referenced by __analog_ss_thread(), action_bridge(), agent_call(), analog_ss_thread(), announce_thread(), app_exec(), ast_app_getdata(), ast_app_getdata_full(), ast_control_streamfile(), ast_play_and_wait(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_fr(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_hu(), ast_say_date_ka(), ast_say_date_nl(), ast_say_date_th(), ast_say_date_with_format_gr(), ast_say_datetime_en(), ast_say_datetime_fr(), ast_say_datetime_from_now_en(), ast_say_datetime_from_now_fr(), ast_say_datetime_from_now_he(), ast_say_datetime_from_now_ka(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_th(), ast_say_datetime_zh(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_enumeration_full_vi(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), ast_say_number_full_en(), ast_say_number_full_en_GB(), ast_say_number_full_es(), ast_say_number_full_fr(), ast_say_number_full_gr(), ast_say_number_full_he(), ast_say_number_full_hu(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_pt(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), ast_say_number_full_ur(), ast_say_number_full_vi(), ast_say_number_full_zh(), ast_say_time_de(), ast_say_time_en(), ast_say_time_fr(), ast_say_time_gr(), ast_say_time_hu(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), auth_exec(), background_detect_exec(), bridge_exec(), check_availability(), check_beep(), common_exec(), conf_exec(), conf_run(), dial_exec_full(), do_directory(), find_conf_realtime(), forward_message(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), local_attended_transfer(), login_exec(), meetme_menu_admin(), meetme_menu_admin_extended(), meetme_menu_normal(), menu_callback(), minivm_greet_exec(), page_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_record_review(), playback_exec(), privacy_exec(), readexten_exec(), record_exec(), retrydial_exec(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), select_item_menu(), setup_privacy_args(), vm_authenticate(), wait_file(), and wait_for_winner().

947 {
948  struct ast_filestream *fs;
949  struct ast_filestream *vfs=NULL;
950  char fmt[256];
951  off_t pos;
952  int seekattempt;
953  int res;
954 
955  fs = ast_openstream(chan, filename, preflang);
956  if (!fs) {
957  ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
958  return -1;
959  }
960 
961  /* check to see if there is any data present (not a zero length file),
962  * done this way because there is no where for ast_openstream_full to
963  * return the file had no data. */
964  pos = ftello(fs->f);
965  seekattempt = fseeko(fs->f, -1, SEEK_END);
966  if (seekattempt) {
967  if (errno == EINVAL) {
968  /* Zero-length file, as opposed to a pipe */
969  return 0;
970  } else {
971  ast_seekstream(fs, 0, SEEK_SET);
972  }
973  } else {
974  fseeko(fs->f, pos, SEEK_SET);
975  }
976 
977  vfs = ast_openvstream(chan, filename, preflang);
978  if (vfs) {
979  ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
980  }
981 
983  fs->orig_chan_name = ast_strdup(chan->name);
984  if (ast_applystream(chan, fs))
985  return -1;
986  if (vfs && ast_applystream(chan, vfs))
987  return -1;
988  ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, chan->name);
989  res = ast_playstream(fs);
990  if (!res && vfs)
991  res = ast_playstream(vfs);
992  ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
993 
994  return res;
995 }
#define ast_strdup(a)
Definition: astmm.h:109
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define LOG_WARNING
Definition: logger.h:144
#define ast_verb(level,...)
Definition: logger.h:243
format_t format
Definition: mod_format.h:47
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:867
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:681
const char * orig_chan_name
Definition: mod_format.h:120
struct ast_format * fmt
Definition: mod_format.h:102
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:184
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applys a open stream to a channel.
Definition: file.c:861
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:879
int errno
char * filename
Definition: mod_format.h:106
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
char * ast_getformatname_multiple(char *buf, size_t size, format_t format)
Get the names of a set of formats.
Definition: frame.c:591
struct ast_filestream * vfs
Definition: mod_format.h:109
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:636
off_t ast_tellstream ( struct ast_filestream fs)

Tell where we are in a stream.

Parameters
fsfs to act on
Returns
a long as a sample offset into stream

Definition at line 889 of file file.c.

References ast_filestream::fmt, and ast_format::tell.

Referenced by __ast_play_and_record(), ast_control_streamfile(), ast_moh_files_next(), handle_getoption(), handle_recordfile(), handle_speechrecognize(), and handle_streamfile().

890 {
891  return fs->fmt->tell(fs);
892 }
off_t(* tell)(struct ast_filestream *fs)
Definition: mod_format.h:69
struct ast_format * fmt
Definition: mod_format.h:102
int ast_truncstream ( struct ast_filestream fs)

Trunc stream at current location.

Parameters
fsfilestream to act on
Return values
0on success.
-1on failure.

Definition at line 884 of file file.c.

References ast_filestream::fmt, and ast_format::trunc.

Referenced by __ast_play_and_record(), handle_recordfile(), and record_exec().

885 {
886  return fs->fmt->trunc(fs);
887 }
struct ast_format * fmt
Definition: mod_format.h:102
int(* trunc)(struct ast_filestream *fs)
Definition: mod_format.h:68
int ast_waitstream ( struct ast_channel c,
const char *  breakon 
)

Waits for a stream to stop or digit to be pressed.

Parameters
cchannel to waitstream on
breakonstring of DTMF digits to break upon Begins playback of a stream... Wait for a stream to stop or for any one of a given digit to arrive,
Return values
0if the stream finishes
thecharacter if it was interrupted,
-1on error

Definition at line 1343 of file file.c.

References waitstream_core().

Referenced by __analog_ss_thread(), action_bridge(), agent_call(), analog_ss_thread(), announce_thread(), app_exec(), ast_play_and_wait(), ast_say_date_da(), ast_say_date_de(), ast_say_date_en(), ast_say_date_es(), ast_say_date_fr(), ast_say_date_gr(), ast_say_date_he(), ast_say_date_hu(), ast_say_date_ka(), ast_say_date_nl(), ast_say_date_th(), ast_say_date_with_format_gr(), ast_say_datetime_en(), ast_say_datetime_fr(), ast_say_datetime_from_now_en(), ast_say_datetime_from_now_fr(), ast_say_datetime_from_now_he(), ast_say_datetime_from_now_ka(), ast_say_datetime_gr(), ast_say_datetime_he(), ast_say_datetime_nl(), ast_say_datetime_pt(), ast_say_datetime_th(), ast_say_datetime_zh(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_he(), ast_say_enumeration_full_vi(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), ast_say_number_full_en(), ast_say_number_full_en_GB(), ast_say_number_full_es(), ast_say_number_full_fr(), ast_say_number_full_gr(), ast_say_number_full_he(), ast_say_number_full_hu(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_pt(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), ast_say_number_full_ur(), ast_say_number_full_vi(), ast_say_number_full_zh(), ast_say_time_de(), ast_say_time_en(), ast_say_time_gr(), ast_say_time_he(), ast_say_time_hu(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), auth_exec(), bridge_exec(), check_availability(), check_beep(), common_exec(), conf_exec(), conf_run(), directory_exec(), find_conf_realtime(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), local_attended_transfer(), login_exec(), meetme_menu_admin(), meetme_menu_admin_extended(), meetme_menu_normal(), menu_callback(), minivm_greet_exec(), page_exec(), parkandannounce_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_and_wait(), play_file(), play_record_review(), playback_exec(), privacy_exec(), record_exec(), retrydial_exec(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_phonetic_str_full(), select_item_menu(), setup_privacy_args(), vm_authenticate(), and wait_file().

1344 {
1345  return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1346 }
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)
the core of all waitstream() functions
Definition: file.c:1190
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.

Parameters
cchannel to waitstream on
contextstring of context to match digits to break upon Begins playback of a stream... Wait for a stream to stop or for any one of a valid extension digit to arrive,
Return values
0if the stream finishes.
thecharacter if it was interrupted.
-1on error.

Definition at line 1354 of file file.c.

References ast_channel::context, and waitstream_core().

Referenced by pbx_builtin_background().

1355 {
1356  /* Waitstream, with return in the case of a valid 1 digit extension */
1357  /* in the current or specified context being pressed */
1358 
1359  if (!context)
1360  context = c->context;
1361  return waitstream_core(c, NULL, NULL, NULL, 0,
1362  -1, -1, context);
1363 }
char context[AST_MAX_CONTEXT]
Definition: channel.h:868
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)
the core of all waitstream() functions
Definition: file.c:1190
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
int ast_waitstream_fr ( struct ast_channel c,
const char *  breakon,
const char *  forward,
const char *  rewind,
int  ms 
)

Same as waitstream but allows stream to be forwarded or rewound.

Parameters
cchannel to waitstream on
breakonstring of DTMF digits to break upon
forwardDTMF digit to fast forward upon
rewindDTMF digit to rewind upon
msHow many miliseconds to skip forward/back Begins playback of a stream... Wait for a stream to stop or for any one of a given digit to arrive,
Return values
0if the stream finishes.
thecharacter if it was interrupted.
-1on error.

Definition at line 1337 of file file.c.

References waitstream_core().

Referenced by ast_control_streamfile().

1338 {
1339  return waitstream_core(c, breakon, forward, reverse, ms,
1340  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1341 }
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)
the core of all waitstream() functions
Definition: file.c:1190
int ast_waitstream_full ( struct ast_channel c,
const char *  breakon,
int  audiofd,
int  monfd 
)
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.

Parameters
filenamethe name of the file to write to
typeformat of file you wish to write out to
commentcomment to go with
flagsoutput file flags
check(unimplemented, hence negligible)
modeOpen mode Create an outgoing file stream. oflags are flags for the open() command, and if check is non-zero, then it will not write a file if there are any files that start with that name and have an extension Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
Return values
astruct ast_filestream on success.
NULLon failure.

Definition at line 1049 of file file.c.

References ast_alloca, ast_closestream(), ast_free, ast_log(), ast_malloc, ast_opt_cache_record_files, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdup, ast_strdupa, ast_filestream::buf, build_filename(), errno, ast_format::exts, exts_compare(), ast_filestream::f, f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, ast_filestream::realfilename, record_cache_dir, rewrite_wrapper(), ast_format::seek, ast_filestream::trans, ast_filestream::vfs, and ast_filestream::write_buffer.

Referenced by __ast_play_and_record(), ast_monitor_start(), ast_writestream(), dictate_exec(), handle_cli_file_convert(), handle_recordfile(), mixmonitor_thread(), record_exec(), and recordthread().

1050 {
1051  int fd, myflags = 0;
1052  /* compiler claims this variable can be used before initialization... */
1053  FILE *bfile = NULL;
1054  struct ast_format *f;
1055  struct ast_filestream *fs = NULL;
1056  char *buf = NULL;
1057  size_t size = 0;
1058  int format_found = 0;
1059 
1061 
1062  /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1063  /* We really can't use O_APPEND as it will break WAV header updates */
1064  if (flags & O_APPEND) {
1065  flags &= ~O_APPEND;
1066  } else {
1067  myflags = O_TRUNC;
1068  }
1069 
1070  myflags |= O_WRONLY | O_CREAT;
1071 
1072  /* XXX need to fix this - we should just do the fopen,
1073  * not open followed by fdopen()
1074  */
1075  AST_RWLIST_TRAVERSE(&formats, f, list) {
1076  char *fn, *orig_fn = NULL;
1077  if (fs)
1078  break;
1079 
1080  if (!exts_compare(f->exts, type))
1081  continue;
1082  else
1083  format_found = 1;
1084 
1085  fn = build_filename(filename, type);
1086  if (!fn) {
1087  continue;
1088  }
1089  fd = open(fn, flags | myflags, mode);
1090  if (fd > -1) {
1091  /* fdopen() the resulting file stream */
1092  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1093  if (!bfile) {
1094  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1095  close(fd);
1096  fd = -1;
1097  }
1098  }
1099 
1100  if (ast_opt_cache_record_files && (fd > -1)) {
1101  char *c;
1102 
1103  fclose(bfile); /* this also closes fd */
1104  /*
1105  We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1106  What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1107  */
1108  orig_fn = ast_strdupa(fn);
1109  for (c = fn; *c; c++)
1110  if (*c == '/')
1111  *c = '_';
1112 
1113  size = strlen(fn) + strlen(record_cache_dir) + 2;
1114  buf = ast_alloca(size);
1115  strcpy(buf, record_cache_dir);
1116  strcat(buf, "/");
1117  strcat(buf, fn);
1118  ast_free(fn);
1119  fn = buf;
1120  fd = open(fn, flags | myflags, mode);
1121  if (fd > -1) {
1122  /* fdopen() the resulting file stream */
1123  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1124  if (!bfile) {
1125  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1126  close(fd);
1127  fd = -1;
1128  }
1129  }
1130  }
1131  if (fd > -1) {
1132  errno = 0;
1133  fs = get_filestream(f, bfile);
1134  if (fs) {
1135  if ((fs->write_buffer = ast_malloc(32768))) {
1136  setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1137  }
1138  }
1139  if (!fs || rewrite_wrapper(fs, comment)) {
1140  ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1141  close(fd);
1142  if (orig_fn) {
1143  unlink(fn);
1144  unlink(orig_fn);
1145  }
1146  if (fs) {
1147  ast_closestream(fs);
1148  fs = NULL;
1149  }
1150  if (!buf) {
1151  ast_free(fn);
1152  }
1153  continue;
1154  }
1155  fs->trans = NULL;
1156  fs->fmt = f;
1157  fs->flags = flags;
1158  fs->mode = mode;
1159  if (orig_fn) {
1160  fs->realfilename = ast_strdup(orig_fn);
1161  fs->filename = ast_strdup(fn);
1162  } else {
1163  fs->realfilename = NULL;
1164  fs->filename = ast_strdup(filename);
1165  }
1166  fs->vfs = NULL;
1167  /* If truncated, we'll be at the beginning; if not truncated, then append */
1168  f->seek(fs, 0, SEEK_END);
1169  } else if (errno != EEXIST) {
1170  ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1171  if (orig_fn)
1172  unlink(orig_fn);
1173  }
1174  /* if buf != NULL then fn is already free and pointing to it */
1175  if (!buf)
1176  ast_free(fn);
1177  }
1178 
1180 
1181  if (!format_found)
1182  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1183 
1184  return fs;
1185 }
#define ast_strdup(a)
Definition: astmm.h:109
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: utils.h:653
#define LOG_WARNING
Definition: logger.h:144
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
Definition: file.c:402
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char * realfilename
Definition: mod_format.h:107
char exts[80]
Definition: mod_format.h:45
char * write_buffer
Definition: mod_format.h:121
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
static int exts_compare(const char *exts, const char *type)
Definition: file.c:280
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:258
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format * fmt
Definition: mod_format.h:102
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define comment
Definition: ael_lex.c:961
Definition: file.c:65
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_trans_pvt * trans
Definition: mod_format.h:111
#define ast_opt_cache_record_files
Definition: options.h:116
int errno
#define ast_free(a)
Definition: astmm.h:97
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:904
static struct ast_format f[]
Definition: format_g726.c:181
static const char type[]
Definition: chan_nbs.c:57
char record_cache_dir[AST_CACHE_DIR_LEN]
Definition: asterisk.c:195
char * filename
Definition: mod_format.h:106
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
int(* seek)(struct ast_filestream *, off_t, int)
Definition: mod_format.h:67
struct ast_filestream * vfs
Definition: mod_format.h:109
#define ast_malloc(a)
Definition: astmm.h:91
static struct ast_filestream * get_filestream(struct ast_format *fmt, FILE *bfile)
Definition: file.c:360
int ast_writestream ( struct ast_filestream fs,
struct ast_frame f 
)

Writes a frame to a stream.

Parameters
fsfilestream to write to
fframe to write to the filestream Send a frame to a filestream – note: does NOT free the frame, call ast_frfree manually
Return values
0on success.
-1on failure.

Definition at line 150 of file file.c.

References ast_debug, AST_FORMAT_AUDIO_MASK, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frfree, ast_getformatname(), AST_LIST_NEXT, ast_log(), ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_writefile(), ast_writestream(), ast_frame_subclass::codec, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, ast_format::format, ast_frame::frametype, ast_filestream::lastwriteformat, LOG_WARNING, ast_filestream::mode, ast_format::name, ast_frame::subclass, ast_filestream::trans, type, ast_filestream::vfs, and ast_format::write.

Referenced by __ast_play_and_record(), __ast_read(), ast_write(), ast_writestream(), dictate_exec(), handle_cli_file_convert(), handle_recordfile(), mixmonitor_thread(), record_exec(), and recordthread().

151 {
152  int res = -1;
153  int alt = 0;
154  if (f->frametype == AST_FRAME_VIDEO) {
155  if (fs->fmt->format & AST_FORMAT_AUDIO_MASK) {
156  /* This is the audio portion. Call the video one... */
157  if (!fs->vfs && fs->filename) {
158  const char *type = ast_getformatname(f->subclass.codec & ~0x1);
159  fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
160  ast_debug(1, "Opened video output file\n");
161  }
162  if (fs->vfs)
163  return ast_writestream(fs->vfs, f);
164  /* else ignore */
165  return 0;
166  } else {
167  /* Might / might not have mark set */
168  alt = 1;
169  }
170  } else if (f->frametype != AST_FRAME_VOICE) {
171  ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
172  return -1;
173  }
174  if (((fs->fmt->format | alt) & f->subclass.codec) == f->subclass.codec) {
175  res = fs->fmt->write(fs, f);
176  if (res < 0)
177  ast_log(LOG_WARNING, "Natural write failed\n");
178  else if (res > 0)
179  ast_log(LOG_WARNING, "Huh??\n");
180  } else {
181  /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
182  the one we've setup a translator for, we do the "wrong thing" XXX */
183  if (fs->trans && f->subclass.codec != fs->lastwriteformat) {
185  fs->trans = NULL;
186  }
187  if (!fs->trans)
189  if (!fs->trans)
190  ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
192  else {
193  struct ast_frame *trf;
194  fs->lastwriteformat = f->subclass.codec;
195  /* Get the translated frame but don't consume the original in case they're using it on another stream */
196  if ((trf = ast_translate(fs->trans, f, 0))) {
197  struct ast_frame *cur;
198 
199  /* the translator may have returned multiple frames, so process them */
200  for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
201  if ((res = fs->fmt->write(fs, trf))) {
202  ast_log(LOG_WARNING, "Translated frame write failed\n");
203  break;
204  }
205  }
206  ast_frfree(trf);
207  } else {
208  res = 0;
209  }
210  }
211  }
212  return res;
213 }
union ast_frame_subclass subclass
Definition: frame.h:146
#define LOG_WARNING
Definition: logger.h:144
int(* write)(struct ast_filestream *, struct ast_frame *)
Definition: mod_format.h:65
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
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:328
format_t codec
Definition: frame.h:137
struct ast_trans_pvt * ast_translator_build_path(format_t dest, format_t source)
Builds a translator path Build a path (possibly NULL) from source to dest.
Definition: translate.c:282
format_t format
Definition: mod_format.h:47
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
struct ast_format * fmt
Definition: mod_format.h:102
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
char name[80]
Definition: mod_format.h:44
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:1049
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct ast_trans_pvt * trans
Definition: mod_format.h:111
#define AST_FORMAT_AUDIO_MASK
Definition: frame.h:274
static const char type[]
Definition: chan_nbs.c:57
char * filename
Definition: mod_format.h:106
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:150
Data structure associated with a single frame of data.
Definition: frame.h:142
enum ast_frame_type frametype
Definition: frame.h:144
struct ast_filestream * vfs
Definition: mod_format.h:109
#define ast_frfree(fr)
Definition: frame.h:583
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:272