file.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 Waits for a stream to stop or digit to be pressed
  123. * \param c channel to waitstream on
  124. * \param breakon string of DTMF digits to break upon
  125. * Begins playback of a stream...
  126. * Wait for a stream to stop or for any one of a given digit to arrive,
  127. * \retval 0 if the stream finishes
  128. * \retval the character if it was interrupted,
  129. * \retval -1 on error
  130. */
  131. int ast_waitstream(struct ast_channel *c, const char *breakon);
  132. /*!
  133. * \brief Waits for a stream to stop or digit matching a valid one digit exten to be pressed
  134. * \param c channel to waitstream on
  135. * \param context string of context to match digits to break upon
  136. * Begins playback of a stream...
  137. * Wait for a stream to stop or for any one of a valid extension digit to arrive,
  138. * \retval 0 if the stream finishes.
  139. * \retval the character if it was interrupted.
  140. * \retval -1 on error.
  141. */
  142. int ast_waitstream_exten(struct ast_channel *c, const char *context);
  143. /*!
  144. * \brief Same as waitstream but allows stream to be forwarded or rewound
  145. * \param c channel to waitstream on
  146. * \param breakon string of DTMF digits to break upon
  147. * \param forward DTMF digit to fast forward upon
  148. * \param rewind DTMF digit to rewind upon
  149. * \param ms How many miliseconds to skip forward/back
  150. * Begins playback of a stream...
  151. * Wait for a stream to stop or for any one of a given digit to arrive,
  152. * \retval 0 if the stream finishes.
  153. * \retval the character if it was interrupted.
  154. * \retval -1 on error.
  155. */
  156. int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *rewind, int ms);
  157. /*!
  158. * \brief Same as waitstream_fr but allows a callback to be alerted when a user
  159. * fastforwards or rewinds the file.
  160. * \param c channel to waitstream on
  161. * \param breakon string of DTMF digits to break upon
  162. * \param forward DTMF digit to fast forward upon
  163. * \param rewind DTMF digit to rewind upon
  164. * \param ms How many milliseconds to skip forward/back
  165. * \param cb to call when rewind or fastfoward occurs.
  166. * Begins playback of a stream...
  167. * Wait for a stream to stop or for any one of a given 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_fr_w_cb(struct ast_channel *c,
  173. const char *breakon,
  174. const char *forward,
  175. const char *rewind,
  176. int ms,
  177. ast_waitstream_fr_cb cb);
  178. /*!
  179. * Same as waitstream, but with audio output to fd and monitored fd checking.
  180. *
  181. * \return 1 if monfd is ready for reading
  182. */
  183. int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int monfd);
  184. /*!
  185. * \brief Starts reading from a file
  186. * \param filename the name of the file to read from
  187. * \param type format of file you wish to read from
  188. * \param comment comment to go with
  189. * \param flags file flags
  190. * \param check (unimplemented, hence negligible)
  191. * \param mode Open mode
  192. * Open an incoming file stream. flags are flags for the open() command, and
  193. * if check is non-zero, then it will not read a file if there are any files that
  194. * start with that name and have an extension
  195. * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
  196. * \retval a struct ast_filestream on success.
  197. * \retval NULL on failure.
  198. */
  199. struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
  200. /*!
  201. * \brief Starts writing a file
  202. * \param filename the name of the file to write to
  203. * \param type format of file you wish to write out to
  204. * \param comment comment to go with
  205. * \param flags output file flags
  206. * \param check (unimplemented, hence negligible)
  207. * \param mode Open mode
  208. * Create an outgoing file stream. oflags are flags for the open() command, and
  209. * if check is non-zero, then it will not write a file if there are any files that
  210. * start with that name and have an extension
  211. * Please note, this is a blocking function. Program execution will not return until ast_waitstream completes it's execution.
  212. * \retval a struct ast_filestream on success.
  213. * \retval NULL on failure.
  214. */
  215. struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode);
  216. /*!
  217. * \brief Writes a frame to a stream
  218. * \param fs filestream to write to
  219. * \param f frame to write to the filestream
  220. * Send a frame to a filestream -- note: does NOT free the frame, call ast_frfree manually
  221. * \retval 0 on success.
  222. * \retval -1 on failure.
  223. */
  224. int ast_writestream(struct ast_filestream *fs, struct ast_frame *f);
  225. /*!
  226. * \brief Closes a stream
  227. * \param f filestream to close
  228. * Close a playback or recording stream
  229. * \retval 0 on success.
  230. * \retval -1 on failure.
  231. */
  232. int ast_closestream(struct ast_filestream *f);
  233. /*!
  234. * \brief Opens stream for use in seeking, playing
  235. * \param chan channel to work with
  236. * \param filename to use
  237. * \param preflang prefered language to use
  238. * \retval a ast_filestream pointer if it opens the file.
  239. * \retval NULL on error.
  240. */
  241. struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang);
  242. /*!
  243. * \brief Opens stream for use in seeking, playing
  244. * \param chan channel to work with
  245. * \param filename to use
  246. * \param preflang prefered language to use
  247. * \param asis if set, don't clear generators
  248. * \retval a ast_filestream pointer if it opens the file.
  249. * \retval NULL on error.
  250. */
  251. struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis);
  252. /*!
  253. * \brief Opens stream for use in seeking, playing
  254. * \param chan channel to work with
  255. * \param filename to use
  256. * \param preflang prefered language to use
  257. * \retval a ast_filestream pointer if it opens the file.
  258. * \retval NULL on error.
  259. */
  260. struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang);
  261. /*!
  262. * \brief Applys a open stream to a channel.
  263. * \param chan channel to work
  264. * \param s ast_filestream to apply
  265. * \retval 0 on success.
  266. * \retval -1 on failure.
  267. */
  268. int ast_applystream(struct ast_channel *chan, struct ast_filestream *s);
  269. /*!
  270. * \brief Play a open stream on a channel.
  271. * \param s filestream to play
  272. * \retval 0 on success.
  273. * \retval -1 on failure.
  274. */
  275. int ast_playstream(struct ast_filestream *s);
  276. /*!
  277. * \brief Seeks into stream
  278. * \param fs ast_filestream to perform seek on
  279. * \param sample_offset numbers of samples to seek
  280. * \param whence SEEK_SET, SEEK_CUR, SEEK_END
  281. * \retval 0 on success.
  282. * \retval -1 on failure.
  283. */
  284. int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence);
  285. /*!
  286. * \brief Trunc stream at current location
  287. * \param fs filestream to act on
  288. * \retval 0 on success.
  289. * \retval -1 on failure.
  290. */
  291. int ast_truncstream(struct ast_filestream *fs);
  292. /*!
  293. * \brief Fast forward stream ms
  294. * \param fs filestream to act on
  295. * \param ms milliseconds to move
  296. * \retval 0 on success.
  297. * \retval -1 on failure.
  298. */
  299. int ast_stream_fastforward(struct ast_filestream *fs, off_t ms);
  300. /*!
  301. * \brief Rewind stream ms
  302. * \param fs filestream to act on
  303. * \param ms milliseconds to move
  304. * \retval 0 on success.
  305. * \retval -1 on failure.
  306. */
  307. int ast_stream_rewind(struct ast_filestream *fs, off_t ms);
  308. /*!
  309. * \brief Tell where we are in a stream
  310. * \param fs fs to act on
  311. * \return a long as a sample offset into stream
  312. */
  313. off_t ast_tellstream(struct ast_filestream *fs);
  314. /*!
  315. * \brief Read a frame from a filestream
  316. * \param s ast_filestream to act on
  317. * \return a frame.
  318. * \retval NULL if read failed.
  319. */
  320. struct ast_frame *ast_readframe(struct ast_filestream *s);
  321. /*! Initialize file stuff */
  322. /*!
  323. * Initializes all the various file stuff. Basically just registers the cli stuff
  324. * Returns 0 all the time
  325. */
  326. int ast_file_init(void);
  327. #define AST_RESERVED_POINTERS 20
  328. /*! Remove duplicate formats from a format string. */
  329. /*!
  330. * \param fmts a format string, this string will be modified
  331. * \retval NULL error
  332. * \return a pointer to the reduced format string, this is a pointer to fmts
  333. */
  334. char *ast_format_str_reduce(char *fmts);
  335. #if defined(__cplusplus) || defined(c_plusplus)
  336. }
  337. #endif
  338. #endif /* _ASTERISK_FILE_H */