res_monitor.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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. *
  20. * \brief PBX channel monitoring
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <depend type="module">func_periodic_hook</depend>
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <sys/stat.h>
  31. #include <libgen.h>
  32. #include "asterisk/paths.h" /* use ast_config_AST_MONITOR_DIR */
  33. #include "asterisk/lock.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/file.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/cli.h"
  39. #include "asterisk/manager.h"
  40. #include "asterisk/stasis.h"
  41. #include "asterisk/stasis_channels.h"
  42. #define AST_API_MODULE
  43. #include "asterisk/monitor.h"
  44. #undef AST_API_MODULE
  45. #include "asterisk/app.h"
  46. #include "asterisk/utils.h"
  47. #include "asterisk/config.h"
  48. #include "asterisk/options.h"
  49. #include "asterisk/beep.h"
  50. /*** DOCUMENTATION
  51. <application name="Monitor" language="en_US">
  52. <synopsis>
  53. Monitor a channel.
  54. </synopsis>
  55. <syntax>
  56. <parameter name="file_format" argsep=":">
  57. <argument name="file_format" required="true">
  58. <para>Optional. If not set, defaults to <literal>wav</literal></para>
  59. </argument>
  60. <argument name="urlbase" />
  61. </parameter>
  62. <parameter name="fname_base">
  63. <para>If set, changes the filename used to the one specified.</para>
  64. </parameter>
  65. <parameter name="options">
  66. <optionlist>
  67. <option name="m">
  68. <para>When the recording ends mix the two leg files into one and
  69. delete the two leg files. If the variable <variable>MONITOR_EXEC</variable>
  70. is set, the application referenced in it will be executed instead of
  71. soxmix/sox and the raw leg files will NOT be deleted automatically.
  72. soxmix/sox or <variable>MONITOR_EXEC</variable> is handed 3 arguments,
  73. the two leg files and a target mixed file name which is the same as
  74. the leg file names only without the in/out designator.</para>
  75. <para>If <variable>MONITOR_EXEC_ARGS</variable> is set, the contents
  76. will be passed on as additional arguments to <variable>MONITOR_EXEC</variable>.
  77. Both <variable>MONITOR_EXEC</variable> and the Mix flag can be set from the
  78. administrator interface.</para>
  79. <warning><para>Do not use untrusted strings such as
  80. <variable>CALLERID(num)</variable> or <variable>CALLERID(name)</variable>
  81. as part of <variable>MONITOR_EXEC</variable> or
  82. <variable>MONITOR_EXEC_ARGS</variable>. You risk a command injection
  83. attack executing arbitrary commands if the untrusted strings aren't
  84. filtered to remove dangerous characters. See function
  85. <variable>FILTER()</variable>.</para></warning>
  86. </option>
  87. <option name="b">
  88. <para>Don't begin recording unless a call is bridged to another channel.</para>
  89. </option>
  90. <option name="B">
  91. <para>Play a periodic beep while this call is being recorded.</para>
  92. <argument name="interval"><para>Interval, in seconds. Default is 15.</para></argument>
  93. </option>
  94. <option name="i">
  95. <para>Skip recording of input stream (disables <literal>m</literal> option).</para>
  96. </option>
  97. <option name="o">
  98. <para>Skip recording of output stream (disables <literal>m</literal> option).</para>
  99. </option>
  100. </optionlist>
  101. </parameter>
  102. </syntax>
  103. <description>
  104. <para>Used to start monitoring a channel. The channel's input and output
  105. voice packets are logged to files until the channel hangs up or
  106. monitoring is stopped by the StopMonitor application.</para>
  107. <para>By default, files are stored to <filename>/var/spool/asterisk/monitor/</filename>.
  108. Returns <literal>-1</literal> if monitor files can't be opened or if the channel is
  109. already monitored, otherwise <literal>0</literal>.</para>
  110. </description>
  111. <see-also>
  112. <ref type="application">StopMonitor</ref>
  113. </see-also>
  114. </application>
  115. <application name="StopMonitor" language="en_US">
  116. <synopsis>
  117. Stop monitoring a channel.
  118. </synopsis>
  119. <syntax />
  120. <description>
  121. <para>Stops monitoring a channel. Has no effect if the channel is not monitored.</para>
  122. </description>
  123. </application>
  124. <application name="ChangeMonitor" language="en_US">
  125. <synopsis>
  126. Change monitoring filename of a channel.
  127. </synopsis>
  128. <syntax>
  129. <parameter name="filename_base" required="true">
  130. <para>The new filename base to use for monitoring this channel.</para>
  131. </parameter>
  132. </syntax>
  133. <description>
  134. <para>Changes monitoring filename of a channel. Has no effect if the
  135. channel is not monitored.</para>
  136. </description>
  137. </application>
  138. <application name="PauseMonitor" language="en_US">
  139. <synopsis>
  140. Pause monitoring of a channel.
  141. </synopsis>
  142. <syntax />
  143. <description>
  144. <para>Pauses monitoring of a channel until it is re-enabled by a call to UnpauseMonitor.</para>
  145. </description>
  146. <see-also>
  147. <ref type="application">UnpauseMonitor</ref>
  148. </see-also>
  149. </application>
  150. <application name="UnpauseMonitor" language="en_US">
  151. <synopsis>
  152. Unpause monitoring of a channel.
  153. </synopsis>
  154. <syntax />
  155. <description>
  156. <para>Unpauses monitoring of a channel on which monitoring had
  157. previously been paused with PauseMonitor.</para>
  158. </description>
  159. <see-also>
  160. <ref type="application">PauseMonitor</ref>
  161. </see-also>
  162. </application>
  163. <manager name="Monitor" language="en_US">
  164. <synopsis>
  165. Monitor a channel.
  166. </synopsis>
  167. <syntax>
  168. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  169. <parameter name="Channel" required="true">
  170. <para>Used to specify the channel to record.</para>
  171. </parameter>
  172. <parameter name="File">
  173. <para>Is the name of the file created in the monitor spool directory.
  174. Defaults to the same name as the channel (with slashes replaced with dashes).</para>
  175. </parameter>
  176. <parameter name="Format">
  177. <para>Is the audio recording format. Defaults to <literal>wav</literal>.</para>
  178. </parameter>
  179. <parameter name="Mix">
  180. <para>Boolean parameter as to whether to mix the input and output channels
  181. together after the recording is finished.</para>
  182. </parameter>
  183. </syntax>
  184. <description>
  185. <para>This action may be used to record the audio on a
  186. specified channel.</para>
  187. </description>
  188. </manager>
  189. <manager name="StopMonitor" language="en_US">
  190. <synopsis>
  191. Stop monitoring a channel.
  192. </synopsis>
  193. <syntax>
  194. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  195. <parameter name="Channel" required="true">
  196. <para>The name of the channel monitored.</para>
  197. </parameter>
  198. </syntax>
  199. <description>
  200. <para>This action may be used to end a previously started 'Monitor' action.</para>
  201. </description>
  202. </manager>
  203. <manager name="ChangeMonitor" language="en_US">
  204. <synopsis>
  205. Change monitoring filename of a channel.
  206. </synopsis>
  207. <syntax>
  208. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  209. <parameter name="Channel" required="true">
  210. <para>Used to specify the channel to record.</para>
  211. </parameter>
  212. <parameter name="File" required="true">
  213. <para>Is the new name of the file created in the
  214. monitor spool directory.</para>
  215. </parameter>
  216. </syntax>
  217. <description>
  218. <para>This action may be used to change the file
  219. started by a previous 'Monitor' action.</para>
  220. </description>
  221. </manager>
  222. <manager name="PauseMonitor" language="en_US">
  223. <synopsis>
  224. Pause monitoring of a channel.
  225. </synopsis>
  226. <syntax>
  227. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  228. <parameter name="Channel" required="true">
  229. <para>Used to specify the channel to record.</para>
  230. </parameter>
  231. </syntax>
  232. <description>
  233. <para>This action may be used to temporarily stop the
  234. recording of a channel.</para>
  235. </description>
  236. </manager>
  237. <manager name="UnpauseMonitor" language="en_US">
  238. <synopsis>
  239. Unpause monitoring of a channel.
  240. </synopsis>
  241. <syntax>
  242. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  243. <parameter name="Channel" required="true">
  244. <para>Used to specify the channel to record.</para>
  245. </parameter>
  246. </syntax>
  247. <description>
  248. <para>This action may be used to re-enable recording
  249. of a channel after calling PauseMonitor.</para>
  250. </description>
  251. </manager>
  252. ***/
  253. AST_MUTEX_DEFINE_STATIC(monitorlock);
  254. #define LOCK_IF_NEEDED(lock, needed) do { \
  255. if (needed) \
  256. ast_channel_lock(lock); \
  257. } while(0)
  258. #define UNLOCK_IF_NEEDED(lock, needed) do { \
  259. if (needed) \
  260. ast_channel_unlock(lock); \
  261. } while (0)
  262. static unsigned long seq = 0;
  263. /*!
  264. * \brief Change state of monitored channel
  265. * \param chan
  266. * \param state monitor state
  267. * \retval 0 on success.
  268. * \retval -1 on failure.
  269. */
  270. static int ast_monitor_set_state(struct ast_channel *chan, int state)
  271. {
  272. LOCK_IF_NEEDED(chan, 1);
  273. if (!ast_channel_monitor(chan)) {
  274. UNLOCK_IF_NEEDED(chan, 1);
  275. return -1;
  276. }
  277. ast_channel_monitor(chan)->state = state;
  278. UNLOCK_IF_NEEDED(chan, 1);
  279. return 0;
  280. }
  281. /*! \brief Start monitoring a channel
  282. * \param chan ast_channel struct to record
  283. * \param format_spec file format to use for recording
  284. * \param fname_base filename base to record to
  285. * \param need_lock whether to lock the channel mutex
  286. * \param stream_action whether to record the input and/or output streams. X_REC_IN | X_REC_OUT is most often used
  287. * Creates the file to record, if no format is specified it assumes WAV
  288. * It also sets channel variable __MONITORED=yes
  289. * \retval 0 on success
  290. * \retval -1 on failure
  291. */
  292. int AST_OPTIONAL_API_NAME(ast_monitor_start)(struct ast_channel *chan, const char *format_spec,
  293. const char *fname_base, int need_lock, int stream_action,
  294. const char *beep_id)
  295. {
  296. int res = 0;
  297. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  298. LOCK_IF_NEEDED(chan, need_lock);
  299. if (!(ast_channel_monitor(chan))) {
  300. struct ast_channel_monitor *monitor;
  301. char *channel_name, *p;
  302. /* Create monitoring directory if needed */
  303. ast_mkdir(ast_config_AST_MONITOR_DIR, 0777);
  304. if (!(monitor = ast_calloc(1, sizeof(*monitor)))) {
  305. UNLOCK_IF_NEEDED(chan, need_lock);
  306. return -1;
  307. }
  308. if (!ast_strlen_zero(beep_id)) {
  309. ast_copy_string(monitor->beep_id, beep_id, sizeof(monitor->beep_id));
  310. }
  311. /* Determine file names */
  312. if (!ast_strlen_zero(fname_base)) {
  313. int directory = strchr(fname_base, '/') ? 1 : 0;
  314. const char *absolute = *fname_base == '/' ? "" : ast_config_AST_MONITOR_DIR;
  315. const char *absolute_suffix = *fname_base == '/' ? "" : "/";
  316. snprintf(monitor->read_filename, FILENAME_MAX, "%s%s%s-in",
  317. absolute, absolute_suffix, fname_base);
  318. snprintf(monitor->write_filename, FILENAME_MAX, "%s%s%s-out",
  319. absolute, absolute_suffix, fname_base);
  320. snprintf(monitor->filename_base, FILENAME_MAX, "%s%s%s",
  321. absolute, absolute_suffix, fname_base);
  322. /* try creating the directory just in case it doesn't exist */
  323. if (directory) {
  324. char *name = ast_strdupa(monitor->filename_base);
  325. ast_mkdir(dirname(name), 0777);
  326. }
  327. } else {
  328. ast_mutex_lock(&monitorlock);
  329. snprintf(monitor->read_filename, FILENAME_MAX, "%s/audio-in-%lu",
  330. ast_config_AST_MONITOR_DIR, seq);
  331. snprintf(monitor->write_filename, FILENAME_MAX, "%s/audio-out-%lu",
  332. ast_config_AST_MONITOR_DIR, seq);
  333. seq++;
  334. ast_mutex_unlock(&monitorlock);
  335. /* Replace all '/' chars from the channel name with '-' chars. */
  336. channel_name = ast_strdupa(ast_channel_name(chan));
  337. for (p = channel_name; (p = strchr(p, '/')); ) {
  338. *p = '-';
  339. }
  340. snprintf(monitor->filename_base, FILENAME_MAX, "%s/%d-%s",
  341. ast_config_AST_MONITOR_DIR, (int)time(NULL), channel_name);
  342. monitor->filename_changed = 1;
  343. }
  344. monitor->stop = ast_monitor_stop;
  345. /* Determine file format */
  346. if (!ast_strlen_zero(format_spec)) {
  347. monitor->format = ast_strdup(format_spec);
  348. } else {
  349. monitor->format = ast_strdup("wav");
  350. }
  351. /* open files */
  352. if (stream_action & X_REC_IN) {
  353. if (ast_fileexists(monitor->read_filename, NULL, NULL) > 0)
  354. ast_filedelete(monitor->read_filename, NULL);
  355. if (!(monitor->read_stream = ast_writefile(monitor->read_filename,
  356. monitor->format, NULL,
  357. O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
  358. ast_log(LOG_WARNING, "Could not create file %s\n",
  359. monitor->read_filename);
  360. ast_free(monitor);
  361. UNLOCK_IF_NEEDED(chan, need_lock);
  362. return -1;
  363. }
  364. } else
  365. monitor->read_stream = NULL;
  366. if (stream_action & X_REC_OUT) {
  367. if (ast_fileexists(monitor->write_filename, NULL, NULL) > 0) {
  368. ast_filedelete(monitor->write_filename, NULL);
  369. }
  370. if (!(monitor->write_stream = ast_writefile(monitor->write_filename,
  371. monitor->format, NULL,
  372. O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
  373. ast_log(LOG_WARNING, "Could not create file %s\n",
  374. monitor->write_filename);
  375. if (monitor->read_stream) {
  376. ast_closestream(monitor->read_stream);
  377. }
  378. ast_free(monitor);
  379. UNLOCK_IF_NEEDED(chan, need_lock);
  380. return -1;
  381. }
  382. } else
  383. monitor->write_stream = NULL;
  384. ast_channel_insmpl_set(chan, 0);
  385. ast_channel_outsmpl_set(chan, 0);
  386. ast_channel_monitor_set(chan, monitor);
  387. ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
  388. /* so we know this call has been monitored in case we need to bill for it or something */
  389. pbx_builtin_setvar_helper(chan, "__MONITORED","true");
  390. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan),
  391. ast_channel_monitor_start_type(),
  392. NULL);
  393. if (message) {
  394. stasis_publish(ast_channel_topic(chan), message);
  395. }
  396. } else {
  397. ast_debug(1,"Cannot start monitoring %s, already monitored\n", ast_channel_name(chan));
  398. res = -1;
  399. }
  400. UNLOCK_IF_NEEDED(chan, need_lock);
  401. return res;
  402. }
  403. /*!
  404. * \brief Get audio format.
  405. * \param format recording format.
  406. * The file format extensions that Asterisk uses are not all the same as that
  407. * which soxmix expects. This function ensures that the format used as the
  408. * extension on the filename is something soxmix will understand.
  409. */
  410. static const char *get_soxmix_format(const char *format)
  411. {
  412. const char *res = format;
  413. if (!strcasecmp(format,"ulaw"))
  414. res = "ul";
  415. if (!strcasecmp(format,"alaw"))
  416. res = "al";
  417. return res;
  418. }
  419. /*!
  420. * \brief Stop monitoring channel
  421. * \param chan
  422. * \param need_lock
  423. * Stop the recording, close any open streams, mix in/out channels if required
  424. * \return Always 0
  425. */
  426. int AST_OPTIONAL_API_NAME(ast_monitor_stop)(struct ast_channel *chan, int need_lock)
  427. {
  428. int delfiles = 0;
  429. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  430. LOCK_IF_NEEDED(chan, need_lock);
  431. if (ast_channel_monitor(chan)) {
  432. RAII_VAR(struct ast_str *, tmp, ast_str_create(1024), ast_free);
  433. if (ast_channel_monitor(chan)->read_stream) {
  434. ast_closestream(ast_channel_monitor(chan)->read_stream);
  435. }
  436. if (ast_channel_monitor(chan)->write_stream) {
  437. ast_closestream(ast_channel_monitor(chan)->write_stream);
  438. }
  439. if (tmp && ast_channel_monitor(chan)->filename_changed && !ast_strlen_zero(ast_channel_monitor(chan)->filename_base)) {
  440. if (ast_fileexists(ast_channel_monitor(chan)->read_filename,NULL,NULL) > 0) {
  441. ast_str_set(&tmp, 0, "%s-in", ast_channel_monitor(chan)->filename_base);
  442. if (ast_fileexists(ast_str_buffer(tmp), NULL, NULL) > 0) {
  443. ast_filedelete(ast_str_buffer(tmp), NULL);
  444. }
  445. ast_filerename(ast_channel_monitor(chan)->read_filename, ast_str_buffer(tmp), ast_channel_monitor(chan)->format);
  446. } else {
  447. ast_log(LOG_WARNING, "File %s not found\n", ast_channel_monitor(chan)->read_filename);
  448. }
  449. if (tmp && ast_fileexists(ast_channel_monitor(chan)->write_filename,NULL,NULL) > 0) {
  450. ast_str_set(&tmp, 0, "%s-out", ast_channel_monitor(chan)->filename_base);
  451. if (ast_fileexists(ast_str_buffer(tmp), NULL, NULL) > 0) {
  452. ast_filedelete(ast_str_buffer(tmp), NULL);
  453. }
  454. ast_filerename(ast_channel_monitor(chan)->write_filename, ast_str_buffer(tmp), ast_channel_monitor(chan)->format);
  455. } else {
  456. ast_log(LOG_WARNING, "File %s not found\n", ast_channel_monitor(chan)->write_filename);
  457. }
  458. }
  459. if (tmp && ast_channel_monitor(chan)->joinfiles && !ast_strlen_zero(ast_channel_monitor(chan)->filename_base)) {
  460. const char *format = !strcasecmp(ast_channel_monitor(chan)->format,"wav49") ? "WAV" : ast_channel_monitor(chan)->format;
  461. char *fname_base = ast_channel_monitor(chan)->filename_base;
  462. const char *execute, *execute_args;
  463. /* at this point, fname_base really is the full path */
  464. /* Set the execute application */
  465. execute = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC");
  466. if (ast_strlen_zero(execute)) {
  467. #ifdef HAVE_SOXMIX
  468. execute = "nice -n 19 soxmix";
  469. #else
  470. execute = "nice -n 19 sox -m";
  471. #endif
  472. format = get_soxmix_format(format);
  473. delfiles = 1;
  474. }
  475. execute_args = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC_ARGS");
  476. if (ast_strlen_zero(execute_args)) {
  477. execute_args = "";
  478. }
  479. ast_str_set(&tmp, 0, delfiles ? "( " : "");
  480. ast_str_append(&tmp, 0, "%s \"%s-in.%s\" \"%s-out.%s\" \"%s.%s\" %s &",
  481. execute, fname_base, format, fname_base, format, fname_base, format,execute_args);
  482. if (delfiles) {
  483. /* remove legs when done mixing */
  484. ast_str_append(&tmp, 0, "& rm -f \"%s-\"* ) &", fname_base);
  485. }
  486. ast_debug(1,"monitor executing %s\n", ast_str_buffer(tmp));
  487. if (ast_safe_system(ast_str_buffer(tmp)) == -1)
  488. ast_log(LOG_WARNING, "Execute of %s failed.\n", ast_str_buffer(tmp));
  489. }
  490. if (!ast_strlen_zero(ast_channel_monitor(chan)->beep_id)) {
  491. ast_beep_stop(chan, ast_channel_monitor(chan)->beep_id);
  492. }
  493. ast_free(ast_channel_monitor(chan)->format);
  494. ast_free(ast_channel_monitor(chan));
  495. ast_channel_monitor_set(chan, NULL);
  496. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan),
  497. ast_channel_monitor_stop_type(),
  498. NULL);
  499. if (message) {
  500. stasis_publish(ast_channel_topic(chan), message);
  501. }
  502. pbx_builtin_setvar_helper(chan, "MONITORED", NULL);
  503. }
  504. pbx_builtin_setvar_helper(chan, "AUTO_MONITOR", NULL);
  505. UNLOCK_IF_NEEDED(chan, need_lock);
  506. return 0;
  507. }
  508. /*! \brief Pause monitoring of channel */
  509. int AST_OPTIONAL_API_NAME(ast_monitor_pause)(struct ast_channel *chan)
  510. {
  511. return ast_monitor_set_state(chan, AST_MONITOR_PAUSED);
  512. }
  513. /*! \brief Unpause monitoring of channel */
  514. int AST_OPTIONAL_API_NAME(ast_monitor_unpause)(struct ast_channel *chan)
  515. {
  516. return ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
  517. }
  518. /*! \brief Wrapper for ast_monitor_pause */
  519. static int pause_monitor_exec(struct ast_channel *chan, const char *data)
  520. {
  521. return ast_monitor_pause(chan);
  522. }
  523. /*! \brief Wrapper for ast_monitor_unpause */
  524. static int unpause_monitor_exec(struct ast_channel *chan, const char *data)
  525. {
  526. return ast_monitor_unpause(chan);
  527. }
  528. /*!
  529. * \brief Change monitored filename of channel
  530. * \param chan
  531. * \param fname_base new filename
  532. * \param need_lock
  533. * \retval 0 on success.
  534. * \retval -1 on failure.
  535. */
  536. int AST_OPTIONAL_API_NAME(ast_monitor_change_fname)(struct ast_channel *chan, const char *fname_base, int need_lock)
  537. {
  538. if (ast_strlen_zero(fname_base)) {
  539. ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to null\n", ast_channel_name(chan));
  540. return -1;
  541. }
  542. LOCK_IF_NEEDED(chan, need_lock);
  543. if (ast_channel_monitor(chan)) {
  544. int directory = strchr(fname_base, '/') ? 1 : 0;
  545. const char *absolute = *fname_base == '/' ? "" : ast_config_AST_MONITOR_DIR;
  546. const char *absolute_suffix = *fname_base == '/' ? "" : "/";
  547. char tmpstring[sizeof(ast_channel_monitor(chan)->filename_base)] = "";
  548. int i, fd[2] = { -1, -1 }, doexit = 0;
  549. /* before continuing, see if we're trying to rename the file to itself... */
  550. snprintf(tmpstring, sizeof(tmpstring), "%s%s%s", absolute, absolute_suffix, fname_base);
  551. /* try creating the directory just in case it doesn't exist */
  552. if (directory) {
  553. char *name = ast_strdupa(tmpstring);
  554. ast_mkdir(dirname(name), 0777);
  555. }
  556. /*!
  557. * \note We cannot just compare filenames, due to symlinks, relative
  558. * paths, and other possible filesystem issues. We could use
  559. * realpath(3), but its use is discouraged. However, if we try to
  560. * create the same file from two different paths, the second will
  561. * fail, and so we have our notification that the filenames point to
  562. * the same path.
  563. *
  564. * Remember, also, that we're using the basename of the file (i.e.
  565. * the file without the format suffix), so it does not already exist
  566. * and we aren't interfering with the recording itself.
  567. */
  568. ast_debug(2, "comparing tmpstring %s to filename_base %s\n", tmpstring, ast_channel_monitor(chan)->filename_base);
  569. if ((fd[0] = open(tmpstring, O_CREAT | O_WRONLY, 0644)) < 0 ||
  570. (fd[1] = open(ast_channel_monitor(chan)->filename_base, O_CREAT | O_EXCL | O_WRONLY, 0644)) < 0) {
  571. if (fd[0] < 0) {
  572. ast_log(LOG_ERROR, "Unable to compare filenames: %s\n", strerror(errno));
  573. } else {
  574. ast_debug(2, "No need to rename monitor filename to itself\n");
  575. }
  576. doexit = 1;
  577. }
  578. /* Cleanup temporary files */
  579. for (i = 0; i < 2; i++) {
  580. if (fd[i] >= 0) {
  581. while (close(fd[i]) < 0 && errno == EINTR);
  582. }
  583. }
  584. unlink(tmpstring);
  585. /* if previous monitor file existed in a subdirectory, the directory will not be removed */
  586. unlink(ast_channel_monitor(chan)->filename_base);
  587. if (doexit) {
  588. UNLOCK_IF_NEEDED(chan, need_lock);
  589. return 0;
  590. }
  591. ast_copy_string(ast_channel_monitor(chan)->filename_base, tmpstring, sizeof(ast_channel_monitor(chan)->filename_base));
  592. ast_channel_monitor(chan)->filename_changed = 1;
  593. } else {
  594. ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to %s, monitoring not started\n", ast_channel_name(chan), fname_base);
  595. }
  596. UNLOCK_IF_NEEDED(chan, need_lock);
  597. return 0;
  598. }
  599. enum {
  600. MON_FLAG_BRIDGED = (1 << 0),
  601. MON_FLAG_MIX = (1 << 1),
  602. MON_FLAG_DROP_IN = (1 << 2),
  603. MON_FLAG_DROP_OUT = (1 << 3),
  604. MON_FLAG_BEEP = (1 << 4),
  605. };
  606. enum {
  607. OPT_ARG_BEEP_INTERVAL,
  608. OPT_ARG_ARRAY_SIZE, /* Always last element of the enum */
  609. };
  610. AST_APP_OPTIONS(monitor_opts, {
  611. AST_APP_OPTION('b', MON_FLAG_BRIDGED),
  612. AST_APP_OPTION('m', MON_FLAG_MIX),
  613. AST_APP_OPTION('i', MON_FLAG_DROP_IN),
  614. AST_APP_OPTION('o', MON_FLAG_DROP_OUT),
  615. AST_APP_OPTION_ARG('B', MON_FLAG_BEEP, OPT_ARG_BEEP_INTERVAL),
  616. });
  617. /*!
  618. * \brief Start monitor
  619. * \param chan
  620. * \param data arguments passed fname|options
  621. * \retval 0 on success.
  622. * \retval -1 on failure.
  623. */
  624. static int start_monitor_exec(struct ast_channel *chan, const char *data)
  625. {
  626. char *arg;
  627. char *options;
  628. char *delay;
  629. char *urlprefix = NULL;
  630. char tmp[256];
  631. int stream_action = X_REC_IN | X_REC_OUT;
  632. int joinfiles = 0;
  633. int res = 0;
  634. char *parse;
  635. struct ast_flags flags = { 0 };
  636. char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
  637. char beep_id[64] = "";
  638. AST_DECLARE_APP_ARGS(args,
  639. AST_APP_ARG(format);
  640. AST_APP_ARG(fname_base);
  641. AST_APP_ARG(options);
  642. );
  643. /* Parse arguments. */
  644. if (ast_strlen_zero(data)) {
  645. ast_log(LOG_ERROR, "Monitor requires an argument\n");
  646. return 0;
  647. }
  648. parse = ast_strdupa(data);
  649. AST_STANDARD_APP_ARGS(args, parse);
  650. if (!ast_strlen_zero(args.options)) {
  651. ast_app_parse_options(monitor_opts, &flags, opts, args.options);
  652. if (ast_test_flag(&flags, MON_FLAG_MIX)) {
  653. stream_action |= X_JOIN;
  654. }
  655. if (ast_test_flag(&flags, MON_FLAG_DROP_IN)) {
  656. stream_action &= ~X_REC_IN;
  657. }
  658. if (ast_test_flag(&flags, MON_FLAG_DROP_OUT)) {
  659. stream_action &= ~X_REC_OUT;
  660. }
  661. if (ast_test_flag(&flags, MON_FLAG_BEEP)) {
  662. const char *interval_str = S_OR(opts[OPT_ARG_BEEP_INTERVAL], "15");
  663. unsigned int interval = 15;
  664. if (sscanf(interval_str, "%30u", &interval) != 1) {
  665. ast_log(LOG_WARNING, "Invalid interval '%s' for periodic beep. Using default of %u\n",
  666. interval_str, interval);
  667. }
  668. if (ast_beep_start(chan, interval, beep_id, sizeof(beep_id))) {
  669. ast_log(LOG_WARNING, "Unable to enable periodic beep, please ensure func_periodic_hook is loaded.\n");
  670. return -1;
  671. }
  672. }
  673. }
  674. arg = strchr(args.format, ':');
  675. if (arg) {
  676. *arg++ = 0;
  677. urlprefix = arg;
  678. }
  679. if (!ast_strlen_zero(urlprefix) && !ast_strlen_zero(args.fname_base)) {
  680. snprintf(tmp, sizeof(tmp), "%s/%s.%s", urlprefix, args.fname_base,
  681. ((strcmp(args.format, "gsm")) ? "wav" : "gsm"));
  682. ast_channel_lock(chan);
  683. ast_cdr_setuserfield(ast_channel_name(chan), tmp);
  684. ast_channel_unlock(chan);
  685. }
  686. if (ast_test_flag(&flags, MON_FLAG_BRIDGED)) {
  687. /* We must remove the "b" option if listed. In principle none of
  688. the following could give NULL results, but we check just to
  689. be pedantic. Reconstructing with checks for 'm' option does not
  690. work if we end up adding more options than 'm' in the future. */
  691. delay = ast_strdupa(data);
  692. options = strrchr(delay, ',');
  693. if (options) {
  694. arg = strchr(options, 'b');
  695. if (arg) {
  696. *arg = 'X';
  697. pbx_builtin_setvar_helper(chan,"AUTO_MONITOR", delay);
  698. }
  699. }
  700. return 0;
  701. }
  702. res = ast_monitor_start(chan, args.format, args.fname_base, 1, stream_action, beep_id);
  703. if (res < 0)
  704. res = ast_monitor_change_fname(chan, args.fname_base, 1);
  705. if (stream_action & X_JOIN) {
  706. if ((stream_action & X_REC_IN) && (stream_action & X_REC_OUT))
  707. joinfiles = 1;
  708. else
  709. ast_log(LOG_WARNING, "Won't mix streams unless both input and output streams are recorded\n");
  710. }
  711. ast_monitor_setjoinfiles(chan, joinfiles);
  712. return res;
  713. }
  714. /*! \brief Wrapper function \see ast_monitor_stop */
  715. static int stop_monitor_exec(struct ast_channel *chan, const char *data)
  716. {
  717. return ast_monitor_stop(chan, 1);
  718. }
  719. /*! \brief Wrapper function \see ast_monitor_change_fname */
  720. static int change_monitor_exec(struct ast_channel *chan, const char *data)
  721. {
  722. return ast_monitor_change_fname(chan, data, 1);
  723. }
  724. /*! \brief Start monitoring a channel by manager connection */
  725. static int start_monitor_action(struct mansession *s, const struct message *m)
  726. {
  727. struct ast_channel *c = NULL;
  728. const char *name = astman_get_header(m, "Channel");
  729. const char *fname = astman_get_header(m, "File");
  730. const char *format = astman_get_header(m, "Format");
  731. const char *mix = astman_get_header(m, "Mix");
  732. char *d;
  733. if (ast_strlen_zero(name)) {
  734. astman_send_error(s, m, "No channel specified");
  735. return AMI_SUCCESS;
  736. }
  737. if (!(c = ast_channel_get_by_name(name))) {
  738. astman_send_error(s, m, "No such channel");
  739. return AMI_SUCCESS;
  740. }
  741. if (ast_strlen_zero(fname)) {
  742. /* No filename specified, default to the channel name. */
  743. ast_channel_lock(c);
  744. fname = ast_strdupa(ast_channel_name(c));
  745. ast_channel_unlock(c);
  746. /* Replace all '/' chars from the channel name with '-' chars. */
  747. for (d = (char *) fname; (d = strchr(d, '/')); ) {
  748. *d = '-';
  749. }
  750. }
  751. if (ast_monitor_start(c, format, fname, 1, X_REC_IN | X_REC_OUT, NULL)) {
  752. if (ast_monitor_change_fname(c, fname, 1)) {
  753. astman_send_error(s, m, "Could not start monitoring channel");
  754. c = ast_channel_unref(c);
  755. return AMI_SUCCESS;
  756. }
  757. }
  758. if (ast_true(mix)) {
  759. ast_channel_lock(c);
  760. ast_monitor_setjoinfiles(c, 1);
  761. ast_channel_unlock(c);
  762. }
  763. c = ast_channel_unref(c);
  764. astman_send_ack(s, m, "Started monitoring channel");
  765. return AMI_SUCCESS;
  766. }
  767. /*! \brief Stop monitoring a channel by manager connection */
  768. static int stop_monitor_action(struct mansession *s, const struct message *m)
  769. {
  770. struct ast_channel *c = NULL;
  771. const char *name = astman_get_header(m, "Channel");
  772. int res;
  773. if (ast_strlen_zero(name)) {
  774. astman_send_error(s, m, "No channel specified");
  775. return AMI_SUCCESS;
  776. }
  777. if (!(c = ast_channel_get_by_name(name))) {
  778. astman_send_error(s, m, "No such channel");
  779. return AMI_SUCCESS;
  780. }
  781. res = ast_monitor_stop(c, 1);
  782. c = ast_channel_unref(c);
  783. if (res) {
  784. astman_send_error(s, m, "Could not stop monitoring channel");
  785. return AMI_SUCCESS;
  786. }
  787. astman_send_ack(s, m, "Stopped monitoring channel");
  788. return AMI_SUCCESS;
  789. }
  790. /*! \brief Change filename of a monitored channel by manager connection */
  791. static int change_monitor_action(struct mansession *s, const struct message *m)
  792. {
  793. struct ast_channel *c = NULL;
  794. const char *name = astman_get_header(m, "Channel");
  795. const char *fname = astman_get_header(m, "File");
  796. if (ast_strlen_zero(name)) {
  797. astman_send_error(s, m, "No channel specified");
  798. return AMI_SUCCESS;
  799. }
  800. if (ast_strlen_zero(fname)) {
  801. astman_send_error(s, m, "No filename specified");
  802. return AMI_SUCCESS;
  803. }
  804. if (!(c = ast_channel_get_by_name(name))) {
  805. astman_send_error(s, m, "No such channel");
  806. return AMI_SUCCESS;
  807. }
  808. if (ast_monitor_change_fname(c, fname, 1)) {
  809. c = ast_channel_unref(c);
  810. astman_send_error(s, m, "Could not change monitored filename of channel");
  811. return AMI_SUCCESS;
  812. }
  813. c = ast_channel_unref(c);
  814. astman_send_ack(s, m, "Changed monitor filename");
  815. return AMI_SUCCESS;
  816. }
  817. void AST_OPTIONAL_API_NAME(ast_monitor_setjoinfiles)(struct ast_channel *chan, int turnon)
  818. {
  819. if (ast_channel_monitor(chan))
  820. ast_channel_monitor(chan)->joinfiles = turnon;
  821. }
  822. enum MONITOR_PAUSING_ACTION
  823. {
  824. MONITOR_ACTION_PAUSE,
  825. MONITOR_ACTION_UNPAUSE
  826. };
  827. static int do_pause_or_unpause(struct mansession *s, const struct message *m, int action)
  828. {
  829. struct ast_channel *c = NULL;
  830. const char *name = astman_get_header(m, "Channel");
  831. if (ast_strlen_zero(name)) {
  832. astman_send_error(s, m, "No channel specified");
  833. return AMI_SUCCESS;
  834. }
  835. if (!(c = ast_channel_get_by_name(name))) {
  836. astman_send_error(s, m, "No such channel");
  837. return AMI_SUCCESS;
  838. }
  839. if (action == MONITOR_ACTION_PAUSE) {
  840. ast_monitor_pause(c);
  841. } else {
  842. ast_monitor_unpause(c);
  843. }
  844. c = ast_channel_unref(c);
  845. astman_send_ack(s, m, (action == MONITOR_ACTION_PAUSE ? "Paused monitoring of the channel" : "Unpaused monitoring of the channel"));
  846. return AMI_SUCCESS;
  847. }
  848. static int pause_monitor_action(struct mansession *s, const struct message *m)
  849. {
  850. return do_pause_or_unpause(s, m, MONITOR_ACTION_PAUSE);
  851. }
  852. static int unpause_monitor_action(struct mansession *s, const struct message *m)
  853. {
  854. return do_pause_or_unpause(s, m, MONITOR_ACTION_UNPAUSE);
  855. }
  856. static int load_module(void)
  857. {
  858. ast_register_application_xml("Monitor", start_monitor_exec);
  859. ast_register_application_xml("StopMonitor", stop_monitor_exec);
  860. ast_register_application_xml("ChangeMonitor", change_monitor_exec);
  861. ast_register_application_xml("PauseMonitor", pause_monitor_exec);
  862. ast_register_application_xml("UnpauseMonitor", unpause_monitor_exec);
  863. ast_manager_register_xml("Monitor", EVENT_FLAG_CALL, start_monitor_action);
  864. ast_manager_register_xml("StopMonitor", EVENT_FLAG_CALL, stop_monitor_action);
  865. ast_manager_register_xml("ChangeMonitor", EVENT_FLAG_CALL, change_monitor_action);
  866. ast_manager_register_xml("PauseMonitor", EVENT_FLAG_CALL, pause_monitor_action);
  867. ast_manager_register_xml("UnpauseMonitor", EVENT_FLAG_CALL, unpause_monitor_action);
  868. /* For Optional API. */
  869. ast_module_shutdown_ref(ast_module_info->self);
  870. return AST_MODULE_LOAD_SUCCESS;
  871. }
  872. static int unload_module(void)
  873. {
  874. ast_unregister_application("Monitor");
  875. ast_unregister_application("StopMonitor");
  876. ast_unregister_application("ChangeMonitor");
  877. ast_unregister_application("PauseMonitor");
  878. ast_unregister_application("UnpauseMonitor");
  879. ast_manager_unregister("Monitor");
  880. ast_manager_unregister("StopMonitor");
  881. ast_manager_unregister("ChangeMonitor");
  882. ast_manager_unregister("PauseMonitor");
  883. ast_manager_unregister("UnpauseMonitor");
  884. return 0;
  885. }
  886. /* usecount semantics need to be defined */
  887. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Call Monitoring Resource",
  888. .support_level = AST_MODULE_SUPPORT_CORE,
  889. .load = load_module,
  890. .unload = unload_module,
  891. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  892. );