app_record.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. struct ast_format rfmt;
  149. int ioflags;
  150. struct ast_silence_generator *silgen = NULL;
  151. struct ast_flags flags = { 0, };
  152. AST_DECLARE_APP_ARGS(args,
  153. AST_APP_ARG(filename);
  154. AST_APP_ARG(silence);
  155. AST_APP_ARG(maxduration);
  156. AST_APP_ARG(options);
  157. );
  158. int ms;
  159. struct timeval start;
  160. ast_format_clear(&rfmt);
  161. /* The next few lines of code parse out the filename and header from the input string */
  162. if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
  163. ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
  164. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  165. return -1;
  166. }
  167. parse = ast_strdupa(data);
  168. AST_STANDARD_APP_ARGS(args, parse);
  169. if (args.argc == 4)
  170. ast_app_parse_options(app_opts, &flags, opts, args.options);
  171. if (!ast_strlen_zero(args.filename)) {
  172. if (strstr(args.filename, "%d"))
  173. ast_set_flag(&flags, FLAG_HAS_PERCENT);
  174. ext = strrchr(args.filename, '.'); /* to support filename with a . in the filename, not format */
  175. if (!ext)
  176. ext = strchr(args.filename, ':');
  177. if (ext) {
  178. *ext = '\0';
  179. ext++;
  180. }
  181. }
  182. if (!ext) {
  183. ast_log(LOG_WARNING, "No extension specified to filename!\n");
  184. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  185. return -1;
  186. }
  187. if (args.silence) {
  188. if ((sscanf(args.silence, "%30d", &i) == 1) && (i > -1)) {
  189. silence = i * 1000;
  190. } else if (!ast_strlen_zero(args.silence)) {
  191. ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", args.silence);
  192. }
  193. }
  194. if (args.maxduration) {
  195. if ((sscanf(args.maxduration, "%30d", &i) == 1) && (i > -1))
  196. /* Convert duration to milliseconds */
  197. maxduration = i * 1000;
  198. else if (!ast_strlen_zero(args.maxduration))
  199. ast_log(LOG_WARNING, "'%s' is not a valid maximum duration\n", args.maxduration);
  200. }
  201. if (ast_test_flag(&flags, OPTION_STAR_TERMINATE))
  202. terminator = '*';
  203. if (ast_test_flag(&flags, OPTION_IGNORE_TERMINATE))
  204. terminator = '\0';
  205. /* done parsing */
  206. /* these are to allow the use of the %d in the config file for a wild card of sort to
  207. create a new file with the inputed name scheme */
  208. if (ast_test_flag(&flags, FLAG_HAS_PERCENT)) {
  209. AST_DECLARE_APP_ARGS(fname,
  210. AST_APP_ARG(piece)[100];
  211. );
  212. char *tmp2 = ast_strdupa(args.filename);
  213. char countstring[15];
  214. int idx;
  215. /* Separate each piece out by the format specifier */
  216. AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%');
  217. do {
  218. int tmplen;
  219. /* First piece has no leading percent, so it's copied verbatim */
  220. ast_copy_string(tmp, fname.piece[0], sizeof(tmp));
  221. tmplen = strlen(tmp);
  222. for (idx = 1; idx < fname.argc; idx++) {
  223. if (fname.piece[idx][0] == 'd') {
  224. /* Substitute the count */
  225. snprintf(countstring, sizeof(countstring), "%d", count);
  226. ast_copy_string(tmp + tmplen, countstring, sizeof(tmp) - tmplen);
  227. tmplen += strlen(countstring);
  228. } else if (tmplen + 2 < sizeof(tmp)) {
  229. /* Unknown format specifier - just copy it verbatim */
  230. tmp[tmplen++] = '%';
  231. tmp[tmplen++] = fname.piece[idx][0];
  232. }
  233. /* Copy the remaining portion of the piece */
  234. ast_copy_string(tmp + tmplen, &(fname.piece[idx][1]), sizeof(tmp) - tmplen);
  235. }
  236. count++;
  237. } while (ast_fileexists(tmp, ext, ast_channel_language(chan)) > 0);
  238. pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
  239. } else
  240. ast_copy_string(tmp, args.filename, sizeof(tmp));
  241. /* end of routine mentioned */
  242. if (ast_channel_state(chan) != AST_STATE_UP) {
  243. if (ast_test_flag(&flags, OPTION_SKIP)) {
  244. /* At the user's option, skip if the line is not up */
  245. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "SKIP");
  246. return 0;
  247. } else if (!ast_test_flag(&flags, OPTION_NOANSWER)) {
  248. /* Otherwise answer unless we're supposed to record while on-hook */
  249. res = ast_answer(chan);
  250. }
  251. }
  252. if (res) {
  253. ast_log(LOG_WARNING, "Could not answer channel '%s'\n", ast_channel_name(chan));
  254. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  255. goto out;
  256. }
  257. if (!ast_test_flag(&flags, OPTION_QUIET)) {
  258. /* Some code to play a nice little beep to signify the start of the record operation */
  259. res = ast_streamfile(chan, "beep", ast_channel_language(chan));
  260. if (!res) {
  261. res = ast_waitstream(chan, "");
  262. } else {
  263. ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", ast_channel_name(chan));
  264. }
  265. ast_stopstream(chan);
  266. }
  267. /* The end of beep code. Now the recording starts */
  268. if (silence > 0) {
  269. ast_format_copy(&rfmt, ast_channel_readformat(chan));
  270. res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
  271. if (res < 0) {
  272. ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
  273. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  274. return -1;
  275. }
  276. sildet = ast_dsp_new();
  277. if (!sildet) {
  278. ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
  279. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  280. return -1;
  281. }
  282. ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
  283. }
  284. /* Create the directory if it does not exist. */
  285. dir = ast_strdupa(tmp);
  286. if ((file = strrchr(dir, '/')))
  287. *file++ = '\0';
  288. ast_mkdir (dir, 0777);
  289. ioflags = ast_test_flag(&flags, OPTION_APPEND) ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
  290. s = ast_writefile(tmp, ext, NULL, ioflags, 0, AST_FILE_MODE);
  291. if (!s) {
  292. ast_log(LOG_WARNING, "Could not create file %s\n", args.filename);
  293. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  294. goto out;
  295. }
  296. if (ast_opt_transmit_silence)
  297. silgen = ast_channel_start_silence_generator(chan);
  298. /* Request a video update */
  299. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  300. if (maxduration <= 0)
  301. maxduration = -1;
  302. start = ast_tvnow();
  303. while ((ms = ast_remaining_ms(start, maxduration))) {
  304. ms = ast_waitfor(chan, ms);
  305. if (ms < 0) {
  306. break;
  307. }
  308. if (maxduration > 0 && ms == 0) {
  309. break;
  310. }
  311. f = ast_read(chan);
  312. if (!f) {
  313. res = -1;
  314. break;
  315. }
  316. if (f->frametype == AST_FRAME_VOICE) {
  317. res = ast_writestream(s, f);
  318. if (res) {
  319. ast_log(LOG_WARNING, "Problem writing frame\n");
  320. ast_frfree(f);
  321. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  322. break;
  323. }
  324. if (silence > 0) {
  325. dspsilence = 0;
  326. ast_dsp_silence(sildet, f, &dspsilence);
  327. if (dspsilence) {
  328. totalsilence = dspsilence;
  329. } else {
  330. totalsilence = 0;
  331. }
  332. if (totalsilence > silence) {
  333. /* Ended happily with silence */
  334. ast_frfree(f);
  335. gotsilence = 1;
  336. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "SILENCE");
  337. break;
  338. }
  339. }
  340. } else if (f->frametype == AST_FRAME_VIDEO) {
  341. res = ast_writestream(s, f);
  342. if (res) {
  343. ast_log(LOG_WARNING, "Problem writing frame\n");
  344. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
  345. ast_frfree(f);
  346. break;
  347. }
  348. } else if ((f->frametype == AST_FRAME_DTMF) &&
  349. ((f->subclass.integer == terminator) ||
  350. (ast_test_flag(&flags, OPTION_ANY_TERMINATE)))) {
  351. ast_frfree(f);
  352. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "DTMF");
  353. break;
  354. }
  355. ast_frfree(f);
  356. }
  357. if (maxduration > 0 && !ms) {
  358. gottimeout = 1;
  359. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "TIMEOUT");
  360. }
  361. if (!f) {
  362. ast_debug(1, "Got hangup\n");
  363. res = -1;
  364. pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "HANGUP");
  365. if (!ast_test_flag(&flags, OPTION_KEEP)) {
  366. ast_filedelete(args.filename, NULL);
  367. }
  368. }
  369. if (gotsilence) {
  370. ast_stream_rewind(s, silence - 1000);
  371. ast_truncstream(s);
  372. } else if (!gottimeout && f) {
  373. /*
  374. * Strip off the last 1/4 second of it, if we didn't end because of a timeout,
  375. * or a hangup. This must mean we ended because of a DTMF tone and while this
  376. * 1/4 second stripping is very old code the most likely explanation is that it
  377. * relates to stripping a partial DTMF tone.
  378. */
  379. ast_stream_rewind(s, 250);
  380. ast_truncstream(s);
  381. }
  382. ast_closestream(s);
  383. if (silgen)
  384. ast_channel_stop_silence_generator(chan, silgen);
  385. out:
  386. if ((silence > 0) && rfmt.id) {
  387. res = ast_set_read_format(chan, &rfmt);
  388. if (res) {
  389. ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(chan));
  390. }
  391. }
  392. if (sildet) {
  393. ast_dsp_free(sildet);
  394. }
  395. return res;
  396. }
  397. static int unload_module(void)
  398. {
  399. return ast_unregister_application(app);
  400. }
  401. static int load_module(void)
  402. {
  403. return ast_register_application_xml(app, record_exec);
  404. }
  405. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial Record Application");