app_record.c 11 KB

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