file.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Generic File Format Support.
  20. * Should be included by clients of the file handling routines.
  21. * File service providers should instead include mod_format.h
  22. */
  23. #ifndef _ASTERISK_FILE_H
  24. #define _ASTERISK_FILE_H
  25. #ifdef HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #endif
  28. #ifdef HAVE_MMAP
  29. #include <sys/mman.h>
  30. #endif
  31. #if defined(__cplusplus) || defined(c_plusplus)
  32. extern "C" {
  33. #endif
  34. struct ast_filestream;
  35. struct ast_format;
  36. /*! The maximum number of formats we expect to see in a format string */
  37. #define AST_MAX_FORMATS 10
  38. /*! Convenient for waiting */
  39. #define AST_DIGIT_NONE ""
  40. #define AST_DIGIT_ANY "0123456789#*ABCD"
  41. #define AST_DIGIT_ANYNUM "0123456789"
  42. #define SEEK_FORCECUR 10
  43. /*! The type of event associated with a ast_waitstream_fr_cb invocation */
  44. enum ast_waitstream_fr_cb_values {
  45. AST_WAITSTREAM_CB_REWIND = 1,
  46. AST_WAITSTREAM_CB_FASTFORWARD,
  47. AST_WAITSTREAM_CB_START
  48. };
  49. /*!
  50. * \brief callback used during dtmf controlled file playback to indicate
  51. * location of playback in a file after rewinding or fastfowarding
  52. * a file.
  53. */
  54. typedef void (ast_waitstream_fr_cb)(struct ast_channel *chan, long ms, enum ast_waitstream_fr_cb_values val);
  55. /*!
  56. * \brief Streams a file
  57. * \param c channel to stream the file to
  58. * \param filename the name of the file you wish to stream, minus the extension
  59. * \param preflang the preferred language you wish to have the file streamed to you in
  60. * Prepares a channel for the streaming of a file. To start the stream, afterward do a ast_waitstream() on the channel
  61. * Also, it will stop any existing streams on the channel.
  62. * \retval 0 on success.
  63. * \retval -1 on failure.
  64. */
  65. int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang);
  66. /*!
  67. * \brief stream file until digit
  68. * If the file name is non-empty, try to play it.
  69. * \note If digits == "" then we can simply check for non-zero.
  70. * \return 0 if success.
  71. * \retval -1 if error.
  72. * \retval digit if interrupted by a digit.
  73. */
  74. int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits);
  75. /*!
  76. * \brief Stops a stream
  77. *
  78. * \param c The channel you wish to stop playback on
  79. *
  80. * Stop playback of a stream
  81. *
  82. * \retval 0 always
  83. *
  84. * \note The channel does not need to be locked before calling this function.
  85. */
  86. int ast_stopstream(struct ast_channel *c);
  87. /*!
  88. * \brief Checks for the existence of a given file
  89. * \param filename name of the file you wish to check, minus the extension
  90. * \param fmt the format you wish to check (the extension)
  91. * \param preflang (the preferred language you wisht to find the file in)
  92. * See if a given file exists in a given format. If fmt is NULL, any format is accepted.
  93. * \retval 0, false. The file does not exist
  94. * \retval 1, true. The file does exist.
  95. */
  96. int ast_fileexists(const char *filename, const char *fmt, const char *preflang);
  97. /*!
  98. * \brief Renames a file
  99. * \param oldname the name of the file you wish to act upon (minus the extension)
  100. * \param newname the name you wish to rename the file to (minus the extension)
  101. * \param fmt the format of the file
  102. * Rename a given file in a given format, or if fmt is NULL, then do so for all
  103. * \return -1 on failure
  104. */
  105. int ast_filerename(const char *oldname, const char *newname, const char *fmt);
  106. /*!
  107. * \brief Deletes a file
  108. * \param filename name of the file you wish to delete (minus the extension)
  109. * \param fmt of the file
  110. * Delete a given file in a given format, or if fmt is NULL, then do so for all
  111. */
  112. int ast_filedelete(const char *filename, const char *fmt);
  113. /*!
  114. * \brief Copies a file
  115. * \param oldname name of the file you wish to copy (minus extension)
  116. * \param newname name you wish the file to be copied to (minus extension)
  117. * \param fmt the format of the file
  118. * Copy a given file in a given format, or if fmt is NULL, then do so for all
  119. */
  120. int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
  121. /*!
  122. * \brief Callback called for each file found when reading directories
  123. * \param dir_name the name of the directory
  124. * \param filename the name of the file
  125. * \param obj user data object
  126. * \return non-zero to stop reading, otherwise zero to continue
  127. *
  128. * \note dir_name is not processed by realpath or other functions,
  129. * symbolic links are not resolved. This ensures dir_name
  130. * always starts with the exact string originally passed to
  131. * \ref ast_file_read_dir or \ref ast_file_read_dirs.
  132. */
  133. typedef int (*ast_file_on_file)(const char *dir_name, const char *filename, void *obj);
  134. /*!
  135. * \brief Recursively iterate through files and directories up to max_depth
  136. * \param dir_name the name of the directory to search
  137. * \param on_file callback called on each file
  138. * \param obj user data object
  139. * \param max_depth re-curse into sub-directories up to a given maximum (-1 = infinite)
  140. * \return -1 or errno on failure, otherwise 0
  141. */
  142. int ast_file_read_dirs(const char *dir_name, ast_file_on_file on_file, void *obj, int max_depth);
  143. /*!
  144. * \brief Iterate over each file in a given directory
  145. * \param dir_name the name of the directory to search
  146. * \param on_file callback called on each file
  147. * \param obj user data object
  148. * \return -1 or errno on failure, otherwise 0
  149. */
  150. #define ast_file_read_dir(dir_name, on_file, obj) ast_file_read_dirs(dir_name, on_file, obj, 1)
  151. /*!
  152. * \brief Waits for a stream to stop or digit to be pressed
  153. * \param c channel to waitstream on
  154. * \param breakon string of DTMF digits to break upon
  155. * Begins playback of a stream...
  156. * Wait for a stream to stop or for any one of a given digit to arrive,
  157. * \retval 0 if the stream finishes
  158. * \retval the character if it was interrupted by the channel.
  159. * \retval -1 on error
  160. */
  161. int ast_waitstream(struct ast_channel *c, const char *breakon);
  162. /*!
  163. * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed
  164. * \param c channel to waitstream on
  165. * \param context string of context to match digits to break upon
  166. * Begins playback of a stream...
  167. * Wait for a stream to stop or for any one of a valid extension digit to arrive,
  168. * \retval 0 if the stream finishes.
  169. * \retval the character if it was interrupted.
  170. * \retval -1 on error.
  171. */
  172. int ast_waitstream_exten(struct ast_channel *c, const char *context);
  173. /*!
  174. * \brief Same as waitstream but allows stream to be forwarded or rewound
  175. * \param c channel to waitstream on
  176. * \param breakon string of DTMF digits to break upon
  177. * \param forward DTMF digit to fast forward upon
  178. * \param rewind DTMF digit to rewind upon
  179. * \param ms How many miliseconds to skip forward/back
  180. * Begins playback of a stream...
  181. * Wait for a stream to stop or for any one of a given digit to arrive,
  182. * \retval 0 if the stream finishes.
  183. * \retval the character if it was interrupted,
  184. * \retval the value of the control frame if it was interrupted by some other party,
  185. * \retval -1 on error.
  186. */
  187. int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
  188. /*!
  189. * \brief Same as waitstream_fr but allows a callback to be alerted when a user
  190. * fastforwards or rewinds the file.
  191. * \param c channel to waitstream on
  192. * \param breakon string of DTMF digits to break upon
  193. * \param forward DTMF digit to fast forward upon
  194. * \param rewind DTMF digit to rewind upon
  195. * \param ms How many milliseconds to skip forward/back
  196. * \param cb to call when rewind or fastfoward occurs.
  197. * Begins playback of a stream...
  198. * Wait for a stream to stop or for any one of a given digit to arrive,
  199. * \retval 0 if the stream finishes.
  200. * \retval the character if it was interrupted,
  201. * \retval the value of the control frame if it was interrupted by some other party,
  202. * \retval -1 on error.
  203. */
  204. int ast_waitstream_fr_w_cb(struct ast_channel *c,
  205. const char *breakon,
  206. const char *forward,
  207. const char *rewind,
  208. int ms,
  209. ast_waitstream_fr_cb cb);
  210. /*!
  211. * Same as waitstream, but with audio output to fd and monitored fd checking.
  212. *
  213. * \return 1 if monfd is ready for reading
  214. */
  215. int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
  216. /*!
  217. * \brief Starts reading from a file
  218. * \param filename the name of the file to read from
  219. * \param type format of file you wish to read from
  220. * \param comment comment to go with
  221. * \param flags file flags
  222. * \param check (unimplemented, hence negligible)
  223. * \param mode Open mode
  224. * Open an incoming file stream. flags are flags for the open() command, and
  225. * if check is non-zero, then it will not read a file if there are any files that
  226. * start with that name and have an extension
  227. * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
  228. * \retval a struct ast_filestream on success.
  229. * \retval NULL on failure.
  230. */
  231. struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
  232. /*!
  233. * \brief Starts writing a file
  234. * \param filename the name of the file to write to
  235. * \param type format of file you wish to write out to
  236. * \param comment comment to go with
  237. * \param flags output file flags
  238. * \param check (unimplemented, hence negligible)
  239. * \param mode Open mode
  240. * Create an outgoing file stream. oflags are flags for the open() command, and
  241. * if check is non-zero, then it will not write a file if there are any files that
  242. * start with that name and have an extension
  243. * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
  244. * \retval a struct ast_filestream on success.
  245. * \retval NULL on failure.
  246. */
  247. struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
  248. /*!
  249. * \brief Writes a frame to a stream
  250. * \param fs filestream to write to
  251. * \param f frame to write to the filestream
  252. * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
  253. * \retval 0 on success.
  254. * \retval -1 on failure.
  255. */
  256. int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
  257. /*!
  258. * \brief Closes a stream
  259. * \param f filestream to close
  260. * Close a playback or recording stream
  261. * \retval 0 on success.
  262. * \retval -1 on failure.
  263. */
  264. int ast_closestream(struct ast_filestream *f);
  265. /*!
  266. * \brief Opens stream for use in seeking, playing
  267. * \param chan channel to work with
  268. * \param filename to use
  269. * \param preflang prefered language to use
  270. * \retval a ast_filestream pointer if it opens the file.
  271. * \retval NULL on error.
  272. */
  273. struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
  274. /*!
  275. * \brief Opens stream for use in seeking, playing
  276. * \param chan channel to work with
  277. * \param filename to use
  278. * \param preflang prefered language to use
  279. * \param asis if set, don't clear generators
  280. * \retval a ast_filestream pointer if it opens the file.
  281. * \retval NULL on error.
  282. */
  283. struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
  284. /*!
  285. * \brief Opens stream for use in seeking, playing
  286. * \param chan channel to work with
  287. * \param filename to use
  288. * \param preflang prefered language to use
  289. * \retval a ast_filestream pointer if it opens the file.
  290. * \retval NULL on error.
  291. */
  292. struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
  293. /*!
  294. * \brief Applys a open stream to a channel.
  295. * \param chan channel to work
  296. * \param s ast_filestream to apply
  297. * \retval 0 on success.
  298. * \retval -1 on failure.
  299. */
  300. int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
  301. /*!
  302. * \brief Play a open stream on a channel.
  303. * \param s filestream to play
  304. * \retval 0 on success.
  305. * \retval -1 on failure.
  306. */
  307. int ast_playstream(struct ast_filestream *s);
  308. /*!
  309. * \brief Seeks into stream
  310. * \param fs ast_filestream to perform seek on
  311. * \param sample_offset numbers of samples to seek
  312. * \param whence SEEK_SET, SEEK_CUR, SEEK_END
  313. * \retval 0 on success.
  314. * \retval -1 on failure.
  315. */
  316. int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
  317. /*!
  318. * \brief Trunc stream at current location
  319. * \param fs filestream to act on
  320. * \retval 0 on success.
  321. * \retval -1 on failure.
  322. */
  323. int ast_truncstream(struct ast_filestream *fs);
  324. /*!
  325. * \brief Fast forward stream ms
  326. * \param fs filestream to act on
  327. * \param ms milliseconds to move
  328. * \retval 0 on success.
  329. * \retval -1 on failure.
  330. */
  331. int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
  332. /*!
  333. * \brief Rewind stream ms
  334. * \param fs filestream to act on
  335. * \param ms milliseconds to move
  336. * \retval 0 on success.
  337. * \retval -1 on failure.
  338. */
  339. int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
  340. /*!
  341. * \brief Tell where we are in a stream
  342. * \param fs fs to act on
  343. * \return a long as a sample offset into stream
  344. */
  345. off_t ast_tellstream(struct ast_filestream *fs);
  346. /*!
  347. * \brief Return the sample rate of the stream's format
  348. * \param fs fs to act on
  349. * \return sample rate in Hz
  350. */
  351. int ast_ratestream(struct ast_filestream *fs);
  352. /*!
  353. * \brief Read a frame from a filestream
  354. * \param s ast_filestream to act on
  355. * \return a frame.
  356. * \retval NULL if read failed.
  357. */
  358. struct ast_frame *ast_readframe(struct ast_filestream *s);
  359. /*! Initialize file stuff */
  360. /*!
  361. * Initializes all the various file stuff. Basically just registers the cli stuff
  362. * Returns 0 all the time
  363. */
  364. int ast_file_init(void);
  365. #define AST_RESERVED_POINTERS 20
  366. /*! Remove duplicate formats from a format string. */
  367. /*!
  368. * \param fmts a format string, this string will be modified
  369. * \retval NULL error
  370. * \return a pointer to the reduced format string, this is a pointer to fmts
  371. */
  372. char *ast_format_str_reduce(char *fmts);
  373. /*!
  374. * \brief Get the ast_format associated with the given file extension
  375. * \since 12
  376. *
  377. * \param file_ext The file extension for which to find the format
  378. *
  379. * \retval NULL if not found
  380. * \retval A pointer to the ast_format associated with this file extension
  381. */
  382. struct ast_format *ast_get_format_for_file_ext(const char *file_ext);
  383. #if defined(__cplusplus) || defined(c_plusplus)
  384. }
  385. #endif
  386. #endif /* _ASTERISK_FILE_H */