Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Enumerations | Functions | Variables
file.c File Reference

Generic File Format Support. More...

#include "asterisk.h"
#include <dirent.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <math.h>
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include "asterisk/mod_format.h"
#include "asterisk/cli.h"
#include "asterisk/channel.h"
#include "asterisk/sched.h"
#include "asterisk/translate.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"
#include "asterisk/test.h"
#include "asterisk/stasis.h"
#include "asterisk/json.h"
#include "asterisk/stasis_system.h"
#include "asterisk/media_cache.h"
Include dependency graph for file.c:

Go to the source code of this file.

Data Structures

struct  formats
 

Macros

#define exts_compare(list, type)   (type_in_list((list), (type), strcmp))
 
#define FORMAT   "%-10s %-10s %-20s\n"
 
#define FORMAT2   "%-10s %-10s %-20s\n"
 

Enumerations

enum  file_action {
  ACTION_EXISTS = 1, ACTION_DELETE, ACTION_RENAME, ACTION_OPEN,
  ACTION_COPY
}
 
enum  fsread_res { FSREAD_FAILURE, FSREAD_SUCCESS_SCHED, FSREAD_SUCCESS_NOSCHED }
 
enum  wrap_fn { WRAP_OPEN, WRAP_REWRITE }
 

Functions

static int __ast_file_read_dirs (const char *path, ast_file_on_file on_file, void *obj, int max_depth)
 
int __ast_format_def_register (const struct ast_format_def *f, struct ast_module *mod)
 Register a new file format capability. Adds a format to Asterisk's format abilities. More...
 
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_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 *filename, const char *filename2, 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 *filename, const char *filename2, const char *fmt)
 Renames a file. More...
 
int ast_format_def_unregister (const char *name)
 Unregisters a file format. More...
 
char * ast_format_str_reduce (char *fmts)
 
static int ast_fsread_audio (const void *data)
 
static int ast_fsread_video (const void *data)
 
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...
 
static enum fsread_res ast_readaudio_callback (struct ast_filestream *s)
 
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...
 
static enum fsread_res ast_readvideo_callback (struct ast_filestream *s)
 
int ast_seekstream (struct ast_filestream *fs, off_t sample_offset, int whence)
 Seeks into stream. More...
 
int ast_stopstream (struct ast_channel *tmp)
 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 *chan, 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 *reverse, 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 *reverse, int ms, ast_waitstream_fr_cb cb)
 Same as waitstream_fr but allows a callback to be alerted when a user fastforwards or rewinds the file. More...
 
int ast_waitstream_full (struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
 
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...
 
static char * build_filename (const char *filename, const char *ext)
 construct a filename. Absolute pathnames are preserved, relative names are prefixed by the sounds/ directory. The wav49 suffix is replaced by 'WAV'. Returns a malloc'ed string to be freed by the caller. More...
 
static int copy (const char *infile, const char *outfile)
 
static void file_shutdown (void)
 
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. More...
 
static int fileexists_test (const char *filename, const char *fmt, const char *lang, char *buf, int buflen, struct ast_format_cap *result_cap)
 test if a file exists for a given format. More...
 
static int filehelper (const char *filename, const void *arg2, const char *fmt, const enum file_action action)
 
static void filestream_close (struct ast_filestream *f)
 
static void filestream_destructor (void *arg)
 
static int fn_wrapper (struct ast_filestream *s, const char *comment, enum wrap_fn mode)
 
static struct ast_filestreamget_filestream (struct ast_format_def *fmt, FILE *bfile)
 
static char * handle_cli_core_show_file_formats (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int is_absolute_path (const char *filename)
 
static int is_remote_path (const char *filename)
 
static struct ast_jsonjson_array_from_list (const char *list, const char *sep)
 
static int open_wrapper (struct ast_filestream *s)
 
static int publish_format_update (const struct ast_format_def *f, struct stasis_message_type *type)
 
static struct ast_frameread_frame (struct ast_filestream *s, int *whennext)
 
static int rewrite_wrapper (struct ast_filestream *s, const char *comment)
 
static int sanitize_waitstream_return (int return_value)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_format_register_type)
 
 STASIS_MESSAGE_TYPE_DEFN (ast_format_unregister_type)
 
static int type_in_list (const char *list, const char *type, int(*cmp)(const char *s1, const char *s2))
 
static void waitstream_control (struct ast_channel *c, enum ast_waitstream_fr_cb_values type, ast_waitstream_fr_cb cb, int skip_ms)
 
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 More...
 

Variables

int ast_language_is_prefix = 1
 The following variable controls the layout of localized sound files. If 0, use the historical layout with prefix just before the filename (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm), if 1 put the prefix at the beginning of the filename (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm). The latter permits a language to be entirely in one directory. More...
 
static struct ast_cli_entry cli_file []
 
static struct formats formats = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
 
static ast_mutex_t read_dirs_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
 Lock to hold when iterating over directories. More...
 

Detailed Description

Generic File Format Support.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file file.c.

Macro Definition Documentation

◆ exts_compare

#define exts_compare (   list,
  type 
)    (type_in_list((list), (type), strcmp))

◆ FORMAT

#define FORMAT   "%-10s %-10s %-20s\n"

◆ FORMAT2

#define FORMAT2   "%-10s %-10s %-20s\n"

Enumeration Type Documentation

◆ file_action

Enumerator
ACTION_EXISTS 
ACTION_DELETE 
ACTION_RENAME 
ACTION_OPEN 
ACTION_COPY 

Definition at line 495 of file file.c.

495  {
496  ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
497  ACTION_DELETE, /* delete file, return 0 on success, -1 on error */
498  ACTION_RENAME, /* rename file. return 0 on success, -1 on error */
499  ACTION_OPEN,
500  ACTION_COPY /* copy file. return 0 on success, -1 on error */
501 };

◆ fsread_res

enum fsread_res
Enumerator
FSREAD_FAILURE 
FSREAD_SUCCESS_SCHED 
FSREAD_SUCCESS_NOSCHED 

Definition at line 906 of file file.c.

◆ wrap_fn

enum wrap_fn
Enumerator
WRAP_OPEN 
WRAP_REWRITE 

Definition at line 466 of file file.c.

Function Documentation

◆ __ast_file_read_dirs()

static int __ast_file_read_dirs ( const char *  path,
ast_file_on_file  on_file,
void *  obj,
int  max_depth 
)
static

Definition at line 1113 of file file.c.

References ast_debug, ast_free, ast_log, ast_malloc, errno, LOG_ERROR, NULL, and RAII_VAR.

Referenced by ast_file_read_dirs().

1115 {
1116  DIR *dir;
1117  struct dirent *entry;
1118  int res;
1119 
1120  if (!(dir = opendir(path))) {
1121  ast_log(LOG_ERROR, "Error opening directory - %s: %s\n",
1122  path, strerror(errno));
1123  return -1;
1124  }
1125 
1126  --max_depth;
1127 
1128  res = 0;
1129 
1130  while ((entry = readdir(dir)) != NULL && !errno) {
1131  int is_file = 0;
1132  int is_dir = 0;
1133  RAII_VAR(char *, full_path, NULL, ast_free);
1134 
1135  if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
1136  continue;
1137  }
1138 
1139 /*
1140  * If the dirent structure has a d_type use it to determine if we are dealing with
1141  * a file or directory. Unfortunately if it doesn't have it, or if the type is
1142  * unknown, or a link then we'll need to use the stat function instead.
1143  */
1144 #ifdef _DIRENT_HAVE_D_TYPE
1145  if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK) {
1146  is_file = entry->d_type == DT_REG;
1147  is_dir = entry->d_type == DT_DIR;
1148  } else
1149 #endif
1150  {
1151  struct stat statbuf;
1152 
1153  /*
1154  * Don't use alloca or we risk blowing out the stack if recursing
1155  * into subdirectories.
1156  */
1157  full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
1158  if (!full_path) {
1159  return -1;
1160  }
1161  sprintf(full_path, "%s/%s", path, entry->d_name);
1162 
1163  if (stat(full_path, &statbuf)) {
1164  ast_log(LOG_ERROR, "Error reading path stats - %s: %s\n",
1165  full_path, strerror(errno));
1166  /*
1167  * Output an error, but keep going. It could just be
1168  * a broken link and other files could be fine.
1169  */
1170  continue;
1171  }
1172 
1173  is_file = S_ISREG(statbuf.st_mode);
1174  is_dir = S_ISDIR(statbuf.st_mode);
1175  }
1176 
1177  if (is_file) {
1178  /* If the handler returns non-zero then stop */
1179  if ((res = on_file(path, entry->d_name, obj))) {
1180  break;
1181  }
1182  /* Otherwise move on to next item in directory */
1183  continue;
1184  }
1185 
1186  if (!is_dir) {
1187  ast_debug(5, "Skipping %s: not a regular file or directory\n", full_path);
1188  continue;
1189  }
1190 
1191  /* Only re-curse into sub-directories if not at the max depth */
1192  if (max_depth != 0) {
1193  if (!full_path) {
1194  /* Don't use alloca. See note above. */
1195  full_path = ast_malloc(strlen(path) + strlen(entry->d_name) + 2);
1196  if (!full_path) {
1197  return -1;
1198  }
1199  sprintf(full_path, "%s/%s", path, entry->d_name);
1200  }
1201 
1202  if ((res = __ast_file_read_dirs(full_path, on_file, obj, max_depth))) {
1203  break;
1204  }
1205  }
1206  }
1207 
1208  closedir(dir);
1209 
1210  if (!res && errno) {
1211  ast_log(LOG_ERROR, "Error while reading directories - %s: %s\n",
1212  path, strerror(errno));
1213  res = -1;
1214  }
1215 
1216  return res;
1217 }
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
#define LOG_ERROR
Definition: logger.h:285
int errno
#define ast_free(a)
Definition: astmm.h:182
static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file, void *obj, int max_depth)
Definition: file.c:1113
Definition: search.h:40

◆ __ast_format_def_register()

int __ast_format_def_register ( const struct ast_format_def f,
struct ast_module mod 
)

Register a new file format capability. Adds a format to Asterisk's format abilities.

Return values
0on success
-1on failure

Definition at line 124 of file file.c.

References a, ast_calloc, ast_format_register_type(), ast_log, AST_RWLIST_INSERT_HEAD, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, b, ast_format_def::buf_size, ast_format_def::exts, ast_format_def::list, LOG_WARNING, ast_format_def::module, ast_format_def::name, publish_format_update(), and tmp().

125 {
126  struct ast_format_def *tmp;
127 
130  if (!strcasecmp(f->name, tmp->name)) {
132  ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
133  return -1;
134  }
135  }
136  if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
138  return -1;
139  }
140  *tmp = *f;
141  tmp->module = mod;
142  if (tmp->buf_size) {
143  /*
144  * Align buf_size properly, rounding up to the machine-specific
145  * alignment for pointers.
146  */
147  struct _test_align { void *a, *b; } p;
148  int align = (char *)&p.b - (char *)&p.a;
149  tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
150  }
151 
152  memset(&tmp->list, 0, sizeof(tmp->list));
153 
154  AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
156  ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
158 
159  return 0;
160 }
char exts[80]
Definition: mod_format.h:45
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
#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
#define ast_verb(level,...)
Definition: logger.h:463
struct ast_format_def::@276 list
struct ast_module * module
Definition: mod_format.h:93
#define AST_RWLIST_INSERT_HEAD
Definition: linkedlists.h:717
#define ast_log
Definition: astobj2.c:42
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
Definition: file.c:69
struct stasis_message_type * ast_format_register_type(void)
Get the message type used for signaling a format registration.
char name[80]
Definition: mod_format.h:44
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static struct test_val b
static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
Definition: file.c:93
static struct test_val a

◆ ast_applystream()

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 1020 of file file.c.

References ast_filestream::owner.

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

1021 {
1022  s->owner = chan;
1023  return 0;
1024 }
struct ast_channel * owner
Definition: mod_format.h:116

◆ ast_closestream()

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 1068 of file file.c.

References ao2_ref, filestream_close(), and NULL.

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

1069 {
1070  /* This used to destroy the filestream, but it now just decrements a refcount.
1071  * We close the stream in order to quit queuing frames now, because we might
1072  * change the writeformat, which could result in a subsequent write error, if
1073  * the format is different. */
1074  if (f == NULL) {
1075  return 0;
1076  }
1077  filestream_close(f);
1078  ao2_ref(f, -1);
1079  return 0;
1080 }
static void filestream_close(struct ast_filestream *f)
Definition: file.c:357
#define NULL
Definition: resample.c:96
#define ao2_ref(o, delta)
Definition: astobj2.h:464

◆ ast_file_init()

int ast_file_init ( void  )

Provided by file.c

Definition at line 1983 of file file.c.

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

Referenced by asterisk_daemon().

1984 {
1989  return 0;
1990 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static struct ast_cli_entry cli_file[]
Definition: file.c:1972
#define STASIS_MESSAGE_TYPE_INIT(name)
Boiler-plate messaging macro for initializing message types.
Definition: stasis.h:1501
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static void file_shutdown(void)
Definition: file.c:1976
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.

◆ ast_file_read_dirs()

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)
Returns
-1 or errno on failure, otherwise 0

Definition at line 1231 of file file.c.

References __ast_file_read_dirs(), ast_mutex_lock, ast_mutex_unlock, errno, and read_dirs_lock.

Referenced by ast_media_index_update_for_file(), AST_TEST_DEFINE(), module_load_helper(), and stasis_app_stored_recording_find_all().

1232 {
1233  int res;
1234 
1235  errno = 0;
1236 
1237 #if !defined(__GLIBC__)
1239 #endif
1240 
1241  res = __ast_file_read_dirs(dir_name, on_file, obj, max_depth);
1242 
1243 #if !defined(__GLIBC__)
1245 #endif
1246 
1247  return res;
1248 }
static ast_mutex_t read_dirs_lock
Lock to hold when iterating over directories.
Definition: file.c:1228
#define ast_mutex_lock(a)
Definition: lock.h:187
int errno
static int __ast_file_read_dirs(const char *path, ast_file_on_file on_file, void *obj, int max_depth)
Definition: file.c:1113
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ ast_filecopy()

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 1108 of file file.c.

References ACTION_COPY, and filehelper().

Referenced by copy_plain_file(), msg_create_from_file(), and vm_forwardoptions().

1109 {
1110  return filehelper(filename, filename2, fmt, ACTION_COPY);
1111 }
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:514

◆ ast_filedelete()

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 1098 of file file.c.

References ACTION_DELETE, filehelper(), and NULL.

Referenced by __ast_play_and_record(), announce_thread(), ast_monitor_start(), ast_monitor_stop(), async_delete_name_rec_task(), conf_free(), conf_rec_name(), conf_run(), confbridge_exec(), dial_exec_full(), handle_cli_file_convert(), leave_voicemail(), msg_create_from_file(), play_message(), play_record_review(), record_exec(), recording_cancel(), setup_privacy_args(), valid_priv_reply(), vm_allocate_dh(), and vm_delete().

1099 {
1100  return filehelper(filename, NULL, fmt, ACTION_DELETE);
1101 }
#define NULL
Definition: resample.c:96
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:514

◆ ast_fileexists()

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
0,false.The file does not exist
1,true.The file does exist.

Definition at line 1086 of file file.c.

References ast_alloca, buf, fileexists_core(), and NULL.

Referenced by announce_thread(), app_exec(), ast_get_character_str(), ast_get_digit_str(), ast_get_phonetic_str(), ast_moh_files_next(), ast_monitor_start(), ast_monitor_stop(), AST_TEST_DEFINE(), common_exec(), conf_run(), dial_exec_full(), eivr_comm(), forward_message(), get_folder(), invent_message(), leave_voicemail(), meetme_menu_admin_extended(), minivm_delete_exec(), msg_create_from_file(), page_exec(), play_file(), play_message(), play_message_by_id_helper(), play_message_callerid(), readexten_exec(), record_exec(), retrydial_exec(), sayname(), setup_privacy_args(), sound_file_exists(), stasis_app_control_record(), valid_priv_reply(), vm_allocate_dh(), vm_intro(), vm_msg_play(), vm_newuser_setup(), vm_options(), and vm_tempgreeting().

1087 {
1088  char *buf;
1089  int buflen;
1090 
1091  if (preflang == NULL)
1092  preflang = "";
1093  buflen = strlen(preflang) + strlen(filename) + 4; /* room for everything */
1094  buf = ast_alloca(buflen);
1095  return fileexists_core(filename, fmt, preflang, buf, buflen, NULL) ? 1 : 0;
1096 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
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:706
#define NULL
Definition: resample.c:96
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290

◆ ast_filerename()

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 1103 of file file.c.

References ACTION_RENAME, and filehelper().

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

1104 {
1105  return filehelper(filename, filename2, fmt, ACTION_RENAME);
1106 }
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:514

◆ ast_format_def_unregister()

int ast_format_def_unregister ( const char *  name)

Unregisters a file format.

Parameters
namethe name of the format you wish to unregister Unregisters a format based on the name of the format.
Return values
0on success
-1on failure to unregister

Definition at line 162 of file file.c.

References ast_format_unregister_type(), ast_free, ast_log, AST_RWLIST_REMOVE_CURRENT, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_verb, ast_format_def::list, LOG_WARNING, ast_format_def::name, publish_format_update(), and tmp().

Referenced by unload_module().

163 {
164  struct ast_format_def *tmp;
165  int res = -1;
166 
169  if (!strcasecmp(name, tmp->name)) {
172  ast_free(tmp);
173  res = 0;
174  }
175  }
178 
179  if (!res)
180  ast_verb(2, "Unregistered format %s\n", name);
181  else
182  ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
183 
184  return res;
185 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
#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
#define ast_verb(level,...)
Definition: logger.h:463
struct ast_format_def::@276 list
#define ast_log
Definition: astobj2.c:42
struct stasis_message_type * ast_format_unregister_type(void)
Get the message type used for signaling a format unregistration.
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:569
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:544
Definition: file.c:69
char name[80]
Definition: mod_format.h:44
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
static int publish_format_update(const struct ast_format_def *f, struct stasis_message_type *type)
Definition: file.c:93
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:616

◆ ast_format_str_reduce()

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 1826 of file file.c.

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

Referenced by actual_load_config(), and AST_TEST_DEFINE().

1827 {
1828  struct ast_format_def *f;
1829  struct ast_format_def *fmts_ptr[AST_MAX_FORMATS];
1830  char *fmts_str[AST_MAX_FORMATS];
1831  char *stringp, *type;
1832  char *orig = fmts;
1833  int i, j, x, first, found = 0;
1834  int len = strlen(fmts) + 1;
1835  int res;
1836 
1837  if (AST_RWLIST_RDLOCK(&formats)) {
1838  ast_log(LOG_WARNING, "Unable to lock format list\n");
1839  return NULL;
1840  }
1841 
1842  stringp = ast_strdupa(fmts);
1843 
1844  for (x = 0; (type = strsep(&stringp, "|")) && x < AST_MAX_FORMATS; x++) {
1846  if (exts_compare(f->exts, type)) {
1847  found = 1;
1848  break;
1849  }
1850  }
1851 
1852  fmts_str[x] = type;
1853  if (found) {
1854  fmts_ptr[x] = f;
1855  } else {
1856  fmts_ptr[x] = NULL;
1857  }
1858  }
1860 
1861  first = 1;
1862  for (i = 0; i < x; i++) {
1863  /* ignore invalid entries */
1864  if (!fmts_ptr[i]) {
1865  ast_log(LOG_WARNING, "ignoring unknown format '%s'\n", fmts_str[i]);
1866  continue;
1867  }
1868 
1869  /* special handling for the first entry */
1870  if (first) {
1871  res = snprintf(fmts, len, "%s", fmts_str[i]);
1872  fmts += res;
1873  len -= res;
1874  first = 0;
1875  continue;
1876  }
1877 
1878  found = 0;
1879  for (j = 0; j < i; j++) {
1880  /* this is a duplicate */
1881  if (fmts_ptr[j] == fmts_ptr[i]) {
1882  found = 1;
1883  break;
1884  }
1885  }
1886 
1887  if (!found) {
1888  res = snprintf(fmts, len, "|%s", fmts_str[i]);
1889  fmts += res;
1890  len -= res;
1891  }
1892  }
1893 
1894  if (first) {
1895  ast_log(LOG_WARNING, "no known formats found in format list (%s)\n", orig);
1896  return NULL;
1897  }
1898 
1899  return orig;
1900 }
char exts[80]
Definition: mod_format.h:45
static const char type[]
Definition: chan_ooh323.c:109
#define exts_compare(list, type)
Definition: file.c:351
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#define NULL
Definition: resample.c:96
Each supported file format is described by the following structure.
Definition: mod_format.h:43
struct ast_format_def::@276 list
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_log
Definition: astobj2.c:42
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Definition: file.c:69
struct sla_ringing_trunk * first
Definition: app_meetme.c:1092
char * strsep(char **str, const char *delims)
#define AST_MAX_FORMATS
Definition: file.h:44

◆ ast_fsread_audio()

static int ast_fsread_audio ( const void *  data)
static

Definition at line 962 of file file.c.

References ast_fsread_video(), ast_readaudio_callback(), and FSREAD_SUCCESS_SCHED.

Referenced by ast_readaudio_callback().

963 {
964  struct ast_filestream *fs = (struct ast_filestream *)data;
965  enum fsread_res res;
966 
967  res = ast_readaudio_callback(fs);
968 
969  if (res == FSREAD_SUCCESS_SCHED)
970  return 1;
971 
972  return 0;
973 }
fsread_res
Definition: file.c:906
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
static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
Definition: file.c:914

◆ ast_fsread_video()

static int ast_fsread_video ( const void *  data)
static

Definition at line 1007 of file file.c.

References ast_readvideo_callback(), and FSREAD_SUCCESS_SCHED.

Referenced by ast_fsread_audio().

1008 {
1009  struct ast_filestream *fs = (struct ast_filestream *)data;
1010  enum fsread_res res;
1011 
1012  res = ast_readvideo_callback(fs);
1013 
1014  if (res == FSREAD_SUCCESS_SCHED)
1015  return 1;
1016 
1017  return 0;
1018 }
static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
Definition: file.c:977
fsread_res
Definition: file.c:906
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

◆ ast_get_extension_for_mime_type()

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 1951 of file file.c.

References ast_assert, AST_RWLIST_TRAVERSE, ast_format_def::exts, formats::lock, lock, ast_format_def::mime_types, SCOPED_RDLOCK, and type_in_list().

Referenced by bucket_file_update_path().

1952 {
1953  struct ast_format_def *f;
1955 
1956  ast_assert(buffer && capacity);
1957 
1959  if (type_in_list(f->mime_types, mime_type, strcasecmp)) {
1960  size_t item_len = strcspn(f->exts, "|");
1961  size_t bytes_written = snprintf(buffer, capacity, ".%.*s", (int) item_len, f->exts);
1962  if (bytes_written < capacity) {
1963  /* Only return success if we didn't truncate */
1964  return 1;
1965  }
1966  }
1967  }
1968 
1969  return 0;
1970 }
char exts[80]
Definition: mod_format.h:45
#define ast_assert(a)
Definition: utils.h:695
ast_rwlock_t lock
Definition: file.c:69
Each supported file format is described by the following structure.
Definition: mod_format.h:43
struct ast_format_def::@276 list
#define SCOPED_RDLOCK(varname, lock)
scoped lock specialization for read locks
Definition: lock.h:592
ast_mutex_t lock
Definition: app_meetme.c:1091
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
Definition: file.c:69
char mime_types[80]
Definition: mod_format.h:47
static int type_in_list(const char *list, const char *type, int(*cmp)(const char *s1, const char *s2))
Definition: file.c:338

◆ ast_get_format_for_file_ext()

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
Apointer to the ast_format associated with this file extension

Definition at line 1938 of file file.c.

References AST_RWLIST_TRAVERSE, ast_format_def::exts, exts_compare, ast_format_def::format, formats::lock, lock, NULL, and SCOPED_RDLOCK.

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

1939 {
1940  struct ast_format_def *f;
1943  if (exts_compare(f->exts, file_ext)) {
1944  return f->format;
1945  }
1946  }
1947 
1948  return NULL;
1949 }
char exts[80]
Definition: mod_format.h:45
#define exts_compare(list, type)
Definition: file.c:351
#define NULL
Definition: resample.c:96
ast_rwlock_t lock
Definition: file.c:69
Each supported file format is described by the following structure.
Definition: mod_format.h:43
struct ast_format_def::@276 list
#define SCOPED_RDLOCK(varname, lock)
scoped lock specialization for read locks
Definition: lock.h:592
ast_mutex_t lock
Definition: app_meetme.c:1091
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format * format
Definition: mod_format.h:48
Definition: file.c:69

◆ ast_openstream()

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 755 of file file.c.

References ast_openstream_full().

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

756 {
757  return ast_openstream_full(chan, filename, preflang, 0);
758 }
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:760

◆ ast_openstream_full()

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 760 of file file.c.

References ACTION_OPEN, ao2_ref, ast_alloca, ast_channel_generator(), ast_channel_lock, ast_channel_set_oldwriteformat(), ast_channel_stream(), ast_channel_unlock, ast_channel_writeformat(), ast_deactivate_generator(), ast_format_cap_alloc, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_has_type(), ast_log, AST_MEDIA_TYPE_AUDIO, ast_set_write_format_from_cap(), ast_stopstream(), buf, fileexists_core(), filehelper(), LOG_WARNING, and NULL.

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

761 {
762  /*
763  * Use fileexists_core() to find a file in a compatible
764  * language and format, set up a suitable translator,
765  * and open the stream.
766  */
767  struct ast_format_cap *file_fmt_cap;
768  int res;
769  int buflen;
770  char *buf;
771 
772  if (!asis) {
773  /* do this first, otherwise we detect the wrong writeformat */
774  ast_stopstream(chan);
775  if (ast_channel_generator(chan))
777  }
778  if (preflang == NULL)
779  preflang = "";
780  buflen = strlen(preflang) + strlen(filename) + 4;
781  buf = ast_alloca(buflen);
782 
783  if (!(file_fmt_cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
784  return NULL;
785  }
786  if (!fileexists_core(filename, NULL, preflang, buf, buflen, file_fmt_cap) ||
788 
789  ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
790  ao2_ref(file_fmt_cap, -1);
791  return NULL;
792  }
793 
794  /* Set the channel to a format we can work with and save off the previous format. */
795  ast_channel_lock(chan);
797  /* Set the channel to the best format that exists for the file. */
798  res = ast_set_write_format_from_cap(chan, file_fmt_cap);
799  ast_channel_unlock(chan);
800  /* don't need this anymore now that the channel's write format is set. */
801  ao2_ref(file_fmt_cap, -1);
802 
803  if (res == -1) { /* No format available that works with this channel */
804  return NULL;
805  }
806  res = filehelper(buf, chan, NULL, ACTION_OPEN);
807  if (res >= 0)
808  return ast_channel_stream(chan);
809  return NULL;
810 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
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:706
#define NULL
Definition: resample.c:96
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:5908
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
struct ast_generator * ast_channel_generator(const struct ast_channel *chan)
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:514
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:187
#define ast_channel_unlock(chan)
Definition: channel.h:2946
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
void ast_deactivate_generator(struct ast_channel *chan)
Definition: channel.c:2902
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
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:615

◆ ast_openvstream()

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 812 of file file.c.

References ACTION_OPEN, ao2_bump, ao2_cleanup, ao2_ref, ast_alloca, ast_channel_lock, ast_channel_nativeformats(), ast_channel_unlock, ast_channel_vstream(), 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(), ast_log, AST_MEDIA_TYPE_VIDEO, buf, fileexists_core(), filehelper(), format, LOG_WARNING, and NULL.

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

813 {
814  /* As above, but for video. But here we don't have translators
815  * so we must enforce a format.
816  */
817  struct ast_format_cap *nativeformats, *tmp_cap;
818  char *buf;
819  int buflen;
820  int i, fd;
821 
822  if (preflang == NULL) {
823  preflang = "";
824  }
825  buflen = strlen(preflang) + strlen(filename) + 4;
826  buf = ast_alloca(buflen);
827 
828  ast_channel_lock(chan);
829  nativeformats = ao2_bump(ast_channel_nativeformats(chan));
830  ast_channel_unlock(chan);
831 
832  /* is the channel capable of video without translation ?*/
833  if (!ast_format_cap_has_type(nativeformats, AST_MEDIA_TYPE_VIDEO)) {
834  ao2_cleanup(nativeformats);
835  return NULL;
836  }
838  ao2_cleanup(nativeformats);
839  return NULL;
840  }
841  /* Video is supported, so see what video formats exist for this file */
842  if (!fileexists_core(filename, NULL, preflang, buf, buflen, tmp_cap)) {
843  ao2_ref(tmp_cap, -1);
844  ao2_cleanup(nativeformats);
845  return NULL;
846  }
847 
848  /* iterate over file formats and pick the first one compatible with the channel's native formats */
849  for (i = 0; i < ast_format_cap_count(tmp_cap); ++i) {
850  struct ast_format *format = ast_format_cap_get_format(tmp_cap, i);
851 
852  if ((ast_format_get_type(format) != AST_MEDIA_TYPE_VIDEO) ||
853  !ast_format_cap_iscompatible(nativeformats, tmp_cap)) {
854  ao2_ref(format, -1);
855  continue;
856  }
857 
858  fd = filehelper(buf, chan, ast_format_get_name(format), ACTION_OPEN);
859  if (fd >= 0) {
860  ao2_ref(format, -1);
861  ao2_ref(tmp_cap, -1);
862  ao2_cleanup(nativeformats);
863  return ast_channel_vstream(chan);
864  }
865  ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
866  ao2_ref(format, -1);
867  }
868  ao2_ref(tmp_cap, -1);
869  ao2_cleanup(nativeformats);
870 
871  return NULL;
872 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
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_filestream * ast_channel_vstream(const struct ast_channel *chan)
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
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:706
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 NULL
Definition: resample.c:96
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:514
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
#define ast_channel_unlock(chan)
Definition: channel.h:2946
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
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
static snd_pcm_format_t format
Definition: chan_alsa.c:102
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:615
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:655

◆ ast_playstream()

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 1026 of file file.c.

References ast_format_get_type(), AST_MEDIA_TYPE_AUDIO, ast_readaudio_callback(), ast_readvideo_callback(), ast_filestream::fmt, ast_format_def::format, and FSREAD_FAILURE.

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

1027 {
1028  enum fsread_res res;
1029 
1031  res = ast_readaudio_callback(s);
1032  else
1033  res = ast_readvideo_callback(s);
1034 
1035  return (res == FSREAD_FAILURE) ? -1 : 0;
1036 }
static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
Definition: file.c:977
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
fsread_res
Definition: file.c:906
static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
Definition: file.c:914

◆ ast_ratestream()

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 1053 of file file.c.

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

Referenced by msg_create_from_file().

1054 {
1055  return ast_format_get_sample_rate(fs->fmt->format);
1056 }
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

◆ ast_readaudio_callback()

static enum fsread_res ast_readaudio_callback ( struct ast_filestream s)
static

Definition at line 914 of file file.c.

References ast_channel_name(), ast_channel_sched(), ast_channel_streamid_set(), ast_channel_timingfd(), ast_debug, ast_format_get_sample_rate(), ast_frfree, ast_fsread_audio(), ast_sched_add(), ast_settimeout(), ast_settimeout_full(), ast_write(), ast_filestream::fmt, ast_format_def::format, FSREAD_FAILURE, FSREAD_SUCCESS_NOSCHED, FSREAD_SUCCESS_SCHED, ast_filestream::lasttimeout, NULL, ast_filestream::orig_chan_name, ast_filestream::owner, read_frame(), and roundf().

Referenced by ast_fsread_audio(), and ast_playstream().

915 {
916  int whennext = 0;
917 
918  while (!whennext) {
919  struct ast_frame *fr;
920 
921  if (s->orig_chan_name && strcasecmp(ast_channel_name(s->owner), s->orig_chan_name)) {
922  goto return_failure;
923  }
924 
925  fr = read_frame(s, &whennext);
926 
927  if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
928  if (fr) {
929  ast_debug(2, "Failed to write frame\n");
930  ast_frfree(fr);
931  }
932  goto return_failure;
933  }
934 
935  if (fr) {
936  ast_frfree(fr);
937  }
938  }
939 
940  if (whennext != s->lasttimeout) {
941  if (ast_channel_timingfd(s->owner) > -1) {
942  float samp_rate = (float) ast_format_get_sample_rate(s->fmt->format);
943  unsigned int rate;
944 
945  rate = (unsigned int) roundf(samp_rate / ((float) whennext));
946 
947  ast_settimeout_full(s->owner, rate, ast_fsread_audio, s, 1);
948  } else {
950  }
951  s->lasttimeout = whennext;
952  return FSREAD_SUCCESS_NOSCHED;
953  }
954  return FSREAD_SUCCESS_SCHED;
955 
956 return_failure:
958  ast_settimeout(s->owner, 0, NULL, NULL);
959  return FSREAD_FAILURE;
960 }
int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data, unsigned int is_ao2_obj)
Definition: channel.c:3194
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
int ast_settimeout(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data)
Enable or disable timer ticks for a channel.
Definition: channel.c:3189
struct ast_format_def * fmt
Definition: mod_format.h:103
const char * orig_chan_name
Definition: mod_format.h:125
struct ast_format * format
Definition: mod_format.h:48
int ast_channel_timingfd(const struct ast_channel *chan)
struct ast_sched_context * ast_channel_sched(const struct ast_channel *chan)
struct ast_channel * owner
Definition: mod_format.h:116
void ast_channel_streamid_set(struct ast_channel *chan, int value)
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:874
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5189
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:565
const char * ast_channel_name(const struct ast_channel *chan)
float roundf(float x)
static int ast_fsread_audio(const void *data)
Definition: file.c:962
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
#define ast_frfree(fr)
Data structure associated with a single frame of data.

◆ ast_readfile()

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 1309 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_def::exts, exts_compare, ast_filestream::f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, NULL, open_wrapper(), ast_filestream::trans, and ast_filestream::vfs.

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

1310 {
1311  FILE *bfile;
1312  struct ast_format_def *f;
1313  struct ast_filestream *fs = NULL;
1314  char *fn;
1315  int format_found = 0;
1316 
1318 
1319  AST_RWLIST_TRAVERSE(&formats, f, list) {
1320  fs = NULL;
1321  if (!exts_compare(f->exts, type))
1322  continue;
1323  else
1324  format_found = 1;
1325 
1326  fn = build_filename(filename, type);
1327  if (!fn) {
1328  continue;
1329  }
1330  errno = 0;
1331  bfile = fopen(fn, "r");
1332 
1333  if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
1334  ast_log(LOG_WARNING, "Unable to open %s\n", fn);
1335  if (fs) {
1336  ast_closestream(fs);
1337  }
1338  fs = NULL;
1339  bfile = NULL;
1340  ast_free(fn);
1341  break;
1342  }
1343  /* found it */
1344  fs->trans = NULL;
1345  fs->fmt = f;
1346  fs->flags = flags;
1347  fs->mode = mode;
1348  fs->filename = ast_strdup(filename);
1349  fs->vfs = NULL;
1350  ast_free(fn);
1351  break;
1352  }
1353 
1355  if (!format_found)
1356  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1357 
1358  return fs;
1359 }
char exts[80]
Definition: mod_format.h:45
static const char type[]
Definition: chan_ooh323.c:109
#define exts_compare(list, type)
Definition: file.c:351
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static struct ast_filestream * get_filestream(struct ast_format_def *fmt, FILE *bfile)
Definition: file.c:428
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
#define NULL
Definition: resample.c:96
Each supported file format is described by the following structure.
Definition: mod_format.h:43
static int open_wrapper(struct ast_filestream *s)
Definition: file.c:490
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_log
Definition: astobj2.c:42
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:314
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format_def * fmt
Definition: mod_format.h:103
int errno
Definition: file.c:69
struct ast_trans_pvt * trans
Definition: mod_format.h:112
#define ast_free(a)
Definition: astmm.h:182
char * filename
Definition: mod_format.h:107
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

◆ ast_readframe()

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 899 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().

900 {
901  int whennext = 0;
902 
903  return read_frame(s, &whennext);
904 }
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:874

◆ ast_readvideo_callback()

static enum fsread_res ast_readvideo_callback ( struct ast_filestream s)
static

Definition at line 977 of file file.c.

References ast_channel_sched(), ast_channel_vstreamid_set(), ast_debug, ast_format_get_sample_rate(), ast_frfree, ast_sched_add(), ast_write(), ast_filestream::fmt, ast_format_def::format, FSREAD_FAILURE, FSREAD_SUCCESS_NOSCHED, FSREAD_SUCCESS_SCHED, ast_filestream::lasttimeout, ast_filestream::owner, and read_frame().

Referenced by ast_fsread_video(), and ast_playstream().

978 {
979  int whennext = 0;
980 
981  while (!whennext) {
982  struct ast_frame *fr = read_frame(s, &whennext);
983 
984  if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
985  if (fr) {
986  ast_debug(2, "Failed to write frame\n");
987  ast_frfree(fr);
988  }
990  return FSREAD_FAILURE;
991  }
992 
993  if (fr) {
994  ast_frfree(fr);
995  }
996  }
997 
998  if (whennext != s->lasttimeout) {
1000  s->lasttimeout = whennext;
1001  return FSREAD_SUCCESS_NOSCHED;
1002  }
1003 
1004  return FSREAD_SUCCESS_SCHED;
1005 }
static int ast_fsread_video(const void *data)
Definition: file.c:1007
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
struct ast_format_def * fmt
Definition: mod_format.h:103
struct ast_format * format
Definition: mod_format.h:48
struct ast_sched_context * ast_channel_sched(const struct ast_channel *chan)
struct ast_channel * owner
Definition: mod_format.h:116
static struct ast_frame * read_frame(struct ast_filestream *s, int *whennext)
Definition: file.c:874
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:5189
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:565
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
#define ast_frfree(fr)
Data structure associated with a single frame of data.
void ast_channel_vstreamid_set(struct ast_channel *chan, int value)

◆ ast_seekstream()

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 1038 of file file.c.

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

Referenced by __ast_read(), ast_moh_files_next(), ast_stream_fastforward(), ast_stream_rewind(), ast_streamfile(), ast_write_stream(), control_streamfile(), dictate_exec(), handle_getoption(), handle_recordfile(), handle_streamfile(), msg_create_from_file(), and speech_streamfile().

1039 {
1040  return fs->fmt->seek(fs, sample_offset, whence);
1041 }
int(* seek)(struct ast_filestream *, off_t, int)
Definition: mod_format.h:68
struct ast_format_def * fmt
Definition: mod_format.h:103

◆ ast_stopstream()

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 187 of file file.c.

References ast_channel_lock, ast_channel_oldwriteformat(), ast_channel_stream(), ast_channel_stream_set(), ast_channel_unlock, ast_channel_vstream(), ast_channel_vstream_set(), ast_closestream(), ast_format_get_name(), ast_log, ast_set_write_format(), LOG_WARNING, and NULL.

Referenced by action_playback_and_continue(), adsi_transmit_message_full(), agent_alert(), announce_to_dial(), 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_is(), 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_is(), ast_say_number_full_it(), ast_say_number_full_ja(), 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(), control_streamfile(), 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(), minivm_greet_exec(), mp3_exec(), NBScat_exec(), pbx_builtin_background(), pl_odtworz_plik(), play_file(), play_files_helper(), 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_money_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().

188 {
189  ast_channel_lock(tmp);
190 
191  /* Stop a running stream if there is one */
192  if (ast_channel_stream(tmp)) {
196  ast_log(LOG_WARNING, "Unable to restore format back to %s\n", ast_format_get_name(ast_channel_oldwriteformat(tmp)));
197  }
198  /* Stop the video stream too */
199  if (ast_channel_vstream(tmp) != NULL) {
202  }
203 
204  ast_channel_unlock(tmp);
205 
206  return 0;
207 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
struct ast_filestream * ast_channel_vstream(const struct ast_channel *chan)
#define LOG_WARNING
Definition: logger.h:274
struct ast_format * ast_channel_oldwriteformat(struct ast_channel *chan)
void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
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:1068
#define NULL
Definition: resample.c:96
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
#define ast_log
Definition: astobj2.c:42
int ast_set_write_format(struct ast_channel *chan, struct ast_format *format)
Sets write format on channel chan.
Definition: channel.c:5890
#define ast_channel_unlock(chan)
Definition: channel.h:2946
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)

◆ ast_stream_and_wait()

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 1814 of file file.c.

References ast_channel_language(), ast_streamfile(), ast_strlen_zero, and ast_waitstream().

Referenced by __ast_play_and_record(), action_playback(), action_toggle_mute_participants(), agent_alert(), agent_login_exec(), announce_user_count(), app_exec(), ast_bridge_channel_playfile(), ast_pickup_call(), ast_record_review(), bridge_features_duration_callback(), confbridge_exec(), directory_exec(), forward_message(), grab_transfer(), invent_message(), ivr_dispatch(), join_conference_bridge(), leave_voicemail(), limits_interval_playback(), mixmonitor_thread(), park_app_exec(), parked_call_app_exec(), play_file(), play_files_helper(), play_mailbox_owner(), play_message_callerid(), play_prompt_to_user(), play_record_review(), playback_common(), sayname(), select_item_seq(), setup_mixmonitor_ds(), stream_failsound(), vm_forwardoptions(), vmsayname_exec(), and wait_file2().

1815 {
1816  int res = 0;
1817  if (!ast_strlen_zero(file)) {
1818  res = ast_streamfile(chan, file, ast_channel_language(chan));
1819  if (!res) {
1820  res = ast_waitstream(chan, digits);
1821  }
1822  }
1823  return res;
1824 }
#define ast_strlen_zero(foo)
Definition: strings.h:52
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1776
int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:1250
const char * ast_channel_language(const struct ast_channel *chan)

◆ ast_stream_fastforward()

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 1058 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

Referenced by waitstream_control().

1059 {
1060  return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1061 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1038
#define DEFAULT_SAMPLES_PER_MS
Definition: asterisk.h:47

◆ ast_stream_rewind()

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 1063 of file file.c.

References ast_seekstream(), and DEFAULT_SAMPLES_PER_MS.

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

1064 {
1065  return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
1066 }
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1038
#define DEFAULT_SAMPLES_PER_MS
Definition: asterisk.h:47

◆ ast_streamfile()

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 1250 of file file.c.

References ast_applystream(), ast_channel_flags(), ast_channel_lock, ast_channel_name(), ast_channel_nativeformats(), ast_channel_unlock, ast_channel_writeformat(), ast_debug, AST_FLAG_MASQ_NOSTREAM, ast_format_cap_get_names(), AST_FORMAT_CAP_NAMES_LEN, ast_format_get_name(), ast_log, ast_openstream(), ast_openvstream(), ast_playstream(), ast_seekstream(), ast_str_alloca, ast_strdup, ast_test_flag, ast_test_suite_event_notify, ast_verb, errno, ast_filestream::f, ast_filestream::fmt, ast_format_def::format, LOG_WARNING, NULL, ast_filestream::orig_chan_name, VERBOSITY_ATLEAST, and ast_filestream::vfs.

Referenced by __analog_ss_thread(), action_playback_and_continue(), analog_ss_thread(), announce_thread(), announce_to_dial(), app_exec(), ast_app_getdata(), ast_app_getdata_full(), 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_is(), ast_say_date_ja(), 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_ja(), 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_is(), 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_is(), ast_say_number_full_it(), ast_say_number_full_ja(), 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_ja(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), auth_exec(), background_detect_exec(), common_exec(), conf_exec(), conf_get_pin(), conf_run(), control_streamfile(), dial_exec_full(), do_directory(), find_conf_realtime(), forward_message(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), meetme_menu_admin(), meetme_menu_admin_extended(), meetme_menu_normal(), minivm_greet_exec(), page_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_money_str_full(), say_phonetic_str_full(), select_item_menu(), setup_privacy_args(), vm_authenticate(), wait_file(), and wait_for_winner().

1251 {
1252  struct ast_filestream *fs;
1253  struct ast_filestream *vfs=NULL;
1254  off_t pos;
1255  int seekattempt;
1256  int res;
1257 
1258  fs = ast_openstream(chan, filename, preflang);
1259  if (!fs) {
1260  struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
1261  ast_channel_lock(chan);
1262  ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n",
1263  filename, ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf), strerror(errno));
1264  ast_channel_unlock(chan);
1265  return -1;
1266  }
1267 
1268  /* check to see if there is any data present (not a zero length file),
1269  * done this way because there is no where for ast_openstream_full to
1270  * return the file had no data. */
1271  pos = ftello(fs->f);
1272  seekattempt = fseeko(fs->f, -1, SEEK_END);
1273  if (seekattempt) {
1274  if (errno == EINVAL) {
1275  /* Zero-length file, as opposed to a pipe */
1276  return 0;
1277  } else {
1278  ast_seekstream(fs, 0, SEEK_SET);
1279  }
1280  } else {
1281  fseeko(fs->f, pos, SEEK_SET);
1282  }
1283 
1284  vfs = ast_openvstream(chan, filename, preflang);
1285  if (vfs) {
1286  ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_format_get_name(vfs->fmt->format));
1287  }
1288 
1291  if (ast_applystream(chan, fs))
1292  return -1;
1293  if (vfs && ast_applystream(chan, vfs))
1294  return -1;
1295  ast_test_suite_event_notify("PLAYBACK", "Message: %s\r\nChannel: %s", filename, ast_channel_name(chan));
1296  res = ast_playstream(fs);
1297  if (!res && vfs)
1298  res = ast_playstream(vfs);
1299 
1300  if (VERBOSITY_ATLEAST(3)) {
1301  ast_channel_lock(chan);
1302  ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", ast_channel_name(chan), filename, ast_format_get_name(ast_channel_writeformat(chan)), preflang ? preflang : "default");
1303  ast_channel_unlock(chan);
1304  }
1305 
1306  return res;
1307 }
#define ast_channel_lock(chan)
Definition: channel.h:2945
int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
Seeks into stream.
Definition: file.c:1038
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define LOG_WARNING
Definition: logger.h:274
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:326
#define VERBOSITY_ATLEAST(level)
Definition: logger.h:461
#define ast_str_alloca(init_len)
Definition: strings.h:800
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:243
int ast_playstream(struct ast_filestream *s)
Play a open stream on a channel.
Definition: file.c:1026
#define NULL
Definition: resample.c:96
#define ast_verb(level,...)
Definition: logger.h:463
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:755
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
struct ast_format_def * fmt
Definition: mod_format.h:103
const char * orig_chan_name
Definition: mod_format.h:125
struct ast_format * format
Definition: mod_format.h:48
int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
Applys a open stream to a channel.
Definition: file.c:1020
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:196
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:812
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
int errno
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:736
#define ast_channel_unlock(chan)
Definition: channel.h:2946
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
char * filename
Definition: mod_format.h:107
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
const char * ast_channel_name(const struct ast_channel *chan)
struct ast_filestream * vfs
Definition: mod_format.h:110
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)

◆ ast_tellstream()

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 1048 of file file.c.

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

Referenced by __ast_play_and_record(), ast_moh_files_next(), control_streamfile(), handle_getoption(), handle_recordfile(), handle_speechrecognize(), handle_streamfile(), msg_create_from_file(), waitstream_control(), and waitstream_core().

1049 {
1050  return fs->fmt->tell(fs);
1051 }
struct ast_format_def * fmt
Definition: mod_format.h:103
off_t(* tell)(struct ast_filestream *fs)
Definition: mod_format.h:70

◆ ast_truncstream()

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 1043 of file file.c.

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

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

1044 {
1045  return fs->fmt->trunc(fs);
1046 }
struct ast_format_def * fmt
Definition: mod_format.h:103
int(* trunc)(struct ast_filestream *fs)
Definition: mod_format.h:69

◆ ast_waitstream()

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 by the channel.
-1on error

Definition at line 1776 of file file.c.

References NULL, sanitize_waitstream_return(), and waitstream_core().

Referenced by __analog_ss_thread(), action_playback_and_continue(), analog_ss_thread(), announce_thread(), announce_to_dial(), app_exec(), 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_is(), ast_say_date_ja(), 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_ja(), 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_is(), 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_is(), ast_say_number_full_it(), ast_say_number_full_ja(), 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_ja(), ast_say_time_ka(), ast_say_time_nl(), ast_say_time_zh(), ast_stream_and_wait(), auth_exec(), common_exec(), conf_exec(), conf_get_pin(), conf_run(), directory_exec(), find_conf_realtime(), gr_say_number_female(), handle_recordfile(), invent_message(), leave_voicemail(), meetme_menu_admin(), meetme_menu_admin_extended(), meetme_menu_normal(), minivm_greet_exec(), page_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_money_str_full(), say_phonetic_str_full(), select_item_menu(), setup_privacy_args(), vm_authenticate(), and wait_file().

1777 {
1778  int res;
1779 
1780  res = waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL, NULL /* no callback */);
1781 
1782  return sanitize_waitstream_return(res);
1783 }
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:1551
#define NULL
Definition: resample.c:96
static int sanitize_waitstream_return(int return_value)
Definition: file.c:1759

◆ ast_waitstream_exten()

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 1795 of file file.c.

References ast_channel_context(), NULL, sanitize_waitstream_return(), and waitstream_core().

Referenced by pbx_builtin_background().

1796 {
1797  int res;
1798 
1799  /* Waitstream, with return in the case of a valid 1 digit extension */
1800  /* in the current or specified context being pressed */
1801  if (!context)
1803  res = waitstream_core(c, NULL, NULL, NULL, 0,
1804  -1, -1, context, NULL /* no callback */);
1805 
1806  return sanitize_waitstream_return(res);
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:1551
#define NULL
Definition: resample.c:96
static int sanitize_waitstream_return(int return_value)
Definition: file.c:1759
const char * ast_channel_context(const struct ast_channel *chan)
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ ast_waitstream_fr()

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,
thevalue of the control frame if it was interrupted by some other party,
-1on error.

Definition at line 1745 of file file.c.

References NULL, and waitstream_core().

Referenced by control_streamfile().

1746 {
1747  return waitstream_core(c, breakon, forward, reverse, ms,
1748  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, NULL /* no callback */);
1749 }
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:1551
#define NULL
Definition: resample.c:96

◆ ast_waitstream_fr_w_cb()

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 fastfoward 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.
thecharacter if it was interrupted,
thevalue of the control frame if it was interrupted by some other party,
-1on error.

Definition at line 1734 of file file.c.

References NULL, and waitstream_core().

Referenced by control_streamfile().

1740 {
1741  return waitstream_core(c, breakon, forward, reverse, ms,
1742  -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */, cb);
1743 }
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:1551
#define NULL
Definition: resample.c:96

◆ ast_waitstream_full()

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.

Returns
1 if monfd is ready for reading

Definition at line 1785 of file file.c.

References NULL, sanitize_waitstream_return(), and waitstream_core().

Referenced by 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_is(), 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_is(), ast_say_number_full_it(), ast_say_number_full_ja(), 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(), handle_getoption(), handle_streamfile(), pl_odtworz_plik(), s_streamwait3(), say_character_str_full(), say_digit_str_full(), say_money_str_full(), and say_phonetic_str_full().

1786 {
1787  int res;
1788 
1789  res = waitstream_core(c, breakon, NULL, NULL, 0,
1790  audiofd, cmdfd, NULL /* no context */, NULL /* no callback */);
1791 
1792  return sanitize_waitstream_return(res);
1793 }
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:1551
#define NULL
Definition: resample.c:96
static int sanitize_waitstream_return(int return_value)
Definition: file.c:1759

◆ ast_writefile()

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 1361 of file file.c.

References ast_closestream(), ast_free, ast_log, ast_malloc, ast_opt_cache_record_files, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, ast_strdup, buf, build_filename(), c, errno, ast_format_def::exts, exts_compare, ast_filestream::f, ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, get_filestream(), LOG_WARNING, ast_filestream::mode, NULL, ast_filestream::realfilename, record_cache_dir, rewrite_wrapper(), ast_format_def::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_save_prep(), record_exec(), and recordthread().

1362 {
1363  int fd, myflags = 0;
1364  /* compiler claims this variable can be used before initialization... */
1365  FILE *bfile = NULL;
1366  struct ast_format_def *f;
1367  struct ast_filestream *fs = NULL;
1368  char *buf = NULL;
1369  size_t size = 0;
1370  int format_found = 0;
1371 
1373 
1374  /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1375  /* We really can't use O_APPEND as it will break WAV header updates */
1376  if (flags & O_APPEND) {
1377  flags &= ~O_APPEND;
1378  } else {
1379  myflags = O_TRUNC;
1380  }
1381 
1382  myflags |= O_WRONLY | O_CREAT;
1383 
1384  /* XXX need to fix this - we should just do the fopen,
1385  * not open followed by fdopen()
1386  */
1387  AST_RWLIST_TRAVERSE(&formats, f, list) {
1388  char *fn, *orig_fn = NULL;
1389  if (fs)
1390  break;
1391 
1392  if (!exts_compare(f->exts, type))
1393  continue;
1394  else
1395  format_found = 1;
1396 
1397  fn = build_filename(filename, type);
1398  if (!fn) {
1399  continue;
1400  }
1401  fd = open(fn, flags | myflags, mode);
1402  if (fd > -1) {
1403  /* fdopen() the resulting file stream */
1404  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1405  if (!bfile) {
1406  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1407  close(fd);
1408  fd = -1;
1409  }
1410  }
1411 
1412  if (ast_opt_cache_record_files && (fd > -1)) {
1413  char *c;
1414 
1415  fclose(bfile); /* this also closes fd */
1416  /*
1417  We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1418  What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1419  */
1420  orig_fn = ast_strdup(fn);
1421  for (c = fn; *c; c++)
1422  if (*c == '/')
1423  *c = '_';
1424 
1425  size = strlen(fn) + strlen(record_cache_dir) + 2;
1426  buf = ast_malloc(size);
1427  strcpy(buf, record_cache_dir);
1428  strcat(buf, "/");
1429  strcat(buf, fn);
1430  ast_free(fn);
1431  fn = buf;
1432  fd = open(fn, flags | myflags, mode);
1433  if (fd > -1) {
1434  /* fdopen() the resulting file stream */
1435  bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1436  if (!bfile) {
1437  ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1438  close(fd);
1439  fd = -1;
1440  }
1441  }
1442  }
1443  if (fd > -1) {
1444  errno = 0;
1445  fs = get_filestream(f, bfile);
1446  if (fs) {
1447  if ((fs->write_buffer = ast_malloc(32768))) {
1448  setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1449  }
1450  }
1451  if (!fs || rewrite_wrapper(fs, comment)) {
1452  ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1453  close(fd);
1454  if (orig_fn) {
1455  unlink(fn);
1456  unlink(orig_fn);
1457  ast_free(orig_fn);
1458  }
1459  if (fs) {
1460  ast_closestream(fs);
1461  fs = NULL;
1462  }
1463  /*
1464  * 'fn' was has either been allocated from build_filename, or that was freed
1465  * and now 'fn' points to memory allocated for 'buf'. Either way the memory
1466  * now needs to be released.
1467  */
1468  ast_free(fn);
1469  continue;
1470  }
1471  fs->trans = NULL;
1472  fs->fmt = f;
1473  fs->flags = flags;
1474  fs->mode = mode;
1475  if (orig_fn) {
1476  fs->realfilename = orig_fn;
1477  fs->filename = fn;
1478  /*
1479  * The above now manages the memory allocated for 'orig_fn' and 'fn', so
1480  * set them to NULL, so they don't get released at the end of the loop.
1481  */
1482  orig_fn = NULL;
1483  fn = NULL;
1484  } else {
1485  fs->realfilename = NULL;
1486  fs->filename = ast_strdup(filename);
1487  }
1488  fs->vfs = NULL;
1489  /* If truncated, we'll be at the beginning; if not truncated, then append */
1490  f->seek(fs, 0, SEEK_END);
1491  } else if (errno != EEXIST) {
1492  ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1493  if (orig_fn)
1494  unlink(orig_fn);
1495  }
1496  /* Free 'fn', or if 'fn' points to 'buf' then free 'buf' */
1497  ast_free(fn);
1498  ast_free(orig_fn);
1499  }
1500 
1502 
1503  if (!format_found)
1504  ast_log(LOG_WARNING, "No such format '%s'\n", type);
1505 
1506  return fs;
1507 }
char exts[80]
Definition: mod_format.h:45
static const char type[]
Definition: chan_ooh323.c:109
#define exts_compare(list, type)
Definition: file.c:351
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static struct ast_filestream * get_filestream(struct ast_format_def *fmt, FILE *bfile)
Definition: file.c:428
static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
Definition: file.c:485
char * realfilename
Definition: mod_format.h:108
static struct test_val c
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
#define NULL
Definition: resample.c:96
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
char * write_buffer
Definition: mod_format.h:126
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_log
Definition: astobj2.c:42
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:314
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format_def * fmt
Definition: mod_format.h:103
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
int errno
#define comment
Definition: ael_lex.c:976
Definition: file.c:69
struct ast_trans_pvt * trans
Definition: mod_format.h:112
#define ast_opt_cache_record_files
Definition: options.h:120
#define ast_free(a)
Definition: astmm.h:182
char record_cache_dir[AST_CACHE_DIR_LEN]
Definition: options.c:96
char * filename
Definition: mod_format.h:107
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

◆ ast_writestream()

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 209 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_frfree, AST_LIST_NEXT, ast_log, AST_MEDIA_TYPE_AUDIO, ast_translate(), ast_translator_build_path(), ast_translator_free_path(), ast_writefile(), ast_filestream::filename, ast_filestream::flags, ast_filestream::fmt, ast_format_def::format, ast_frame_subclass::format, ast_frame::frametype, ast_filestream::lastwriteformat, LOG_WARNING, ast_filestream::mode, ast_format_def::name, NULL, ast_frame::subclass, ast_filestream::trans, type, ast_filestream::vfs, and ast_format_def::write.

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

210 {
211  int res = -1;
212  if (f->frametype == AST_FRAME_VIDEO) {
214  /* This is the audio portion. Call the video one... */
215  if (!fs->vfs && fs->filename) {
216  const char *type = ast_format_get_name(f->subclass.format);
217  fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
218  ast_debug(1, "Opened video output file\n");
219  }
220  if (fs->vfs)
221  return ast_writestream(fs->vfs, f);
222  /* else ignore */
223  return 0;
224  }
225  } else if (f->frametype != AST_FRAME_VOICE) {
226  ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
227  return -1;
228  }
230  res = fs->fmt->write(fs, f);
231  if (res < 0)
232  ast_log(LOG_WARNING, "Natural write failed\n");
233  else if (res > 0)
234  ast_log(LOG_WARNING, "Huh??\n");
235  } else {
236  /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
237  the one we've setup a translator for, we do the "wrong thing" XXX */
240  fs->trans = NULL;
241  }
242  if (!fs->trans) {
244  }
245  if (!fs->trans) {
246  ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
248  } else {
249  struct ast_frame *trf;
251  /* Get the translated frame but don't consume the original in case they're using it on another stream */
252  if ((trf = ast_translate(fs->trans, f, 0))) {
253  struct ast_frame *cur;
254 
255  /* the translator may have returned multiple frames, so process them */
256  for (cur = trf; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
257  if ((res = fs->fmt->write(fs, trf))) {
258  ast_log(LOG_WARNING, "Translated frame write failed\n");
259  break;
260  }
261  }
262  ast_frfree(trf);
263  } else {
264  res = 0;
265  }
266  }
267  }
268  return res;
269 }
static const char type[]
Definition: chan_ooh323.c:109
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 LOG_WARNING
Definition: logger.h:274
#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:565
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
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:209
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:1361
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
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:485
struct ast_format_def * fmt
Definition: mod_format.h:103
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
struct ast_format * lastwriteformat
Definition: mod_format.h:114
char * filename
Definition: mod_format.h:107
#define ao2_replace(dst, src)
Definition: astobj2.h:517
#define ast_frfree(fr)
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:475

◆ build_filename()

static char* build_filename ( const char *  filename,
const char *  ext 
)
static

construct a filename. Absolute pathnames are preserved, relative names are prefixed by the sounds/ directory. The wav49 suffix is replaced by 'WAV'. Returns a malloc'ed string to be freed by the caller.

Definition at line 314 of file file.c.

References ast_asprintf, ast_config_AST_DATA_DIR, and NULL.

Referenced by ast_readfile(), ast_writefile(), and filehelper().

315 {
316  char *fn = NULL;
317 
318  /* The wav49 -> WAV translation is duplicated in apps/app_mixmonitor.c, so
319  if you change it here you need to change it there as well */
320  if (!strcmp(ext, "wav49"))
321  ext = "WAV";
322 
323  if (filename[0] == '/') {
324  if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
325  fn = NULL;
326  }
327  } else {
328  if (ast_asprintf(&fn, "%s/sounds/%s.%s",
329  ast_config_AST_DATA_DIR, filename, ext) < 0) {
330  fn = NULL;
331  }
332  }
333  return fn;
334 }
#define NULL
Definition: resample.c:96
const char * ext
Definition: http.c:147
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
const char * ast_config_AST_DATA_DIR
Definition: options.c:158

◆ copy()

static int copy ( const char *  infile,
const char *  outfile 
)
static

Definition at line 271 of file file.c.

References AST_FILE_MODE, ast_log, buf, errno, len(), and LOG_WARNING.

Referenced by filehelper().

272 {
273  int ifd, ofd, len;
274  char buf[4096]; /* XXX make it lerger. */
275 
276  if ((ifd = open(infile, O_RDONLY)) < 0) {
277  ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
278  return -1;
279  }
280  if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
281  ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
282  close(ifd);
283  return -1;
284  }
285  while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
286  int res;
287  if (len < 0) {
288  ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
289  break;
290  }
291  /* XXX handle partial writes */
292  res = write(ofd, buf, len);
293  if (res != len) {
294  ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
295  len = -1; /* error marker */
296  break;
297  }
298  }
299  close(ifd);
300  close(ofd);
301  if (len < 0) {
302  unlink(outfile);
303  return -1; /* error */
304  }
305  return 0; /* success */
306 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
#define AST_FILE_MODE
Definition: asterisk.h:32
#define ast_log
Definition: astobj2.c:42
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno

◆ file_shutdown()

static void file_shutdown ( void  )
static

Definition at line 1976 of file file.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_format_register_type(), ast_format_unregister_type(), and STASIS_MESSAGE_TYPE_CLEANUP.

Referenced by ast_file_init().

1977 {
1981 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ast_cli_entry cli_file[]
Definition: file.c:1972
#define STASIS_MESSAGE_TYPE_CLEANUP(name)
Boiler-plate messaging macro for cleaning up message types.
Definition: stasis.h:1523
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.

◆ fileexists_core()

static int fileexists_core ( const char *  filename,
const char *  fmt,
const char *  preflang,
char *  buf,
int  buflen,
struct ast_format_cap result_cap 
)
static

helper routine to locate a file with a given format and language preference.

Note
Try preflang, preflang with stripped '_' suffices, or NULL.
The last parameter(s) point to a buffer of sufficient size, which on success is filled with the matching filename.
Parameters
filenameName of the file.
fmtFormat to look for the file in. OPTIONAL
preflangThe perfered language
bufReturns the matching filename
buflenSize of the buf
result_capOPTIONAL format capabilities result structure returns what formats the file was found in.
Return values
1,true.file exists and result format is set
0,false.file does not exist.

Definition at line 706 of file file.c.

References ast_strdupa, ast_strlen_zero, DEFAULT_LANGUAGE, end, fileexists_test(), and NULL.

Referenced by ast_fileexists(), ast_openstream_full(), and ast_openvstream().

708 {
709  char *lang;
710 
711  if (buf == NULL) {
712  return 0;
713  }
714 
715  /* We try languages in the following order:
716  * preflang (may include dialect and style codes)
717  * lang (preflang without dialect - if any)
718  * <none>
719  * default (unless the same as preflang or lang without dialect)
720  */
721 
722  lang = ast_strdupa(preflang);
723 
724  /* Try preferred language, including removing any style or dialect codes */
725  while (!ast_strlen_zero(lang)) {
726  char *end;
727 
728  if (fileexists_test(filename, fmt, lang, buf, buflen, result_cap)) {
729  return 1;
730  }
731 
732  if ((end = strrchr(lang, '_')) != NULL) {
733  *end = '\0';
734  continue;
735  }
736 
737  break;
738  }
739 
740  /* Try without any language */
741  if (fileexists_test(filename, fmt, NULL, buf, buflen, result_cap)) {
742  return 1;
743  }
744 
745  /* Finally try the default language unless it was already tried before */
746  if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
747  if ((fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen, result_cap)) > 0) {
748  return 1;
749  }
750  }
751 
752  return 0;
753 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static int fileexists_test(const char *filename, const char *fmt, const char *lang, char *buf, int buflen, struct ast_format_cap *result_cap)
test if a file exists for a given format.
Definition: file.c:656
#define DEFAULT_LANGUAGE
Definition: asterisk.h:44

◆ fileexists_test()

static int fileexists_test ( const char *  filename,
const char *  fmt,
const char *  lang,
char *  buf,
int  buflen,
struct ast_format_cap result_cap 
)
static

test if a file exists for a given format.

Note
result_cap is OPTIONAL
Return values
1,trueand result_cap represents format capabilities file exists in.
0,false

Definition at line 656 of file file.c.

References ACTION_EXISTS, ast_language_is_prefix, ast_media_cache_retrieve(), c, filehelper(), is_absolute_path(), is_remote_path(), and NULL.

Referenced by fileexists_core().

658 {
659  if (buf == NULL) {
660  return 0;
661  }
662 
663  if (is_remote_path(filename) && !ast_media_cache_retrieve(filename, NULL, buf, buflen)) {
664  return filehelper(buf, result_cap, NULL, ACTION_EXISTS);
665  }
666 
667  if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
668  if (lang) {
669  snprintf(buf, buflen, "%s/%s", lang, filename);
670  } else {
671  snprintf(buf, buflen, "%s", filename);
672  }
673  } else { /* old layout */
674  strcpy(buf, filename); /* first copy the full string */
675  if (lang) {
676  /* insert the language and suffix if needed */
677  const char *c = strrchr(filename, '/');
678  int offset = c ? c - filename + 1 : 0; /* points right after the last '/' */
679  snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
680  }
681  }
682 
683  return filehelper(buf, result_cap, fmt, ACTION_EXISTS);
684 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static struct test_val c
#define NULL
Definition: resample.c:96
static int is_absolute_path(const char *filename)
Definition: file.c:640
static int filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
Definition: file.c:514
int ast_media_cache_retrieve(const char *uri, const char *preferred_file_name, char *file_path, size_t len)
Retrieve an item from the cache.
Definition: media_cache.c:197
static int is_remote_path(const char *filename)
Definition: file.c:645
int ast_language_is_prefix
The following variable controls the layout of localized sound files. If 0, use the historical layout ...
Definition: file.c:67

◆ filehelper()

static int filehelper ( const char *  filename,
const void *  arg2,
const char *  fmt,
const enum file_action  action 
)
static

Definition at line 514 of file file.c.

References ACTION_COPY, ACTION_DELETE, ACTION_EXISTS, ACTION_OPEN, ACTION_RENAME, ast_channel_stream(), ast_channel_stream_set(), ast_channel_vstream(), ast_channel_vstream_set(), ast_channel_writeformat(), ast_closestream(), ast_format_cap_append, ast_format_cmp(), AST_FORMAT_CMP_NOT_EQUAL, ast_format_get_type(), ast_free, ast_log, AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_VIDEO, AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, build_filename(), copy(), errno, ext, ast_format_def::exts, exts_compare, ast_filestream::f, ast_filestream::filename, ast_filestream::fmt, ast_format_def::format, get_filestream(), ast_filestream::lasttimeout, LOG_WARNING, NULL, open_wrapper(), strsep(), and ast_filestream::trans.

Referenced by ast_filecopy(), ast_filedelete(), ast_filerename(), ast_openstream_full(), ast_openvstream(), and fileexists_test().

515 {
516  struct ast_format_def *f;
517  int res = (action == ACTION_EXISTS) ? 0 : -1;
518 
520  /* Check for a specific format */
522  char *ext = NULL;
523  char storage[strlen(f->exts) + 1];
524  char *stringp;
525 
526  if (fmt && !exts_compare(f->exts, fmt))
527  continue;
528 
529  /* Look for a file matching the supported extensions.
530  * The file must exist, and for OPEN, must match
531  * one of the formats supported by the channel.
532  */
533  strcpy(storage, f->exts); /* safe - this is in the stack so does not need to be freed */
534  stringp = storage;
535  while ( (ext = strsep(&stringp, "|")) ) {
536  struct stat st;
537  char *fn = build_filename(filename, ext);
538 
539  if (fn == NULL)
540  continue;
541 
542  if ( stat(fn, &st) ) { /* file not existent */
543  ast_free(fn);
544  continue;
545  }
546  /* for 'OPEN' we need to be sure that the format matches
547  * what the channel can process
548  */
549  if (action == ACTION_OPEN) {
550  struct ast_channel *chan = (struct ast_channel *)arg2;
551  FILE *bfile;
552  struct ast_filestream *s;
553 
557  ast_free(fn);
558  continue; /* not a supported format */
559  }
560  if ( (bfile = fopen(fn, "r")) == NULL) {
561  ast_free(fn);
562  continue; /* cannot open file */
563  }
564  s = get_filestream(f, bfile);
565  if (!s) {
566  fclose(bfile);
567  ast_free(fn); /* cannot allocate descriptor */
568  continue;
569  }
570  if (open_wrapper(s)) {
571  ast_free(fn);
572  ast_closestream(s);
573  continue; /* cannot run open on file */
574  }
575  if (st.st_size == 0) {
576  ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
577  }
578  /* ok this is good for OPEN */
579  res = 1; /* found */
580  s->lasttimeout = -1;
581  s->fmt = f;
582  s->trans = NULL;
583  s->filename = NULL;
585  if (ast_channel_stream(chan))
587  ast_channel_stream_set(chan, s);
588  } else {
589  if (ast_channel_vstream(chan))
591  ast_channel_vstream_set(chan, s);
592  }
593  ast_free(fn);
594  break;
595  }
596  switch (action) {
597  case ACTION_OPEN:
598  break; /* will never get here */
599 
600  case ACTION_EXISTS: /* return the matching format */
601  /* if arg2 is present, it is a format capabilities structure.
602  * Add this format to the set of formats this file can be played in */
603  if (arg2) {
604  ast_format_cap_append((struct ast_format_cap *) arg2, f->format, 0);
605  }
606  res = 1; /* file does exist and format it exists in is returned in arg2 */
607  break;
608 
609  case ACTION_DELETE:
610  if ( (res = unlink(fn)) )
611  ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
612  break;
613 
614  case ACTION_RENAME:
615  case ACTION_COPY: {
616  char *nfn = build_filename((const char *)arg2, ext);
617  if (!nfn)
618  ast_log(LOG_WARNING, "Out of memory\n");
619  else {
620  res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
621  if (res)
622  ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
623  action == ACTION_COPY ? "copy" : "rename",
624  fn, nfn, strerror(errno));
625  ast_free(nfn);
626  }
627  }
628  break;
629 
630  default:
631  ast_log(LOG_WARNING, "Unknown helper %u\n", action);
632  }
633  ast_free(fn);
634  }
635  }
637  return res;
638 }
char exts[80]
Definition: mod_format.h:45
Main Channel structure associated with a channel.
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_filestream * ast_channel_vstream(const struct ast_channel *chan)
#define exts_compare(list, type)
Definition: file.c:351
#define LOG_WARNING
Definition: logger.h:274
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static struct ast_filestream * get_filestream(struct ast_format_def *fmt, FILE *bfile)
Definition: file.c:428
void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
#define NULL
Definition: resample.c:96
Each supported file format is described by the following structure.
Definition: mod_format.h:43
const char * ext
Definition: http.c:147
static int open_wrapper(struct ast_filestream *s)
Definition: file.c:490
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
struct ast_format_def::@276 list
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
#define ast_log
Definition: astobj2.c:42
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:314
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
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format_def * fmt
Definition: mod_format.h:103
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
struct ast_format * format
Definition: mod_format.h:48
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
int errno
Definition: file.c:69
struct ast_trans_pvt * trans
Definition: mod_format.h:112
#define ast_free(a)
Definition: astmm.h:182
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
char * strsep(char **str, const char *delims)
char * filename
Definition: mod_format.h:107
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
static int copy(const char *infile, const char *outfile)
Definition: file.c:271
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)

◆ filestream_close()

static void filestream_close ( struct ast_filestream f)
static

Definition at line 357 of file file.c.

References ast_channel_sched(), ast_channel_stream_set(), ast_channel_streamid(), ast_channel_streamid_set(), ast_channel_vstream_set(), ast_channel_vstreamid(), ast_channel_vstreamid_set(), ast_format_get_type(), ast_log, AST_LOG_WARNING, AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_VIDEO, AST_SCHED_DEL_ACCESSOR, ast_settimeout(), ast_filestream::fmt, ast_format_def::format, ast_format_def::name, NULL, and ast_filestream::owner.

Referenced by ast_closestream(), and filestream_destructor().

358 {
359  enum ast_media_type format_type = ast_format_get_type(f->fmt->format);
360 
361  if (!f->owner) {
362  return;
363  }
364 
365  /* Stop a running stream if there is one */
366  switch (format_type)
367  {
371  ast_settimeout(f->owner, 0, NULL, NULL);
372  break;
376  break;
377  default:
378  ast_log(AST_LOG_WARNING, "Unable to schedule deletion of filestream with unsupported type %s\n", f->fmt->name);
379  break;
380  }
381 }
int ast_channel_vstreamid(const struct ast_channel *chan)
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_LOG_WARNING
Definition: logger.h:279
void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
#define NULL
Definition: resample.c:96
void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
#define ast_log
Definition: astobj2.c:42
int ast_settimeout(struct ast_channel *c, unsigned int rate, int(*func)(const void *data), void *data)
Enable or disable timer ticks for a channel.
Definition: channel.c:3189
struct ast_format_def * fmt
Definition: mod_format.h:103
#define AST_SCHED_DEL_ACCESSOR(sched, obj, getter, setter)
Definition: sched.h:59
struct ast_format * format
Definition: mod_format.h:48
struct ast_sched_context * ast_channel_sched(const struct ast_channel *chan)
int ast_channel_streamid(const struct ast_channel *chan)
struct ast_channel * owner
Definition: mod_format.h:116
void ast_channel_streamid_set(struct ast_channel *chan, int value)
char name[80]
Definition: mod_format.h:44
ast_media_type
Types of media.
Definition: codec.h:30
void ast_channel_vstreamid_set(struct ast_channel *chan, int value)

◆ filestream_destructor()

static void filestream_destructor ( void *  arg)
static

Definition at line 383 of file file.c.

References ao2_cleanup, ast_closestream(), ast_free, ast_module_unref, ast_safe_fork(), ast_translator_free_path(), ast_format_def::close, ast_filestream::f, ast_filestream::filename, filestream_close(), ast_filestream::fmt, ast_frame_subclass::format, ast_filestream::fr, ast_filestream::lastwriteformat, ast_format_def::module, ast_filestream::orig_chan_name, ast_filestream::realfilename, SENTINEL, status, ast_frame::subclass, ast_filestream::trans, ast_filestream::vfs, and ast_filestream::write_buffer.

Referenced by get_filestream().

384 {
385  struct ast_filestream *f = arg;
386  int status;
387  int pid = -1;
388 
389  /* Stop a running stream if there is one */
390  filestream_close(f);
391 
392  /* destroy the translator on exit */
393  if (f->trans)
395 
396  if (f->fmt->close) {
397  void (*closefn)(struct ast_filestream *) = f->fmt->close;
398  closefn(f);
399  }
400 
401  if (f->f) {
402  fclose(f->f);
403  }
404 
405  if (f->realfilename && f->filename) {
406  pid = ast_safe_fork(0);
407  if (!pid) {
408  execl("/bin/mv", "mv", "-f", f->filename, f->realfilename, SENTINEL);
409  _exit(1);
410  }
411  else if (pid > 0) {
412  /* Block the parent until the move is complete.*/
413  waitpid(pid, &status, 0);
414  }
415  }
416 
417  ast_free(f->filename);
419  if (f->vfs)
420  ast_closestream(f->vfs);
422  ast_free((void *)f->orig_chan_name);
426 }
void(* close)(struct ast_filestream *)
Definition: mod_format.h:77
static void filestream_close(struct ast_filestream *f)
Definition: file.c:357
char * realfilename
Definition: mod_format.h:108
int ast_closestream(struct ast_filestream *f)
Closes a stream.
Definition: file.c:1068
struct ast_frame_subclass subclass
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
char * write_buffer
Definition: mod_format.h:126
struct ast_module * module
Definition: mod_format.h:93
#define SENTINEL
Definition: compiler.h:87
struct ast_format_def * fmt
Definition: mod_format.h:103
const char * orig_chan_name
Definition: mod_format.h:125
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
int ast_safe_fork(int stop_reaper)
Common routine to safely fork without a chance of a signal handler firing badly in the child...
Definition: main/app.c:3047
struct ast_trans_pvt * trans
Definition: mod_format.h:112
#define ast_free(a)
Definition: astmm.h:182
struct ast_format * lastwriteformat
Definition: mod_format.h:114
char * filename
Definition: mod_format.h:107
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 ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_filestream * vfs
Definition: mod_format.h:110
struct ast_format * format
void ast_translator_free_path(struct ast_trans_pvt *tr)
Frees a translator path Frees the given translator path structure.
Definition: translate.c:475
jack_status_t status
Definition: app_jack.c:146

◆ fn_wrapper()

static int fn_wrapper ( struct ast_filestream s,
const char *  comment,
enum wrap_fn  mode 
)
static

Definition at line 468 of file file.c.

References ast_log, ast_filestream::fmt, LOG_WARNING, ast_format_def::name, ast_format_def::open, ast_format_def::rewrite, and WRAP_OPEN.

Referenced by open_wrapper(), and rewrite_wrapper().

469 {
470  struct ast_format_def *f = s->fmt;
471  int ret = -1;
472  int (*openfn)(struct ast_filestream *s);
473 
474  if (mode == WRAP_OPEN && (openfn = f->open) && openfn(s))
475  ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
476  else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
477  ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
478  else {
479  /* preliminary checks succeed. */
480  ret = 0;
481  }
482  return ret;
483 }
#define LOG_WARNING
Definition: logger.h:274
Each supported file format is described by the following structure.
Definition: mod_format.h:43
int(* open)(struct ast_filestream *s)
Prepare an input stream for playback.
Definition: mod_format.h:56
#define ast_log
Definition: astobj2.c:42
struct ast_format_def * fmt
Definition: mod_format.h:103
int(* rewrite)(struct ast_filestream *s, const char *comment)
Prepare a stream for output, and comment it appropriately if applicable.
Definition: mod_format.h:64
#define comment
Definition: ael_lex.c:976
char name[80]
Definition: mod_format.h:44
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

◆ get_filestream()

static struct ast_filestream* get_filestream ( struct ast_format_def fmt,
FILE *  bfile 
)
static

Definition at line 428 of file file.c.

References ast_filestream::_private, ao2_alloc, ao2_bump, ast_format_get_type(), AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_VIDEO, ast_module_running_ref, ast_module_unref, ast_filestream::buf, ast_format_def::buf_size, ast_format_def::desc_size, ast_filestream::f, filestream_destructor(), ast_filestream::fmt, ast_format_def::format, ast_frame_subclass::format, ast_filestream::fr, ast_frame::frametype, if(), ast_frame::mallocd, ast_format_def::module, ast_format_def::name, NULL, ast_frame::src, and ast_frame::subclass.

Referenced by ast_readfile(), ast_writefile(), and filehelper().

429 {
430  struct ast_filestream *s;
431  int l = sizeof(*s) + fmt->buf_size + fmt->desc_size; /* total allocation size */
432 
433  if (!ast_module_running_ref(fmt->module)) {
434  return NULL;
435  }
436 
438  if (!s) {
439  ast_module_unref(fmt->module);
440  return NULL;
441  }
442  s->fmt = fmt;
443  s->f = bfile;
444 
445  if (fmt->desc_size)
446  s->_private = ((char *)(s + 1)) + fmt->buf_size;
447  if (fmt->buf_size)
448  s->buf = (char *)(s + 1);
449  s->fr.src = fmt->name;
450 
453  } else if (ast_format_get_type(fmt->format) == AST_MEDIA_TYPE_VIDEO) {
455  }
456  s->fr.mallocd = 0;
457  s->fr.subclass.format = ao2_bump(fmt->format);
458 
459  return s;
460 }
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
#define NULL
Definition: resample.c:96
struct ast_frame_subclass subclass
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
#define ao2_bump(obj)
Definition: astobj2.h:491
struct ast_module * module
Definition: mod_format.h:93
const char * src
struct ast_format_def * fmt
Definition: mod_format.h:103
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
struct ast_format * format
Definition: mod_format.h:48
void * _private
Definition: mod_format.h:124
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
char name[80]
Definition: mod_format.h:44
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:455
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
enum ast_frame_type frametype
struct ast_format * format
static void filestream_destructor(void *arg)
Definition: file.c:383

◆ handle_cli_core_show_file_formats()

static char* handle_cli_core_show_file_formats ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 1902 of file file.c.

References ast_cli_args::argc, ast_cli(), ast_format_get_name(), AST_RWLIST_RDLOCK, AST_RWLIST_TRAVERSE, AST_RWLIST_UNLOCK, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_format_def::exts, ast_cli_args::fd, ast_format_def::format, FORMAT, FORMAT2, ast_format_def::name, NULL, and ast_cli_entry::usage.

1903 {
1904 #define FORMAT "%-10s %-10s %-20s\n"
1905 #define FORMAT2 "%-10s %-10s %-20s\n"
1906  struct ast_format_def *f;
1907  int count_fmt = 0;
1908 
1909  switch (cmd) {
1910  case CLI_INIT:
1911  e->command = "core show file formats";
1912  e->usage =
1913  "Usage: core show file formats\n"
1914  " Displays currently registered file formats (if any).\n";
1915  return NULL;
1916  case CLI_GENERATE:
1917  return NULL;
1918  }
1919 
1920  if (a->argc != 4)
1921  return CLI_SHOWUSAGE;
1922 
1923  ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1924  ast_cli(a->fd, FORMAT, "------", "----", "----------");
1925 
1928  ast_cli(a->fd, FORMAT2, ast_format_get_name(f->format), f->name, f->exts);
1929  count_fmt++;
1930  }
1932  ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1933  return CLI_SUCCESS;
1934 #undef FORMAT
1935 #undef FORMAT2
1936 }
char exts[80]
Definition: mod_format.h:45
const int argc
Definition: cli.h:160
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
Definition: cli.h:152
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
Each supported file format is described by the following structure.
Definition: mod_format.h:43
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
struct ast_format_def::@276 list
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
const int fd
Definition: cli.h:159
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
struct ast_format * format
Definition: mod_format.h:48
#define CLI_SHOWUSAGE
Definition: cli.h:45
Definition: file.c:69
char name[80]
Definition: mod_format.h:44
#define FORMAT2
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
#define FORMAT

◆ is_absolute_path()

static int is_absolute_path ( const char *  filename)
static

Definition at line 640 of file file.c.

Referenced by fileexists_test().

641 {
642  return filename[0] == '/';
643 }

◆ is_remote_path()

static int is_remote_path ( const char *  filename)
static

Definition at line 645 of file file.c.

Referenced by fileexists_test().

646 {
647  return strstr(filename, "://") ? 1 : 0;
648 }

◆ json_array_from_list()

static struct ast_json* json_array_from_list ( const char *  list,
const char *  sep 
)
static

Definition at line 74 of file file.c.

References array(), ast_json_array_append(), ast_json_array_create(), ast_json_ref(), ast_json_string_create(), ast_json_unref(), ast_strdupa, ext, NULL, RAII_VAR, and strsep().

Referenced by publish_format_update().

75 {
77  char *stringp, *ext;
78 
79  stringp = ast_strdupa(list); /* this is in the stack so does not need to be freed */
80  if (!array || !stringp) {
81  return NULL;
82  }
83 
84  while ((ext = strsep(&stringp, sep))) {
86  return NULL;
87  }
88  }
89 
90  return ast_json_ref(array);
91 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define NULL
Definition: resample.c:96
const char * ext
Definition: http.c:147
struct ast_format_def::@276 list
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
char * strsep(char **str, const char *delims)
Abstract JSON element (object, array, string, int, ...).

◆ open_wrapper()

static int open_wrapper ( struct ast_filestream s)
static

Definition at line 490 of file file.c.

References fn_wrapper(), NULL, and WRAP_OPEN.

Referenced by ast_readfile(), and filehelper().

491 {
492  return fn_wrapper(s, NULL, WRAP_OPEN);
493 }
#define NULL
Definition: resample.c:96
static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
Definition: file.c:468

◆ publish_format_update()

static int publish_format_update ( const struct ast_format_def f,
struct stasis_message_type type 
)
static

Definition at line 93 of file file.c.

References ao2_cleanup, ast_json_pack(), ast_json_payload_create(), ast_json_unref(), ast_system_topic(), ast_format_def::exts, json_array_from_list(), ast_format_def::name, NULL, RAII_VAR, stasis_message_create(), and stasis_publish().

Referenced by __ast_format_def_register(), and ast_format_def_unregister().

94 {
95  RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
96  RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
97  RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref);
98 
99  if (!type) {
100  return -1;
101  }
102 
103  json_object = ast_json_pack("{s: s, s: o}",
104  "format", f->name,
105  "extensions", json_array_from_list(f->exts, "|"));
106  if (!json_object) {
107  return -1;
108  }
109 
110  json_payload = ast_json_payload_create(json_object);
111  if (!json_payload) {
112  return -1;
113  }
114 
115  msg = stasis_message_create(type, json_payload);
116  if (!msg) {
117  return -1;
118  }
119 
121  return 0;
122 }
char exts[80]
Definition: mod_format.h:45
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
struct ast_json_payload * ast_json_payload_create(struct ast_json *json)
Create an ao2 object to pass json blobs as data payloads for stasis.
Definition: json.c:735
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
char name[80]
Definition: mod_format.h:44
struct stasis_topic * ast_system_topic(void)
A Stasis Message Bus API topic which publishes messages regarding system changes. ...
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic&#39;s subscribers.
Definition: stasis.c:1511
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Abstract JSON element (object, array, string, int, ...).
static struct ast_json * json_array_from_list(const char *list, const char *sep)
Definition: file.c:74

◆ read_frame()

static struct ast_frame* read_frame ( struct ast_filestream s,
int *  whennext 
)
static

Definition at line 874 of file file.c.

References ast_frfree, ast_frisolate, ast_filestream::fmt, NULL, and ast_format_def::read.

Referenced by ast_readaudio_callback(), ast_readframe(), ast_readvideo_callback(), and audiohook_read_frame_helper().

875 {
876  struct ast_frame *fr, *new_fr;
877 
878  if (!s || !s->fmt) {
879  return NULL;
880  }
881 
882  if (!(fr = s->fmt->read(s, whennext))) {
883  return NULL;
884  }
885 
886  if (!(new_fr = ast_frisolate(fr))) {
887  ast_frfree(fr);
888  return NULL;
889  }
890 
891  if (new_fr != fr) {
892  ast_frfree(fr);
893  fr = new_fr;
894  }
895 
896  return fr;
897 }
#define NULL
Definition: resample.c:96
struct ast_format_def * fmt
Definition: mod_format.h:103
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
struct ast_frame *(* read)(struct ast_filestream *, int *whennext)
Definition: mod_format.h:74
#define ast_frfree(fr)
Data structure associated with a single frame of data.

◆ rewrite_wrapper()

static int rewrite_wrapper ( struct ast_filestream s,
const char *  comment 
)
static

Definition at line 485 of file file.c.

References fn_wrapper().

Referenced by ast_writefile().

486 {
487  return fn_wrapper(s, comment, WRAP_REWRITE);
488 }
static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
Definition: file.c:468
#define comment
Definition: ael_lex.c:976

◆ sanitize_waitstream_return()

static int sanitize_waitstream_return ( int  return_value)
static

Definition at line 1759 of file file.c.

References AST_CONTROL_STREAM_RESTART, AST_CONTROL_STREAM_STOP, and AST_CONTROL_STREAM_SUSPEND.

Referenced by ast_waitstream(), ast_waitstream_exten(), and ast_waitstream_full().

1760 {
1761  switch (return_value) {
1765  /* Fall through and set return_value to 0 */
1766  return_value = 0;
1767  break;
1768  default:
1769  /* Do nothing */
1770  break;
1771  }
1772 
1773  return return_value;
1774 }

◆ STASIS_MESSAGE_TYPE_DEFN() [1/2]

STASIS_MESSAGE_TYPE_DEFN ( ast_format_register_type  )

◆ STASIS_MESSAGE_TYPE_DEFN() [2/2]

STASIS_MESSAGE_TYPE_DEFN ( ast_format_unregister_type  )

◆ type_in_list()

static int type_in_list ( const char *  list,
const char *  type,
int(*)(const char *s1, const char *s2)  cmp 
)
static

Definition at line 338 of file file.c.

References ast_strdupa, item, and strsep().

Referenced by ast_get_extension_for_mime_type().

339 {
340  char *stringp = ast_strdupa(list), *item;
341 
342  while ((item = strsep(&stringp, "|"))) {
343  if (!cmp(item, type)) {
344  return 1;
345  }
346  }
347 
348  return 0;
349 }
static const char type[]
Definition: chan_ooh323.c:109
static struct aco_type item
Definition: test_config.c:1463
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
char * strsep(char **str, const char *delims)

◆ waitstream_control()

static void waitstream_control ( struct ast_channel c,
enum ast_waitstream_fr_cb_values  type,
ast_waitstream_fr_cb  cb,
int  skip_ms 
)
static

Definition at line 1509 of file file.c.

References ast_channel_name(), ast_channel_stream(), ast_format_get_sample_rate(), ast_stream_fastforward(), ast_stream_rewind(), ast_tellstream(), ast_test_suite_event_notify, AST_WAITSTREAM_CB_FASTFORWARD, AST_WAITSTREAM_CB_REWIND, ast_filestream::f, ast_filestream::fmt, and ast_format_def::format.

Referenced by waitstream_core().

1513 {
1514  switch (type)
1515  {
1517  {
1518  int eoftest;
1520  eoftest = fgetc(ast_channel_stream(c)->f);
1521  if (feof(ast_channel_stream(c)->f)) {
1523  } else {
1524  ungetc(eoftest, ast_channel_stream(c)->f);
1525  }
1526  }
1527  break;
1530  break;
1531  default:
1532  break;
1533  }
1534 
1535  if (cb) {
1536  long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1537  cb(c, ms_len, type);
1538  }
1539 
1540  ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1541  "Control: %s\r\n"
1542  "SkipMs: %d\r\n",
1543  ast_channel_name(c),
1544  (type == AST_WAITSTREAM_CB_FASTFORWARD) ? "FastForward" : "Rewind",
1545  skip_ms);
1546 }
static const char type[]
Definition: chan_ooh323.c:109
int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
Rewind stream ms.
Definition: file.c:1063
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1048
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:196
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
const char * ast_channel_name(const struct ast_channel *chan)
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
Fast forward stream ms.
Definition: file.c:1058

◆ waitstream_core()

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 
)
static

the core of all waitstream() functions

Definition at line 1551 of file file.c.

References ast_channel_caller(), ast_channel_clear_flag(), ast_channel_flags(), ast_channel_name(), ast_channel_sched(), ast_channel_set_flag(), ast_channel_softhangup_internal_flag(), ast_channel_stream(), ast_channel_timingfunc(), AST_CONTROL_ANSWER, AST_CONTROL_AOC, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_CONNECTED_LINE, AST_CONTROL_FLASH, AST_CONTROL_HANGUP, AST_CONTROL_HOLD, AST_CONTROL_PVT_CAUSE_CODE, AST_CONTROL_REDIRECTING, AST_CONTROL_RINGING, AST_CONTROL_SRCCHANGE, AST_CONTROL_SRCUPDATE, AST_CONTROL_STREAM_FORWARD, AST_CONTROL_STREAM_RESTART, AST_CONTROL_STREAM_REVERSE, AST_CONTROL_STREAM_STOP, AST_CONTROL_STREAM_SUSPEND, AST_CONTROL_UNHOLD, AST_CONTROL_UPDATE_RTP_PEER, AST_CONTROL_VIDUPDATE, ast_exists_extension(), AST_FLAG_END_DTMF_ONLY, AST_FLAG_MASQ_NOSTREAM, ast_format_get_sample_rate(), AST_FRAME_CONTROL, AST_FRAME_DTMF_END, AST_FRAME_VOICE, ast_frfree, ast_log, ast_read(), ast_sched_runq(), ast_sched_wait(), ast_stopstream(), ast_strdupa, ast_tellstream(), ast_test_flag, ast_test_suite_event_notify, ast_waitfor(), ast_waitfor_nandfds(), AST_WAITSTREAM_CB_FASTFORWARD, AST_WAITSTREAM_CB_REWIND, AST_WAITSTREAM_CB_START, ast_frame::data, ast_frame::datalen, errno, exten, ast_filestream::fmt, ast_format_def::format, ast_frame::frametype, ast_frame_subclass::integer, LOG_WARNING, NULL, ast_filestream::orig_chan_name, ast_frame::ptr, S_COR, ast_frame::subclass, and waitstream_control().

Referenced by ast_waitstream(), ast_waitstream_exten(), ast_waitstream_fr(), ast_waitstream_fr_w_cb(), and ast_waitstream_full().

1560 {
1561  const char *orig_chan_name = NULL;
1562 
1563  int err = 0;
1564 
1565  if (!breakon)
1566  breakon = "";
1567  if (!forward)
1568  forward = "";
1569  if (!reverse)
1570  reverse = "";
1571 
1572  /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1574 
1576  orig_chan_name = ast_strdupa(ast_channel_name(c));
1577 
1578  if (ast_channel_stream(c) && cb) {
1579  long ms_len = ast_tellstream(ast_channel_stream(c)) / (ast_format_get_sample_rate(ast_channel_stream(c)->fmt->format) / 1000);
1580  cb(c, ms_len, AST_WAITSTREAM_CB_START);
1581  }
1582 
1583  while (ast_channel_stream(c)) {
1584  int res;
1585  int ms;
1586 
1587  if (orig_chan_name && strcasecmp(orig_chan_name, ast_channel_name(c))) {
1588  ast_stopstream(c);
1589  err = 1;
1590  break;
1591  }
1592 
1594 
1595  if (ms < 0 && !ast_channel_timingfunc(c)) {
1596  ast_stopstream(c);
1597  break;
1598  }
1599  if (ms < 0)
1600  ms = 1000;
1601  if (cmdfd < 0) {
1602  res = ast_waitfor(c, ms);
1603  if (res < 0) {
1604  ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1606  return res;
1607  }
1608  } else {
1609  int outfd;
1610  struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1611  if (!rchan && (outfd < 0) && (ms)) {
1612  /* Continue */
1613  if (errno == EINTR)
1614  continue;
1615  ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1617  return -1;
1618  } else if (outfd > -1) { /* this requires cmdfd set */
1619  /* The FD we were watching has something waiting */
1621  return 1;
1622  }
1623  /* if rchan is set, it is 'c' */
1624  res = rchan ? 1 : 0; /* map into 'res' values */
1625  }
1626  if (res > 0) {
1627  struct ast_frame *fr = ast_read(c);
1628  if (!fr) {
1630  return -1;
1631  }
1632  switch (fr->frametype) {
1633  case AST_FRAME_DTMF_END:
1634  if (context) {
1635  const char exten[2] = { fr->subclass.integer, '\0' };
1636  if (ast_exists_extension(c, context, exten, 1,
1637  S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, NULL))) {
1638  res = fr->subclass.integer;
1639  ast_frfree(fr);
1641  return res;
1642  }
1643  } else {
1644  res = fr->subclass.integer;
1645  if (strchr(forward, res)) {
1647  } else if (strchr(reverse, res)) {
1649  } else if (strchr(breakon, res)) {
1650  ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1651  "Control: %s\r\n",
1652  ast_channel_name(c),
1653  "Break");
1654 
1655  ast_frfree(fr);
1657  return res;
1658  }
1659  }
1660  break;
1661  case AST_FRAME_CONTROL:
1662  switch (fr->subclass.integer) {
1666  /* Fall-through and break out */
1667  ast_test_suite_event_notify("PLAYBACK","Channel: %s\r\n"
1668  "Control: %s\r\n",
1669  ast_channel_name(c),
1670  "Break");
1671  res = fr->subclass.integer;
1672  ast_frfree(fr);
1674  return res;
1676  if (!skip_ms) {
1677  skip_ms = 3000;
1678  }
1680  break;
1682  if (!skip_ms) {
1683  skip_ms = 3000;
1684  }
1686  break;
1687  case AST_CONTROL_HANGUP:
1688  case AST_CONTROL_BUSY:
1690  ast_frfree(fr);
1692  return -1;
1693  case AST_CONTROL_RINGING:
1694  case AST_CONTROL_ANSWER:
1695  case AST_CONTROL_VIDUPDATE:
1696  case AST_CONTROL_SRCUPDATE:
1697  case AST_CONTROL_SRCCHANGE:
1698  case AST_CONTROL_HOLD:
1699  case AST_CONTROL_UNHOLD:
1702  case AST_CONTROL_AOC:
1705  case AST_CONTROL_FLASH:
1706  case -1:
1707  /* Unimportant */
1708  break;
1709  default:
1710  ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass.integer);
1711  }
1712  break;
1713  case AST_FRAME_VOICE:
1714  /* Write audio if appropriate */
1715  if (audiofd > -1) {
1716  if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1717  ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1718  }
1719  }
1720  default:
1721  /* Ignore all others */
1722  break;
1723  }
1724  ast_frfree(fr);
1725  }
1727  }
1728 
1730 
1731  return (err || ast_channel_softhangup_internal_flag(c)) ? -1 : 0;
1732 }
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
Main Channel structure associated with a channel.
int ast_sched_runq(struct ast_sched_context *con)
Runs the queue.
Definition: sched.c:755
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define LOG_WARNING
Definition: logger.h:274
void ast_channel_clear_flag(struct ast_channel *chan, unsigned int flag)
Definition: channel.c:11235
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4302
#define NULL
Definition: resample.c:96
struct ast_channel * ast_waitfor_nandfds(struct ast_channel **chan, int n, int *fds, int nfds, int *exception, int *outfd, int *ms)
Waits for activity on a group of channels.
Definition: channel.c:2997
struct ast_frame_subclass subclass
Number structure.
Definition: app_followme.c:154
#define ast_log
Definition: astobj2.c:42
off_t ast_tellstream(struct ast_filestream *fs)
Tell where we are in a stream.
Definition: file.c:1048
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:85
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4179
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:196
struct ast_sched_context * ast_channel_sched(const struct ast_channel *chan)
int ast_stopstream(struct ast_channel *tmp)
Stops a stream.
Definition: file.c:187
void ast_channel_set_flag(struct ast_channel *chan, unsigned int flag)
Set a flag on a channel.
Definition: channel.c:11228
int errno
struct ast_filestream * ast_channel_stream(const struct ast_channel *chan)
int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3171
const char * ast_channel_name(const struct ast_channel *chan)
#define ast_frfree(fr)
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
static void waitstream_control(struct ast_channel *c, enum ast_waitstream_fr_cb_values type, ast_waitstream_fr_cb cb, int skip_ms)
Definition: file.c:1509
int ast_sched_wait(struct ast_sched_context *con) attribute_warn_unused_result
Determines number of seconds until the next outstanding event to take place.
Definition: sched.c:431
Data structure associated with a single frame of data.
union ast_frame::@263 data
enum ast_frame_type frametype
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
struct ast_flags * ast_channel_flags(struct ast_channel *chan)
ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)

Variable Documentation

◆ ast_language_is_prefix

int ast_language_is_prefix = 1

The following variable controls the layout of localized sound files. If 0, use the historical layout with prefix just before the filename (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm), if 1 put the prefix at the beginning of the filename (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm). The latter permits a language to be entirely in one directory.

This is settable in asterisk.conf.

Definition at line 67 of file file.c.

Referenced by fileexists_test(), handle_show_settings(), load_asterisk_conf(), and main().

◆ cli_file

struct ast_cli_entry cli_file[]
static
Initial value:
= {
{ .handler = handle_cli_core_show_file_formats , .summary = "Displays file formats" ,}
}
static char * handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: file.c:1902

Definition at line 1972 of file file.c.

◆ formats

struct formats formats = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, {1, 0} } , }
static

◆ read_dirs_lock

ast_mutex_t read_dirs_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static

Lock to hold when iterating over directories.

Currently, 'readdir' is not required to be thread-safe. In most modern implementations it should be safe to make concurrent calls into 'readdir' that specify different directory streams (glibc would be one of these). However, since it is potentially unsafe for some implementations we'll use our own locking in order to achieve synchronization for those.

Definition at line 1228 of file file.c.

Referenced by ast_file_read_dirs().