app_record.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Matthew Fredrickson <creslin@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. *
  20. * \brief Trivial application to record a sound file
  21. *
  22. * \author Matthew Fredrickson <creslin@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/dsp.h" /* use dsp routines for silence detection */
  37. /*** DOCUMENTATION
  38. <application name="Record" language="en_US">
  39. <synopsis>
  40. Record to a file.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="filename" required="true" argsep=".">
  44. <argument name="filename" required="true" />
  45. <argument name="format" required="true">
  46. <para>Is the format of the file type to be recorded (wav, gsm, etc).</para>
  47. </argument>
  48. </parameter>
  49. <parameter name="silence">
  50. <para>Is the number of seconds of silence to allow before returning.</para>
  51. </parameter>
  52. <parameter name="maxduration">
  53. <para>Is the maximum recording duration in seconds. If missing
  54. or 0 there is no maximum.</para>
  55. </parameter>
  56. <parameter name="options">
  57. <optionlist>
  58. <option name="a">
  59. <para>Append to existing recording rather than replacing.</para>
  60. </option>
  61. <option name="n">
  62. <para>Do not answer, but record anyway if line not yet answered.</para>
  63. </option>
  64. <option name="q">
  65. <para>quiet (do not play a beep tone).</para>
  66. </option>
  67. <option name="s">
  68. <para>skip recording if the line is not yet answered.</para>
  69. </option>
  70. <option name="t">
  71. <para>use alternate '*' terminator key (DTMF) instead of default '#'</para>
  72. </option>
  73. <option name="x">
  74. <para>Ignore all terminator keys (DTMF) and keep recording until hangup.</para>
  75. </option>
  76. <option name="k">
  77. <para>Keep recorded file upon hangup.</para>
  78. </option>
  79. <option name="y">
  80. <para>Terminate recording if *any* DTMF digit is received.</para>
  81. </option>
  82. </optionlist>
  83. </parameter>
  84. </syntax>
  85. <description>
  86. <para>If filename contains <literal>%d</literal>, these characters will be replaced with a number
  87. incremented by one each time the file is recorded.
  88. Use <astcli>core show file formats</astcli> to see the available formats on your system
  89. User can press <literal>#</literal> to terminate the recording and continue to the next priority.
  90. If the user hangs up during a recording, all data will be lost and the application will terminate.</para>
  91. <variablelist>
  92. <variable name="RECORDED_FILE">
  93. <para>Will be set to the final filename of the recording.</para>
  94. </variable>
  95. <variable name="RECORD_STATUS">
  96. <para>This is the final status of the command</para>
  97. <value name="DTMF">A terminating DTMF was received ('#' or '*', depending upon option 't')</value>
  98. <value name="SILENCE">The maximum silence occurred in the recording.</value>
  99. <value name="SKIP">The line was not yet answered and the 's' option was specified.</value>
  100. <value name="TIMEOUT">The maximum length was reached.</value>
  101. <value name="HANGUP">The channel was hung up.</value>
  102. <value name="ERROR">An unrecoverable error occurred, which resulted in a WARNING to the logs.</value>
  103. </variable>
  104. </variablelist>
  105. </description>
  106. </application>
  107. ***/
  108. static char *app = "Record";
  109. enum {
  110. OPTION_APPEND = (1 << 0),
  111. OPTION_NOANSWER = (1 << 1),
  112. OPTION_QUIET = (1 << 2),
  113. OPTION_SKIP = (1 << 3),
  114. OPTION_STAR_TERMINATE = (1 << 4),
  115. OPTION_IGNORE_TERMINATE = (1 << 5),
  116. OPTION_KEEP = (1 << 6),
  117. FLAG_HAS_PERCENT = (1 << 7),
  118. OPTION_ANY_TERMINATE = (1 << 8),
  119. };
  120. AST_APP_OPTIONS(app_opts,{
  121. AST_APP_OPTION('a', OPTION_APPEND),
  122. AST_APP_OPTION('k', OPTION_KEEP),
  123. AST_APP_OPTION('n', OPTION_NOANSWER),
  124. AST_APP_OPTION('q', OPTION_QUIET),
  125. AST_APP_OPTION('s', OPTION_SKIP),
  126. AST_APP_OPTION('t', OPTION_STAR_TERMINATE),
  127. AST_APP_OPTION('y', OPTION_ANY_TERMINATE),
  128. AST_APP_OPTION('x', OPTION_IGNORE_TERMINATE),
  129. });
  130. static int record_exec(struct ast_channel *chan, const char *data)
  131. {
  132. int res = 0;
  133. int count = 0;
  134. char *ext = NULL, *opts[0];
  135. char *parse, *dir, *file;
  136. int i = 0;
  137. char tmp[256];
  138. struct ast_filestream *s = NULL;
  139. struct ast_frame *f = NULL;
  140. struct ast_dsp *sildet = NULL; /* silence detector dsp */
  141. int totalsilence = 0;
  142. int dspsilence = 0;
  143. int silence = 0; /* amount of silence to allow */
  144. int gotsilence = 0; /* did we timeout for silence? */
  145. int maxduration = 0; /* max duration of recording in milliseconds */
  146. int gottimeout = 0; /* did we timeout for maxduration exceeded? */
  147. int terminator = '#';
  148. int rfmt = 0;
  149. int ioflags;
  150. int waitres;
  151. struct ast_silence_generator *silgen = NULL;
  152. struct ast_flags flags = { 0, };
  153. AST_DECLARE_APP_ARGS(args,
  154. AST_APP_ARG(filename);
  155. AST_APP_ARG(silence);
  156. AST_APP_ARG(maxduration);
  157. AST_APP_ARG(options);
  158. );
  159. /* The next few lines of code parse out the filename and header from the input string */
  160. if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
  161. ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
  162. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  163. return -1;
  164. }
  165. parse = ast_strdupa(data);
  166. AST_STANDARD_APP_ARGS(args, parse);
  167. if (args.argc == 4)
  168. ast_app_parse_options(app_opts, &flags, opts, args.options);
  169. if (!ast_strlen_zero(args.filename)) {
  170. if (strstr(args.filename, "%d"))
  171. ast_set_flag(&flags, FLAG_HAS_PERCENT);
  172. ext = strrchr(args.filename, '.'); /* to support filename with a . in the filename, not format */
  173. if (!ext)
  174. ext = strchr(args.filename, ':');
  175. if (ext) {
  176. *ext = '\0';
  177. ext++;
  178. }
  179. }
  180. if (!ext) {
  181. ast_log(LOG_WARNING, "No extension specified to filename!\n");
  182. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  183. return -1;
  184. }
  185. if (args.silence) {
  186. if ((sscanf(args.silence, "%30d", &i) == 1) && (i > -1)) {
  187. silence = i * 1000;
  188. } else if (!ast_strlen_zero(args.silence)) {
  189. ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", args.silence);
  190. }
  191. }
  192. if (args.maxduration) {
  193. if ((sscanf(args.maxduration, "%30d", &i) == 1) && (i > -1))
  194. /* Convert duration to milliseconds */
  195. maxduration = i * 1000;
  196. else if (!ast_strlen_zero(args.maxduration))
  197. ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", args.maxduration);
  198. }
  199. if (ast_test_flag(&flags, OPTION_STAR_TERMINATE))
  200. terminator = '*';
  201. if (ast_test_flag(&flags, OPTION_IGNORE_TERMINATE))
  202. terminator = '\0';
  203. /* done parsing */
  204. /* these are to allow the use of the %d in the config file for a wild card of sort to
  205. create a new file with the inputed name scheme */
  206. if (ast_test_flag(&flags, FLAG_HAS_PERCENT)) {
  207. AST_DECLARE_APP_ARGS(fname,
  208. AST_APP_ARG(piece)[100];
  209. );
  210. char *tmp2 = ast_strdupa(args.filename);
  211. char countstring[15];
  212. int idx;
  213. /* Separate each piece out by the format specifier */
  214. AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%');
  215. do {
  216. int tmplen;
  217. /* First piece has no leading percent, so it's copied verbatim */
  218. ast_copy_string(tmp, fname.piece[0], sizeof(tmp));
  219. tmplen = strlen(tmp);
  220. for (idx = 1; idx < fname.argc; idx++) {
  221. if (fname.piece[idx][0] == 'd') {
  222. /* Substitute the count */
  223. snprintf(countstring, sizeof(countstring), "%d", count);
  224. ast_copy_string(tmp + tmplen, countstring, sizeof(tmp) - tmplen);
  225. tmplen += strlen(countstring);
  226. } else if (tmplen + 2 < sizeof(tmp)) {
  227. /* Unknown format specifier - just copy it verbatim */
  228. tmp[tmplen++] = '%';
  229. tmp[tmplen++] = fname.piece[idx][0];
  230. }
  231. /* Copy the remaining portion of the piece */
  232. ast_copy_string(tmp + tmplen, &(fname.piece[idx][1]), sizeof(tmp) - tmplen);
  233. }
  234. count++;
  235. } while (ast_fileexists(tmp, ext, chan->language) > 0);
  236. pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
  237. } else
  238. ast_copy_string(tmp, args.filename, sizeof(tmp));
  239. /* end of routine mentioned */
  240. if (chan->_state != AST_STATE_UP) {
  241. if (ast_test_flag(&flags, OPTION_SKIP)) {
  242. /* At the user's option, skip if the line is not up */
  243. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "SKIP");
  244. return 0;
  245. } else if (!ast_test_flag(&flags, OPTION_NOANSWER)) {
  246. /* Otherwise answer unless we're supposed to record while on-hook */
  247. res = ast_answer(chan);
  248. }
  249. }
  250. if (res) {
  251. ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
  252. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  253. goto out;
  254. }
  255. if (!ast_test_flag(&flags, OPTION_QUIET)) {
  256. /* Some code to play a nice little beep to signify the start of the record operation */
  257. res = ast_streamfile(chan, "beep", chan->language);
  258. if (!res) {
  259. res = ast_waitstream(chan, "");
  260. } else {
  261. ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
  262. }
  263. ast_stopstream(chan);
  264. }
  265. /* The end of beep code. Now the recording starts */
  266. if (silence > 0) {
  267. rfmt = chan->readformat;
  268. res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
  269. if (res < 0) {
  270. ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
  271. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  272. return -1;
  273. }
  274. sildet = ast_dsp_new();
  275. if (!sildet) {
  276. ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
  277. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  278. return -1;
  279. }
  280. ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
  281. }
  282. /* Create the directory if it does not exist. */
  283. dir = ast_strdupa(tmp);
  284. if ((file = strrchr(dir, '/')))
  285. *file++ = '\0';
  286. ast_mkdir (dir, 0777);
  287. ioflags = ast_test_flag(&flags, OPTION_APPEND) ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
  288. s = ast_writefile(tmp, ext, NULL, ioflags, 0, AST_FILE_MODE);
  289. if (!s) {
  290. ast_log(LOG_WARNING, "Could not create file %s\n", args.filename);
  291. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  292. goto out;
  293. }
  294. if (ast_opt_transmit_silence)
  295. silgen = ast_channel_start_silence_generator(chan);
  296. /* Request a video update */
  297. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  298. if (maxduration <= 0)
  299. maxduration = -1;
  300. while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
  301. if (maxduration > 0) {
  302. if (waitres == 0) {
  303. gottimeout = 1;
  304. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "TIMEOUT");
  305. break;
  306. }
  307. maxduration = waitres;
  308. }
  309. f = ast_read(chan);
  310. if (!f) {
  311. res = -1;
  312. break;
  313. }
  314. if (f->frametype == AST_FRAME_VOICE) {
  315. res = ast_writestream(s, f);
  316. if (res) {
  317. ast_log(LOG_WARNING, "Problem writing frame\n");
  318. ast_frfree(f);
  319. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  320. break;
  321. }
  322. if (silence > 0) {
  323. dspsilence = 0;
  324. ast_dsp_silence(sildet, f, &dspsilence);
  325. if (dspsilence) {
  326. totalsilence = dspsilence;
  327. } else {
  328. totalsilence = 0;
  329. }
  330. if (totalsilence > silence) {
  331. /* Ended happily with silence */
  332. ast_frfree(f);
  333. gotsilence = 1;
  334. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "SILENCE");
  335. break;
  336. }
  337. }
  338. } else if (f->frametype == AST_FRAME_VIDEO) {
  339. res = ast_writestream(s, f);
  340. if (res) {
  341. ast_log(LOG_WARNING, "Problem writing frame\n");
  342. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  343. ast_frfree(f);
  344. break;
  345. }
  346. } else if ((f->frametype == AST_FRAME_DTMF) &&
  347. ((f->subclass.integer == terminator) ||
  348. (ast_test_flag(&flags, OPTION_ANY_TERMINATE)))) {
  349. ast_frfree(f);
  350. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "DTMF");
  351. break;
  352. }
  353. ast_frfree(f);
  354. }
  355. if (!f) {
  356. ast_debug(1, "Got hangup\n");
  357. res = -1;
  358. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "HANGUP");
  359. if (!ast_test_flag(&flags, OPTION_KEEP)) {
  360. ast_filedelete(args.filename, NULL);
  361. }
  362. }
  363. if (gotsilence) {
  364. ast_stream_rewind(s, silence - 1000);
  365. ast_truncstream(s);
  366. } else if (!gottimeout) {
  367. /* Strip off the last 1/4 second of it */
  368. ast_stream_rewind(s, 250);
  369. ast_truncstream(s);
  370. }
  371. ast_closestream(s);
  372. if (silgen)
  373. ast_channel_stop_silence_generator(chan, silgen);
  374. out:
  375. if ((silence > 0) && rfmt) {
  376. res = ast_set_read_format(chan, rfmt);
  377. if (res)
  378. ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
  379. if (sildet)
  380. ast_dsp_free(sildet);
  381. }
  382. return res;
  383. }
  384. static int unload_module(void)
  385. {
  386. return ast_unregister_application(app);
  387. }
  388. static int load_module(void)
  389. {
  390. return ast_register_application_xml(app, record_exec);
  391. }
  392. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial Record Application");