PageRenderTime 247ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 1ms

/res/res_monitor.c

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