app_record.c 13 KB

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