Asterisk - The Open Source Telephony Project  21.4.1
Macros | Typedefs | Enumerations | Functions
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_DIGIT_NONE   ""
 
#define ast_file_read_dir(dir_name, on_file, obj)   ast_file_read_dirs(dir_name, on_file, obj, 1)
 Iterate over each file in a given directory. More...
 
#define AST_MAX_FORMATS   10
 
#define AST_RESERVED_POINTERS   20
 
#define SEEK_FORCECUR   10
 

Typedefs

typedef int(* ast_file_on_file) (const char *dir_name, const char *filename, void *obj)
 Callback called for each file found when reading directories. More...
 
typedef 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 rewinding or fastforwarding a file.
 

Enumerations

enum  ast_waitstream_fr_cb_values { AST_WAITSTREAM_CB_REWIND = 1, AST_WAITSTREAM_CB_FASTFORWARD, AST_WAITSTREAM_CB_START }
 

Functions

int ast_applystream (struct ast_channel *chan, struct ast_filestream *s)
 Applies a open stream to a channel. More...
 
int ast_closestream (struct ast_filestream *f)
 Closes a stream. More...
 
int ast_file_fdtemp (const char *path, char **filename, const char *template_name)
 Create a temporary file located at path. More...
 
int ast_file_init (void)
 
FILE * ast_file_mkftemp (char *template_name, mode_t mode)
 same as mkstemp, but return a FILE More...
 
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. More...
 
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)
 
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. More...
 
struct ast_formatast_get_format_for_file_ext (const char *file_ext)
 Get the ast_format associated with the given file extension. More...
 
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...
 
int ast_ratestream (struct ast_filestream *fs)
 Return the sample rate of the stream's format. 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_fr_w_cb (struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, 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 file. 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_NONE   ""

Convenient for waiting

Definition at line 47 of file file.h.

Referenced by agent_login_exec(), ast_bridge_channel_playfile(), grab_transfer(), play_file(), and stream_failsound().

#define ast_file_read_dir (   dir_name,
  on_file,
  obj 
)    ast_file_read_dirs(dir_name, on_file, obj, 1)

Iterate over each file in a given directory.

Parameters
dir_namethe name of the directory to search
on_filecallback called on each file
objuser data object
Returns
-1
Return values
errnoon failure
0otherwise

Definition at line 203 of file file.h.

Referenced by find_recording().

#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_play_and_record(), and ast_format_str_reduce().

Typedef Documentation

typedef int(* ast_file_on_file) (const char *dir_name, const char *filename, void *obj)

Callback called for each file found when reading directories.

Parameters
dir_namethe name of the directory
filenamethe name of the file
objuser data object
Return values
non-zeroto stop reading, otherwise zero to continue
Note
dir_name is not processed by realpath or other functions, symbolic links are not resolved. This ensures dir_name always starts with the exact string originally passed to ast_file_read_dir or ast_file_read_dirs.

Definition at line 180 of file file.h.

Enumeration Type Documentation

The type of event associated with a ast_waitstream_fr_cb invocation

Definition at line 54 of file file.h.

54  {
55  AST_WAITSTREAM_CB_REWIND = 1,
56  AST_WAITSTREAM_CB_FASTFORWARD,
57  AST_WAITSTREAM_CB_START
58 };

Function Documentation

int ast_applystream ( struct ast_channel chan,
struct ast_filestream s 
)

Applies a open stream to a channel.

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

Definition at line 1057 of file file.c.

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

1058 {
1059  s->owner = chan;
1060  return 0;
1061 }
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 1111 of file file.c.

References ao2_ref.

Referenced by __ast_play_and_record(), ast_hangup(), ast_readfile(), ast_stopstream(), ast_writefile(), and handle_cli_file_convert().

1112 {
1113  /* This used to destroy the filestream, but it now just decrements a refcount.
1114  * We close the stream in order to quit queuing frames now, because we might
1115  * change the writeformat, which could result in a subsequent write error, if
1116  * the format is different. */
1117  if (f == NULL) {
1118  return 0;
1119  }
1120  filestream_close(f);
1121  ao2_ref(f, -1);
1122  return 0;
1123 }
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
int ast_file_fdtemp ( const char *  path,
char **  filename,
const char *  template_name 
)

Create a temporary file located at path.

Note
The directory containing path will be created if it does not exist
This function assumes path does not end with a '/'
Parameters
pathThe directory path to create the file in
filenameFunction allocates memory and stores full filename (including path) here
template_namemkstemp template to use. Must end with XXXXXX.
Note
filename will need to be freed with ast_free if this function succeeds
Return values
-1on failure
Returns
file descriptor on success

Definition at line 202 of file file.c.

References ast_asprintf, and ast_mkdir().

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 }
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
int ast_mkdir(const char *path, int mode)
Recursively create directory path.
Definition: utils.c:2479
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 2051 of file file.c.

References ast_cli_register_multiple, ast_format_register_type(), ast_format_unregister_type(), ast_register_cleanup(), and STASIS_MESSAGE_TYPE_INIT.

2052 {
2055  ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
2056  ast_register_cleanup(file_shutdown);
2057  return 0;
2058 }
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1493
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
struct stasis_message_type * ast_format_unregister_type(void)
Get the message type used for signaling a format unregistration.
struct stasis_message_type * ast_format_register_type(void)
Get the message type used for signaling a format registration.
FILE* ast_file_mkftemp ( char *  template_name,
mode_t  mode 
)

same as mkstemp, but return a FILE

Parameters
template_nameThe template for the unique file name to generate. Modified in place to return the file name.
modeThe mode for file permissions
Returns
FILE handle to the temporary file on success or NULL if creation failed

Definition at line 187 of file file.c.

References ast_format_def::close.

188 {
189  FILE *p = NULL;
190  int pfd = mkstemp(template_name);
191  chmod(template_name, 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 }
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.

Parameters
dir_namethe name of the directory to search
on_filecallback called on each file
objuser data object
max_depthre-curse into sub-directories up to a given maximum (-1 = infinite)
Return values
-1on failure
errnoon failure
0otherwise

Definition at line 1274 of file file.c.

References read_dirs_lock.

Referenced by ast_media_index_update_for_file(), and crypto_load().

1275 {
1276  int res;
1277 
1278  errno = 0;
1279 
1280 #if !defined(__GLIBC__)
1281  ast_mutex_lock(&read_dirs_lock);
1282 #endif
1283 
1284  res = __ast_file_read_dirs(dir_name, on_file, obj, max_depth);
1285 
1286 #if !defined(__GLIBC__)
1287  ast_mutex_unlock(&read_dirs_lock);
1288 #endif
1289 
1290  return res;
1291 }
static ast_mutex_t read_dirs_lock
Lock to hold when iterating over directories.
Definition: file.c:1271
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 1151 of file file.c.

Referenced by copy_plain_file(), and vm_forwardoptions().

1152 {
1153  return filehelper(filename, filename2, fmt, ACTION_COPY);
1154 }
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 1141 of file file.c.

Referenced by __ast_play_and_record(), async_delete_name_rec_task(), conf_free(), confbridge_exec(), dial_exec_full(), handle_cli_file_convert(), leave_voicemail(), setup_privacy_args(), and vm_delete().

1142 {
1143  return filehelper(filename, NULL, fmt, ACTION_DELETE);
1144 }
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.
Return values
0The file does not exist
1The file does exist.

Definition at line 1129 of file file.c.

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

Referenced by ast_get_character_str(), ast_get_digit_str(), ast_get_phonetic_str(), dial_exec_full(), forward_message(), get_folder(), leave_voicemail(), setup_privacy_args(), stasis_app_control_record(), and vm_tempgreeting().

1130 {
1131  char *buf;
1132  int buflen;
1133 
1134  if (preflang == NULL)
1135  preflang = "";
1136  buflen = strlen(preflang) + strlen(filename) + 4; /* room for everything */
1137  buf = ast_alloca(buflen);
1138  return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1139 }
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
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
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
Return values
-1on failure

Definition at line 1146 of file file.c.

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

1147 {
1148  return filehelper(filename, filename2, fmt, ACTION_RENAME);
1149 }
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 1894 of file file.c.

References AST_MAX_FORMATS, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_strdupa, and ast_format_def::exts.

1895 {
1896  struct ast_format_def *f;
1897  struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1898  char *fmts_str[AST_MAX_FORMATS];
1899  char *stringp, *type;
1900  char *orig = fmts;
1901  int i, j, x, first, found = 0;
1902  int len = strlen(fmts) + 1;
1903  int res;
1904 
1905  if (AST_RWLIST_RDLOCK(&formats)) {
1906  ast_log(LOG_WARNING, "Unable to lock format list\n");
1907  return NULL;
1908  }
1909 
1910  stringp = ast_strdupa(fmts);
1911 
1912  for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1913  AST_RWLIST_TRAVERSE(&formats, f, list) {
1914  if (exts_compare(f->exts, type)) {
1915  found = 1;
1916  break;
1917  }
1918  }
1919 
1920  fmts_str[x] = type;
1921  if (found) {
1922  fmts_ptr[x] = f;
1923  } else {
1924  fmts_ptr[x] = NULL;
1925  }
1926  }
1928 
1929  first = 1;
1930  for (i = 0; i < x; i++) {
1931  /* ignore invalid entries */
1932  if (!fmts_ptr[i]) {
1933  ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1934  continue;
1935  }
1936 
1937  /* special handling for the first entry */
1938  if (first) {
1939  res = snprintf(fmts, len, "%s", fmts_str[i]);
1940  fmts += res;
1941  len -= res;
1942  first = 0;
1943  continue;
1944  }
1945 
1946  found = 0;
1947  for (j = 0; j < i; j++) {
1948  /* this is a duplicate */
1949  if (fmts_ptr[j] == fmts_ptr[i]) {
1950  found = 1;
1951  break;
1952  }
1953  }
1954 
1955  if (!found) {
1956  res = snprintf(fmts, len, "|%s", fmts_str[i]);
1957  fmts += res;
1958  len -= res;
1959  }
1960  }
1961 
1962  if (first) {
1963  ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1964  return NULL;
1965  }
1966 
1967  return orig;
1968 }
char exts[80]
Definition: mod_format.h:45
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
Each supported file format is described by the following structure.
Definition: mod_format.h:43
struct ast_format_def::@237 list
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
Definition: file.c:69
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_MAX_FORMATS
Definition: file.h:44
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.

Parameters
mime_typeThe MIME type for which to find extensions
bufferA pointer to a buffer to receive the extension
capacityThe size of 'buffer' in bytes
Return values
1if an extension was found for the provided MIME type
0if the MIME type was not found

Definition at line 2019 of file file.c.

References ast_format_def::exts, lock, ast_format_def::mime_types, and SCOPED_RDLOCK.

2020 {
2021  struct ast_format_def *f;
2022  SCOPED_RDLOCK(lock, &formats.lock);
2023 
2024  ast_assert(buffer && capacity);
2025 
2026  AST_RWLIST_TRAVERSE(&formats, f, list) {
2027  if (type_in_list(f->mime_types, mime_type, strcasecmp)) {
2028  size_t item_len = strcspn(f->exts, "|");
2029  size_t bytes_written = snprintf(buffer, capacity, ".%.*s", (int) item_len, f->exts);
2030  if (bytes_written < capacity) {
2031  /* Only return success if we didn't truncate */
2032  return 1;
2033  }
2034  }
2035  }
2036 
2037  return 0;
2038 }
char exts[80]
Definition: mod_format.h:45
Each supported file format is described by the following structure.
Definition: mod_format.h:43
#define SCOPED_RDLOCK(varname, lock)
scoped lock specialization for read locks
Definition: lock.h:594
ast_mutex_t lock
struct ast_format_def::@237 list
Definition: file.c:69
char mime_types[80]
Definition: mod_format.h:47
struct ast_format* ast_get_format_for_file_ext ( const char *  file_ext)

Get the ast_format associated with the given file extension.

Since
12
Parameters
file_extThe file extension for which to find the format
Return values
NULLif not found
Returns
A pointer to the ast_format associated with this file extension

Definition at line 2006 of file file.c.

References ast_format_def::exts, ast_format_def::format, lock, and SCOPED_RDLOCK.

Referenced by ast_ari_bridges_record(), ast_ari_channels_record(), ast_ari_recordings_get_stored_file(), and process_media_file().

2007 {
2008  struct ast_format_def *f;
2009  SCOPED_RDLOCK(lock, &formats.lock);
2010  AST_RWLIST_TRAVERSE(&formats, f, list) {
2011  if (exts_compare(f->exts, file_ext)) {
2012  return f->format;
2013  }
2014  }
2015 
2016  return NULL;
2017 }
char exts[80]
Definition: mod_format.h:45
Each supported file format is described by the following structure.
Definition: mod_format.h:43
#define SCOPED_RDLOCK(varname, lock)
scoped lock specialization for read locks
Definition: lock.h:594
ast_mutex_t lock
struct ast_format_def::@237 list
struct ast_format * format
Definition: mod_format.h:48
Definition: file.c:69
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
Returns
a ast_filestream pointer if it opens the file.
Return values
NULLon error.

Definition at line 790 of file file.c.

References ast_openstream_full().

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

791 {
792  return ast_openstream_full(chan, filename, preflang, 0);
793 }
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
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 795 of file file.c.

References ao2_ref, ast_alloca, ast_deactivate_generator(), ast_format_cap_alloc, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_has_type(), ast_set_write_format_from_cap(), ast_stopstream(), and fileexists_core().

Referenced by ast_openstream().

797 {
798  /*
799  * Use fileexists_core() to find a file in a compatible
800  * language and format, set up a suitable translator,
801  * and open the stream.
802  */
803  struct ast_format_cap *file_fmt_cap;
804  int res;
805  int buflen;
806  char *buf;
807 
808  if (!asis) {
809  /* do this first, otherwise we detect the wrong writeformat */
810  ast_stopstream(chan);
811  if (ast_channel_generator(chan))
813  }
814  if (preflang == NULL)
815  preflang = "";
816  buflen = strlen(preflang) + strlen(filename) + 4;
817  buf = ast_alloca(buflen);
818 
819  if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
820  return NULL;
821  }
822  if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
823  !ast_format_cap_has_type(file_fmt_cap, AST_MEDIA_TYPE_AUDIO)) {
824 
825  ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
826  ao2_ref(file_fmt_cap, -1);
827  return NULL;
828  }
829 
830  /* Set the channel to a format we can work with and save off the previous format. */
831  ast_channel_lock(chan);
832  ast_channel_set_oldwriteformat(chan, ast_channel_writeformat(chan));
833  /* Set the channel to the best format that exists for the file. */
834  res = ast_set_write_format_from_cap(chan, file_fmt_cap);
835  ast_channel_unlock(chan);
836  /* don't need this anymore now that the channel's write format is set. */
837  ao2_ref(file_fmt_cap, -1);
838 
839  if (res == -1) { /* No format available that works with this channel */
840  return NULL;
841  }
842  res = filehelper(buf, chan, NULL, ACTION_OPEN);
843  if (res >= 0)
844  return ast_channel_stream(chan);
845  return NULL;
846 }
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
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:5821
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
int ast_stopstream(struct ast_channel *tmp)
Stops a stream.
Definition: file.c:222
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:2893
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
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
Returns
a ast_filestream pointer if it opens the file.
Return values
NULLon error.

Definition at line 848 of file file.c.

References ao2_bump, ao2_ref, ast_alloca, ast_format_cap_alloc, ast_format_cap_count(), AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_get_format(), ast_format_cap_has_type(), ast_format_cap_iscompatible(), ast_format_get_name(), ast_format_get_type(), and fileexists_core().

Referenced by ast_streamfile(), and handle_getoption().

850 {
851  /* As above, but for video. But here we don't have translators
852  * so we must enforce a format.
853  */
854  struct ast_format_cap *nativeformats, *tmp_cap;
855  char *buf;
856  int buflen;
857  int i, fd;
858 
859  if (preflang == NULL) {
860  preflang = "";
861  }
862  buflen = strlen(preflang) + strlen(filename) + 4;
863  buf = ast_alloca(buflen);
864 
865  ast_channel_lock(chan);
866  nativeformats = ao2_bump(ast_channel_nativeformats(chan));
867  ast_channel_unlock(chan);
868 
869  /* is the channel capable of video without translation ?*/
870  if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) {
871  ao2_cleanup(nativeformats);
872  return NULL;
873  }
875  ao2_cleanup(nativeformats);
876  return NULL;
877  }
878  /* Video is supported, so see what video formats exist for this file */
879  if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
880  ao2_ref(tmp_cap, -1);
881  ao2_cleanup(nativeformats);
882  return NULL;
883  }
884 
885  /* iterate over file formats and pick the first one compatible with the channel's native formats */
886  for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
887  struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
888 
889  if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
890  !ast_format_cap_iscompatible(nativeformats, tmp_cap)) {
891  ao2_ref(format, -1);
892  continue;
893  }
894 
895  fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
896  if (fd >= 0) {
897  ao2_ref(format, -1);
898  ao2_ref(tmp_cap, -1);
899  ao2_cleanup(nativeformats);
900  return ast_channel_vstream(chan);
901  }
902  ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
903  ao2_ref(format, -1);
904  }
905  ao2_ref(tmp_cap, -1);
906  ao2_cleanup(nativeformats);
907 
908  return NULL;
909 }
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
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
Definition of a media format.
Definition: format.c:43
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
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
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
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 1063 of file file.c.

References ast_format_get_type(), ast_filestream::fmt, and ast_format_def::format.

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

1064 {
1065  enum fsread_res res;
1066 
1067  if (ast_format_get_type(s->fmt->format) == AST_MEDIA_TYPE_AUDIO)
1068  res = ast_readaudio_callback(s);
1069  else
1070  res = ast_readvideo_callback(s);
1071 
1072  return (res == FSREAD_FAILURE) ? -1 : 0;
1073 }
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 ast_format_def * fmt
Definition: mod_format.h:103
struct ast_format * format
Definition: mod_format.h:48
int ast_ratestream ( struct ast_filestream fs)

Return the sample rate of the stream's format.

Parameters
fsfs to act on
Returns
sample rate in Hz

Definition at line 1090 of file file.c.

References ast_format_get_sample_rate(), ast_filestream::fmt, and ast_format_def::format.

1091 {
1092  return ast_format_get_sample_rate(fs->fmt->format);
1093 }
struct ast_format_def * fmt
Definition: mod_format.h:103
struct ast_format * format
Definition: mod_format.h:48
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 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.
Returns
a struct ast_filestream on success.
Return values
NULLon failure.

Definition at line 1371 of file file.c.

References ast_closestream(), AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_strdup, build_filename(), ast_format_def::exts, ast_filestream::fmt, ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), and handle_cli_file_convert().

1372 {
1373  FILE *bfile;
1374  struct ast_format_def *f;
1375  struct ast_filestream *fs = NULL;
1376  char *fn;
1377  int format_found = 0;
1378 
1380 
1381  AST_RWLIST_TRAVERSE(&formats, f, list) {
1382  fs = NULL;
1383  if (!exts_compare(f->exts, type))
1384  continue;
1385  else
1386  format_found = 1;
1387 
1388  fn = build_filename(filename, type);
1389  if (!fn) {
1390  continue;
1391  }
1392  errno = 0;
1393  bfile = fopen(fn, "r");
1394 
1395  if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1396  ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1397  if (fs) {
1398  ast_closestream(fs);
1399  }
1400  fs = NULL;
1401  bfile = NULL;
1402  ast_free(fn);
1403  break;
1404  }
1405  /* found it */
1406  fs->trans = NULL;
1407  fs->fmt = f;
1408  fs->flags = flags;
1409  fs->mode = mode;
1410  fs->filename = ast_strdup(filename);
1411  fs->vfs = NULL;
1412  ast_free(fn);
1413  break;
1414  }
1415 
1417  if (!format_found)
1418  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1419 
1420  return fs;
1421 }
char exts[80]
Definition: mod_format.h:45
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
Each supported file format is described by the following structure.
Definition: mod_format.h:43
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
struct ast_format_def * fmt
Definition: mod_format.h:103
Definition: file.c:69
struct ast_trans_pvt * trans
Definition: mod_format.h:112
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
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
struct ast_filestream * vfs
Definition: mod_format.h:110
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 936 of file file.c.

Referenced by __ast_play_and_record(), and handle_cli_file_convert().

937 {
938  int whennext = 0;
939 
940  return read_frame(s, &whennext);
941 }
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 1075 of file file.c.

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

Referenced by ast_stream_fastforward(), ast_stream_rewind(), ast_streamfile(), and handle_getoption().

1076 {
1077  return fs->fmt->seek(fs, sample_offset, whence);
1078 }
int(* seek)(struct ast_filestream *, off_t, int)
Definition: mod_format.h:68
struct ast_format_def * fmt
Definition: mod_format.h:103
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.
Examples:
app_skel.c.

Definition at line 222 of file file.c.

References ast_closestream(), ast_format_get_name(), and ast_set_write_format().

Referenced by ast_openstream_full(), ast_play_and_wait(), ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_is(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), 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_hu(), ast_say_number_full_is(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), ast_say_number_full_vi(), ast_say_number_full_zh(), ast_stream_and_wait(), conf_exec(), dial_exec_full(), grab_transfer(), handle_getoption(), leave_voicemail(), play_file(), queue_exec(), speech_background(), and waitstream_core().

223 {
224  ast_channel_lock(tmp);
225 
226  /* Stop a running stream if there is one */
227  if (ast_channel_stream(tmp)) {
228  ast_closestream(ast_channel_stream(tmp));
229  ast_channel_stream_set(tmp, NULL);
230  if (ast_channel_oldwriteformat(tmp) && ast_set_write_format(tmp, ast_channel_oldwriteformat(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) {
235  ast_closestream(ast_channel_vstream(tmp));
236  ast_channel_vstream_set(tmp, NULL);
237  }
238 
239  ast_channel_unlock(tmp);
240 
241  return 0;
242 }
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5803
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.
If a failure is encountered, the stream will be closed before returning.
Return values
0if success.
-1if error.
digitif interrupted by a digit.
Examples:
app_skel.c.

Definition at line 1878 of file file.c.

References ast_stopstream(), ast_streamfile(), and ast_waitstream().

Referenced by __ast_play_and_record(), agent_login_exec(), announce_user_count(), ast_bridge_channel_playfile(), ast_pickup_call(), ast_record_review(), confbridge_exec(), forward_message(), grab_transfer(), join_conference_bridge(), leave_voicemail(), play_file(), play_prompt_to_user(), stream_failsound(), and vm_forwardoptions().

1879 {
1880  int res = 0;
1881  if (!ast_strlen_zero(file)) {
1882  res = ast_streamfile(chan, file, ast_channel_language(chan));
1883  if (!res) {
1884  res = ast_waitstream(chan, digits);
1885  }
1886  }
1887  if (res == -1) {
1888  ast_stopstream(chan);
1889  }
1890 
1891  return res;
1892 }
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1840
int ast_stopstream(struct ast_channel *tmp)
Stops a stream.
Definition: file.c:222
int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1293
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 1095 of file file.c.

References ast_seekstream().

1096 {
1097  return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1098 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1075
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 1100 of file file.c.

References ast_debug, ast_seekstream(), and ast_tellstream().

Referenced by __ast_play_and_record().

1101 {
1102  off_t offset = ast_tellstream(fs);
1103  if (ms * DEFAULT_SAMPLES_PER_MS > offset) {
1104  /* Don't even bother asking the file format to seek to a negative offset... */
1105  ast_debug(1, "Restarting, rather than seeking to negative offset %ld\n", (long) (offset - (ms * DEFAULT_SAMPLES_PER_MS)));
1106  return ast_seekstream(fs, 0, SEEK_SET);
1107  }
1108  return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1109 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1075
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1085
#define ast_debug(level,...)
Log a DEBUG message.
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 1293 of file file.c.

References ast_applystream(), ast_debug, AST_FLAG_MASQ_NOSTREAM, ast_format_cap_get_names(), AST_FORMAT_CAP_NAMES_LEN, ast_format_get_name(), ast_openstream(), ast_openvstream(), ast_playstream(), ast_seekstream(), ast_strdup, ast_test_suite_event_notify, ast_filestream::fmt, ast_format_def::format, and ast_filestream::vfs.

Referenced by ast_app_getdata_full(), ast_app_getdata_terminator(), 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_is(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), 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_hu(), ast_say_number_full_is(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), 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(), conf_exec(), dial_exec_full(), forward_message(), gr_say_number_female(), leave_voicemail(), and setup_privacy_args().

1295 {
1296  struct ast_filestream *fs = NULL;
1297  struct ast_filestream *vfs = NULL;
1298  off_t pos;
1299  int seekattempt;
1300  int res;
1301  char custom_filename[256];
1302  char *tmp_filename;
1303 
1304  /* If file with the same name exists in /var/lib/asterisk/sounds/custom directory, use that file.
1305  * Otherwise, use the original file*/
1306 
1307  if (ast_opt_sounds_search_custom && !is_absolute_path(filename)) {
1308  memset(custom_filename, 0, sizeof(custom_filename));
1309  snprintf(custom_filename, sizeof(custom_filename), "custom/%s", filename);
1310  fs = ast_openstream(chan, custom_filename, preflang);
1311  if (fs) {
1312  tmp_filename = custom_filename;
1313  ast_debug(3, "Found file %s in custom directory\n", filename);
1314  }
1315  }
1316 
1317  if (!fs) {
1318  fs = ast_openstream(chan, filename, preflang);
1319  if (!fs) {
1320  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
1321  ast_channel_lock(chan);
1322  ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
1323  filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
1324  ast_channel_unlock(chan);
1325  return -1;
1326  }
1327  tmp_filename = (char *)filename;
1328  }
1329 
1330  /* check to see if there is any data present (not a zero length file),
1331  * done this way because there is no where for ast_openstream_full to
1332  * return the file had no data. */
1333  pos = ftello(fs->f);
1334  seekattempt = fseeko(fs->f, -1, SEEK_END);
1335  if (seekattempt) {
1336  if (errno == EINVAL) {
1337  /* Zero-length file, as opposed to a pipe */
1338  return 0;
1339  } else {
1340  ast_seekstream(fs, 0, SEEK_SET);
1341  }
1342  } else {
1343  fseeko(fs->f, pos, SEEK_SET);
1344  }
1345 
1346  vfs = ast_openvstream(chan, tmp_filename, preflang);
1347  if (vfs) {
1348  ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
1349  }
1350 
1351  if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_MASQ_NOSTREAM))
1352  fs->orig_chan_name = ast_strdup(ast_channel_name(chan));
1353  if (ast_applystream(chan, fs))
1354  return -1;
1355  if (vfs && ast_applystream(chan, vfs))
1356  return -1;
1357  ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", tmp_filename, ast_channel_name(chan));
1358  res = ast_playstream(fs);
1359  if (!res && vfs)
1360  res = ast_playstream(vfs);
1361 
1362  if (VERBOSITY_ATLEAST(3)) {
1363  ast_channel_lock(chan);
1364  ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), tmp_filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default");
1365  ast_channel_unlock(chan);
1366  }
1367 
1368  return res;
1369 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1075
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:324
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:1063
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
struct ast_format_def * fmt
Definition: mod_format.h:103
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_format * format
Definition: mod_format.h:48
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applies a open stream to a channel.
Definition: file.c:1057
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:189
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:848
Support for dynamic strings.
Definition: strings.h:623
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
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
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 1085 of file file.c.

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

Referenced by __ast_play_and_record(), ast_stream_rewind(), handle_getoption(), and waitstream_core().

1086 {
1087  return fs->fmt->tell(fs);
1088 }
struct ast_format_def * fmt
Definition: mod_format.h:103
off_t(* tell)(struct ast_filestream *fs)
Definition: mod_format.h:70
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 1080 of file file.c.

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

Referenced by __ast_play_and_record().

1081 {
1082  return fs->fmt->trunc(fs);
1083 }
struct ast_format_def * fmt
Definition: mod_format.h:103
int(* trunc)(struct ast_filestream *fs)
Definition: mod_format.h:69
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
characterif it was interrupted by the channel.
-1on error

Definition at line 1840 of file file.c.

References waitstream_core().

Referenced by 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_is(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), 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_hu(), ast_say_number_full_is(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), 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_he(), ast_say_time_hu(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), conf_exec(), gr_say_number_female(), leave_voicemail(), and setup_privacy_args().

1841 {
1842  int res;
1843 
1844  res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1845 
1846  return sanitize_waitstream_return(res);
1847 }
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:1613
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.
characterif it was interrupted.
-1on error.

Definition at line 1859 of file file.c.

References waitstream_core().

1860 {
1861  int res;
1862 
1863  /* Waitstream, with return in the case of a valid 1 digit extension */
1864  /* in the current or specified context being pressed */
1865  if (!context)
1866  context = ast_channel_context(c);
1867  res = waitstream_core(c, NULL, NULL, NULL, 0,
1868  -1, -1, context, NULL /* no callback */);
1869 
1870  return sanitize_waitstream_return(res);
1871 }
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:1613
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.
characterif it was interrupted,
Returns
the value of the control frame if it was interrupted by some other party,
Return values
-1on error.

Definition at line 1809 of file file.c.

References waitstream_core().

1810 {
1811  return waitstream_core(c, breakon, forward, reverse, ms,
1812  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1813 }
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:1613
int ast_waitstream_fr_w_cb ( struct ast_channel c,
const char *  breakon,
const char *  forward,
const char *  rewind,
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 file.

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 milliseconds to skip forward/back
cbto call when rewind or fastforward occurs. 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.
characterif it was interrupted,
Returns
the value of the control frame if it was interrupted by some other party,
Return values
-1on error.

Definition at line 1798 of file file.c.

References waitstream_core().

1804 {
1805  return waitstream_core(c, breakon, forward, reverse, ms,
1806  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1807 }
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:1613
int ast_waitstream_full ( struct ast_channel c,
const char *  breakon,
int  audiofd,
int  monfd 
)

Same as waitstream, but with audio output to fd and monitored fd checking.

Return values
1if monfd is ready for reading

Definition at line 1849 of file file.c.

References waitstream_core().

Referenced by ast_say_enumeration_full_da(), ast_say_enumeration_full_de(), ast_say_enumeration_full_en(), ast_say_enumeration_full_is(), ast_say_number_full_cs(), ast_say_number_full_da(), ast_say_number_full_de(), 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_hu(), ast_say_number_full_is(), ast_say_number_full_it(), ast_say_number_full_ka(), ast_say_number_full_nl(), ast_say_number_full_no(), ast_say_number_full_ru(), ast_say_number_full_se(), ast_say_number_full_th(), ast_say_number_full_vi(), ast_say_number_full_zh(), and handle_getoption().

1850 {
1851  int res;
1852 
1853  res = waitstream_core(c, breakon, NULL, NULL, 0,
1854  audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1855 
1856  return sanitize_waitstream_return(res);
1857 }
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:1613
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.
Returns
a struct ast_filestream on success.
Return values
NULLon failure.

Definition at line 1423 of file file.c.

References ast_closestream(), ast_malloc, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_strdup, ast_filestream::buf, build_filename(), ast_format_def::exts, ast_filestream::fmt, ast_format_def::seek, ast_filestream::trans, and ast_filestream::vfs.

Referenced by __ast_play_and_record(), ast_writestream(), and handle_cli_file_convert().

1424 {
1425  int fd, myflags = 0;
1426  /* compiler claims this variable can be used before initialization... */
1427  FILE *bfile = NULL;
1428  struct ast_format_def *f;
1429  struct ast_filestream *fs = NULL;
1430  char *buf = NULL;
1431  size_t size = 0;
1432  int format_found = 0;
1433 
1435 
1436  /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1437  /* We really can't use O_APPEND as it will break WAV header updates */
1438  if (flags & O_APPEND) {
1439  flags &= ~O_APPEND;
1440  } else {
1441  myflags = O_TRUNC;
1442  }
1443 
1444  myflags |= O_WRONLY | O_CREAT;
1445 
1446  /* XXX need to fix this - we should just do the fopen,
1447  * not open followed by fdopen()
1448  */
1449  AST_RWLIST_TRAVERSE(&formats, f, list) {
1450  char *fn, *orig_fn = NULL;
1451  if (fs)
1452  break;
1453 
1454  if (!exts_compare(f->exts, type))
1455  continue;
1456  else
1457  format_found = 1;
1458 
1459  fn = build_filename(filename, type);
1460  if (!fn) {
1461  continue;
1462  }
1463  fd = open(fn, flags | myflags, mode);
1464  if (fd > -1) {
1465  /* fdopen() the resulting file stream */
1466  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1467  if (!bfile) {
1468  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1469  close(fd);
1470  fd = -1;
1471  }
1472  }
1473 
1474  if (ast_opt_cache_record_files && (fd > -1)) {
1475  char *c;
1476 
1477  fclose(bfile); /* this also closes fd */
1478  /*
1479  We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1480  What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1481  */
1482  orig_fn = ast_strdup(fn);
1483  for (c = fn; *c; c++)
1484  if (*c == '/')
1485  *c = '_';
1486 
1487  size = strlen(fn) + strlen(record_cache_dir) + 2;
1488  buf = ast_malloc(size);
1489  strcpy(buf, record_cache_dir);
1490  strcat(buf, "/");
1491  strcat(buf, fn);
1492  ast_free(fn);
1493  fn = buf;
1494  fd = open(fn, flags | myflags, mode);
1495  if (fd > -1) {
1496  /* fdopen() the resulting file stream */
1497  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1498  if (!bfile) {
1499  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1500  close(fd);
1501  fd = -1;
1502  }
1503  }
1504  }
1505  if (fd > -1) {
1506  errno = 0;
1507  fs = get_filestream(f, bfile);
1508  if (fs) {
1509  if ((fs->write_buffer = ast_malloc(32768))) {
1510  setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1511  }
1512  }
1513  if (!fs || rewrite_wrapper(fs, comment)) {
1514  ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1515  close(fd);
1516  if (orig_fn) {
1517  unlink(fn);
1518  unlink(orig_fn);
1519  ast_free(orig_fn);
1520  }
1521  if (fs) {
1522  ast_closestream(fs);
1523  fs = NULL;
1524  }
1525  /*
1526  * 'fn' was has either been allocated from build_filename, or that was freed
1527  * and now 'fn' points to memory allocated for 'buf'. Either way the memory
1528  * now needs to be released.
1529  */
1530  ast_free(fn);
1531  continue;
1532  }
1533  fs->trans = NULL;
1534  fs->fmt = f;
1535  fs->flags = flags;
1536  fs->mode = mode;
1537  if (orig_fn) {
1538  fs->realfilename = orig_fn;
1539  fs->filename = fn;
1540  /*
1541  * The above now manages the memory allocated for 'orig_fn' and 'fn', so
1542  * set them to NULL, so they don't get released at the end of the loop.
1543  */
1544  orig_fn = NULL;
1545  fn = NULL;
1546  } else {
1547  fs->realfilename = NULL;
1548  fs->filename = ast_strdup(filename);
1549  }
1550  fs->vfs = NULL;
1551  /* If truncated, we'll be at the beginning; if not truncated, then append */
1552  f->seek(fs, 0, SEEK_END);
1553  } else if (errno != EEXIST) {
1554  ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1555  if (orig_fn)
1556  unlink(orig_fn);
1557  }
1558  /* Free 'fn', or if 'fn' points to 'buf' then free 'buf' */
1559  ast_free(fn);
1560  ast_free(orig_fn);
1561  }
1562 
1564 
1565  if (!format_found)
1566  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1567 
1568  return fs;
1569 }
char exts[80]
Definition: mod_format.h:45
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1111
Each supported file format is described by the following structure.
Definition: mod_format.h:43
int(* seek)(struct ast_filestream *, off_t, int)
Definition: mod_format.h:68
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
struct ast_format_def * fmt
Definition: mod_format.h:103
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
Definition: file.c:69
struct ast_trans_pvt * trans
Definition: mod_format.h:112
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
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
struct ast_filestream * vfs
Definition: mod_format.h:110
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 244 of file file.c.

References ao2_replace, ast_debug, ast_format_cmp(), AST_FORMAT_CMP_EQUAL, AST_FORMAT_CMP_NOT_EQUAL, ast_format_get_name(), ast_format_get_type(), AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_LIST_NEXT, ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_writefile(), ast_filestream::fmt, ast_format_def::format, ast_frame_subclass::format, ast_frame::frametype, ast_format_def::name, ast_frame::subclass, ast_filestream::trans, ast_filestream::vfs, and ast_format_def::write.

Referenced by __ast_play_and_record(), and handle_cli_file_convert().

245 {
246  int res = -1;
247  if (f->frametype == AST_FRAME_VIDEO) {
248  if (ast_format_get_type(fs->fmt->format) == AST_MEDIA_TYPE_AUDIO) {
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 */
273  if (fs->trans && (ast_format_cmp(f->subclass.format, fs->lastwriteformat) != AST_FORMAT_CMP_EQUAL)) {
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;
285  ao2_replace(fs->lastwriteformat, f->subclass.format);
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 }
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
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:566
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
struct ast_frame_subclass subclass
int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
Writes a frame to a stream.
Definition: file.c:244
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:1423
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
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:486
struct ast_format_def * fmt
Definition: mod_format.h:103
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_format * format
Definition: mod_format.h:48
char name[80]
Definition: mod_format.h:44
struct ast_trans_pvt * trans
Definition: mod_format.h:112
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
Definition: astobj2.h:501
Data structure associated with a single frame of data.
enum ast_frame_type frametype
struct ast_filestream * vfs
Definition: mod_format.h:110
struct ast_format * format
int(* write)(struct ast_filestream *, struct ast_frame *)
Definition: mod_format.h:66
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:476