res_monitor.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. /* Replace all '/' chars from the channel name with '-' chars. */
  315. channel_name = ast_strdupa(chan->name);
  316. for (p = channel_name; (p = strchr(p, '/')); ) {
  317. *p = '-';
  318. }
  319. snprintf(monitor->filename_base, FILENAME_MAX, "%s/%d-%s",
  320. ast_config_AST_MONITOR_DIR, (int)time(NULL), channel_name);
  321. monitor->filename_changed = 1;
  322. }
  323. monitor->stop = ast_monitor_stop;
  324. /* Determine file format */
  325. if (!ast_strlen_zero(format_spec)) {
  326. monitor->format = ast_strdup(format_spec);
  327. } else {
  328. monitor->format = ast_strdup("wav");
  329. }
  330. /* open files */
  331. if (stream_action & X_REC_IN) {
  332. if (ast_fileexists(monitor->read_filename, NULL, NULL) > 0)
  333. ast_filedelete(monitor->read_filename, NULL);
  334. if (!(monitor->read_stream = ast_writefile(monitor->read_filename,
  335. monitor->format, NULL,
  336. O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
  337. ast_log(LOG_WARNING, "Could not create file %s\n",
  338. monitor->read_filename);
  339. ast_free(monitor);
  340. UNLOCK_IF_NEEDED(chan, need_lock);
  341. return -1;
  342. }
  343. } else
  344. monitor->read_stream = NULL;
  345. if (stream_action & X_REC_OUT) {
  346. if (ast_fileexists(monitor->write_filename, NULL, NULL) > 0) {
  347. ast_filedelete(monitor->write_filename, NULL);
  348. }
  349. if (!(monitor->write_stream = ast_writefile(monitor->write_filename,
  350. monitor->format, NULL,
  351. O_CREAT|O_TRUNC|O_WRONLY, 0, AST_FILE_MODE))) {
  352. ast_log(LOG_WARNING, "Could not create file %s\n",
  353. monitor->write_filename);
  354. if (monitor->read_stream) {
  355. ast_closestream(monitor->read_stream);
  356. }
  357. ast_free(monitor);
  358. UNLOCK_IF_NEEDED(chan, need_lock);
  359. return -1;
  360. }
  361. } else
  362. monitor->write_stream = NULL;
  363. chan->monitor = monitor;
  364. ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
  365. /* so we know this call has been monitored in case we need to bill for it or something */
  366. pbx_builtin_setvar_helper(chan, "__MONITORED","true");
  367. ast_manager_event(chan, EVENT_FLAG_CALL, "MonitorStart",
  368. "Channel: %s\r\n"
  369. "Uniqueid: %s\r\n",
  370. chan->name,
  371. chan->uniqueid);
  372. } else {
  373. ast_debug(1,"Cannot start monitoring %s, already monitored\n", chan->name);
  374. res = -1;
  375. }
  376. UNLOCK_IF_NEEDED(chan, need_lock);
  377. return res;
  378. }
  379. /*!
  380. * \brief Get audio format.
  381. * \param format recording format.
  382. * The file format extensions that Asterisk uses are not all the same as that
  383. * which soxmix expects. This function ensures that the format used as the
  384. * extension on the filename is something soxmix will understand.
  385. */
  386. static const char *get_soxmix_format(const char *format)
  387. {
  388. const char *res = format;
  389. if (!strcasecmp(format,"ulaw"))
  390. res = "ul";
  391. if (!strcasecmp(format,"alaw"))
  392. res = "al";
  393. return res;
  394. }
  395. /*!
  396. * \brief Stop monitoring channel
  397. * \param chan
  398. * \param need_lock
  399. * Stop the recording, close any open streams, mix in/out channels if required
  400. * \return Always 0
  401. */
  402. int AST_OPTIONAL_API_NAME(ast_monitor_stop)(struct ast_channel *chan, int need_lock)
  403. {
  404. int delfiles = 0;
  405. LOCK_IF_NEEDED(chan, need_lock);
  406. if (chan->monitor) {
  407. char filename[ FILENAME_MAX ];
  408. if (chan->monitor->read_stream) {
  409. ast_closestream(chan->monitor->read_stream);
  410. }
  411. if (chan->monitor->write_stream) {
  412. ast_closestream(chan->monitor->write_stream);
  413. }
  414. if (chan->monitor->filename_changed && !ast_strlen_zero(chan->monitor->filename_base)) {
  415. if (chan->monitor->read_stream) {
  416. if (ast_fileexists(chan->monitor->read_filename,NULL,NULL) > 0) {
  417. snprintf(filename, FILENAME_MAX, "%s-in", chan->monitor->filename_base);
  418. if (ast_fileexists(filename, NULL, NULL) > 0) {
  419. ast_filedelete(filename, NULL);
  420. }
  421. ast_filerename(chan->monitor->read_filename, filename, chan->monitor->format);
  422. } else {
  423. ast_log(LOG_WARNING, "File %s not found\n", chan->monitor->read_filename);
  424. }
  425. }
  426. if (chan->monitor->write_stream) {
  427. if (ast_fileexists(chan->monitor->write_filename,NULL,NULL) > 0) {
  428. snprintf(filename, FILENAME_MAX, "%s-out", chan->monitor->filename_base);
  429. if (ast_fileexists(filename, NULL, NULL) > 0) {
  430. ast_filedelete(filename, NULL);
  431. }
  432. ast_filerename(chan->monitor->write_filename, filename, chan->monitor->format);
  433. } else {
  434. ast_log(LOG_WARNING, "File %s not found\n", chan->monitor->write_filename);
  435. }
  436. }
  437. }
  438. if (chan->monitor->joinfiles && !ast_strlen_zero(chan->monitor->filename_base)) {
  439. char tmp[1024];
  440. char tmp2[1024];
  441. const char *format = !strcasecmp(chan->monitor->format,"wav49") ? "WAV" : chan->monitor->format;
  442. char *fname_base = chan->monitor->filename_base;
  443. const char *execute, *execute_args;
  444. /* at this point, fname_base really is the full path */
  445. /* Set the execute application */
  446. execute = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC");
  447. if (ast_strlen_zero(execute)) {
  448. #ifdef HAVE_SOXMIX
  449. execute = "nice -n 19 soxmix";
  450. #else
  451. execute = "nice -n 19 sox -m";
  452. #endif
  453. format = get_soxmix_format(format);
  454. delfiles = 1;
  455. }
  456. execute_args = pbx_builtin_getvar_helper(chan, "MONITOR_EXEC_ARGS");
  457. if (ast_strlen_zero(execute_args)) {
  458. execute_args = "";
  459. }
  460. snprintf(tmp, sizeof(tmp), "%s \"%s-in.%s\" \"%s-out.%s\" \"%s.%s\" %s &",
  461. execute, fname_base, format, fname_base, format, fname_base, format,execute_args);
  462. if (delfiles) {
  463. snprintf(tmp2,sizeof(tmp2), "( %s& rm -f \"%s-\"* ) &",tmp, fname_base); /* remove legs when done mixing */
  464. ast_copy_string(tmp, tmp2, sizeof(tmp));
  465. }
  466. ast_debug(1,"monitor executing %s\n",tmp);
  467. if (ast_safe_system(tmp) == -1)
  468. ast_log(LOG_WARNING, "Execute of %s failed.\n",tmp);
  469. }
  470. ast_free(chan->monitor->format);
  471. ast_free(chan->monitor);
  472. chan->monitor = NULL;
  473. ast_manager_event(chan, EVENT_FLAG_CALL, "MonitorStop",
  474. "Channel: %s\r\n"
  475. "Uniqueid: %s\r\n",
  476. chan->name,
  477. chan->uniqueid
  478. );
  479. pbx_builtin_setvar_helper(chan, "MONITORED", NULL);
  480. }
  481. pbx_builtin_setvar_helper(chan, "AUTO_MONITOR", NULL);
  482. UNLOCK_IF_NEEDED(chan, need_lock);
  483. return 0;
  484. }
  485. /*! \brief Pause monitoring of channel */
  486. int AST_OPTIONAL_API_NAME(ast_monitor_pause)(struct ast_channel *chan)
  487. {
  488. return ast_monitor_set_state(chan, AST_MONITOR_PAUSED);
  489. }
  490. /*! \brief Unpause monitoring of channel */
  491. int AST_OPTIONAL_API_NAME(ast_monitor_unpause)(struct ast_channel *chan)
  492. {
  493. return ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
  494. }
  495. /*! \brief Wrapper for ast_monitor_pause */
  496. static int pause_monitor_exec(struct ast_channel *chan, const char *data)
  497. {
  498. return ast_monitor_pause(chan);
  499. }
  500. /*! \brief Wrapper for ast_monitor_unpause */
  501. static int unpause_monitor_exec(struct ast_channel *chan, const char *data)
  502. {
  503. return ast_monitor_unpause(chan);
  504. }
  505. /*!
  506. * \brief Change monitored filename of channel
  507. * \param chan
  508. * \param fname_base new filename
  509. * \param need_lock
  510. * \retval 0 on success.
  511. * \retval -1 on failure.
  512. */
  513. int AST_OPTIONAL_API_NAME(ast_monitor_change_fname)(struct ast_channel *chan, const char *fname_base, int need_lock)
  514. {
  515. if (ast_strlen_zero(fname_base)) {
  516. ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to null\n", chan->name);
  517. return -1;
  518. }
  519. LOCK_IF_NEEDED(chan, need_lock);
  520. if (chan->monitor) {
  521. int directory = strchr(fname_base, '/') ? 1 : 0;
  522. const char *absolute = *fname_base == '/' ? "" : ast_config_AST_MONITOR_DIR;
  523. const char *absolute_suffix = *fname_base == '/' ? "" : "/";
  524. char tmpstring[sizeof(chan->monitor->filename_base)] = "";
  525. int i, fd[2] = { -1, -1 }, doexit = 0;
  526. /* before continuing, see if we're trying to rename the file to itself... */
  527. snprintf(tmpstring, sizeof(tmpstring), "%s%s%s", absolute, absolute_suffix, fname_base);
  528. /* try creating the directory just in case it doesn't exist */
  529. if (directory) {
  530. char *name = ast_strdupa(tmpstring);
  531. ast_mkdir(dirname(name), 0777);
  532. }
  533. /*!
  534. * \note We cannot just compare filenames, due to symlinks, relative
  535. * paths, and other possible filesystem issues. We could use
  536. * realpath(3), but its use is discouraged. However, if we try to
  537. * create the same file from two different paths, the second will
  538. * fail, and so we have our notification that the filenames point to
  539. * the same path.
  540. *
  541. * Remember, also, that we're using the basename of the file (i.e.
  542. * the file without the format suffix), so it does not already exist
  543. * and we aren't interfering with the recording itself.
  544. */
  545. ast_debug(2, "comparing tmpstring %s to filename_base %s\n", tmpstring, chan->monitor->filename_base);
  546. if ((fd[0] = open(tmpstring, O_CREAT | O_WRONLY, 0644)) < 0 ||
  547. (fd[1] = open(chan->monitor->filename_base, O_CREAT | O_EXCL | O_WRONLY, 0644)) < 0) {
  548. if (fd[0] < 0) {
  549. ast_log(LOG_ERROR, "Unable to compare filenames: %s\n", strerror(errno));
  550. } else {
  551. ast_debug(2, "No need to rename monitor filename to itself\n");
  552. }
  553. doexit = 1;
  554. }
  555. /* Cleanup temporary files */
  556. for (i = 0; i < 2; i++) {
  557. if (fd[i] >= 0) {
  558. while (close(fd[i]) < 0 && errno == EINTR);
  559. }
  560. }
  561. unlink(tmpstring);
  562. /* if previous monitor file existed in a subdirectory, the directory will not be removed */
  563. unlink(chan->monitor->filename_base);
  564. if (doexit) {
  565. UNLOCK_IF_NEEDED(chan, need_lock);
  566. return 0;
  567. }
  568. ast_copy_string(chan->monitor->filename_base, tmpstring, sizeof(chan->monitor->filename_base));
  569. chan->monitor->filename_changed = 1;
  570. } else {
  571. ast_log(LOG_WARNING, "Cannot change monitor filename of channel %s to %s, monitoring not started\n", chan->name, fname_base);
  572. }
  573. UNLOCK_IF_NEEDED(chan, need_lock);
  574. return 0;
  575. }
  576. /*!
  577. * \brief Start monitor
  578. * \param chan
  579. * \param data arguments passed fname|options
  580. * \retval 0 on success.
  581. * \retval -1 on failure.
  582. */
  583. static int start_monitor_exec(struct ast_channel *chan, const char *data)
  584. {
  585. char *arg;
  586. char *options;
  587. char *delay;
  588. char *urlprefix = NULL;
  589. char tmp[256];
  590. int stream_action = X_REC_IN | X_REC_OUT;
  591. int joinfiles = 0;
  592. int waitforbridge = 0;
  593. int res = 0;
  594. char *parse;
  595. AST_DECLARE_APP_ARGS(args,
  596. AST_APP_ARG(format);
  597. AST_APP_ARG(fname_base);
  598. AST_APP_ARG(options);
  599. );
  600. /* Parse arguments. */
  601. if (ast_strlen_zero(data)) {
  602. ast_log(LOG_ERROR, "Monitor requires an argument\n");
  603. return 0;
  604. }
  605. parse = ast_strdupa(data);
  606. AST_STANDARD_APP_ARGS(args, parse);
  607. if (!ast_strlen_zero(args.options)) {
  608. if (strchr(args.options, 'm'))
  609. stream_action |= X_JOIN;
  610. if (strchr(args.options, 'b'))
  611. waitforbridge = 1;
  612. if (strchr(args.options, 'i'))
  613. stream_action &= ~X_REC_IN;
  614. if (strchr(args.options, 'o'))
  615. stream_action &= ~X_REC_OUT;
  616. }
  617. arg = strchr(args.format, ':');
  618. if (arg) {
  619. *arg++ = 0;
  620. urlprefix = arg;
  621. }
  622. if (!ast_strlen_zero(urlprefix) && !ast_strlen_zero(args.fname_base)) {
  623. snprintf(tmp, sizeof(tmp), "%s/%s.%s", urlprefix, args.fname_base,
  624. ((strcmp(args.format, "gsm")) ? "wav" : "gsm"));
  625. ast_channel_lock(chan);
  626. if (!chan->cdr && !(chan->cdr = ast_cdr_alloc())) {
  627. ast_channel_unlock(chan);
  628. return -1;
  629. }
  630. ast_cdr_setuserfield(chan, tmp);
  631. ast_channel_unlock(chan);
  632. }
  633. if (waitforbridge) {
  634. /* We must remove the "b" option if listed. In principle none of
  635. the following could give NULL results, but we check just to
  636. be pedantic. Reconstructing with checks for 'm' option does not
  637. work if we end up adding more options than 'm' in the future. */
  638. delay = ast_strdupa(data);
  639. options = strrchr(delay, ',');
  640. if (options) {
  641. arg = strchr(options, 'b');
  642. if (arg) {
  643. *arg = 'X';
  644. pbx_builtin_setvar_helper(chan,"AUTO_MONITOR", delay);
  645. }
  646. }
  647. return 0;
  648. }
  649. res = ast_monitor_start(chan, args.format, args.fname_base, 1, stream_action);
  650. if (res < 0)
  651. res = ast_monitor_change_fname(chan, args.fname_base, 1);
  652. if (stream_action & X_JOIN) {
  653. if ((stream_action & X_REC_IN) && (stream_action & X_REC_OUT))
  654. joinfiles = 1;
  655. else
  656. ast_log(LOG_WARNING, "Won't mix streams unless both input and output streams are recorded\n");
  657. }
  658. ast_monitor_setjoinfiles(chan, joinfiles);
  659. return res;
  660. }
  661. /*! \brief Wrapper function \see ast_monitor_stop */
  662. static int stop_monitor_exec(struct ast_channel *chan, const char *data)
  663. {
  664. return ast_monitor_stop(chan, 1);
  665. }
  666. /*! \brief Wrapper function \see ast_monitor_change_fname */
  667. static int change_monitor_exec(struct ast_channel *chan, const char *data)
  668. {
  669. return ast_monitor_change_fname(chan, data, 1);
  670. }
  671. /*! \brief Start monitoring a channel by manager connection */
  672. static int start_monitor_action(struct mansession *s, const struct message *m)
  673. {
  674. struct ast_channel *c = NULL;
  675. const char *name = astman_get_header(m, "Channel");
  676. const char *fname = astman_get_header(m, "File");
  677. const char *format = astman_get_header(m, "Format");
  678. const char *mix = astman_get_header(m, "Mix");
  679. char *d;
  680. if (ast_strlen_zero(name)) {
  681. astman_send_error(s, m, "No channel specified");
  682. return AMI_SUCCESS;
  683. }
  684. if (!(c = ast_channel_get_by_name(name))) {
  685. astman_send_error(s, m, "No such channel");
  686. return AMI_SUCCESS;
  687. }
  688. if (ast_strlen_zero(fname)) {
  689. /* No filename specified, default to the channel name. */
  690. ast_channel_lock(c);
  691. fname = ast_strdupa(c->name);
  692. ast_channel_unlock(c);
  693. /* Replace all '/' chars from the channel name with '-' chars. */
  694. for (d = (char *) fname; (d = strchr(d, '/')); ) {
  695. *d = '-';
  696. }
  697. }
  698. if (ast_monitor_start(c, format, fname, 1, X_REC_IN | X_REC_OUT)) {
  699. if (ast_monitor_change_fname(c, fname, 1)) {
  700. astman_send_error(s, m, "Could not start monitoring channel");
  701. c = ast_channel_unref(c);
  702. return AMI_SUCCESS;
  703. }
  704. }
  705. if (ast_true(mix)) {
  706. ast_channel_lock(c);
  707. ast_monitor_setjoinfiles(c, 1);
  708. ast_channel_unlock(c);
  709. }
  710. c = ast_channel_unref(c);
  711. astman_send_ack(s, m, "Started monitoring channel");
  712. return AMI_SUCCESS;
  713. }
  714. /*! \brief Stop monitoring a channel by manager connection */
  715. static int stop_monitor_action(struct mansession *s, const struct message *m)
  716. {
  717. struct ast_channel *c = NULL;
  718. const char *name = astman_get_header(m, "Channel");
  719. int res;
  720. if (ast_strlen_zero(name)) {
  721. astman_send_error(s, m, "No channel specified");
  722. return AMI_SUCCESS;
  723. }
  724. if (!(c = ast_channel_get_by_name(name))) {
  725. astman_send_error(s, m, "No such channel");
  726. return AMI_SUCCESS;
  727. }
  728. res = ast_monitor_stop(c, 1);
  729. c = ast_channel_unref(c);
  730. if (res) {
  731. astman_send_error(s, m, "Could not stop monitoring channel");
  732. return AMI_SUCCESS;
  733. }
  734. astman_send_ack(s, m, "Stopped monitoring channel");
  735. return AMI_SUCCESS;
  736. }
  737. /*! \brief Change filename of a monitored channel by manager connection */
  738. static int change_monitor_action(struct mansession *s, const struct message *m)
  739. {
  740. struct ast_channel *c = NULL;
  741. const char *name = astman_get_header(m, "Channel");
  742. const char *fname = astman_get_header(m, "File");
  743. if (ast_strlen_zero(name)) {
  744. astman_send_error(s, m, "No channel specified");
  745. return AMI_SUCCESS;
  746. }
  747. if (ast_strlen_zero(fname)) {
  748. astman_send_error(s, m, "No filename specified");
  749. return AMI_SUCCESS;
  750. }
  751. if (!(c = ast_channel_get_by_name(name))) {
  752. astman_send_error(s, m, "No such channel");
  753. return AMI_SUCCESS;
  754. }
  755. if (ast_monitor_change_fname(c, fname, 1)) {
  756. c = ast_channel_unref(c);
  757. astman_send_error(s, m, "Could not change monitored filename of channel");
  758. return AMI_SUCCESS;
  759. }
  760. c = ast_channel_unref(c);
  761. astman_send_ack(s, m, "Changed monitor filename");
  762. return AMI_SUCCESS;
  763. }
  764. void AST_OPTIONAL_API_NAME(ast_monitor_setjoinfiles)(struct ast_channel *chan, int turnon)
  765. {
  766. if (chan->monitor)
  767. chan->monitor->joinfiles = turnon;
  768. }
  769. enum MONITOR_PAUSING_ACTION
  770. {
  771. MONITOR_ACTION_PAUSE,
  772. MONITOR_ACTION_UNPAUSE
  773. };
  774. static int do_pause_or_unpause(struct mansession *s, const struct message *m, int action)
  775. {
  776. struct ast_channel *c = NULL;
  777. const char *name = astman_get_header(m, "Channel");
  778. if (ast_strlen_zero(name)) {
  779. astman_send_error(s, m, "No channel specified");
  780. return AMI_SUCCESS;
  781. }
  782. if (!(c = ast_channel_get_by_name(name))) {
  783. astman_send_error(s, m, "No such channel");
  784. return AMI_SUCCESS;
  785. }
  786. if (action == MONITOR_ACTION_PAUSE) {
  787. ast_monitor_pause(c);
  788. } else {
  789. ast_monitor_unpause(c);
  790. }
  791. c = ast_channel_unref(c);
  792. astman_send_ack(s, m, (action == MONITOR_ACTION_PAUSE ? "Paused monitoring of the channel" : "Unpaused monitoring of the channel"));
  793. return AMI_SUCCESS;
  794. }
  795. static int pause_monitor_action(struct mansession *s, const struct message *m)
  796. {
  797. return do_pause_or_unpause(s, m, MONITOR_ACTION_PAUSE);
  798. }
  799. static int unpause_monitor_action(struct mansession *s, const struct message *m)
  800. {
  801. return do_pause_or_unpause(s, m, MONITOR_ACTION_UNPAUSE);
  802. }
  803. static int load_module(void)
  804. {
  805. ast_register_application_xml("Monitor", start_monitor_exec);
  806. ast_register_application_xml("StopMonitor", stop_monitor_exec);
  807. ast_register_application_xml("ChangeMonitor", change_monitor_exec);
  808. ast_register_application_xml("PauseMonitor", pause_monitor_exec);
  809. ast_register_application_xml("UnpauseMonitor", unpause_monitor_exec);
  810. ast_manager_register_xml("Monitor", EVENT_FLAG_CALL, start_monitor_action);
  811. ast_manager_register_xml("StopMonitor", EVENT_FLAG_CALL, stop_monitor_action);
  812. ast_manager_register_xml("ChangeMonitor", EVENT_FLAG_CALL, change_monitor_action);
  813. ast_manager_register_xml("PauseMonitor", EVENT_FLAG_CALL, pause_monitor_action);
  814. ast_manager_register_xml("UnpauseMonitor", EVENT_FLAG_CALL, unpause_monitor_action);
  815. return AST_MODULE_LOAD_SUCCESS;
  816. }
  817. static int unload_module(void)
  818. {
  819. ast_unregister_application("Monitor");
  820. ast_unregister_application("StopMonitor");
  821. ast_unregister_application("ChangeMonitor");
  822. ast_unregister_application("PauseMonitor");
  823. ast_unregister_application("UnpauseMonitor");
  824. ast_manager_unregister("Monitor");
  825. ast_manager_unregister("StopMonitor");
  826. ast_manager_unregister("ChangeMonitor");
  827. ast_manager_unregister("PauseMonitor");
  828. ast_manager_unregister("UnpauseMonitor");
  829. return 0;
  830. }
  831. /* usecount semantics need to be defined */
  832. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Call Monitoring Resource",
  833. .load = load_module,
  834. .unload = unload_module,
  835. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  836. );