app_mixmonitor.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2005, Anthony Minessale II
  5. * Copyright (C) 2005 - 2006, Digium, Inc.
  6. *
  7. * Mark Spencer <markster@digium.com>
  8. * Kevin P. Fleming <kpfleming@digium.com>
  9. *
  10. * Based on app_muxmon.c provided by
  11. * Anthony Minessale II <anthmct@yahoo.com>
  12. *
  13. * See http://www.asterisk.org for more information about
  14. * the Asterisk project. Please do not directly contact
  15. * any of the maintainers of this project for assistance;
  16. * the project provides a web site, mailing lists and IRC
  17. * channels for your use.
  18. *
  19. * This program is free software, distributed under the terms of
  20. * the GNU General Public License Version 2. See the LICENSE file
  21. * at the top of the source tree.
  22. */
  23. /*! \file
  24. *
  25. * \brief MixMonitor() - Record a call and mix the audio during the recording
  26. * \ingroup applications
  27. *
  28. * \author Mark Spencer <markster@digium.com>
  29. * \author Kevin P. Fleming <kpfleming@digium.com>
  30. *
  31. * \note Based on app_muxmon.c provided by
  32. * Anthony Minessale II <anthmct@yahoo.com>
  33. */
  34. /*** MODULEINFO
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/paths.h" /* use ast_config_AST_MONITOR_DIR */
  40. #include "asterisk/stringfields.h"
  41. #include "asterisk/file.h"
  42. #include "asterisk/audiohook.h"
  43. #include "asterisk/pbx.h"
  44. #include "asterisk/module.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/app.h"
  47. #include "asterisk/channel.h"
  48. #include "asterisk/autochan.h"
  49. #include "asterisk/manager.h"
  50. #include "asterisk/callerid.h"
  51. #include "asterisk/mod_format.h"
  52. #include "asterisk/linkedlists.h"
  53. #include "asterisk/test.h"
  54. #include "asterisk/mixmonitor.h"
  55. #include "asterisk/format_cache.h"
  56. #include "asterisk/beep.h"
  57. /*** DOCUMENTATION
  58. <application name="MixMonitor" language="en_US">
  59. <synopsis>
  60. Record a call and mix the audio during the recording. Use of StopMixMonitor is required
  61. to guarantee the audio file is available for processing during dialplan execution.
  62. </synopsis>
  63. <syntax>
  64. <parameter name="file" required="true" argsep=".">
  65. <argument name="filename" required="true">
  66. <para>If <replaceable>filename</replaceable> is an absolute path, uses that path, otherwise
  67. creates the file in the configured monitoring directory from <filename>asterisk.conf.</filename></para>
  68. </argument>
  69. <argument name="extension" required="true" />
  70. </parameter>
  71. <parameter name="options">
  72. <optionlist>
  73. <option name="a">
  74. <para>Append to the file instead of overwriting it.</para>
  75. </option>
  76. <option name="b">
  77. <para>Only save audio to the file while the channel is bridged.</para>
  78. <note><para>If you utilize this option inside a Local channel, you must make sure the Local
  79. channel is not optimized away. To do this, be sure to call your Local channel with the
  80. <literal>/n</literal> option. For example: Dial(Local/start@mycontext/n)</para></note>
  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="v">
  87. <para>Adjust the <emphasis>heard</emphasis> volume by a factor of <replaceable>x</replaceable>
  88. (range <literal>-4</literal> to <literal>4</literal>)</para>
  89. <argument name="x" required="true" />
  90. </option>
  91. <option name="V">
  92. <para>Adjust the <emphasis>spoken</emphasis> volume by a factor
  93. of <replaceable>x</replaceable> (range <literal>-4</literal> to <literal>4</literal>)</para>
  94. <argument name="x" required="true" />
  95. </option>
  96. <option name="W">
  97. <para>Adjust both, <emphasis>heard and spoken</emphasis> volumes by a factor
  98. of <replaceable>x</replaceable> (range <literal>-4</literal> to <literal>4</literal>)</para>
  99. <argument name="x" required="true" />
  100. </option>
  101. <option name="r">
  102. <argument name="file" required="true" />
  103. <para>Use the specified file to record the <emphasis>receive</emphasis> audio feed.
  104. Like with the basic filename argument, if an absolute path isn't given, it will create
  105. the file in the configured monitoring directory.</para>
  106. </option>
  107. <option name="t">
  108. <argument name="file" required="true" />
  109. <para>Use the specified file to record the <emphasis>transmit</emphasis> audio feed.
  110. Like with the basic filename argument, if an absolute path isn't given, it will create
  111. the file in the configured monitoring directory.</para>
  112. </option>
  113. <option name="i">
  114. <argument name="chanvar" required="true" />
  115. <para>Stores the MixMonitor's ID on this channel variable.</para>
  116. </option>
  117. <option name="p">
  118. <para>Play a beep on the channel that starts the recording.</para>
  119. </option>
  120. <option name="P">
  121. <para>Play a beep on the channel that stops the recording.</para>
  122. </option>
  123. <option name="m">
  124. <argument name="mailbox" required="true" />
  125. <para>Create a copy of the recording as a voicemail in the indicated <emphasis>mailbox</emphasis>(es)
  126. separated by commas eg. m(1111@default,2222@default,...). Folders can be optionally specified using
  127. the syntax: mailbox@context/folder</para>
  128. </option>
  129. </optionlist>
  130. </parameter>
  131. <parameter name="command">
  132. <para>Will be executed when the recording is over.</para>
  133. <para>Any strings matching <literal>^{X}</literal> will be unescaped to <variable>X</variable>.</para>
  134. <para>All variables will be evaluated at the time MixMonitor is called.</para>
  135. </parameter>
  136. </syntax>
  137. <description>
  138. <para>Records the audio on the current channel to the specified file.</para>
  139. <para>This application does not automatically answer and should be preceeded by
  140. an application such as Answer or Progress().</para>
  141. <note><para>MixMonitor runs as an audiohook.</para></note>
  142. <variablelist>
  143. <variable name="MIXMONITOR_FILENAME">
  144. <para>Will contain the filename used to record.</para>
  145. </variable>
  146. </variablelist>
  147. </description>
  148. <see-also>
  149. <ref type="application">Monitor</ref>
  150. <ref type="application">StopMixMonitor</ref>
  151. <ref type="application">PauseMonitor</ref>
  152. <ref type="application">UnpauseMonitor</ref>
  153. <ref type="function">AUDIOHOOK_INHERIT</ref>
  154. </see-also>
  155. </application>
  156. <application name="StopMixMonitor" language="en_US">
  157. <synopsis>
  158. Stop recording a call through MixMonitor, and free the recording's file handle.
  159. </synopsis>
  160. <syntax>
  161. <parameter name="MixMonitorID" required="false">
  162. <para>If a valid ID is provided, then this command will stop only that specific
  163. MixMonitor.</para>
  164. </parameter>
  165. </syntax>
  166. <description>
  167. <para>Stops the audio recording that was started with a call to <literal>MixMonitor()</literal>
  168. on the current channel.</para>
  169. </description>
  170. <see-also>
  171. <ref type="application">MixMonitor</ref>
  172. </see-also>
  173. </application>
  174. <manager name="MixMonitorMute" language="en_US">
  175. <synopsis>
  176. Mute / unMute a Mixmonitor recording.
  177. </synopsis>
  178. <syntax>
  179. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  180. <parameter name="Channel" required="true">
  181. <para>Used to specify the channel to mute.</para>
  182. </parameter>
  183. <parameter name="Direction">
  184. <para>Which part of the recording to mute: read, write or both (from channel, to channel or both channels).</para>
  185. </parameter>
  186. <parameter name="State">
  187. <para>Turn mute on or off : 1 to turn on, 0 to turn off.</para>
  188. </parameter>
  189. </syntax>
  190. <description>
  191. <para>This action may be used to mute a MixMonitor recording.</para>
  192. </description>
  193. </manager>
  194. <manager name="MixMonitor" language="en_US">
  195. <synopsis>
  196. Record a call and mix the audio during the recording. Use of StopMixMonitor is required
  197. to guarantee the audio file is available for processing during dialplan execution.
  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">
  205. <para>Is the name of the file created in the monitor spool directory.
  206. Defaults to the same name as the channel (with slashes replaced with dashes).
  207. This argument is optional if you specify to record unidirectional audio with
  208. either the r(filename) or t(filename) options in the options field. If
  209. neither MIXMONITOR_FILENAME or this parameter is set, the mixed stream won't
  210. be recorded.</para>
  211. </parameter>
  212. <parameter name="options">
  213. <para>Options that apply to the MixMonitor in the same way as they
  214. would apply if invoked from the MixMonitor application. For a list of
  215. available options, see the documentation for the mixmonitor application. </para>
  216. </parameter>
  217. <parameter name="Command">
  218. <para>Will be executed when the recording is over.
  219. Any strings matching <literal>^{X}</literal> will be unescaped to <variable>X</variable>.
  220. All variables will be evaluated at the time MixMonitor is called.</para>
  221. </parameter>
  222. </syntax>
  223. <description>
  224. <para>This action records the audio on the current channel to the specified file.</para>
  225. <variablelist>
  226. <variable name="MIXMONITOR_FILENAME">
  227. <para>Will contain the filename used to record the mixed stream.</para>
  228. </variable>
  229. </variablelist>
  230. </description>
  231. </manager>
  232. <manager name="StopMixMonitor" language="en_US">
  233. <synopsis>
  234. Stop recording a call through MixMonitor, and free the recording's file handle.
  235. </synopsis>
  236. <syntax>
  237. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  238. <parameter name="Channel" required="true">
  239. <para>The name of the channel monitored.</para>
  240. </parameter>
  241. <parameter name="MixMonitorID" required="false">
  242. <para>If a valid ID is provided, then this command will stop only that specific
  243. MixMonitor.</para>
  244. </parameter>
  245. </syntax>
  246. <description>
  247. <para>This action stops the audio recording that was started with the <literal>MixMonitor</literal>
  248. action on the current channel.</para>
  249. </description>
  250. </manager>
  251. <function name="MIXMONITOR" language="en_US">
  252. <synopsis>
  253. Retrieve data pertaining to specific instances of MixMonitor on a channel.
  254. </synopsis>
  255. <syntax>
  256. <parameter name="id" required="true">
  257. <para>The unique ID of the MixMonitor instance. The unique ID can be retrieved through the channel
  258. variable used as an argument to the <replaceable>i</replaceable> option to MixMonitor.</para>
  259. </parameter>
  260. <parameter name="key" required="true">
  261. <para>The piece of data to retrieve from the MixMonitor.</para>
  262. <enumlist>
  263. <enum name="filename" />
  264. </enumlist>
  265. </parameter>
  266. </syntax>
  267. </function>
  268. ***/
  269. #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
  270. static const char * const app = "MixMonitor";
  271. static const char * const stop_app = "StopMixMonitor";
  272. static const char * const mixmonitor_spy_type = "MixMonitor";
  273. /*!
  274. * \internal
  275. * \brief This struct is a list item holds data needed to find a vm_recipient within voicemail
  276. */
  277. struct vm_recipient {
  278. char mailbox[AST_MAX_CONTEXT];
  279. char context[AST_MAX_EXTENSION];
  280. char folder[80];
  281. AST_LIST_ENTRY(vm_recipient) list;
  282. };
  283. struct mixmonitor {
  284. struct ast_audiohook audiohook;
  285. char *filename;
  286. char *filename_read;
  287. char *filename_write;
  288. char *post_process;
  289. char *name;
  290. ast_callid callid;
  291. unsigned int flags;
  292. struct ast_autochan *autochan;
  293. struct mixmonitor_ds *mixmonitor_ds;
  294. /* the below string fields describe data used for creating voicemails from the recording */
  295. AST_DECLARE_STRING_FIELDS(
  296. AST_STRING_FIELD(call_context);
  297. AST_STRING_FIELD(call_macrocontext);
  298. AST_STRING_FIELD(call_extension);
  299. AST_STRING_FIELD(call_callerchan);
  300. AST_STRING_FIELD(call_callerid);
  301. );
  302. int call_priority;
  303. /* FUTURE DEVELOPMENT NOTICE
  304. * recipient_list will need locks if we make it editable after the monitor is started */
  305. AST_LIST_HEAD_NOLOCK(, vm_recipient) recipient_list;
  306. };
  307. enum mixmonitor_flags {
  308. MUXFLAG_APPEND = (1 << 1),
  309. MUXFLAG_BRIDGED = (1 << 2),
  310. MUXFLAG_VOLUME = (1 << 3),
  311. MUXFLAG_READVOLUME = (1 << 4),
  312. MUXFLAG_WRITEVOLUME = (1 << 5),
  313. MUXFLAG_READ = (1 << 6),
  314. MUXFLAG_WRITE = (1 << 7),
  315. MUXFLAG_COMBINED = (1 << 8),
  316. MUXFLAG_UID = (1 << 9),
  317. MUXFLAG_VMRECIPIENTS = (1 << 10),
  318. MUXFLAG_BEEP = (1 << 11),
  319. MUXFLAG_BEEP_START = (1 << 12),
  320. MUXFLAG_BEEP_STOP = (1 << 13)
  321. };
  322. enum mixmonitor_args {
  323. OPT_ARG_READVOLUME = 0,
  324. OPT_ARG_WRITEVOLUME,
  325. OPT_ARG_VOLUME,
  326. OPT_ARG_WRITENAME,
  327. OPT_ARG_READNAME,
  328. OPT_ARG_UID,
  329. OPT_ARG_VMRECIPIENTS,
  330. OPT_ARG_BEEP_INTERVAL,
  331. OPT_ARG_ARRAY_SIZE, /* Always last element of the enum */
  332. };
  333. AST_APP_OPTIONS(mixmonitor_opts, {
  334. AST_APP_OPTION('a', MUXFLAG_APPEND),
  335. AST_APP_OPTION('b', MUXFLAG_BRIDGED),
  336. AST_APP_OPTION_ARG('B', MUXFLAG_BEEP, OPT_ARG_BEEP_INTERVAL),
  337. AST_APP_OPTION('p', MUXFLAG_BEEP_START),
  338. AST_APP_OPTION('P', MUXFLAG_BEEP_STOP),
  339. AST_APP_OPTION_ARG('v', MUXFLAG_READVOLUME, OPT_ARG_READVOLUME),
  340. AST_APP_OPTION_ARG('V', MUXFLAG_WRITEVOLUME, OPT_ARG_WRITEVOLUME),
  341. AST_APP_OPTION_ARG('W', MUXFLAG_VOLUME, OPT_ARG_VOLUME),
  342. AST_APP_OPTION_ARG('r', MUXFLAG_READ, OPT_ARG_READNAME),
  343. AST_APP_OPTION_ARG('t', MUXFLAG_WRITE, OPT_ARG_WRITENAME),
  344. AST_APP_OPTION_ARG('i', MUXFLAG_UID, OPT_ARG_UID),
  345. AST_APP_OPTION_ARG('m', MUXFLAG_VMRECIPIENTS, OPT_ARG_VMRECIPIENTS),
  346. });
  347. struct mixmonitor_ds {
  348. unsigned int destruction_ok;
  349. ast_cond_t destruction_condition;
  350. ast_mutex_t lock;
  351. /* The filestream is held in the datastore so it can be stopped
  352. * immediately during stop_mixmonitor or channel destruction. */
  353. int fs_quit;
  354. struct ast_filestream *fs;
  355. struct ast_filestream *fs_read;
  356. struct ast_filestream *fs_write;
  357. struct ast_audiohook *audiohook;
  358. unsigned int samp_rate;
  359. char *filename;
  360. char *beep_id;
  361. };
  362. /*!
  363. * \internal
  364. * \pre mixmonitor_ds must be locked before calling this function
  365. */
  366. static void mixmonitor_ds_close_fs(struct mixmonitor_ds *mixmonitor_ds)
  367. {
  368. unsigned char quitting = 0;
  369. if (mixmonitor_ds->fs) {
  370. quitting = 1;
  371. ast_closestream(mixmonitor_ds->fs);
  372. mixmonitor_ds->fs = NULL;
  373. ast_verb(2, "MixMonitor close filestream (mixed)\n");
  374. }
  375. if (mixmonitor_ds->fs_read) {
  376. quitting = 1;
  377. ast_closestream(mixmonitor_ds->fs_read);
  378. mixmonitor_ds->fs_read = NULL;
  379. ast_verb(2, "MixMonitor close filestream (read)\n");
  380. }
  381. if (mixmonitor_ds->fs_write) {
  382. quitting = 1;
  383. ast_closestream(mixmonitor_ds->fs_write);
  384. mixmonitor_ds->fs_write = NULL;
  385. ast_verb(2, "MixMonitor close filestream (write)\n");
  386. }
  387. if (quitting) {
  388. mixmonitor_ds->fs_quit = 1;
  389. }
  390. }
  391. static void mixmonitor_ds_destroy(void *data)
  392. {
  393. struct mixmonitor_ds *mixmonitor_ds = data;
  394. ast_mutex_lock(&mixmonitor_ds->lock);
  395. mixmonitor_ds->audiohook = NULL;
  396. mixmonitor_ds->destruction_ok = 1;
  397. ast_free(mixmonitor_ds->filename);
  398. ast_free(mixmonitor_ds->beep_id);
  399. ast_cond_signal(&mixmonitor_ds->destruction_condition);
  400. ast_mutex_unlock(&mixmonitor_ds->lock);
  401. }
  402. static const struct ast_datastore_info mixmonitor_ds_info = {
  403. .type = "mixmonitor",
  404. .destroy = mixmonitor_ds_destroy,
  405. };
  406. static void destroy_monitor_audiohook(struct mixmonitor *mixmonitor)
  407. {
  408. if (mixmonitor->mixmonitor_ds) {
  409. ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
  410. mixmonitor->mixmonitor_ds->audiohook = NULL;
  411. ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
  412. }
  413. /* kill the audiohook.*/
  414. ast_audiohook_lock(&mixmonitor->audiohook);
  415. ast_audiohook_detach(&mixmonitor->audiohook);
  416. ast_audiohook_unlock(&mixmonitor->audiohook);
  417. ast_audiohook_destroy(&mixmonitor->audiohook);
  418. }
  419. static int startmon(struct ast_channel *chan, struct ast_audiohook *audiohook)
  420. {
  421. if (!chan) {
  422. return -1;
  423. }
  424. return ast_audiohook_attach(chan, audiohook);
  425. }
  426. /*!
  427. * \internal
  428. * \brief adds recipients to a mixmonitor's recipient list
  429. * \param mixmonitor mixmonitor being affected
  430. * \param vm_recipients string containing the desired recipients to add
  431. */
  432. static void add_vm_recipients_from_string(struct mixmonitor *mixmonitor, const char *vm_recipients)
  433. {
  434. /* recipients are in a single string with a format format resembling "mailbox@context/INBOX,mailbox2@context2,mailbox3@context3/Work" */
  435. char *cur_mailbox = ast_strdupa(vm_recipients);
  436. char *cur_context;
  437. char *cur_folder;
  438. char *next;
  439. int elements_processed = 0;
  440. while (!ast_strlen_zero(cur_mailbox)) {
  441. ast_debug(3, "attempting to add next element %d from %s\n", elements_processed, cur_mailbox);
  442. if ((next = strchr(cur_mailbox, ',')) || (next = strchr(cur_mailbox, '&'))) {
  443. *(next++) = '\0';
  444. }
  445. if ((cur_folder = strchr(cur_mailbox, '/'))) {
  446. *(cur_folder++) = '\0';
  447. } else {
  448. cur_folder = "INBOX";
  449. }
  450. if ((cur_context = strchr(cur_mailbox, '@'))) {
  451. *(cur_context++) = '\0';
  452. } else {
  453. cur_context = "default";
  454. }
  455. if (!ast_strlen_zero(cur_mailbox) && !ast_strlen_zero(cur_context)) {
  456. struct vm_recipient *recipient;
  457. if (!(recipient = ast_malloc(sizeof(*recipient)))) {
  458. ast_log(LOG_ERROR, "Failed to allocate recipient. Aborting function.\n");
  459. return;
  460. }
  461. ast_copy_string(recipient->context, cur_context, sizeof(recipient->context));
  462. ast_copy_string(recipient->mailbox, cur_mailbox, sizeof(recipient->mailbox));
  463. ast_copy_string(recipient->folder, cur_folder, sizeof(recipient->folder));
  464. /* Add to list */
  465. ast_verb(4, "Adding %s@%s to recipient list\n", recipient->mailbox, recipient->context);
  466. AST_LIST_INSERT_HEAD(&mixmonitor->recipient_list, recipient, list);
  467. } else {
  468. ast_log(LOG_ERROR, "Failed to properly parse extension and/or context from element %d of recipient string: %s\n", elements_processed, vm_recipients);
  469. }
  470. cur_mailbox = next;
  471. elements_processed++;
  472. }
  473. }
  474. static void clear_mixmonitor_recipient_list(struct mixmonitor *mixmonitor)
  475. {
  476. struct vm_recipient *current;
  477. while ((current = AST_LIST_REMOVE_HEAD(&mixmonitor->recipient_list, list))) {
  478. /* Clear list element data */
  479. ast_free(current);
  480. }
  481. }
  482. #define SAMPLES_PER_FRAME 160
  483. static void mixmonitor_free(struct mixmonitor *mixmonitor)
  484. {
  485. if (mixmonitor) {
  486. if (mixmonitor->mixmonitor_ds) {
  487. ast_mutex_destroy(&mixmonitor->mixmonitor_ds->lock);
  488. ast_cond_destroy(&mixmonitor->mixmonitor_ds->destruction_condition);
  489. ast_free(mixmonitor->mixmonitor_ds);
  490. }
  491. ast_free(mixmonitor->name);
  492. ast_free(mixmonitor->post_process);
  493. ast_free(mixmonitor->filename);
  494. ast_free(mixmonitor->filename_write);
  495. ast_free(mixmonitor->filename_read);
  496. /* Free everything in the recipient list */
  497. clear_mixmonitor_recipient_list(mixmonitor);
  498. /* clean stringfields */
  499. ast_string_field_free_memory(mixmonitor);
  500. ast_free(mixmonitor);
  501. }
  502. }
  503. /*!
  504. * \internal
  505. * \brief Copies the mixmonitor to all voicemail recipients
  506. * \param mixmonitor The mixmonitor that needs to forward its file to recipients
  507. * \param ext Format of the file that was saved
  508. */
  509. static void copy_to_voicemail(struct mixmonitor *mixmonitor, const char *ext, const char *filename)
  510. {
  511. struct vm_recipient *recipient = NULL;
  512. struct ast_vm_recording_data recording_data;
  513. if (ast_string_field_init(&recording_data, 512)) {
  514. ast_log(LOG_ERROR, "Failed to string_field_init, skipping copy_to_voicemail\n");
  515. return;
  516. }
  517. /* Copy strings to stringfields that will be used for all recipients */
  518. ast_string_field_set(&recording_data, recording_file, filename);
  519. ast_string_field_set(&recording_data, recording_ext, ext);
  520. ast_string_field_set(&recording_data, call_context, mixmonitor->call_context);
  521. ast_string_field_set(&recording_data, call_macrocontext, mixmonitor->call_macrocontext);
  522. ast_string_field_set(&recording_data, call_extension, mixmonitor->call_extension);
  523. ast_string_field_set(&recording_data, call_callerchan, mixmonitor->call_callerchan);
  524. ast_string_field_set(&recording_data, call_callerid, mixmonitor->call_callerid);
  525. /* and call_priority gets copied too */
  526. recording_data.call_priority = mixmonitor->call_priority;
  527. AST_LIST_TRAVERSE(&mixmonitor->recipient_list, recipient, list) {
  528. /* context, mailbox, and folder need to be set per recipient */
  529. ast_string_field_set(&recording_data, context, recipient->context);
  530. ast_string_field_set(&recording_data, mailbox, recipient->mailbox);
  531. ast_string_field_set(&recording_data, folder, recipient->folder);
  532. ast_verb(4, "MixMonitor attempting to send voicemail copy to %s@%s\n", recording_data.mailbox,
  533. recording_data.context);
  534. ast_app_copy_recording_to_vm(&recording_data);
  535. }
  536. /* Free the string fields for recording_data before exiting the function. */
  537. ast_string_field_free_memory(&recording_data);
  538. }
  539. static void mixmonitor_save_prep(struct mixmonitor *mixmonitor, char *filename, struct ast_filestream **fs, unsigned int *oflags, int *errflag, char **ext)
  540. {
  541. /* Initialize the file if not already done so */
  542. char *last_slash = NULL;
  543. if (!ast_strlen_zero(filename)) {
  544. if (!*fs && !*errflag && !mixmonitor->mixmonitor_ds->fs_quit) {
  545. *oflags = O_CREAT | O_WRONLY;
  546. *oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC;
  547. last_slash = strrchr(filename, '/');
  548. if ((*ext = strrchr(filename, '.')) && (*ext > last_slash)) {
  549. **ext = '\0';
  550. *ext = *ext + 1;
  551. } else {
  552. *ext = "raw";
  553. }
  554. if (!(*fs = ast_writefile(filename, *ext, NULL, *oflags, 0, 0666))) {
  555. ast_log(LOG_ERROR, "Cannot open %s.%s\n", filename, *ext);
  556. *errflag = 1;
  557. } else {
  558. struct ast_filestream *tmp = *fs;
  559. mixmonitor->mixmonitor_ds->samp_rate = MAX(mixmonitor->mixmonitor_ds->samp_rate, ast_format_get_sample_rate(tmp->fmt->format));
  560. }
  561. }
  562. }
  563. }
  564. static void *mixmonitor_thread(void *obj)
  565. {
  566. struct mixmonitor *mixmonitor = obj;
  567. char *fs_ext = "";
  568. char *fs_read_ext = "";
  569. char *fs_write_ext = "";
  570. struct ast_filestream **fs = NULL;
  571. struct ast_filestream **fs_read = NULL;
  572. struct ast_filestream **fs_write = NULL;
  573. unsigned int oflags;
  574. int errflag = 0;
  575. struct ast_format *format_slin;
  576. /* Keep callid association before any log messages */
  577. if (mixmonitor->callid) {
  578. ast_callid_threadassoc_add(mixmonitor->callid);
  579. }
  580. ast_verb(2, "Begin MixMonitor Recording %s\n", mixmonitor->name);
  581. fs = &mixmonitor->mixmonitor_ds->fs;
  582. fs_read = &mixmonitor->mixmonitor_ds->fs_read;
  583. fs_write = &mixmonitor->mixmonitor_ds->fs_write;
  584. ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
  585. mixmonitor_save_prep(mixmonitor, mixmonitor->filename, fs, &oflags, &errflag, &fs_ext);
  586. mixmonitor_save_prep(mixmonitor, mixmonitor->filename_read, fs_read, &oflags, &errflag, &fs_read_ext);
  587. mixmonitor_save_prep(mixmonitor, mixmonitor->filename_write, fs_write, &oflags, &errflag, &fs_write_ext);
  588. format_slin = ast_format_cache_get_slin_by_rate(mixmonitor->mixmonitor_ds->samp_rate);
  589. ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
  590. /* The audiohook must enter and exit the loop locked */
  591. ast_audiohook_lock(&mixmonitor->audiohook);
  592. while (mixmonitor->audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING && !mixmonitor->mixmonitor_ds->fs_quit) {
  593. struct ast_frame *fr = NULL;
  594. struct ast_frame *fr_read = NULL;
  595. struct ast_frame *fr_write = NULL;
  596. if (!(fr = ast_audiohook_read_frame_all(&mixmonitor->audiohook, SAMPLES_PER_FRAME, format_slin,
  597. &fr_read, &fr_write))) {
  598. ast_audiohook_trigger_wait(&mixmonitor->audiohook);
  599. if (mixmonitor->audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
  600. break;
  601. }
  602. continue;
  603. }
  604. /* audiohook lock is not required for the next block.
  605. * Unlock it, but remember to lock it before looping or exiting */
  606. ast_audiohook_unlock(&mixmonitor->audiohook);
  607. if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED)
  608. || (mixmonitor->autochan->chan
  609. && ast_channel_is_bridged(mixmonitor->autochan->chan))) {
  610. ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
  611. /* Write out the frame(s) */
  612. if ((*fs_read) && (fr_read)) {
  613. struct ast_frame *cur;
  614. for (cur = fr_read; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  615. ast_writestream(*fs_read, cur);
  616. }
  617. }
  618. if ((*fs_write) && (fr_write)) {
  619. struct ast_frame *cur;
  620. for (cur = fr_write; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  621. ast_writestream(*fs_write, cur);
  622. }
  623. }
  624. if ((*fs) && (fr)) {
  625. struct ast_frame *cur;
  626. for (cur = fr; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
  627. ast_writestream(*fs, cur);
  628. }
  629. }
  630. ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
  631. }
  632. /* All done! free it. */
  633. if (fr) {
  634. ast_frame_free(fr, 0);
  635. }
  636. if (fr_read) {
  637. ast_frame_free(fr_read, 0);
  638. }
  639. if (fr_write) {
  640. ast_frame_free(fr_write, 0);
  641. }
  642. fr = NULL;
  643. fr_write = NULL;
  644. fr_read = NULL;
  645. ast_audiohook_lock(&mixmonitor->audiohook);
  646. }
  647. ast_audiohook_unlock(&mixmonitor->audiohook);
  648. ast_channel_lock(mixmonitor->autochan->chan);
  649. if (ast_test_flag(mixmonitor, MUXFLAG_BEEP_STOP)) {
  650. ast_stream_and_wait(mixmonitor->autochan->chan, "beep", "");
  651. }
  652. ast_channel_unlock(mixmonitor->autochan->chan);
  653. ast_autochan_destroy(mixmonitor->autochan);
  654. /* Datastore cleanup. close the filestream and wait for ds destruction */
  655. ast_mutex_lock(&mixmonitor->mixmonitor_ds->lock);
  656. mixmonitor_ds_close_fs(mixmonitor->mixmonitor_ds);
  657. if (!mixmonitor->mixmonitor_ds->destruction_ok) {
  658. ast_cond_wait(&mixmonitor->mixmonitor_ds->destruction_condition, &mixmonitor->mixmonitor_ds->lock);
  659. }
  660. ast_mutex_unlock(&mixmonitor->mixmonitor_ds->lock);
  661. /* kill the audiohook */
  662. destroy_monitor_audiohook(mixmonitor);
  663. if (mixmonitor->post_process) {
  664. ast_verb(2, "Executing [%s]\n", mixmonitor->post_process);
  665. ast_safe_system(mixmonitor->post_process);
  666. }
  667. ast_verb(2, "End MixMonitor Recording %s\n", mixmonitor->name);
  668. ast_test_suite_event_notify("MIXMONITOR_END", "File: %s\r\n", mixmonitor->filename);
  669. if (!AST_LIST_EMPTY(&mixmonitor->recipient_list)) {
  670. if (ast_strlen_zero(fs_ext)) {
  671. ast_log(LOG_ERROR, "No file extension set for Mixmonitor %s. Skipping copy to voicemail.\n",
  672. mixmonitor -> name);
  673. } else {
  674. ast_verb(3, "Copying recordings for Mixmonitor %s to voicemail recipients\n", mixmonitor->name);
  675. copy_to_voicemail(mixmonitor, fs_ext, mixmonitor->filename);
  676. }
  677. if (!ast_strlen_zero(fs_read_ext)) {
  678. ast_verb(3, "Copying read recording for Mixmonitor %s to voicemail recipients\n", mixmonitor->name);
  679. copy_to_voicemail(mixmonitor, fs_read_ext, mixmonitor->filename_read);
  680. }
  681. if (!ast_strlen_zero(fs_write_ext)) {
  682. ast_verb(3, "Copying write recording for Mixmonitor %s to voicemail recipients\n", mixmonitor->name);
  683. copy_to_voicemail(mixmonitor, fs_write_ext, mixmonitor->filename_write);
  684. }
  685. } else {
  686. ast_debug(3, "No recipients to forward monitor to, moving on.\n");
  687. }
  688. mixmonitor_free(mixmonitor);
  689. ast_module_unref(ast_module_info->self);
  690. return NULL;
  691. }
  692. static int setup_mixmonitor_ds(struct mixmonitor *mixmonitor, struct ast_channel *chan, char **datastore_id, const char *beep_id)
  693. {
  694. struct ast_datastore *datastore = NULL;
  695. struct mixmonitor_ds *mixmonitor_ds;
  696. if (!(mixmonitor_ds = ast_calloc(1, sizeof(*mixmonitor_ds)))) {
  697. return -1;
  698. }
  699. if (ast_asprintf(datastore_id, "%p", mixmonitor_ds) == -1) {
  700. ast_log(LOG_ERROR, "Failed to allocate memory for MixMonitor ID.\n");
  701. }
  702. ast_mutex_init(&mixmonitor_ds->lock);
  703. ast_cond_init(&mixmonitor_ds->destruction_condition, NULL);
  704. if (!(datastore = ast_datastore_alloc(&mixmonitor_ds_info, *datastore_id))) {
  705. ast_mutex_destroy(&mixmonitor_ds->lock);
  706. ast_cond_destroy(&mixmonitor_ds->destruction_condition);
  707. ast_free(mixmonitor_ds);
  708. return -1;
  709. }
  710. ast_channel_lock(mixmonitor->autochan->chan);
  711. if (ast_test_flag(mixmonitor, MUXFLAG_BEEP_START)) {
  712. ast_stream_and_wait(mixmonitor->autochan->chan, "beep", "");
  713. }
  714. ast_channel_unlock(mixmonitor->autochan->chan);
  715. mixmonitor_ds->samp_rate = 8000;
  716. mixmonitor_ds->audiohook = &mixmonitor->audiohook;
  717. mixmonitor_ds->filename = ast_strdup(mixmonitor->filename);
  718. if (!ast_strlen_zero(beep_id)) {
  719. mixmonitor_ds->beep_id = ast_strdup(beep_id);
  720. }
  721. datastore->data = mixmonitor_ds;
  722. ast_channel_lock(chan);
  723. ast_channel_datastore_add(chan, datastore);
  724. ast_channel_unlock(chan);
  725. mixmonitor->mixmonitor_ds = mixmonitor_ds;
  726. return 0;
  727. }
  728. static int launch_monitor_thread(struct ast_channel *chan, const char *filename,
  729. unsigned int flags, int readvol, int writevol,
  730. const char *post_process, const char *filename_write,
  731. char *filename_read, const char *uid_channel_var,
  732. const char *recipients, const char *beep_id)
  733. {
  734. pthread_t thread;
  735. struct mixmonitor *mixmonitor;
  736. char postprocess2[1024] = "";
  737. char *datastore_id = NULL;
  738. postprocess2[0] = 0;
  739. /* If a post process system command is given attach it to the structure */
  740. if (!ast_strlen_zero(post_process)) {
  741. char *p1, *p2;
  742. p1 = ast_strdupa(post_process);
  743. for (p2 = p1; *p2; p2++) {
  744. if (*p2 == '^' && *(p2+1) == '{') {
  745. *p2 = '$';
  746. }
  747. }
  748. ast_channel_lock(chan);
  749. pbx_substitute_variables_helper(chan, p1, postprocess2, sizeof(postprocess2) - 1);
  750. ast_channel_unlock(chan);
  751. }
  752. /* Pre-allocate mixmonitor structure and spy */
  753. if (!(mixmonitor = ast_calloc(1, sizeof(*mixmonitor)))) {
  754. return -1;
  755. }
  756. /* Now that the struct has been calloced, go ahead and initialize the string fields. */
  757. if (ast_string_field_init(mixmonitor, 512)) {
  758. mixmonitor_free(mixmonitor);
  759. return -1;
  760. }
  761. /* Setup the actual spy before creating our thread */
  762. if (ast_audiohook_init(&mixmonitor->audiohook, AST_AUDIOHOOK_TYPE_SPY, mixmonitor_spy_type, 0)) {
  763. mixmonitor_free(mixmonitor);
  764. return -1;
  765. }
  766. /* Copy over flags and channel name */
  767. mixmonitor->flags = flags;
  768. if (!(mixmonitor->autochan = ast_autochan_setup(chan))) {
  769. mixmonitor_free(mixmonitor);
  770. return -1;
  771. }
  772. if (!ast_strlen_zero(filename)) {
  773. mixmonitor->filename = ast_strdup(filename);
  774. }
  775. if (!ast_strlen_zero(filename_write)) {
  776. mixmonitor->filename_write = ast_strdup(filename_write);
  777. }
  778. if (!ast_strlen_zero(filename_read)) {
  779. mixmonitor->filename_read = ast_strdup(filename_read);
  780. }
  781. if (setup_mixmonitor_ds(mixmonitor, chan, &datastore_id, beep_id)) {
  782. ast_autochan_destroy(mixmonitor->autochan);
  783. mixmonitor_free(mixmonitor);
  784. ast_free(datastore_id);
  785. return -1;
  786. }
  787. if (!ast_strlen_zero(uid_channel_var)) {
  788. if (datastore_id) {
  789. pbx_builtin_setvar_helper(chan, uid_channel_var, datastore_id);
  790. }
  791. }
  792. ast_free(datastore_id);
  793. mixmonitor->name = ast_strdup(ast_channel_name(chan));
  794. if (!ast_strlen_zero(postprocess2)) {
  795. mixmonitor->post_process = ast_strdup(postprocess2);
  796. }
  797. if (!ast_strlen_zero(recipients)) {
  798. char callerid[256];
  799. struct ast_party_connected_line *connected;
  800. ast_channel_lock(chan);
  801. /* We use the connected line of the invoking channel for caller ID. */
  802. connected = ast_channel_connected(chan);
  803. ast_debug(3, "Connected Line CID = %d - %s : %d - %s\n", connected->id.name.valid,
  804. connected->id.name.str, connected->id.number.valid,
  805. connected->id.number.str);
  806. ast_callerid_merge(callerid, sizeof(callerid),
  807. S_COR(connected->id.name.valid, connected->id.name.str, NULL),
  808. S_COR(connected->id.number.valid, connected->id.number.str, NULL),
  809. "Unknown");
  810. ast_string_field_set(mixmonitor, call_context, ast_channel_context(chan));
  811. ast_string_field_set(mixmonitor, call_macrocontext, ast_channel_macrocontext(chan));
  812. ast_string_field_set(mixmonitor, call_extension, ast_channel_exten(chan));
  813. ast_string_field_set(mixmonitor, call_callerchan, ast_channel_name(chan));
  814. ast_string_field_set(mixmonitor, call_callerid, callerid);
  815. mixmonitor->call_priority = ast_channel_priority(chan);
  816. ast_channel_unlock(chan);
  817. add_vm_recipients_from_string(mixmonitor, recipients);
  818. }
  819. ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_TRIGGER_SYNC);
  820. if (readvol)
  821. mixmonitor->audiohook.options.read_volume = readvol;
  822. if (writevol)
  823. mixmonitor->audiohook.options.write_volume = writevol;
  824. if (startmon(chan, &mixmonitor->audiohook)) {
  825. ast_log(LOG_WARNING, "Unable to add '%s' spy to channel '%s'\n",
  826. mixmonitor_spy_type, ast_channel_name(chan));
  827. ast_audiohook_destroy(&mixmonitor->audiohook);
  828. mixmonitor_free(mixmonitor);
  829. return -1;
  830. }
  831. /* reference be released at mixmonitor destruction */
  832. mixmonitor->callid = ast_read_threadstorage_callid();
  833. return ast_pthread_create_detached_background(&thread, NULL, mixmonitor_thread, mixmonitor);
  834. }
  835. /* a note on filename_parse: creates directory structure and assigns absolute path from relative paths for filenames */
  836. /* requires immediate copying of string from return to retain data since otherwise it will immediately lose scope */
  837. static char *filename_parse(char *filename, char *buffer, size_t len)
  838. {
  839. char *slash;
  840. if (ast_strlen_zero(filename)) {
  841. ast_log(LOG_WARNING, "No file name was provided for a file save option.\n");
  842. } else if (filename[0] != '/') {
  843. char *build;
  844. build = ast_alloca(strlen(ast_config_AST_MONITOR_DIR) + strlen(filename) + 3);
  845. sprintf(build, "%s/%s", ast_config_AST_MONITOR_DIR, filename);
  846. filename = build;
  847. }
  848. ast_copy_string(buffer, filename, len);
  849. if ((slash = strrchr(filename, '/'))) {
  850. *slash = '\0';
  851. }
  852. ast_mkdir(filename, 0777);
  853. return buffer;
  854. }
  855. static int mixmonitor_exec(struct ast_channel *chan, const char *data)
  856. {
  857. int x, readvol = 0, writevol = 0;
  858. char *filename_read = NULL;
  859. char *filename_write = NULL;
  860. char filename_buffer[1024] = "";
  861. char *uid_channel_var = NULL;
  862. char beep_id[64] = "";
  863. struct ast_flags flags = { 0 };
  864. char *recipients = NULL;
  865. char *parse;
  866. AST_DECLARE_APP_ARGS(args,
  867. AST_APP_ARG(filename);
  868. AST_APP_ARG(options);
  869. AST_APP_ARG(post_process);
  870. );
  871. if (ast_strlen_zero(data)) {
  872. ast_log(LOG_WARNING, "MixMonitor requires an argument (filename or ,t(filename) and/or r(filename)\n");
  873. return -1;
  874. }
  875. parse = ast_strdupa(data);
  876. AST_STANDARD_APP_ARGS(args, parse);
  877. if (args.options) {
  878. char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
  879. ast_app_parse_options(mixmonitor_opts, &flags, opts, args.options);
  880. if (ast_test_flag(&flags, MUXFLAG_READVOLUME)) {
  881. if (ast_strlen_zero(opts[OPT_ARG_READVOLUME])) {
  882. ast_log(LOG_WARNING, "No volume level was provided for the heard volume ('v') option.\n");
  883. } else if ((sscanf(opts[OPT_ARG_READVOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) {
  884. ast_log(LOG_NOTICE, "Heard volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_READVOLUME]);
  885. } else {
  886. readvol = get_volfactor(x);
  887. }
  888. }
  889. if (ast_test_flag(&flags, MUXFLAG_WRITEVOLUME)) {
  890. if (ast_strlen_zero(opts[OPT_ARG_WRITEVOLUME])) {
  891. ast_log(LOG_WARNING, "No volume level was provided for the spoken volume ('V') option.\n");
  892. } else if ((sscanf(opts[OPT_ARG_WRITEVOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) {
  893. ast_log(LOG_NOTICE, "Spoken volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_WRITEVOLUME]);
  894. } else {
  895. writevol = get_volfactor(x);
  896. }
  897. }
  898. if (ast_test_flag(&flags, MUXFLAG_VOLUME)) {
  899. if (ast_strlen_zero(opts[OPT_ARG_VOLUME])) {
  900. ast_log(LOG_WARNING, "No volume level was provided for the combined volume ('W') option.\n");
  901. } else if ((sscanf(opts[OPT_ARG_VOLUME], "%2d", &x) != 1) || (x < -4) || (x > 4)) {
  902. ast_log(LOG_NOTICE, "Combined volume must be a number between -4 and 4, not '%s'\n", opts[OPT_ARG_VOLUME]);
  903. } else {
  904. readvol = writevol = get_volfactor(x);
  905. }
  906. }
  907. if (ast_test_flag(&flags, MUXFLAG_VMRECIPIENTS)) {
  908. if (ast_strlen_zero(opts[OPT_ARG_VMRECIPIENTS])) {
  909. ast_log(LOG_WARNING, "No voicemail recipients were specified for the vm copy ('m') option.\n");
  910. } else {
  911. recipients = ast_strdupa(opts[OPT_ARG_VMRECIPIENTS]);
  912. }
  913. }
  914. if (ast_test_flag(&flags, MUXFLAG_WRITE)) {
  915. filename_write = ast_strdupa(filename_parse(opts[OPT_ARG_WRITENAME], filename_buffer, sizeof(filename_buffer)));
  916. }
  917. if (ast_test_flag(&flags, MUXFLAG_READ)) {
  918. filename_read = ast_strdupa(filename_parse(opts[OPT_ARG_READNAME], filename_buffer, sizeof(filename_buffer)));
  919. }
  920. if (ast_test_flag(&flags, MUXFLAG_UID)) {
  921. uid_channel_var = opts[OPT_ARG_UID];
  922. }
  923. if (ast_test_flag(&flags, MUXFLAG_BEEP)) {
  924. const char *interval_str = S_OR(opts[OPT_ARG_BEEP_INTERVAL], "15");
  925. unsigned int interval = 15;
  926. if (sscanf(interval_str, "%30u", &interval) != 1) {
  927. ast_log(LOG_WARNING, "Invalid interval '%s' for periodic beep. Using default of %u\n",
  928. interval_str, interval);
  929. }
  930. if (ast_beep_start(chan, interval, beep_id, sizeof(beep_id))) {
  931. ast_log(LOG_WARNING, "Unable to enable periodic beep, please ensure func_periodic_hook is loaded.\n");
  932. return -1;
  933. }
  934. }
  935. }
  936. /* If there are no file writing arguments/options for the mix monitor, send a warning message and return -1 */
  937. if (!ast_test_flag(&flags, MUXFLAG_WRITE) && !ast_test_flag(&flags, MUXFLAG_READ) && ast_strlen_zero(args.filename)) {
  938. ast_log(LOG_WARNING, "MixMonitor requires an argument (filename)\n");
  939. return -1;
  940. }
  941. /* If filename exists, try to create directories for it */
  942. if (!(ast_strlen_zero(args.filename))) {
  943. args.filename = ast_strdupa(filename_parse(args.filename, filename_buffer, sizeof(filename_buffer)));
  944. }
  945. pbx_builtin_setvar_helper(chan, "MIXMONITOR_FILENAME", args.filename);
  946. /* If launch_monitor_thread works, the module reference must not be released until it is finished. */
  947. ast_module_ref(ast_module_info->self);
  948. if (launch_monitor_thread(chan,
  949. args.filename,
  950. flags.flags,
  951. readvol,
  952. writevol,
  953. args.post_process,
  954. filename_write,
  955. filename_read,
  956. uid_channel_var,
  957. recipients,
  958. beep_id)) {
  959. ast_module_unref(ast_module_info->self);
  960. }
  961. return 0;
  962. }
  963. static int stop_mixmonitor_full(struct ast_channel *chan, const char *data)
  964. {
  965. struct ast_datastore *datastore = NULL;
  966. char *parse = "";
  967. struct mixmonitor_ds *mixmonitor_ds;
  968. const char *beep_id = NULL;
  969. AST_DECLARE_APP_ARGS(args,
  970. AST_APP_ARG(mixmonid);
  971. );
  972. if (!ast_strlen_zero(data)) {
  973. parse = ast_strdupa(data);
  974. }
  975. AST_STANDARD_APP_ARGS(args, parse);
  976. ast_channel_lock(chan);
  977. datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info,
  978. S_OR(args.mixmonid, NULL));
  979. if (!datastore) {
  980. ast_channel_unlock(chan);
  981. return -1;
  982. }
  983. mixmonitor_ds = datastore->data;
  984. ast_mutex_lock(&mixmonitor_ds->lock);
  985. /* closing the filestream here guarantees the file is available to the dialplan
  986. * after calling StopMixMonitor */
  987. mixmonitor_ds_close_fs(mixmonitor_ds);
  988. /* The mixmonitor thread may be waiting on the audiohook trigger.
  989. * In order to exit from the mixmonitor loop before waiting on channel
  990. * destruction, poke the audiohook trigger. */
  991. if (mixmonitor_ds->audiohook) {
  992. if (mixmonitor_ds->audiohook->status != AST_AUDIOHOOK_STATUS_DONE) {
  993. ast_audiohook_update_status(mixmonitor_ds->audiohook, AST_AUDIOHOOK_STATUS_SHUTDOWN);
  994. }
  995. ast_audiohook_lock(mixmonitor_ds->audiohook);
  996. ast_cond_signal(&mixmonitor_ds->audiohook->trigger);
  997. ast_audiohook_unlock(mixmonitor_ds->audiohook);
  998. mixmonitor_ds->audiohook = NULL;
  999. }
  1000. if (!ast_strlen_zero(mixmonitor_ds->beep_id)) {
  1001. beep_id = ast_strdupa(mixmonitor_ds->beep_id);
  1002. }
  1003. ast_mutex_unlock(&mixmonitor_ds->lock);
  1004. /* Remove the datastore so the monitor thread can exit */
  1005. if (!ast_channel_datastore_remove(chan, datastore)) {
  1006. ast_datastore_free(datastore);
  1007. }
  1008. ast_channel_unlock(chan);
  1009. if (!ast_strlen_zero(beep_id)) {
  1010. ast_beep_stop(chan, beep_id);
  1011. }
  1012. return 0;
  1013. }
  1014. static int stop_mixmonitor_exec(struct ast_channel *chan, const char *data)
  1015. {
  1016. stop_mixmonitor_full(chan, data);
  1017. return 0;
  1018. }
  1019. static char *handle_cli_mixmonitor(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1020. {
  1021. struct ast_channel *chan;
  1022. struct ast_datastore *datastore = NULL;
  1023. struct mixmonitor_ds *mixmonitor_ds = NULL;
  1024. switch (cmd) {
  1025. case CLI_INIT:
  1026. e->command = "mixmonitor {start|stop|list}";
  1027. e->usage =
  1028. "Usage: mixmonitor start <chan_name> [args]\n"
  1029. " The optional arguments are passed to the MixMonitor application.\n"
  1030. " mixmonitor stop <chan_name> [args]\n"
  1031. " The optional arguments are passed to the StopMixMonitor application.\n"
  1032. " mixmonitor list <chan_name>\n";
  1033. return NULL;
  1034. case CLI_GENERATE:
  1035. return ast_complete_channels(a->line, a->word, a->pos, a->n, 2);
  1036. }
  1037. if (a->argc < 3) {
  1038. return CLI_SHOWUSAGE;
  1039. }
  1040. if (!(chan = ast_channel_get_by_name_prefix(a->argv[2], strlen(a->argv[2])))) {
  1041. ast_cli(a->fd, "No channel matching '%s' found.\n", a->argv[2]);
  1042. /* Technically this is a failure, but we don't want 2 errors printing out */
  1043. return CLI_SUCCESS;
  1044. }
  1045. if (!strcasecmp(a->argv[1], "start")) {
  1046. mixmonitor_exec(chan, (a->argc >= 4) ? a->argv[3] : "");
  1047. } else if (!strcasecmp(a->argv[1], "stop")){
  1048. stop_mixmonitor_exec(chan, (a->argc >= 4) ? a->argv[3] : "");
  1049. } else if (!strcasecmp(a->argv[1], "list")) {
  1050. ast_cli(a->fd, "MixMonitor ID\tFile\tReceive File\tTransmit File\n");
  1051. ast_cli(a->fd, "=========================================================================\n");
  1052. ast_channel_lock(chan);
  1053. AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
  1054. if (datastore->info == &mixmonitor_ds_info) {
  1055. char *filename = "";
  1056. char *filename_read = "";
  1057. char *filename_write = "";
  1058. mixmonitor_ds = datastore->data;
  1059. if (mixmonitor_ds->fs) {
  1060. filename = mixmonitor_ds->fs->filename;
  1061. }
  1062. if (mixmonitor_ds->fs_read) {
  1063. filename_read = mixmonitor_ds->fs_read->filename;
  1064. }
  1065. if (mixmonitor_ds->fs_write) {
  1066. filename_write = mixmonitor_ds->fs_write->filename;
  1067. }
  1068. ast_cli(a->fd, "%p\t%s\t%s\t%s\n", mixmonitor_ds, filename, filename_read, filename_write);
  1069. }
  1070. }
  1071. ast_channel_unlock(chan);
  1072. } else {
  1073. chan = ast_channel_unref(chan);
  1074. return CLI_SHOWUSAGE;
  1075. }
  1076. chan = ast_channel_unref(chan);
  1077. return CLI_SUCCESS;
  1078. }
  1079. /*! \brief Mute / unmute a MixMonitor channel */
  1080. static int manager_mute_mixmonitor(struct mansession *s, const struct message *m)
  1081. {
  1082. struct ast_channel *c;
  1083. const char *name = astman_get_header(m, "Channel");
  1084. const char *id = astman_get_header(m, "ActionID");
  1085. const char *state = astman_get_header(m, "State");
  1086. const char *direction = astman_get_header(m,"Direction");
  1087. int clearmute = 1;
  1088. enum ast_audiohook_flags flag;
  1089. if (ast_strlen_zero(direction)) {
  1090. astman_send_error(s, m, "No direction specified. Must be read, write or both");
  1091. return AMI_SUCCESS;
  1092. }
  1093. if (!strcasecmp(direction, "read")) {
  1094. flag = AST_AUDIOHOOK_MUTE_READ;
  1095. } else if (!strcasecmp(direction, "write")) {
  1096. flag = AST_AUDIOHOOK_MUTE_WRITE;
  1097. } else if (!strcasecmp(direction, "both")) {
  1098. flag = AST_AUDIOHOOK_MUTE_READ | AST_AUDIOHOOK_MUTE_WRITE;
  1099. } else {
  1100. astman_send_error(s, m, "Invalid direction specified. Must be read, write or both");
  1101. return AMI_SUCCESS;
  1102. }
  1103. if (ast_strlen_zero(name)) {
  1104. astman_send_error(s, m, "No channel specified");
  1105. return AMI_SUCCESS;
  1106. }
  1107. if (ast_strlen_zero(state)) {
  1108. astman_send_error(s, m, "No state specified");
  1109. return AMI_SUCCESS;
  1110. }
  1111. clearmute = ast_false(state);
  1112. c = ast_channel_get_by_name(name);
  1113. if (!c) {
  1114. astman_send_error(s, m, "No such channel");
  1115. return AMI_SUCCESS;
  1116. }
  1117. if (ast_audiohook_set_mute(c, mixmonitor_spy_type, flag, clearmute)) {
  1118. ast_channel_unref(c);
  1119. astman_send_error(s, m, "Cannot set mute flag");
  1120. return AMI_SUCCESS;
  1121. }
  1122. astman_append(s, "Response: Success\r\n");
  1123. if (!ast_strlen_zero(id)) {
  1124. astman_append(s, "ActionID: %s\r\n", id);
  1125. }
  1126. astman_append(s, "\r\n");
  1127. ast_channel_unref(c);
  1128. return AMI_SUCCESS;
  1129. }
  1130. static int start_mixmonitor_callback(struct ast_channel *chan, const char *filename, const char *options)
  1131. {
  1132. char args[PATH_MAX];
  1133. if (ast_strlen_zero(options)) {
  1134. snprintf(args, sizeof(args), "%s", filename);
  1135. } else {
  1136. snprintf(args, sizeof(args), "%s,%s", filename, options);
  1137. }
  1138. return mixmonitor_exec(chan, args);
  1139. }
  1140. static int stop_mixmonitor_callback(struct ast_channel *chan, const char *mixmonitor_id)
  1141. {
  1142. return stop_mixmonitor_full(chan, mixmonitor_id);
  1143. }
  1144. static int manager_mixmonitor(struct mansession *s, const struct message *m)
  1145. {
  1146. struct ast_channel *c;
  1147. const char *name = astman_get_header(m, "Channel");
  1148. const char *id = astman_get_header(m, "ActionID");
  1149. const char *file = astman_get_header(m, "File");
  1150. const char *options = astman_get_header(m, "Options");
  1151. const char *command = astman_get_header(m, "Command");
  1152. char *opts[OPT_ARG_ARRAY_SIZE] = { NULL, };
  1153. struct ast_flags flags = { 0 };
  1154. char *uid_channel_var = NULL;
  1155. const char *mixmonitor_id = NULL;
  1156. int res;
  1157. char args[PATH_MAX];
  1158. if (ast_strlen_zero(name)) {
  1159. astman_send_error(s, m, "No channel specified");
  1160. return AMI_SUCCESS;
  1161. }
  1162. c = ast_channel_get_by_name(name);
  1163. if (!c) {
  1164. astman_send_error(s, m, "No such channel");
  1165. return AMI_SUCCESS;
  1166. }
  1167. if (!ast_strlen_zero(options)) {
  1168. ast_app_parse_options(mixmonitor_opts, &flags, opts, ast_strdupa(options));
  1169. }
  1170. snprintf(args, sizeof(args), "%s,%s,%s", file, options, command);
  1171. res = mixmonitor_exec(c, args);
  1172. if (ast_test_flag(&flags, MUXFLAG_UID)) {
  1173. uid_channel_var = opts[OPT_ARG_UID];
  1174. ast_channel_lock(c);
  1175. mixmonitor_id = pbx_builtin_getvar_helper(c, uid_channel_var);
  1176. mixmonitor_id = ast_strdupa(S_OR(mixmonitor_id, ""));
  1177. ast_channel_unlock(c);
  1178. }
  1179. if (res) {
  1180. ast_channel_unref(c);
  1181. astman_send_error(s, m, "Could not start monitoring channel");
  1182. return AMI_SUCCESS;
  1183. }
  1184. astman_append(s, "Response: Success\r\n");
  1185. if (!ast_strlen_zero(id)) {
  1186. astman_append(s, "ActionID: %s\r\n", id);
  1187. }
  1188. if (!ast_strlen_zero(mixmonitor_id)) {
  1189. astman_append(s, "MixMonitorID: %s\r\n", mixmonitor_id);
  1190. }
  1191. astman_append(s, "\r\n");
  1192. ast_channel_unref(c);
  1193. return AMI_SUCCESS;
  1194. }
  1195. static int manager_stop_mixmonitor(struct mansession *s, const struct message *m)
  1196. {
  1197. struct ast_channel *c;
  1198. const char *name = astman_get_header(m, "Channel");
  1199. const char *id = astman_get_header(m, "ActionID");
  1200. const char *mixmonitor_id = astman_get_header(m, "MixMonitorID");
  1201. int res;
  1202. if (ast_strlen_zero(name)) {
  1203. astman_send_error(s, m, "No channel specified");
  1204. return AMI_SUCCESS;
  1205. }
  1206. c = ast_channel_get_by_name(name);
  1207. if (!c) {
  1208. astman_send_error(s, m, "No such channel");
  1209. return AMI_SUCCESS;
  1210. }
  1211. res = stop_mixmonitor_full(c, mixmonitor_id);
  1212. if (res) {
  1213. ast_channel_unref(c);
  1214. astman_send_error(s, m, "Could not stop monitoring channel");
  1215. return AMI_SUCCESS;
  1216. }
  1217. astman_append(s, "Response: Success\r\n");
  1218. if (!ast_strlen_zero(id)) {
  1219. astman_append(s, "ActionID: %s\r\n", id);
  1220. }
  1221. astman_append(s, "\r\n");
  1222. ast_channel_unref(c);
  1223. return AMI_SUCCESS;
  1224. }
  1225. static int func_mixmonitor_read(struct ast_channel *chan, const char *cmd, char *data,
  1226. char *buf, size_t len)
  1227. {
  1228. struct ast_datastore *datastore;
  1229. struct mixmonitor_ds *ds_data;
  1230. AST_DECLARE_APP_ARGS(args,
  1231. AST_APP_ARG(id);
  1232. AST_APP_ARG(key);
  1233. );
  1234. AST_STANDARD_APP_ARGS(args, data);
  1235. if (ast_strlen_zero(args.id) || ast_strlen_zero(args.key)) {
  1236. ast_log(LOG_WARNING, "Not enough arguments provided to %s. "
  1237. "An ID and key must be provided\n", cmd);
  1238. return -1;
  1239. }
  1240. ast_channel_lock(chan);
  1241. datastore = ast_channel_datastore_find(chan, &mixmonitor_ds_info, args.id);
  1242. ast_channel_unlock(chan);
  1243. if (!datastore) {
  1244. ast_log(LOG_WARNING, "Could not find MixMonitor with ID %s\n", args.id);
  1245. return -1;
  1246. }
  1247. ds_data = datastore->data;
  1248. if (!strcasecmp(args.key, "filename")) {
  1249. ast_copy_string(buf, ds_data->filename, len);
  1250. } else {
  1251. ast_log(LOG_WARNING, "Unrecognized %s option %s\n", cmd, args.key);
  1252. return -1;
  1253. }
  1254. return 0;
  1255. }
  1256. static struct ast_custom_function mixmonitor_function = {
  1257. .name = "MIXMONITOR",
  1258. .read = func_mixmonitor_read,
  1259. };
  1260. static struct ast_cli_entry cli_mixmonitor[] = {
  1261. AST_CLI_DEFINE(handle_cli_mixmonitor, "Execute a MixMonitor command")
  1262. };
  1263. static int set_mixmonitor_methods(void)
  1264. {
  1265. struct ast_mixmonitor_methods mixmonitor_methods = {
  1266. .start = start_mixmonitor_callback,
  1267. .stop = stop_mixmonitor_callback,
  1268. };
  1269. return ast_set_mixmonitor_methods(&mixmonitor_methods);
  1270. }
  1271. static int clear_mixmonitor_methods(void)
  1272. {
  1273. return ast_clear_mixmonitor_methods();
  1274. }
  1275. static int unload_module(void)
  1276. {
  1277. int res;
  1278. ast_cli_unregister_multiple(cli_mixmonitor, ARRAY_LEN(cli_mixmonitor));
  1279. res = ast_unregister_application(stop_app);
  1280. res |= ast_unregister_application(app);
  1281. res |= ast_manager_unregister("MixMonitorMute");
  1282. res |= ast_manager_unregister("MixMonitor");
  1283. res |= ast_manager_unregister("StopMixMonitor");
  1284. res |= ast_custom_function_unregister(&mixmonitor_function);
  1285. res |= clear_mixmonitor_methods();
  1286. return res;
  1287. }
  1288. static int load_module(void)
  1289. {
  1290. int res;
  1291. ast_cli_register_multiple(cli_mixmonitor, ARRAY_LEN(cli_mixmonitor));
  1292. res = ast_register_application_xml(app, mixmonitor_exec);
  1293. res |= ast_register_application_xml(stop_app, stop_mixmonitor_exec);
  1294. res |= ast_manager_register_xml("MixMonitorMute", EVENT_FLAG_SYSTEM | EVENT_FLAG_CALL, manager_mute_mixmonitor);
  1295. res |= ast_manager_register_xml("MixMonitor", EVENT_FLAG_SYSTEM, manager_mixmonitor);
  1296. res |= ast_manager_register_xml("StopMixMonitor", EVENT_FLAG_SYSTEM | EVENT_FLAG_CALL, manager_stop_mixmonitor);
  1297. res |= ast_custom_function_register(&mixmonitor_function);
  1298. res |= set_mixmonitor_methods();
  1299. return res;
  1300. }
  1301. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mixed Audio Monitoring Application");