res_monitor.c 31 KB

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